2 * Scatterlist Cryptographic API.
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>
8 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
9 * and Nettle, by Niels Möller.
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)
17 #ifndef _LINUX_CRYPTO_H
18 #define _LINUX_CRYPTO_H
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>
29 * Algorithm masks and types.
31 #define CRYPTO_ALG_TYPE_MASK 0x0000000f
32 #define CRYPTO_ALG_TYPE_CIPHER 0x00000001
33 #define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
34 #define CRYPTO_ALG_TYPE_AEAD 0x00000003
35 #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
36 #define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
37 #define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
38 #define CRYPTO_ALG_TYPE_DIGEST 0x00000008
39 #define CRYPTO_ALG_TYPE_HASH 0x00000008
40 #define CRYPTO_ALG_TYPE_SHASH 0x00000009
41 #define CRYPTO_ALG_TYPE_AHASH 0x0000000a
42 #define CRYPTO_ALG_TYPE_RNG 0x0000000c
43 #define CRYPTO_ALG_TYPE_PCOMPRESS 0x0000000f
45 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
46 #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
47 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
49 #define CRYPTO_ALG_LARVAL 0x00000010
50 #define CRYPTO_ALG_DEAD 0x00000020
51 #define CRYPTO_ALG_DYING 0x00000040
52 #define CRYPTO_ALG_ASYNC 0x00000080
55 * Set this bit if and only if the algorithm requires another algorithm of
56 * the same type to handle corner cases.
58 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
61 * This bit is set for symmetric key ciphers that have already been wrapped
62 * with a generic IV generator to prevent them from being wrapped again.
64 #define CRYPTO_ALG_GENIV 0x00000200
67 * Set if the algorithm has passed automated run-time testing. Note that
68 * if there is no run-time testing for a given algorithm it is considered
72 #define CRYPTO_ALG_TESTED 0x00000400
75 * Set if the algorithm is an instance that is build from templates.
77 #define CRYPTO_ALG_INSTANCE 0x00000800
79 /* Set this bit if the algorithm provided is hardware accelerated but
80 * not available to userspace via instruction set or so.
82 #define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
85 * Transform masks and values (for crt_flags).
87 #define CRYPTO_TFM_REQ_MASK 0x000fff00
88 #define CRYPTO_TFM_RES_MASK 0xfff00000
90 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
91 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
92 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
93 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
94 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
95 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
96 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
97 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
100 * Miscellaneous stuff.
102 #define CRYPTO_MAX_ALG_NAME 64
105 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
106 * declaration) is used to ensure that the crypto_tfm context structure is
107 * aligned correctly for the given architecture so that there are no alignment
108 * faults for C data types. In particular, this is required on platforms such
109 * as arm where pointers are 32-bit aligned but there are data types such as
110 * u64 which require 64-bit alignment.
112 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
114 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
117 struct crypto_ablkcipher;
118 struct crypto_async_request;
120 struct crypto_blkcipher;
125 struct aead_givcrypt_request;
126 struct skcipher_givcrypt_request;
128 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
131 * DOC: Block Cipher Context Data Structures
133 * These data structures define the operating context for each block cipher
137 struct crypto_async_request {
138 struct list_head list;
139 crypto_completion_t complete;
141 struct crypto_tfm *tfm;
146 struct ablkcipher_request {
147 struct crypto_async_request base;
153 struct scatterlist *src;
154 struct scatterlist *dst;
156 void *__ctx[] CRYPTO_MINALIGN_ATTR;
160 * struct aead_request - AEAD request
161 * @base: Common attributes for async crypto requests
162 * @assoclen: Length in bytes of associated data for authentication
163 * @cryptlen: Length of data to be encrypted or decrypted
164 * @iv: Initialisation vector
165 * @assoc: Associated data
167 * @dst: Destination data
168 * @__ctx: Start of private context data
170 struct aead_request {
171 struct crypto_async_request base;
173 unsigned int assoclen;
174 unsigned int cryptlen;
178 struct scatterlist *assoc;
179 struct scatterlist *src;
180 struct scatterlist *dst;
182 void *__ctx[] CRYPTO_MINALIGN_ATTR;
185 struct blkcipher_desc {
186 struct crypto_blkcipher *tfm;
192 struct crypto_tfm *tfm;
193 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
194 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
195 const u8 *src, unsigned int nbytes);
200 struct crypto_hash *tfm;
205 * DOC: Block Cipher Algorithm Definitions
207 * These data structures define modular crypto algorithm implementations,
208 * managed via crypto_register_alg() and crypto_unregister_alg().
212 * struct ablkcipher_alg - asynchronous block cipher definition
213 * @min_keysize: Minimum key size supported by the transformation. This is the
214 * smallest key length supported by this transformation algorithm.
215 * This must be set to one of the pre-defined values as this is
216 * not hardware specific. Possible values for this field can be
217 * found via git grep "_MIN_KEY_SIZE" include/crypto/
218 * @max_keysize: Maximum key size supported by the transformation. This is the
219 * largest key length supported by this transformation algorithm.
220 * This must be set to one of the pre-defined values as this is
221 * not hardware specific. Possible values for this field can be
222 * found via git grep "_MAX_KEY_SIZE" include/crypto/
223 * @setkey: Set key for the transformation. This function is used to either
224 * program a supplied key into the hardware or store the key in the
225 * transformation context for programming it later. Note that this
226 * function does modify the transformation context. This function can
227 * be called multiple times during the existence of the transformation
228 * object, so one must make sure the key is properly reprogrammed into
229 * the hardware. This function is also responsible for checking the key
230 * length for validity. In case a software fallback was put in place in
231 * the @cra_init call, this function might need to use the fallback if
232 * the algorithm doesn't support all of the key sizes.
233 * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt
234 * the supplied scatterlist containing the blocks of data. The crypto
235 * API consumer is responsible for aligning the entries of the
236 * scatterlist properly and making sure the chunks are correctly
237 * sized. In case a software fallback was put in place in the
238 * @cra_init call, this function might need to use the fallback if
239 * the algorithm doesn't support all of the key sizes. In case the
240 * key was stored in transformation context, the key might need to be
241 * re-programmed into the hardware in this function. This function
242 * shall not modify the transformation context, as this function may
243 * be called in parallel with the same transformation object.
244 * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt
245 * and the conditions are exactly the same.
246 * @givencrypt: Update the IV for encryption. With this function, a cipher
247 * implementation may provide the function on how to update the IV
249 * @givdecrypt: Update the IV for decryption. This is the reverse of
251 * @geniv: The transformation implementation may use an "IV generator" provided
252 * by the kernel crypto API. Several use cases have a predefined
253 * approach how IVs are to be updated. For such use cases, the kernel
254 * crypto API provides ready-to-use implementations that can be
255 * referenced with this variable.
256 * @ivsize: IV size applicable for transformation. The consumer must provide an
257 * IV of exactly that size to perform the encrypt or decrypt operation.
259 * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
260 * mandatory and must be filled.
262 struct ablkcipher_alg {
263 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
264 unsigned int keylen);
265 int (*encrypt)(struct ablkcipher_request *req);
266 int (*decrypt)(struct ablkcipher_request *req);
267 int (*givencrypt)(struct skcipher_givcrypt_request *req);
268 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
272 unsigned int min_keysize;
273 unsigned int max_keysize;
278 * struct aead_alg - AEAD cipher definition
279 * @maxauthsize: Set the maximum authentication tag size supported by the
280 * transformation. A transformation may support smaller tag sizes.
281 * As the authentication tag is a message digest to ensure the
282 * integrity of the encrypted data, a consumer typically wants the
283 * largest authentication tag possible as defined by this
285 * @setauthsize: Set authentication size for the AEAD transformation. This
286 * function is used to specify the consumer requested size of the
287 * authentication tag to be either generated by the transformation
288 * during encryption or the size of the authentication tag to be
289 * supplied during the decryption operation. This function is also
290 * responsible for checking the authentication tag size for
292 * @setkey: see struct ablkcipher_alg
293 * @encrypt: see struct ablkcipher_alg
294 * @decrypt: see struct ablkcipher_alg
295 * @givencrypt: see struct ablkcipher_alg
296 * @givdecrypt: see struct ablkcipher_alg
297 * @geniv: see struct ablkcipher_alg
298 * @ivsize: see struct ablkcipher_alg
300 * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
301 * mandatory and must be filled.
304 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
305 unsigned int keylen);
306 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
307 int (*encrypt)(struct aead_request *req);
308 int (*decrypt)(struct aead_request *req);
309 int (*givencrypt)(struct aead_givcrypt_request *req);
310 int (*givdecrypt)(struct aead_givcrypt_request *req);
315 unsigned int maxauthsize;
319 * struct blkcipher_alg - synchronous block cipher definition
320 * @min_keysize: see struct ablkcipher_alg
321 * @max_keysize: see struct ablkcipher_alg
322 * @setkey: see struct ablkcipher_alg
323 * @encrypt: see struct ablkcipher_alg
324 * @decrypt: see struct ablkcipher_alg
325 * @geniv: see struct ablkcipher_alg
326 * @ivsize: see struct ablkcipher_alg
328 * All fields except @geniv and @ivsize are mandatory and must be filled.
330 struct blkcipher_alg {
331 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
332 unsigned int keylen);
333 int (*encrypt)(struct blkcipher_desc *desc,
334 struct scatterlist *dst, struct scatterlist *src,
335 unsigned int nbytes);
336 int (*decrypt)(struct blkcipher_desc *desc,
337 struct scatterlist *dst, struct scatterlist *src,
338 unsigned int nbytes);
342 unsigned int min_keysize;
343 unsigned int max_keysize;
348 * struct cipher_alg - single-block symmetric ciphers definition
349 * @cia_min_keysize: Minimum key size supported by the transformation. This is
350 * the smallest key length supported by this transformation
351 * algorithm. This must be set to one of the pre-defined
352 * values as this is not hardware specific. Possible values
353 * for this field can be found via git grep "_MIN_KEY_SIZE"
355 * @cia_max_keysize: Maximum key size supported by the transformation. This is
356 * the largest key length supported by this transformation
357 * algorithm. This must be set to one of the pre-defined values
358 * as this is not hardware specific. Possible values for this
359 * field can be found via git grep "_MAX_KEY_SIZE"
361 * @cia_setkey: Set key for the transformation. This function is used to either
362 * program a supplied key into the hardware or store the key in the
363 * transformation context for programming it later. Note that this
364 * function does modify the transformation context. This function
365 * can be called multiple times during the existence of the
366 * transformation object, so one must make sure the key is properly
367 * reprogrammed into the hardware. This function is also
368 * responsible for checking the key length for validity.
369 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
370 * single block of data, which must be @cra_blocksize big. This
371 * always operates on a full @cra_blocksize and it is not possible
372 * to encrypt a block of smaller size. The supplied buffers must
373 * therefore also be at least of @cra_blocksize size. Both the
374 * input and output buffers are always aligned to @cra_alignmask.
375 * In case either of the input or output buffer supplied by user
376 * of the crypto API is not aligned to @cra_alignmask, the crypto
377 * API will re-align the buffers. The re-alignment means that a
378 * new buffer will be allocated, the data will be copied into the
379 * new buffer, then the processing will happen on the new buffer,
380 * then the data will be copied back into the original buffer and
381 * finally the new buffer will be freed. In case a software
382 * fallback was put in place in the @cra_init call, this function
383 * might need to use the fallback if the algorithm doesn't support
384 * all of the key sizes. In case the key was stored in
385 * transformation context, the key might need to be re-programmed
386 * into the hardware in this function. This function shall not
387 * modify the transformation context, as this function may be
388 * called in parallel with the same transformation object.
389 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
390 * @cia_encrypt, and the conditions are exactly the same.
392 * All fields are mandatory and must be filled.
395 unsigned int cia_min_keysize;
396 unsigned int cia_max_keysize;
397 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
398 unsigned int keylen);
399 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
400 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
403 struct compress_alg {
404 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
405 unsigned int slen, u8 *dst, unsigned int *dlen);
406 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
407 unsigned int slen, u8 *dst, unsigned int *dlen);
411 * struct rng_alg - random number generator definition
412 * @rng_make_random: The function defined by this variable obtains a random
413 * number. The random number generator transform must generate
414 * the random number out of the context provided with this
416 * @rng_reset: Reset of the random number generator by clearing the entire state.
417 * With the invocation of this function call, the random number
418 * generator shall completely reinitialize its state. If the random
419 * number generator requires a seed for setting up a new state,
420 * the seed must be provided by the consumer while invoking this
421 * function. The required size of the seed is defined with
423 * @seedsize: The seed size required for a random number generator
424 * initialization defined with this variable. Some random number
425 * generators like the SP800-90A DRBG does not require a seed as the
426 * seeding is implemented internally without the need of support by
427 * the consumer. In this case, the seed size is set to zero.
430 int (*rng_make_random)(struct crypto_rng *tfm, u8 *rdata,
432 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
434 unsigned int seedsize;
438 #define cra_ablkcipher cra_u.ablkcipher
439 #define cra_aead cra_u.aead
440 #define cra_blkcipher cra_u.blkcipher
441 #define cra_cipher cra_u.cipher
442 #define cra_compress cra_u.compress
443 #define cra_rng cra_u.rng
446 * struct crypto_alg - definition of a cryptograpic cipher algorithm
447 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
448 * CRYPTO_ALG_* flags for the flags which go in here. Those are
449 * used for fine-tuning the description of the transformation
451 * @cra_blocksize: Minimum block size of this transformation. The size in bytes
452 * of the smallest possible unit which can be transformed with
453 * this algorithm. The users must respect this value.
454 * In case of HASH transformation, it is possible for a smaller
455 * block than @cra_blocksize to be passed to the crypto API for
456 * transformation, in case of any other transformation type, an
457 * error will be returned upon any attempt to transform smaller
458 * than @cra_blocksize chunks.
459 * @cra_ctxsize: Size of the operational context of the transformation. This
460 * value informs the kernel crypto API about the memory size
461 * needed to be allocated for the transformation context.
462 * @cra_alignmask: Alignment mask for the input and output data buffer. The data
463 * buffer containing the input data for the algorithm must be
464 * aligned to this alignment mask. The data buffer for the
465 * output data must be aligned to this alignment mask. Note that
466 * the Crypto API will do the re-alignment in software, but
467 * only under special conditions and there is a performance hit.
468 * The re-alignment happens at these occasions for different
469 * @cra_u types: cipher -- For both input data and output data
470 * buffer; ahash -- For output hash destination buf; shash --
471 * For output hash destination buf.
472 * This is needed on hardware which is flawed by design and
473 * cannot pick data from arbitrary addresses.
474 * @cra_priority: Priority of this transformation implementation. In case
475 * multiple transformations with same @cra_name are available to
476 * the Crypto API, the kernel will use the one with highest
478 * @cra_name: Generic name (usable by multiple implementations) of the
479 * transformation algorithm. This is the name of the transformation
480 * itself. This field is used by the kernel when looking up the
481 * providers of particular transformation.
482 * @cra_driver_name: Unique name of the transformation provider. This is the
483 * name of the provider of the transformation. This can be any
484 * arbitrary value, but in the usual case, this contains the
485 * name of the chip or provider and the name of the
486 * transformation algorithm.
487 * @cra_type: Type of the cryptographic transformation. This is a pointer to
488 * struct crypto_type, which implements callbacks common for all
489 * trasnformation types. There are multiple options:
490 * &crypto_blkcipher_type, &crypto_ablkcipher_type,
491 * &crypto_ahash_type, &crypto_aead_type, &crypto_rng_type.
492 * This field might be empty. In that case, there are no common
493 * callbacks. This is the case for: cipher, compress, shash.
494 * @cra_u: Callbacks implementing the transformation. This is a union of
495 * multiple structures. Depending on the type of transformation selected
496 * by @cra_type and @cra_flags above, the associated structure must be
497 * filled with callbacks. This field might be empty. This is the case
499 * @cra_init: Initialize the cryptographic transformation object. This function
500 * is used to initialize the cryptographic transformation object.
501 * This function is called only once at the instantiation time, right
502 * after the transformation context was allocated. In case the
503 * cryptographic hardware has some special requirements which need to
504 * be handled by software, this function shall check for the precise
505 * requirement of the transformation and put any software fallbacks
507 * @cra_exit: Deinitialize the cryptographic transformation object. This is a
508 * counterpart to @cra_init, used to remove various changes set in
510 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
511 * @cra_list: internally used
512 * @cra_users: internally used
513 * @cra_refcnt: internally used
514 * @cra_destroy: internally used
516 * The struct crypto_alg describes a generic Crypto API algorithm and is common
517 * for all of the transformations. Any variable not documented here shall not
518 * be used by a cipher implementation as it is internal to the Crypto API.
521 struct list_head cra_list;
522 struct list_head cra_users;
525 unsigned int cra_blocksize;
526 unsigned int cra_ctxsize;
527 unsigned int cra_alignmask;
532 char cra_name[CRYPTO_MAX_ALG_NAME];
533 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
535 const struct crypto_type *cra_type;
538 struct ablkcipher_alg ablkcipher;
539 struct aead_alg aead;
540 struct blkcipher_alg blkcipher;
541 struct cipher_alg cipher;
542 struct compress_alg compress;
546 int (*cra_init)(struct crypto_tfm *tfm);
547 void (*cra_exit)(struct crypto_tfm *tfm);
548 void (*cra_destroy)(struct crypto_alg *alg);
550 struct module *cra_module;
554 * Algorithm registration interface.
556 int crypto_register_alg(struct crypto_alg *alg);
557 int crypto_unregister_alg(struct crypto_alg *alg);
558 int crypto_register_algs(struct crypto_alg *algs, int count);
559 int crypto_unregister_algs(struct crypto_alg *algs, int count);
562 * Algorithm query interface.
564 int crypto_has_alg(const char *name, u32 type, u32 mask);
567 * Transforms: user-instantiated objects which encapsulate algorithms
568 * and core processing logic. Managed via crypto_alloc_*() and
569 * crypto_free_*(), as well as the various helpers below.
572 struct ablkcipher_tfm {
573 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
574 unsigned int keylen);
575 int (*encrypt)(struct ablkcipher_request *req);
576 int (*decrypt)(struct ablkcipher_request *req);
577 int (*givencrypt)(struct skcipher_givcrypt_request *req);
578 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
580 struct crypto_ablkcipher *base;
583 unsigned int reqsize;
587 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
588 unsigned int keylen);
589 int (*encrypt)(struct aead_request *req);
590 int (*decrypt)(struct aead_request *req);
591 int (*givencrypt)(struct aead_givcrypt_request *req);
592 int (*givdecrypt)(struct aead_givcrypt_request *req);
594 struct crypto_aead *base;
597 unsigned int authsize;
598 unsigned int reqsize;
601 struct blkcipher_tfm {
603 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
604 unsigned int keylen);
605 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
606 struct scatterlist *src, unsigned int nbytes);
607 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
608 struct scatterlist *src, unsigned int nbytes);
612 int (*cit_setkey)(struct crypto_tfm *tfm,
613 const u8 *key, unsigned int keylen);
614 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
615 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
619 int (*init)(struct hash_desc *desc);
620 int (*update)(struct hash_desc *desc,
621 struct scatterlist *sg, unsigned int nsg);
622 int (*final)(struct hash_desc *desc, u8 *out);
623 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
624 unsigned int nsg, u8 *out);
625 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
626 unsigned int keylen);
627 unsigned int digestsize;
630 struct compress_tfm {
631 int (*cot_compress)(struct crypto_tfm *tfm,
632 const u8 *src, unsigned int slen,
633 u8 *dst, unsigned int *dlen);
634 int (*cot_decompress)(struct crypto_tfm *tfm,
635 const u8 *src, unsigned int slen,
636 u8 *dst, unsigned int *dlen);
640 int (*rng_gen_random)(struct crypto_rng *tfm, u8 *rdata,
642 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
645 #define crt_ablkcipher crt_u.ablkcipher
646 #define crt_aead crt_u.aead
647 #define crt_blkcipher crt_u.blkcipher
648 #define crt_cipher crt_u.cipher
649 #define crt_hash crt_u.hash
650 #define crt_compress crt_u.compress
651 #define crt_rng crt_u.rng
658 struct ablkcipher_tfm ablkcipher;
659 struct aead_tfm aead;
660 struct blkcipher_tfm blkcipher;
661 struct cipher_tfm cipher;
662 struct hash_tfm hash;
663 struct compress_tfm compress;
667 void (*exit)(struct crypto_tfm *tfm);
669 struct crypto_alg *__crt_alg;
671 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
674 struct crypto_ablkcipher {
675 struct crypto_tfm base;
679 struct crypto_tfm base;
682 struct crypto_blkcipher {
683 struct crypto_tfm base;
686 struct crypto_cipher {
687 struct crypto_tfm base;
691 struct crypto_tfm base;
695 struct crypto_tfm base;
699 struct crypto_tfm base;
710 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
712 /* Maximum number of (rtattr) parameters for each template. */
713 #define CRYPTO_MAX_ATTRS 32
715 struct crypto_attr_alg {
716 char name[CRYPTO_MAX_ALG_NAME];
719 struct crypto_attr_type {
724 struct crypto_attr_u32 {
729 * Transform user interface.
732 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
733 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
735 static inline void crypto_free_tfm(struct crypto_tfm *tfm)
737 return crypto_destroy_tfm(tfm, tfm);
740 int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
743 * Transform helpers which query the underlying algorithm.
745 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
747 return tfm->__crt_alg->cra_name;
750 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
752 return tfm->__crt_alg->cra_driver_name;
755 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
757 return tfm->__crt_alg->cra_priority;
760 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
762 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
765 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
767 return tfm->__crt_alg->cra_blocksize;
770 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
772 return tfm->__crt_alg->cra_alignmask;
775 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
777 return tfm->crt_flags;
780 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
782 tfm->crt_flags |= flags;
785 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
787 tfm->crt_flags &= ~flags;
790 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
792 return tfm->__crt_ctx;
795 static inline unsigned int crypto_tfm_ctx_alignment(void)
797 struct crypto_tfm *tfm;
798 return __alignof__(tfm->__crt_ctx);
804 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
805 struct crypto_tfm *tfm)
807 return (struct crypto_ablkcipher *)tfm;
810 static inline u32 crypto_skcipher_type(u32 type)
812 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
813 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
817 static inline u32 crypto_skcipher_mask(u32 mask)
819 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
820 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
825 * DOC: Asynchronous Block Cipher API
827 * Asynchronous block cipher API is used with the ciphers of type
828 * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto).
830 * Asynchronous cipher operations imply that the function invocation for a
831 * cipher request returns immediately before the completion of the operation.
832 * The cipher request is scheduled as a separate kernel thread and therefore
833 * load-balanced on the different CPUs via the process scheduler. To allow
834 * the kernel crypto API to inform the caller about the completion of a cipher
835 * request, the caller must provide a callback function. That function is
836 * invoked with the cipher handle when the request completes.
838 * To support the asynchronous operation, additional information than just the
839 * cipher handle must be supplied to the kernel crypto API. That additional
840 * information is given by filling in the ablkcipher_request data structure.
842 * For the asynchronous block cipher API, the state is maintained with the tfm
843 * cipher handle. A single tfm can be used across multiple calls and in
844 * parallel. For asynchronous block cipher calls, context data supplied and
845 * only used by the caller can be referenced the request data structure in
846 * addition to the IV used for the cipher request. The maintenance of such
847 * state information would be important for a crypto driver implementer to
848 * have, because when calling the callback function upon completion of the
849 * cipher operation, that callback function may need some information about
850 * which operation just finished if it invoked multiple in parallel. This
851 * state information is unused by the kernel crypto API.
855 * crypto_alloc_ablkcipher() - allocate asynchronous block cipher handle
856 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
858 * @type: specifies the type of the cipher
859 * @mask: specifies the mask for the cipher
861 * Allocate a cipher handle for an ablkcipher. The returned struct
862 * crypto_ablkcipher is the cipher handle that is required for any subsequent
863 * API invocation for that ablkcipher.
865 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
866 * of an error, PTR_ERR() returns the error code.
868 struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
871 static inline struct crypto_tfm *crypto_ablkcipher_tfm(
872 struct crypto_ablkcipher *tfm)
878 * crypto_free_ablkcipher() - zeroize and free cipher handle
879 * @tfm: cipher handle to be freed
881 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
883 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
887 * crypto_has_ablkcipher() - Search for the availability of an ablkcipher.
888 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
890 * @type: specifies the type of the cipher
891 * @mask: specifies the mask for the cipher
893 * Return: true when the ablkcipher is known to the kernel crypto API; false
896 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
899 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
900 crypto_skcipher_mask(mask));
903 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
904 struct crypto_ablkcipher *tfm)
906 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
910 * crypto_ablkcipher_ivsize() - obtain IV size
911 * @tfm: cipher handle
913 * The size of the IV for the ablkcipher referenced by the cipher handle is
914 * returned. This IV size may be zero if the cipher does not need an IV.
916 * Return: IV size in bytes
918 static inline unsigned int crypto_ablkcipher_ivsize(
919 struct crypto_ablkcipher *tfm)
921 return crypto_ablkcipher_crt(tfm)->ivsize;
925 * crypto_ablkcipher_blocksize() - obtain block size of cipher
926 * @tfm: cipher handle
928 * The block size for the ablkcipher referenced with the cipher handle is
929 * returned. The caller may use that information to allocate appropriate
930 * memory for the data returned by the encryption or decryption operation
932 * Return: block size of cipher
934 static inline unsigned int crypto_ablkcipher_blocksize(
935 struct crypto_ablkcipher *tfm)
937 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
940 static inline unsigned int crypto_ablkcipher_alignmask(
941 struct crypto_ablkcipher *tfm)
943 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
946 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
948 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
951 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
954 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
957 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
960 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
964 * crypto_ablkcipher_setkey() - set key for cipher
965 * @tfm: cipher handle
966 * @key: buffer holding the key
967 * @keylen: length of the key in bytes
969 * The caller provided key is set for the ablkcipher referenced by the cipher
972 * Note, the key length determines the cipher type. Many block ciphers implement
973 * different cipher modes depending on the key size, such as AES-128 vs AES-192
974 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
977 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
979 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
980 const u8 *key, unsigned int keylen)
982 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
984 return crt->setkey(crt->base, key, keylen);
988 * crypto_ablkcipher_reqtfm() - obtain cipher handle from request
989 * @req: ablkcipher_request out of which the cipher handle is to be obtained
991 * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request
994 * Return: crypto_ablkcipher handle
996 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
997 struct ablkcipher_request *req)
999 return __crypto_ablkcipher_cast(req->base.tfm);
1003 * crypto_ablkcipher_encrypt() - encrypt plaintext
1004 * @req: reference to the ablkcipher_request handle that holds all information
1005 * needed to perform the cipher operation
1007 * Encrypt plaintext data using the ablkcipher_request handle. That data
1008 * structure and how it is filled with data is discussed with the
1009 * ablkcipher_request_* functions.
1011 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1013 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
1015 struct ablkcipher_tfm *crt =
1016 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
1017 return crt->encrypt(req);
1021 * crypto_ablkcipher_decrypt() - decrypt ciphertext
1022 * @req: reference to the ablkcipher_request handle that holds all information
1023 * needed to perform the cipher operation
1025 * Decrypt ciphertext data using the ablkcipher_request handle. That data
1026 * structure and how it is filled with data is discussed with the
1027 * ablkcipher_request_* functions.
1029 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1031 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
1033 struct ablkcipher_tfm *crt =
1034 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
1035 return crt->decrypt(req);
1039 * DOC: Asynchronous Cipher Request Handle
1041 * The ablkcipher_request data structure contains all pointers to data
1042 * required for the asynchronous cipher operation. This includes the cipher
1043 * handle (which can be used by multiple ablkcipher_request instances), pointer
1044 * to plaintext and ciphertext, asynchronous callback function, etc. It acts
1045 * as a handle to the ablkcipher_request_* API calls in a similar way as
1046 * ablkcipher handle to the crypto_ablkcipher_* API calls.
1050 * crypto_ablkcipher_reqsize() - obtain size of the request data structure
1051 * @tfm: cipher handle
1053 * Return: number of bytes
1055 static inline unsigned int crypto_ablkcipher_reqsize(
1056 struct crypto_ablkcipher *tfm)
1058 return crypto_ablkcipher_crt(tfm)->reqsize;
1062 * ablkcipher_request_set_tfm() - update cipher handle reference in request
1063 * @req: request handle to be modified
1064 * @tfm: cipher handle that shall be added to the request handle
1066 * Allow the caller to replace the existing ablkcipher handle in the request
1067 * data structure with a different one.
1069 static inline void ablkcipher_request_set_tfm(
1070 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
1072 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
1075 static inline struct ablkcipher_request *ablkcipher_request_cast(
1076 struct crypto_async_request *req)
1078 return container_of(req, struct ablkcipher_request, base);
1082 * ablkcipher_request_alloc() - allocate request data structure
1083 * @tfm: cipher handle to be registered with the request
1084 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
1086 * Allocate the request data structure that must be used with the ablkcipher
1087 * encrypt and decrypt API calls. During the allocation, the provided ablkcipher
1088 * handle is registered in the request data structure.
1090 * Return: allocated request handle in case of success; IS_ERR() is true in case
1091 * of an error, PTR_ERR() returns the error code.
1093 static inline struct ablkcipher_request *ablkcipher_request_alloc(
1094 struct crypto_ablkcipher *tfm, gfp_t gfp)
1096 struct ablkcipher_request *req;
1098 req = kmalloc(sizeof(struct ablkcipher_request) +
1099 crypto_ablkcipher_reqsize(tfm), gfp);
1102 ablkcipher_request_set_tfm(req, tfm);
1108 * ablkcipher_request_free() - zeroize and free request data structure
1109 * @req: request data structure cipher handle to be freed
1111 static inline void ablkcipher_request_free(struct ablkcipher_request *req)
1117 * ablkcipher_request_set_callback() - set asynchronous callback function
1118 * @req: request handle
1119 * @flags: specify zero or an ORing of the flags
1120 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
1121 * increase the wait queue beyond the initial maximum size;
1122 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
1123 * @compl: callback function pointer to be registered with the request handle
1124 * @data: The data pointer refers to memory that is not used by the kernel
1125 * crypto API, but provided to the callback function for it to use. Here,
1126 * the caller can provide a reference to memory the callback function can
1127 * operate on. As the callback function is invoked asynchronously to the
1128 * related functionality, it may need to access data structures of the
1129 * related functionality which can be referenced using this pointer. The
1130 * callback function can access the memory via the "data" field in the
1131 * crypto_async_request data structure provided to the callback function.
1133 * This function allows setting the callback function that is triggered once the
1134 * cipher operation completes.
1136 * The callback function is registered with the ablkcipher_request handle and
1137 * must comply with the following template:
1139 * void callback_function(struct crypto_async_request *req, int error)
1141 static inline void ablkcipher_request_set_callback(
1142 struct ablkcipher_request *req,
1143 u32 flags, crypto_completion_t compl, void *data)
1145 req->base.complete = compl;
1146 req->base.data = data;
1147 req->base.flags = flags;
1151 * ablkcipher_request_set_crypt() - set data buffers
1152 * @req: request handle
1153 * @src: source scatter / gather list
1154 * @dst: destination scatter / gather list
1155 * @nbytes: number of bytes to process from @src
1156 * @iv: IV for the cipher operation which must comply with the IV size defined
1157 * by crypto_ablkcipher_ivsize
1159 * This function allows setting of the source data and destination data
1160 * scatter / gather lists.
1162 * For encryption, the source is treated as the plaintext and the
1163 * destination is the ciphertext. For a decryption operation, the use is
1164 * reversed: the source is the ciphertext and the destination is the plaintext.
1166 static inline void ablkcipher_request_set_crypt(
1167 struct ablkcipher_request *req,
1168 struct scatterlist *src, struct scatterlist *dst,
1169 unsigned int nbytes, void *iv)
1173 req->nbytes = nbytes;
1178 * DOC: Authenticated Encryption With Associated Data (AEAD) Cipher API
1180 * The AEAD cipher API is used with the ciphers of type CRYPTO_ALG_TYPE_AEAD
1181 * (listed as type "aead" in /proc/crypto)
1183 * The most prominent examples for this type of encryption is GCM and CCM.
1184 * However, the kernel supports other types of AEAD ciphers which are defined
1185 * with the following cipher string:
1187 * authenc(keyed message digest, block cipher)
1189 * For example: authenc(hmac(sha256), cbc(aes))
1191 * The example code provided for the asynchronous block cipher operation
1192 * applies here as well. Naturally all *ablkcipher* symbols must be exchanged
1193 * the *aead* pendants discussed in the following. In addtion, for the AEAD
1194 * operation, the aead_request_set_assoc function must be used to set the
1195 * pointer to the associated data memory location before performing the
1196 * encryption or decryption operation. In case of an encryption, the associated
1197 * data memory is filled during the encryption operation. For decryption, the
1198 * associated data memory must contain data that is used to verify the integrity
1199 * of the decrypted data. Another deviation from the asynchronous block cipher
1200 * operation is that the caller should explicitly check for -EBADMSG of the
1201 * crypto_aead_decrypt. That error indicates an authentication error, i.e.
1202 * a breach in the integrity of the message. In essence, that -EBADMSG error
1203 * code is the key bonus an AEAD cipher has over "standard" block chaining
1207 static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
1209 return (struct crypto_aead *)tfm;
1213 * crypto_alloc_aead() - allocate AEAD cipher handle
1214 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1216 * @type: specifies the type of the cipher
1217 * @mask: specifies the mask for the cipher
1219 * Allocate a cipher handle for an AEAD. The returned struct
1220 * crypto_aead is the cipher handle that is required for any subsequent
1221 * API invocation for that AEAD.
1223 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1224 * of an error, PTR_ERR() returns the error code.
1226 struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
1228 static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
1234 * crypto_free_aead() - zeroize and free aead handle
1235 * @tfm: cipher handle to be freed
1237 static inline void crypto_free_aead(struct crypto_aead *tfm)
1239 crypto_free_tfm(crypto_aead_tfm(tfm));
1242 static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
1244 return &crypto_aead_tfm(tfm)->crt_aead;
1248 * crypto_aead_ivsize() - obtain IV size
1249 * @tfm: cipher handle
1251 * The size of the IV for the aead referenced by the cipher handle is
1252 * returned. This IV size may be zero if the cipher does not need an IV.
1254 * Return: IV size in bytes
1256 static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
1258 return crypto_aead_crt(tfm)->ivsize;
1262 * crypto_aead_authsize() - obtain maximum authentication data size
1263 * @tfm: cipher handle
1265 * The maximum size of the authentication data for the AEAD cipher referenced
1266 * by the AEAD cipher handle is returned. The authentication data size may be
1267 * zero if the cipher implements a hard-coded maximum.
1269 * The authentication data may also be known as "tag value".
1271 * Return: authentication data size / tag size in bytes
1273 static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
1275 return crypto_aead_crt(tfm)->authsize;
1279 * crypto_aead_blocksize() - obtain block size of cipher
1280 * @tfm: cipher handle
1282 * The block size for the AEAD referenced with the cipher handle is returned.
1283 * The caller may use that information to allocate appropriate memory for the
1284 * data returned by the encryption or decryption operation
1286 * Return: block size of cipher
1288 static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
1290 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
1293 static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
1295 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
1298 static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
1300 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
1303 static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
1305 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
1308 static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
1310 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
1314 * crypto_aead_setkey() - set key for cipher
1315 * @tfm: cipher handle
1316 * @key: buffer holding the key
1317 * @keylen: length of the key in bytes
1319 * The caller provided key is set for the AEAD referenced by the cipher
1322 * Note, the key length determines the cipher type. Many block ciphers implement
1323 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1324 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1327 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1329 static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
1330 unsigned int keylen)
1332 struct aead_tfm *crt = crypto_aead_crt(tfm);
1334 return crt->setkey(crt->base, key, keylen);
1338 * crypto_aead_setauthsize() - set authentication data size
1339 * @tfm: cipher handle
1340 * @authsize: size of the authentication data / tag in bytes
1342 * Set the authentication data size / tag size. AEAD requires an authentication
1343 * tag (or MAC) in addition to the associated data.
1345 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1347 int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
1349 static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
1351 return __crypto_aead_cast(req->base.tfm);
1355 * crypto_aead_encrypt() - encrypt plaintext
1356 * @req: reference to the aead_request handle that holds all information
1357 * needed to perform the cipher operation
1359 * Encrypt plaintext data using the aead_request handle. That data structure
1360 * and how it is filled with data is discussed with the aead_request_*
1363 * IMPORTANT NOTE The encryption operation creates the authentication data /
1364 * tag. That data is concatenated with the created ciphertext.
1365 * The ciphertext memory size is therefore the given number of
1366 * block cipher blocks + the size defined by the
1367 * crypto_aead_setauthsize invocation. The caller must ensure
1368 * that sufficient memory is available for the ciphertext and
1369 * the authentication tag.
1371 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1373 static inline int crypto_aead_encrypt(struct aead_request *req)
1375 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
1379 * crypto_aead_decrypt() - decrypt ciphertext
1380 * @req: reference to the ablkcipher_request handle that holds all information
1381 * needed to perform the cipher operation
1383 * Decrypt ciphertext data using the aead_request handle. That data structure
1384 * and how it is filled with data is discussed with the aead_request_*
1387 * IMPORTANT NOTE The caller must concatenate the ciphertext followed by the
1388 * authentication data / tag. That authentication data / tag
1389 * must have the size defined by the crypto_aead_setauthsize
1393 * Return: 0 if the cipher operation was successful; -EBADMSG: The AEAD
1394 * cipher operation performs the authentication of the data during the
1395 * decryption operation. Therefore, the function returns this error if
1396 * the authentication of the ciphertext was unsuccessful (i.e. the
1397 * integrity of the ciphertext or the associated data was violated);
1398 * < 0 if an error occurred.
1400 static inline int crypto_aead_decrypt(struct aead_request *req)
1402 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
1406 * DOC: Asynchronous AEAD Request Handle
1408 * The aead_request data structure contains all pointers to data required for
1409 * the AEAD cipher operation. This includes the cipher handle (which can be
1410 * used by multiple aead_request instances), pointer to plaintext and
1411 * ciphertext, asynchronous callback function, etc. It acts as a handle to the
1412 * aead_request_* API calls in a similar way as AEAD handle to the
1413 * crypto_aead_* API calls.
1417 * crypto_aead_reqsize() - obtain size of the request data structure
1418 * @tfm: cipher handle
1420 * Return: number of bytes
1422 static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
1424 return crypto_aead_crt(tfm)->reqsize;
1428 * aead_request_set_tfm() - update cipher handle reference in request
1429 * @req: request handle to be modified
1430 * @tfm: cipher handle that shall be added to the request handle
1432 * Allow the caller to replace the existing aead handle in the request
1433 * data structure with a different one.
1435 static inline void aead_request_set_tfm(struct aead_request *req,
1436 struct crypto_aead *tfm)
1438 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
1442 * aead_request_alloc() - allocate request data structure
1443 * @tfm: cipher handle to be registered with the request
1444 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
1446 * Allocate the request data structure that must be used with the AEAD
1447 * encrypt and decrypt API calls. During the allocation, the provided aead
1448 * handle is registered in the request data structure.
1450 * Return: allocated request handle in case of success; IS_ERR() is true in case
1451 * of an error, PTR_ERR() returns the error code.
1453 static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
1456 struct aead_request *req;
1458 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
1461 aead_request_set_tfm(req, tfm);
1467 * aead_request_free() - zeroize and free request data structure
1468 * @req: request data structure cipher handle to be freed
1470 static inline void aead_request_free(struct aead_request *req)
1476 * aead_request_set_callback() - set asynchronous callback function
1477 * @req: request handle
1478 * @flags: specify zero or an ORing of the flags
1479 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
1480 * increase the wait queue beyond the initial maximum size;
1481 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
1482 * @compl: callback function pointer to be registered with the request handle
1483 * @data: The data pointer refers to memory that is not used by the kernel
1484 * crypto API, but provided to the callback function for it to use. Here,
1485 * the caller can provide a reference to memory the callback function can
1486 * operate on. As the callback function is invoked asynchronously to the
1487 * related functionality, it may need to access data structures of the
1488 * related functionality which can be referenced using this pointer. The
1489 * callback function can access the memory via the "data" field in the
1490 * crypto_async_request data structure provided to the callback function.
1492 * Setting the callback function that is triggered once the cipher operation
1495 * The callback function is registered with the aead_request handle and
1496 * must comply with the following template:
1498 * void callback_function(struct crypto_async_request *req, int error)
1500 static inline void aead_request_set_callback(struct aead_request *req,
1502 crypto_completion_t compl,
1505 req->base.complete = compl;
1506 req->base.data = data;
1507 req->base.flags = flags;
1511 * aead_request_set_crypt - set data buffers
1512 * @req: request handle
1513 * @src: source scatter / gather list
1514 * @dst: destination scatter / gather list
1515 * @cryptlen: number of bytes to process from @src
1516 * @iv: IV for the cipher operation which must comply with the IV size defined
1517 * by crypto_aead_ivsize()
1519 * Setting the source data and destination data scatter / gather lists.
1521 * For encryption, the source is treated as the plaintext and the
1522 * destination is the ciphertext. For a decryption operation, the use is
1523 * reversed: the source is the ciphertext and the destination is the plaintext.
1525 * IMPORTANT NOTE AEAD requires an authentication tag (MAC). For decryption,
1526 * the caller must concatenate the ciphertext followed by the
1527 * authentication tag and provide the entire data stream to the
1528 * decryption operation (i.e. the data length used for the
1529 * initialization of the scatterlist and the data length for the
1530 * decryption operation is identical). For encryption, however,
1531 * the authentication tag is created while encrypting the data.
1532 * The destination buffer must hold sufficient space for the
1533 * ciphertext and the authentication tag while the encryption
1534 * invocation must only point to the plaintext data size. The
1535 * following code snippet illustrates the memory usage
1536 * buffer = kmalloc(ptbuflen + (enc ? authsize : 0));
1537 * sg_init_one(&sg, buffer, ptbuflen + (enc ? authsize : 0));
1538 * aead_request_set_crypt(req, &sg, &sg, ptbuflen, iv);
1540 static inline void aead_request_set_crypt(struct aead_request *req,
1541 struct scatterlist *src,
1542 struct scatterlist *dst,
1543 unsigned int cryptlen, u8 *iv)
1547 req->cryptlen = cryptlen;
1552 * aead_request_set_assoc() - set the associated data scatter / gather list
1553 * @req: request handle
1554 * @assoc: associated data scatter / gather list
1555 * @assoclen: number of bytes to process from @assoc
1557 * For encryption, the memory is filled with the associated data. For
1558 * decryption, the memory must point to the associated data.
1560 static inline void aead_request_set_assoc(struct aead_request *req,
1561 struct scatterlist *assoc,
1562 unsigned int assoclen)
1565 req->assoclen = assoclen;
1569 * DOC: Synchronous Block Cipher API
1571 * The synchronous block cipher API is used with the ciphers of type
1572 * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto)
1574 * Synchronous calls, have a context in the tfm. But since a single tfm can be
1575 * used in multiple calls and in parallel, this info should not be changeable
1576 * (unless a lock is used). This applies, for example, to the symmetric key.
1577 * However, the IV is changeable, so there is an iv field in blkcipher_tfm
1578 * structure for synchronous blkcipher api. So, its the only state info that can
1579 * be kept for synchronous calls without using a big lock across a tfm.
1581 * The block cipher API allows the use of a complete cipher, i.e. a cipher
1582 * consisting of a template (a block chaining mode) and a single block cipher
1583 * primitive (e.g. AES).
1585 * The plaintext data buffer and the ciphertext data buffer are pointed to
1586 * by using scatter/gather lists. The cipher operation is performed
1587 * on all segments of the provided scatter/gather lists.
1589 * The kernel crypto API supports a cipher operation "in-place" which means that
1590 * the caller may provide the same scatter/gather list for the plaintext and
1591 * cipher text. After the completion of the cipher operation, the plaintext
1592 * data is replaced with the ciphertext data in case of an encryption and vice
1593 * versa for a decryption. The caller must ensure that the scatter/gather lists
1594 * for the output data point to sufficiently large buffers, i.e. multiples of
1595 * the block size of the cipher.
1598 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
1599 struct crypto_tfm *tfm)
1601 return (struct crypto_blkcipher *)tfm;
1604 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
1605 struct crypto_tfm *tfm)
1607 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
1608 return __crypto_blkcipher_cast(tfm);
1612 * crypto_alloc_blkcipher() - allocate synchronous block cipher handle
1613 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1615 * @type: specifies the type of the cipher
1616 * @mask: specifies the mask for the cipher
1618 * Allocate a cipher handle for a block cipher. The returned struct
1619 * crypto_blkcipher is the cipher handle that is required for any subsequent
1620 * API invocation for that block cipher.
1622 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1623 * of an error, PTR_ERR() returns the error code.
1625 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
1626 const char *alg_name, u32 type, u32 mask)
1628 type &= ~CRYPTO_ALG_TYPE_MASK;
1629 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
1630 mask |= CRYPTO_ALG_TYPE_MASK;
1632 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
1635 static inline struct crypto_tfm *crypto_blkcipher_tfm(
1636 struct crypto_blkcipher *tfm)
1642 * crypto_free_blkcipher() - zeroize and free the block cipher handle
1643 * @tfm: cipher handle to be freed
1645 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
1647 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
1651 * crypto_has_blkcipher() - Search for the availability of a block cipher
1652 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1654 * @type: specifies the type of the cipher
1655 * @mask: specifies the mask for the cipher
1657 * Return: true when the block cipher is known to the kernel crypto API; false
1660 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
1662 type &= ~CRYPTO_ALG_TYPE_MASK;
1663 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
1664 mask |= CRYPTO_ALG_TYPE_MASK;
1666 return crypto_has_alg(alg_name, type, mask);
1670 * crypto_blkcipher_name() - return the name / cra_name from the cipher handle
1671 * @tfm: cipher handle
1673 * Return: The character string holding the name of the cipher
1675 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
1677 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
1680 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
1681 struct crypto_blkcipher *tfm)
1683 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
1686 static inline struct blkcipher_alg *crypto_blkcipher_alg(
1687 struct crypto_blkcipher *tfm)
1689 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
1693 * crypto_blkcipher_ivsize() - obtain IV size
1694 * @tfm: cipher handle
1696 * The size of the IV for the block cipher referenced by the cipher handle is
1697 * returned. This IV size may be zero if the cipher does not need an IV.
1699 * Return: IV size in bytes
1701 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
1703 return crypto_blkcipher_alg(tfm)->ivsize;
1707 * crypto_blkcipher_blocksize() - obtain block size of cipher
1708 * @tfm: cipher handle
1710 * The block size for the block cipher referenced with the cipher handle is
1711 * returned. The caller may use that information to allocate appropriate
1712 * memory for the data returned by the encryption or decryption operation.
1714 * Return: block size of cipher
1716 static inline unsigned int crypto_blkcipher_blocksize(
1717 struct crypto_blkcipher *tfm)
1719 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1722 static inline unsigned int crypto_blkcipher_alignmask(
1723 struct crypto_blkcipher *tfm)
1725 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1728 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1730 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1733 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1736 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1739 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1742 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1746 * crypto_blkcipher_setkey() - set key for cipher
1747 * @tfm: cipher handle
1748 * @key: buffer holding the key
1749 * @keylen: length of the key in bytes
1751 * The caller provided key is set for the block cipher referenced by the cipher
1754 * Note, the key length determines the cipher type. Many block ciphers implement
1755 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1756 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1759 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1761 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1762 const u8 *key, unsigned int keylen)
1764 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1769 * crypto_blkcipher_encrypt() - encrypt plaintext
1770 * @desc: reference to the block cipher handle with meta data
1771 * @dst: scatter/gather list that is filled by the cipher operation with the
1773 * @src: scatter/gather list that holds the plaintext
1774 * @nbytes: number of bytes of the plaintext to encrypt.
1776 * Encrypt plaintext data using the IV set by the caller with a preceding
1777 * call of crypto_blkcipher_set_iv.
1779 * The blkcipher_desc data structure must be filled by the caller and can
1780 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1781 * with the block cipher handle; desc.flags is filled with either
1782 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1784 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1786 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1787 struct scatterlist *dst,
1788 struct scatterlist *src,
1789 unsigned int nbytes)
1791 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1792 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1796 * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV
1797 * @desc: reference to the block cipher handle with meta data
1798 * @dst: scatter/gather list that is filled by the cipher operation with the
1800 * @src: scatter/gather list that holds the plaintext
1801 * @nbytes: number of bytes of the plaintext to encrypt.
1803 * Encrypt plaintext data with the use of an IV that is solely used for this
1804 * cipher operation. Any previously set IV is not used.
1806 * The blkcipher_desc data structure must be filled by the caller and can
1807 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1808 * with the block cipher handle; desc.info is filled with the IV to be used for
1809 * the current operation; desc.flags is filled with either
1810 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1812 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1814 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1815 struct scatterlist *dst,
1816 struct scatterlist *src,
1817 unsigned int nbytes)
1819 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1823 * crypto_blkcipher_decrypt() - decrypt ciphertext
1824 * @desc: reference to the block cipher handle with meta data
1825 * @dst: scatter/gather list that is filled by the cipher operation with the
1827 * @src: scatter/gather list that holds the ciphertext
1828 * @nbytes: number of bytes of the ciphertext to decrypt.
1830 * Decrypt ciphertext data using the IV set by the caller with a preceding
1831 * call of crypto_blkcipher_set_iv.
1833 * The blkcipher_desc data structure must be filled by the caller as documented
1834 * for the crypto_blkcipher_encrypt call above.
1836 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1839 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1840 struct scatterlist *dst,
1841 struct scatterlist *src,
1842 unsigned int nbytes)
1844 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1845 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1849 * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV
1850 * @desc: reference to the block cipher handle with meta data
1851 * @dst: scatter/gather list that is filled by the cipher operation with the
1853 * @src: scatter/gather list that holds the ciphertext
1854 * @nbytes: number of bytes of the ciphertext to decrypt.
1856 * Decrypt ciphertext data with the use of an IV that is solely used for this
1857 * cipher operation. Any previously set IV is not used.
1859 * The blkcipher_desc data structure must be filled by the caller as documented
1860 * for the crypto_blkcipher_encrypt_iv call above.
1862 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1864 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1865 struct scatterlist *dst,
1866 struct scatterlist *src,
1867 unsigned int nbytes)
1869 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1873 * crypto_blkcipher_set_iv() - set IV for cipher
1874 * @tfm: cipher handle
1875 * @src: buffer holding the IV
1876 * @len: length of the IV in bytes
1878 * The caller provided IV is set for the block cipher referenced by the cipher
1881 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1882 const u8 *src, unsigned int len)
1884 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1888 * crypto_blkcipher_get_iv() - obtain IV from cipher
1889 * @tfm: cipher handle
1890 * @dst: buffer filled with the IV
1891 * @len: length of the buffer dst
1893 * The caller can obtain the IV set for the block cipher referenced by the
1894 * cipher handle and store it into the user-provided buffer. If the buffer
1895 * has an insufficient space, the IV is truncated to fit the buffer.
1897 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1898 u8 *dst, unsigned int len)
1900 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1904 * DOC: Single Block Cipher API
1906 * The single block cipher API is used with the ciphers of type
1907 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
1909 * Using the single block cipher API calls, operations with the basic cipher
1910 * primitive can be implemented. These cipher primitives exclude any block
1911 * chaining operations including IV handling.
1913 * The purpose of this single block cipher API is to support the implementation
1914 * of templates or other concepts that only need to perform the cipher operation
1915 * on one block at a time. Templates invoke the underlying cipher primitive
1916 * block-wise and process either the input or the output data of these cipher
1920 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1922 return (struct crypto_cipher *)tfm;
1925 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1927 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1928 return __crypto_cipher_cast(tfm);
1932 * crypto_alloc_cipher() - allocate single block cipher handle
1933 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1934 * single block cipher
1935 * @type: specifies the type of the cipher
1936 * @mask: specifies the mask for the cipher
1938 * Allocate a cipher handle for a single block cipher. The returned struct
1939 * crypto_cipher is the cipher handle that is required for any subsequent API
1940 * invocation for that single block cipher.
1942 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1943 * of an error, PTR_ERR() returns the error code.
1945 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1948 type &= ~CRYPTO_ALG_TYPE_MASK;
1949 type |= CRYPTO_ALG_TYPE_CIPHER;
1950 mask |= CRYPTO_ALG_TYPE_MASK;
1952 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1955 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1961 * crypto_free_cipher() - zeroize and free the single block cipher handle
1962 * @tfm: cipher handle to be freed
1964 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1966 crypto_free_tfm(crypto_cipher_tfm(tfm));
1970 * crypto_has_cipher() - Search for the availability of a single block cipher
1971 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1972 * single block cipher
1973 * @type: specifies the type of the cipher
1974 * @mask: specifies the mask for the cipher
1976 * Return: true when the single block cipher is known to the kernel crypto API;
1979 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1981 type &= ~CRYPTO_ALG_TYPE_MASK;
1982 type |= CRYPTO_ALG_TYPE_CIPHER;
1983 mask |= CRYPTO_ALG_TYPE_MASK;
1985 return crypto_has_alg(alg_name, type, mask);
1988 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1990 return &crypto_cipher_tfm(tfm)->crt_cipher;
1994 * crypto_cipher_blocksize() - obtain block size for cipher
1995 * @tfm: cipher handle
1997 * The block size for the single block cipher referenced with the cipher handle
1998 * tfm is returned. The caller may use that information to allocate appropriate
1999 * memory for the data returned by the encryption or decryption operation
2001 * Return: block size of cipher
2003 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
2005 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
2008 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
2010 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
2013 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
2015 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
2018 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
2021 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
2024 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
2027 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
2031 * crypto_cipher_setkey() - set key for cipher
2032 * @tfm: cipher handle
2033 * @key: buffer holding the key
2034 * @keylen: length of the key in bytes
2036 * The caller provided key is set for the single block cipher referenced by the
2039 * Note, the key length determines the cipher type. Many block ciphers implement
2040 * different cipher modes depending on the key size, such as AES-128 vs AES-192
2041 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
2044 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
2046 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
2047 const u8 *key, unsigned int keylen)
2049 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
2054 * crypto_cipher_encrypt_one() - encrypt one block of plaintext
2055 * @tfm: cipher handle
2056 * @dst: points to the buffer that will be filled with the ciphertext
2057 * @src: buffer holding the plaintext to be encrypted
2059 * Invoke the encryption operation of one block. The caller must ensure that
2060 * the plaintext and ciphertext buffers are at least one block in size.
2062 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
2063 u8 *dst, const u8 *src)
2065 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
2070 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
2071 * @tfm: cipher handle
2072 * @dst: points to the buffer that will be filled with the plaintext
2073 * @src: buffer holding the ciphertext to be decrypted
2075 * Invoke the decryption operation of one block. The caller must ensure that
2076 * the plaintext and ciphertext buffers are at least one block in size.
2078 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
2079 u8 *dst, const u8 *src)
2081 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
2086 * DOC: Synchronous Message Digest API
2088 * The synchronous message digest API is used with the ciphers of type
2089 * CRYPTO_ALG_TYPE_HASH (listed as type "hash" in /proc/crypto)
2092 static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
2094 return (struct crypto_hash *)tfm;
2097 static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
2099 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
2100 CRYPTO_ALG_TYPE_HASH_MASK);
2101 return __crypto_hash_cast(tfm);
2105 * crypto_alloc_hash() - allocate synchronous message digest handle
2106 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
2107 * message digest cipher
2108 * @type: specifies the type of the cipher
2109 * @mask: specifies the mask for the cipher
2111 * Allocate a cipher handle for a message digest. The returned struct
2112 * crypto_hash is the cipher handle that is required for any subsequent
2113 * API invocation for that message digest.
2115 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
2116 * of an error, PTR_ERR() returns the error code.
2118 static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
2121 type &= ~CRYPTO_ALG_TYPE_MASK;
2122 mask &= ~CRYPTO_ALG_TYPE_MASK;
2123 type |= CRYPTO_ALG_TYPE_HASH;
2124 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
2126 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
2129 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
2135 * crypto_free_hash() - zeroize and free message digest handle
2136 * @tfm: cipher handle to be freed
2138 static inline void crypto_free_hash(struct crypto_hash *tfm)
2140 crypto_free_tfm(crypto_hash_tfm(tfm));
2144 * crypto_has_hash() - Search for the availability of a message digest
2145 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
2146 * message digest cipher
2147 * @type: specifies the type of the cipher
2148 * @mask: specifies the mask for the cipher
2150 * Return: true when the message digest cipher is known to the kernel crypto
2151 * API; false otherwise
2153 static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
2155 type &= ~CRYPTO_ALG_TYPE_MASK;
2156 mask &= ~CRYPTO_ALG_TYPE_MASK;
2157 type |= CRYPTO_ALG_TYPE_HASH;
2158 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
2160 return crypto_has_alg(alg_name, type, mask);
2163 static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
2165 return &crypto_hash_tfm(tfm)->crt_hash;
2169 * crypto_hash_blocksize() - obtain block size for message digest
2170 * @tfm: cipher handle
2172 * The block size for the message digest cipher referenced with the cipher
2173 * handle is returned.
2175 * Return: block size of cipher
2177 static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
2179 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
2182 static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
2184 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
2188 * crypto_hash_digestsize() - obtain message digest size
2189 * @tfm: cipher handle
2191 * The size for the message digest created by the message digest cipher
2192 * referenced with the cipher handle is returned.
2194 * Return: message digest size
2196 static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
2198 return crypto_hash_crt(tfm)->digestsize;
2201 static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
2203 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
2206 static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
2208 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
2211 static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
2213 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
2217 * crypto_hash_init() - (re)initialize message digest handle
2218 * @desc: cipher request handle that to be filled by caller --
2219 * desc.tfm is filled with the hash cipher handle;
2220 * desc.flags is filled with either CRYPTO_TFM_REQ_MAY_SLEEP or 0.
2222 * The call (re-)initializes the message digest referenced by the hash cipher
2223 * request handle. Any potentially existing state created by previous
2224 * operations is discarded.
2226 * Return: 0 if the message digest initialization was successful; < 0 if an
2229 static inline int crypto_hash_init(struct hash_desc *desc)
2231 return crypto_hash_crt(desc->tfm)->init(desc);
2235 * crypto_hash_update() - add data to message digest for processing
2236 * @desc: cipher request handle
2237 * @sg: scatter / gather list pointing to the data to be added to the message
2239 * @nbytes: number of bytes to be processed from @sg
2241 * Updates the message digest state of the cipher handle pointed to by the
2242 * hash cipher request handle with the input data pointed to by the
2243 * scatter/gather list.
2245 * Return: 0 if the message digest update was successful; < 0 if an error
2248 static inline int crypto_hash_update(struct hash_desc *desc,
2249 struct scatterlist *sg,
2250 unsigned int nbytes)
2252 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
2256 * crypto_hash_final() - calculate message digest
2257 * @desc: cipher request handle
2258 * @out: message digest output buffer -- The caller must ensure that the out
2259 * buffer has a sufficient size (e.g. by using the crypto_hash_digestsize
2262 * Finalize the message digest operation and create the message digest
2263 * based on all data added to the cipher handle. The message digest is placed
2264 * into the output buffer.
2266 * Return: 0 if the message digest creation was successful; < 0 if an error
2269 static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
2271 return crypto_hash_crt(desc->tfm)->final(desc, out);
2275 * crypto_hash_digest() - calculate message digest for a buffer
2276 * @desc: see crypto_hash_final()
2277 * @sg: see crypto_hash_update()
2278 * @nbytes: see crypto_hash_update()
2279 * @out: see crypto_hash_final()
2281 * This function is a "short-hand" for the function calls of crypto_hash_init,
2282 * crypto_hash_update and crypto_hash_final. The parameters have the same
2283 * meaning as discussed for those separate three functions.
2285 * Return: 0 if the message digest creation was successful; < 0 if an error
2288 static inline int crypto_hash_digest(struct hash_desc *desc,
2289 struct scatterlist *sg,
2290 unsigned int nbytes, u8 *out)
2292 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
2296 * crypto_hash_setkey() - set key for message digest
2297 * @hash: cipher handle
2298 * @key: buffer holding the key
2299 * @keylen: length of the key in bytes
2301 * The caller provided key is set for the message digest cipher. The cipher
2302 * handle must point to a keyed hash in order for this function to succeed.
2304 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
2306 static inline int crypto_hash_setkey(struct crypto_hash *hash,
2307 const u8 *key, unsigned int keylen)
2309 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
2312 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
2314 return (struct crypto_comp *)tfm;
2317 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
2319 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
2320 CRYPTO_ALG_TYPE_MASK);
2321 return __crypto_comp_cast(tfm);
2324 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
2327 type &= ~CRYPTO_ALG_TYPE_MASK;
2328 type |= CRYPTO_ALG_TYPE_COMPRESS;
2329 mask |= CRYPTO_ALG_TYPE_MASK;
2331 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
2334 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
2339 static inline void crypto_free_comp(struct crypto_comp *tfm)
2341 crypto_free_tfm(crypto_comp_tfm(tfm));
2344 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
2346 type &= ~CRYPTO_ALG_TYPE_MASK;
2347 type |= CRYPTO_ALG_TYPE_COMPRESS;
2348 mask |= CRYPTO_ALG_TYPE_MASK;
2350 return crypto_has_alg(alg_name, type, mask);
2353 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
2355 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
2358 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
2360 return &crypto_comp_tfm(tfm)->crt_compress;
2363 static inline int crypto_comp_compress(struct crypto_comp *tfm,
2364 const u8 *src, unsigned int slen,
2365 u8 *dst, unsigned int *dlen)
2367 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
2368 src, slen, dst, dlen);
2371 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
2372 const u8 *src, unsigned int slen,
2373 u8 *dst, unsigned int *dlen)
2375 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
2376 src, slen, dst, dlen);
2379 #endif /* _LINUX_CRYPTO_H */