Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Scatterlist Cryptographic API. | |
3 | * | |
4 | * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> | |
5 | * Copyright (c) 2002 David S. Miller (davem@redhat.com) | |
5cb1454b | 6 | * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au> |
1da177e4 LT |
7 | * |
8 | * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no> | |
18735dd8 | 9 | * and Nettle, by Niels Möller. |
1da177e4 LT |
10 | * |
11 | * This program is free software; you can redistribute it and/or modify it | |
12 | * under the terms of the GNU General Public License as published by the Free | |
13 | * Software Foundation; either version 2 of the License, or (at your option) | |
14 | * any later version. | |
15 | * | |
16 | */ | |
17 | #ifndef _LINUX_CRYPTO_H | |
18 | #define _LINUX_CRYPTO_H | |
19 | ||
60063497 | 20 | #include <linux/atomic.h> |
1da177e4 | 21 | #include <linux/kernel.h> |
1da177e4 | 22 | #include <linux/list.h> |
187f1882 | 23 | #include <linux/bug.h> |
79911102 | 24 | #include <linux/slab.h> |
1da177e4 | 25 | #include <linux/string.h> |
79911102 | 26 | #include <linux/uaccess.h> |
ada69a16 | 27 | #include <linux/completion.h> |
1da177e4 | 28 | |
5d26a105 KC |
29 | /* |
30 | * Autoloaded crypto modules should only use a prefixed name to avoid allowing | |
31 | * arbitrary modules to be loaded. Loading from userspace may still need the | |
32 | * unprefixed names, so retains those aliases as well. | |
33 | * This uses __MODULE_INFO directly instead of MODULE_ALIAS because pre-4.3 | |
34 | * gcc (e.g. avr32 toolchain) uses __LINE__ for uniqueness, and this macro | |
35 | * expands twice on the same line. Instead, use a separate base name for the | |
36 | * alias. | |
37 | */ | |
38 | #define MODULE_ALIAS_CRYPTO(name) \ | |
39 | __MODULE_INFO(alias, alias_userspace, name); \ | |
40 | __MODULE_INFO(alias, alias_crypto, "crypto-" name) | |
41 | ||
1da177e4 LT |
42 | /* |
43 | * Algorithm masks and types. | |
44 | */ | |
2825982d | 45 | #define CRYPTO_ALG_TYPE_MASK 0x0000000f |
1da177e4 | 46 | #define CRYPTO_ALG_TYPE_CIPHER 0x00000001 |
004a403c LH |
47 | #define CRYPTO_ALG_TYPE_COMPRESS 0x00000002 |
48 | #define CRYPTO_ALG_TYPE_AEAD 0x00000003 | |
055bcee3 | 49 | #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004 |
332f8840 | 50 | #define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005 |
4e6c3df4 | 51 | #define CRYPTO_ALG_TYPE_SKCIPHER 0x00000005 |
61da88e2 | 52 | #define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006 |
4e5f2c40 | 53 | #define CRYPTO_ALG_TYPE_KPP 0x00000008 |
2ebda74f | 54 | #define CRYPTO_ALG_TYPE_ACOMPRESS 0x0000000a |
1ab53a77 | 55 | #define CRYPTO_ALG_TYPE_SCOMPRESS 0x0000000b |
17f0f4a4 | 56 | #define CRYPTO_ALG_TYPE_RNG 0x0000000c |
3c339ab8 | 57 | #define CRYPTO_ALG_TYPE_AKCIPHER 0x0000000d |
63044c4f GC |
58 | #define CRYPTO_ALG_TYPE_DIGEST 0x0000000e |
59 | #define CRYPTO_ALG_TYPE_HASH 0x0000000e | |
60 | #define CRYPTO_ALG_TYPE_SHASH 0x0000000e | |
61 | #define CRYPTO_ALG_TYPE_AHASH 0x0000000f | |
055bcee3 HX |
62 | |
63 | #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e | |
63044c4f | 64 | #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000e |
332f8840 | 65 | #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c |
1ab53a77 | 66 | #define CRYPTO_ALG_TYPE_ACOMPRESS_MASK 0x0000000e |
1da177e4 | 67 | |
2825982d | 68 | #define CRYPTO_ALG_LARVAL 0x00000010 |
6bfd4809 HX |
69 | #define CRYPTO_ALG_DEAD 0x00000020 |
70 | #define CRYPTO_ALG_DYING 0x00000040 | |
f3f632d6 | 71 | #define CRYPTO_ALG_ASYNC 0x00000080 |
2825982d | 72 | |
6010439f HX |
73 | /* |
74 | * Set this bit if and only if the algorithm requires another algorithm of | |
75 | * the same type to handle corner cases. | |
76 | */ | |
77 | #define CRYPTO_ALG_NEED_FALLBACK 0x00000100 | |
78 | ||
ecfc4329 HX |
79 | /* |
80 | * This bit is set for symmetric key ciphers that have already been wrapped | |
81 | * with a generic IV generator to prevent them from being wrapped again. | |
82 | */ | |
83 | #define CRYPTO_ALG_GENIV 0x00000200 | |
84 | ||
73d3864a HX |
85 | /* |
86 | * Set if the algorithm has passed automated run-time testing. Note that | |
87 | * if there is no run-time testing for a given algorithm it is considered | |
88 | * to have passed. | |
89 | */ | |
90 | ||
91 | #define CRYPTO_ALG_TESTED 0x00000400 | |
92 | ||
64a947b1 | 93 | /* |
864e0981 | 94 | * Set if the algorithm is an instance that is built from templates. |
64a947b1 SK |
95 | */ |
96 | #define CRYPTO_ALG_INSTANCE 0x00000800 | |
97 | ||
d912bb76 NM |
98 | /* Set this bit if the algorithm provided is hardware accelerated but |
99 | * not available to userspace via instruction set or so. | |
100 | */ | |
101 | #define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000 | |
102 | ||
06ca7f68 SM |
103 | /* |
104 | * Mark a cipher as a service implementation only usable by another | |
105 | * cipher and never by a normal user of the kernel crypto API | |
106 | */ | |
107 | #define CRYPTO_ALG_INTERNAL 0x00002000 | |
108 | ||
a208fa8f EB |
109 | /* |
110 | * Set if the algorithm has a ->setkey() method but can be used without | |
111 | * calling it first, i.e. there is a default key. | |
112 | */ | |
113 | #define CRYPTO_ALG_OPTIONAL_KEY 0x00004000 | |
114 | ||
1da177e4 LT |
115 | /* |
116 | * Transform masks and values (for crt_flags). | |
117 | */ | |
9fa68f62 EB |
118 | #define CRYPTO_TFM_NEED_KEY 0x00000001 |
119 | ||
1da177e4 LT |
120 | #define CRYPTO_TFM_REQ_MASK 0x000fff00 |
121 | #define CRYPTO_TFM_RES_MASK 0xfff00000 | |
122 | ||
1da177e4 | 123 | #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100 |
64baf3cf | 124 | #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200 |
32e3983f | 125 | #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400 |
1da177e4 LT |
126 | #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000 |
127 | #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000 | |
128 | #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000 | |
129 | #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000 | |
130 | #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000 | |
131 | ||
132 | /* | |
133 | * Miscellaneous stuff. | |
134 | */ | |
f437a3f4 | 135 | #define CRYPTO_MAX_ALG_NAME 128 |
1da177e4 | 136 | |
79911102 HX |
137 | /* |
138 | * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual | |
139 | * declaration) is used to ensure that the crypto_tfm context structure is | |
140 | * aligned correctly for the given architecture so that there are no alignment | |
141 | * faults for C data types. In particular, this is required on platforms such | |
142 | * as arm where pointers are 32-bit aligned but there are data types such as | |
143 | * u64 which require 64-bit alignment. | |
144 | */ | |
79911102 | 145 | #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN |
79911102 | 146 | |
79911102 | 147 | #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN))) |
79911102 | 148 | |
1da177e4 | 149 | struct scatterlist; |
32e3983f HX |
150 | struct crypto_ablkcipher; |
151 | struct crypto_async_request; | |
5cde0af2 | 152 | struct crypto_blkcipher; |
40725181 | 153 | struct crypto_tfm; |
e853c3cf | 154 | struct crypto_type; |
61da88e2 | 155 | struct skcipher_givcrypt_request; |
40725181 | 156 | |
32e3983f HX |
157 | typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err); |
158 | ||
0d7f488f SM |
159 | /** |
160 | * DOC: Block Cipher Context Data Structures | |
161 | * | |
162 | * These data structures define the operating context for each block cipher | |
163 | * type. | |
164 | */ | |
165 | ||
32e3983f HX |
166 | struct crypto_async_request { |
167 | struct list_head list; | |
168 | crypto_completion_t complete; | |
169 | void *data; | |
170 | struct crypto_tfm *tfm; | |
171 | ||
172 | u32 flags; | |
173 | }; | |
174 | ||
175 | struct ablkcipher_request { | |
176 | struct crypto_async_request base; | |
177 | ||
178 | unsigned int nbytes; | |
179 | ||
180 | void *info; | |
181 | ||
182 | struct scatterlist *src; | |
183 | struct scatterlist *dst; | |
184 | ||
185 | void *__ctx[] CRYPTO_MINALIGN_ATTR; | |
186 | }; | |
187 | ||
5cde0af2 HX |
188 | struct blkcipher_desc { |
189 | struct crypto_blkcipher *tfm; | |
190 | void *info; | |
191 | u32 flags; | |
192 | }; | |
193 | ||
40725181 HX |
194 | struct cipher_desc { |
195 | struct crypto_tfm *tfm; | |
6c2bb98b | 196 | void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); |
40725181 HX |
197 | unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst, |
198 | const u8 *src, unsigned int nbytes); | |
199 | void *info; | |
200 | }; | |
1da177e4 | 201 | |
0d7f488f SM |
202 | /** |
203 | * DOC: Block Cipher Algorithm Definitions | |
204 | * | |
205 | * These data structures define modular crypto algorithm implementations, | |
206 | * managed via crypto_register_alg() and crypto_unregister_alg(). | |
207 | */ | |
208 | ||
209 | /** | |
210 | * struct ablkcipher_alg - asynchronous block cipher definition | |
211 | * @min_keysize: Minimum key size supported by the transformation. This is the | |
212 | * smallest key length supported by this transformation algorithm. | |
213 | * This must be set to one of the pre-defined values as this is | |
214 | * not hardware specific. Possible values for this field can be | |
215 | * found via git grep "_MIN_KEY_SIZE" include/crypto/ | |
216 | * @max_keysize: Maximum key size supported by the transformation. This is the | |
217 | * largest key length supported by this transformation algorithm. | |
218 | * This must be set to one of the pre-defined values as this is | |
219 | * not hardware specific. Possible values for this field can be | |
220 | * found via git grep "_MAX_KEY_SIZE" include/crypto/ | |
221 | * @setkey: Set key for the transformation. This function is used to either | |
222 | * program a supplied key into the hardware or store the key in the | |
223 | * transformation context for programming it later. Note that this | |
224 | * function does modify the transformation context. This function can | |
225 | * be called multiple times during the existence of the transformation | |
226 | * object, so one must make sure the key is properly reprogrammed into | |
227 | * the hardware. This function is also responsible for checking the key | |
228 | * length for validity. In case a software fallback was put in place in | |
229 | * the @cra_init call, this function might need to use the fallback if | |
230 | * the algorithm doesn't support all of the key sizes. | |
231 | * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt | |
232 | * the supplied scatterlist containing the blocks of data. The crypto | |
233 | * API consumer is responsible for aligning the entries of the | |
234 | * scatterlist properly and making sure the chunks are correctly | |
235 | * sized. In case a software fallback was put in place in the | |
236 | * @cra_init call, this function might need to use the fallback if | |
237 | * the algorithm doesn't support all of the key sizes. In case the | |
238 | * key was stored in transformation context, the key might need to be | |
239 | * re-programmed into the hardware in this function. This function | |
240 | * shall not modify the transformation context, as this function may | |
241 | * be called in parallel with the same transformation object. | |
242 | * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt | |
243 | * and the conditions are exactly the same. | |
244 | * @givencrypt: Update the IV for encryption. With this function, a cipher | |
245 | * implementation may provide the function on how to update the IV | |
246 | * for encryption. | |
247 | * @givdecrypt: Update the IV for decryption. This is the reverse of | |
248 | * @givencrypt . | |
249 | * @geniv: The transformation implementation may use an "IV generator" provided | |
250 | * by the kernel crypto API. Several use cases have a predefined | |
251 | * approach how IVs are to be updated. For such use cases, the kernel | |
252 | * crypto API provides ready-to-use implementations that can be | |
253 | * referenced with this variable. | |
254 | * @ivsize: IV size applicable for transformation. The consumer must provide an | |
255 | * IV of exactly that size to perform the encrypt or decrypt operation. | |
256 | * | |
257 | * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are | |
258 | * mandatory and must be filled. | |
1da177e4 | 259 | */ |
b5b7f088 HX |
260 | struct ablkcipher_alg { |
261 | int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key, | |
262 | unsigned int keylen); | |
263 | int (*encrypt)(struct ablkcipher_request *req); | |
264 | int (*decrypt)(struct ablkcipher_request *req); | |
61da88e2 HX |
265 | int (*givencrypt)(struct skcipher_givcrypt_request *req); |
266 | int (*givdecrypt)(struct skcipher_givcrypt_request *req); | |
b5b7f088 | 267 | |
23508e11 HX |
268 | const char *geniv; |
269 | ||
b5b7f088 HX |
270 | unsigned int min_keysize; |
271 | unsigned int max_keysize; | |
272 | unsigned int ivsize; | |
273 | }; | |
274 | ||
0d7f488f SM |
275 | /** |
276 | * struct blkcipher_alg - synchronous block cipher definition | |
277 | * @min_keysize: see struct ablkcipher_alg | |
278 | * @max_keysize: see struct ablkcipher_alg | |
279 | * @setkey: see struct ablkcipher_alg | |
280 | * @encrypt: see struct ablkcipher_alg | |
281 | * @decrypt: see struct ablkcipher_alg | |
282 | * @geniv: see struct ablkcipher_alg | |
283 | * @ivsize: see struct ablkcipher_alg | |
284 | * | |
285 | * All fields except @geniv and @ivsize are mandatory and must be filled. | |
286 | */ | |
5cde0af2 HX |
287 | struct blkcipher_alg { |
288 | int (*setkey)(struct crypto_tfm *tfm, const u8 *key, | |
289 | unsigned int keylen); | |
290 | int (*encrypt)(struct blkcipher_desc *desc, | |
291 | struct scatterlist *dst, struct scatterlist *src, | |
292 | unsigned int nbytes); | |
293 | int (*decrypt)(struct blkcipher_desc *desc, | |
294 | struct scatterlist *dst, struct scatterlist *src, | |
295 | unsigned int nbytes); | |
296 | ||
23508e11 HX |
297 | const char *geniv; |
298 | ||
5cde0af2 HX |
299 | unsigned int min_keysize; |
300 | unsigned int max_keysize; | |
301 | unsigned int ivsize; | |
302 | }; | |
303 | ||
0d7f488f SM |
304 | /** |
305 | * struct cipher_alg - single-block symmetric ciphers definition | |
306 | * @cia_min_keysize: Minimum key size supported by the transformation. This is | |
307 | * the smallest key length supported by this transformation | |
308 | * algorithm. This must be set to one of the pre-defined | |
309 | * values as this is not hardware specific. Possible values | |
310 | * for this field can be found via git grep "_MIN_KEY_SIZE" | |
311 | * include/crypto/ | |
312 | * @cia_max_keysize: Maximum key size supported by the transformation. This is | |
313 | * the largest key length supported by this transformation | |
314 | * algorithm. This must be set to one of the pre-defined values | |
315 | * as this is not hardware specific. Possible values for this | |
316 | * field can be found via git grep "_MAX_KEY_SIZE" | |
317 | * include/crypto/ | |
318 | * @cia_setkey: Set key for the transformation. This function is used to either | |
319 | * program a supplied key into the hardware or store the key in the | |
320 | * transformation context for programming it later. Note that this | |
321 | * function does modify the transformation context. This function | |
322 | * can be called multiple times during the existence of the | |
323 | * transformation object, so one must make sure the key is properly | |
324 | * reprogrammed into the hardware. This function is also | |
325 | * responsible for checking the key length for validity. | |
326 | * @cia_encrypt: Encrypt a single block. This function is used to encrypt a | |
327 | * single block of data, which must be @cra_blocksize big. This | |
328 | * always operates on a full @cra_blocksize and it is not possible | |
329 | * to encrypt a block of smaller size. The supplied buffers must | |
330 | * therefore also be at least of @cra_blocksize size. Both the | |
331 | * input and output buffers are always aligned to @cra_alignmask. | |
332 | * In case either of the input or output buffer supplied by user | |
333 | * of the crypto API is not aligned to @cra_alignmask, the crypto | |
334 | * API will re-align the buffers. The re-alignment means that a | |
335 | * new buffer will be allocated, the data will be copied into the | |
336 | * new buffer, then the processing will happen on the new buffer, | |
337 | * then the data will be copied back into the original buffer and | |
338 | * finally the new buffer will be freed. In case a software | |
339 | * fallback was put in place in the @cra_init call, this function | |
340 | * might need to use the fallback if the algorithm doesn't support | |
341 | * all of the key sizes. In case the key was stored in | |
342 | * transformation context, the key might need to be re-programmed | |
343 | * into the hardware in this function. This function shall not | |
344 | * modify the transformation context, as this function may be | |
345 | * called in parallel with the same transformation object. | |
346 | * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to | |
347 | * @cia_encrypt, and the conditions are exactly the same. | |
348 | * | |
349 | * All fields are mandatory and must be filled. | |
350 | */ | |
1da177e4 LT |
351 | struct cipher_alg { |
352 | unsigned int cia_min_keysize; | |
353 | unsigned int cia_max_keysize; | |
6c2bb98b | 354 | int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key, |
560c06ae | 355 | unsigned int keylen); |
6c2bb98b HX |
356 | void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); |
357 | void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); | |
1da177e4 LT |
358 | }; |
359 | ||
1da177e4 | 360 | struct compress_alg { |
6c2bb98b HX |
361 | int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src, |
362 | unsigned int slen, u8 *dst, unsigned int *dlen); | |
363 | int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src, | |
364 | unsigned int slen, u8 *dst, unsigned int *dlen); | |
1da177e4 LT |
365 | }; |
366 | ||
17f0f4a4 | 367 | |
b5b7f088 | 368 | #define cra_ablkcipher cra_u.ablkcipher |
5cde0af2 | 369 | #define cra_blkcipher cra_u.blkcipher |
1da177e4 | 370 | #define cra_cipher cra_u.cipher |
1da177e4 LT |
371 | #define cra_compress cra_u.compress |
372 | ||
0d7f488f SM |
373 | /** |
374 | * struct crypto_alg - definition of a cryptograpic cipher algorithm | |
375 | * @cra_flags: Flags describing this transformation. See include/linux/crypto.h | |
376 | * CRYPTO_ALG_* flags for the flags which go in here. Those are | |
377 | * used for fine-tuning the description of the transformation | |
378 | * algorithm. | |
379 | * @cra_blocksize: Minimum block size of this transformation. The size in bytes | |
380 | * of the smallest possible unit which can be transformed with | |
381 | * this algorithm. The users must respect this value. | |
382 | * In case of HASH transformation, it is possible for a smaller | |
383 | * block than @cra_blocksize to be passed to the crypto API for | |
384 | * transformation, in case of any other transformation type, an | |
385 | * error will be returned upon any attempt to transform smaller | |
386 | * than @cra_blocksize chunks. | |
387 | * @cra_ctxsize: Size of the operational context of the transformation. This | |
388 | * value informs the kernel crypto API about the memory size | |
389 | * needed to be allocated for the transformation context. | |
390 | * @cra_alignmask: Alignment mask for the input and output data buffer. The data | |
391 | * buffer containing the input data for the algorithm must be | |
392 | * aligned to this alignment mask. The data buffer for the | |
393 | * output data must be aligned to this alignment mask. Note that | |
394 | * the Crypto API will do the re-alignment in software, but | |
395 | * only under special conditions and there is a performance hit. | |
396 | * The re-alignment happens at these occasions for different | |
397 | * @cra_u types: cipher -- For both input data and output data | |
398 | * buffer; ahash -- For output hash destination buf; shash -- | |
399 | * For output hash destination buf. | |
400 | * This is needed on hardware which is flawed by design and | |
401 | * cannot pick data from arbitrary addresses. | |
402 | * @cra_priority: Priority of this transformation implementation. In case | |
403 | * multiple transformations with same @cra_name are available to | |
404 | * the Crypto API, the kernel will use the one with highest | |
405 | * @cra_priority. | |
406 | * @cra_name: Generic name (usable by multiple implementations) of the | |
407 | * transformation algorithm. This is the name of the transformation | |
408 | * itself. This field is used by the kernel when looking up the | |
409 | * providers of particular transformation. | |
410 | * @cra_driver_name: Unique name of the transformation provider. This is the | |
411 | * name of the provider of the transformation. This can be any | |
412 | * arbitrary value, but in the usual case, this contains the | |
413 | * name of the chip or provider and the name of the | |
414 | * transformation algorithm. | |
415 | * @cra_type: Type of the cryptographic transformation. This is a pointer to | |
416 | * struct crypto_type, which implements callbacks common for all | |
12f7c14a | 417 | * transformation types. There are multiple options: |
0d7f488f | 418 | * &crypto_blkcipher_type, &crypto_ablkcipher_type, |
b0d955ba | 419 | * &crypto_ahash_type, &crypto_rng_type. |
0d7f488f SM |
420 | * This field might be empty. In that case, there are no common |
421 | * callbacks. This is the case for: cipher, compress, shash. | |
422 | * @cra_u: Callbacks implementing the transformation. This is a union of | |
423 | * multiple structures. Depending on the type of transformation selected | |
424 | * by @cra_type and @cra_flags above, the associated structure must be | |
425 | * filled with callbacks. This field might be empty. This is the case | |
426 | * for ahash, shash. | |
427 | * @cra_init: Initialize the cryptographic transformation object. This function | |
428 | * is used to initialize the cryptographic transformation object. | |
429 | * This function is called only once at the instantiation time, right | |
430 | * after the transformation context was allocated. In case the | |
431 | * cryptographic hardware has some special requirements which need to | |
432 | * be handled by software, this function shall check for the precise | |
433 | * requirement of the transformation and put any software fallbacks | |
434 | * in place. | |
435 | * @cra_exit: Deinitialize the cryptographic transformation object. This is a | |
436 | * counterpart to @cra_init, used to remove various changes set in | |
437 | * @cra_init. | |
438 | * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE | |
439 | * @cra_list: internally used | |
440 | * @cra_users: internally used | |
441 | * @cra_refcnt: internally used | |
442 | * @cra_destroy: internally used | |
443 | * | |
444 | * The struct crypto_alg describes a generic Crypto API algorithm and is common | |
445 | * for all of the transformations. Any variable not documented here shall not | |
446 | * be used by a cipher implementation as it is internal to the Crypto API. | |
447 | */ | |
1da177e4 LT |
448 | struct crypto_alg { |
449 | struct list_head cra_list; | |
6bfd4809 HX |
450 | struct list_head cra_users; |
451 | ||
1da177e4 LT |
452 | u32 cra_flags; |
453 | unsigned int cra_blocksize; | |
454 | unsigned int cra_ctxsize; | |
95477377 | 455 | unsigned int cra_alignmask; |
5cb1454b HX |
456 | |
457 | int cra_priority; | |
ce8614a3 | 458 | refcount_t cra_refcnt; |
5cb1454b | 459 | |
d913ea0d HX |
460 | char cra_name[CRYPTO_MAX_ALG_NAME]; |
461 | char cra_driver_name[CRYPTO_MAX_ALG_NAME]; | |
1da177e4 | 462 | |
e853c3cf HX |
463 | const struct crypto_type *cra_type; |
464 | ||
1da177e4 | 465 | union { |
b5b7f088 | 466 | struct ablkcipher_alg ablkcipher; |
5cde0af2 | 467 | struct blkcipher_alg blkcipher; |
1da177e4 | 468 | struct cipher_alg cipher; |
1da177e4 LT |
469 | struct compress_alg compress; |
470 | } cra_u; | |
c7fc0599 HX |
471 | |
472 | int (*cra_init)(struct crypto_tfm *tfm); | |
473 | void (*cra_exit)(struct crypto_tfm *tfm); | |
6521f302 | 474 | void (*cra_destroy)(struct crypto_alg *alg); |
1da177e4 LT |
475 | |
476 | struct module *cra_module; | |
edf18b91 | 477 | } CRYPTO_MINALIGN_ATTR; |
1da177e4 | 478 | |
ada69a16 GBY |
479 | /* |
480 | * A helper struct for waiting for completion of async crypto ops | |
481 | */ | |
482 | struct crypto_wait { | |
483 | struct completion completion; | |
484 | int err; | |
485 | }; | |
486 | ||
487 | /* | |
488 | * Macro for declaring a crypto op async wait object on stack | |
489 | */ | |
490 | #define DECLARE_CRYPTO_WAIT(_wait) \ | |
491 | struct crypto_wait _wait = { \ | |
492 | COMPLETION_INITIALIZER_ONSTACK((_wait).completion), 0 } | |
493 | ||
494 | /* | |
495 | * Async ops completion helper functioons | |
496 | */ | |
497 | void crypto_req_done(struct crypto_async_request *req, int err); | |
498 | ||
499 | static inline int crypto_wait_req(int err, struct crypto_wait *wait) | |
500 | { | |
501 | switch (err) { | |
502 | case -EINPROGRESS: | |
503 | case -EBUSY: | |
504 | wait_for_completion(&wait->completion); | |
505 | reinit_completion(&wait->completion); | |
506 | err = wait->err; | |
507 | break; | |
508 | }; | |
509 | ||
510 | return err; | |
511 | } | |
512 | ||
513 | static inline void crypto_init_wait(struct crypto_wait *wait) | |
514 | { | |
515 | init_completion(&wait->completion); | |
516 | } | |
517 | ||
1da177e4 LT |
518 | /* |
519 | * Algorithm registration interface. | |
520 | */ | |
521 | int crypto_register_alg(struct crypto_alg *alg); | |
522 | int crypto_unregister_alg(struct crypto_alg *alg); | |
4b004346 MB |
523 | int crypto_register_algs(struct crypto_alg *algs, int count); |
524 | int crypto_unregister_algs(struct crypto_alg *algs, int count); | |
1da177e4 LT |
525 | |
526 | /* | |
527 | * Algorithm query interface. | |
528 | */ | |
fce32d70 | 529 | int crypto_has_alg(const char *name, u32 type, u32 mask); |
1da177e4 LT |
530 | |
531 | /* | |
532 | * Transforms: user-instantiated objects which encapsulate algorithms | |
6d7d684d HX |
533 | * and core processing logic. Managed via crypto_alloc_*() and |
534 | * crypto_free_*(), as well as the various helpers below. | |
1da177e4 | 535 | */ |
1da177e4 | 536 | |
32e3983f HX |
537 | struct ablkcipher_tfm { |
538 | int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key, | |
539 | unsigned int keylen); | |
540 | int (*encrypt)(struct ablkcipher_request *req); | |
541 | int (*decrypt)(struct ablkcipher_request *req); | |
61da88e2 | 542 | |
ecfc4329 HX |
543 | struct crypto_ablkcipher *base; |
544 | ||
32e3983f HX |
545 | unsigned int ivsize; |
546 | unsigned int reqsize; | |
547 | }; | |
548 | ||
5cde0af2 HX |
549 | struct blkcipher_tfm { |
550 | void *iv; | |
551 | int (*setkey)(struct crypto_tfm *tfm, const u8 *key, | |
552 | unsigned int keylen); | |
553 | int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst, | |
554 | struct scatterlist *src, unsigned int nbytes); | |
555 | int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst, | |
556 | struct scatterlist *src, unsigned int nbytes); | |
557 | }; | |
558 | ||
1da177e4 | 559 | struct cipher_tfm { |
1da177e4 LT |
560 | int (*cit_setkey)(struct crypto_tfm *tfm, |
561 | const u8 *key, unsigned int keylen); | |
f28776a3 HX |
562 | void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); |
563 | void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); | |
1da177e4 LT |
564 | }; |
565 | ||
1da177e4 LT |
566 | struct compress_tfm { |
567 | int (*cot_compress)(struct crypto_tfm *tfm, | |
568 | const u8 *src, unsigned int slen, | |
569 | u8 *dst, unsigned int *dlen); | |
570 | int (*cot_decompress)(struct crypto_tfm *tfm, | |
571 | const u8 *src, unsigned int slen, | |
572 | u8 *dst, unsigned int *dlen); | |
573 | }; | |
574 | ||
32e3983f | 575 | #define crt_ablkcipher crt_u.ablkcipher |
5cde0af2 | 576 | #define crt_blkcipher crt_u.blkcipher |
1da177e4 | 577 | #define crt_cipher crt_u.cipher |
1da177e4 LT |
578 | #define crt_compress crt_u.compress |
579 | ||
580 | struct crypto_tfm { | |
581 | ||
582 | u32 crt_flags; | |
583 | ||
584 | union { | |
32e3983f | 585 | struct ablkcipher_tfm ablkcipher; |
5cde0af2 | 586 | struct blkcipher_tfm blkcipher; |
1da177e4 | 587 | struct cipher_tfm cipher; |
1da177e4 LT |
588 | struct compress_tfm compress; |
589 | } crt_u; | |
4a779486 HX |
590 | |
591 | void (*exit)(struct crypto_tfm *tfm); | |
1da177e4 LT |
592 | |
593 | struct crypto_alg *__crt_alg; | |
f10b7897 | 594 | |
79911102 | 595 | void *__crt_ctx[] CRYPTO_MINALIGN_ATTR; |
1da177e4 LT |
596 | }; |
597 | ||
32e3983f HX |
598 | struct crypto_ablkcipher { |
599 | struct crypto_tfm base; | |
600 | }; | |
601 | ||
5cde0af2 HX |
602 | struct crypto_blkcipher { |
603 | struct crypto_tfm base; | |
604 | }; | |
605 | ||
78a1fe4f HX |
606 | struct crypto_cipher { |
607 | struct crypto_tfm base; | |
608 | }; | |
609 | ||
610 | struct crypto_comp { | |
611 | struct crypto_tfm base; | |
612 | }; | |
613 | ||
2b8c19db HX |
614 | enum { |
615 | CRYPTOA_UNSPEC, | |
616 | CRYPTOA_ALG, | |
ebc610e5 | 617 | CRYPTOA_TYPE, |
39e1ee01 | 618 | CRYPTOA_U32, |
ebc610e5 | 619 | __CRYPTOA_MAX, |
2b8c19db HX |
620 | }; |
621 | ||
ebc610e5 HX |
622 | #define CRYPTOA_MAX (__CRYPTOA_MAX - 1) |
623 | ||
39e1ee01 HX |
624 | /* Maximum number of (rtattr) parameters for each template. */ |
625 | #define CRYPTO_MAX_ATTRS 32 | |
626 | ||
2b8c19db HX |
627 | struct crypto_attr_alg { |
628 | char name[CRYPTO_MAX_ALG_NAME]; | |
629 | }; | |
630 | ||
ebc610e5 HX |
631 | struct crypto_attr_type { |
632 | u32 type; | |
633 | u32 mask; | |
634 | }; | |
635 | ||
39e1ee01 HX |
636 | struct crypto_attr_u32 { |
637 | u32 num; | |
638 | }; | |
639 | ||
1da177e4 LT |
640 | /* |
641 | * Transform user interface. | |
642 | */ | |
643 | ||
6d7d684d | 644 | struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask); |
7b2cd92a HX |
645 | void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm); |
646 | ||
647 | static inline void crypto_free_tfm(struct crypto_tfm *tfm) | |
648 | { | |
649 | return crypto_destroy_tfm(tfm, tfm); | |
650 | } | |
1da177e4 | 651 | |
da7f033d HX |
652 | int alg_test(const char *driver, const char *alg, u32 type, u32 mask); |
653 | ||
1da177e4 LT |
654 | /* |
655 | * Transform helpers which query the underlying algorithm. | |
656 | */ | |
657 | static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm) | |
658 | { | |
659 | return tfm->__crt_alg->cra_name; | |
660 | } | |
661 | ||
b14cdd67 ML |
662 | static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm) |
663 | { | |
664 | return tfm->__crt_alg->cra_driver_name; | |
665 | } | |
666 | ||
667 | static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm) | |
668 | { | |
669 | return tfm->__crt_alg->cra_priority; | |
670 | } | |
671 | ||
1da177e4 LT |
672 | static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm) |
673 | { | |
674 | return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK; | |
675 | } | |
676 | ||
1da177e4 LT |
677 | static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm) |
678 | { | |
679 | return tfm->__crt_alg->cra_blocksize; | |
680 | } | |
681 | ||
fbdae9f3 HX |
682 | static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm) |
683 | { | |
684 | return tfm->__crt_alg->cra_alignmask; | |
685 | } | |
686 | ||
f28776a3 HX |
687 | static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm) |
688 | { | |
689 | return tfm->crt_flags; | |
690 | } | |
691 | ||
692 | static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags) | |
693 | { | |
694 | tfm->crt_flags |= flags; | |
695 | } | |
696 | ||
697 | static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags) | |
698 | { | |
699 | tfm->crt_flags &= ~flags; | |
700 | } | |
701 | ||
40725181 HX |
702 | static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm) |
703 | { | |
f10b7897 HX |
704 | return tfm->__crt_ctx; |
705 | } | |
706 | ||
707 | static inline unsigned int crypto_tfm_ctx_alignment(void) | |
708 | { | |
709 | struct crypto_tfm *tfm; | |
710 | return __alignof__(tfm->__crt_ctx); | |
40725181 HX |
711 | } |
712 | ||
1da177e4 LT |
713 | /* |
714 | * API wrappers. | |
715 | */ | |
32e3983f HX |
716 | static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast( |
717 | struct crypto_tfm *tfm) | |
718 | { | |
719 | return (struct crypto_ablkcipher *)tfm; | |
720 | } | |
721 | ||
378f4f51 | 722 | static inline u32 crypto_skcipher_type(u32 type) |
32e3983f | 723 | { |
ecfc4329 | 724 | type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV); |
32e3983f | 725 | type |= CRYPTO_ALG_TYPE_BLKCIPHER; |
378f4f51 HX |
726 | return type; |
727 | } | |
728 | ||
729 | static inline u32 crypto_skcipher_mask(u32 mask) | |
730 | { | |
ecfc4329 | 731 | mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV); |
332f8840 | 732 | mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK; |
378f4f51 HX |
733 | return mask; |
734 | } | |
32e3983f | 735 | |
f13ec330 SM |
736 | /** |
737 | * DOC: Asynchronous Block Cipher API | |
738 | * | |
739 | * Asynchronous block cipher API is used with the ciphers of type | |
740 | * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto). | |
741 | * | |
742 | * Asynchronous cipher operations imply that the function invocation for a | |
743 | * cipher request returns immediately before the completion of the operation. | |
744 | * The cipher request is scheduled as a separate kernel thread and therefore | |
745 | * load-balanced on the different CPUs via the process scheduler. To allow | |
746 | * the kernel crypto API to inform the caller about the completion of a cipher | |
747 | * request, the caller must provide a callback function. That function is | |
748 | * invoked with the cipher handle when the request completes. | |
749 | * | |
750 | * To support the asynchronous operation, additional information than just the | |
751 | * cipher handle must be supplied to the kernel crypto API. That additional | |
752 | * information is given by filling in the ablkcipher_request data structure. | |
753 | * | |
754 | * For the asynchronous block cipher API, the state is maintained with the tfm | |
755 | * cipher handle. A single tfm can be used across multiple calls and in | |
756 | * parallel. For asynchronous block cipher calls, context data supplied and | |
757 | * only used by the caller can be referenced the request data structure in | |
758 | * addition to the IV used for the cipher request. The maintenance of such | |
759 | * state information would be important for a crypto driver implementer to | |
760 | * have, because when calling the callback function upon completion of the | |
761 | * cipher operation, that callback function may need some information about | |
762 | * which operation just finished if it invoked multiple in parallel. This | |
763 | * state information is unused by the kernel crypto API. | |
764 | */ | |
765 | ||
32e3983f HX |
766 | static inline struct crypto_tfm *crypto_ablkcipher_tfm( |
767 | struct crypto_ablkcipher *tfm) | |
768 | { | |
769 | return &tfm->base; | |
770 | } | |
771 | ||
f13ec330 SM |
772 | /** |
773 | * crypto_free_ablkcipher() - zeroize and free cipher handle | |
774 | * @tfm: cipher handle to be freed | |
775 | */ | |
32e3983f HX |
776 | static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm) |
777 | { | |
778 | crypto_free_tfm(crypto_ablkcipher_tfm(tfm)); | |
779 | } | |
780 | ||
f13ec330 SM |
781 | /** |
782 | * crypto_has_ablkcipher() - Search for the availability of an ablkcipher. | |
783 | * @alg_name: is the cra_name / name or cra_driver_name / driver name of the | |
784 | * ablkcipher | |
785 | * @type: specifies the type of the cipher | |
786 | * @mask: specifies the mask for the cipher | |
787 | * | |
788 | * Return: true when the ablkcipher is known to the kernel crypto API; false | |
789 | * otherwise | |
790 | */ | |
32e3983f HX |
791 | static inline int crypto_has_ablkcipher(const char *alg_name, u32 type, |
792 | u32 mask) | |
793 | { | |
378f4f51 HX |
794 | return crypto_has_alg(alg_name, crypto_skcipher_type(type), |
795 | crypto_skcipher_mask(mask)); | |
32e3983f HX |
796 | } |
797 | ||
798 | static inline struct ablkcipher_tfm *crypto_ablkcipher_crt( | |
799 | struct crypto_ablkcipher *tfm) | |
800 | { | |
801 | return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher; | |
802 | } | |
803 | ||
f13ec330 SM |
804 | /** |
805 | * crypto_ablkcipher_ivsize() - obtain IV size | |
806 | * @tfm: cipher handle | |
807 | * | |
808 | * The size of the IV for the ablkcipher referenced by the cipher handle is | |
809 | * returned. This IV size may be zero if the cipher does not need an IV. | |
810 | * | |
811 | * Return: IV size in bytes | |
812 | */ | |
32e3983f HX |
813 | static inline unsigned int crypto_ablkcipher_ivsize( |
814 | struct crypto_ablkcipher *tfm) | |
815 | { | |
816 | return crypto_ablkcipher_crt(tfm)->ivsize; | |
817 | } | |
818 | ||
f13ec330 SM |
819 | /** |
820 | * crypto_ablkcipher_blocksize() - obtain block size of cipher | |
821 | * @tfm: cipher handle | |
822 | * | |
823 | * The block size for the ablkcipher referenced with the cipher handle is | |
824 | * returned. The caller may use that information to allocate appropriate | |
825 | * memory for the data returned by the encryption or decryption operation | |
826 | * | |
827 | * Return: block size of cipher | |
828 | */ | |
32e3983f HX |
829 | static inline unsigned int crypto_ablkcipher_blocksize( |
830 | struct crypto_ablkcipher *tfm) | |
831 | { | |
832 | return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm)); | |
833 | } | |
834 | ||
835 | static inline unsigned int crypto_ablkcipher_alignmask( | |
836 | struct crypto_ablkcipher *tfm) | |
837 | { | |
838 | return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm)); | |
839 | } | |
840 | ||
841 | static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm) | |
842 | { | |
843 | return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm)); | |
844 | } | |
845 | ||
846 | static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm, | |
847 | u32 flags) | |
848 | { | |
849 | crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags); | |
850 | } | |
851 | ||
852 | static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm, | |
853 | u32 flags) | |
854 | { | |
855 | crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags); | |
856 | } | |
857 | ||
f13ec330 SM |
858 | /** |
859 | * crypto_ablkcipher_setkey() - set key for cipher | |
860 | * @tfm: cipher handle | |
861 | * @key: buffer holding the key | |
862 | * @keylen: length of the key in bytes | |
863 | * | |
864 | * The caller provided key is set for the ablkcipher referenced by the cipher | |
865 | * handle. | |
866 | * | |
867 | * Note, the key length determines the cipher type. Many block ciphers implement | |
868 | * different cipher modes depending on the key size, such as AES-128 vs AES-192 | |
869 | * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128 | |
870 | * is performed. | |
871 | * | |
872 | * Return: 0 if the setting of the key was successful; < 0 if an error occurred | |
873 | */ | |
32e3983f HX |
874 | static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm, |
875 | const u8 *key, unsigned int keylen) | |
876 | { | |
ecfc4329 HX |
877 | struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm); |
878 | ||
879 | return crt->setkey(crt->base, key, keylen); | |
32e3983f HX |
880 | } |
881 | ||
f13ec330 SM |
882 | /** |
883 | * crypto_ablkcipher_reqtfm() - obtain cipher handle from request | |
884 | * @req: ablkcipher_request out of which the cipher handle is to be obtained | |
885 | * | |
886 | * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request | |
887 | * data structure. | |
888 | * | |
889 | * Return: crypto_ablkcipher handle | |
890 | */ | |
32e3983f HX |
891 | static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm( |
892 | struct ablkcipher_request *req) | |
893 | { | |
894 | return __crypto_ablkcipher_cast(req->base.tfm); | |
895 | } | |
896 | ||
f13ec330 SM |
897 | /** |
898 | * crypto_ablkcipher_encrypt() - encrypt plaintext | |
899 | * @req: reference to the ablkcipher_request handle that holds all information | |
900 | * needed to perform the cipher operation | |
901 | * | |
902 | * Encrypt plaintext data using the ablkcipher_request handle. That data | |
903 | * structure and how it is filled with data is discussed with the | |
904 | * ablkcipher_request_* functions. | |
905 | * | |
906 | * Return: 0 if the cipher operation was successful; < 0 if an error occurred | |
907 | */ | |
32e3983f HX |
908 | static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req) |
909 | { | |
910 | struct ablkcipher_tfm *crt = | |
911 | crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req)); | |
912 | return crt->encrypt(req); | |
913 | } | |
914 | ||
f13ec330 SM |
915 | /** |
916 | * crypto_ablkcipher_decrypt() - decrypt ciphertext | |
917 | * @req: reference to the ablkcipher_request handle that holds all information | |
918 | * needed to perform the cipher operation | |
919 | * | |
920 | * Decrypt ciphertext data using the ablkcipher_request handle. That data | |
921 | * structure and how it is filled with data is discussed with the | |
922 | * ablkcipher_request_* functions. | |
923 | * | |
924 | * Return: 0 if the cipher operation was successful; < 0 if an error occurred | |
925 | */ | |
32e3983f HX |
926 | static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req) |
927 | { | |
928 | struct ablkcipher_tfm *crt = | |
929 | crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req)); | |
930 | return crt->decrypt(req); | |
931 | } | |
932 | ||
f13ec330 SM |
933 | /** |
934 | * DOC: Asynchronous Cipher Request Handle | |
935 | * | |
936 | * The ablkcipher_request data structure contains all pointers to data | |
937 | * required for the asynchronous cipher operation. This includes the cipher | |
938 | * handle (which can be used by multiple ablkcipher_request instances), pointer | |
939 | * to plaintext and ciphertext, asynchronous callback function, etc. It acts | |
940 | * as a handle to the ablkcipher_request_* API calls in a similar way as | |
941 | * ablkcipher handle to the crypto_ablkcipher_* API calls. | |
942 | */ | |
943 | ||
944 | /** | |
945 | * crypto_ablkcipher_reqsize() - obtain size of the request data structure | |
946 | * @tfm: cipher handle | |
947 | * | |
948 | * Return: number of bytes | |
949 | */ | |
b16c3a2e HX |
950 | static inline unsigned int crypto_ablkcipher_reqsize( |
951 | struct crypto_ablkcipher *tfm) | |
32e3983f HX |
952 | { |
953 | return crypto_ablkcipher_crt(tfm)->reqsize; | |
954 | } | |
955 | ||
f13ec330 SM |
956 | /** |
957 | * ablkcipher_request_set_tfm() - update cipher handle reference in request | |
958 | * @req: request handle to be modified | |
959 | * @tfm: cipher handle that shall be added to the request handle | |
960 | * | |
961 | * Allow the caller to replace the existing ablkcipher handle in the request | |
962 | * data structure with a different one. | |
963 | */ | |
e196d625 HX |
964 | static inline void ablkcipher_request_set_tfm( |
965 | struct ablkcipher_request *req, struct crypto_ablkcipher *tfm) | |
966 | { | |
ecfc4329 | 967 | req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base); |
e196d625 HX |
968 | } |
969 | ||
b5b7f088 HX |
970 | static inline struct ablkcipher_request *ablkcipher_request_cast( |
971 | struct crypto_async_request *req) | |
972 | { | |
973 | return container_of(req, struct ablkcipher_request, base); | |
974 | } | |
975 | ||
f13ec330 SM |
976 | /** |
977 | * ablkcipher_request_alloc() - allocate request data structure | |
978 | * @tfm: cipher handle to be registered with the request | |
979 | * @gfp: memory allocation flag that is handed to kmalloc by the API call. | |
980 | * | |
981 | * Allocate the request data structure that must be used with the ablkcipher | |
982 | * encrypt and decrypt API calls. During the allocation, the provided ablkcipher | |
983 | * handle is registered in the request data structure. | |
984 | * | |
6eae29e7 | 985 | * Return: allocated request handle in case of success, or NULL if out of memory |
f13ec330 | 986 | */ |
32e3983f HX |
987 | static inline struct ablkcipher_request *ablkcipher_request_alloc( |
988 | struct crypto_ablkcipher *tfm, gfp_t gfp) | |
989 | { | |
990 | struct ablkcipher_request *req; | |
991 | ||
992 | req = kmalloc(sizeof(struct ablkcipher_request) + | |
993 | crypto_ablkcipher_reqsize(tfm), gfp); | |
994 | ||
995 | if (likely(req)) | |
e196d625 | 996 | ablkcipher_request_set_tfm(req, tfm); |
32e3983f HX |
997 | |
998 | return req; | |
999 | } | |
1000 | ||
f13ec330 SM |
1001 | /** |
1002 | * ablkcipher_request_free() - zeroize and free request data structure | |
1003 | * @req: request data structure cipher handle to be freed | |
1004 | */ | |
32e3983f HX |
1005 | static inline void ablkcipher_request_free(struct ablkcipher_request *req) |
1006 | { | |
aef73cfc | 1007 | kzfree(req); |
32e3983f HX |
1008 | } |
1009 | ||
f13ec330 SM |
1010 | /** |
1011 | * ablkcipher_request_set_callback() - set asynchronous callback function | |
1012 | * @req: request handle | |
1013 | * @flags: specify zero or an ORing of the flags | |
0184cfe7 | 1014 | * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and |
f13ec330 SM |
1015 | * increase the wait queue beyond the initial maximum size; |
1016 | * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep | |
1017 | * @compl: callback function pointer to be registered with the request handle | |
1018 | * @data: The data pointer refers to memory that is not used by the kernel | |
1019 | * crypto API, but provided to the callback function for it to use. Here, | |
1020 | * the caller can provide a reference to memory the callback function can | |
1021 | * operate on. As the callback function is invoked asynchronously to the | |
1022 | * related functionality, it may need to access data structures of the | |
1023 | * related functionality which can be referenced using this pointer. The | |
1024 | * callback function can access the memory via the "data" field in the | |
1025 | * crypto_async_request data structure provided to the callback function. | |
1026 | * | |
1027 | * This function allows setting the callback function that is triggered once the | |
1028 | * cipher operation completes. | |
1029 | * | |
1030 | * The callback function is registered with the ablkcipher_request handle and | |
0184cfe7 | 1031 | * must comply with the following template:: |
f13ec330 SM |
1032 | * |
1033 | * void callback_function(struct crypto_async_request *req, int error) | |
1034 | */ | |
32e3983f HX |
1035 | static inline void ablkcipher_request_set_callback( |
1036 | struct ablkcipher_request *req, | |
3e3dc25f | 1037 | u32 flags, crypto_completion_t compl, void *data) |
32e3983f | 1038 | { |
3e3dc25f | 1039 | req->base.complete = compl; |
32e3983f HX |
1040 | req->base.data = data; |
1041 | req->base.flags = flags; | |
1042 | } | |
1043 | ||
f13ec330 SM |
1044 | /** |
1045 | * ablkcipher_request_set_crypt() - set data buffers | |
1046 | * @req: request handle | |
1047 | * @src: source scatter / gather list | |
1048 | * @dst: destination scatter / gather list | |
1049 | * @nbytes: number of bytes to process from @src | |
1050 | * @iv: IV for the cipher operation which must comply with the IV size defined | |
1051 | * by crypto_ablkcipher_ivsize | |
1052 | * | |
1053 | * This function allows setting of the source data and destination data | |
1054 | * scatter / gather lists. | |
1055 | * | |
1056 | * For encryption, the source is treated as the plaintext and the | |
1057 | * destination is the ciphertext. For a decryption operation, the use is | |
379dcfb4 | 1058 | * reversed - the source is the ciphertext and the destination is the plaintext. |
f13ec330 | 1059 | */ |
32e3983f HX |
1060 | static inline void ablkcipher_request_set_crypt( |
1061 | struct ablkcipher_request *req, | |
1062 | struct scatterlist *src, struct scatterlist *dst, | |
1063 | unsigned int nbytes, void *iv) | |
1064 | { | |
1065 | req->src = src; | |
1066 | req->dst = dst; | |
1067 | req->nbytes = nbytes; | |
1068 | req->info = iv; | |
1069 | } | |
1070 | ||
58284f0d SM |
1071 | /** |
1072 | * DOC: Synchronous Block Cipher API | |
1073 | * | |
1074 | * The synchronous block cipher API is used with the ciphers of type | |
1075 | * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto) | |
1076 | * | |
1077 | * Synchronous calls, have a context in the tfm. But since a single tfm can be | |
1078 | * used in multiple calls and in parallel, this info should not be changeable | |
1079 | * (unless a lock is used). This applies, for example, to the symmetric key. | |
1080 | * However, the IV is changeable, so there is an iv field in blkcipher_tfm | |
1081 | * structure for synchronous blkcipher api. So, its the only state info that can | |
1082 | * be kept for synchronous calls without using a big lock across a tfm. | |
1083 | * | |
1084 | * The block cipher API allows the use of a complete cipher, i.e. a cipher | |
1085 | * consisting of a template (a block chaining mode) and a single block cipher | |
1086 | * primitive (e.g. AES). | |
1087 | * | |
1088 | * The plaintext data buffer and the ciphertext data buffer are pointed to | |
1089 | * by using scatter/gather lists. The cipher operation is performed | |
1090 | * on all segments of the provided scatter/gather lists. | |
1091 | * | |
1092 | * The kernel crypto API supports a cipher operation "in-place" which means that | |
1093 | * the caller may provide the same scatter/gather list for the plaintext and | |
1094 | * cipher text. After the completion of the cipher operation, the plaintext | |
1095 | * data is replaced with the ciphertext data in case of an encryption and vice | |
1096 | * versa for a decryption. The caller must ensure that the scatter/gather lists | |
1097 | * for the output data point to sufficiently large buffers, i.e. multiples of | |
1098 | * the block size of the cipher. | |
1099 | */ | |
1100 | ||
5cde0af2 HX |
1101 | static inline struct crypto_blkcipher *__crypto_blkcipher_cast( |
1102 | struct crypto_tfm *tfm) | |
1103 | { | |
1104 | return (struct crypto_blkcipher *)tfm; | |
1105 | } | |
1106 | ||
1107 | static inline struct crypto_blkcipher *crypto_blkcipher_cast( | |
1108 | struct crypto_tfm *tfm) | |
1109 | { | |
1110 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER); | |
1111 | return __crypto_blkcipher_cast(tfm); | |
1112 | } | |
1113 | ||
58284f0d SM |
1114 | /** |
1115 | * crypto_alloc_blkcipher() - allocate synchronous block cipher handle | |
1116 | * @alg_name: is the cra_name / name or cra_driver_name / driver name of the | |
1117 | * blkcipher cipher | |
1118 | * @type: specifies the type of the cipher | |
1119 | * @mask: specifies the mask for the cipher | |
1120 | * | |
1121 | * Allocate a cipher handle for a block cipher. The returned struct | |
1122 | * crypto_blkcipher is the cipher handle that is required for any subsequent | |
1123 | * API invocation for that block cipher. | |
1124 | * | |
1125 | * Return: allocated cipher handle in case of success; IS_ERR() is true in case | |
1126 | * of an error, PTR_ERR() returns the error code. | |
1127 | */ | |
5cde0af2 HX |
1128 | static inline struct crypto_blkcipher *crypto_alloc_blkcipher( |
1129 | const char *alg_name, u32 type, u32 mask) | |
1130 | { | |
332f8840 | 1131 | type &= ~CRYPTO_ALG_TYPE_MASK; |
5cde0af2 | 1132 | type |= CRYPTO_ALG_TYPE_BLKCIPHER; |
332f8840 | 1133 | mask |= CRYPTO_ALG_TYPE_MASK; |
5cde0af2 HX |
1134 | |
1135 | return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask)); | |
1136 | } | |
1137 | ||
1138 | static inline struct crypto_tfm *crypto_blkcipher_tfm( | |
1139 | struct crypto_blkcipher *tfm) | |
1140 | { | |
1141 | return &tfm->base; | |
1142 | } | |
1143 | ||
58284f0d SM |
1144 | /** |
1145 | * crypto_free_blkcipher() - zeroize and free the block cipher handle | |
1146 | * @tfm: cipher handle to be freed | |
1147 | */ | |
5cde0af2 HX |
1148 | static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm) |
1149 | { | |
1150 | crypto_free_tfm(crypto_blkcipher_tfm(tfm)); | |
1151 | } | |
1152 | ||
58284f0d SM |
1153 | /** |
1154 | * crypto_has_blkcipher() - Search for the availability of a block cipher | |
1155 | * @alg_name: is the cra_name / name or cra_driver_name / driver name of the | |
1156 | * block cipher | |
1157 | * @type: specifies the type of the cipher | |
1158 | * @mask: specifies the mask for the cipher | |
1159 | * | |
1160 | * Return: true when the block cipher is known to the kernel crypto API; false | |
1161 | * otherwise | |
1162 | */ | |
fce32d70 HX |
1163 | static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask) |
1164 | { | |
332f8840 | 1165 | type &= ~CRYPTO_ALG_TYPE_MASK; |
fce32d70 | 1166 | type |= CRYPTO_ALG_TYPE_BLKCIPHER; |
332f8840 | 1167 | mask |= CRYPTO_ALG_TYPE_MASK; |
fce32d70 HX |
1168 | |
1169 | return crypto_has_alg(alg_name, type, mask); | |
1170 | } | |
1171 | ||
58284f0d SM |
1172 | /** |
1173 | * crypto_blkcipher_name() - return the name / cra_name from the cipher handle | |
1174 | * @tfm: cipher handle | |
1175 | * | |
1176 | * Return: The character string holding the name of the cipher | |
1177 | */ | |
5cde0af2 HX |
1178 | static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm) |
1179 | { | |
1180 | return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm)); | |
1181 | } | |
1182 | ||
1183 | static inline struct blkcipher_tfm *crypto_blkcipher_crt( | |
1184 | struct crypto_blkcipher *tfm) | |
1185 | { | |
1186 | return &crypto_blkcipher_tfm(tfm)->crt_blkcipher; | |
1187 | } | |
1188 | ||
1189 | static inline struct blkcipher_alg *crypto_blkcipher_alg( | |
1190 | struct crypto_blkcipher *tfm) | |
1191 | { | |
1192 | return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher; | |
1193 | } | |
1194 | ||
58284f0d SM |
1195 | /** |
1196 | * crypto_blkcipher_ivsize() - obtain IV size | |
1197 | * @tfm: cipher handle | |
1198 | * | |
1199 | * The size of the IV for the block cipher referenced by the cipher handle is | |
1200 | * returned. This IV size may be zero if the cipher does not need an IV. | |
1201 | * | |
1202 | * Return: IV size in bytes | |
1203 | */ | |
5cde0af2 HX |
1204 | static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm) |
1205 | { | |
1206 | return crypto_blkcipher_alg(tfm)->ivsize; | |
1207 | } | |
1208 | ||
58284f0d SM |
1209 | /** |
1210 | * crypto_blkcipher_blocksize() - obtain block size of cipher | |
1211 | * @tfm: cipher handle | |
1212 | * | |
1213 | * The block size for the block cipher referenced with the cipher handle is | |
1214 | * returned. The caller may use that information to allocate appropriate | |
1215 | * memory for the data returned by the encryption or decryption operation. | |
1216 | * | |
1217 | * Return: block size of cipher | |
1218 | */ | |
5cde0af2 HX |
1219 | static inline unsigned int crypto_blkcipher_blocksize( |
1220 | struct crypto_blkcipher *tfm) | |
1221 | { | |
1222 | return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm)); | |
1223 | } | |
1224 | ||
1225 | static inline unsigned int crypto_blkcipher_alignmask( | |
1226 | struct crypto_blkcipher *tfm) | |
1227 | { | |
1228 | return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm)); | |
1229 | } | |
1230 | ||
1231 | static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm) | |
1232 | { | |
1233 | return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm)); | |
1234 | } | |
1235 | ||
1236 | static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm, | |
1237 | u32 flags) | |
1238 | { | |
1239 | crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags); | |
1240 | } | |
1241 | ||
1242 | static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm, | |
1243 | u32 flags) | |
1244 | { | |
1245 | crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags); | |
1246 | } | |
1247 | ||
58284f0d SM |
1248 | /** |
1249 | * crypto_blkcipher_setkey() - set key for cipher | |
1250 | * @tfm: cipher handle | |
1251 | * @key: buffer holding the key | |
1252 | * @keylen: length of the key in bytes | |
1253 | * | |
1254 | * The caller provided key is set for the block cipher referenced by the cipher | |
1255 | * handle. | |
1256 | * | |
1257 | * Note, the key length determines the cipher type. Many block ciphers implement | |
1258 | * different cipher modes depending on the key size, such as AES-128 vs AES-192 | |
1259 | * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128 | |
1260 | * is performed. | |
1261 | * | |
1262 | * Return: 0 if the setting of the key was successful; < 0 if an error occurred | |
1263 | */ | |
5cde0af2 HX |
1264 | static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm, |
1265 | const u8 *key, unsigned int keylen) | |
1266 | { | |
1267 | return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm), | |
1268 | key, keylen); | |
1269 | } | |
1270 | ||
58284f0d SM |
1271 | /** |
1272 | * crypto_blkcipher_encrypt() - encrypt plaintext | |
1273 | * @desc: reference to the block cipher handle with meta data | |
1274 | * @dst: scatter/gather list that is filled by the cipher operation with the | |
1275 | * ciphertext | |
1276 | * @src: scatter/gather list that holds the plaintext | |
1277 | * @nbytes: number of bytes of the plaintext to encrypt. | |
1278 | * | |
1279 | * Encrypt plaintext data using the IV set by the caller with a preceding | |
1280 | * call of crypto_blkcipher_set_iv. | |
1281 | * | |
1282 | * The blkcipher_desc data structure must be filled by the caller and can | |
1283 | * reside on the stack. The caller must fill desc as follows: desc.tfm is filled | |
1284 | * with the block cipher handle; desc.flags is filled with either | |
1285 | * CRYPTO_TFM_REQ_MAY_SLEEP or 0. | |
1286 | * | |
1287 | * Return: 0 if the cipher operation was successful; < 0 if an error occurred | |
1288 | */ | |
5cde0af2 HX |
1289 | static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc, |
1290 | struct scatterlist *dst, | |
1291 | struct scatterlist *src, | |
1292 | unsigned int nbytes) | |
1293 | { | |
1294 | desc->info = crypto_blkcipher_crt(desc->tfm)->iv; | |
1295 | return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes); | |
1296 | } | |
1297 | ||
58284f0d SM |
1298 | /** |
1299 | * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV | |
1300 | * @desc: reference to the block cipher handle with meta data | |
1301 | * @dst: scatter/gather list that is filled by the cipher operation with the | |
1302 | * ciphertext | |
1303 | * @src: scatter/gather list that holds the plaintext | |
1304 | * @nbytes: number of bytes of the plaintext to encrypt. | |
1305 | * | |
1306 | * Encrypt plaintext data with the use of an IV that is solely used for this | |
1307 | * cipher operation. Any previously set IV is not used. | |
1308 | * | |
1309 | * The blkcipher_desc data structure must be filled by the caller and can | |
1310 | * reside on the stack. The caller must fill desc as follows: desc.tfm is filled | |
1311 | * with the block cipher handle; desc.info is filled with the IV to be used for | |
1312 | * the current operation; desc.flags is filled with either | |
1313 | * CRYPTO_TFM_REQ_MAY_SLEEP or 0. | |
1314 | * | |
1315 | * Return: 0 if the cipher operation was successful; < 0 if an error occurred | |
1316 | */ | |
5cde0af2 HX |
1317 | static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc, |
1318 | struct scatterlist *dst, | |
1319 | struct scatterlist *src, | |
1320 | unsigned int nbytes) | |
1321 | { | |
1322 | return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes); | |
1323 | } | |
1324 | ||
58284f0d SM |
1325 | /** |
1326 | * crypto_blkcipher_decrypt() - decrypt ciphertext | |
1327 | * @desc: reference to the block cipher handle with meta data | |
1328 | * @dst: scatter/gather list that is filled by the cipher operation with the | |
1329 | * plaintext | |
1330 | * @src: scatter/gather list that holds the ciphertext | |
1331 | * @nbytes: number of bytes of the ciphertext to decrypt. | |
1332 | * | |
1333 | * Decrypt ciphertext data using the IV set by the caller with a preceding | |
1334 | * call of crypto_blkcipher_set_iv. | |
1335 | * | |
1336 | * The blkcipher_desc data structure must be filled by the caller as documented | |
1337 | * for the crypto_blkcipher_encrypt call above. | |
1338 | * | |
1339 | * Return: 0 if the cipher operation was successful; < 0 if an error occurred | |
1340 | * | |
1341 | */ | |
5cde0af2 HX |
1342 | static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc, |
1343 | struct scatterlist *dst, | |
1344 | struct scatterlist *src, | |
1345 | unsigned int nbytes) | |
1346 | { | |
1347 | desc->info = crypto_blkcipher_crt(desc->tfm)->iv; | |
1348 | return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes); | |
1349 | } | |
1350 | ||
58284f0d SM |
1351 | /** |
1352 | * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV | |
1353 | * @desc: reference to the block cipher handle with meta data | |
1354 | * @dst: scatter/gather list that is filled by the cipher operation with the | |
1355 | * plaintext | |
1356 | * @src: scatter/gather list that holds the ciphertext | |
1357 | * @nbytes: number of bytes of the ciphertext to decrypt. | |
1358 | * | |
1359 | * Decrypt ciphertext data with the use of an IV that is solely used for this | |
1360 | * cipher operation. Any previously set IV is not used. | |
1361 | * | |
1362 | * The blkcipher_desc data structure must be filled by the caller as documented | |
1363 | * for the crypto_blkcipher_encrypt_iv call above. | |
1364 | * | |
1365 | * Return: 0 if the cipher operation was successful; < 0 if an error occurred | |
1366 | */ | |
5cde0af2 HX |
1367 | static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc, |
1368 | struct scatterlist *dst, | |
1369 | struct scatterlist *src, | |
1370 | unsigned int nbytes) | |
1371 | { | |
1372 | return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes); | |
1373 | } | |
1374 | ||
58284f0d SM |
1375 | /** |
1376 | * crypto_blkcipher_set_iv() - set IV for cipher | |
1377 | * @tfm: cipher handle | |
1378 | * @src: buffer holding the IV | |
1379 | * @len: length of the IV in bytes | |
1380 | * | |
1381 | * The caller provided IV is set for the block cipher referenced by the cipher | |
1382 | * handle. | |
1383 | */ | |
5cde0af2 HX |
1384 | static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm, |
1385 | const u8 *src, unsigned int len) | |
1386 | { | |
1387 | memcpy(crypto_blkcipher_crt(tfm)->iv, src, len); | |
1388 | } | |
1389 | ||
58284f0d SM |
1390 | /** |
1391 | * crypto_blkcipher_get_iv() - obtain IV from cipher | |
1392 | * @tfm: cipher handle | |
1393 | * @dst: buffer filled with the IV | |
1394 | * @len: length of the buffer dst | |
1395 | * | |
1396 | * The caller can obtain the IV set for the block cipher referenced by the | |
1397 | * cipher handle and store it into the user-provided buffer. If the buffer | |
1398 | * has an insufficient space, the IV is truncated to fit the buffer. | |
1399 | */ | |
5cde0af2 HX |
1400 | static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm, |
1401 | u8 *dst, unsigned int len) | |
1402 | { | |
1403 | memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len); | |
1404 | } | |
1405 | ||
16e61030 SM |
1406 | /** |
1407 | * DOC: Single Block Cipher API | |
1408 | * | |
1409 | * The single block cipher API is used with the ciphers of type | |
1410 | * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto). | |
1411 | * | |
1412 | * Using the single block cipher API calls, operations with the basic cipher | |
1413 | * primitive can be implemented. These cipher primitives exclude any block | |
1414 | * chaining operations including IV handling. | |
1415 | * | |
1416 | * The purpose of this single block cipher API is to support the implementation | |
1417 | * of templates or other concepts that only need to perform the cipher operation | |
1418 | * on one block at a time. Templates invoke the underlying cipher primitive | |
1419 | * block-wise and process either the input or the output data of these cipher | |
1420 | * operations. | |
1421 | */ | |
1422 | ||
f28776a3 HX |
1423 | static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm) |
1424 | { | |
1425 | return (struct crypto_cipher *)tfm; | |
1426 | } | |
1427 | ||
1428 | static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm) | |
1429 | { | |
1430 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER); | |
1431 | return __crypto_cipher_cast(tfm); | |
1432 | } | |
1433 | ||
16e61030 SM |
1434 | /** |
1435 | * crypto_alloc_cipher() - allocate single block cipher handle | |
1436 | * @alg_name: is the cra_name / name or cra_driver_name / driver name of the | |
1437 | * single block cipher | |
1438 | * @type: specifies the type of the cipher | |
1439 | * @mask: specifies the mask for the cipher | |
1440 | * | |
1441 | * Allocate a cipher handle for a single block cipher. The returned struct | |
1442 | * crypto_cipher is the cipher handle that is required for any subsequent API | |
1443 | * invocation for that single block cipher. | |
1444 | * | |
1445 | * Return: allocated cipher handle in case of success; IS_ERR() is true in case | |
1446 | * of an error, PTR_ERR() returns the error code. | |
1447 | */ | |
f28776a3 HX |
1448 | static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name, |
1449 | u32 type, u32 mask) | |
1450 | { | |
1451 | type &= ~CRYPTO_ALG_TYPE_MASK; | |
1452 | type |= CRYPTO_ALG_TYPE_CIPHER; | |
1453 | mask |= CRYPTO_ALG_TYPE_MASK; | |
1454 | ||
1455 | return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask)); | |
1456 | } | |
1457 | ||
1458 | static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm) | |
1459 | { | |
78a1fe4f | 1460 | return &tfm->base; |
f28776a3 HX |
1461 | } |
1462 | ||
16e61030 SM |
1463 | /** |
1464 | * crypto_free_cipher() - zeroize and free the single block cipher handle | |
1465 | * @tfm: cipher handle to be freed | |
1466 | */ | |
f28776a3 HX |
1467 | static inline void crypto_free_cipher(struct crypto_cipher *tfm) |
1468 | { | |
1469 | crypto_free_tfm(crypto_cipher_tfm(tfm)); | |
1470 | } | |
1471 | ||
16e61030 SM |
1472 | /** |
1473 | * crypto_has_cipher() - Search for the availability of a single block cipher | |
1474 | * @alg_name: is the cra_name / name or cra_driver_name / driver name of the | |
1475 | * single block cipher | |
1476 | * @type: specifies the type of the cipher | |
1477 | * @mask: specifies the mask for the cipher | |
1478 | * | |
1479 | * Return: true when the single block cipher is known to the kernel crypto API; | |
1480 | * false otherwise | |
1481 | */ | |
fce32d70 HX |
1482 | static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask) |
1483 | { | |
1484 | type &= ~CRYPTO_ALG_TYPE_MASK; | |
1485 | type |= CRYPTO_ALG_TYPE_CIPHER; | |
1486 | mask |= CRYPTO_ALG_TYPE_MASK; | |
1487 | ||
1488 | return crypto_has_alg(alg_name, type, mask); | |
1489 | } | |
1490 | ||
f28776a3 HX |
1491 | static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm) |
1492 | { | |
1493 | return &crypto_cipher_tfm(tfm)->crt_cipher; | |
1494 | } | |
1495 | ||
16e61030 SM |
1496 | /** |
1497 | * crypto_cipher_blocksize() - obtain block size for cipher | |
1498 | * @tfm: cipher handle | |
1499 | * | |
1500 | * The block size for the single block cipher referenced with the cipher handle | |
1501 | * tfm is returned. The caller may use that information to allocate appropriate | |
1502 | * memory for the data returned by the encryption or decryption operation | |
1503 | * | |
1504 | * Return: block size of cipher | |
1505 | */ | |
f28776a3 HX |
1506 | static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm) |
1507 | { | |
1508 | return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm)); | |
1509 | } | |
1510 | ||
1511 | static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm) | |
1512 | { | |
1513 | return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm)); | |
1514 | } | |
1515 | ||
1516 | static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm) | |
1517 | { | |
1518 | return crypto_tfm_get_flags(crypto_cipher_tfm(tfm)); | |
1519 | } | |
1520 | ||
1521 | static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm, | |
1522 | u32 flags) | |
1523 | { | |
1524 | crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags); | |
1525 | } | |
1526 | ||
1527 | static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm, | |
1528 | u32 flags) | |
1529 | { | |
1530 | crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags); | |
1531 | } | |
1532 | ||
16e61030 SM |
1533 | /** |
1534 | * crypto_cipher_setkey() - set key for cipher | |
1535 | * @tfm: cipher handle | |
1536 | * @key: buffer holding the key | |
1537 | * @keylen: length of the key in bytes | |
1538 | * | |
1539 | * The caller provided key is set for the single block cipher referenced by the | |
1540 | * cipher handle. | |
1541 | * | |
1542 | * Note, the key length determines the cipher type. Many block ciphers implement | |
1543 | * different cipher modes depending on the key size, such as AES-128 vs AES-192 | |
1544 | * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128 | |
1545 | * is performed. | |
1546 | * | |
1547 | * Return: 0 if the setting of the key was successful; < 0 if an error occurred | |
1548 | */ | |
7226bc87 HX |
1549 | static inline int crypto_cipher_setkey(struct crypto_cipher *tfm, |
1550 | const u8 *key, unsigned int keylen) | |
1551 | { | |
1552 | return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm), | |
1553 | key, keylen); | |
1554 | } | |
1555 | ||
16e61030 SM |
1556 | /** |
1557 | * crypto_cipher_encrypt_one() - encrypt one block of plaintext | |
1558 | * @tfm: cipher handle | |
1559 | * @dst: points to the buffer that will be filled with the ciphertext | |
1560 | * @src: buffer holding the plaintext to be encrypted | |
1561 | * | |
1562 | * Invoke the encryption operation of one block. The caller must ensure that | |
1563 | * the plaintext and ciphertext buffers are at least one block in size. | |
1564 | */ | |
f28776a3 HX |
1565 | static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm, |
1566 | u8 *dst, const u8 *src) | |
1567 | { | |
1568 | crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm), | |
1569 | dst, src); | |
1570 | } | |
1571 | ||
16e61030 SM |
1572 | /** |
1573 | * crypto_cipher_decrypt_one() - decrypt one block of ciphertext | |
1574 | * @tfm: cipher handle | |
1575 | * @dst: points to the buffer that will be filled with the plaintext | |
1576 | * @src: buffer holding the ciphertext to be decrypted | |
1577 | * | |
1578 | * Invoke the decryption operation of one block. The caller must ensure that | |
1579 | * the plaintext and ciphertext buffers are at least one block in size. | |
1580 | */ | |
f28776a3 HX |
1581 | static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm, |
1582 | u8 *dst, const u8 *src) | |
1583 | { | |
1584 | crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm), | |
1585 | dst, src); | |
1586 | } | |
1587 | ||
fce32d70 HX |
1588 | static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm) |
1589 | { | |
1590 | return (struct crypto_comp *)tfm; | |
1591 | } | |
1592 | ||
1593 | static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm) | |
1594 | { | |
1595 | BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) & | |
1596 | CRYPTO_ALG_TYPE_MASK); | |
1597 | return __crypto_comp_cast(tfm); | |
1598 | } | |
1599 | ||
1600 | static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name, | |
1601 | u32 type, u32 mask) | |
1602 | { | |
1603 | type &= ~CRYPTO_ALG_TYPE_MASK; | |
1604 | type |= CRYPTO_ALG_TYPE_COMPRESS; | |
1605 | mask |= CRYPTO_ALG_TYPE_MASK; | |
1606 | ||
1607 | return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask)); | |
1608 | } | |
1609 | ||
1610 | static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm) | |
1611 | { | |
78a1fe4f | 1612 | return &tfm->base; |
fce32d70 HX |
1613 | } |
1614 | ||
1615 | static inline void crypto_free_comp(struct crypto_comp *tfm) | |
1616 | { | |
1617 | crypto_free_tfm(crypto_comp_tfm(tfm)); | |
1618 | } | |
1619 | ||
1620 | static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask) | |
1621 | { | |
1622 | type &= ~CRYPTO_ALG_TYPE_MASK; | |
1623 | type |= CRYPTO_ALG_TYPE_COMPRESS; | |
1624 | mask |= CRYPTO_ALG_TYPE_MASK; | |
1625 | ||
1626 | return crypto_has_alg(alg_name, type, mask); | |
1627 | } | |
1628 | ||
e4d5b79c HX |
1629 | static inline const char *crypto_comp_name(struct crypto_comp *tfm) |
1630 | { | |
1631 | return crypto_tfm_alg_name(crypto_comp_tfm(tfm)); | |
1632 | } | |
1633 | ||
fce32d70 HX |
1634 | static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm) |
1635 | { | |
1636 | return &crypto_comp_tfm(tfm)->crt_compress; | |
1637 | } | |
1638 | ||
1639 | static inline int crypto_comp_compress(struct crypto_comp *tfm, | |
1da177e4 LT |
1640 | const u8 *src, unsigned int slen, |
1641 | u8 *dst, unsigned int *dlen) | |
1642 | { | |
78a1fe4f HX |
1643 | return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm), |
1644 | src, slen, dst, dlen); | |
1da177e4 LT |
1645 | } |
1646 | ||
fce32d70 | 1647 | static inline int crypto_comp_decompress(struct crypto_comp *tfm, |
1da177e4 LT |
1648 | const u8 *src, unsigned int slen, |
1649 | u8 *dst, unsigned int *dlen) | |
1650 | { | |
78a1fe4f HX |
1651 | return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm), |
1652 | src, slen, dst, dlen); | |
1da177e4 LT |
1653 | } |
1654 | ||
1da177e4 LT |
1655 | #endif /* _LINUX_CRYPTO_H */ |
1656 |