1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
4*4882a593Smuzhiyun * Copyright (c) 2002 David S. Miller (davem@redhat.com)
5*4882a593Smuzhiyun * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
8*4882a593Smuzhiyun * and Nettle, by Niels Möller.
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #ifndef _CRYPTO_INTERNAL_CIPHER_H
12*4882a593Smuzhiyun #define _CRYPTO_INTERNAL_CIPHER_H
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #include <crypto/algapi.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun struct crypto_cipher {
17*4882a593Smuzhiyun struct crypto_tfm base;
18*4882a593Smuzhiyun };
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun /**
21*4882a593Smuzhiyun * DOC: Single Block Cipher API
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * The single block cipher API is used with the ciphers of type
24*4882a593Smuzhiyun * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
25*4882a593Smuzhiyun *
26*4882a593Smuzhiyun * Using the single block cipher API calls, operations with the basic cipher
27*4882a593Smuzhiyun * primitive can be implemented. These cipher primitives exclude any block
28*4882a593Smuzhiyun * chaining operations including IV handling.
29*4882a593Smuzhiyun *
30*4882a593Smuzhiyun * The purpose of this single block cipher API is to support the implementation
31*4882a593Smuzhiyun * of templates or other concepts that only need to perform the cipher operation
32*4882a593Smuzhiyun * on one block at a time. Templates invoke the underlying cipher primitive
33*4882a593Smuzhiyun * block-wise and process either the input or the output data of these cipher
34*4882a593Smuzhiyun * operations.
35*4882a593Smuzhiyun */
36*4882a593Smuzhiyun
__crypto_cipher_cast(struct crypto_tfm * tfm)37*4882a593Smuzhiyun static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun return (struct crypto_cipher *)tfm;
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun /**
43*4882a593Smuzhiyun * crypto_alloc_cipher() - allocate single block cipher handle
44*4882a593Smuzhiyun * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
45*4882a593Smuzhiyun * single block cipher
46*4882a593Smuzhiyun * @type: specifies the type of the cipher
47*4882a593Smuzhiyun * @mask: specifies the mask for the cipher
48*4882a593Smuzhiyun *
49*4882a593Smuzhiyun * Allocate a cipher handle for a single block cipher. The returned struct
50*4882a593Smuzhiyun * crypto_cipher is the cipher handle that is required for any subsequent API
51*4882a593Smuzhiyun * invocation for that single block cipher.
52*4882a593Smuzhiyun *
53*4882a593Smuzhiyun * Return: allocated cipher handle in case of success; IS_ERR() is true in case
54*4882a593Smuzhiyun * of an error, PTR_ERR() returns the error code.
55*4882a593Smuzhiyun */
crypto_alloc_cipher(const char * alg_name,u32 type,u32 mask)56*4882a593Smuzhiyun static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
57*4882a593Smuzhiyun u32 type, u32 mask)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun type &= ~CRYPTO_ALG_TYPE_MASK;
60*4882a593Smuzhiyun type |= CRYPTO_ALG_TYPE_CIPHER;
61*4882a593Smuzhiyun mask |= CRYPTO_ALG_TYPE_MASK;
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun
crypto_cipher_tfm(struct crypto_cipher * tfm)66*4882a593Smuzhiyun static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun return &tfm->base;
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /**
72*4882a593Smuzhiyun * crypto_free_cipher() - zeroize and free the single block cipher handle
73*4882a593Smuzhiyun * @tfm: cipher handle to be freed
74*4882a593Smuzhiyun */
crypto_free_cipher(struct crypto_cipher * tfm)75*4882a593Smuzhiyun static inline void crypto_free_cipher(struct crypto_cipher *tfm)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun crypto_free_tfm(crypto_cipher_tfm(tfm));
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun /**
81*4882a593Smuzhiyun * crypto_has_cipher() - Search for the availability of a single block cipher
82*4882a593Smuzhiyun * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
83*4882a593Smuzhiyun * single block cipher
84*4882a593Smuzhiyun * @type: specifies the type of the cipher
85*4882a593Smuzhiyun * @mask: specifies the mask for the cipher
86*4882a593Smuzhiyun *
87*4882a593Smuzhiyun * Return: true when the single block cipher is known to the kernel crypto API;
88*4882a593Smuzhiyun * false otherwise
89*4882a593Smuzhiyun */
crypto_has_cipher(const char * alg_name,u32 type,u32 mask)90*4882a593Smuzhiyun static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun type &= ~CRYPTO_ALG_TYPE_MASK;
93*4882a593Smuzhiyun type |= CRYPTO_ALG_TYPE_CIPHER;
94*4882a593Smuzhiyun mask |= CRYPTO_ALG_TYPE_MASK;
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun return crypto_has_alg(alg_name, type, mask);
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun /**
100*4882a593Smuzhiyun * crypto_cipher_blocksize() - obtain block size for cipher
101*4882a593Smuzhiyun * @tfm: cipher handle
102*4882a593Smuzhiyun *
103*4882a593Smuzhiyun * The block size for the single block cipher referenced with the cipher handle
104*4882a593Smuzhiyun * tfm is returned. The caller may use that information to allocate appropriate
105*4882a593Smuzhiyun * memory for the data returned by the encryption or decryption operation
106*4882a593Smuzhiyun *
107*4882a593Smuzhiyun * Return: block size of cipher
108*4882a593Smuzhiyun */
crypto_cipher_blocksize(struct crypto_cipher * tfm)109*4882a593Smuzhiyun static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun
crypto_cipher_alignmask(struct crypto_cipher * tfm)114*4882a593Smuzhiyun static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun
crypto_cipher_get_flags(struct crypto_cipher * tfm)119*4882a593Smuzhiyun static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun
crypto_cipher_set_flags(struct crypto_cipher * tfm,u32 flags)124*4882a593Smuzhiyun static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
125*4882a593Smuzhiyun u32 flags)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun
crypto_cipher_clear_flags(struct crypto_cipher * tfm,u32 flags)130*4882a593Smuzhiyun static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
131*4882a593Smuzhiyun u32 flags)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun /**
137*4882a593Smuzhiyun * crypto_cipher_setkey() - set key for cipher
138*4882a593Smuzhiyun * @tfm: cipher handle
139*4882a593Smuzhiyun * @key: buffer holding the key
140*4882a593Smuzhiyun * @keylen: length of the key in bytes
141*4882a593Smuzhiyun *
142*4882a593Smuzhiyun * The caller provided key is set for the single block cipher referenced by the
143*4882a593Smuzhiyun * cipher handle.
144*4882a593Smuzhiyun *
145*4882a593Smuzhiyun * Note, the key length determines the cipher type. Many block ciphers implement
146*4882a593Smuzhiyun * different cipher modes depending on the key size, such as AES-128 vs AES-192
147*4882a593Smuzhiyun * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
148*4882a593Smuzhiyun * is performed.
149*4882a593Smuzhiyun *
150*4882a593Smuzhiyun * Return: 0 if the setting of the key was successful; < 0 if an error occurred
151*4882a593Smuzhiyun */
152*4882a593Smuzhiyun int crypto_cipher_setkey(struct crypto_cipher *tfm,
153*4882a593Smuzhiyun const u8 *key, unsigned int keylen);
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun /**
156*4882a593Smuzhiyun * crypto_cipher_encrypt_one() - encrypt one block of plaintext
157*4882a593Smuzhiyun * @tfm: cipher handle
158*4882a593Smuzhiyun * @dst: points to the buffer that will be filled with the ciphertext
159*4882a593Smuzhiyun * @src: buffer holding the plaintext to be encrypted
160*4882a593Smuzhiyun *
161*4882a593Smuzhiyun * Invoke the encryption operation of one block. The caller must ensure that
162*4882a593Smuzhiyun * the plaintext and ciphertext buffers are at least one block in size.
163*4882a593Smuzhiyun */
164*4882a593Smuzhiyun void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
165*4882a593Smuzhiyun u8 *dst, const u8 *src);
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun /**
168*4882a593Smuzhiyun * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
169*4882a593Smuzhiyun * @tfm: cipher handle
170*4882a593Smuzhiyun * @dst: points to the buffer that will be filled with the plaintext
171*4882a593Smuzhiyun * @src: buffer holding the ciphertext to be decrypted
172*4882a593Smuzhiyun *
173*4882a593Smuzhiyun * Invoke the decryption operation of one block. The caller must ensure that
174*4882a593Smuzhiyun * the plaintext and ciphertext buffers are at least one block in size.
175*4882a593Smuzhiyun */
176*4882a593Smuzhiyun void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
177*4882a593Smuzhiyun u8 *dst, const u8 *src);
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun struct crypto_cipher_spawn {
180*4882a593Smuzhiyun struct crypto_spawn base;
181*4882a593Smuzhiyun };
182*4882a593Smuzhiyun
crypto_grab_cipher(struct crypto_cipher_spawn * spawn,struct crypto_instance * inst,const char * name,u32 type,u32 mask)183*4882a593Smuzhiyun static inline int crypto_grab_cipher(struct crypto_cipher_spawn *spawn,
184*4882a593Smuzhiyun struct crypto_instance *inst,
185*4882a593Smuzhiyun const char *name, u32 type, u32 mask)
186*4882a593Smuzhiyun {
187*4882a593Smuzhiyun type &= ~CRYPTO_ALG_TYPE_MASK;
188*4882a593Smuzhiyun type |= CRYPTO_ALG_TYPE_CIPHER;
189*4882a593Smuzhiyun mask |= CRYPTO_ALG_TYPE_MASK;
190*4882a593Smuzhiyun return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun
crypto_drop_cipher(struct crypto_cipher_spawn * spawn)193*4882a593Smuzhiyun static inline void crypto_drop_cipher(struct crypto_cipher_spawn *spawn)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun crypto_drop_spawn(&spawn->base);
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun
crypto_spawn_cipher_alg(struct crypto_cipher_spawn * spawn)198*4882a593Smuzhiyun static inline struct crypto_alg *crypto_spawn_cipher_alg(
199*4882a593Smuzhiyun struct crypto_cipher_spawn *spawn)
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun return spawn->base.alg;
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun
crypto_spawn_cipher(struct crypto_cipher_spawn * spawn)204*4882a593Smuzhiyun static inline struct crypto_cipher *crypto_spawn_cipher(
205*4882a593Smuzhiyun struct crypto_cipher_spawn *spawn)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun u32 type = CRYPTO_ALG_TYPE_CIPHER;
208*4882a593Smuzhiyun u32 mask = CRYPTO_ALG_TYPE_MASK;
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun return __crypto_cipher_cast(crypto_spawn_tfm(&spawn->base, type, mask));
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun
crypto_cipher_alg(struct crypto_cipher * tfm)213*4882a593Smuzhiyun static inline struct cipher_alg *crypto_cipher_alg(struct crypto_cipher *tfm)
214*4882a593Smuzhiyun {
215*4882a593Smuzhiyun return &crypto_cipher_tfm(tfm)->__crt_alg->cra_cipher;
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun #endif
219