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/slab.h>
24 #include <linux/string.h>
25 #include <linux/uaccess.h>
28 * Autoloaded crypto modules should only use a prefixed name to avoid allowing
29 * arbitrary modules to be loaded. Loading from userspace may still need the
30 * unprefixed names, so retains those aliases as well.
31 * This uses __MODULE_INFO directly instead of MODULE_ALIAS because pre-4.3
32 * gcc (e.g. avr32 toolchain) uses __LINE__ for uniqueness, and this macro
33 * expands twice on the same line. Instead, use a separate base name for the
36 #define MODULE_ALIAS_CRYPTO(name) \
37 __MODULE_INFO(alias, alias_userspace, name); \
38 __MODULE_INFO(alias, alias_crypto, "crypto-" name)
41 * Algorithm masks and types.
43 #define CRYPTO_ALG_TYPE_MASK 0x0000000f
44 #define CRYPTO_ALG_TYPE_CIPHER 0x00000001
45 #define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
46 #define CRYPTO_ALG_TYPE_AEAD 0x00000003
47 #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
48 #define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
49 #define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
50 #define CRYPTO_ALG_TYPE_DIGEST 0x00000008
51 #define CRYPTO_ALG_TYPE_HASH 0x00000008
52 #define CRYPTO_ALG_TYPE_SHASH 0x00000009
53 #define CRYPTO_ALG_TYPE_AHASH 0x0000000a
54 #define CRYPTO_ALG_TYPE_RNG 0x0000000c
55 #define CRYPTO_ALG_TYPE_PCOMPRESS 0x0000000f
57 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
58 #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
59 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
61 #define CRYPTO_ALG_LARVAL 0x00000010
62 #define CRYPTO_ALG_DEAD 0x00000020
63 #define CRYPTO_ALG_DYING 0x00000040
64 #define CRYPTO_ALG_ASYNC 0x00000080
67 * Set this bit if and only if the algorithm requires another algorithm of
68 * the same type to handle corner cases.
70 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
73 * This bit is set for symmetric key ciphers that have already been wrapped
74 * with a generic IV generator to prevent them from being wrapped again.
76 #define CRYPTO_ALG_GENIV 0x00000200
79 * Set if the algorithm has passed automated run-time testing. Note that
80 * if there is no run-time testing for a given algorithm it is considered
84 #define CRYPTO_ALG_TESTED 0x00000400
87 * Set if the algorithm is an instance that is build from templates.
89 #define CRYPTO_ALG_INSTANCE 0x00000800
92 * Set if the algorithm has a ->setkey() method but can be used without
93 * calling it first, i.e. there is a default key.
95 #define CRYPTO_ALG_OPTIONAL_KEY 0x00004000
98 * Transform masks and values (for crt_flags).
100 #define CRYPTO_TFM_NEED_KEY 0x00000001
102 #define CRYPTO_TFM_REQ_MASK 0x000fff00
103 #define CRYPTO_TFM_RES_MASK 0xfff00000
105 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
106 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
107 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
108 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
109 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
110 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
111 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
112 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
115 * Miscellaneous stuff.
117 #define CRYPTO_MAX_ALG_NAME 64
120 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
121 * declaration) is used to ensure that the crypto_tfm context structure is
122 * aligned correctly for the given architecture so that there are no alignment
123 * faults for C data types. In particular, this is required on platforms such
124 * as arm where pointers are 32-bit aligned but there are data types such as
125 * u64 which require 64-bit alignment.
127 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
129 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
132 struct crypto_ablkcipher;
133 struct crypto_async_request;
135 struct crypto_blkcipher;
140 struct aead_givcrypt_request;
141 struct skcipher_givcrypt_request;
143 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
145 struct crypto_async_request {
146 struct list_head list;
147 crypto_completion_t complete;
149 struct crypto_tfm *tfm;
154 struct ablkcipher_request {
155 struct crypto_async_request base;
161 struct scatterlist *src;
162 struct scatterlist *dst;
164 void *__ctx[] CRYPTO_MINALIGN_ATTR;
168 * struct aead_request - AEAD request
169 * @base: Common attributes for async crypto requests
170 * @assoclen: Length in bytes of associated data for authentication
171 * @cryptlen: Length of data to be encrypted or decrypted
172 * @iv: Initialisation vector
173 * @assoc: Associated data
175 * @dst: Destination data
176 * @__ctx: Start of private context data
178 struct aead_request {
179 struct crypto_async_request base;
181 unsigned int assoclen;
182 unsigned int cryptlen;
186 struct scatterlist *assoc;
187 struct scatterlist *src;
188 struct scatterlist *dst;
190 void *__ctx[] CRYPTO_MINALIGN_ATTR;
193 struct blkcipher_desc {
194 struct crypto_blkcipher *tfm;
200 struct crypto_tfm *tfm;
201 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
202 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
203 const u8 *src, unsigned int nbytes);
208 struct crypto_hash *tfm;
213 * Algorithms: modular crypto algorithm implementations, managed
214 * via crypto_register_alg() and crypto_unregister_alg().
216 struct ablkcipher_alg {
217 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
218 unsigned int keylen);
219 int (*encrypt)(struct ablkcipher_request *req);
220 int (*decrypt)(struct ablkcipher_request *req);
221 int (*givencrypt)(struct skcipher_givcrypt_request *req);
222 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
226 unsigned int min_keysize;
227 unsigned int max_keysize;
232 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
233 unsigned int keylen);
234 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
235 int (*encrypt)(struct aead_request *req);
236 int (*decrypt)(struct aead_request *req);
237 int (*givencrypt)(struct aead_givcrypt_request *req);
238 int (*givdecrypt)(struct aead_givcrypt_request *req);
243 unsigned int maxauthsize;
246 struct blkcipher_alg {
247 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
248 unsigned int keylen);
249 int (*encrypt)(struct blkcipher_desc *desc,
250 struct scatterlist *dst, struct scatterlist *src,
251 unsigned int nbytes);
252 int (*decrypt)(struct blkcipher_desc *desc,
253 struct scatterlist *dst, struct scatterlist *src,
254 unsigned int nbytes);
258 unsigned int min_keysize;
259 unsigned int max_keysize;
264 unsigned int cia_min_keysize;
265 unsigned int cia_max_keysize;
266 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
267 unsigned int keylen);
268 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
269 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
272 struct compress_alg {
273 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
274 unsigned int slen, u8 *dst, unsigned int *dlen);
275 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
276 unsigned int slen, u8 *dst, unsigned int *dlen);
280 int (*rng_make_random)(struct crypto_rng *tfm, u8 *rdata,
282 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
284 unsigned int seedsize;
288 #define cra_ablkcipher cra_u.ablkcipher
289 #define cra_aead cra_u.aead
290 #define cra_blkcipher cra_u.blkcipher
291 #define cra_cipher cra_u.cipher
292 #define cra_compress cra_u.compress
293 #define cra_rng cra_u.rng
296 struct list_head cra_list;
297 struct list_head cra_users;
300 unsigned int cra_blocksize;
301 unsigned int cra_ctxsize;
302 unsigned int cra_alignmask;
307 char cra_name[CRYPTO_MAX_ALG_NAME];
308 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
310 const struct crypto_type *cra_type;
313 struct ablkcipher_alg ablkcipher;
314 struct aead_alg aead;
315 struct blkcipher_alg blkcipher;
316 struct cipher_alg cipher;
317 struct compress_alg compress;
321 int (*cra_init)(struct crypto_tfm *tfm);
322 void (*cra_exit)(struct crypto_tfm *tfm);
323 void (*cra_destroy)(struct crypto_alg *alg);
325 struct module *cra_module;
329 * Algorithm registration interface.
331 int crypto_register_alg(struct crypto_alg *alg);
332 int crypto_unregister_alg(struct crypto_alg *alg);
335 * Algorithm query interface.
337 int crypto_has_alg(const char *name, u32 type, u32 mask);
340 * Transforms: user-instantiated objects which encapsulate algorithms
341 * and core processing logic. Managed via crypto_alloc_*() and
342 * crypto_free_*(), as well as the various helpers below.
345 struct ablkcipher_tfm {
346 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
347 unsigned int keylen);
348 int (*encrypt)(struct ablkcipher_request *req);
349 int (*decrypt)(struct ablkcipher_request *req);
350 int (*givencrypt)(struct skcipher_givcrypt_request *req);
351 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
353 struct crypto_ablkcipher *base;
356 unsigned int reqsize;
361 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
362 unsigned int keylen);
363 int (*encrypt)(struct aead_request *req);
364 int (*decrypt)(struct aead_request *req);
365 int (*givencrypt)(struct aead_givcrypt_request *req);
366 int (*givdecrypt)(struct aead_givcrypt_request *req);
368 struct crypto_aead *base;
371 unsigned int authsize;
372 unsigned int reqsize;
375 struct blkcipher_tfm {
377 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
378 unsigned int keylen);
379 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
380 struct scatterlist *src, unsigned int nbytes);
381 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
382 struct scatterlist *src, unsigned int nbytes);
386 int (*cit_setkey)(struct crypto_tfm *tfm,
387 const u8 *key, unsigned int keylen);
388 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
389 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
393 int (*init)(struct hash_desc *desc);
394 int (*update)(struct hash_desc *desc,
395 struct scatterlist *sg, unsigned int nsg);
396 int (*final)(struct hash_desc *desc, u8 *out);
397 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
398 unsigned int nsg, u8 *out);
399 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
400 unsigned int keylen);
401 unsigned int digestsize;
404 struct compress_tfm {
405 int (*cot_compress)(struct crypto_tfm *tfm,
406 const u8 *src, unsigned int slen,
407 u8 *dst, unsigned int *dlen);
408 int (*cot_decompress)(struct crypto_tfm *tfm,
409 const u8 *src, unsigned int slen,
410 u8 *dst, unsigned int *dlen);
414 int (*rng_gen_random)(struct crypto_rng *tfm, u8 *rdata,
416 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
419 #define crt_ablkcipher crt_u.ablkcipher
420 #define crt_aead crt_u.aead
421 #define crt_blkcipher crt_u.blkcipher
422 #define crt_cipher crt_u.cipher
423 #define crt_hash crt_u.hash
424 #define crt_compress crt_u.compress
425 #define crt_rng crt_u.rng
432 struct ablkcipher_tfm ablkcipher;
433 struct aead_tfm aead;
434 struct blkcipher_tfm blkcipher;
435 struct cipher_tfm cipher;
436 struct hash_tfm hash;
437 struct compress_tfm compress;
441 void (*exit)(struct crypto_tfm *tfm);
443 struct crypto_alg *__crt_alg;
445 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
448 struct crypto_ablkcipher {
449 struct crypto_tfm base;
453 struct crypto_tfm base;
456 struct crypto_blkcipher {
457 struct crypto_tfm base;
460 struct crypto_cipher {
461 struct crypto_tfm base;
465 struct crypto_tfm base;
469 struct crypto_tfm base;
473 struct crypto_tfm base;
484 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
486 /* Maximum number of (rtattr) parameters for each template. */
487 #define CRYPTO_MAX_ATTRS 32
489 struct crypto_attr_alg {
490 char name[CRYPTO_MAX_ALG_NAME];
493 struct crypto_attr_type {
498 struct crypto_attr_u32 {
503 * Transform user interface.
506 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
507 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
509 static inline void crypto_free_tfm(struct crypto_tfm *tfm)
511 return crypto_destroy_tfm(tfm, tfm);
514 int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
517 * Transform helpers which query the underlying algorithm.
519 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
521 return tfm->__crt_alg->cra_name;
524 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
526 return tfm->__crt_alg->cra_driver_name;
529 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
531 return tfm->__crt_alg->cra_priority;
534 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
536 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
539 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
541 return tfm->__crt_alg->cra_blocksize;
544 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
546 return tfm->__crt_alg->cra_alignmask;
549 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
551 return tfm->crt_flags;
554 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
556 tfm->crt_flags |= flags;
559 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
561 tfm->crt_flags &= ~flags;
564 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
566 return tfm->__crt_ctx;
569 static inline unsigned int crypto_tfm_ctx_alignment(void)
571 struct crypto_tfm *tfm;
572 return __alignof__(tfm->__crt_ctx);
578 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
579 struct crypto_tfm *tfm)
581 return (struct crypto_ablkcipher *)tfm;
584 static inline u32 crypto_skcipher_type(u32 type)
586 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
587 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
591 static inline u32 crypto_skcipher_mask(u32 mask)
593 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
594 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
598 struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
601 static inline struct crypto_tfm *crypto_ablkcipher_tfm(
602 struct crypto_ablkcipher *tfm)
607 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
609 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
612 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
615 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
616 crypto_skcipher_mask(mask));
619 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
620 struct crypto_ablkcipher *tfm)
622 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
625 static inline unsigned int crypto_ablkcipher_ivsize(
626 struct crypto_ablkcipher *tfm)
628 return crypto_ablkcipher_crt(tfm)->ivsize;
631 static inline unsigned int crypto_ablkcipher_blocksize(
632 struct crypto_ablkcipher *tfm)
634 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
637 static inline unsigned int crypto_ablkcipher_alignmask(
638 struct crypto_ablkcipher *tfm)
640 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
643 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
645 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
648 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
651 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
654 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
657 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
660 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
661 const u8 *key, unsigned int keylen)
663 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
665 return crt->setkey(crt->base, key, keylen);
668 static inline bool crypto_ablkcipher_has_setkey(struct crypto_ablkcipher *tfm)
670 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
672 return crt->has_setkey;
675 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
676 struct ablkcipher_request *req)
678 return __crypto_ablkcipher_cast(req->base.tfm);
681 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
683 struct ablkcipher_tfm *crt =
684 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
685 return crt->encrypt(req);
688 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
690 struct ablkcipher_tfm *crt =
691 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
692 return crt->decrypt(req);
695 static inline unsigned int crypto_ablkcipher_reqsize(
696 struct crypto_ablkcipher *tfm)
698 return crypto_ablkcipher_crt(tfm)->reqsize;
701 static inline void ablkcipher_request_set_tfm(
702 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
704 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
707 static inline struct ablkcipher_request *ablkcipher_request_cast(
708 struct crypto_async_request *req)
710 return container_of(req, struct ablkcipher_request, base);
713 static inline struct ablkcipher_request *ablkcipher_request_alloc(
714 struct crypto_ablkcipher *tfm, gfp_t gfp)
716 struct ablkcipher_request *req;
718 req = kmalloc(sizeof(struct ablkcipher_request) +
719 crypto_ablkcipher_reqsize(tfm), gfp);
722 ablkcipher_request_set_tfm(req, tfm);
727 static inline void ablkcipher_request_free(struct ablkcipher_request *req)
732 static inline void ablkcipher_request_set_callback(
733 struct ablkcipher_request *req,
734 u32 flags, crypto_completion_t complete, void *data)
736 req->base.complete = complete;
737 req->base.data = data;
738 req->base.flags = flags;
741 static inline void ablkcipher_request_set_crypt(
742 struct ablkcipher_request *req,
743 struct scatterlist *src, struct scatterlist *dst,
744 unsigned int nbytes, void *iv)
748 req->nbytes = nbytes;
752 static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
754 return (struct crypto_aead *)tfm;
757 struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
759 static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
764 static inline void crypto_free_aead(struct crypto_aead *tfm)
766 crypto_free_tfm(crypto_aead_tfm(tfm));
769 static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
771 return &crypto_aead_tfm(tfm)->crt_aead;
774 static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
776 return crypto_aead_crt(tfm)->ivsize;
779 static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
781 return crypto_aead_crt(tfm)->authsize;
784 static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
786 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
789 static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
791 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
794 static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
796 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
799 static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
801 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
804 static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
806 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
809 static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
812 struct aead_tfm *crt = crypto_aead_crt(tfm);
814 return crt->setkey(crt->base, key, keylen);
817 int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
819 static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
821 return __crypto_aead_cast(req->base.tfm);
824 static inline int crypto_aead_encrypt(struct aead_request *req)
826 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
829 static inline int crypto_aead_decrypt(struct aead_request *req)
831 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
834 static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
836 return crypto_aead_crt(tfm)->reqsize;
839 static inline void aead_request_set_tfm(struct aead_request *req,
840 struct crypto_aead *tfm)
842 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
845 static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
848 struct aead_request *req;
850 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
853 aead_request_set_tfm(req, tfm);
858 static inline void aead_request_free(struct aead_request *req)
863 static inline void aead_request_set_callback(struct aead_request *req,
865 crypto_completion_t complete,
868 req->base.complete = complete;
869 req->base.data = data;
870 req->base.flags = flags;
873 static inline void aead_request_set_crypt(struct aead_request *req,
874 struct scatterlist *src,
875 struct scatterlist *dst,
876 unsigned int cryptlen, u8 *iv)
880 req->cryptlen = cryptlen;
884 static inline void aead_request_set_assoc(struct aead_request *req,
885 struct scatterlist *assoc,
886 unsigned int assoclen)
889 req->assoclen = assoclen;
892 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
893 struct crypto_tfm *tfm)
895 return (struct crypto_blkcipher *)tfm;
898 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
899 struct crypto_tfm *tfm)
901 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
902 return __crypto_blkcipher_cast(tfm);
905 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
906 const char *alg_name, u32 type, u32 mask)
908 type &= ~CRYPTO_ALG_TYPE_MASK;
909 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
910 mask |= CRYPTO_ALG_TYPE_MASK;
912 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
915 static inline struct crypto_tfm *crypto_blkcipher_tfm(
916 struct crypto_blkcipher *tfm)
921 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
923 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
926 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
928 type &= ~CRYPTO_ALG_TYPE_MASK;
929 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
930 mask |= CRYPTO_ALG_TYPE_MASK;
932 return crypto_has_alg(alg_name, type, mask);
935 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
937 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
940 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
941 struct crypto_blkcipher *tfm)
943 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
946 static inline struct blkcipher_alg *crypto_blkcipher_alg(
947 struct crypto_blkcipher *tfm)
949 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
952 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
954 return crypto_blkcipher_alg(tfm)->ivsize;
957 static inline unsigned int crypto_blkcipher_blocksize(
958 struct crypto_blkcipher *tfm)
960 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
963 static inline unsigned int crypto_blkcipher_alignmask(
964 struct crypto_blkcipher *tfm)
966 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
969 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
971 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
974 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
977 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
980 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
983 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
986 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
987 const u8 *key, unsigned int keylen)
989 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
993 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
994 struct scatterlist *dst,
995 struct scatterlist *src,
998 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
999 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1002 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1003 struct scatterlist *dst,
1004 struct scatterlist *src,
1005 unsigned int nbytes)
1007 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1010 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1011 struct scatterlist *dst,
1012 struct scatterlist *src,
1013 unsigned int nbytes)
1015 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1016 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1019 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1020 struct scatterlist *dst,
1021 struct scatterlist *src,
1022 unsigned int nbytes)
1024 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1027 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1028 const u8 *src, unsigned int len)
1030 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1033 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1034 u8 *dst, unsigned int len)
1036 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1039 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1041 return (struct crypto_cipher *)tfm;
1044 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1046 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1047 return __crypto_cipher_cast(tfm);
1050 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1053 type &= ~CRYPTO_ALG_TYPE_MASK;
1054 type |= CRYPTO_ALG_TYPE_CIPHER;
1055 mask |= CRYPTO_ALG_TYPE_MASK;
1057 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1060 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1065 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1067 crypto_free_tfm(crypto_cipher_tfm(tfm));
1070 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1072 type &= ~CRYPTO_ALG_TYPE_MASK;
1073 type |= CRYPTO_ALG_TYPE_CIPHER;
1074 mask |= CRYPTO_ALG_TYPE_MASK;
1076 return crypto_has_alg(alg_name, type, mask);
1079 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1081 return &crypto_cipher_tfm(tfm)->crt_cipher;
1084 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1086 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1089 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1091 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1094 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1096 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1099 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1102 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1105 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1108 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1111 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1112 const u8 *key, unsigned int keylen)
1114 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1118 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1119 u8 *dst, const u8 *src)
1121 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1125 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1126 u8 *dst, const u8 *src)
1128 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1132 static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
1134 return (struct crypto_hash *)tfm;
1137 static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
1139 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1140 CRYPTO_ALG_TYPE_HASH_MASK);
1141 return __crypto_hash_cast(tfm);
1144 static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1147 type &= ~CRYPTO_ALG_TYPE_MASK;
1148 mask &= ~CRYPTO_ALG_TYPE_MASK;
1149 type |= CRYPTO_ALG_TYPE_HASH;
1150 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1152 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1155 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1160 static inline void crypto_free_hash(struct crypto_hash *tfm)
1162 crypto_free_tfm(crypto_hash_tfm(tfm));
1165 static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1167 type &= ~CRYPTO_ALG_TYPE_MASK;
1168 mask &= ~CRYPTO_ALG_TYPE_MASK;
1169 type |= CRYPTO_ALG_TYPE_HASH;
1170 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1172 return crypto_has_alg(alg_name, type, mask);
1175 static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1177 return &crypto_hash_tfm(tfm)->crt_hash;
1180 static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1182 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1185 static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1187 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1190 static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1192 return crypto_hash_crt(tfm)->digestsize;
1195 static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1197 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1200 static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1202 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1205 static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1207 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1210 static inline int crypto_hash_init(struct hash_desc *desc)
1212 return crypto_hash_crt(desc->tfm)->init(desc);
1215 static inline int crypto_hash_update(struct hash_desc *desc,
1216 struct scatterlist *sg,
1217 unsigned int nbytes)
1219 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1222 static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1224 return crypto_hash_crt(desc->tfm)->final(desc, out);
1227 static inline int crypto_hash_digest(struct hash_desc *desc,
1228 struct scatterlist *sg,
1229 unsigned int nbytes, u8 *out)
1231 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1234 static inline int crypto_hash_setkey(struct crypto_hash *hash,
1235 const u8 *key, unsigned int keylen)
1237 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
1240 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1242 return (struct crypto_comp *)tfm;
1245 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1247 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1248 CRYPTO_ALG_TYPE_MASK);
1249 return __crypto_comp_cast(tfm);
1252 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1255 type &= ~CRYPTO_ALG_TYPE_MASK;
1256 type |= CRYPTO_ALG_TYPE_COMPRESS;
1257 mask |= CRYPTO_ALG_TYPE_MASK;
1259 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1262 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1267 static inline void crypto_free_comp(struct crypto_comp *tfm)
1269 crypto_free_tfm(crypto_comp_tfm(tfm));
1272 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1274 type &= ~CRYPTO_ALG_TYPE_MASK;
1275 type |= CRYPTO_ALG_TYPE_COMPRESS;
1276 mask |= CRYPTO_ALG_TYPE_MASK;
1278 return crypto_has_alg(alg_name, type, mask);
1281 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1283 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1286 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1288 return &crypto_comp_tfm(tfm)->crt_compress;
1291 static inline int crypto_comp_compress(struct crypto_comp *tfm,
1292 const u8 *src, unsigned int slen,
1293 u8 *dst, unsigned int *dlen)
1295 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1296 src, slen, dst, dlen);
1299 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
1300 const u8 *src, unsigned int slen,
1301 u8 *dst, unsigned int *dlen)
1303 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1304 src, slen, dst, dlen);
1307 #endif /* _LINUX_CRYPTO_H */