2 * Scatterlist Cryptographic API.
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
6 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
8 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
9 * and Nettle, by Niels Möller.
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
17 #ifndef _LINUX_CRYPTO_H
18 #define _LINUX_CRYPTO_H
20 #include <asm/atomic.h>
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/list.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/uaccess.h>
29 * Algorithm masks and types.
31 #define CRYPTO_ALG_TYPE_MASK 0x0000000f
32 #define CRYPTO_ALG_TYPE_CIPHER 0x00000001
33 #define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
34 #define CRYPTO_ALG_TYPE_AEAD 0x00000003
35 #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
36 #define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
37 #define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
38 #define CRYPTO_ALG_TYPE_DIGEST 0x00000008
39 #define CRYPTO_ALG_TYPE_HASH 0x00000008
40 #define CRYPTO_ALG_TYPE_SHASH 0x00000009
41 #define CRYPTO_ALG_TYPE_AHASH 0x0000000a
42 #define CRYPTO_ALG_TYPE_RNG 0x0000000c
43 #define CRYPTO_ALG_TYPE_PCOMPRESS 0x0000000f
45 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
46 #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
47 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
49 #define CRYPTO_ALG_LARVAL 0x00000010
50 #define CRYPTO_ALG_DEAD 0x00000020
51 #define CRYPTO_ALG_DYING 0x00000040
52 #define CRYPTO_ALG_ASYNC 0x00000080
55 * Set this bit if and only if the algorithm requires another algorithm of
56 * the same type to handle corner cases.
58 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
61 * This bit is set for symmetric key ciphers that have already been wrapped
62 * with a generic IV generator to prevent them from being wrapped again.
64 #define CRYPTO_ALG_GENIV 0x00000200
67 * Set if the algorithm has passed automated run-time testing. Note that
68 * if there is no run-time testing for a given algorithm it is considered
72 #define CRYPTO_ALG_TESTED 0x00000400
75 * Transform masks and values (for crt_flags).
77 #define CRYPTO_TFM_REQ_MASK 0x000fff00
78 #define CRYPTO_TFM_RES_MASK 0xfff00000
80 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
81 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
82 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
83 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
84 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
85 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
86 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
87 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
90 * Miscellaneous stuff.
92 #define CRYPTO_MAX_ALG_NAME 64
95 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
96 * declaration) is used to ensure that the crypto_tfm context structure is
97 * aligned correctly for the given architecture so that there are no alignment
98 * faults for C data types. In particular, this is required on platforms such
99 * as arm where pointers are 32-bit aligned but there are data types such as
100 * u64 which require 64-bit alignment.
102 #if defined(ARCH_KMALLOC_MINALIGN)
103 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
104 #elif defined(ARCH_SLAB_MINALIGN)
105 #define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
107 #define CRYPTO_MINALIGN __alignof__(unsigned long long)
110 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
113 struct crypto_ablkcipher;
114 struct crypto_async_request;
116 struct crypto_blkcipher;
122 struct aead_givcrypt_request;
123 struct ahash_request;
124 struct skcipher_givcrypt_request;
126 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
128 struct crypto_async_request {
129 struct list_head list;
130 crypto_completion_t complete;
132 struct crypto_tfm *tfm;
137 struct ablkcipher_request {
138 struct crypto_async_request base;
144 struct scatterlist *src;
145 struct scatterlist *dst;
147 void *__ctx[] CRYPTO_MINALIGN_ATTR;
151 * struct aead_request - AEAD request
152 * @base: Common attributes for async crypto requests
153 * @assoclen: Length in bytes of associated data for authentication
154 * @cryptlen: Length of data to be encrypted or decrypted
155 * @iv: Initialisation vector
156 * @assoc: Associated data
158 * @dst: Destination data
159 * @__ctx: Start of private context data
161 struct aead_request {
162 struct crypto_async_request base;
164 unsigned int assoclen;
165 unsigned int cryptlen;
169 struct scatterlist *assoc;
170 struct scatterlist *src;
171 struct scatterlist *dst;
173 void *__ctx[] CRYPTO_MINALIGN_ATTR;
176 struct blkcipher_desc {
177 struct crypto_blkcipher *tfm;
183 struct crypto_tfm *tfm;
184 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
185 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
186 const u8 *src, unsigned int nbytes);
191 struct crypto_hash *tfm;
196 * Algorithms: modular crypto algorithm implementations, managed
197 * via crypto_register_alg() and crypto_unregister_alg().
199 struct ablkcipher_alg {
200 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
201 unsigned int keylen);
202 int (*encrypt)(struct ablkcipher_request *req);
203 int (*decrypt)(struct ablkcipher_request *req);
204 int (*givencrypt)(struct skcipher_givcrypt_request *req);
205 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
209 unsigned int min_keysize;
210 unsigned int max_keysize;
214 struct old_ahash_alg {
215 int (*init)(struct ahash_request *req);
216 int (*reinit)(struct ahash_request *req);
217 int (*update)(struct ahash_request *req);
218 int (*final)(struct ahash_request *req);
219 int (*digest)(struct ahash_request *req);
220 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
221 unsigned int keylen);
223 unsigned int digestsize;
227 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
228 unsigned int keylen);
229 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
230 int (*encrypt)(struct aead_request *req);
231 int (*decrypt)(struct aead_request *req);
232 int (*givencrypt)(struct aead_givcrypt_request *req);
233 int (*givdecrypt)(struct aead_givcrypt_request *req);
238 unsigned int maxauthsize;
241 struct blkcipher_alg {
242 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
243 unsigned int keylen);
244 int (*encrypt)(struct blkcipher_desc *desc,
245 struct scatterlist *dst, struct scatterlist *src,
246 unsigned int nbytes);
247 int (*decrypt)(struct blkcipher_desc *desc,
248 struct scatterlist *dst, struct scatterlist *src,
249 unsigned int nbytes);
253 unsigned int min_keysize;
254 unsigned int max_keysize;
259 unsigned int cia_min_keysize;
260 unsigned int cia_max_keysize;
261 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
262 unsigned int keylen);
263 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
264 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
268 unsigned int dia_digestsize;
269 void (*dia_init)(struct crypto_tfm *tfm);
270 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
272 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
273 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
274 unsigned int keylen);
278 int (*init)(struct hash_desc *desc);
279 int (*update)(struct hash_desc *desc, struct scatterlist *sg,
280 unsigned int nbytes);
281 int (*final)(struct hash_desc *desc, u8 *out);
282 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
283 unsigned int nbytes, u8 *out);
284 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
285 unsigned int keylen);
287 unsigned int digestsize;
290 struct compress_alg {
291 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
292 unsigned int slen, u8 *dst, unsigned int *dlen);
293 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
294 unsigned int slen, u8 *dst, unsigned int *dlen);
298 int (*rng_make_random)(struct crypto_rng *tfm, u8 *rdata,
300 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
302 unsigned int seedsize;
306 #define cra_ablkcipher cra_u.ablkcipher
307 #define cra_aead cra_u.aead
308 #define cra_blkcipher cra_u.blkcipher
309 #define cra_cipher cra_u.cipher
310 #define cra_digest cra_u.digest
311 #define cra_hash cra_u.hash
312 #define cra_ahash cra_u.ahash
313 #define cra_compress cra_u.compress
314 #define cra_rng cra_u.rng
317 struct list_head cra_list;
318 struct list_head cra_users;
321 unsigned int cra_blocksize;
322 unsigned int cra_ctxsize;
323 unsigned int cra_alignmask;
328 char cra_name[CRYPTO_MAX_ALG_NAME];
329 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
331 const struct crypto_type *cra_type;
334 struct ablkcipher_alg ablkcipher;
335 struct aead_alg aead;
336 struct blkcipher_alg blkcipher;
337 struct cipher_alg cipher;
338 struct digest_alg digest;
339 struct hash_alg hash;
340 struct old_ahash_alg ahash;
341 struct compress_alg compress;
345 int (*cra_init)(struct crypto_tfm *tfm);
346 void (*cra_exit)(struct crypto_tfm *tfm);
347 void (*cra_destroy)(struct crypto_alg *alg);
349 struct module *cra_module;
353 * Algorithm registration interface.
355 int crypto_register_alg(struct crypto_alg *alg);
356 int crypto_unregister_alg(struct crypto_alg *alg);
359 * Algorithm query interface.
361 int crypto_has_alg(const char *name, u32 type, u32 mask);
364 * Transforms: user-instantiated objects which encapsulate algorithms
365 * and core processing logic. Managed via crypto_alloc_*() and
366 * crypto_free_*(), as well as the various helpers below.
369 struct ablkcipher_tfm {
370 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
371 unsigned int keylen);
372 int (*encrypt)(struct ablkcipher_request *req);
373 int (*decrypt)(struct ablkcipher_request *req);
374 int (*givencrypt)(struct skcipher_givcrypt_request *req);
375 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
377 struct crypto_ablkcipher *base;
380 unsigned int reqsize;
384 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
385 unsigned int keylen);
386 int (*encrypt)(struct aead_request *req);
387 int (*decrypt)(struct aead_request *req);
388 int (*givencrypt)(struct aead_givcrypt_request *req);
389 int (*givdecrypt)(struct aead_givcrypt_request *req);
391 struct crypto_aead *base;
394 unsigned int authsize;
395 unsigned int reqsize;
398 struct blkcipher_tfm {
400 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
401 unsigned int keylen);
402 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
403 struct scatterlist *src, unsigned int nbytes);
404 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
405 struct scatterlist *src, unsigned int nbytes);
409 int (*cit_setkey)(struct crypto_tfm *tfm,
410 const u8 *key, unsigned int keylen);
411 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
412 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
416 int (*init)(struct hash_desc *desc);
417 int (*update)(struct hash_desc *desc,
418 struct scatterlist *sg, unsigned int nsg);
419 int (*final)(struct hash_desc *desc, u8 *out);
420 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
421 unsigned int nsg, u8 *out);
422 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
423 unsigned int keylen);
424 unsigned int digestsize;
427 struct compress_tfm {
428 int (*cot_compress)(struct crypto_tfm *tfm,
429 const u8 *src, unsigned int slen,
430 u8 *dst, unsigned int *dlen);
431 int (*cot_decompress)(struct crypto_tfm *tfm,
432 const u8 *src, unsigned int slen,
433 u8 *dst, unsigned int *dlen);
437 int (*rng_gen_random)(struct crypto_rng *tfm, u8 *rdata,
439 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
442 #define crt_ablkcipher crt_u.ablkcipher
443 #define crt_aead crt_u.aead
444 #define crt_blkcipher crt_u.blkcipher
445 #define crt_cipher crt_u.cipher
446 #define crt_hash crt_u.hash
447 #define crt_compress crt_u.compress
448 #define crt_rng crt_u.rng
455 struct ablkcipher_tfm ablkcipher;
456 struct aead_tfm aead;
457 struct blkcipher_tfm blkcipher;
458 struct cipher_tfm cipher;
459 struct hash_tfm hash;
460 struct compress_tfm compress;
464 void (*exit)(struct crypto_tfm *tfm);
466 struct crypto_alg *__crt_alg;
468 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
471 struct crypto_ablkcipher {
472 struct crypto_tfm base;
476 struct crypto_tfm base;
479 struct crypto_blkcipher {
480 struct crypto_tfm base;
483 struct crypto_cipher {
484 struct crypto_tfm base;
488 struct crypto_tfm base;
492 struct crypto_tfm base;
496 struct crypto_tfm base;
507 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
509 /* Maximum number of (rtattr) parameters for each template. */
510 #define CRYPTO_MAX_ATTRS 32
512 struct crypto_attr_alg {
513 char name[CRYPTO_MAX_ALG_NAME];
516 struct crypto_attr_type {
521 struct crypto_attr_u32 {
526 * Transform user interface.
529 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
530 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
532 static inline void crypto_free_tfm(struct crypto_tfm *tfm)
534 return crypto_destroy_tfm(tfm, tfm);
537 int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
540 * Transform helpers which query the underlying algorithm.
542 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
544 return tfm->__crt_alg->cra_name;
547 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
549 return tfm->__crt_alg->cra_driver_name;
552 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
554 return tfm->__crt_alg->cra_priority;
557 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
559 return module_name(tfm->__crt_alg->cra_module);
562 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
564 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
567 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
569 return tfm->__crt_alg->cra_blocksize;
572 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
574 return tfm->__crt_alg->cra_alignmask;
577 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
579 return tfm->crt_flags;
582 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
584 tfm->crt_flags |= flags;
587 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
589 tfm->crt_flags &= ~flags;
592 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
594 return tfm->__crt_ctx;
597 static inline unsigned int crypto_tfm_ctx_alignment(void)
599 struct crypto_tfm *tfm;
600 return __alignof__(tfm->__crt_ctx);
606 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
607 struct crypto_tfm *tfm)
609 return (struct crypto_ablkcipher *)tfm;
612 static inline u32 crypto_skcipher_type(u32 type)
614 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
615 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
619 static inline u32 crypto_skcipher_mask(u32 mask)
621 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
622 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
626 struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
629 static inline struct crypto_tfm *crypto_ablkcipher_tfm(
630 struct crypto_ablkcipher *tfm)
635 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
637 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
640 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
643 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
644 crypto_skcipher_mask(mask));
647 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
648 struct crypto_ablkcipher *tfm)
650 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
653 static inline unsigned int crypto_ablkcipher_ivsize(
654 struct crypto_ablkcipher *tfm)
656 return crypto_ablkcipher_crt(tfm)->ivsize;
659 static inline unsigned int crypto_ablkcipher_blocksize(
660 struct crypto_ablkcipher *tfm)
662 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
665 static inline unsigned int crypto_ablkcipher_alignmask(
666 struct crypto_ablkcipher *tfm)
668 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
671 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
673 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
676 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
679 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
682 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
685 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
688 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
689 const u8 *key, unsigned int keylen)
691 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
693 return crt->setkey(crt->base, key, keylen);
696 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
697 struct ablkcipher_request *req)
699 return __crypto_ablkcipher_cast(req->base.tfm);
702 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
704 struct ablkcipher_tfm *crt =
705 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
706 return crt->encrypt(req);
709 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
711 struct ablkcipher_tfm *crt =
712 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
713 return crt->decrypt(req);
716 static inline unsigned int crypto_ablkcipher_reqsize(
717 struct crypto_ablkcipher *tfm)
719 return crypto_ablkcipher_crt(tfm)->reqsize;
722 static inline void ablkcipher_request_set_tfm(
723 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
725 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
728 static inline struct ablkcipher_request *ablkcipher_request_cast(
729 struct crypto_async_request *req)
731 return container_of(req, struct ablkcipher_request, base);
734 static inline struct ablkcipher_request *ablkcipher_request_alloc(
735 struct crypto_ablkcipher *tfm, gfp_t gfp)
737 struct ablkcipher_request *req;
739 req = kmalloc(sizeof(struct ablkcipher_request) +
740 crypto_ablkcipher_reqsize(tfm), gfp);
743 ablkcipher_request_set_tfm(req, tfm);
748 static inline void ablkcipher_request_free(struct ablkcipher_request *req)
753 static inline void ablkcipher_request_set_callback(
754 struct ablkcipher_request *req,
755 u32 flags, crypto_completion_t complete, void *data)
757 req->base.complete = complete;
758 req->base.data = data;
759 req->base.flags = flags;
762 static inline void ablkcipher_request_set_crypt(
763 struct ablkcipher_request *req,
764 struct scatterlist *src, struct scatterlist *dst,
765 unsigned int nbytes, void *iv)
769 req->nbytes = nbytes;
773 static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
775 return (struct crypto_aead *)tfm;
778 struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
780 static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
785 static inline void crypto_free_aead(struct crypto_aead *tfm)
787 crypto_free_tfm(crypto_aead_tfm(tfm));
790 static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
792 return &crypto_aead_tfm(tfm)->crt_aead;
795 static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
797 return crypto_aead_crt(tfm)->ivsize;
800 static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
802 return crypto_aead_crt(tfm)->authsize;
805 static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
807 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
810 static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
812 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
815 static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
817 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
820 static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
822 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
825 static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
827 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
830 static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
833 struct aead_tfm *crt = crypto_aead_crt(tfm);
835 return crt->setkey(crt->base, key, keylen);
838 int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
840 static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
842 return __crypto_aead_cast(req->base.tfm);
845 static inline int crypto_aead_encrypt(struct aead_request *req)
847 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
850 static inline int crypto_aead_decrypt(struct aead_request *req)
852 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
855 static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
857 return crypto_aead_crt(tfm)->reqsize;
860 static inline void aead_request_set_tfm(struct aead_request *req,
861 struct crypto_aead *tfm)
863 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
866 static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
869 struct aead_request *req;
871 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
874 aead_request_set_tfm(req, tfm);
879 static inline void aead_request_free(struct aead_request *req)
884 static inline void aead_request_set_callback(struct aead_request *req,
886 crypto_completion_t complete,
889 req->base.complete = complete;
890 req->base.data = data;
891 req->base.flags = flags;
894 static inline void aead_request_set_crypt(struct aead_request *req,
895 struct scatterlist *src,
896 struct scatterlist *dst,
897 unsigned int cryptlen, u8 *iv)
901 req->cryptlen = cryptlen;
905 static inline void aead_request_set_assoc(struct aead_request *req,
906 struct scatterlist *assoc,
907 unsigned int assoclen)
910 req->assoclen = assoclen;
913 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
914 struct crypto_tfm *tfm)
916 return (struct crypto_blkcipher *)tfm;
919 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
920 struct crypto_tfm *tfm)
922 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
923 return __crypto_blkcipher_cast(tfm);
926 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
927 const char *alg_name, u32 type, u32 mask)
929 type &= ~CRYPTO_ALG_TYPE_MASK;
930 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
931 mask |= CRYPTO_ALG_TYPE_MASK;
933 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
936 static inline struct crypto_tfm *crypto_blkcipher_tfm(
937 struct crypto_blkcipher *tfm)
942 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
944 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
947 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
949 type &= ~CRYPTO_ALG_TYPE_MASK;
950 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
951 mask |= CRYPTO_ALG_TYPE_MASK;
953 return crypto_has_alg(alg_name, type, mask);
956 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
958 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
961 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
962 struct crypto_blkcipher *tfm)
964 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
967 static inline struct blkcipher_alg *crypto_blkcipher_alg(
968 struct crypto_blkcipher *tfm)
970 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
973 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
975 return crypto_blkcipher_alg(tfm)->ivsize;
978 static inline unsigned int crypto_blkcipher_blocksize(
979 struct crypto_blkcipher *tfm)
981 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
984 static inline unsigned int crypto_blkcipher_alignmask(
985 struct crypto_blkcipher *tfm)
987 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
990 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
992 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
995 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
998 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1001 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1004 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1007 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1008 const u8 *key, unsigned int keylen)
1010 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1014 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1015 struct scatterlist *dst,
1016 struct scatterlist *src,
1017 unsigned int nbytes)
1019 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1020 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1023 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1024 struct scatterlist *dst,
1025 struct scatterlist *src,
1026 unsigned int nbytes)
1028 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1031 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1032 struct scatterlist *dst,
1033 struct scatterlist *src,
1034 unsigned int nbytes)
1036 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1037 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1040 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1041 struct scatterlist *dst,
1042 struct scatterlist *src,
1043 unsigned int nbytes)
1045 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1048 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1049 const u8 *src, unsigned int len)
1051 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1054 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1055 u8 *dst, unsigned int len)
1057 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1060 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1062 return (struct crypto_cipher *)tfm;
1065 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1067 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1068 return __crypto_cipher_cast(tfm);
1071 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1074 type &= ~CRYPTO_ALG_TYPE_MASK;
1075 type |= CRYPTO_ALG_TYPE_CIPHER;
1076 mask |= CRYPTO_ALG_TYPE_MASK;
1078 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1081 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1086 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1088 crypto_free_tfm(crypto_cipher_tfm(tfm));
1091 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1093 type &= ~CRYPTO_ALG_TYPE_MASK;
1094 type |= CRYPTO_ALG_TYPE_CIPHER;
1095 mask |= CRYPTO_ALG_TYPE_MASK;
1097 return crypto_has_alg(alg_name, type, mask);
1100 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1102 return &crypto_cipher_tfm(tfm)->crt_cipher;
1105 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1107 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1110 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1112 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1115 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1117 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1120 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1123 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1126 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1129 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1132 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1133 const u8 *key, unsigned int keylen)
1135 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1139 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1140 u8 *dst, const u8 *src)
1142 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1146 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1147 u8 *dst, const u8 *src)
1149 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1153 static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
1155 return (struct crypto_hash *)tfm;
1158 static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
1160 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1161 CRYPTO_ALG_TYPE_HASH_MASK);
1162 return __crypto_hash_cast(tfm);
1165 static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1168 type &= ~CRYPTO_ALG_TYPE_MASK;
1169 mask &= ~CRYPTO_ALG_TYPE_MASK;
1170 type |= CRYPTO_ALG_TYPE_HASH;
1171 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1173 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1176 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1181 static inline void crypto_free_hash(struct crypto_hash *tfm)
1183 crypto_free_tfm(crypto_hash_tfm(tfm));
1186 static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1188 type &= ~CRYPTO_ALG_TYPE_MASK;
1189 mask &= ~CRYPTO_ALG_TYPE_MASK;
1190 type |= CRYPTO_ALG_TYPE_HASH;
1191 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1193 return crypto_has_alg(alg_name, type, mask);
1196 static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1198 return &crypto_hash_tfm(tfm)->crt_hash;
1201 static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1203 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1206 static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1208 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1211 static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1213 return crypto_hash_crt(tfm)->digestsize;
1216 static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1218 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1221 static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1223 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1226 static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1228 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1231 static inline int crypto_hash_init(struct hash_desc *desc)
1233 return crypto_hash_crt(desc->tfm)->init(desc);
1236 static inline int crypto_hash_update(struct hash_desc *desc,
1237 struct scatterlist *sg,
1238 unsigned int nbytes)
1240 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1243 static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1245 return crypto_hash_crt(desc->tfm)->final(desc, out);
1248 static inline int crypto_hash_digest(struct hash_desc *desc,
1249 struct scatterlist *sg,
1250 unsigned int nbytes, u8 *out)
1252 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1255 static inline int crypto_hash_setkey(struct crypto_hash *hash,
1256 const u8 *key, unsigned int keylen)
1258 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
1261 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1263 return (struct crypto_comp *)tfm;
1266 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1268 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1269 CRYPTO_ALG_TYPE_MASK);
1270 return __crypto_comp_cast(tfm);
1273 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1276 type &= ~CRYPTO_ALG_TYPE_MASK;
1277 type |= CRYPTO_ALG_TYPE_COMPRESS;
1278 mask |= CRYPTO_ALG_TYPE_MASK;
1280 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1283 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1288 static inline void crypto_free_comp(struct crypto_comp *tfm)
1290 crypto_free_tfm(crypto_comp_tfm(tfm));
1293 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1295 type &= ~CRYPTO_ALG_TYPE_MASK;
1296 type |= CRYPTO_ALG_TYPE_COMPRESS;
1297 mask |= CRYPTO_ALG_TYPE_MASK;
1299 return crypto_has_alg(alg_name, type, mask);
1302 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1304 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1307 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1309 return &crypto_comp_tfm(tfm)->crt_compress;
1312 static inline int crypto_comp_compress(struct crypto_comp *tfm,
1313 const u8 *src, unsigned int slen,
1314 u8 *dst, unsigned int *dlen)
1316 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1317 src, slen, dst, dlen);
1320 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
1321 const u8 *src, unsigned int slen,
1322 u8 *dst, unsigned int *dlen)
1324 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1325 src, slen, dst, dlen);
1328 #endif /* _LINUX_CRYPTO_H */