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_REQ_MASK 0x000fff00
101 #define CRYPTO_TFM_RES_MASK 0xfff00000
103 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
104 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
105 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
106 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
107 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
108 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
109 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
110 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
113 * Miscellaneous stuff.
115 #define CRYPTO_MAX_ALG_NAME 64
118 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
119 * declaration) is used to ensure that the crypto_tfm context structure is
120 * aligned correctly for the given architecture so that there are no alignment
121 * faults for C data types. In particular, this is required on platforms such
122 * as arm where pointers are 32-bit aligned but there are data types such as
123 * u64 which require 64-bit alignment.
125 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
127 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
130 struct crypto_ablkcipher;
131 struct crypto_async_request;
133 struct crypto_blkcipher;
138 struct aead_givcrypt_request;
139 struct skcipher_givcrypt_request;
141 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
143 struct crypto_async_request {
144 struct list_head list;
145 crypto_completion_t complete;
147 struct crypto_tfm *tfm;
152 struct ablkcipher_request {
153 struct crypto_async_request base;
159 struct scatterlist *src;
160 struct scatterlist *dst;
162 void *__ctx[] CRYPTO_MINALIGN_ATTR;
166 * struct aead_request - AEAD request
167 * @base: Common attributes for async crypto requests
168 * @assoclen: Length in bytes of associated data for authentication
169 * @cryptlen: Length of data to be encrypted or decrypted
170 * @iv: Initialisation vector
171 * @assoc: Associated data
173 * @dst: Destination data
174 * @__ctx: Start of private context data
176 struct aead_request {
177 struct crypto_async_request base;
179 unsigned int assoclen;
180 unsigned int cryptlen;
184 struct scatterlist *assoc;
185 struct scatterlist *src;
186 struct scatterlist *dst;
188 void *__ctx[] CRYPTO_MINALIGN_ATTR;
191 struct blkcipher_desc {
192 struct crypto_blkcipher *tfm;
198 struct crypto_tfm *tfm;
199 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
200 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
201 const u8 *src, unsigned int nbytes);
206 struct crypto_hash *tfm;
211 * Algorithms: modular crypto algorithm implementations, managed
212 * via crypto_register_alg() and crypto_unregister_alg().
214 struct ablkcipher_alg {
215 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
216 unsigned int keylen);
217 int (*encrypt)(struct ablkcipher_request *req);
218 int (*decrypt)(struct ablkcipher_request *req);
219 int (*givencrypt)(struct skcipher_givcrypt_request *req);
220 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
224 unsigned int min_keysize;
225 unsigned int max_keysize;
230 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
231 unsigned int keylen);
232 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
233 int (*encrypt)(struct aead_request *req);
234 int (*decrypt)(struct aead_request *req);
235 int (*givencrypt)(struct aead_givcrypt_request *req);
236 int (*givdecrypt)(struct aead_givcrypt_request *req);
241 unsigned int maxauthsize;
244 struct blkcipher_alg {
245 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
246 unsigned int keylen);
247 int (*encrypt)(struct blkcipher_desc *desc,
248 struct scatterlist *dst, struct scatterlist *src,
249 unsigned int nbytes);
250 int (*decrypt)(struct blkcipher_desc *desc,
251 struct scatterlist *dst, struct scatterlist *src,
252 unsigned int nbytes);
256 unsigned int min_keysize;
257 unsigned int max_keysize;
262 unsigned int cia_min_keysize;
263 unsigned int cia_max_keysize;
264 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
265 unsigned int keylen);
266 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
267 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
270 struct compress_alg {
271 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
272 unsigned int slen, u8 *dst, unsigned int *dlen);
273 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
274 unsigned int slen, u8 *dst, unsigned int *dlen);
278 int (*rng_make_random)(struct crypto_rng *tfm, u8 *rdata,
280 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
282 unsigned int seedsize;
286 #define cra_ablkcipher cra_u.ablkcipher
287 #define cra_aead cra_u.aead
288 #define cra_blkcipher cra_u.blkcipher
289 #define cra_cipher cra_u.cipher
290 #define cra_compress cra_u.compress
291 #define cra_rng cra_u.rng
294 struct list_head cra_list;
295 struct list_head cra_users;
298 unsigned int cra_blocksize;
299 unsigned int cra_ctxsize;
300 unsigned int cra_alignmask;
305 char cra_name[CRYPTO_MAX_ALG_NAME];
306 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
308 const struct crypto_type *cra_type;
311 struct ablkcipher_alg ablkcipher;
312 struct aead_alg aead;
313 struct blkcipher_alg blkcipher;
314 struct cipher_alg cipher;
315 struct compress_alg compress;
319 int (*cra_init)(struct crypto_tfm *tfm);
320 void (*cra_exit)(struct crypto_tfm *tfm);
321 void (*cra_destroy)(struct crypto_alg *alg);
323 struct module *cra_module;
327 * Algorithm registration interface.
329 int crypto_register_alg(struct crypto_alg *alg);
330 int crypto_unregister_alg(struct crypto_alg *alg);
333 * Algorithm query interface.
335 int crypto_has_alg(const char *name, u32 type, u32 mask);
338 * Transforms: user-instantiated objects which encapsulate algorithms
339 * and core processing logic. Managed via crypto_alloc_*() and
340 * crypto_free_*(), as well as the various helpers below.
343 struct ablkcipher_tfm {
344 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
345 unsigned int keylen);
346 int (*encrypt)(struct ablkcipher_request *req);
347 int (*decrypt)(struct ablkcipher_request *req);
348 int (*givencrypt)(struct skcipher_givcrypt_request *req);
349 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
351 struct crypto_ablkcipher *base;
354 unsigned int reqsize;
359 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
360 unsigned int keylen);
361 int (*encrypt)(struct aead_request *req);
362 int (*decrypt)(struct aead_request *req);
363 int (*givencrypt)(struct aead_givcrypt_request *req);
364 int (*givdecrypt)(struct aead_givcrypt_request *req);
366 struct crypto_aead *base;
369 unsigned int authsize;
370 unsigned int reqsize;
373 struct blkcipher_tfm {
375 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
376 unsigned int keylen);
377 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
378 struct scatterlist *src, unsigned int nbytes);
379 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
380 struct scatterlist *src, unsigned int nbytes);
384 int (*cit_setkey)(struct crypto_tfm *tfm,
385 const u8 *key, unsigned int keylen);
386 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
387 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
391 int (*init)(struct hash_desc *desc);
392 int (*update)(struct hash_desc *desc,
393 struct scatterlist *sg, unsigned int nsg);
394 int (*final)(struct hash_desc *desc, u8 *out);
395 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
396 unsigned int nsg, u8 *out);
397 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
398 unsigned int keylen);
399 unsigned int digestsize;
402 struct compress_tfm {
403 int (*cot_compress)(struct crypto_tfm *tfm,
404 const u8 *src, unsigned int slen,
405 u8 *dst, unsigned int *dlen);
406 int (*cot_decompress)(struct crypto_tfm *tfm,
407 const u8 *src, unsigned int slen,
408 u8 *dst, unsigned int *dlen);
412 int (*rng_gen_random)(struct crypto_rng *tfm, u8 *rdata,
414 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
417 #define crt_ablkcipher crt_u.ablkcipher
418 #define crt_aead crt_u.aead
419 #define crt_blkcipher crt_u.blkcipher
420 #define crt_cipher crt_u.cipher
421 #define crt_hash crt_u.hash
422 #define crt_compress crt_u.compress
423 #define crt_rng crt_u.rng
430 struct ablkcipher_tfm ablkcipher;
431 struct aead_tfm aead;
432 struct blkcipher_tfm blkcipher;
433 struct cipher_tfm cipher;
434 struct hash_tfm hash;
435 struct compress_tfm compress;
439 void (*exit)(struct crypto_tfm *tfm);
441 struct crypto_alg *__crt_alg;
443 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
446 struct crypto_ablkcipher {
447 struct crypto_tfm base;
451 struct crypto_tfm base;
454 struct crypto_blkcipher {
455 struct crypto_tfm base;
458 struct crypto_cipher {
459 struct crypto_tfm base;
463 struct crypto_tfm base;
467 struct crypto_tfm base;
471 struct crypto_tfm base;
482 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
484 /* Maximum number of (rtattr) parameters for each template. */
485 #define CRYPTO_MAX_ATTRS 32
487 struct crypto_attr_alg {
488 char name[CRYPTO_MAX_ALG_NAME];
491 struct crypto_attr_type {
496 struct crypto_attr_u32 {
501 * Transform user interface.
504 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
505 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
507 static inline void crypto_free_tfm(struct crypto_tfm *tfm)
509 return crypto_destroy_tfm(tfm, tfm);
512 int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
515 * Transform helpers which query the underlying algorithm.
517 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
519 return tfm->__crt_alg->cra_name;
522 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
524 return tfm->__crt_alg->cra_driver_name;
527 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
529 return tfm->__crt_alg->cra_priority;
532 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
534 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
537 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
539 return tfm->__crt_alg->cra_blocksize;
542 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
544 return tfm->__crt_alg->cra_alignmask;
547 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
549 return tfm->crt_flags;
552 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
554 tfm->crt_flags |= flags;
557 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
559 tfm->crt_flags &= ~flags;
562 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
564 return tfm->__crt_ctx;
567 static inline unsigned int crypto_tfm_ctx_alignment(void)
569 struct crypto_tfm *tfm;
570 return __alignof__(tfm->__crt_ctx);
576 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
577 struct crypto_tfm *tfm)
579 return (struct crypto_ablkcipher *)tfm;
582 static inline u32 crypto_skcipher_type(u32 type)
584 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
585 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
589 static inline u32 crypto_skcipher_mask(u32 mask)
591 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
592 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
596 struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
599 static inline struct crypto_tfm *crypto_ablkcipher_tfm(
600 struct crypto_ablkcipher *tfm)
605 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
607 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
610 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
613 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
614 crypto_skcipher_mask(mask));
617 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
618 struct crypto_ablkcipher *tfm)
620 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
623 static inline unsigned int crypto_ablkcipher_ivsize(
624 struct crypto_ablkcipher *tfm)
626 return crypto_ablkcipher_crt(tfm)->ivsize;
629 static inline unsigned int crypto_ablkcipher_blocksize(
630 struct crypto_ablkcipher *tfm)
632 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
635 static inline unsigned int crypto_ablkcipher_alignmask(
636 struct crypto_ablkcipher *tfm)
638 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
641 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
643 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
646 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
649 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
652 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
655 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
658 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
659 const u8 *key, unsigned int keylen)
661 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
663 return crt->setkey(crt->base, key, keylen);
666 static inline bool crypto_ablkcipher_has_setkey(struct crypto_ablkcipher *tfm)
668 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
670 return crt->has_setkey;
673 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
674 struct ablkcipher_request *req)
676 return __crypto_ablkcipher_cast(req->base.tfm);
679 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
681 struct ablkcipher_tfm *crt =
682 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
683 return crt->encrypt(req);
686 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
688 struct ablkcipher_tfm *crt =
689 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
690 return crt->decrypt(req);
693 static inline unsigned int crypto_ablkcipher_reqsize(
694 struct crypto_ablkcipher *tfm)
696 return crypto_ablkcipher_crt(tfm)->reqsize;
699 static inline void ablkcipher_request_set_tfm(
700 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
702 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
705 static inline struct ablkcipher_request *ablkcipher_request_cast(
706 struct crypto_async_request *req)
708 return container_of(req, struct ablkcipher_request, base);
711 static inline struct ablkcipher_request *ablkcipher_request_alloc(
712 struct crypto_ablkcipher *tfm, gfp_t gfp)
714 struct ablkcipher_request *req;
716 req = kmalloc(sizeof(struct ablkcipher_request) +
717 crypto_ablkcipher_reqsize(tfm), gfp);
720 ablkcipher_request_set_tfm(req, tfm);
725 static inline void ablkcipher_request_free(struct ablkcipher_request *req)
730 static inline void ablkcipher_request_set_callback(
731 struct ablkcipher_request *req,
732 u32 flags, crypto_completion_t complete, void *data)
734 req->base.complete = complete;
735 req->base.data = data;
736 req->base.flags = flags;
739 static inline void ablkcipher_request_set_crypt(
740 struct ablkcipher_request *req,
741 struct scatterlist *src, struct scatterlist *dst,
742 unsigned int nbytes, void *iv)
746 req->nbytes = nbytes;
750 static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
752 return (struct crypto_aead *)tfm;
755 struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
757 static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
762 static inline void crypto_free_aead(struct crypto_aead *tfm)
764 crypto_free_tfm(crypto_aead_tfm(tfm));
767 static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
769 return &crypto_aead_tfm(tfm)->crt_aead;
772 static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
774 return crypto_aead_crt(tfm)->ivsize;
777 static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
779 return crypto_aead_crt(tfm)->authsize;
782 static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
784 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
787 static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
789 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
792 static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
794 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
797 static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
799 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
802 static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
804 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
807 static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
810 struct aead_tfm *crt = crypto_aead_crt(tfm);
812 return crt->setkey(crt->base, key, keylen);
815 int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
817 static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
819 return __crypto_aead_cast(req->base.tfm);
822 static inline int crypto_aead_encrypt(struct aead_request *req)
824 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
827 static inline int crypto_aead_decrypt(struct aead_request *req)
829 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
832 static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
834 return crypto_aead_crt(tfm)->reqsize;
837 static inline void aead_request_set_tfm(struct aead_request *req,
838 struct crypto_aead *tfm)
840 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
843 static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
846 struct aead_request *req;
848 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
851 aead_request_set_tfm(req, tfm);
856 static inline void aead_request_free(struct aead_request *req)
861 static inline void aead_request_set_callback(struct aead_request *req,
863 crypto_completion_t complete,
866 req->base.complete = complete;
867 req->base.data = data;
868 req->base.flags = flags;
871 static inline void aead_request_set_crypt(struct aead_request *req,
872 struct scatterlist *src,
873 struct scatterlist *dst,
874 unsigned int cryptlen, u8 *iv)
878 req->cryptlen = cryptlen;
882 static inline void aead_request_set_assoc(struct aead_request *req,
883 struct scatterlist *assoc,
884 unsigned int assoclen)
887 req->assoclen = assoclen;
890 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
891 struct crypto_tfm *tfm)
893 return (struct crypto_blkcipher *)tfm;
896 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
897 struct crypto_tfm *tfm)
899 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
900 return __crypto_blkcipher_cast(tfm);
903 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
904 const char *alg_name, u32 type, u32 mask)
906 type &= ~CRYPTO_ALG_TYPE_MASK;
907 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
908 mask |= CRYPTO_ALG_TYPE_MASK;
910 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
913 static inline struct crypto_tfm *crypto_blkcipher_tfm(
914 struct crypto_blkcipher *tfm)
919 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
921 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
924 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
926 type &= ~CRYPTO_ALG_TYPE_MASK;
927 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
928 mask |= CRYPTO_ALG_TYPE_MASK;
930 return crypto_has_alg(alg_name, type, mask);
933 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
935 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
938 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
939 struct crypto_blkcipher *tfm)
941 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
944 static inline struct blkcipher_alg *crypto_blkcipher_alg(
945 struct crypto_blkcipher *tfm)
947 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
950 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
952 return crypto_blkcipher_alg(tfm)->ivsize;
955 static inline unsigned int crypto_blkcipher_blocksize(
956 struct crypto_blkcipher *tfm)
958 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
961 static inline unsigned int crypto_blkcipher_alignmask(
962 struct crypto_blkcipher *tfm)
964 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
967 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
969 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
972 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
975 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
978 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
981 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
984 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
985 const u8 *key, unsigned int keylen)
987 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
991 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
992 struct scatterlist *dst,
993 struct scatterlist *src,
996 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
997 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1000 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1001 struct scatterlist *dst,
1002 struct scatterlist *src,
1003 unsigned int nbytes)
1005 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1008 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1009 struct scatterlist *dst,
1010 struct scatterlist *src,
1011 unsigned int nbytes)
1013 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1014 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1017 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1018 struct scatterlist *dst,
1019 struct scatterlist *src,
1020 unsigned int nbytes)
1022 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1025 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1026 const u8 *src, unsigned int len)
1028 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1031 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1032 u8 *dst, unsigned int len)
1034 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1037 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1039 return (struct crypto_cipher *)tfm;
1042 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1044 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1045 return __crypto_cipher_cast(tfm);
1048 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1051 type &= ~CRYPTO_ALG_TYPE_MASK;
1052 type |= CRYPTO_ALG_TYPE_CIPHER;
1053 mask |= CRYPTO_ALG_TYPE_MASK;
1055 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1058 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1063 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1065 crypto_free_tfm(crypto_cipher_tfm(tfm));
1068 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1070 type &= ~CRYPTO_ALG_TYPE_MASK;
1071 type |= CRYPTO_ALG_TYPE_CIPHER;
1072 mask |= CRYPTO_ALG_TYPE_MASK;
1074 return crypto_has_alg(alg_name, type, mask);
1077 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1079 return &crypto_cipher_tfm(tfm)->crt_cipher;
1082 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1084 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1087 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1089 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1092 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1094 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1097 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1100 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1103 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1106 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1109 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1110 const u8 *key, unsigned int keylen)
1112 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1116 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1117 u8 *dst, const u8 *src)
1119 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1123 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1124 u8 *dst, const u8 *src)
1126 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1130 static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
1132 return (struct crypto_hash *)tfm;
1135 static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
1137 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1138 CRYPTO_ALG_TYPE_HASH_MASK);
1139 return __crypto_hash_cast(tfm);
1142 static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1145 type &= ~CRYPTO_ALG_TYPE_MASK;
1146 mask &= ~CRYPTO_ALG_TYPE_MASK;
1147 type |= CRYPTO_ALG_TYPE_HASH;
1148 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1150 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1153 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1158 static inline void crypto_free_hash(struct crypto_hash *tfm)
1160 crypto_free_tfm(crypto_hash_tfm(tfm));
1163 static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1165 type &= ~CRYPTO_ALG_TYPE_MASK;
1166 mask &= ~CRYPTO_ALG_TYPE_MASK;
1167 type |= CRYPTO_ALG_TYPE_HASH;
1168 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1170 return crypto_has_alg(alg_name, type, mask);
1173 static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1175 return &crypto_hash_tfm(tfm)->crt_hash;
1178 static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1180 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1183 static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1185 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1188 static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1190 return crypto_hash_crt(tfm)->digestsize;
1193 static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1195 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1198 static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1200 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1203 static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1205 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1208 static inline int crypto_hash_init(struct hash_desc *desc)
1210 return crypto_hash_crt(desc->tfm)->init(desc);
1213 static inline int crypto_hash_update(struct hash_desc *desc,
1214 struct scatterlist *sg,
1215 unsigned int nbytes)
1217 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1220 static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1222 return crypto_hash_crt(desc->tfm)->final(desc, out);
1225 static inline int crypto_hash_digest(struct hash_desc *desc,
1226 struct scatterlist *sg,
1227 unsigned int nbytes, u8 *out)
1229 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1232 static inline int crypto_hash_setkey(struct crypto_hash *hash,
1233 const u8 *key, unsigned int keylen)
1235 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
1238 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1240 return (struct crypto_comp *)tfm;
1243 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1245 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1246 CRYPTO_ALG_TYPE_MASK);
1247 return __crypto_comp_cast(tfm);
1250 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1253 type &= ~CRYPTO_ALG_TYPE_MASK;
1254 type |= CRYPTO_ALG_TYPE_COMPRESS;
1255 mask |= CRYPTO_ALG_TYPE_MASK;
1257 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1260 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1265 static inline void crypto_free_comp(struct crypto_comp *tfm)
1267 crypto_free_tfm(crypto_comp_tfm(tfm));
1270 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1272 type &= ~CRYPTO_ALG_TYPE_MASK;
1273 type |= CRYPTO_ALG_TYPE_COMPRESS;
1274 mask |= CRYPTO_ALG_TYPE_MASK;
1276 return crypto_has_alg(alg_name, type, mask);
1279 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1281 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1284 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1286 return &crypto_comp_tfm(tfm)->crt_compress;
1289 static inline int crypto_comp_compress(struct crypto_comp *tfm,
1290 const u8 *src, unsigned int slen,
1291 u8 *dst, unsigned int *dlen)
1293 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1294 src, slen, dst, dlen);
1297 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
1298 const u8 *src, unsigned int slen,
1299 u8 *dst, unsigned int *dlen)
1301 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1302 src, slen, dst, dlen);
1305 #endif /* _LINUX_CRYPTO_H */