]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - include/linux/crypto.h
crypto: rng - Convert crypto_rng to new style crypto_type
[mirror_ubuntu-zesty-kernel.git] / include / linux / crypto.h
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)
6 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
7 *
8 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
9 * and Nettle, by Niels Möller.
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
20 #include <linux/atomic.h>
21 #include <linux/kernel.h>
22 #include <linux/list.h>
23 #include <linux/bug.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/uaccess.h>
27
28 /*
29 * Autoloaded crypto modules should only use a prefixed name to avoid allowing
30 * arbitrary modules to be loaded. Loading from userspace may still need the
31 * unprefixed names, so retains those aliases as well.
32 * This uses __MODULE_INFO directly instead of MODULE_ALIAS because pre-4.3
33 * gcc (e.g. avr32 toolchain) uses __LINE__ for uniqueness, and this macro
34 * expands twice on the same line. Instead, use a separate base name for the
35 * alias.
36 */
37 #define MODULE_ALIAS_CRYPTO(name) \
38 __MODULE_INFO(alias, alias_userspace, name); \
39 __MODULE_INFO(alias, alias_crypto, "crypto-" name)
40
41 /*
42 * Algorithm masks and types.
43 */
44 #define CRYPTO_ALG_TYPE_MASK 0x0000000f
45 #define CRYPTO_ALG_TYPE_CIPHER 0x00000001
46 #define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
47 #define CRYPTO_ALG_TYPE_AEAD 0x00000003
48 #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
49 #define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
50 #define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
51 #define CRYPTO_ALG_TYPE_DIGEST 0x00000008
52 #define CRYPTO_ALG_TYPE_HASH 0x00000008
53 #define CRYPTO_ALG_TYPE_SHASH 0x00000009
54 #define CRYPTO_ALG_TYPE_AHASH 0x0000000a
55 #define CRYPTO_ALG_TYPE_RNG 0x0000000c
56 #define CRYPTO_ALG_TYPE_PCOMPRESS 0x0000000f
57
58 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
59 #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
60 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
61
62 #define CRYPTO_ALG_LARVAL 0x00000010
63 #define CRYPTO_ALG_DEAD 0x00000020
64 #define CRYPTO_ALG_DYING 0x00000040
65 #define CRYPTO_ALG_ASYNC 0x00000080
66
67 /*
68 * Set this bit if and only if the algorithm requires another algorithm of
69 * the same type to handle corner cases.
70 */
71 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
72
73 /*
74 * This bit is set for symmetric key ciphers that have already been wrapped
75 * with a generic IV generator to prevent them from being wrapped again.
76 */
77 #define CRYPTO_ALG_GENIV 0x00000200
78
79 /*
80 * Set if the algorithm has passed automated run-time testing. Note that
81 * if there is no run-time testing for a given algorithm it is considered
82 * to have passed.
83 */
84
85 #define CRYPTO_ALG_TESTED 0x00000400
86
87 /*
88 * Set if the algorithm is an instance that is build from templates.
89 */
90 #define CRYPTO_ALG_INSTANCE 0x00000800
91
92 /* Set this bit if the algorithm provided is hardware accelerated but
93 * not available to userspace via instruction set or so.
94 */
95 #define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
96
97 /*
98 * Mark a cipher as a service implementation only usable by another
99 * cipher and never by a normal user of the kernel crypto API
100 */
101 #define CRYPTO_ALG_INTERNAL 0x00002000
102
103 /*
104 * Transform masks and values (for crt_flags).
105 */
106 #define CRYPTO_TFM_REQ_MASK 0x000fff00
107 #define CRYPTO_TFM_RES_MASK 0xfff00000
108
109 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
110 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
111 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
112 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
113 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
114 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
115 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
116 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
117
118 /*
119 * Miscellaneous stuff.
120 */
121 #define CRYPTO_MAX_ALG_NAME 64
122
123 /*
124 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
125 * declaration) is used to ensure that the crypto_tfm context structure is
126 * aligned correctly for the given architecture so that there are no alignment
127 * faults for C data types. In particular, this is required on platforms such
128 * as arm where pointers are 32-bit aligned but there are data types such as
129 * u64 which require 64-bit alignment.
130 */
131 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
132
133 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
134
135 struct scatterlist;
136 struct crypto_ablkcipher;
137 struct crypto_async_request;
138 struct crypto_aead;
139 struct crypto_blkcipher;
140 struct crypto_hash;
141 struct crypto_rng;
142 struct crypto_tfm;
143 struct crypto_type;
144 struct aead_givcrypt_request;
145 struct skcipher_givcrypt_request;
146
147 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
148
149 /**
150 * DOC: Block Cipher Context Data Structures
151 *
152 * These data structures define the operating context for each block cipher
153 * type.
154 */
155
156 struct crypto_async_request {
157 struct list_head list;
158 crypto_completion_t complete;
159 void *data;
160 struct crypto_tfm *tfm;
161
162 u32 flags;
163 };
164
165 struct ablkcipher_request {
166 struct crypto_async_request base;
167
168 unsigned int nbytes;
169
170 void *info;
171
172 struct scatterlist *src;
173 struct scatterlist *dst;
174
175 void *__ctx[] CRYPTO_MINALIGN_ATTR;
176 };
177
178 /**
179 * struct aead_request - AEAD request
180 * @base: Common attributes for async crypto requests
181 * @assoclen: Length in bytes of associated data for authentication
182 * @cryptlen: Length of data to be encrypted or decrypted
183 * @iv: Initialisation vector
184 * @assoc: Associated data
185 * @src: Source data
186 * @dst: Destination data
187 * @__ctx: Start of private context data
188 */
189 struct aead_request {
190 struct crypto_async_request base;
191
192 unsigned int assoclen;
193 unsigned int cryptlen;
194
195 u8 *iv;
196
197 struct scatterlist *assoc;
198 struct scatterlist *src;
199 struct scatterlist *dst;
200
201 void *__ctx[] CRYPTO_MINALIGN_ATTR;
202 };
203
204 struct blkcipher_desc {
205 struct crypto_blkcipher *tfm;
206 void *info;
207 u32 flags;
208 };
209
210 struct cipher_desc {
211 struct crypto_tfm *tfm;
212 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
213 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
214 const u8 *src, unsigned int nbytes);
215 void *info;
216 };
217
218 struct hash_desc {
219 struct crypto_hash *tfm;
220 u32 flags;
221 };
222
223 /**
224 * DOC: Block Cipher Algorithm Definitions
225 *
226 * These data structures define modular crypto algorithm implementations,
227 * managed via crypto_register_alg() and crypto_unregister_alg().
228 */
229
230 /**
231 * struct ablkcipher_alg - asynchronous block cipher definition
232 * @min_keysize: Minimum key size supported by the transformation. This is the
233 * smallest key length supported by this transformation algorithm.
234 * This must be set to one of the pre-defined values as this is
235 * not hardware specific. Possible values for this field can be
236 * found via git grep "_MIN_KEY_SIZE" include/crypto/
237 * @max_keysize: Maximum key size supported by the transformation. This is the
238 * largest key length supported by this transformation algorithm.
239 * This must be set to one of the pre-defined values as this is
240 * not hardware specific. Possible values for this field can be
241 * found via git grep "_MAX_KEY_SIZE" include/crypto/
242 * @setkey: Set key for the transformation. This function is used to either
243 * program a supplied key into the hardware or store the key in the
244 * transformation context for programming it later. Note that this
245 * function does modify the transformation context. This function can
246 * be called multiple times during the existence of the transformation
247 * object, so one must make sure the key is properly reprogrammed into
248 * the hardware. This function is also responsible for checking the key
249 * length for validity. In case a software fallback was put in place in
250 * the @cra_init call, this function might need to use the fallback if
251 * the algorithm doesn't support all of the key sizes.
252 * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt
253 * the supplied scatterlist containing the blocks of data. The crypto
254 * API consumer is responsible for aligning the entries of the
255 * scatterlist properly and making sure the chunks are correctly
256 * sized. In case a software fallback was put in place in the
257 * @cra_init call, this function might need to use the fallback if
258 * the algorithm doesn't support all of the key sizes. In case the
259 * key was stored in transformation context, the key might need to be
260 * re-programmed into the hardware in this function. This function
261 * shall not modify the transformation context, as this function may
262 * be called in parallel with the same transformation object.
263 * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt
264 * and the conditions are exactly the same.
265 * @givencrypt: Update the IV for encryption. With this function, a cipher
266 * implementation may provide the function on how to update the IV
267 * for encryption.
268 * @givdecrypt: Update the IV for decryption. This is the reverse of
269 * @givencrypt .
270 * @geniv: The transformation implementation may use an "IV generator" provided
271 * by the kernel crypto API. Several use cases have a predefined
272 * approach how IVs are to be updated. For such use cases, the kernel
273 * crypto API provides ready-to-use implementations that can be
274 * referenced with this variable.
275 * @ivsize: IV size applicable for transformation. The consumer must provide an
276 * IV of exactly that size to perform the encrypt or decrypt operation.
277 *
278 * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
279 * mandatory and must be filled.
280 */
281 struct ablkcipher_alg {
282 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
283 unsigned int keylen);
284 int (*encrypt)(struct ablkcipher_request *req);
285 int (*decrypt)(struct ablkcipher_request *req);
286 int (*givencrypt)(struct skcipher_givcrypt_request *req);
287 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
288
289 const char *geniv;
290
291 unsigned int min_keysize;
292 unsigned int max_keysize;
293 unsigned int ivsize;
294 };
295
296 /**
297 * struct aead_alg - AEAD cipher definition
298 * @maxauthsize: Set the maximum authentication tag size supported by the
299 * transformation. A transformation may support smaller tag sizes.
300 * As the authentication tag is a message digest to ensure the
301 * integrity of the encrypted data, a consumer typically wants the
302 * largest authentication tag possible as defined by this
303 * variable.
304 * @setauthsize: Set authentication size for the AEAD transformation. This
305 * function is used to specify the consumer requested size of the
306 * authentication tag to be either generated by the transformation
307 * during encryption or the size of the authentication tag to be
308 * supplied during the decryption operation. This function is also
309 * responsible for checking the authentication tag size for
310 * validity.
311 * @setkey: see struct ablkcipher_alg
312 * @encrypt: see struct ablkcipher_alg
313 * @decrypt: see struct ablkcipher_alg
314 * @givencrypt: see struct ablkcipher_alg
315 * @givdecrypt: see struct ablkcipher_alg
316 * @geniv: see struct ablkcipher_alg
317 * @ivsize: see struct ablkcipher_alg
318 *
319 * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
320 * mandatory and must be filled.
321 */
322 struct aead_alg {
323 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
324 unsigned int keylen);
325 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
326 int (*encrypt)(struct aead_request *req);
327 int (*decrypt)(struct aead_request *req);
328 int (*givencrypt)(struct aead_givcrypt_request *req);
329 int (*givdecrypt)(struct aead_givcrypt_request *req);
330
331 const char *geniv;
332
333 unsigned int ivsize;
334 unsigned int maxauthsize;
335 };
336
337 /**
338 * struct blkcipher_alg - synchronous block cipher definition
339 * @min_keysize: see struct ablkcipher_alg
340 * @max_keysize: see struct ablkcipher_alg
341 * @setkey: see struct ablkcipher_alg
342 * @encrypt: see struct ablkcipher_alg
343 * @decrypt: see struct ablkcipher_alg
344 * @geniv: see struct ablkcipher_alg
345 * @ivsize: see struct ablkcipher_alg
346 *
347 * All fields except @geniv and @ivsize are mandatory and must be filled.
348 */
349 struct blkcipher_alg {
350 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
351 unsigned int keylen);
352 int (*encrypt)(struct blkcipher_desc *desc,
353 struct scatterlist *dst, struct scatterlist *src,
354 unsigned int nbytes);
355 int (*decrypt)(struct blkcipher_desc *desc,
356 struct scatterlist *dst, struct scatterlist *src,
357 unsigned int nbytes);
358
359 const char *geniv;
360
361 unsigned int min_keysize;
362 unsigned int max_keysize;
363 unsigned int ivsize;
364 };
365
366 /**
367 * struct cipher_alg - single-block symmetric ciphers definition
368 * @cia_min_keysize: Minimum key size supported by the transformation. This is
369 * the smallest key length supported by this transformation
370 * algorithm. This must be set to one of the pre-defined
371 * values as this is not hardware specific. Possible values
372 * for this field can be found via git grep "_MIN_KEY_SIZE"
373 * include/crypto/
374 * @cia_max_keysize: Maximum key size supported by the transformation. This is
375 * the largest key length supported by this transformation
376 * algorithm. This must be set to one of the pre-defined values
377 * as this is not hardware specific. Possible values for this
378 * field can be found via git grep "_MAX_KEY_SIZE"
379 * include/crypto/
380 * @cia_setkey: Set key for the transformation. This function is used to either
381 * program a supplied key into the hardware or store the key in the
382 * transformation context for programming it later. Note that this
383 * function does modify the transformation context. This function
384 * can be called multiple times during the existence of the
385 * transformation object, so one must make sure the key is properly
386 * reprogrammed into the hardware. This function is also
387 * responsible for checking the key length for validity.
388 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
389 * single block of data, which must be @cra_blocksize big. This
390 * always operates on a full @cra_blocksize and it is not possible
391 * to encrypt a block of smaller size. The supplied buffers must
392 * therefore also be at least of @cra_blocksize size. Both the
393 * input and output buffers are always aligned to @cra_alignmask.
394 * In case either of the input or output buffer supplied by user
395 * of the crypto API is not aligned to @cra_alignmask, the crypto
396 * API will re-align the buffers. The re-alignment means that a
397 * new buffer will be allocated, the data will be copied into the
398 * new buffer, then the processing will happen on the new buffer,
399 * then the data will be copied back into the original buffer and
400 * finally the new buffer will be freed. In case a software
401 * fallback was put in place in the @cra_init call, this function
402 * might need to use the fallback if the algorithm doesn't support
403 * all of the key sizes. In case the key was stored in
404 * transformation context, the key might need to be re-programmed
405 * into the hardware in this function. This function shall not
406 * modify the transformation context, as this function may be
407 * called in parallel with the same transformation object.
408 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
409 * @cia_encrypt, and the conditions are exactly the same.
410 *
411 * All fields are mandatory and must be filled.
412 */
413 struct cipher_alg {
414 unsigned int cia_min_keysize;
415 unsigned int cia_max_keysize;
416 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
417 unsigned int keylen);
418 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
419 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
420 };
421
422 struct compress_alg {
423 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
424 unsigned int slen, u8 *dst, unsigned int *dlen);
425 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
426 unsigned int slen, u8 *dst, unsigned int *dlen);
427 };
428
429 /**
430 * struct rng_alg - random number generator definition
431 * @rng_make_random: The function defined by this variable obtains a random
432 * number. The random number generator transform must generate
433 * the random number out of the context provided with this
434 * call.
435 * @rng_reset: Reset of the random number generator by clearing the entire state.
436 * With the invocation of this function call, the random number
437 * generator shall completely reinitialize its state. If the random
438 * number generator requires a seed for setting up a new state,
439 * the seed must be provided by the consumer while invoking this
440 * function. The required size of the seed is defined with
441 * @seedsize .
442 * @seedsize: The seed size required for a random number generator
443 * initialization defined with this variable. Some random number
444 * generators like the SP800-90A DRBG does not require a seed as the
445 * seeding is implemented internally without the need of support by
446 * the consumer. In this case, the seed size is set to zero.
447 */
448 struct rng_alg {
449 int (*rng_make_random)(struct crypto_rng *tfm, u8 *rdata,
450 unsigned int dlen);
451 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
452
453 unsigned int seedsize;
454 };
455
456
457 #define cra_ablkcipher cra_u.ablkcipher
458 #define cra_aead cra_u.aead
459 #define cra_blkcipher cra_u.blkcipher
460 #define cra_cipher cra_u.cipher
461 #define cra_compress cra_u.compress
462 #define cra_rng cra_u.rng
463
464 /**
465 * struct crypto_alg - definition of a cryptograpic cipher algorithm
466 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
467 * CRYPTO_ALG_* flags for the flags which go in here. Those are
468 * used for fine-tuning the description of the transformation
469 * algorithm.
470 * @cra_blocksize: Minimum block size of this transformation. The size in bytes
471 * of the smallest possible unit which can be transformed with
472 * this algorithm. The users must respect this value.
473 * In case of HASH transformation, it is possible for a smaller
474 * block than @cra_blocksize to be passed to the crypto API for
475 * transformation, in case of any other transformation type, an
476 * error will be returned upon any attempt to transform smaller
477 * than @cra_blocksize chunks.
478 * @cra_ctxsize: Size of the operational context of the transformation. This
479 * value informs the kernel crypto API about the memory size
480 * needed to be allocated for the transformation context.
481 * @cra_alignmask: Alignment mask for the input and output data buffer. The data
482 * buffer containing the input data for the algorithm must be
483 * aligned to this alignment mask. The data buffer for the
484 * output data must be aligned to this alignment mask. Note that
485 * the Crypto API will do the re-alignment in software, but
486 * only under special conditions and there is a performance hit.
487 * The re-alignment happens at these occasions for different
488 * @cra_u types: cipher -- For both input data and output data
489 * buffer; ahash -- For output hash destination buf; shash --
490 * For output hash destination buf.
491 * This is needed on hardware which is flawed by design and
492 * cannot pick data from arbitrary addresses.
493 * @cra_priority: Priority of this transformation implementation. In case
494 * multiple transformations with same @cra_name are available to
495 * the Crypto API, the kernel will use the one with highest
496 * @cra_priority.
497 * @cra_name: Generic name (usable by multiple implementations) of the
498 * transformation algorithm. This is the name of the transformation
499 * itself. This field is used by the kernel when looking up the
500 * providers of particular transformation.
501 * @cra_driver_name: Unique name of the transformation provider. This is the
502 * name of the provider of the transformation. This can be any
503 * arbitrary value, but in the usual case, this contains the
504 * name of the chip or provider and the name of the
505 * transformation algorithm.
506 * @cra_type: Type of the cryptographic transformation. This is a pointer to
507 * struct crypto_type, which implements callbacks common for all
508 * trasnformation types. There are multiple options:
509 * &crypto_blkcipher_type, &crypto_ablkcipher_type,
510 * &crypto_ahash_type, &crypto_aead_type, &crypto_rng_type.
511 * This field might be empty. In that case, there are no common
512 * callbacks. This is the case for: cipher, compress, shash.
513 * @cra_u: Callbacks implementing the transformation. This is a union of
514 * multiple structures. Depending on the type of transformation selected
515 * by @cra_type and @cra_flags above, the associated structure must be
516 * filled with callbacks. This field might be empty. This is the case
517 * for ahash, shash.
518 * @cra_init: Initialize the cryptographic transformation object. This function
519 * is used to initialize the cryptographic transformation object.
520 * This function is called only once at the instantiation time, right
521 * after the transformation context was allocated. In case the
522 * cryptographic hardware has some special requirements which need to
523 * be handled by software, this function shall check for the precise
524 * requirement of the transformation and put any software fallbacks
525 * in place.
526 * @cra_exit: Deinitialize the cryptographic transformation object. This is a
527 * counterpart to @cra_init, used to remove various changes set in
528 * @cra_init.
529 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
530 * @cra_list: internally used
531 * @cra_users: internally used
532 * @cra_refcnt: internally used
533 * @cra_destroy: internally used
534 *
535 * The struct crypto_alg describes a generic Crypto API algorithm and is common
536 * for all of the transformations. Any variable not documented here shall not
537 * be used by a cipher implementation as it is internal to the Crypto API.
538 */
539 struct crypto_alg {
540 struct list_head cra_list;
541 struct list_head cra_users;
542
543 u32 cra_flags;
544 unsigned int cra_blocksize;
545 unsigned int cra_ctxsize;
546 unsigned int cra_alignmask;
547
548 int cra_priority;
549 atomic_t cra_refcnt;
550
551 char cra_name[CRYPTO_MAX_ALG_NAME];
552 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
553
554 const struct crypto_type *cra_type;
555
556 union {
557 struct ablkcipher_alg ablkcipher;
558 struct aead_alg aead;
559 struct blkcipher_alg blkcipher;
560 struct cipher_alg cipher;
561 struct compress_alg compress;
562 struct rng_alg rng;
563 } cra_u;
564
565 int (*cra_init)(struct crypto_tfm *tfm);
566 void (*cra_exit)(struct crypto_tfm *tfm);
567 void (*cra_destroy)(struct crypto_alg *alg);
568
569 struct module *cra_module;
570 };
571
572 /*
573 * Algorithm registration interface.
574 */
575 int crypto_register_alg(struct crypto_alg *alg);
576 int crypto_unregister_alg(struct crypto_alg *alg);
577 int crypto_register_algs(struct crypto_alg *algs, int count);
578 int crypto_unregister_algs(struct crypto_alg *algs, int count);
579
580 /*
581 * Algorithm query interface.
582 */
583 int crypto_has_alg(const char *name, u32 type, u32 mask);
584
585 /*
586 * Transforms: user-instantiated objects which encapsulate algorithms
587 * and core processing logic. Managed via crypto_alloc_*() and
588 * crypto_free_*(), as well as the various helpers below.
589 */
590
591 struct ablkcipher_tfm {
592 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
593 unsigned int keylen);
594 int (*encrypt)(struct ablkcipher_request *req);
595 int (*decrypt)(struct ablkcipher_request *req);
596 int (*givencrypt)(struct skcipher_givcrypt_request *req);
597 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
598
599 struct crypto_ablkcipher *base;
600
601 unsigned int ivsize;
602 unsigned int reqsize;
603 };
604
605 struct aead_tfm {
606 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
607 unsigned int keylen);
608 int (*encrypt)(struct aead_request *req);
609 int (*decrypt)(struct aead_request *req);
610 int (*givencrypt)(struct aead_givcrypt_request *req);
611 int (*givdecrypt)(struct aead_givcrypt_request *req);
612
613 struct crypto_aead *base;
614
615 unsigned int ivsize;
616 unsigned int authsize;
617 unsigned int reqsize;
618 };
619
620 struct blkcipher_tfm {
621 void *iv;
622 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
623 unsigned int keylen);
624 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
625 struct scatterlist *src, unsigned int nbytes);
626 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
627 struct scatterlist *src, unsigned int nbytes);
628 };
629
630 struct cipher_tfm {
631 int (*cit_setkey)(struct crypto_tfm *tfm,
632 const u8 *key, unsigned int keylen);
633 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
634 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
635 };
636
637 struct hash_tfm {
638 int (*init)(struct hash_desc *desc);
639 int (*update)(struct hash_desc *desc,
640 struct scatterlist *sg, unsigned int nsg);
641 int (*final)(struct hash_desc *desc, u8 *out);
642 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
643 unsigned int nsg, u8 *out);
644 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
645 unsigned int keylen);
646 unsigned int digestsize;
647 };
648
649 struct compress_tfm {
650 int (*cot_compress)(struct crypto_tfm *tfm,
651 const u8 *src, unsigned int slen,
652 u8 *dst, unsigned int *dlen);
653 int (*cot_decompress)(struct crypto_tfm *tfm,
654 const u8 *src, unsigned int slen,
655 u8 *dst, unsigned int *dlen);
656 };
657
658 #define crt_ablkcipher crt_u.ablkcipher
659 #define crt_aead crt_u.aead
660 #define crt_blkcipher crt_u.blkcipher
661 #define crt_cipher crt_u.cipher
662 #define crt_hash crt_u.hash
663 #define crt_compress crt_u.compress
664
665 struct crypto_tfm {
666
667 u32 crt_flags;
668
669 union {
670 struct ablkcipher_tfm ablkcipher;
671 struct aead_tfm aead;
672 struct blkcipher_tfm blkcipher;
673 struct cipher_tfm cipher;
674 struct hash_tfm hash;
675 struct compress_tfm compress;
676 } crt_u;
677
678 void (*exit)(struct crypto_tfm *tfm);
679
680 struct crypto_alg *__crt_alg;
681
682 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
683 };
684
685 struct crypto_ablkcipher {
686 struct crypto_tfm base;
687 };
688
689 struct crypto_aead {
690 struct crypto_tfm base;
691 };
692
693 struct crypto_blkcipher {
694 struct crypto_tfm base;
695 };
696
697 struct crypto_cipher {
698 struct crypto_tfm base;
699 };
700
701 struct crypto_comp {
702 struct crypto_tfm base;
703 };
704
705 struct crypto_hash {
706 struct crypto_tfm base;
707 };
708
709 enum {
710 CRYPTOA_UNSPEC,
711 CRYPTOA_ALG,
712 CRYPTOA_TYPE,
713 CRYPTOA_U32,
714 __CRYPTOA_MAX,
715 };
716
717 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
718
719 /* Maximum number of (rtattr) parameters for each template. */
720 #define CRYPTO_MAX_ATTRS 32
721
722 struct crypto_attr_alg {
723 char name[CRYPTO_MAX_ALG_NAME];
724 };
725
726 struct crypto_attr_type {
727 u32 type;
728 u32 mask;
729 };
730
731 struct crypto_attr_u32 {
732 u32 num;
733 };
734
735 /*
736 * Transform user interface.
737 */
738
739 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
740 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
741
742 static inline void crypto_free_tfm(struct crypto_tfm *tfm)
743 {
744 return crypto_destroy_tfm(tfm, tfm);
745 }
746
747 int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
748
749 /*
750 * Transform helpers which query the underlying algorithm.
751 */
752 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
753 {
754 return tfm->__crt_alg->cra_name;
755 }
756
757 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
758 {
759 return tfm->__crt_alg->cra_driver_name;
760 }
761
762 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
763 {
764 return tfm->__crt_alg->cra_priority;
765 }
766
767 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
768 {
769 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
770 }
771
772 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
773 {
774 return tfm->__crt_alg->cra_blocksize;
775 }
776
777 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
778 {
779 return tfm->__crt_alg->cra_alignmask;
780 }
781
782 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
783 {
784 return tfm->crt_flags;
785 }
786
787 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
788 {
789 tfm->crt_flags |= flags;
790 }
791
792 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
793 {
794 tfm->crt_flags &= ~flags;
795 }
796
797 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
798 {
799 return tfm->__crt_ctx;
800 }
801
802 static inline unsigned int crypto_tfm_ctx_alignment(void)
803 {
804 struct crypto_tfm *tfm;
805 return __alignof__(tfm->__crt_ctx);
806 }
807
808 /*
809 * API wrappers.
810 */
811 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
812 struct crypto_tfm *tfm)
813 {
814 return (struct crypto_ablkcipher *)tfm;
815 }
816
817 static inline u32 crypto_skcipher_type(u32 type)
818 {
819 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
820 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
821 return type;
822 }
823
824 static inline u32 crypto_skcipher_mask(u32 mask)
825 {
826 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
827 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
828 return mask;
829 }
830
831 /**
832 * DOC: Asynchronous Block Cipher API
833 *
834 * Asynchronous block cipher API is used with the ciphers of type
835 * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto).
836 *
837 * Asynchronous cipher operations imply that the function invocation for a
838 * cipher request returns immediately before the completion of the operation.
839 * The cipher request is scheduled as a separate kernel thread and therefore
840 * load-balanced on the different CPUs via the process scheduler. To allow
841 * the kernel crypto API to inform the caller about the completion of a cipher
842 * request, the caller must provide a callback function. That function is
843 * invoked with the cipher handle when the request completes.
844 *
845 * To support the asynchronous operation, additional information than just the
846 * cipher handle must be supplied to the kernel crypto API. That additional
847 * information is given by filling in the ablkcipher_request data structure.
848 *
849 * For the asynchronous block cipher API, the state is maintained with the tfm
850 * cipher handle. A single tfm can be used across multiple calls and in
851 * parallel. For asynchronous block cipher calls, context data supplied and
852 * only used by the caller can be referenced the request data structure in
853 * addition to the IV used for the cipher request. The maintenance of such
854 * state information would be important for a crypto driver implementer to
855 * have, because when calling the callback function upon completion of the
856 * cipher operation, that callback function may need some information about
857 * which operation just finished if it invoked multiple in parallel. This
858 * state information is unused by the kernel crypto API.
859 */
860
861 /**
862 * crypto_alloc_ablkcipher() - allocate asynchronous block cipher handle
863 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
864 * ablkcipher cipher
865 * @type: specifies the type of the cipher
866 * @mask: specifies the mask for the cipher
867 *
868 * Allocate a cipher handle for an ablkcipher. The returned struct
869 * crypto_ablkcipher is the cipher handle that is required for any subsequent
870 * API invocation for that ablkcipher.
871 *
872 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
873 * of an error, PTR_ERR() returns the error code.
874 */
875 struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
876 u32 type, u32 mask);
877
878 static inline struct crypto_tfm *crypto_ablkcipher_tfm(
879 struct crypto_ablkcipher *tfm)
880 {
881 return &tfm->base;
882 }
883
884 /**
885 * crypto_free_ablkcipher() - zeroize and free cipher handle
886 * @tfm: cipher handle to be freed
887 */
888 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
889 {
890 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
891 }
892
893 /**
894 * crypto_has_ablkcipher() - Search for the availability of an ablkcipher.
895 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
896 * ablkcipher
897 * @type: specifies the type of the cipher
898 * @mask: specifies the mask for the cipher
899 *
900 * Return: true when the ablkcipher is known to the kernel crypto API; false
901 * otherwise
902 */
903 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
904 u32 mask)
905 {
906 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
907 crypto_skcipher_mask(mask));
908 }
909
910 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
911 struct crypto_ablkcipher *tfm)
912 {
913 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
914 }
915
916 /**
917 * crypto_ablkcipher_ivsize() - obtain IV size
918 * @tfm: cipher handle
919 *
920 * The size of the IV for the ablkcipher referenced by the cipher handle is
921 * returned. This IV size may be zero if the cipher does not need an IV.
922 *
923 * Return: IV size in bytes
924 */
925 static inline unsigned int crypto_ablkcipher_ivsize(
926 struct crypto_ablkcipher *tfm)
927 {
928 return crypto_ablkcipher_crt(tfm)->ivsize;
929 }
930
931 /**
932 * crypto_ablkcipher_blocksize() - obtain block size of cipher
933 * @tfm: cipher handle
934 *
935 * The block size for the ablkcipher referenced with the cipher handle is
936 * returned. The caller may use that information to allocate appropriate
937 * memory for the data returned by the encryption or decryption operation
938 *
939 * Return: block size of cipher
940 */
941 static inline unsigned int crypto_ablkcipher_blocksize(
942 struct crypto_ablkcipher *tfm)
943 {
944 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
945 }
946
947 static inline unsigned int crypto_ablkcipher_alignmask(
948 struct crypto_ablkcipher *tfm)
949 {
950 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
951 }
952
953 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
954 {
955 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
956 }
957
958 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
959 u32 flags)
960 {
961 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
962 }
963
964 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
965 u32 flags)
966 {
967 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
968 }
969
970 /**
971 * crypto_ablkcipher_setkey() - set key for cipher
972 * @tfm: cipher handle
973 * @key: buffer holding the key
974 * @keylen: length of the key in bytes
975 *
976 * The caller provided key is set for the ablkcipher referenced by the cipher
977 * handle.
978 *
979 * Note, the key length determines the cipher type. Many block ciphers implement
980 * different cipher modes depending on the key size, such as AES-128 vs AES-192
981 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
982 * is performed.
983 *
984 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
985 */
986 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
987 const u8 *key, unsigned int keylen)
988 {
989 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
990
991 return crt->setkey(crt->base, key, keylen);
992 }
993
994 /**
995 * crypto_ablkcipher_reqtfm() - obtain cipher handle from request
996 * @req: ablkcipher_request out of which the cipher handle is to be obtained
997 *
998 * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request
999 * data structure.
1000 *
1001 * Return: crypto_ablkcipher handle
1002 */
1003 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
1004 struct ablkcipher_request *req)
1005 {
1006 return __crypto_ablkcipher_cast(req->base.tfm);
1007 }
1008
1009 /**
1010 * crypto_ablkcipher_encrypt() - encrypt plaintext
1011 * @req: reference to the ablkcipher_request handle that holds all information
1012 * needed to perform the cipher operation
1013 *
1014 * Encrypt plaintext data using the ablkcipher_request handle. That data
1015 * structure and how it is filled with data is discussed with the
1016 * ablkcipher_request_* functions.
1017 *
1018 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1019 */
1020 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
1021 {
1022 struct ablkcipher_tfm *crt =
1023 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
1024 return crt->encrypt(req);
1025 }
1026
1027 /**
1028 * crypto_ablkcipher_decrypt() - decrypt ciphertext
1029 * @req: reference to the ablkcipher_request handle that holds all information
1030 * needed to perform the cipher operation
1031 *
1032 * Decrypt ciphertext data using the ablkcipher_request handle. That data
1033 * structure and how it is filled with data is discussed with the
1034 * ablkcipher_request_* functions.
1035 *
1036 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1037 */
1038 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
1039 {
1040 struct ablkcipher_tfm *crt =
1041 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
1042 return crt->decrypt(req);
1043 }
1044
1045 /**
1046 * DOC: Asynchronous Cipher Request Handle
1047 *
1048 * The ablkcipher_request data structure contains all pointers to data
1049 * required for the asynchronous cipher operation. This includes the cipher
1050 * handle (which can be used by multiple ablkcipher_request instances), pointer
1051 * to plaintext and ciphertext, asynchronous callback function, etc. It acts
1052 * as a handle to the ablkcipher_request_* API calls in a similar way as
1053 * ablkcipher handle to the crypto_ablkcipher_* API calls.
1054 */
1055
1056 /**
1057 * crypto_ablkcipher_reqsize() - obtain size of the request data structure
1058 * @tfm: cipher handle
1059 *
1060 * Return: number of bytes
1061 */
1062 static inline unsigned int crypto_ablkcipher_reqsize(
1063 struct crypto_ablkcipher *tfm)
1064 {
1065 return crypto_ablkcipher_crt(tfm)->reqsize;
1066 }
1067
1068 /**
1069 * ablkcipher_request_set_tfm() - update cipher handle reference in request
1070 * @req: request handle to be modified
1071 * @tfm: cipher handle that shall be added to the request handle
1072 *
1073 * Allow the caller to replace the existing ablkcipher handle in the request
1074 * data structure with a different one.
1075 */
1076 static inline void ablkcipher_request_set_tfm(
1077 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
1078 {
1079 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
1080 }
1081
1082 static inline struct ablkcipher_request *ablkcipher_request_cast(
1083 struct crypto_async_request *req)
1084 {
1085 return container_of(req, struct ablkcipher_request, base);
1086 }
1087
1088 /**
1089 * ablkcipher_request_alloc() - allocate request data structure
1090 * @tfm: cipher handle to be registered with the request
1091 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
1092 *
1093 * Allocate the request data structure that must be used with the ablkcipher
1094 * encrypt and decrypt API calls. During the allocation, the provided ablkcipher
1095 * handle is registered in the request data structure.
1096 *
1097 * Return: allocated request handle in case of success; IS_ERR() is true in case
1098 * of an error, PTR_ERR() returns the error code.
1099 */
1100 static inline struct ablkcipher_request *ablkcipher_request_alloc(
1101 struct crypto_ablkcipher *tfm, gfp_t gfp)
1102 {
1103 struct ablkcipher_request *req;
1104
1105 req = kmalloc(sizeof(struct ablkcipher_request) +
1106 crypto_ablkcipher_reqsize(tfm), gfp);
1107
1108 if (likely(req))
1109 ablkcipher_request_set_tfm(req, tfm);
1110
1111 return req;
1112 }
1113
1114 /**
1115 * ablkcipher_request_free() - zeroize and free request data structure
1116 * @req: request data structure cipher handle to be freed
1117 */
1118 static inline void ablkcipher_request_free(struct ablkcipher_request *req)
1119 {
1120 kzfree(req);
1121 }
1122
1123 /**
1124 * ablkcipher_request_set_callback() - set asynchronous callback function
1125 * @req: request handle
1126 * @flags: specify zero or an ORing of the flags
1127 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
1128 * increase the wait queue beyond the initial maximum size;
1129 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
1130 * @compl: callback function pointer to be registered with the request handle
1131 * @data: The data pointer refers to memory that is not used by the kernel
1132 * crypto API, but provided to the callback function for it to use. Here,
1133 * the caller can provide a reference to memory the callback function can
1134 * operate on. As the callback function is invoked asynchronously to the
1135 * related functionality, it may need to access data structures of the
1136 * related functionality which can be referenced using this pointer. The
1137 * callback function can access the memory via the "data" field in the
1138 * crypto_async_request data structure provided to the callback function.
1139 *
1140 * This function allows setting the callback function that is triggered once the
1141 * cipher operation completes.
1142 *
1143 * The callback function is registered with the ablkcipher_request handle and
1144 * must comply with the following template
1145 *
1146 * void callback_function(struct crypto_async_request *req, int error)
1147 */
1148 static inline void ablkcipher_request_set_callback(
1149 struct ablkcipher_request *req,
1150 u32 flags, crypto_completion_t compl, void *data)
1151 {
1152 req->base.complete = compl;
1153 req->base.data = data;
1154 req->base.flags = flags;
1155 }
1156
1157 /**
1158 * ablkcipher_request_set_crypt() - set data buffers
1159 * @req: request handle
1160 * @src: source scatter / gather list
1161 * @dst: destination scatter / gather list
1162 * @nbytes: number of bytes to process from @src
1163 * @iv: IV for the cipher operation which must comply with the IV size defined
1164 * by crypto_ablkcipher_ivsize
1165 *
1166 * This function allows setting of the source data and destination data
1167 * scatter / gather lists.
1168 *
1169 * For encryption, the source is treated as the plaintext and the
1170 * destination is the ciphertext. For a decryption operation, the use is
1171 * reversed - the source is the ciphertext and the destination is the plaintext.
1172 */
1173 static inline void ablkcipher_request_set_crypt(
1174 struct ablkcipher_request *req,
1175 struct scatterlist *src, struct scatterlist *dst,
1176 unsigned int nbytes, void *iv)
1177 {
1178 req->src = src;
1179 req->dst = dst;
1180 req->nbytes = nbytes;
1181 req->info = iv;
1182 }
1183
1184 /**
1185 * DOC: Authenticated Encryption With Associated Data (AEAD) Cipher API
1186 *
1187 * The AEAD cipher API is used with the ciphers of type CRYPTO_ALG_TYPE_AEAD
1188 * (listed as type "aead" in /proc/crypto)
1189 *
1190 * The most prominent examples for this type of encryption is GCM and CCM.
1191 * However, the kernel supports other types of AEAD ciphers which are defined
1192 * with the following cipher string:
1193 *
1194 * authenc(keyed message digest, block cipher)
1195 *
1196 * For example: authenc(hmac(sha256), cbc(aes))
1197 *
1198 * The example code provided for the asynchronous block cipher operation
1199 * applies here as well. Naturally all *ablkcipher* symbols must be exchanged
1200 * the *aead* pendants discussed in the following. In addtion, for the AEAD
1201 * operation, the aead_request_set_assoc function must be used to set the
1202 * pointer to the associated data memory location before performing the
1203 * encryption or decryption operation. In case of an encryption, the associated
1204 * data memory is filled during the encryption operation. For decryption, the
1205 * associated data memory must contain data that is used to verify the integrity
1206 * of the decrypted data. Another deviation from the asynchronous block cipher
1207 * operation is that the caller should explicitly check for -EBADMSG of the
1208 * crypto_aead_decrypt. That error indicates an authentication error, i.e.
1209 * a breach in the integrity of the message. In essence, that -EBADMSG error
1210 * code is the key bonus an AEAD cipher has over "standard" block chaining
1211 * modes.
1212 */
1213
1214 static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
1215 {
1216 return (struct crypto_aead *)tfm;
1217 }
1218
1219 /**
1220 * crypto_alloc_aead() - allocate AEAD cipher handle
1221 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1222 * AEAD cipher
1223 * @type: specifies the type of the cipher
1224 * @mask: specifies the mask for the cipher
1225 *
1226 * Allocate a cipher handle for an AEAD. The returned struct
1227 * crypto_aead is the cipher handle that is required for any subsequent
1228 * API invocation for that AEAD.
1229 *
1230 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1231 * of an error, PTR_ERR() returns the error code.
1232 */
1233 struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
1234
1235 static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
1236 {
1237 return &tfm->base;
1238 }
1239
1240 /**
1241 * crypto_free_aead() - zeroize and free aead handle
1242 * @tfm: cipher handle to be freed
1243 */
1244 static inline void crypto_free_aead(struct crypto_aead *tfm)
1245 {
1246 crypto_free_tfm(crypto_aead_tfm(tfm));
1247 }
1248
1249 static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
1250 {
1251 return &crypto_aead_tfm(tfm)->crt_aead;
1252 }
1253
1254 /**
1255 * crypto_aead_ivsize() - obtain IV size
1256 * @tfm: cipher handle
1257 *
1258 * The size of the IV for the aead referenced by the cipher handle is
1259 * returned. This IV size may be zero if the cipher does not need an IV.
1260 *
1261 * Return: IV size in bytes
1262 */
1263 static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
1264 {
1265 return crypto_aead_crt(tfm)->ivsize;
1266 }
1267
1268 /**
1269 * crypto_aead_authsize() - obtain maximum authentication data size
1270 * @tfm: cipher handle
1271 *
1272 * The maximum size of the authentication data for the AEAD cipher referenced
1273 * by the AEAD cipher handle is returned. The authentication data size may be
1274 * zero if the cipher implements a hard-coded maximum.
1275 *
1276 * The authentication data may also be known as "tag value".
1277 *
1278 * Return: authentication data size / tag size in bytes
1279 */
1280 static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
1281 {
1282 return crypto_aead_crt(tfm)->authsize;
1283 }
1284
1285 /**
1286 * crypto_aead_blocksize() - obtain block size of cipher
1287 * @tfm: cipher handle
1288 *
1289 * The block size for the AEAD referenced with the cipher handle is returned.
1290 * The caller may use that information to allocate appropriate memory for the
1291 * data returned by the encryption or decryption operation
1292 *
1293 * Return: block size of cipher
1294 */
1295 static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
1296 {
1297 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
1298 }
1299
1300 static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
1301 {
1302 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
1303 }
1304
1305 static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
1306 {
1307 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
1308 }
1309
1310 static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
1311 {
1312 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
1313 }
1314
1315 static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
1316 {
1317 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
1318 }
1319
1320 /**
1321 * crypto_aead_setkey() - set key for cipher
1322 * @tfm: cipher handle
1323 * @key: buffer holding the key
1324 * @keylen: length of the key in bytes
1325 *
1326 * The caller provided key is set for the AEAD referenced by the cipher
1327 * handle.
1328 *
1329 * Note, the key length determines the cipher type. Many block ciphers implement
1330 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1331 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1332 * is performed.
1333 *
1334 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1335 */
1336 static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
1337 unsigned int keylen)
1338 {
1339 struct aead_tfm *crt = crypto_aead_crt(tfm);
1340
1341 return crt->setkey(crt->base, key, keylen);
1342 }
1343
1344 /**
1345 * crypto_aead_setauthsize() - set authentication data size
1346 * @tfm: cipher handle
1347 * @authsize: size of the authentication data / tag in bytes
1348 *
1349 * Set the authentication data size / tag size. AEAD requires an authentication
1350 * tag (or MAC) in addition to the associated data.
1351 *
1352 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1353 */
1354 int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
1355
1356 static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
1357 {
1358 return __crypto_aead_cast(req->base.tfm);
1359 }
1360
1361 /**
1362 * crypto_aead_encrypt() - encrypt plaintext
1363 * @req: reference to the aead_request handle that holds all information
1364 * needed to perform the cipher operation
1365 *
1366 * Encrypt plaintext data using the aead_request handle. That data structure
1367 * and how it is filled with data is discussed with the aead_request_*
1368 * functions.
1369 *
1370 * IMPORTANT NOTE The encryption operation creates the authentication data /
1371 * tag. That data is concatenated with the created ciphertext.
1372 * The ciphertext memory size is therefore the given number of
1373 * block cipher blocks + the size defined by the
1374 * crypto_aead_setauthsize invocation. The caller must ensure
1375 * that sufficient memory is available for the ciphertext and
1376 * the authentication tag.
1377 *
1378 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1379 */
1380 static inline int crypto_aead_encrypt(struct aead_request *req)
1381 {
1382 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
1383 }
1384
1385 /**
1386 * crypto_aead_decrypt() - decrypt ciphertext
1387 * @req: reference to the ablkcipher_request handle that holds all information
1388 * needed to perform the cipher operation
1389 *
1390 * Decrypt ciphertext data using the aead_request handle. That data structure
1391 * and how it is filled with data is discussed with the aead_request_*
1392 * functions.
1393 *
1394 * IMPORTANT NOTE The caller must concatenate the ciphertext followed by the
1395 * authentication data / tag. That authentication data / tag
1396 * must have the size defined by the crypto_aead_setauthsize
1397 * invocation.
1398 *
1399 *
1400 * Return: 0 if the cipher operation was successful; -EBADMSG: The AEAD
1401 * cipher operation performs the authentication of the data during the
1402 * decryption operation. Therefore, the function returns this error if
1403 * the authentication of the ciphertext was unsuccessful (i.e. the
1404 * integrity of the ciphertext or the associated data was violated);
1405 * < 0 if an error occurred.
1406 */
1407 static inline int crypto_aead_decrypt(struct aead_request *req)
1408 {
1409 if (req->cryptlen < crypto_aead_authsize(crypto_aead_reqtfm(req)))
1410 return -EINVAL;
1411
1412 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
1413 }
1414
1415 /**
1416 * DOC: Asynchronous AEAD Request Handle
1417 *
1418 * The aead_request data structure contains all pointers to data required for
1419 * the AEAD cipher operation. This includes the cipher handle (which can be
1420 * used by multiple aead_request instances), pointer to plaintext and
1421 * ciphertext, asynchronous callback function, etc. It acts as a handle to the
1422 * aead_request_* API calls in a similar way as AEAD handle to the
1423 * crypto_aead_* API calls.
1424 */
1425
1426 /**
1427 * crypto_aead_reqsize() - obtain size of the request data structure
1428 * @tfm: cipher handle
1429 *
1430 * Return: number of bytes
1431 */
1432 static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
1433 {
1434 return crypto_aead_crt(tfm)->reqsize;
1435 }
1436
1437 /**
1438 * aead_request_set_tfm() - update cipher handle reference in request
1439 * @req: request handle to be modified
1440 * @tfm: cipher handle that shall be added to the request handle
1441 *
1442 * Allow the caller to replace the existing aead handle in the request
1443 * data structure with a different one.
1444 */
1445 static inline void aead_request_set_tfm(struct aead_request *req,
1446 struct crypto_aead *tfm)
1447 {
1448 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
1449 }
1450
1451 /**
1452 * aead_request_alloc() - allocate request data structure
1453 * @tfm: cipher handle to be registered with the request
1454 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
1455 *
1456 * Allocate the request data structure that must be used with the AEAD
1457 * encrypt and decrypt API calls. During the allocation, the provided aead
1458 * handle is registered in the request data structure.
1459 *
1460 * Return: allocated request handle in case of success; IS_ERR() is true in case
1461 * of an error, PTR_ERR() returns the error code.
1462 */
1463 static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
1464 gfp_t gfp)
1465 {
1466 struct aead_request *req;
1467
1468 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
1469
1470 if (likely(req))
1471 aead_request_set_tfm(req, tfm);
1472
1473 return req;
1474 }
1475
1476 /**
1477 * aead_request_free() - zeroize and free request data structure
1478 * @req: request data structure cipher handle to be freed
1479 */
1480 static inline void aead_request_free(struct aead_request *req)
1481 {
1482 kzfree(req);
1483 }
1484
1485 /**
1486 * aead_request_set_callback() - set asynchronous callback function
1487 * @req: request handle
1488 * @flags: specify zero or an ORing of the flags
1489 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
1490 * increase the wait queue beyond the initial maximum size;
1491 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
1492 * @compl: callback function pointer to be registered with the request handle
1493 * @data: The data pointer refers to memory that is not used by the kernel
1494 * crypto API, but provided to the callback function for it to use. Here,
1495 * the caller can provide a reference to memory the callback function can
1496 * operate on. As the callback function is invoked asynchronously to the
1497 * related functionality, it may need to access data structures of the
1498 * related functionality which can be referenced using this pointer. The
1499 * callback function can access the memory via the "data" field in the
1500 * crypto_async_request data structure provided to the callback function.
1501 *
1502 * Setting the callback function that is triggered once the cipher operation
1503 * completes
1504 *
1505 * The callback function is registered with the aead_request handle and
1506 * must comply with the following template
1507 *
1508 * void callback_function(struct crypto_async_request *req, int error)
1509 */
1510 static inline void aead_request_set_callback(struct aead_request *req,
1511 u32 flags,
1512 crypto_completion_t compl,
1513 void *data)
1514 {
1515 req->base.complete = compl;
1516 req->base.data = data;
1517 req->base.flags = flags;
1518 }
1519
1520 /**
1521 * aead_request_set_crypt - set data buffers
1522 * @req: request handle
1523 * @src: source scatter / gather list
1524 * @dst: destination scatter / gather list
1525 * @cryptlen: number of bytes to process from @src
1526 * @iv: IV for the cipher operation which must comply with the IV size defined
1527 * by crypto_aead_ivsize()
1528 *
1529 * Setting the source data and destination data scatter / gather lists.
1530 *
1531 * For encryption, the source is treated as the plaintext and the
1532 * destination is the ciphertext. For a decryption operation, the use is
1533 * reversed - the source is the ciphertext and the destination is the plaintext.
1534 *
1535 * IMPORTANT NOTE AEAD requires an authentication tag (MAC). For decryption,
1536 * the caller must concatenate the ciphertext followed by the
1537 * authentication tag and provide the entire data stream to the
1538 * decryption operation (i.e. the data length used for the
1539 * initialization of the scatterlist and the data length for the
1540 * decryption operation is identical). For encryption, however,
1541 * the authentication tag is created while encrypting the data.
1542 * The destination buffer must hold sufficient space for the
1543 * ciphertext and the authentication tag while the encryption
1544 * invocation must only point to the plaintext data size. The
1545 * following code snippet illustrates the memory usage
1546 * buffer = kmalloc(ptbuflen + (enc ? authsize : 0));
1547 * sg_init_one(&sg, buffer, ptbuflen + (enc ? authsize : 0));
1548 * aead_request_set_crypt(req, &sg, &sg, ptbuflen, iv);
1549 */
1550 static inline void aead_request_set_crypt(struct aead_request *req,
1551 struct scatterlist *src,
1552 struct scatterlist *dst,
1553 unsigned int cryptlen, u8 *iv)
1554 {
1555 req->src = src;
1556 req->dst = dst;
1557 req->cryptlen = cryptlen;
1558 req->iv = iv;
1559 }
1560
1561 /**
1562 * aead_request_set_assoc() - set the associated data scatter / gather list
1563 * @req: request handle
1564 * @assoc: associated data scatter / gather list
1565 * @assoclen: number of bytes to process from @assoc
1566 *
1567 * For encryption, the memory is filled with the associated data. For
1568 * decryption, the memory must point to the associated data.
1569 */
1570 static inline void aead_request_set_assoc(struct aead_request *req,
1571 struct scatterlist *assoc,
1572 unsigned int assoclen)
1573 {
1574 req->assoc = assoc;
1575 req->assoclen = assoclen;
1576 }
1577
1578 /**
1579 * DOC: Synchronous Block Cipher API
1580 *
1581 * The synchronous block cipher API is used with the ciphers of type
1582 * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto)
1583 *
1584 * Synchronous calls, have a context in the tfm. But since a single tfm can be
1585 * used in multiple calls and in parallel, this info should not be changeable
1586 * (unless a lock is used). This applies, for example, to the symmetric key.
1587 * However, the IV is changeable, so there is an iv field in blkcipher_tfm
1588 * structure for synchronous blkcipher api. So, its the only state info that can
1589 * be kept for synchronous calls without using a big lock across a tfm.
1590 *
1591 * The block cipher API allows the use of a complete cipher, i.e. a cipher
1592 * consisting of a template (a block chaining mode) and a single block cipher
1593 * primitive (e.g. AES).
1594 *
1595 * The plaintext data buffer and the ciphertext data buffer are pointed to
1596 * by using scatter/gather lists. The cipher operation is performed
1597 * on all segments of the provided scatter/gather lists.
1598 *
1599 * The kernel crypto API supports a cipher operation "in-place" which means that
1600 * the caller may provide the same scatter/gather list for the plaintext and
1601 * cipher text. After the completion of the cipher operation, the plaintext
1602 * data is replaced with the ciphertext data in case of an encryption and vice
1603 * versa for a decryption. The caller must ensure that the scatter/gather lists
1604 * for the output data point to sufficiently large buffers, i.e. multiples of
1605 * the block size of the cipher.
1606 */
1607
1608 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
1609 struct crypto_tfm *tfm)
1610 {
1611 return (struct crypto_blkcipher *)tfm;
1612 }
1613
1614 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
1615 struct crypto_tfm *tfm)
1616 {
1617 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
1618 return __crypto_blkcipher_cast(tfm);
1619 }
1620
1621 /**
1622 * crypto_alloc_blkcipher() - allocate synchronous block cipher handle
1623 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1624 * blkcipher cipher
1625 * @type: specifies the type of the cipher
1626 * @mask: specifies the mask for the cipher
1627 *
1628 * Allocate a cipher handle for a block cipher. The returned struct
1629 * crypto_blkcipher is the cipher handle that is required for any subsequent
1630 * API invocation for that block cipher.
1631 *
1632 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1633 * of an error, PTR_ERR() returns the error code.
1634 */
1635 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
1636 const char *alg_name, u32 type, u32 mask)
1637 {
1638 type &= ~CRYPTO_ALG_TYPE_MASK;
1639 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
1640 mask |= CRYPTO_ALG_TYPE_MASK;
1641
1642 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
1643 }
1644
1645 static inline struct crypto_tfm *crypto_blkcipher_tfm(
1646 struct crypto_blkcipher *tfm)
1647 {
1648 return &tfm->base;
1649 }
1650
1651 /**
1652 * crypto_free_blkcipher() - zeroize and free the block cipher handle
1653 * @tfm: cipher handle to be freed
1654 */
1655 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
1656 {
1657 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
1658 }
1659
1660 /**
1661 * crypto_has_blkcipher() - Search for the availability of a block cipher
1662 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1663 * block cipher
1664 * @type: specifies the type of the cipher
1665 * @mask: specifies the mask for the cipher
1666 *
1667 * Return: true when the block cipher is known to the kernel crypto API; false
1668 * otherwise
1669 */
1670 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
1671 {
1672 type &= ~CRYPTO_ALG_TYPE_MASK;
1673 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
1674 mask |= CRYPTO_ALG_TYPE_MASK;
1675
1676 return crypto_has_alg(alg_name, type, mask);
1677 }
1678
1679 /**
1680 * crypto_blkcipher_name() - return the name / cra_name from the cipher handle
1681 * @tfm: cipher handle
1682 *
1683 * Return: The character string holding the name of the cipher
1684 */
1685 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
1686 {
1687 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
1688 }
1689
1690 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
1691 struct crypto_blkcipher *tfm)
1692 {
1693 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
1694 }
1695
1696 static inline struct blkcipher_alg *crypto_blkcipher_alg(
1697 struct crypto_blkcipher *tfm)
1698 {
1699 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
1700 }
1701
1702 /**
1703 * crypto_blkcipher_ivsize() - obtain IV size
1704 * @tfm: cipher handle
1705 *
1706 * The size of the IV for the block cipher referenced by the cipher handle is
1707 * returned. This IV size may be zero if the cipher does not need an IV.
1708 *
1709 * Return: IV size in bytes
1710 */
1711 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
1712 {
1713 return crypto_blkcipher_alg(tfm)->ivsize;
1714 }
1715
1716 /**
1717 * crypto_blkcipher_blocksize() - obtain block size of cipher
1718 * @tfm: cipher handle
1719 *
1720 * The block size for the block cipher referenced with the cipher handle is
1721 * returned. The caller may use that information to allocate appropriate
1722 * memory for the data returned by the encryption or decryption operation.
1723 *
1724 * Return: block size of cipher
1725 */
1726 static inline unsigned int crypto_blkcipher_blocksize(
1727 struct crypto_blkcipher *tfm)
1728 {
1729 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1730 }
1731
1732 static inline unsigned int crypto_blkcipher_alignmask(
1733 struct crypto_blkcipher *tfm)
1734 {
1735 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1736 }
1737
1738 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1739 {
1740 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1741 }
1742
1743 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1744 u32 flags)
1745 {
1746 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1747 }
1748
1749 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1750 u32 flags)
1751 {
1752 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1753 }
1754
1755 /**
1756 * crypto_blkcipher_setkey() - set key for cipher
1757 * @tfm: cipher handle
1758 * @key: buffer holding the key
1759 * @keylen: length of the key in bytes
1760 *
1761 * The caller provided key is set for the block cipher referenced by the cipher
1762 * handle.
1763 *
1764 * Note, the key length determines the cipher type. Many block ciphers implement
1765 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1766 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1767 * is performed.
1768 *
1769 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1770 */
1771 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1772 const u8 *key, unsigned int keylen)
1773 {
1774 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1775 key, keylen);
1776 }
1777
1778 /**
1779 * crypto_blkcipher_encrypt() - encrypt plaintext
1780 * @desc: reference to the block cipher handle with meta data
1781 * @dst: scatter/gather list that is filled by the cipher operation with the
1782 * ciphertext
1783 * @src: scatter/gather list that holds the plaintext
1784 * @nbytes: number of bytes of the plaintext to encrypt.
1785 *
1786 * Encrypt plaintext data using the IV set by the caller with a preceding
1787 * call of crypto_blkcipher_set_iv.
1788 *
1789 * The blkcipher_desc data structure must be filled by the caller and can
1790 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1791 * with the block cipher handle; desc.flags is filled with either
1792 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1793 *
1794 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1795 */
1796 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1797 struct scatterlist *dst,
1798 struct scatterlist *src,
1799 unsigned int nbytes)
1800 {
1801 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1802 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1803 }
1804
1805 /**
1806 * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV
1807 * @desc: reference to the block cipher handle with meta data
1808 * @dst: scatter/gather list that is filled by the cipher operation with the
1809 * ciphertext
1810 * @src: scatter/gather list that holds the plaintext
1811 * @nbytes: number of bytes of the plaintext to encrypt.
1812 *
1813 * Encrypt plaintext data with the use of an IV that is solely used for this
1814 * cipher operation. Any previously set IV is not used.
1815 *
1816 * The blkcipher_desc data structure must be filled by the caller and can
1817 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1818 * with the block cipher handle; desc.info is filled with the IV to be used for
1819 * the current operation; desc.flags is filled with either
1820 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1821 *
1822 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1823 */
1824 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1825 struct scatterlist *dst,
1826 struct scatterlist *src,
1827 unsigned int nbytes)
1828 {
1829 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1830 }
1831
1832 /**
1833 * crypto_blkcipher_decrypt() - decrypt ciphertext
1834 * @desc: reference to the block cipher handle with meta data
1835 * @dst: scatter/gather list that is filled by the cipher operation with the
1836 * plaintext
1837 * @src: scatter/gather list that holds the ciphertext
1838 * @nbytes: number of bytes of the ciphertext to decrypt.
1839 *
1840 * Decrypt ciphertext data using the IV set by the caller with a preceding
1841 * call of crypto_blkcipher_set_iv.
1842 *
1843 * The blkcipher_desc data structure must be filled by the caller as documented
1844 * for the crypto_blkcipher_encrypt call above.
1845 *
1846 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1847 *
1848 */
1849 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1850 struct scatterlist *dst,
1851 struct scatterlist *src,
1852 unsigned int nbytes)
1853 {
1854 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1855 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1856 }
1857
1858 /**
1859 * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV
1860 * @desc: reference to the block cipher handle with meta data
1861 * @dst: scatter/gather list that is filled by the cipher operation with the
1862 * plaintext
1863 * @src: scatter/gather list that holds the ciphertext
1864 * @nbytes: number of bytes of the ciphertext to decrypt.
1865 *
1866 * Decrypt ciphertext data with the use of an IV that is solely used for this
1867 * cipher operation. Any previously set IV is not used.
1868 *
1869 * The blkcipher_desc data structure must be filled by the caller as documented
1870 * for the crypto_blkcipher_encrypt_iv call above.
1871 *
1872 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1873 */
1874 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1875 struct scatterlist *dst,
1876 struct scatterlist *src,
1877 unsigned int nbytes)
1878 {
1879 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1880 }
1881
1882 /**
1883 * crypto_blkcipher_set_iv() - set IV for cipher
1884 * @tfm: cipher handle
1885 * @src: buffer holding the IV
1886 * @len: length of the IV in bytes
1887 *
1888 * The caller provided IV is set for the block cipher referenced by the cipher
1889 * handle.
1890 */
1891 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1892 const u8 *src, unsigned int len)
1893 {
1894 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1895 }
1896
1897 /**
1898 * crypto_blkcipher_get_iv() - obtain IV from cipher
1899 * @tfm: cipher handle
1900 * @dst: buffer filled with the IV
1901 * @len: length of the buffer dst
1902 *
1903 * The caller can obtain the IV set for the block cipher referenced by the
1904 * cipher handle and store it into the user-provided buffer. If the buffer
1905 * has an insufficient space, the IV is truncated to fit the buffer.
1906 */
1907 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1908 u8 *dst, unsigned int len)
1909 {
1910 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1911 }
1912
1913 /**
1914 * DOC: Single Block Cipher API
1915 *
1916 * The single block cipher API is used with the ciphers of type
1917 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
1918 *
1919 * Using the single block cipher API calls, operations with the basic cipher
1920 * primitive can be implemented. These cipher primitives exclude any block
1921 * chaining operations including IV handling.
1922 *
1923 * The purpose of this single block cipher API is to support the implementation
1924 * of templates or other concepts that only need to perform the cipher operation
1925 * on one block at a time. Templates invoke the underlying cipher primitive
1926 * block-wise and process either the input or the output data of these cipher
1927 * operations.
1928 */
1929
1930 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1931 {
1932 return (struct crypto_cipher *)tfm;
1933 }
1934
1935 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1936 {
1937 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1938 return __crypto_cipher_cast(tfm);
1939 }
1940
1941 /**
1942 * crypto_alloc_cipher() - allocate single block cipher handle
1943 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1944 * single block cipher
1945 * @type: specifies the type of the cipher
1946 * @mask: specifies the mask for the cipher
1947 *
1948 * Allocate a cipher handle for a single block cipher. The returned struct
1949 * crypto_cipher is the cipher handle that is required for any subsequent API
1950 * invocation for that single block cipher.
1951 *
1952 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1953 * of an error, PTR_ERR() returns the error code.
1954 */
1955 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1956 u32 type, u32 mask)
1957 {
1958 type &= ~CRYPTO_ALG_TYPE_MASK;
1959 type |= CRYPTO_ALG_TYPE_CIPHER;
1960 mask |= CRYPTO_ALG_TYPE_MASK;
1961
1962 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1963 }
1964
1965 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1966 {
1967 return &tfm->base;
1968 }
1969
1970 /**
1971 * crypto_free_cipher() - zeroize and free the single block cipher handle
1972 * @tfm: cipher handle to be freed
1973 */
1974 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1975 {
1976 crypto_free_tfm(crypto_cipher_tfm(tfm));
1977 }
1978
1979 /**
1980 * crypto_has_cipher() - Search for the availability of a single block cipher
1981 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1982 * single block cipher
1983 * @type: specifies the type of the cipher
1984 * @mask: specifies the mask for the cipher
1985 *
1986 * Return: true when the single block cipher is known to the kernel crypto API;
1987 * false otherwise
1988 */
1989 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1990 {
1991 type &= ~CRYPTO_ALG_TYPE_MASK;
1992 type |= CRYPTO_ALG_TYPE_CIPHER;
1993 mask |= CRYPTO_ALG_TYPE_MASK;
1994
1995 return crypto_has_alg(alg_name, type, mask);
1996 }
1997
1998 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1999 {
2000 return &crypto_cipher_tfm(tfm)->crt_cipher;
2001 }
2002
2003 /**
2004 * crypto_cipher_blocksize() - obtain block size for cipher
2005 * @tfm: cipher handle
2006 *
2007 * The block size for the single block cipher referenced with the cipher handle
2008 * tfm is returned. The caller may use that information to allocate appropriate
2009 * memory for the data returned by the encryption or decryption operation
2010 *
2011 * Return: block size of cipher
2012 */
2013 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
2014 {
2015 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
2016 }
2017
2018 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
2019 {
2020 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
2021 }
2022
2023 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
2024 {
2025 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
2026 }
2027
2028 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
2029 u32 flags)
2030 {
2031 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
2032 }
2033
2034 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
2035 u32 flags)
2036 {
2037 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
2038 }
2039
2040 /**
2041 * crypto_cipher_setkey() - set key for cipher
2042 * @tfm: cipher handle
2043 * @key: buffer holding the key
2044 * @keylen: length of the key in bytes
2045 *
2046 * The caller provided key is set for the single block cipher referenced by the
2047 * cipher handle.
2048 *
2049 * Note, the key length determines the cipher type. Many block ciphers implement
2050 * different cipher modes depending on the key size, such as AES-128 vs AES-192
2051 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
2052 * is performed.
2053 *
2054 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
2055 */
2056 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
2057 const u8 *key, unsigned int keylen)
2058 {
2059 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
2060 key, keylen);
2061 }
2062
2063 /**
2064 * crypto_cipher_encrypt_one() - encrypt one block of plaintext
2065 * @tfm: cipher handle
2066 * @dst: points to the buffer that will be filled with the ciphertext
2067 * @src: buffer holding the plaintext to be encrypted
2068 *
2069 * Invoke the encryption operation of one block. The caller must ensure that
2070 * the plaintext and ciphertext buffers are at least one block in size.
2071 */
2072 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
2073 u8 *dst, const u8 *src)
2074 {
2075 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
2076 dst, src);
2077 }
2078
2079 /**
2080 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
2081 * @tfm: cipher handle
2082 * @dst: points to the buffer that will be filled with the plaintext
2083 * @src: buffer holding the ciphertext to be decrypted
2084 *
2085 * Invoke the decryption operation of one block. The caller must ensure that
2086 * the plaintext and ciphertext buffers are at least one block in size.
2087 */
2088 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
2089 u8 *dst, const u8 *src)
2090 {
2091 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
2092 dst, src);
2093 }
2094
2095 /**
2096 * DOC: Synchronous Message Digest API
2097 *
2098 * The synchronous message digest API is used with the ciphers of type
2099 * CRYPTO_ALG_TYPE_HASH (listed as type "hash" in /proc/crypto)
2100 */
2101
2102 static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
2103 {
2104 return (struct crypto_hash *)tfm;
2105 }
2106
2107 static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
2108 {
2109 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
2110 CRYPTO_ALG_TYPE_HASH_MASK);
2111 return __crypto_hash_cast(tfm);
2112 }
2113
2114 /**
2115 * crypto_alloc_hash() - allocate synchronous message digest handle
2116 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
2117 * message digest cipher
2118 * @type: specifies the type of the cipher
2119 * @mask: specifies the mask for the cipher
2120 *
2121 * Allocate a cipher handle for a message digest. The returned struct
2122 * crypto_hash is the cipher handle that is required for any subsequent
2123 * API invocation for that message digest.
2124 *
2125 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
2126 * of an error, PTR_ERR() returns the error code.
2127 */
2128 static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
2129 u32 type, u32 mask)
2130 {
2131 type &= ~CRYPTO_ALG_TYPE_MASK;
2132 mask &= ~CRYPTO_ALG_TYPE_MASK;
2133 type |= CRYPTO_ALG_TYPE_HASH;
2134 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
2135
2136 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
2137 }
2138
2139 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
2140 {
2141 return &tfm->base;
2142 }
2143
2144 /**
2145 * crypto_free_hash() - zeroize and free message digest handle
2146 * @tfm: cipher handle to be freed
2147 */
2148 static inline void crypto_free_hash(struct crypto_hash *tfm)
2149 {
2150 crypto_free_tfm(crypto_hash_tfm(tfm));
2151 }
2152
2153 /**
2154 * crypto_has_hash() - Search for the availability of a message digest
2155 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
2156 * message digest cipher
2157 * @type: specifies the type of the cipher
2158 * @mask: specifies the mask for the cipher
2159 *
2160 * Return: true when the message digest cipher is known to the kernel crypto
2161 * API; false otherwise
2162 */
2163 static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
2164 {
2165 type &= ~CRYPTO_ALG_TYPE_MASK;
2166 mask &= ~CRYPTO_ALG_TYPE_MASK;
2167 type |= CRYPTO_ALG_TYPE_HASH;
2168 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
2169
2170 return crypto_has_alg(alg_name, type, mask);
2171 }
2172
2173 static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
2174 {
2175 return &crypto_hash_tfm(tfm)->crt_hash;
2176 }
2177
2178 /**
2179 * crypto_hash_blocksize() - obtain block size for message digest
2180 * @tfm: cipher handle
2181 *
2182 * The block size for the message digest cipher referenced with the cipher
2183 * handle is returned.
2184 *
2185 * Return: block size of cipher
2186 */
2187 static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
2188 {
2189 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
2190 }
2191
2192 static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
2193 {
2194 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
2195 }
2196
2197 /**
2198 * crypto_hash_digestsize() - obtain message digest size
2199 * @tfm: cipher handle
2200 *
2201 * The size for the message digest created by the message digest cipher
2202 * referenced with the cipher handle is returned.
2203 *
2204 * Return: message digest size
2205 */
2206 static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
2207 {
2208 return crypto_hash_crt(tfm)->digestsize;
2209 }
2210
2211 static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
2212 {
2213 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
2214 }
2215
2216 static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
2217 {
2218 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
2219 }
2220
2221 static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
2222 {
2223 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
2224 }
2225
2226 /**
2227 * crypto_hash_init() - (re)initialize message digest handle
2228 * @desc: cipher request handle that to be filled by caller --
2229 * desc.tfm is filled with the hash cipher handle;
2230 * desc.flags is filled with either CRYPTO_TFM_REQ_MAY_SLEEP or 0.
2231 *
2232 * The call (re-)initializes the message digest referenced by the hash cipher
2233 * request handle. Any potentially existing state created by previous
2234 * operations is discarded.
2235 *
2236 * Return: 0 if the message digest initialization was successful; < 0 if an
2237 * error occurred
2238 */
2239 static inline int crypto_hash_init(struct hash_desc *desc)
2240 {
2241 return crypto_hash_crt(desc->tfm)->init(desc);
2242 }
2243
2244 /**
2245 * crypto_hash_update() - add data to message digest for processing
2246 * @desc: cipher request handle
2247 * @sg: scatter / gather list pointing to the data to be added to the message
2248 * digest
2249 * @nbytes: number of bytes to be processed from @sg
2250 *
2251 * Updates the message digest state of the cipher handle pointed to by the
2252 * hash cipher request handle with the input data pointed to by the
2253 * scatter/gather list.
2254 *
2255 * Return: 0 if the message digest update was successful; < 0 if an error
2256 * occurred
2257 */
2258 static inline int crypto_hash_update(struct hash_desc *desc,
2259 struct scatterlist *sg,
2260 unsigned int nbytes)
2261 {
2262 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
2263 }
2264
2265 /**
2266 * crypto_hash_final() - calculate message digest
2267 * @desc: cipher request handle
2268 * @out: message digest output buffer -- The caller must ensure that the out
2269 * buffer has a sufficient size (e.g. by using the crypto_hash_digestsize
2270 * function).
2271 *
2272 * Finalize the message digest operation and create the message digest
2273 * based on all data added to the cipher handle. The message digest is placed
2274 * into the output buffer.
2275 *
2276 * Return: 0 if the message digest creation was successful; < 0 if an error
2277 * occurred
2278 */
2279 static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
2280 {
2281 return crypto_hash_crt(desc->tfm)->final(desc, out);
2282 }
2283
2284 /**
2285 * crypto_hash_digest() - calculate message digest for a buffer
2286 * @desc: see crypto_hash_final()
2287 * @sg: see crypto_hash_update()
2288 * @nbytes: see crypto_hash_update()
2289 * @out: see crypto_hash_final()
2290 *
2291 * This function is a "short-hand" for the function calls of crypto_hash_init,
2292 * crypto_hash_update and crypto_hash_final. The parameters have the same
2293 * meaning as discussed for those separate three functions.
2294 *
2295 * Return: 0 if the message digest creation was successful; < 0 if an error
2296 * occurred
2297 */
2298 static inline int crypto_hash_digest(struct hash_desc *desc,
2299 struct scatterlist *sg,
2300 unsigned int nbytes, u8 *out)
2301 {
2302 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
2303 }
2304
2305 /**
2306 * crypto_hash_setkey() - set key for message digest
2307 * @hash: cipher handle
2308 * @key: buffer holding the key
2309 * @keylen: length of the key in bytes
2310 *
2311 * The caller provided key is set for the message digest cipher. The cipher
2312 * handle must point to a keyed hash in order for this function to succeed.
2313 *
2314 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
2315 */
2316 static inline int crypto_hash_setkey(struct crypto_hash *hash,
2317 const u8 *key, unsigned int keylen)
2318 {
2319 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
2320 }
2321
2322 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
2323 {
2324 return (struct crypto_comp *)tfm;
2325 }
2326
2327 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
2328 {
2329 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
2330 CRYPTO_ALG_TYPE_MASK);
2331 return __crypto_comp_cast(tfm);
2332 }
2333
2334 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
2335 u32 type, u32 mask)
2336 {
2337 type &= ~CRYPTO_ALG_TYPE_MASK;
2338 type |= CRYPTO_ALG_TYPE_COMPRESS;
2339 mask |= CRYPTO_ALG_TYPE_MASK;
2340
2341 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
2342 }
2343
2344 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
2345 {
2346 return &tfm->base;
2347 }
2348
2349 static inline void crypto_free_comp(struct crypto_comp *tfm)
2350 {
2351 crypto_free_tfm(crypto_comp_tfm(tfm));
2352 }
2353
2354 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
2355 {
2356 type &= ~CRYPTO_ALG_TYPE_MASK;
2357 type |= CRYPTO_ALG_TYPE_COMPRESS;
2358 mask |= CRYPTO_ALG_TYPE_MASK;
2359
2360 return crypto_has_alg(alg_name, type, mask);
2361 }
2362
2363 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
2364 {
2365 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
2366 }
2367
2368 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
2369 {
2370 return &crypto_comp_tfm(tfm)->crt_compress;
2371 }
2372
2373 static inline int crypto_comp_compress(struct crypto_comp *tfm,
2374 const u8 *src, unsigned int slen,
2375 u8 *dst, unsigned int *dlen)
2376 {
2377 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
2378 src, slen, dst, dlen);
2379 }
2380
2381 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
2382 const u8 *src, unsigned int slen,
2383 u8 *dst, unsigned int *dlen)
2384 {
2385 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
2386 src, slen, dst, dlen);
2387 }
2388
2389 #endif /* _LINUX_CRYPTO_H */
2390