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
41 #define CRYPTO_ALG_TYPE_RNG 0x0000000c
43 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
44 #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
45 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
47 #define CRYPTO_ALG_LARVAL 0x00000010
48 #define CRYPTO_ALG_DEAD 0x00000020
49 #define CRYPTO_ALG_DYING 0x00000040
50 #define CRYPTO_ALG_ASYNC 0x00000080
53 * Set this bit if and only if the algorithm requires another algorithm of
54 * the same type to handle corner cases.
56 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
59 * This bit is set for symmetric key ciphers that have already been wrapped
60 * with a generic IV generator to prevent them from being wrapped again.
62 #define CRYPTO_ALG_GENIV 0x00000200
65 * Set if the algorithm has passed automated run-time testing. Note that
66 * if there is no run-time testing for a given algorithm it is considered
70 #define CRYPTO_ALG_TESTED 0x00000400
73 * Transform masks and values (for crt_flags).
75 #define CRYPTO_TFM_REQ_MASK 0x000fff00
76 #define CRYPTO_TFM_RES_MASK 0xfff00000
78 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
79 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
80 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
81 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
82 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
83 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
84 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
85 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
88 * Miscellaneous stuff.
90 #define CRYPTO_MAX_ALG_NAME 64
93 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
94 * declaration) is used to ensure that the crypto_tfm context structure is
95 * aligned correctly for the given architecture so that there are no alignment
96 * faults for C data types. In particular, this is required on platforms such
97 * as arm where pointers are 32-bit aligned but there are data types such as
98 * u64 which require 64-bit alignment.
100 #if defined(ARCH_KMALLOC_MINALIGN)
101 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
102 #elif defined(ARCH_SLAB_MINALIGN)
103 #define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
105 #define CRYPTO_MINALIGN __alignof__(unsigned long long)
108 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
111 struct crypto_ablkcipher;
112 struct crypto_async_request;
114 struct crypto_blkcipher;
120 struct aead_givcrypt_request;
121 struct skcipher_givcrypt_request;
123 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
125 struct crypto_async_request {
126 struct list_head list;
127 crypto_completion_t complete;
129 struct crypto_tfm *tfm;
134 struct ablkcipher_request {
135 struct crypto_async_request base;
141 struct scatterlist *src;
142 struct scatterlist *dst;
144 void *__ctx[] CRYPTO_MINALIGN_ATTR;
147 struct ahash_request {
148 struct crypto_async_request base;
151 struct scatterlist *src;
154 void *__ctx[] CRYPTO_MINALIGN_ATTR;
158 * struct aead_request - AEAD request
159 * @base: Common attributes for async crypto requests
160 * @assoclen: Length in bytes of associated data for authentication
161 * @cryptlen: Length of data to be encrypted or decrypted
162 * @iv: Initialisation vector
163 * @assoc: Associated data
165 * @dst: Destination data
166 * @__ctx: Start of private context data
168 struct aead_request {
169 struct crypto_async_request base;
171 unsigned int assoclen;
172 unsigned int cryptlen;
176 struct scatterlist *assoc;
177 struct scatterlist *src;
178 struct scatterlist *dst;
180 void *__ctx[] CRYPTO_MINALIGN_ATTR;
183 struct blkcipher_desc {
184 struct crypto_blkcipher *tfm;
190 struct crypto_tfm *tfm;
191 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
192 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
193 const u8 *src, unsigned int nbytes);
198 struct crypto_hash *tfm;
203 * Algorithms: modular crypto algorithm implementations, managed
204 * via crypto_register_alg() and crypto_unregister_alg().
206 struct ablkcipher_alg {
207 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
208 unsigned int keylen);
209 int (*encrypt)(struct ablkcipher_request *req);
210 int (*decrypt)(struct ablkcipher_request *req);
211 int (*givencrypt)(struct skcipher_givcrypt_request *req);
212 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
216 unsigned int min_keysize;
217 unsigned int max_keysize;
222 int (*init)(struct ahash_request *req);
223 int (*update)(struct ahash_request *req);
224 int (*final)(struct ahash_request *req);
225 int (*digest)(struct ahash_request *req);
226 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
227 unsigned int keylen);
229 unsigned int digestsize;
233 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
234 unsigned int keylen);
235 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
236 int (*encrypt)(struct aead_request *req);
237 int (*decrypt)(struct aead_request *req);
238 int (*givencrypt)(struct aead_givcrypt_request *req);
239 int (*givdecrypt)(struct aead_givcrypt_request *req);
244 unsigned int maxauthsize;
247 struct blkcipher_alg {
248 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
249 unsigned int keylen);
250 int (*encrypt)(struct blkcipher_desc *desc,
251 struct scatterlist *dst, struct scatterlist *src,
252 unsigned int nbytes);
253 int (*decrypt)(struct blkcipher_desc *desc,
254 struct scatterlist *dst, struct scatterlist *src,
255 unsigned int nbytes);
259 unsigned int min_keysize;
260 unsigned int max_keysize;
265 unsigned int cia_min_keysize;
266 unsigned int cia_max_keysize;
267 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
268 unsigned int keylen);
269 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
270 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
274 unsigned int dia_digestsize;
275 void (*dia_init)(struct crypto_tfm *tfm);
276 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
278 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
279 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
280 unsigned int keylen);
284 int (*init)(struct hash_desc *desc);
285 int (*update)(struct hash_desc *desc, struct scatterlist *sg,
286 unsigned int nbytes);
287 int (*final)(struct hash_desc *desc, u8 *out);
288 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
289 unsigned int nbytes, u8 *out);
290 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
291 unsigned int keylen);
293 unsigned int digestsize;
296 struct compress_alg {
297 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
298 unsigned int slen, u8 *dst, unsigned int *dlen);
299 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
300 unsigned int slen, u8 *dst, unsigned int *dlen);
304 int (*rng_make_random)(struct crypto_rng *tfm, u8 *rdata,
306 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
308 unsigned int seedsize;
312 #define cra_ablkcipher cra_u.ablkcipher
313 #define cra_aead cra_u.aead
314 #define cra_blkcipher cra_u.blkcipher
315 #define cra_cipher cra_u.cipher
316 #define cra_digest cra_u.digest
317 #define cra_hash cra_u.hash
318 #define cra_ahash cra_u.ahash
319 #define cra_compress cra_u.compress
320 #define cra_rng cra_u.rng
323 struct list_head cra_list;
324 struct list_head cra_users;
327 unsigned int cra_blocksize;
328 unsigned int cra_ctxsize;
329 unsigned int cra_alignmask;
334 char cra_name[CRYPTO_MAX_ALG_NAME];
335 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
337 const struct crypto_type *cra_type;
340 struct ablkcipher_alg ablkcipher;
341 struct aead_alg aead;
342 struct blkcipher_alg blkcipher;
343 struct cipher_alg cipher;
344 struct digest_alg digest;
345 struct hash_alg hash;
346 struct ahash_alg ahash;
347 struct compress_alg compress;
351 int (*cra_init)(struct crypto_tfm *tfm);
352 void (*cra_exit)(struct crypto_tfm *tfm);
353 void (*cra_destroy)(struct crypto_alg *alg);
355 struct module *cra_module;
359 * Algorithm registration interface.
361 int crypto_register_alg(struct crypto_alg *alg);
362 int crypto_unregister_alg(struct crypto_alg *alg);
365 * Algorithm query interface.
367 int crypto_has_alg(const char *name, u32 type, u32 mask);
370 * Transforms: user-instantiated objects which encapsulate algorithms
371 * and core processing logic. Managed via crypto_alloc_*() and
372 * crypto_free_*(), as well as the various helpers below.
375 struct ablkcipher_tfm {
376 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
377 unsigned int keylen);
378 int (*encrypt)(struct ablkcipher_request *req);
379 int (*decrypt)(struct ablkcipher_request *req);
380 int (*givencrypt)(struct skcipher_givcrypt_request *req);
381 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
383 struct crypto_ablkcipher *base;
386 unsigned int reqsize;
390 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
391 unsigned int keylen);
392 int (*encrypt)(struct aead_request *req);
393 int (*decrypt)(struct aead_request *req);
394 int (*givencrypt)(struct aead_givcrypt_request *req);
395 int (*givdecrypt)(struct aead_givcrypt_request *req);
397 struct crypto_aead *base;
400 unsigned int authsize;
401 unsigned int reqsize;
404 struct blkcipher_tfm {
406 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
407 unsigned int keylen);
408 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
409 struct scatterlist *src, unsigned int nbytes);
410 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
411 struct scatterlist *src, unsigned int nbytes);
415 int (*cit_setkey)(struct crypto_tfm *tfm,
416 const u8 *key, unsigned int keylen);
417 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
418 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
422 int (*init)(struct hash_desc *desc);
423 int (*update)(struct hash_desc *desc,
424 struct scatterlist *sg, unsigned int nsg);
425 int (*final)(struct hash_desc *desc, u8 *out);
426 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
427 unsigned int nsg, u8 *out);
428 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
429 unsigned int keylen);
430 unsigned int digestsize;
434 int (*init)(struct ahash_request *req);
435 int (*update)(struct ahash_request *req);
436 int (*final)(struct ahash_request *req);
437 int (*digest)(struct ahash_request *req);
438 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
439 unsigned int keylen);
441 unsigned int digestsize;
442 unsigned int reqsize;
445 struct compress_tfm {
446 int (*cot_compress)(struct crypto_tfm *tfm,
447 const u8 *src, unsigned int slen,
448 u8 *dst, unsigned int *dlen);
449 int (*cot_decompress)(struct crypto_tfm *tfm,
450 const u8 *src, unsigned int slen,
451 u8 *dst, unsigned int *dlen);
455 int (*rng_gen_random)(struct crypto_rng *tfm, u8 *rdata,
457 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
460 #define crt_ablkcipher crt_u.ablkcipher
461 #define crt_aead crt_u.aead
462 #define crt_blkcipher crt_u.blkcipher
463 #define crt_cipher crt_u.cipher
464 #define crt_hash crt_u.hash
465 #define crt_ahash crt_u.ahash
466 #define crt_compress crt_u.compress
467 #define crt_rng crt_u.rng
474 struct ablkcipher_tfm ablkcipher;
475 struct aead_tfm aead;
476 struct blkcipher_tfm blkcipher;
477 struct cipher_tfm cipher;
478 struct hash_tfm hash;
479 struct ahash_tfm ahash;
480 struct compress_tfm compress;
484 void (*exit)(struct crypto_tfm *tfm);
486 struct crypto_alg *__crt_alg;
488 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
491 struct crypto_ablkcipher {
492 struct crypto_tfm base;
496 struct crypto_tfm base;
499 struct crypto_blkcipher {
500 struct crypto_tfm base;
503 struct crypto_cipher {
504 struct crypto_tfm base;
508 struct crypto_tfm base;
512 struct crypto_tfm base;
516 struct crypto_tfm base;
527 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
529 /* Maximum number of (rtattr) parameters for each template. */
530 #define CRYPTO_MAX_ATTRS 32
532 struct crypto_attr_alg {
533 char name[CRYPTO_MAX_ALG_NAME];
536 struct crypto_attr_type {
541 struct crypto_attr_u32 {
546 * Transform user interface.
549 struct crypto_tfm *crypto_alloc_tfm(const char *alg_name,
550 const struct crypto_type *frontend,
552 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
553 void crypto_free_tfm(struct crypto_tfm *tfm);
555 int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
558 * Transform helpers which query the underlying algorithm.
560 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
562 return tfm->__crt_alg->cra_name;
565 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
567 return tfm->__crt_alg->cra_driver_name;
570 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
572 return tfm->__crt_alg->cra_priority;
575 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
577 return module_name(tfm->__crt_alg->cra_module);
580 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
582 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
585 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
587 return tfm->__crt_alg->cra_blocksize;
590 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
592 return tfm->__crt_alg->cra_alignmask;
595 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
597 return tfm->crt_flags;
600 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
602 tfm->crt_flags |= flags;
605 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
607 tfm->crt_flags &= ~flags;
610 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
612 return tfm->__crt_ctx;
615 static inline unsigned int crypto_tfm_ctx_alignment(void)
617 struct crypto_tfm *tfm;
618 return __alignof__(tfm->__crt_ctx);
624 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
625 struct crypto_tfm *tfm)
627 return (struct crypto_ablkcipher *)tfm;
630 static inline u32 crypto_skcipher_type(u32 type)
632 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
633 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
637 static inline u32 crypto_skcipher_mask(u32 mask)
639 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
640 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
644 struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
647 static inline struct crypto_tfm *crypto_ablkcipher_tfm(
648 struct crypto_ablkcipher *tfm)
653 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
655 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
658 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
661 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
662 crypto_skcipher_mask(mask));
665 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
666 struct crypto_ablkcipher *tfm)
668 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
671 static inline unsigned int crypto_ablkcipher_ivsize(
672 struct crypto_ablkcipher *tfm)
674 return crypto_ablkcipher_crt(tfm)->ivsize;
677 static inline unsigned int crypto_ablkcipher_blocksize(
678 struct crypto_ablkcipher *tfm)
680 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
683 static inline unsigned int crypto_ablkcipher_alignmask(
684 struct crypto_ablkcipher *tfm)
686 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
689 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
691 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
694 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
697 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
700 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
703 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
706 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
707 const u8 *key, unsigned int keylen)
709 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
711 return crt->setkey(crt->base, key, keylen);
714 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
715 struct ablkcipher_request *req)
717 return __crypto_ablkcipher_cast(req->base.tfm);
720 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
722 struct ablkcipher_tfm *crt =
723 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
724 return crt->encrypt(req);
727 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
729 struct ablkcipher_tfm *crt =
730 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
731 return crt->decrypt(req);
734 static inline unsigned int crypto_ablkcipher_reqsize(
735 struct crypto_ablkcipher *tfm)
737 return crypto_ablkcipher_crt(tfm)->reqsize;
740 static inline void ablkcipher_request_set_tfm(
741 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
743 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
746 static inline struct ablkcipher_request *ablkcipher_request_cast(
747 struct crypto_async_request *req)
749 return container_of(req, struct ablkcipher_request, base);
752 static inline struct ablkcipher_request *ablkcipher_request_alloc(
753 struct crypto_ablkcipher *tfm, gfp_t gfp)
755 struct ablkcipher_request *req;
757 req = kmalloc(sizeof(struct ablkcipher_request) +
758 crypto_ablkcipher_reqsize(tfm), gfp);
761 ablkcipher_request_set_tfm(req, tfm);
766 static inline void ablkcipher_request_free(struct ablkcipher_request *req)
771 static inline void ablkcipher_request_set_callback(
772 struct ablkcipher_request *req,
773 u32 flags, crypto_completion_t complete, void *data)
775 req->base.complete = complete;
776 req->base.data = data;
777 req->base.flags = flags;
780 static inline void ablkcipher_request_set_crypt(
781 struct ablkcipher_request *req,
782 struct scatterlist *src, struct scatterlist *dst,
783 unsigned int nbytes, void *iv)
787 req->nbytes = nbytes;
791 static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
793 return (struct crypto_aead *)tfm;
796 struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
798 static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
803 static inline void crypto_free_aead(struct crypto_aead *tfm)
805 crypto_free_tfm(crypto_aead_tfm(tfm));
808 static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
810 return &crypto_aead_tfm(tfm)->crt_aead;
813 static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
815 return crypto_aead_crt(tfm)->ivsize;
818 static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
820 return crypto_aead_crt(tfm)->authsize;
823 static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
825 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
828 static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
830 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
833 static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
835 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
838 static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
840 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
843 static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
845 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
848 static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
851 struct aead_tfm *crt = crypto_aead_crt(tfm);
853 return crt->setkey(crt->base, key, keylen);
856 int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
858 static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
860 return __crypto_aead_cast(req->base.tfm);
863 static inline int crypto_aead_encrypt(struct aead_request *req)
865 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
868 static inline int crypto_aead_decrypt(struct aead_request *req)
870 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
873 static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
875 return crypto_aead_crt(tfm)->reqsize;
878 static inline void aead_request_set_tfm(struct aead_request *req,
879 struct crypto_aead *tfm)
881 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
884 static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
887 struct aead_request *req;
889 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
892 aead_request_set_tfm(req, tfm);
897 static inline void aead_request_free(struct aead_request *req)
902 static inline void aead_request_set_callback(struct aead_request *req,
904 crypto_completion_t complete,
907 req->base.complete = complete;
908 req->base.data = data;
909 req->base.flags = flags;
912 static inline void aead_request_set_crypt(struct aead_request *req,
913 struct scatterlist *src,
914 struct scatterlist *dst,
915 unsigned int cryptlen, u8 *iv)
919 req->cryptlen = cryptlen;
923 static inline void aead_request_set_assoc(struct aead_request *req,
924 struct scatterlist *assoc,
925 unsigned int assoclen)
928 req->assoclen = assoclen;
931 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
932 struct crypto_tfm *tfm)
934 return (struct crypto_blkcipher *)tfm;
937 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
938 struct crypto_tfm *tfm)
940 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
941 return __crypto_blkcipher_cast(tfm);
944 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
945 const char *alg_name, u32 type, u32 mask)
947 type &= ~CRYPTO_ALG_TYPE_MASK;
948 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
949 mask |= CRYPTO_ALG_TYPE_MASK;
951 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
954 static inline struct crypto_tfm *crypto_blkcipher_tfm(
955 struct crypto_blkcipher *tfm)
960 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
962 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
965 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
967 type &= ~CRYPTO_ALG_TYPE_MASK;
968 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
969 mask |= CRYPTO_ALG_TYPE_MASK;
971 return crypto_has_alg(alg_name, type, mask);
974 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
976 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
979 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
980 struct crypto_blkcipher *tfm)
982 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
985 static inline struct blkcipher_alg *crypto_blkcipher_alg(
986 struct crypto_blkcipher *tfm)
988 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
991 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
993 return crypto_blkcipher_alg(tfm)->ivsize;
996 static inline unsigned int crypto_blkcipher_blocksize(
997 struct crypto_blkcipher *tfm)
999 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1002 static inline unsigned int crypto_blkcipher_alignmask(
1003 struct crypto_blkcipher *tfm)
1005 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1008 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1010 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1013 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1016 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1019 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1022 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1025 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1026 const u8 *key, unsigned int keylen)
1028 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1032 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1033 struct scatterlist *dst,
1034 struct scatterlist *src,
1035 unsigned int nbytes)
1037 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1038 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1041 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1042 struct scatterlist *dst,
1043 struct scatterlist *src,
1044 unsigned int nbytes)
1046 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1049 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1050 struct scatterlist *dst,
1051 struct scatterlist *src,
1052 unsigned int nbytes)
1054 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1055 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1058 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1059 struct scatterlist *dst,
1060 struct scatterlist *src,
1061 unsigned int nbytes)
1063 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1066 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1067 const u8 *src, unsigned int len)
1069 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1072 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1073 u8 *dst, unsigned int len)
1075 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1078 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1080 return (struct crypto_cipher *)tfm;
1083 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1085 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1086 return __crypto_cipher_cast(tfm);
1089 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1092 type &= ~CRYPTO_ALG_TYPE_MASK;
1093 type |= CRYPTO_ALG_TYPE_CIPHER;
1094 mask |= CRYPTO_ALG_TYPE_MASK;
1096 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1099 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1104 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1106 crypto_free_tfm(crypto_cipher_tfm(tfm));
1109 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1111 type &= ~CRYPTO_ALG_TYPE_MASK;
1112 type |= CRYPTO_ALG_TYPE_CIPHER;
1113 mask |= CRYPTO_ALG_TYPE_MASK;
1115 return crypto_has_alg(alg_name, type, mask);
1118 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1120 return &crypto_cipher_tfm(tfm)->crt_cipher;
1123 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1125 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1128 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1130 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1133 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1135 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1138 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1141 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1144 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1147 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1150 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1151 const u8 *key, unsigned int keylen)
1153 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1157 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1158 u8 *dst, const u8 *src)
1160 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1164 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1165 u8 *dst, const u8 *src)
1167 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1171 static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
1173 return (struct crypto_hash *)tfm;
1176 static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
1178 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1179 CRYPTO_ALG_TYPE_HASH_MASK);
1180 return __crypto_hash_cast(tfm);
1183 static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1186 type &= ~CRYPTO_ALG_TYPE_MASK;
1187 mask &= ~CRYPTO_ALG_TYPE_MASK;
1188 type |= CRYPTO_ALG_TYPE_HASH;
1189 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1191 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1194 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1199 static inline void crypto_free_hash(struct crypto_hash *tfm)
1201 crypto_free_tfm(crypto_hash_tfm(tfm));
1204 static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1206 type &= ~CRYPTO_ALG_TYPE_MASK;
1207 mask &= ~CRYPTO_ALG_TYPE_MASK;
1208 type |= CRYPTO_ALG_TYPE_HASH;
1209 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1211 return crypto_has_alg(alg_name, type, mask);
1214 static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1216 return &crypto_hash_tfm(tfm)->crt_hash;
1219 static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1221 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1224 static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1226 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1229 static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1231 return crypto_hash_crt(tfm)->digestsize;
1234 static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1236 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1239 static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1241 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1244 static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1246 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1249 static inline int crypto_hash_init(struct hash_desc *desc)
1251 return crypto_hash_crt(desc->tfm)->init(desc);
1254 static inline int crypto_hash_update(struct hash_desc *desc,
1255 struct scatterlist *sg,
1256 unsigned int nbytes)
1258 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1261 static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1263 return crypto_hash_crt(desc->tfm)->final(desc, out);
1266 static inline int crypto_hash_digest(struct hash_desc *desc,
1267 struct scatterlist *sg,
1268 unsigned int nbytes, u8 *out)
1270 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1273 static inline int crypto_hash_setkey(struct crypto_hash *hash,
1274 const u8 *key, unsigned int keylen)
1276 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
1279 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1281 return (struct crypto_comp *)tfm;
1284 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1286 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1287 CRYPTO_ALG_TYPE_MASK);
1288 return __crypto_comp_cast(tfm);
1291 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1294 type &= ~CRYPTO_ALG_TYPE_MASK;
1295 type |= CRYPTO_ALG_TYPE_COMPRESS;
1296 mask |= CRYPTO_ALG_TYPE_MASK;
1298 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1301 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1306 static inline void crypto_free_comp(struct crypto_comp *tfm)
1308 crypto_free_tfm(crypto_comp_tfm(tfm));
1311 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1313 type &= ~CRYPTO_ALG_TYPE_MASK;
1314 type |= CRYPTO_ALG_TYPE_COMPRESS;
1315 mask |= CRYPTO_ALG_TYPE_MASK;
1317 return crypto_has_alg(alg_name, type, mask);
1320 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1322 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1325 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1327 return &crypto_comp_tfm(tfm)->crt_compress;
1330 static inline int crypto_comp_compress(struct crypto_comp *tfm,
1331 const u8 *src, unsigned int slen,
1332 u8 *dst, unsigned int *dlen)
1334 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1335 src, slen, dst, dlen);
1338 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
1339 const u8 *src, unsigned int slen,
1340 u8 *dst, unsigned int *dlen)
1342 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1343 src, slen, dst, dlen);
1346 #endif /* _LINUX_CRYPTO_H */