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 <asm/atomic.h>
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/list.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 * Transform masks and values (for crt_flags).
77 #define CRYPTO_TFM_REQ_MASK 0x000fff00
78 #define CRYPTO_TFM_RES_MASK 0xfff00000
80 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
81 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
82 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
83 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
84 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
85 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
86 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
87 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
90 * Miscellaneous stuff.
92 #define CRYPTO_MAX_ALG_NAME 64
95 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
96 * declaration) is used to ensure that the crypto_tfm context structure is
97 * aligned correctly for the given architecture so that there are no alignment
98 * faults for C data types. In particular, this is required on platforms such
99 * as arm where pointers are 32-bit aligned but there are data types such as
100 * u64 which require 64-bit alignment.
102 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
104 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
107 struct crypto_ablkcipher;
108 struct crypto_async_request;
110 struct crypto_blkcipher;
115 struct aead_givcrypt_request;
116 struct skcipher_givcrypt_request;
118 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
120 struct crypto_async_request {
121 struct list_head list;
122 crypto_completion_t complete;
124 struct crypto_tfm *tfm;
129 struct ablkcipher_request {
130 struct crypto_async_request base;
136 struct scatterlist *src;
137 struct scatterlist *dst;
139 void *__ctx[] CRYPTO_MINALIGN_ATTR;
143 * struct aead_request - AEAD request
144 * @base: Common attributes for async crypto requests
145 * @assoclen: Length in bytes of associated data for authentication
146 * @cryptlen: Length of data to be encrypted or decrypted
147 * @iv: Initialisation vector
148 * @assoc: Associated data
150 * @dst: Destination data
151 * @__ctx: Start of private context data
153 struct aead_request {
154 struct crypto_async_request base;
156 unsigned int assoclen;
157 unsigned int cryptlen;
161 struct scatterlist *assoc;
162 struct scatterlist *src;
163 struct scatterlist *dst;
165 void *__ctx[] CRYPTO_MINALIGN_ATTR;
168 struct blkcipher_desc {
169 struct crypto_blkcipher *tfm;
175 struct crypto_tfm *tfm;
176 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
177 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
178 const u8 *src, unsigned int nbytes);
183 struct crypto_hash *tfm;
188 * Algorithms: modular crypto algorithm implementations, managed
189 * via crypto_register_alg() and crypto_unregister_alg().
191 struct ablkcipher_alg {
192 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
193 unsigned int keylen);
194 int (*encrypt)(struct ablkcipher_request *req);
195 int (*decrypt)(struct ablkcipher_request *req);
196 int (*givencrypt)(struct skcipher_givcrypt_request *req);
197 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
201 unsigned int min_keysize;
202 unsigned int max_keysize;
207 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
208 unsigned int keylen);
209 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
210 int (*encrypt)(struct aead_request *req);
211 int (*decrypt)(struct aead_request *req);
212 int (*givencrypt)(struct aead_givcrypt_request *req);
213 int (*givdecrypt)(struct aead_givcrypt_request *req);
218 unsigned int maxauthsize;
221 struct blkcipher_alg {
222 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
223 unsigned int keylen);
224 int (*encrypt)(struct blkcipher_desc *desc,
225 struct scatterlist *dst, struct scatterlist *src,
226 unsigned int nbytes);
227 int (*decrypt)(struct blkcipher_desc *desc,
228 struct scatterlist *dst, struct scatterlist *src,
229 unsigned int nbytes);
233 unsigned int min_keysize;
234 unsigned int max_keysize;
239 unsigned int cia_min_keysize;
240 unsigned int cia_max_keysize;
241 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
242 unsigned int keylen);
243 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
244 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
247 struct compress_alg {
248 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
249 unsigned int slen, u8 *dst, unsigned int *dlen);
250 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
251 unsigned int slen, u8 *dst, unsigned int *dlen);
255 int (*rng_make_random)(struct crypto_rng *tfm, u8 *rdata,
257 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
259 unsigned int seedsize;
263 #define cra_ablkcipher cra_u.ablkcipher
264 #define cra_aead cra_u.aead
265 #define cra_blkcipher cra_u.blkcipher
266 #define cra_cipher cra_u.cipher
267 #define cra_compress cra_u.compress
268 #define cra_rng cra_u.rng
271 struct list_head cra_list;
272 struct list_head cra_users;
275 unsigned int cra_blocksize;
276 unsigned int cra_ctxsize;
277 unsigned int cra_alignmask;
282 char cra_name[CRYPTO_MAX_ALG_NAME];
283 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
285 const struct crypto_type *cra_type;
288 struct ablkcipher_alg ablkcipher;
289 struct aead_alg aead;
290 struct blkcipher_alg blkcipher;
291 struct cipher_alg cipher;
292 struct compress_alg compress;
296 int (*cra_init)(struct crypto_tfm *tfm);
297 void (*cra_exit)(struct crypto_tfm *tfm);
298 void (*cra_destroy)(struct crypto_alg *alg);
300 struct module *cra_module;
304 * Algorithm registration interface.
306 int crypto_register_alg(struct crypto_alg *alg);
307 int crypto_unregister_alg(struct crypto_alg *alg);
310 * Algorithm query interface.
312 int crypto_has_alg(const char *name, u32 type, u32 mask);
315 * Transforms: user-instantiated objects which encapsulate algorithms
316 * and core processing logic. Managed via crypto_alloc_*() and
317 * crypto_free_*(), as well as the various helpers below.
320 struct ablkcipher_tfm {
321 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
322 unsigned int keylen);
323 int (*encrypt)(struct ablkcipher_request *req);
324 int (*decrypt)(struct ablkcipher_request *req);
325 int (*givencrypt)(struct skcipher_givcrypt_request *req);
326 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
328 struct crypto_ablkcipher *base;
331 unsigned int reqsize;
335 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
336 unsigned int keylen);
337 int (*encrypt)(struct aead_request *req);
338 int (*decrypt)(struct aead_request *req);
339 int (*givencrypt)(struct aead_givcrypt_request *req);
340 int (*givdecrypt)(struct aead_givcrypt_request *req);
342 struct crypto_aead *base;
345 unsigned int authsize;
346 unsigned int reqsize;
349 struct blkcipher_tfm {
351 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
352 unsigned int keylen);
353 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
354 struct scatterlist *src, unsigned int nbytes);
355 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
356 struct scatterlist *src, unsigned int nbytes);
360 int (*cit_setkey)(struct crypto_tfm *tfm,
361 const u8 *key, unsigned int keylen);
362 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
363 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
367 int (*init)(struct hash_desc *desc);
368 int (*update)(struct hash_desc *desc,
369 struct scatterlist *sg, unsigned int nsg);
370 int (*final)(struct hash_desc *desc, u8 *out);
371 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
372 unsigned int nsg, u8 *out);
373 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
374 unsigned int keylen);
375 unsigned int digestsize;
378 struct compress_tfm {
379 int (*cot_compress)(struct crypto_tfm *tfm,
380 const u8 *src, unsigned int slen,
381 u8 *dst, unsigned int *dlen);
382 int (*cot_decompress)(struct crypto_tfm *tfm,
383 const u8 *src, unsigned int slen,
384 u8 *dst, unsigned int *dlen);
388 int (*rng_gen_random)(struct crypto_rng *tfm, u8 *rdata,
390 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
393 #define crt_ablkcipher crt_u.ablkcipher
394 #define crt_aead crt_u.aead
395 #define crt_blkcipher crt_u.blkcipher
396 #define crt_cipher crt_u.cipher
397 #define crt_hash crt_u.hash
398 #define crt_compress crt_u.compress
399 #define crt_rng crt_u.rng
406 struct ablkcipher_tfm ablkcipher;
407 struct aead_tfm aead;
408 struct blkcipher_tfm blkcipher;
409 struct cipher_tfm cipher;
410 struct hash_tfm hash;
411 struct compress_tfm compress;
415 void (*exit)(struct crypto_tfm *tfm);
417 struct crypto_alg *__crt_alg;
419 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
422 struct crypto_ablkcipher {
423 struct crypto_tfm base;
427 struct crypto_tfm base;
430 struct crypto_blkcipher {
431 struct crypto_tfm base;
434 struct crypto_cipher {
435 struct crypto_tfm base;
439 struct crypto_tfm base;
443 struct crypto_tfm base;
447 struct crypto_tfm base;
458 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
460 /* Maximum number of (rtattr) parameters for each template. */
461 #define CRYPTO_MAX_ATTRS 32
463 struct crypto_attr_alg {
464 char name[CRYPTO_MAX_ALG_NAME];
467 struct crypto_attr_type {
472 struct crypto_attr_u32 {
477 * Transform user interface.
480 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
481 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
483 static inline void crypto_free_tfm(struct crypto_tfm *tfm)
485 return crypto_destroy_tfm(tfm, tfm);
488 int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
491 * Transform helpers which query the underlying algorithm.
493 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
495 return tfm->__crt_alg->cra_name;
498 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
500 return tfm->__crt_alg->cra_driver_name;
503 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
505 return tfm->__crt_alg->cra_priority;
508 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
510 return module_name(tfm->__crt_alg->cra_module);
513 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
515 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
518 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
520 return tfm->__crt_alg->cra_blocksize;
523 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
525 return tfm->__crt_alg->cra_alignmask;
528 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
530 return tfm->crt_flags;
533 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
535 tfm->crt_flags |= flags;
538 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
540 tfm->crt_flags &= ~flags;
543 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
545 return tfm->__crt_ctx;
548 static inline unsigned int crypto_tfm_ctx_alignment(void)
550 struct crypto_tfm *tfm;
551 return __alignof__(tfm->__crt_ctx);
557 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
558 struct crypto_tfm *tfm)
560 return (struct crypto_ablkcipher *)tfm;
563 static inline u32 crypto_skcipher_type(u32 type)
565 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
566 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
570 static inline u32 crypto_skcipher_mask(u32 mask)
572 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
573 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
577 struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
580 static inline struct crypto_tfm *crypto_ablkcipher_tfm(
581 struct crypto_ablkcipher *tfm)
586 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
588 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
591 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
594 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
595 crypto_skcipher_mask(mask));
598 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
599 struct crypto_ablkcipher *tfm)
601 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
604 static inline unsigned int crypto_ablkcipher_ivsize(
605 struct crypto_ablkcipher *tfm)
607 return crypto_ablkcipher_crt(tfm)->ivsize;
610 static inline unsigned int crypto_ablkcipher_blocksize(
611 struct crypto_ablkcipher *tfm)
613 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
616 static inline unsigned int crypto_ablkcipher_alignmask(
617 struct crypto_ablkcipher *tfm)
619 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
622 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
624 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
627 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
630 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
633 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
636 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
639 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
640 const u8 *key, unsigned int keylen)
642 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
644 return crt->setkey(crt->base, key, keylen);
647 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
648 struct ablkcipher_request *req)
650 return __crypto_ablkcipher_cast(req->base.tfm);
653 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
655 struct ablkcipher_tfm *crt =
656 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
657 return crt->encrypt(req);
660 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
662 struct ablkcipher_tfm *crt =
663 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
664 return crt->decrypt(req);
667 static inline unsigned int crypto_ablkcipher_reqsize(
668 struct crypto_ablkcipher *tfm)
670 return crypto_ablkcipher_crt(tfm)->reqsize;
673 static inline void ablkcipher_request_set_tfm(
674 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
676 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
679 static inline struct ablkcipher_request *ablkcipher_request_cast(
680 struct crypto_async_request *req)
682 return container_of(req, struct ablkcipher_request, base);
685 static inline struct ablkcipher_request *ablkcipher_request_alloc(
686 struct crypto_ablkcipher *tfm, gfp_t gfp)
688 struct ablkcipher_request *req;
690 req = kmalloc(sizeof(struct ablkcipher_request) +
691 crypto_ablkcipher_reqsize(tfm), gfp);
694 ablkcipher_request_set_tfm(req, tfm);
699 static inline void ablkcipher_request_free(struct ablkcipher_request *req)
704 static inline void ablkcipher_request_set_callback(
705 struct ablkcipher_request *req,
706 u32 flags, crypto_completion_t complete, void *data)
708 req->base.complete = complete;
709 req->base.data = data;
710 req->base.flags = flags;
713 static inline void ablkcipher_request_set_crypt(
714 struct ablkcipher_request *req,
715 struct scatterlist *src, struct scatterlist *dst,
716 unsigned int nbytes, void *iv)
720 req->nbytes = nbytes;
724 static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
726 return (struct crypto_aead *)tfm;
729 struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
731 static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
736 static inline void crypto_free_aead(struct crypto_aead *tfm)
738 crypto_free_tfm(crypto_aead_tfm(tfm));
741 static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
743 return &crypto_aead_tfm(tfm)->crt_aead;
746 static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
748 return crypto_aead_crt(tfm)->ivsize;
751 static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
753 return crypto_aead_crt(tfm)->authsize;
756 static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
758 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
761 static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
763 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
766 static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
768 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
771 static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
773 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
776 static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
778 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
781 static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
784 struct aead_tfm *crt = crypto_aead_crt(tfm);
786 return crt->setkey(crt->base, key, keylen);
789 int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
791 static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
793 return __crypto_aead_cast(req->base.tfm);
796 static inline int crypto_aead_encrypt(struct aead_request *req)
798 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
801 static inline int crypto_aead_decrypt(struct aead_request *req)
803 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
806 static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
808 return crypto_aead_crt(tfm)->reqsize;
811 static inline void aead_request_set_tfm(struct aead_request *req,
812 struct crypto_aead *tfm)
814 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
817 static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
820 struct aead_request *req;
822 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
825 aead_request_set_tfm(req, tfm);
830 static inline void aead_request_free(struct aead_request *req)
835 static inline void aead_request_set_callback(struct aead_request *req,
837 crypto_completion_t complete,
840 req->base.complete = complete;
841 req->base.data = data;
842 req->base.flags = flags;
845 static inline void aead_request_set_crypt(struct aead_request *req,
846 struct scatterlist *src,
847 struct scatterlist *dst,
848 unsigned int cryptlen, u8 *iv)
852 req->cryptlen = cryptlen;
856 static inline void aead_request_set_assoc(struct aead_request *req,
857 struct scatterlist *assoc,
858 unsigned int assoclen)
861 req->assoclen = assoclen;
864 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
865 struct crypto_tfm *tfm)
867 return (struct crypto_blkcipher *)tfm;
870 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
871 struct crypto_tfm *tfm)
873 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
874 return __crypto_blkcipher_cast(tfm);
877 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
878 const char *alg_name, u32 type, u32 mask)
880 type &= ~CRYPTO_ALG_TYPE_MASK;
881 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
882 mask |= CRYPTO_ALG_TYPE_MASK;
884 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
887 static inline struct crypto_tfm *crypto_blkcipher_tfm(
888 struct crypto_blkcipher *tfm)
893 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
895 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
898 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
900 type &= ~CRYPTO_ALG_TYPE_MASK;
901 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
902 mask |= CRYPTO_ALG_TYPE_MASK;
904 return crypto_has_alg(alg_name, type, mask);
907 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
909 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
912 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
913 struct crypto_blkcipher *tfm)
915 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
918 static inline struct blkcipher_alg *crypto_blkcipher_alg(
919 struct crypto_blkcipher *tfm)
921 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
924 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
926 return crypto_blkcipher_alg(tfm)->ivsize;
929 static inline unsigned int crypto_blkcipher_blocksize(
930 struct crypto_blkcipher *tfm)
932 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
935 static inline unsigned int crypto_blkcipher_alignmask(
936 struct crypto_blkcipher *tfm)
938 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
941 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
943 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
946 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
949 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
952 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
955 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
958 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
959 const u8 *key, unsigned int keylen)
961 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
965 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
966 struct scatterlist *dst,
967 struct scatterlist *src,
970 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
971 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
974 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
975 struct scatterlist *dst,
976 struct scatterlist *src,
979 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
982 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
983 struct scatterlist *dst,
984 struct scatterlist *src,
987 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
988 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
991 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
992 struct scatterlist *dst,
993 struct scatterlist *src,
996 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
999 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1000 const u8 *src, unsigned int len)
1002 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1005 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1006 u8 *dst, unsigned int len)
1008 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1011 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1013 return (struct crypto_cipher *)tfm;
1016 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1018 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1019 return __crypto_cipher_cast(tfm);
1022 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1025 type &= ~CRYPTO_ALG_TYPE_MASK;
1026 type |= CRYPTO_ALG_TYPE_CIPHER;
1027 mask |= CRYPTO_ALG_TYPE_MASK;
1029 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1032 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1037 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1039 crypto_free_tfm(crypto_cipher_tfm(tfm));
1042 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1044 type &= ~CRYPTO_ALG_TYPE_MASK;
1045 type |= CRYPTO_ALG_TYPE_CIPHER;
1046 mask |= CRYPTO_ALG_TYPE_MASK;
1048 return crypto_has_alg(alg_name, type, mask);
1051 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1053 return &crypto_cipher_tfm(tfm)->crt_cipher;
1056 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1058 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1061 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1063 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1066 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1068 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1071 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1074 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1077 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1080 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1083 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1084 const u8 *key, unsigned int keylen)
1086 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1090 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1091 u8 *dst, const u8 *src)
1093 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1097 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1098 u8 *dst, const u8 *src)
1100 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1104 static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
1106 return (struct crypto_hash *)tfm;
1109 static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
1111 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1112 CRYPTO_ALG_TYPE_HASH_MASK);
1113 return __crypto_hash_cast(tfm);
1116 static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1119 type &= ~CRYPTO_ALG_TYPE_MASK;
1120 mask &= ~CRYPTO_ALG_TYPE_MASK;
1121 type |= CRYPTO_ALG_TYPE_HASH;
1122 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1124 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1127 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1132 static inline void crypto_free_hash(struct crypto_hash *tfm)
1134 crypto_free_tfm(crypto_hash_tfm(tfm));
1137 static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1139 type &= ~CRYPTO_ALG_TYPE_MASK;
1140 mask &= ~CRYPTO_ALG_TYPE_MASK;
1141 type |= CRYPTO_ALG_TYPE_HASH;
1142 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1144 return crypto_has_alg(alg_name, type, mask);
1147 static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1149 return &crypto_hash_tfm(tfm)->crt_hash;
1152 static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1154 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1157 static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1159 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1162 static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1164 return crypto_hash_crt(tfm)->digestsize;
1167 static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1169 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1172 static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1174 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1177 static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1179 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1182 static inline int crypto_hash_init(struct hash_desc *desc)
1184 return crypto_hash_crt(desc->tfm)->init(desc);
1187 static inline int crypto_hash_update(struct hash_desc *desc,
1188 struct scatterlist *sg,
1189 unsigned int nbytes)
1191 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1194 static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1196 return crypto_hash_crt(desc->tfm)->final(desc, out);
1199 static inline int crypto_hash_digest(struct hash_desc *desc,
1200 struct scatterlist *sg,
1201 unsigned int nbytes, u8 *out)
1203 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1206 static inline int crypto_hash_setkey(struct crypto_hash *hash,
1207 const u8 *key, unsigned int keylen)
1209 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
1212 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1214 return (struct crypto_comp *)tfm;
1217 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1219 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1220 CRYPTO_ALG_TYPE_MASK);
1221 return __crypto_comp_cast(tfm);
1224 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1227 type &= ~CRYPTO_ALG_TYPE_MASK;
1228 type |= CRYPTO_ALG_TYPE_COMPRESS;
1229 mask |= CRYPTO_ALG_TYPE_MASK;
1231 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1234 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1239 static inline void crypto_free_comp(struct crypto_comp *tfm)
1241 crypto_free_tfm(crypto_comp_tfm(tfm));
1244 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1246 type &= ~CRYPTO_ALG_TYPE_MASK;
1247 type |= CRYPTO_ALG_TYPE_COMPRESS;
1248 mask |= CRYPTO_ALG_TYPE_MASK;
1250 return crypto_has_alg(alg_name, type, mask);
1253 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1255 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1258 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1260 return &crypto_comp_tfm(tfm)->crt_compress;
1263 static inline int crypto_comp_compress(struct crypto_comp *tfm,
1264 const u8 *src, unsigned int slen,
1265 u8 *dst, unsigned int *dlen)
1267 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1268 src, slen, dst, dlen);
1271 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
1272 const u8 *src, unsigned int slen,
1273 u8 *dst, unsigned int *dlen)
1275 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1276 src, slen, dst, dlen);
1279 #endif /* _LINUX_CRYPTO_H */