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 0x00000009
40 #define CRYPTO_ALG_TYPE_AHASH 0x0000000a
42 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
43 #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
44 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
46 #define CRYPTO_ALG_LARVAL 0x00000010
47 #define CRYPTO_ALG_DEAD 0x00000020
48 #define CRYPTO_ALG_DYING 0x00000040
49 #define CRYPTO_ALG_ASYNC 0x00000080
52 * Set this bit if and only if the algorithm requires another algorithm of
53 * the same type to handle corner cases.
55 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
58 * This bit is set for symmetric key ciphers that have already been wrapped
59 * with a generic IV generator to prevent them from being wrapped again.
61 #define CRYPTO_ALG_GENIV 0x00000200
64 * Transform masks and values (for crt_flags).
66 #define CRYPTO_TFM_REQ_MASK 0x000fff00
67 #define CRYPTO_TFM_RES_MASK 0xfff00000
69 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
70 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
71 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
72 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
73 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
74 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
75 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
76 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
79 * Miscellaneous stuff.
81 #define CRYPTO_MAX_ALG_NAME 64
84 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
85 * declaration) is used to ensure that the crypto_tfm context structure is
86 * aligned correctly for the given architecture so that there are no alignment
87 * faults for C data types. In particular, this is required on platforms such
88 * as arm where pointers are 32-bit aligned but there are data types such as
89 * u64 which require 64-bit alignment.
91 #if defined(ARCH_KMALLOC_MINALIGN)
92 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
93 #elif defined(ARCH_SLAB_MINALIGN)
94 #define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
96 #define CRYPTO_MINALIGN __alignof__(unsigned long long)
99 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
102 struct crypto_ablkcipher;
103 struct crypto_async_request;
105 struct crypto_blkcipher;
110 struct aead_givcrypt_request;
111 struct skcipher_givcrypt_request;
113 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
115 struct crypto_async_request {
116 struct list_head list;
117 crypto_completion_t complete;
119 struct crypto_tfm *tfm;
124 struct ablkcipher_request {
125 struct crypto_async_request base;
131 struct scatterlist *src;
132 struct scatterlist *dst;
134 void *__ctx[] CRYPTO_MINALIGN_ATTR;
137 struct ahash_request {
138 struct crypto_async_request base;
143 struct scatterlist *src;
146 void *__ctx[] CRYPTO_MINALIGN_ATTR;
150 * struct aead_request - AEAD request
151 * @base: Common attributes for async crypto requests
152 * @assoclen: Length in bytes of associated data for authentication
153 * @cryptlen: Length of data to be encrypted or decrypted
154 * @iv: Initialisation vector
155 * @assoc: Associated data
157 * @dst: Destination data
158 * @__ctx: Start of private context data
160 struct aead_request {
161 struct crypto_async_request base;
163 unsigned int assoclen;
164 unsigned int cryptlen;
168 struct scatterlist *assoc;
169 struct scatterlist *src;
170 struct scatterlist *dst;
172 void *__ctx[] CRYPTO_MINALIGN_ATTR;
175 struct blkcipher_desc {
176 struct crypto_blkcipher *tfm;
182 struct crypto_tfm *tfm;
183 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
184 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
185 const u8 *src, unsigned int nbytes);
190 struct crypto_hash *tfm;
195 * Algorithms: modular crypto algorithm implementations, managed
196 * via crypto_register_alg() and crypto_unregister_alg().
198 struct ablkcipher_alg {
199 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
200 unsigned int keylen);
201 int (*encrypt)(struct ablkcipher_request *req);
202 int (*decrypt)(struct ablkcipher_request *req);
203 int (*givencrypt)(struct skcipher_givcrypt_request *req);
204 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
208 unsigned int min_keysize;
209 unsigned int max_keysize;
214 int (*init)(struct ahash_request *req);
215 int (*update)(struct ahash_request *req);
216 int (*final)(struct ahash_request *req);
217 int (*digest)(struct ahash_request *req);
218 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
219 unsigned int keylen);
221 unsigned int digestsize;
225 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
226 unsigned int keylen);
227 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
228 int (*encrypt)(struct aead_request *req);
229 int (*decrypt)(struct aead_request *req);
230 int (*givencrypt)(struct aead_givcrypt_request *req);
231 int (*givdecrypt)(struct aead_givcrypt_request *req);
236 unsigned int maxauthsize;
239 struct blkcipher_alg {
240 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
241 unsigned int keylen);
242 int (*encrypt)(struct blkcipher_desc *desc,
243 struct scatterlist *dst, struct scatterlist *src,
244 unsigned int nbytes);
245 int (*decrypt)(struct blkcipher_desc *desc,
246 struct scatterlist *dst, struct scatterlist *src,
247 unsigned int nbytes);
251 unsigned int min_keysize;
252 unsigned int max_keysize;
257 unsigned int cia_min_keysize;
258 unsigned int cia_max_keysize;
259 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
260 unsigned int keylen);
261 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
262 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
266 unsigned int dia_digestsize;
267 void (*dia_init)(struct crypto_tfm *tfm);
268 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
270 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
271 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
272 unsigned int keylen);
276 int (*init)(struct hash_desc *desc);
277 int (*update)(struct hash_desc *desc, struct scatterlist *sg,
278 unsigned int nbytes);
279 int (*final)(struct hash_desc *desc, u8 *out);
280 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
281 unsigned int nbytes, u8 *out);
282 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
283 unsigned int keylen);
285 unsigned int digestsize;
288 struct compress_alg {
289 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
290 unsigned int slen, u8 *dst, unsigned int *dlen);
291 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
292 unsigned int slen, u8 *dst, unsigned int *dlen);
295 #define cra_ablkcipher cra_u.ablkcipher
296 #define cra_aead cra_u.aead
297 #define cra_blkcipher cra_u.blkcipher
298 #define cra_cipher cra_u.cipher
299 #define cra_digest cra_u.digest
300 #define cra_hash cra_u.hash
301 #define cra_ahash cra_u.ahash
302 #define cra_compress cra_u.compress
305 struct list_head cra_list;
306 struct list_head cra_users;
309 unsigned int cra_blocksize;
310 unsigned int cra_ctxsize;
311 unsigned int cra_alignmask;
316 char cra_name[CRYPTO_MAX_ALG_NAME];
317 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
319 const struct crypto_type *cra_type;
322 struct ablkcipher_alg ablkcipher;
323 struct aead_alg aead;
324 struct blkcipher_alg blkcipher;
325 struct cipher_alg cipher;
326 struct digest_alg digest;
327 struct hash_alg hash;
328 struct ahash_alg ahash;
329 struct compress_alg compress;
332 int (*cra_init)(struct crypto_tfm *tfm);
333 void (*cra_exit)(struct crypto_tfm *tfm);
334 void (*cra_destroy)(struct crypto_alg *alg);
336 struct module *cra_module;
340 * Algorithm registration interface.
342 int crypto_register_alg(struct crypto_alg *alg);
343 int crypto_unregister_alg(struct crypto_alg *alg);
346 * Algorithm query interface.
348 int crypto_has_alg(const char *name, u32 type, u32 mask);
351 * Transforms: user-instantiated objects which encapsulate algorithms
352 * and core processing logic. Managed via crypto_alloc_*() and
353 * crypto_free_*(), as well as the various helpers below.
356 struct ablkcipher_tfm {
357 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
358 unsigned int keylen);
359 int (*encrypt)(struct ablkcipher_request *req);
360 int (*decrypt)(struct ablkcipher_request *req);
361 int (*givencrypt)(struct skcipher_givcrypt_request *req);
362 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
364 struct crypto_ablkcipher *base;
367 unsigned int reqsize;
371 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
372 unsigned int keylen);
373 int (*encrypt)(struct aead_request *req);
374 int (*decrypt)(struct aead_request *req);
375 int (*givencrypt)(struct aead_givcrypt_request *req);
376 int (*givdecrypt)(struct aead_givcrypt_request *req);
378 struct crypto_aead *base;
381 unsigned int authsize;
382 unsigned int reqsize;
385 struct blkcipher_tfm {
387 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
388 unsigned int keylen);
389 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
390 struct scatterlist *src, unsigned int nbytes);
391 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
392 struct scatterlist *src, unsigned int nbytes);
396 int (*cit_setkey)(struct crypto_tfm *tfm,
397 const u8 *key, unsigned int keylen);
398 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
399 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
403 int (*init)(struct hash_desc *desc);
404 int (*update)(struct hash_desc *desc,
405 struct scatterlist *sg, unsigned int nsg);
406 int (*final)(struct hash_desc *desc, u8 *out);
407 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
408 unsigned int nsg, u8 *out);
409 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
410 unsigned int keylen);
411 unsigned int digestsize;
415 int (*init)(struct ahash_request *req);
416 int (*update)(struct ahash_request *req);
417 int (*final)(struct ahash_request *req);
418 int (*digest)(struct ahash_request *req);
419 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
420 unsigned int keylen);
422 unsigned int digestsize;
423 struct crypto_ahash *base;
424 unsigned int reqsize;
427 struct compress_tfm {
428 int (*cot_compress)(struct crypto_tfm *tfm,
429 const u8 *src, unsigned int slen,
430 u8 *dst, unsigned int *dlen);
431 int (*cot_decompress)(struct crypto_tfm *tfm,
432 const u8 *src, unsigned int slen,
433 u8 *dst, unsigned int *dlen);
436 #define crt_ablkcipher crt_u.ablkcipher
437 #define crt_aead crt_u.aead
438 #define crt_blkcipher crt_u.blkcipher
439 #define crt_cipher crt_u.cipher
440 #define crt_hash crt_u.hash
441 #define crt_ahash crt_u.ahash
442 #define crt_compress crt_u.compress
449 struct ablkcipher_tfm ablkcipher;
450 struct aead_tfm aead;
451 struct blkcipher_tfm blkcipher;
452 struct cipher_tfm cipher;
453 struct hash_tfm hash;
454 struct ahash_tfm ahash;
455 struct compress_tfm compress;
458 struct crypto_alg *__crt_alg;
460 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
463 struct crypto_ablkcipher {
464 struct crypto_tfm base;
468 struct crypto_tfm base;
471 struct crypto_blkcipher {
472 struct crypto_tfm base;
475 struct crypto_cipher {
476 struct crypto_tfm base;
480 struct crypto_tfm base;
484 struct crypto_tfm base;
487 struct crypto_ahash {
488 struct crypto_tfm base;
499 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
501 /* Maximum number of (rtattr) parameters for each template. */
502 #define CRYPTO_MAX_ATTRS 32
504 struct crypto_attr_alg {
505 char name[CRYPTO_MAX_ALG_NAME];
508 struct crypto_attr_type {
513 struct crypto_attr_u32 {
518 * Transform user interface.
521 struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
522 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
523 void crypto_free_tfm(struct crypto_tfm *tfm);
526 * Transform helpers which query the underlying algorithm.
528 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
530 return tfm->__crt_alg->cra_name;
533 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
535 return tfm->__crt_alg->cra_driver_name;
538 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
540 return tfm->__crt_alg->cra_priority;
543 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
545 return module_name(tfm->__crt_alg->cra_module);
548 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
550 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
553 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
555 return tfm->__crt_alg->cra_blocksize;
558 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
560 return tfm->__crt_alg->cra_alignmask;
563 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
565 return tfm->crt_flags;
568 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
570 tfm->crt_flags |= flags;
573 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
575 tfm->crt_flags &= ~flags;
578 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
580 return tfm->__crt_ctx;
583 static inline unsigned int crypto_tfm_ctx_alignment(void)
585 struct crypto_tfm *tfm;
586 return __alignof__(tfm->__crt_ctx);
592 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
593 struct crypto_tfm *tfm)
595 return (struct crypto_ablkcipher *)tfm;
598 static inline u32 crypto_skcipher_type(u32 type)
600 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
601 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
605 static inline u32 crypto_skcipher_mask(u32 mask)
607 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
608 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
612 struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
615 static inline struct crypto_tfm *crypto_ablkcipher_tfm(
616 struct crypto_ablkcipher *tfm)
621 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
623 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
626 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
629 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
630 crypto_skcipher_mask(mask));
633 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
634 struct crypto_ablkcipher *tfm)
636 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
639 static inline unsigned int crypto_ablkcipher_ivsize(
640 struct crypto_ablkcipher *tfm)
642 return crypto_ablkcipher_crt(tfm)->ivsize;
645 static inline unsigned int crypto_ablkcipher_blocksize(
646 struct crypto_ablkcipher *tfm)
648 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
651 static inline unsigned int crypto_ablkcipher_alignmask(
652 struct crypto_ablkcipher *tfm)
654 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
657 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
659 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
662 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
665 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
668 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
671 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
674 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
675 const u8 *key, unsigned int keylen)
677 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
679 return crt->setkey(crt->base, key, keylen);
682 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
683 struct ablkcipher_request *req)
685 return __crypto_ablkcipher_cast(req->base.tfm);
688 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
690 struct ablkcipher_tfm *crt =
691 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
692 return crt->encrypt(req);
695 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
697 struct ablkcipher_tfm *crt =
698 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
699 return crt->decrypt(req);
702 static inline unsigned int crypto_ablkcipher_reqsize(
703 struct crypto_ablkcipher *tfm)
705 return crypto_ablkcipher_crt(tfm)->reqsize;
708 static inline void ablkcipher_request_set_tfm(
709 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
711 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
714 static inline struct ablkcipher_request *ablkcipher_request_cast(
715 struct crypto_async_request *req)
717 return container_of(req, struct ablkcipher_request, base);
720 static inline struct ablkcipher_request *ablkcipher_request_alloc(
721 struct crypto_ablkcipher *tfm, gfp_t gfp)
723 struct ablkcipher_request *req;
725 req = kmalloc(sizeof(struct ablkcipher_request) +
726 crypto_ablkcipher_reqsize(tfm), gfp);
729 ablkcipher_request_set_tfm(req, tfm);
734 static inline void ablkcipher_request_free(struct ablkcipher_request *req)
739 static inline void ablkcipher_request_set_callback(
740 struct ablkcipher_request *req,
741 u32 flags, crypto_completion_t complete, void *data)
743 req->base.complete = complete;
744 req->base.data = data;
745 req->base.flags = flags;
748 static inline void ablkcipher_request_set_crypt(
749 struct ablkcipher_request *req,
750 struct scatterlist *src, struct scatterlist *dst,
751 unsigned int nbytes, void *iv)
755 req->nbytes = nbytes;
759 static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
761 return (struct crypto_aead *)tfm;
764 struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
766 static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
771 static inline void crypto_free_aead(struct crypto_aead *tfm)
773 crypto_free_tfm(crypto_aead_tfm(tfm));
776 static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
778 return &crypto_aead_tfm(tfm)->crt_aead;
781 static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
783 return crypto_aead_crt(tfm)->ivsize;
786 static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
788 return crypto_aead_crt(tfm)->authsize;
791 static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
793 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
796 static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
798 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
801 static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
803 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
806 static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
808 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
811 static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
813 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
816 static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
819 struct aead_tfm *crt = crypto_aead_crt(tfm);
821 return crt->setkey(crt->base, key, keylen);
824 int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
826 static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
828 return __crypto_aead_cast(req->base.tfm);
831 static inline int crypto_aead_encrypt(struct aead_request *req)
833 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
836 static inline int crypto_aead_decrypt(struct aead_request *req)
838 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
841 static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
843 return crypto_aead_crt(tfm)->reqsize;
846 static inline void aead_request_set_tfm(struct aead_request *req,
847 struct crypto_aead *tfm)
849 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
852 static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
855 struct aead_request *req;
857 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
860 aead_request_set_tfm(req, tfm);
865 static inline void aead_request_free(struct aead_request *req)
870 static inline void aead_request_set_callback(struct aead_request *req,
872 crypto_completion_t complete,
875 req->base.complete = complete;
876 req->base.data = data;
877 req->base.flags = flags;
880 static inline void aead_request_set_crypt(struct aead_request *req,
881 struct scatterlist *src,
882 struct scatterlist *dst,
883 unsigned int cryptlen, u8 *iv)
887 req->cryptlen = cryptlen;
891 static inline void aead_request_set_assoc(struct aead_request *req,
892 struct scatterlist *assoc,
893 unsigned int assoclen)
896 req->assoclen = assoclen;
899 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
900 struct crypto_tfm *tfm)
902 return (struct crypto_blkcipher *)tfm;
905 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
906 struct crypto_tfm *tfm)
908 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
909 return __crypto_blkcipher_cast(tfm);
912 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
913 const char *alg_name, u32 type, u32 mask)
915 type &= ~CRYPTO_ALG_TYPE_MASK;
916 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
917 mask |= CRYPTO_ALG_TYPE_MASK;
919 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
922 static inline struct crypto_tfm *crypto_blkcipher_tfm(
923 struct crypto_blkcipher *tfm)
928 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
930 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
933 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
935 type &= ~CRYPTO_ALG_TYPE_MASK;
936 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
937 mask |= CRYPTO_ALG_TYPE_MASK;
939 return crypto_has_alg(alg_name, type, mask);
942 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
944 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
947 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
948 struct crypto_blkcipher *tfm)
950 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
953 static inline struct blkcipher_alg *crypto_blkcipher_alg(
954 struct crypto_blkcipher *tfm)
956 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
959 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
961 return crypto_blkcipher_alg(tfm)->ivsize;
964 static inline unsigned int crypto_blkcipher_blocksize(
965 struct crypto_blkcipher *tfm)
967 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
970 static inline unsigned int crypto_blkcipher_alignmask(
971 struct crypto_blkcipher *tfm)
973 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
976 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
978 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
981 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
984 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
987 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
990 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
993 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
994 const u8 *key, unsigned int keylen)
996 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1000 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1001 struct scatterlist *dst,
1002 struct scatterlist *src,
1003 unsigned int nbytes)
1005 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1006 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1009 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1010 struct scatterlist *dst,
1011 struct scatterlist *src,
1012 unsigned int nbytes)
1014 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1017 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1018 struct scatterlist *dst,
1019 struct scatterlist *src,
1020 unsigned int nbytes)
1022 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1023 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1026 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1027 struct scatterlist *dst,
1028 struct scatterlist *src,
1029 unsigned int nbytes)
1031 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1034 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1035 const u8 *src, unsigned int len)
1037 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1040 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1041 u8 *dst, unsigned int len)
1043 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1046 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1048 return (struct crypto_cipher *)tfm;
1051 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1053 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1054 return __crypto_cipher_cast(tfm);
1057 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1060 type &= ~CRYPTO_ALG_TYPE_MASK;
1061 type |= CRYPTO_ALG_TYPE_CIPHER;
1062 mask |= CRYPTO_ALG_TYPE_MASK;
1064 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1067 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1072 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1074 crypto_free_tfm(crypto_cipher_tfm(tfm));
1077 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1079 type &= ~CRYPTO_ALG_TYPE_MASK;
1080 type |= CRYPTO_ALG_TYPE_CIPHER;
1081 mask |= CRYPTO_ALG_TYPE_MASK;
1083 return crypto_has_alg(alg_name, type, mask);
1086 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1088 return &crypto_cipher_tfm(tfm)->crt_cipher;
1091 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1093 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1096 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1098 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1101 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1103 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1106 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1109 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1112 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1115 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1118 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1119 const u8 *key, unsigned int keylen)
1121 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1125 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1126 u8 *dst, const u8 *src)
1128 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1132 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1133 u8 *dst, const u8 *src)
1135 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1139 static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
1141 return (struct crypto_hash *)tfm;
1144 static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
1146 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1147 CRYPTO_ALG_TYPE_HASH_MASK);
1148 return __crypto_hash_cast(tfm);
1151 static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1154 type &= ~CRYPTO_ALG_TYPE_MASK;
1155 mask &= ~CRYPTO_ALG_TYPE_MASK;
1156 type |= CRYPTO_ALG_TYPE_HASH;
1157 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1159 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1162 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1167 static inline void crypto_free_hash(struct crypto_hash *tfm)
1169 crypto_free_tfm(crypto_hash_tfm(tfm));
1172 static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1174 type &= ~CRYPTO_ALG_TYPE_MASK;
1175 mask &= ~CRYPTO_ALG_TYPE_MASK;
1176 type |= CRYPTO_ALG_TYPE_HASH;
1177 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1179 return crypto_has_alg(alg_name, type, mask);
1182 static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1184 return &crypto_hash_tfm(tfm)->crt_hash;
1187 static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1189 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1192 static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1194 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1197 static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1199 return crypto_hash_crt(tfm)->digestsize;
1202 static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1204 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1207 static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1209 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1212 static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1214 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1217 static inline int crypto_hash_init(struct hash_desc *desc)
1219 return crypto_hash_crt(desc->tfm)->init(desc);
1222 static inline int crypto_hash_update(struct hash_desc *desc,
1223 struct scatterlist *sg,
1224 unsigned int nbytes)
1226 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1229 static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1231 return crypto_hash_crt(desc->tfm)->final(desc, out);
1234 static inline int crypto_hash_digest(struct hash_desc *desc,
1235 struct scatterlist *sg,
1236 unsigned int nbytes, u8 *out)
1238 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1241 static inline int crypto_hash_setkey(struct crypto_hash *hash,
1242 const u8 *key, unsigned int keylen)
1244 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
1247 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1249 return (struct crypto_comp *)tfm;
1252 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1254 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1255 CRYPTO_ALG_TYPE_MASK);
1256 return __crypto_comp_cast(tfm);
1259 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1262 type &= ~CRYPTO_ALG_TYPE_MASK;
1263 type |= CRYPTO_ALG_TYPE_COMPRESS;
1264 mask |= CRYPTO_ALG_TYPE_MASK;
1266 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1269 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1274 static inline void crypto_free_comp(struct crypto_comp *tfm)
1276 crypto_free_tfm(crypto_comp_tfm(tfm));
1279 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1281 type &= ~CRYPTO_ALG_TYPE_MASK;
1282 type |= CRYPTO_ALG_TYPE_COMPRESS;
1283 mask |= CRYPTO_ALG_TYPE_MASK;
1285 return crypto_has_alg(alg_name, type, mask);
1288 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1290 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1293 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1295 return &crypto_comp_tfm(tfm)->crt_compress;
1298 static inline int crypto_comp_compress(struct crypto_comp *tfm,
1299 const u8 *src, unsigned int slen,
1300 u8 *dst, unsigned int *dlen)
1302 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1303 src, slen, dst, dlen);
1306 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
1307 const u8 *src, unsigned int slen,
1308 u8 *dst, unsigned int *dlen)
1310 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1311 src, slen, dst, dlen);
1314 static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)
1316 return (struct crypto_ahash *)tfm;
1319 static inline struct crypto_ahash *crypto_alloc_ahash(const char *alg_name,
1322 type &= ~CRYPTO_ALG_TYPE_MASK;
1323 mask &= ~CRYPTO_ALG_TYPE_MASK;
1324 type |= CRYPTO_ALG_TYPE_AHASH;
1325 mask |= CRYPTO_ALG_TYPE_AHASH_MASK;
1327 return __crypto_ahash_cast(crypto_alloc_base(alg_name, type, mask));
1330 static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
1335 static inline void crypto_free_ahash(struct crypto_ahash *tfm)
1337 crypto_free_tfm(crypto_ahash_tfm(tfm));
1340 static inline unsigned int crypto_ahash_alignmask(
1341 struct crypto_ahash *tfm)
1343 return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm));
1346 static inline struct ahash_tfm *crypto_ahash_crt(struct crypto_ahash *tfm)
1348 return &crypto_ahash_tfm(tfm)->crt_ahash;
1351 static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm)
1353 return crypto_ahash_crt(tfm)->digestsize;
1356 static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm)
1358 return crypto_tfm_get_flags(crypto_ahash_tfm(tfm));
1361 static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags)
1363 crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags);
1366 static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags)
1368 crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags);
1371 static inline struct crypto_ahash *crypto_ahash_reqtfm(
1372 struct ahash_request *req)
1374 return __crypto_ahash_cast(req->base.tfm);
1377 static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)
1379 return crypto_ahash_crt(tfm)->reqsize;
1382 static inline int crypto_ahash_setkey(struct crypto_ahash *tfm,
1383 const u8 *key, unsigned int keylen)
1385 struct ahash_tfm *crt = crypto_ahash_crt(tfm);
1387 return crt->setkey(crt->base, key, keylen);
1390 static inline int crypto_ahash_digest(struct ahash_request *req)
1392 struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
1393 return crt->digest(req);
1396 static inline void ahash_request_set_tfm(struct ahash_request *req,
1397 struct crypto_ahash *tfm)
1399 req->base.tfm = crypto_ahash_tfm(crypto_ahash_crt(tfm)->base);
1402 static inline struct ahash_request *ahash_request_alloc(
1403 struct crypto_ahash *tfm, gfp_t gfp)
1405 struct ahash_request *req;
1407 req = kmalloc(sizeof(struct ahash_request) +
1408 crypto_ahash_reqsize(tfm), gfp);
1411 ahash_request_set_tfm(req, tfm);
1416 static inline void ahash_request_free(struct ahash_request *req)
1421 static inline struct ahash_request *ahash_request_cast(
1422 struct crypto_async_request *req)
1424 return container_of(req, struct ahash_request, base);
1427 static inline void ahash_request_set_callback(struct ahash_request *req,
1429 crypto_completion_t complete,
1432 req->base.complete = complete;
1433 req->base.data = data;
1434 req->base.flags = flags;
1437 static inline void ahash_request_set_crypt(struct ahash_request *req,
1438 struct scatterlist *src, u8 *result,
1439 unsigned int nbytes)
1442 req->nbytes = nbytes;
1443 req->result = result;
1446 #endif /* _LINUX_CRYPTO_H */