1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
4 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
5 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
7 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
8 * and Nettle, by Niels Möller.
11 #ifndef _CRYPTO_INTERNAL_CIPHER_H
12 #define _CRYPTO_INTERNAL_CIPHER_H
14 #include <crypto/algapi.h>
16 struct crypto_cipher
{
17 struct crypto_tfm base
;
21 * DOC: Single Block Cipher API
23 * The single block cipher API is used with the ciphers of type
24 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
26 * Using the single block cipher API calls, operations with the basic cipher
27 * primitive can be implemented. These cipher primitives exclude any block
28 * chaining operations including IV handling.
30 * The purpose of this single block cipher API is to support the implementation
31 * of templates or other concepts that only need to perform the cipher operation
32 * on one block at a time. Templates invoke the underlying cipher primitive
33 * block-wise and process either the input or the output data of these cipher
37 static inline struct crypto_cipher
*__crypto_cipher_cast(struct crypto_tfm
*tfm
)
39 return (struct crypto_cipher
*)tfm
;
43 * crypto_alloc_cipher() - allocate single block cipher handle
44 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
46 * @type: specifies the type of the cipher
47 * @mask: specifies the mask for the cipher
49 * Allocate a cipher handle for a single block cipher. The returned struct
50 * crypto_cipher is the cipher handle that is required for any subsequent API
51 * invocation for that single block cipher.
53 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
54 * of an error, PTR_ERR() returns the error code.
56 static inline struct crypto_cipher
*crypto_alloc_cipher(const char *alg_name
,
59 type
&= ~CRYPTO_ALG_TYPE_MASK
;
60 type
|= CRYPTO_ALG_TYPE_CIPHER
;
61 mask
|= CRYPTO_ALG_TYPE_MASK
;
63 return __crypto_cipher_cast(crypto_alloc_base(alg_name
, type
, mask
));
66 static inline struct crypto_tfm
*crypto_cipher_tfm(struct crypto_cipher
*tfm
)
72 * crypto_free_cipher() - zeroize and free the single block cipher handle
73 * @tfm: cipher handle to be freed
75 static inline void crypto_free_cipher(struct crypto_cipher
*tfm
)
77 crypto_free_tfm(crypto_cipher_tfm(tfm
));
81 * crypto_has_cipher() - Search for the availability of a single block cipher
82 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
84 * @type: specifies the type of the cipher
85 * @mask: specifies the mask for the cipher
87 * Return: true when the single block cipher is known to the kernel crypto API;
90 static inline int crypto_has_cipher(const char *alg_name
, u32 type
, u32 mask
)
92 type
&= ~CRYPTO_ALG_TYPE_MASK
;
93 type
|= CRYPTO_ALG_TYPE_CIPHER
;
94 mask
|= CRYPTO_ALG_TYPE_MASK
;
96 return crypto_has_alg(alg_name
, type
, mask
);
100 * crypto_cipher_blocksize() - obtain block size for cipher
101 * @tfm: cipher handle
103 * The block size for the single block cipher referenced with the cipher handle
104 * tfm is returned. The caller may use that information to allocate appropriate
105 * memory for the data returned by the encryption or decryption operation
107 * Return: block size of cipher
109 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher
*tfm
)
111 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm
));
114 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher
*tfm
)
116 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm
));
119 static inline u32
crypto_cipher_get_flags(struct crypto_cipher
*tfm
)
121 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm
));
124 static inline void crypto_cipher_set_flags(struct crypto_cipher
*tfm
,
127 crypto_tfm_set_flags(crypto_cipher_tfm(tfm
), flags
);
130 static inline void crypto_cipher_clear_flags(struct crypto_cipher
*tfm
,
133 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm
), flags
);
137 * crypto_cipher_setkey() - set key for cipher
138 * @tfm: cipher handle
139 * @key: buffer holding the key
140 * @keylen: length of the key in bytes
142 * The caller provided key is set for the single block cipher referenced by the
145 * Note, the key length determines the cipher type. Many block ciphers implement
146 * different cipher modes depending on the key size, such as AES-128 vs AES-192
147 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
150 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
152 int crypto_cipher_setkey(struct crypto_cipher
*tfm
,
153 const u8
*key
, unsigned int keylen
);
156 * crypto_cipher_encrypt_one() - encrypt one block of plaintext
157 * @tfm: cipher handle
158 * @dst: points to the buffer that will be filled with the ciphertext
159 * @src: buffer holding the plaintext to be encrypted
161 * Invoke the encryption operation of one block. The caller must ensure that
162 * the plaintext and ciphertext buffers are at least one block in size.
164 void crypto_cipher_encrypt_one(struct crypto_cipher
*tfm
,
165 u8
*dst
, const u8
*src
);
168 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
169 * @tfm: cipher handle
170 * @dst: points to the buffer that will be filled with the plaintext
171 * @src: buffer holding the ciphertext to be decrypted
173 * Invoke the decryption operation of one block. The caller must ensure that
174 * the plaintext and ciphertext buffers are at least one block in size.
176 void crypto_cipher_decrypt_one(struct crypto_cipher
*tfm
,
177 u8
*dst
, const u8
*src
);
179 struct crypto_cipher
*crypto_clone_cipher(struct crypto_cipher
*cipher
);
181 struct crypto_cipher_spawn
{
182 struct crypto_spawn base
;
185 static inline int crypto_grab_cipher(struct crypto_cipher_spawn
*spawn
,
186 struct crypto_instance
*inst
,
187 const char *name
, u32 type
, u32 mask
)
189 type
&= ~CRYPTO_ALG_TYPE_MASK
;
190 type
|= CRYPTO_ALG_TYPE_CIPHER
;
191 mask
|= CRYPTO_ALG_TYPE_MASK
;
192 return crypto_grab_spawn(&spawn
->base
, inst
, name
, type
, mask
);
195 static inline void crypto_drop_cipher(struct crypto_cipher_spawn
*spawn
)
197 crypto_drop_spawn(&spawn
->base
);
200 static inline struct crypto_alg
*crypto_spawn_cipher_alg(
201 struct crypto_cipher_spawn
*spawn
)
203 return spawn
->base
.alg
;
206 static inline struct crypto_cipher
*crypto_spawn_cipher(
207 struct crypto_cipher_spawn
*spawn
)
209 u32 type
= CRYPTO_ALG_TYPE_CIPHER
;
210 u32 mask
= CRYPTO_ALG_TYPE_MASK
;
212 return __crypto_cipher_cast(crypto_spawn_tfm(&spawn
->base
, type
, mask
));
215 static inline struct cipher_alg
*crypto_cipher_alg(struct crypto_cipher
*tfm
)
217 return &crypto_cipher_tfm(tfm
)->__crt_alg
->cra_cipher
;