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_DIGEST 0x00000002
34 #define CRYPTO_ALG_TYPE_HASH 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_COMPRESS 0x00000008
39 #define CRYPTO_ALG_TYPE_AEAD 0x00000009
41 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
42 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
44 #define CRYPTO_ALG_LARVAL 0x00000010
45 #define CRYPTO_ALG_DEAD 0x00000020
46 #define CRYPTO_ALG_DYING 0x00000040
47 #define CRYPTO_ALG_ASYNC 0x00000080
50 * Set this bit if and only if the algorithm requires another algorithm of
51 * the same type to handle corner cases.
53 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
56 * This bit is set for symmetric key ciphers that have already been wrapped
57 * with a generic IV generator to prevent them from being wrapped again.
59 #define CRYPTO_ALG_GENIV 0x00000200
62 * Transform masks and values (for crt_flags).
64 #define CRYPTO_TFM_REQ_MASK 0x000fff00
65 #define CRYPTO_TFM_RES_MASK 0xfff00000
67 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
68 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
69 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
70 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
71 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
72 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
73 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
74 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
77 * Miscellaneous stuff.
79 #define CRYPTO_MAX_ALG_NAME 64
82 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
83 * declaration) is used to ensure that the crypto_tfm context structure is
84 * aligned correctly for the given architecture so that there are no alignment
85 * faults for C data types. In particular, this is required on platforms such
86 * as arm where pointers are 32-bit aligned but there are data types such as
87 * u64 which require 64-bit alignment.
89 #if defined(ARCH_KMALLOC_MINALIGN)
90 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
91 #elif defined(ARCH_SLAB_MINALIGN)
92 #define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
95 #ifdef CRYPTO_MINALIGN
96 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
98 #define CRYPTO_MINALIGN_ATTR
102 struct crypto_ablkcipher;
103 struct crypto_async_request;
105 struct crypto_blkcipher;
109 struct skcipher_givcrypt_request;
111 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
113 struct crypto_async_request {
114 struct list_head list;
115 crypto_completion_t complete;
117 struct crypto_tfm *tfm;
122 struct ablkcipher_request {
123 struct crypto_async_request base;
129 struct scatterlist *src;
130 struct scatterlist *dst;
132 void *__ctx[] CRYPTO_MINALIGN_ATTR;
136 * struct aead_request - AEAD request
137 * @base: Common attributes for async crypto requests
138 * @assoclen: Length in bytes of associated data for authentication
139 * @cryptlen: Length of data to be encrypted or decrypted
140 * @iv: Initialisation vector
141 * @assoc: Associated data
143 * @dst: Destination data
144 * @__ctx: Start of private context data
146 struct aead_request {
147 struct crypto_async_request base;
149 unsigned int assoclen;
150 unsigned int cryptlen;
154 struct scatterlist *assoc;
155 struct scatterlist *src;
156 struct scatterlist *dst;
158 void *__ctx[] CRYPTO_MINALIGN_ATTR;
161 struct blkcipher_desc {
162 struct crypto_blkcipher *tfm;
168 struct crypto_tfm *tfm;
169 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
170 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
171 const u8 *src, unsigned int nbytes);
176 struct crypto_hash *tfm;
181 * Algorithms: modular crypto algorithm implementations, managed
182 * via crypto_register_alg() and crypto_unregister_alg().
184 struct ablkcipher_alg {
185 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
186 unsigned int keylen);
187 int (*encrypt)(struct ablkcipher_request *req);
188 int (*decrypt)(struct ablkcipher_request *req);
189 int (*givencrypt)(struct skcipher_givcrypt_request *req);
190 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
194 unsigned int min_keysize;
195 unsigned int max_keysize;
200 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
201 unsigned int keylen);
202 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
203 int (*encrypt)(struct aead_request *req);
204 int (*decrypt)(struct aead_request *req);
207 unsigned int maxauthsize;
210 struct blkcipher_alg {
211 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
212 unsigned int keylen);
213 int (*encrypt)(struct blkcipher_desc *desc,
214 struct scatterlist *dst, struct scatterlist *src,
215 unsigned int nbytes);
216 int (*decrypt)(struct blkcipher_desc *desc,
217 struct scatterlist *dst, struct scatterlist *src,
218 unsigned int nbytes);
222 unsigned int min_keysize;
223 unsigned int max_keysize;
228 unsigned int cia_min_keysize;
229 unsigned int cia_max_keysize;
230 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
231 unsigned int keylen);
232 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
233 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
237 unsigned int dia_digestsize;
238 void (*dia_init)(struct crypto_tfm *tfm);
239 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
241 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
242 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
243 unsigned int keylen);
247 int (*init)(struct hash_desc *desc);
248 int (*update)(struct hash_desc *desc, struct scatterlist *sg,
249 unsigned int nbytes);
250 int (*final)(struct hash_desc *desc, u8 *out);
251 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
252 unsigned int nbytes, u8 *out);
253 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
254 unsigned int keylen);
256 unsigned int digestsize;
259 struct compress_alg {
260 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
261 unsigned int slen, u8 *dst, unsigned int *dlen);
262 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
263 unsigned int slen, u8 *dst, unsigned int *dlen);
266 #define cra_ablkcipher cra_u.ablkcipher
267 #define cra_aead cra_u.aead
268 #define cra_blkcipher cra_u.blkcipher
269 #define cra_cipher cra_u.cipher
270 #define cra_digest cra_u.digest
271 #define cra_hash cra_u.hash
272 #define cra_compress cra_u.compress
275 struct list_head cra_list;
276 struct list_head cra_users;
279 unsigned int cra_blocksize;
280 unsigned int cra_ctxsize;
281 unsigned int cra_alignmask;
286 char cra_name[CRYPTO_MAX_ALG_NAME];
287 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
289 const struct crypto_type *cra_type;
292 struct ablkcipher_alg ablkcipher;
293 struct aead_alg aead;
294 struct blkcipher_alg blkcipher;
295 struct cipher_alg cipher;
296 struct digest_alg digest;
297 struct hash_alg hash;
298 struct compress_alg compress;
301 int (*cra_init)(struct crypto_tfm *tfm);
302 void (*cra_exit)(struct crypto_tfm *tfm);
303 void (*cra_destroy)(struct crypto_alg *alg);
305 struct module *cra_module;
309 * Algorithm registration interface.
311 int crypto_register_alg(struct crypto_alg *alg);
312 int crypto_unregister_alg(struct crypto_alg *alg);
315 * Algorithm query interface.
318 int crypto_has_alg(const char *name, u32 type, u32 mask);
320 static inline int crypto_has_alg(const char *name, u32 type, u32 mask)
327 * Transforms: user-instantiated objects which encapsulate algorithms
328 * and core processing logic. Managed via crypto_alloc_*() and
329 * crypto_free_*(), as well as the various helpers below.
332 struct ablkcipher_tfm {
333 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
334 unsigned int keylen);
335 int (*encrypt)(struct ablkcipher_request *req);
336 int (*decrypt)(struct ablkcipher_request *req);
337 int (*givencrypt)(struct skcipher_givcrypt_request *req);
338 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
340 struct crypto_ablkcipher *base;
343 unsigned int reqsize;
347 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
348 unsigned int keylen);
349 int (*encrypt)(struct aead_request *req);
350 int (*decrypt)(struct aead_request *req);
352 unsigned int authsize;
353 unsigned int reqsize;
356 struct blkcipher_tfm {
358 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
359 unsigned int keylen);
360 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
361 struct scatterlist *src, unsigned int nbytes);
362 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
363 struct scatterlist *src, unsigned int nbytes);
367 int (*cit_setkey)(struct crypto_tfm *tfm,
368 const u8 *key, unsigned int keylen);
369 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
370 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
374 int (*init)(struct hash_desc *desc);
375 int (*update)(struct hash_desc *desc,
376 struct scatterlist *sg, unsigned int nsg);
377 int (*final)(struct hash_desc *desc, u8 *out);
378 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
379 unsigned int nsg, u8 *out);
380 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
381 unsigned int keylen);
382 unsigned int digestsize;
385 struct compress_tfm {
386 int (*cot_compress)(struct crypto_tfm *tfm,
387 const u8 *src, unsigned int slen,
388 u8 *dst, unsigned int *dlen);
389 int (*cot_decompress)(struct crypto_tfm *tfm,
390 const u8 *src, unsigned int slen,
391 u8 *dst, unsigned int *dlen);
394 #define crt_ablkcipher crt_u.ablkcipher
395 #define crt_aead crt_u.aead
396 #define crt_blkcipher crt_u.blkcipher
397 #define crt_cipher crt_u.cipher
398 #define crt_hash crt_u.hash
399 #define crt_compress crt_u.compress
406 struct ablkcipher_tfm ablkcipher;
407 struct aead_tfm aead;
408 struct blkcipher_tfm blkcipher;
409 struct cipher_tfm cipher;
410 struct hash_tfm hash;
411 struct compress_tfm compress;
414 struct crypto_alg *__crt_alg;
416 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
419 struct crypto_ablkcipher {
420 struct crypto_tfm base;
424 struct crypto_tfm base;
427 struct crypto_blkcipher {
428 struct crypto_tfm base;
431 struct crypto_cipher {
432 struct crypto_tfm base;
436 struct crypto_tfm base;
440 struct crypto_tfm base;
451 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
453 /* Maximum number of (rtattr) parameters for each template. */
454 #define CRYPTO_MAX_ATTRS 32
456 struct crypto_attr_alg {
457 char name[CRYPTO_MAX_ALG_NAME];
460 struct crypto_attr_type {
465 struct crypto_attr_u32 {
470 * Transform user interface.
473 struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
474 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
475 void crypto_free_tfm(struct crypto_tfm *tfm);
478 * Transform helpers which query the underlying algorithm.
480 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
482 return tfm->__crt_alg->cra_name;
485 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
487 return tfm->__crt_alg->cra_driver_name;
490 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
492 return tfm->__crt_alg->cra_priority;
495 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
497 return module_name(tfm->__crt_alg->cra_module);
500 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
502 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
505 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
507 return tfm->__crt_alg->cra_blocksize;
510 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
512 return tfm->__crt_alg->cra_alignmask;
515 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
517 return tfm->crt_flags;
520 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
522 tfm->crt_flags |= flags;
525 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
527 tfm->crt_flags &= ~flags;
530 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
532 return tfm->__crt_ctx;
535 static inline unsigned int crypto_tfm_ctx_alignment(void)
537 struct crypto_tfm *tfm;
538 return __alignof__(tfm->__crt_ctx);
544 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
545 struct crypto_tfm *tfm)
547 return (struct crypto_ablkcipher *)tfm;
550 static inline u32 crypto_skcipher_type(u32 type)
552 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
553 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
557 static inline u32 crypto_skcipher_mask(u32 mask)
559 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
560 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
564 static inline struct crypto_ablkcipher *crypto_alloc_ablkcipher(
565 const char *alg_name, u32 type, u32 mask)
567 return __crypto_ablkcipher_cast(
568 crypto_alloc_base(alg_name, crypto_skcipher_type(type),
569 crypto_skcipher_mask(mask)));
572 static inline struct crypto_tfm *crypto_ablkcipher_tfm(
573 struct crypto_ablkcipher *tfm)
578 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
580 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
583 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
586 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
587 crypto_skcipher_mask(mask));
590 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
591 struct crypto_ablkcipher *tfm)
593 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
596 static inline unsigned int crypto_ablkcipher_ivsize(
597 struct crypto_ablkcipher *tfm)
599 return crypto_ablkcipher_crt(tfm)->ivsize;
602 static inline unsigned int crypto_ablkcipher_blocksize(
603 struct crypto_ablkcipher *tfm)
605 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
608 static inline unsigned int crypto_ablkcipher_alignmask(
609 struct crypto_ablkcipher *tfm)
611 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
614 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
616 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
619 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
622 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
625 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
628 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
631 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
632 const u8 *key, unsigned int keylen)
634 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
636 return crt->setkey(crt->base, key, keylen);
639 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
640 struct ablkcipher_request *req)
642 return __crypto_ablkcipher_cast(req->base.tfm);
645 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
647 struct ablkcipher_tfm *crt =
648 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
649 return crt->encrypt(req);
652 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
654 struct ablkcipher_tfm *crt =
655 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
656 return crt->decrypt(req);
659 static inline unsigned int crypto_ablkcipher_reqsize(
660 struct crypto_ablkcipher *tfm)
662 return crypto_ablkcipher_crt(tfm)->reqsize;
665 static inline void ablkcipher_request_set_tfm(
666 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
668 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
671 static inline struct ablkcipher_request *ablkcipher_request_cast(
672 struct crypto_async_request *req)
674 return container_of(req, struct ablkcipher_request, base);
677 static inline struct ablkcipher_request *ablkcipher_request_alloc(
678 struct crypto_ablkcipher *tfm, gfp_t gfp)
680 struct ablkcipher_request *req;
682 req = kmalloc(sizeof(struct ablkcipher_request) +
683 crypto_ablkcipher_reqsize(tfm), gfp);
686 ablkcipher_request_set_tfm(req, tfm);
691 static inline void ablkcipher_request_free(struct ablkcipher_request *req)
696 static inline void ablkcipher_request_set_callback(
697 struct ablkcipher_request *req,
698 u32 flags, crypto_completion_t complete, void *data)
700 req->base.complete = complete;
701 req->base.data = data;
702 req->base.flags = flags;
705 static inline void ablkcipher_request_set_crypt(
706 struct ablkcipher_request *req,
707 struct scatterlist *src, struct scatterlist *dst,
708 unsigned int nbytes, void *iv)
712 req->nbytes = nbytes;
716 static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
718 return (struct crypto_aead *)tfm;
721 static inline struct crypto_aead *crypto_alloc_aead(const char *alg_name,
724 type &= ~CRYPTO_ALG_TYPE_MASK;
725 type |= CRYPTO_ALG_TYPE_AEAD;
726 mask |= CRYPTO_ALG_TYPE_MASK;
728 return __crypto_aead_cast(crypto_alloc_base(alg_name, type, mask));
731 static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
736 static inline void crypto_free_aead(struct crypto_aead *tfm)
738 crypto_free_tfm(crypto_aead_tfm(tfm));
741 static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
743 return &crypto_aead_tfm(tfm)->crt_aead;
746 static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
748 return crypto_aead_crt(tfm)->ivsize;
751 static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
753 return crypto_aead_crt(tfm)->authsize;
756 static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
758 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
761 static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
763 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
766 static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
768 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
771 static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
773 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
776 static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
778 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
781 static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
784 return crypto_aead_crt(tfm)->setkey(tfm, key, keylen);
787 int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
789 static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
791 return __crypto_aead_cast(req->base.tfm);
794 static inline int crypto_aead_encrypt(struct aead_request *req)
796 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
799 static inline int crypto_aead_decrypt(struct aead_request *req)
801 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
804 static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
806 return crypto_aead_crt(tfm)->reqsize;
809 static inline void aead_request_set_tfm(struct aead_request *req,
810 struct crypto_aead *tfm)
812 req->base.tfm = crypto_aead_tfm(tfm);
815 static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
818 struct aead_request *req;
820 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
823 aead_request_set_tfm(req, tfm);
828 static inline void aead_request_free(struct aead_request *req)
833 static inline void aead_request_set_callback(struct aead_request *req,
835 crypto_completion_t complete,
838 req->base.complete = complete;
839 req->base.data = data;
840 req->base.flags = flags;
843 static inline void aead_request_set_crypt(struct aead_request *req,
844 struct scatterlist *src,
845 struct scatterlist *dst,
846 unsigned int cryptlen, u8 *iv)
850 req->cryptlen = cryptlen;
854 static inline void aead_request_set_assoc(struct aead_request *req,
855 struct scatterlist *assoc,
856 unsigned int assoclen)
859 req->assoclen = assoclen;
862 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
863 struct crypto_tfm *tfm)
865 return (struct crypto_blkcipher *)tfm;
868 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
869 struct crypto_tfm *tfm)
871 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
872 return __crypto_blkcipher_cast(tfm);
875 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
876 const char *alg_name, u32 type, u32 mask)
878 type &= ~CRYPTO_ALG_TYPE_MASK;
879 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
880 mask |= CRYPTO_ALG_TYPE_MASK;
882 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
885 static inline struct crypto_tfm *crypto_blkcipher_tfm(
886 struct crypto_blkcipher *tfm)
891 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
893 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
896 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
898 type &= ~CRYPTO_ALG_TYPE_MASK;
899 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
900 mask |= CRYPTO_ALG_TYPE_MASK;
902 return crypto_has_alg(alg_name, type, mask);
905 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
907 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
910 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
911 struct crypto_blkcipher *tfm)
913 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
916 static inline struct blkcipher_alg *crypto_blkcipher_alg(
917 struct crypto_blkcipher *tfm)
919 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
922 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
924 return crypto_blkcipher_alg(tfm)->ivsize;
927 static inline unsigned int crypto_blkcipher_blocksize(
928 struct crypto_blkcipher *tfm)
930 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
933 static inline unsigned int crypto_blkcipher_alignmask(
934 struct crypto_blkcipher *tfm)
936 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
939 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
941 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
944 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
947 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
950 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
953 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
956 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
957 const u8 *key, unsigned int keylen)
959 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
963 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
964 struct scatterlist *dst,
965 struct scatterlist *src,
968 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
969 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
972 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
973 struct scatterlist *dst,
974 struct scatterlist *src,
977 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
980 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
981 struct scatterlist *dst,
982 struct scatterlist *src,
985 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
986 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
989 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
990 struct scatterlist *dst,
991 struct scatterlist *src,
994 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
997 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
998 const u8 *src, unsigned int len)
1000 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1003 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1004 u8 *dst, unsigned int len)
1006 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1009 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1011 return (struct crypto_cipher *)tfm;
1014 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1016 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1017 return __crypto_cipher_cast(tfm);
1020 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1023 type &= ~CRYPTO_ALG_TYPE_MASK;
1024 type |= CRYPTO_ALG_TYPE_CIPHER;
1025 mask |= CRYPTO_ALG_TYPE_MASK;
1027 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1030 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1035 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1037 crypto_free_tfm(crypto_cipher_tfm(tfm));
1040 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1042 type &= ~CRYPTO_ALG_TYPE_MASK;
1043 type |= CRYPTO_ALG_TYPE_CIPHER;
1044 mask |= CRYPTO_ALG_TYPE_MASK;
1046 return crypto_has_alg(alg_name, type, mask);
1049 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1051 return &crypto_cipher_tfm(tfm)->crt_cipher;
1054 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1056 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1059 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1061 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1064 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1066 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1069 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1072 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1075 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1078 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1081 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1082 const u8 *key, unsigned int keylen)
1084 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1088 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1089 u8 *dst, const u8 *src)
1091 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1095 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1096 u8 *dst, const u8 *src)
1098 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1102 static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
1104 return (struct crypto_hash *)tfm;
1107 static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
1109 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1110 CRYPTO_ALG_TYPE_HASH_MASK);
1111 return __crypto_hash_cast(tfm);
1114 static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1117 type &= ~CRYPTO_ALG_TYPE_MASK;
1118 mask &= ~CRYPTO_ALG_TYPE_MASK;
1119 type |= CRYPTO_ALG_TYPE_HASH;
1120 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1122 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1125 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1130 static inline void crypto_free_hash(struct crypto_hash *tfm)
1132 crypto_free_tfm(crypto_hash_tfm(tfm));
1135 static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1137 type &= ~CRYPTO_ALG_TYPE_MASK;
1138 mask &= ~CRYPTO_ALG_TYPE_MASK;
1139 type |= CRYPTO_ALG_TYPE_HASH;
1140 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1142 return crypto_has_alg(alg_name, type, mask);
1145 static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1147 return &crypto_hash_tfm(tfm)->crt_hash;
1150 static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1152 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1155 static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1157 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1160 static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1162 return crypto_hash_crt(tfm)->digestsize;
1165 static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1167 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1170 static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1172 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1175 static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1177 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1180 static inline int crypto_hash_init(struct hash_desc *desc)
1182 return crypto_hash_crt(desc->tfm)->init(desc);
1185 static inline int crypto_hash_update(struct hash_desc *desc,
1186 struct scatterlist *sg,
1187 unsigned int nbytes)
1189 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1192 static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1194 return crypto_hash_crt(desc->tfm)->final(desc, out);
1197 static inline int crypto_hash_digest(struct hash_desc *desc,
1198 struct scatterlist *sg,
1199 unsigned int nbytes, u8 *out)
1201 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1204 static inline int crypto_hash_setkey(struct crypto_hash *hash,
1205 const u8 *key, unsigned int keylen)
1207 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
1210 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1212 return (struct crypto_comp *)tfm;
1215 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1217 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1218 CRYPTO_ALG_TYPE_MASK);
1219 return __crypto_comp_cast(tfm);
1222 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1225 type &= ~CRYPTO_ALG_TYPE_MASK;
1226 type |= CRYPTO_ALG_TYPE_COMPRESS;
1227 mask |= CRYPTO_ALG_TYPE_MASK;
1229 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1232 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1237 static inline void crypto_free_comp(struct crypto_comp *tfm)
1239 crypto_free_tfm(crypto_comp_tfm(tfm));
1242 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1244 type &= ~CRYPTO_ALG_TYPE_MASK;
1245 type |= CRYPTO_ALG_TYPE_COMPRESS;
1246 mask |= CRYPTO_ALG_TYPE_MASK;
1248 return crypto_has_alg(alg_name, type, mask);
1251 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1253 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1256 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1258 return &crypto_comp_tfm(tfm)->crt_compress;
1261 static inline int crypto_comp_compress(struct crypto_comp *tfm,
1262 const u8 *src, unsigned int slen,
1263 u8 *dst, unsigned int *dlen)
1265 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1266 src, slen, dst, dlen);
1269 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
1270 const u8 *src, unsigned int slen,
1271 u8 *dst, unsigned int *dlen)
1273 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1274 src, slen, dst, dlen);
1277 #endif /* _LINUX_CRYPTO_H */