2 * Scatterlist Cryptographic API.
9 * and Nettle, by Niels Möller.
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
17 #ifndef _LINUX_CRYPTO_H
18 #define _LINUX_CRYPTO_H
20 #include <linux/atomic.h>
21 #include <linux/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 * Set if the algorithm is an instance that is build from templates.
77 #define CRYPTO_ALG_INSTANCE 0x00000800
80 * Transform masks and values (for crt_flags).
82 #define CRYPTO_TFM_REQ_MASK 0x000fff00
83 #define CRYPTO_TFM_RES_MASK 0xfff00000
85 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
86 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
87 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
88 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
89 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
90 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
91 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
92 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
95 * Miscellaneous stuff.
97 #define CRYPTO_MAX_ALG_NAME 64
100 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
101 * declaration) is used to ensure that the crypto_tfm context structure is
102 * aligned correctly for the given architecture so that there are no alignment
103 * faults for C data types. In particular, this is required on platforms such
104 * as arm where pointers are 32-bit aligned but there are data types such as
105 * u64 which require 64-bit alignment.
107 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
109 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
112 struct crypto_ablkcipher;
113 struct crypto_async_request;
115 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;
148 * struct aead_request - AEAD request
149 * @base: Common attributes for async crypto requests
150 * @assoclen: Length in bytes of associated data for authentication
151 * @cryptlen: Length of data to be encrypted or decrypted
152 * @iv: Initialisation vector
153 * @assoc: Associated data
155 * @dst: Destination data
156 * @__ctx: Start of private context data
158 struct aead_request {
159 struct crypto_async_request base;
161 unsigned int assoclen;
162 unsigned int cryptlen;
166 struct scatterlist *assoc;
167 struct scatterlist *src;
168 struct scatterlist *dst;
170 void *__ctx[] CRYPTO_MINALIGN_ATTR;
173 struct blkcipher_desc {
174 struct crypto_blkcipher *tfm;
180 struct crypto_tfm *tfm;
181 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
182 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
183 const u8 *src, unsigned int nbytes);
188 struct crypto_hash *tfm;
193 * Algorithms: modular crypto algorithm implementations, managed
194 * via crypto_register_alg() and crypto_unregister_alg().
196 struct ablkcipher_alg {
197 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
198 unsigned int keylen);
199 int (*encrypt)(struct ablkcipher_request *req);
200 int (*decrypt)(struct ablkcipher_request *req);
201 int (*givencrypt)(struct skcipher_givcrypt_request *req);
202 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
206 unsigned int min_keysize;
207 unsigned int max_keysize;
212 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
213 unsigned int keylen);
214 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
215 int (*encrypt)(struct aead_request *req);
216 int (*decrypt)(struct aead_request *req);
217 int (*givencrypt)(struct aead_givcrypt_request *req);
218 int (*givdecrypt)(struct aead_givcrypt_request *req);
223 unsigned int maxauthsize;
226 struct blkcipher_alg {
227 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
228 unsigned int keylen);
229 int (*encrypt)(struct blkcipher_desc *desc,
230 struct scatterlist *dst, struct scatterlist *src,
231 unsigned int nbytes);
232 int (*decrypt)(struct blkcipher_desc *desc,
233 struct scatterlist *dst, struct scatterlist *src,
234 unsigned int nbytes);
238 unsigned int min_keysize;
239 unsigned int max_keysize;
244 unsigned int cia_min_keysize;
245 unsigned int cia_max_keysize;
246 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
247 unsigned int keylen);
248 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
249 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
252 struct compress_alg {
253 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
254 unsigned int slen, u8 *dst, unsigned int *dlen);
255 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
256 unsigned int slen, u8 *dst, unsigned int *dlen);
260 int (*rng_make_random)(struct crypto_rng *tfm, u8 *rdata,
262 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
264 unsigned int seedsize;
268 #define cra_ablkcipher cra_u.ablkcipher
269 #define cra_aead cra_u.aead
270 #define cra_blkcipher cra_u.blkcipher
271 #define cra_cipher cra_u.cipher
272 #define cra_compress cra_u.compress
273 #define cra_rng cra_u.rng
276 struct list_head cra_list;
277 struct list_head cra_users;
280 unsigned int cra_blocksize;
281 unsigned int cra_ctxsize;
282 unsigned int cra_alignmask;
287 char cra_name[CRYPTO_MAX_ALG_NAME];
288 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
290 const struct crypto_type *cra_type;
293 struct ablkcipher_alg ablkcipher;
294 struct aead_alg aead;
295 struct blkcipher_alg blkcipher;
296 struct cipher_alg cipher;
297 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.
317 int crypto_has_alg(const char *name, u32 type, u32 mask);
320 * Transforms: user-instantiated objects which encapsulate algorithms
321 * and core processing logic. Managed via crypto_alloc_*() and
322 * crypto_free_*(), as well as the various helpers below.
325 struct ablkcipher_tfm {
326 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
327 unsigned int keylen);
328 int (*encrypt)(struct ablkcipher_request *req);
329 int (*decrypt)(struct ablkcipher_request *req);
330 int (*givencrypt)(struct skcipher_givcrypt_request *req);
331 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
333 struct crypto_ablkcipher *base;
336 unsigned int reqsize;
340 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
341 unsigned int keylen);
342 int (*encrypt)(struct aead_request *req);
343 int (*decrypt)(struct aead_request *req);
344 int (*givencrypt)(struct aead_givcrypt_request *req);
345 int (*givdecrypt)(struct aead_givcrypt_request *req);
347 struct crypto_aead *base;
350 unsigned int authsize;
351 unsigned int reqsize;
354 struct blkcipher_tfm {
356 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
357 unsigned int keylen);
358 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
359 struct scatterlist *src, unsigned int nbytes);
360 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
361 struct scatterlist *src, unsigned int nbytes);
365 int (*cit_setkey)(struct crypto_tfm *tfm,
366 const u8 *key, unsigned int keylen);
367 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
368 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
372 int (*init)(struct hash_desc *desc);
373 int (*update)(struct hash_desc *desc,
374 struct scatterlist *sg, unsigned int nsg);
375 int (*final)(struct hash_desc *desc, u8 *out);
376 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
377 unsigned int nsg, u8 *out);
378 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
379 unsigned int keylen);
380 unsigned int digestsize;
383 struct compress_tfm {
384 int (*cot_compress)(struct crypto_tfm *tfm,
385 const u8 *src, unsigned int slen,
386 u8 *dst, unsigned int *dlen);
387 int (*cot_decompress)(struct crypto_tfm *tfm,
388 const u8 *src, unsigned int slen,
389 u8 *dst, unsigned int *dlen);
393 int (*rng_gen_random)(struct crypto_rng *tfm, u8 *rdata,
395 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
398 #define crt_ablkcipher crt_u.ablkcipher
399 #define crt_aead crt_u.aead
400 #define crt_blkcipher crt_u.blkcipher
401 #define crt_cipher crt_u.cipher
402 #define crt_hash crt_u.hash
403 #define crt_compress crt_u.compress
404 #define crt_rng crt_u.rng
411 struct ablkcipher_tfm ablkcipher;
412 struct aead_tfm aead;
413 struct blkcipher_tfm blkcipher;
414 struct cipher_tfm cipher;
415 struct hash_tfm hash;
416 struct compress_tfm compress;
420 void (*exit)(struct crypto_tfm *tfm);
422 struct crypto_alg *__crt_alg;
424 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
427 struct crypto_ablkcipher {
428 struct crypto_tfm base;
432 struct crypto_tfm base;
435 struct crypto_blkcipher {
436 struct crypto_tfm base;
439 struct crypto_cipher {
440 struct crypto_tfm base;
444 struct crypto_tfm base;
448 struct crypto_tfm base;
452 struct crypto_tfm base;
463 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
465 /* Maximum number of (rtattr) parameters for each template. */
466 #define CRYPTO_MAX_ATTRS 32
468 struct crypto_attr_alg {
469 char name[CRYPTO_MAX_ALG_NAME];
472 struct crypto_attr_type {
477 struct crypto_attr_u32 {
482 * Transform user interface.
485 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
486 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
488 static inline void crypto_free_tfm(struct crypto_tfm *tfm)
490 return crypto_destroy_tfm(tfm, tfm);
493 int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
496 * Transform helpers which query the underlying algorithm.
498 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
500 return tfm->__crt_alg->cra_name;
503 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
505 return tfm->__crt_alg->cra_driver_name;
508 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
510 return tfm->__crt_alg->cra_priority;
513 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
515 return module_name(tfm->__crt_alg->cra_module);
518 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
520 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
523 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
525 return tfm->__crt_alg->cra_blocksize;
528 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
530 return tfm->__crt_alg->cra_alignmask;
533 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
535 return tfm->crt_flags;
538 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
540 tfm->crt_flags |= flags;
543 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
545 tfm->crt_flags &= ~flags;
548 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
550 return tfm->__crt_ctx;
553 static inline unsigned int crypto_tfm_ctx_alignment(void)
555 struct crypto_tfm *tfm;
556 return __alignof__(tfm->__crt_ctx);
562 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
563 struct crypto_tfm *tfm)
565 return (struct crypto_ablkcipher *)tfm;
568 static inline u32 crypto_skcipher_type(u32 type)
570 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
571 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
575 static inline u32 crypto_skcipher_mask(u32 mask)
577 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
578 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
582 struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
585 static inline struct crypto_tfm *crypto_ablkcipher_tfm(
586 struct crypto_ablkcipher *tfm)
591 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
593 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
596 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
599 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
600 crypto_skcipher_mask(mask));
603 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
604 struct crypto_ablkcipher *tfm)
606 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
609 static inline unsigned int crypto_ablkcipher_ivsize(
610 struct crypto_ablkcipher *tfm)
612 return crypto_ablkcipher_crt(tfm)->ivsize;
615 static inline unsigned int crypto_ablkcipher_blocksize(
616 struct crypto_ablkcipher *tfm)
618 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
621 static inline unsigned int crypto_ablkcipher_alignmask(
622 struct crypto_ablkcipher *tfm)
624 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
627 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
629 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
632 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
635 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
638 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
641 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
644 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
645 const u8 *key, unsigned int keylen)
647 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
649 return crt->setkey(crt->base, key, keylen);
652 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
653 struct ablkcipher_request *req)
655 return __crypto_ablkcipher_cast(req->base.tfm);
658 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
660 struct ablkcipher_tfm *crt =
661 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
662 return crt->encrypt(req);
665 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
667 struct ablkcipher_tfm *crt =
668 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
669 return crt->decrypt(req);
672 static inline unsigned int crypto_ablkcipher_reqsize(
673 struct crypto_ablkcipher *tfm)
675 return crypto_ablkcipher_crt(tfm)->reqsize;
678 static inline void ablkcipher_request_set_tfm(
679 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
681 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
684 static inline struct ablkcipher_request *ablkcipher_request_cast(
685 struct crypto_async_request *req)
687 return container_of(req, struct ablkcipher_request, base);
690 static inline struct ablkcipher_request *ablkcipher_request_alloc(
691 struct crypto_ablkcipher *tfm, gfp_t gfp)
693 struct ablkcipher_request *req;
695 req = kmalloc(sizeof(struct ablkcipher_request) +
696 crypto_ablkcipher_reqsize(tfm), gfp);
699 ablkcipher_request_set_tfm(req, tfm);
704 static inline void ablkcipher_request_free(struct ablkcipher_request *req)
709 static inline void ablkcipher_request_set_callback(
710 struct ablkcipher_request *req,
711 u32 flags, crypto_completion_t complete, void *data)
713 req->base.complete = complete;
714 req->base.data = data;
715 req->base.flags = flags;
718 static inline void ablkcipher_request_set_crypt(
719 struct ablkcipher_request *req,
720 struct scatterlist *src, struct scatterlist *dst,
721 unsigned int nbytes, void *iv)
725 req->nbytes = nbytes;
729 static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
731 return (struct crypto_aead *)tfm;
734 struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
736 static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
741 static inline void crypto_free_aead(struct crypto_aead *tfm)
743 crypto_free_tfm(crypto_aead_tfm(tfm));
746 static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
748 return &crypto_aead_tfm(tfm)->crt_aead;
751 static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
753 return crypto_aead_crt(tfm)->ivsize;
756 static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
758 return crypto_aead_crt(tfm)->authsize;
761 static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
763 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
766 static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
768 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
771 static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
773 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
776 static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
778 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
781 static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
783 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
786 static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
789 struct aead_tfm *crt = crypto_aead_crt(tfm);
791 return crt->setkey(crt->base, key, keylen);
794 int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
796 static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
798 return __crypto_aead_cast(req->base.tfm);
801 static inline int crypto_aead_encrypt(struct aead_request *req)
803 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
806 static inline int crypto_aead_decrypt(struct aead_request *req)
808 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
811 static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
813 return crypto_aead_crt(tfm)->reqsize;
816 static inline void aead_request_set_tfm(struct aead_request *req,
817 struct crypto_aead *tfm)
819 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
822 static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
825 struct aead_request *req;
827 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
830 aead_request_set_tfm(req, tfm);
835 static inline void aead_request_free(struct aead_request *req)
840 static inline void aead_request_set_callback(struct aead_request *req,
842 crypto_completion_t complete,
845 req->base.complete = complete;
846 req->base.data = data;
847 req->base.flags = flags;
850 static inline void aead_request_set_crypt(struct aead_request *req,
851 struct scatterlist *src,
852 struct scatterlist *dst,
853 unsigned int cryptlen, u8 *iv)
857 req->cryptlen = cryptlen;
861 static inline void aead_request_set_assoc(struct aead_request *req,
862 struct scatterlist *assoc,
863 unsigned int assoclen)
866 req->assoclen = assoclen;
869 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
870 struct crypto_tfm *tfm)
872 return (struct crypto_blkcipher *)tfm;
875 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
876 struct crypto_tfm *tfm)
878 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
879 return __crypto_blkcipher_cast(tfm);
882 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
883 const char *alg_name, u32 type, u32 mask)
885 type &= ~CRYPTO_ALG_TYPE_MASK;
886 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
887 mask |= CRYPTO_ALG_TYPE_MASK;
889 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
892 static inline struct crypto_tfm *crypto_blkcipher_tfm(
893 struct crypto_blkcipher *tfm)
898 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
900 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
903 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
905 type &= ~CRYPTO_ALG_TYPE_MASK;
906 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
907 mask |= CRYPTO_ALG_TYPE_MASK;
909 return crypto_has_alg(alg_name, type, mask);
912 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
914 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
917 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
918 struct crypto_blkcipher *tfm)
920 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
923 static inline struct blkcipher_alg *crypto_blkcipher_alg(
924 struct crypto_blkcipher *tfm)
926 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
929 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
931 return crypto_blkcipher_alg(tfm)->ivsize;
934 static inline unsigned int crypto_blkcipher_blocksize(
935 struct crypto_blkcipher *tfm)
937 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
940 static inline unsigned int crypto_blkcipher_alignmask(
941 struct crypto_blkcipher *tfm)
943 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
946 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
948 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
951 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
954 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
957 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
960 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
963 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
964 const u8 *key, unsigned int keylen)
966 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
970 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
971 struct scatterlist *dst,
972 struct scatterlist *src,
975 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
976 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
979 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
980 struct scatterlist *dst,
981 struct scatterlist *src,
984 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
987 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
988 struct scatterlist *dst,
989 struct scatterlist *src,
992 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
993 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
996 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
997 struct scatterlist *dst,
998 struct scatterlist *src,
1001 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1004 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1005 const u8 *src, unsigned int len)
1007 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1010 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1011 u8 *dst, unsigned int len)
1013 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1016 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1018 return (struct crypto_cipher *)tfm;
1021 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1023 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1024 return __crypto_cipher_cast(tfm);
1027 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1030 type &= ~CRYPTO_ALG_TYPE_MASK;
1031 type |= CRYPTO_ALG_TYPE_CIPHER;
1032 mask |= CRYPTO_ALG_TYPE_MASK;
1034 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1037 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1042 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1044 crypto_free_tfm(crypto_cipher_tfm(tfm));
1047 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1049 type &= ~CRYPTO_ALG_TYPE_MASK;
1050 type |= CRYPTO_ALG_TYPE_CIPHER;
1051 mask |= CRYPTO_ALG_TYPE_MASK;
1053 return crypto_has_alg(alg_name, type, mask);
1056 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1058 return &crypto_cipher_tfm(tfm)->crt_cipher;
1061 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1063 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1066 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1068 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1071 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1073 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1076 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1079 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1082 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1085 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1088 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1089 const u8 *key, unsigned int keylen)
1091 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1095 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1096 u8 *dst, const u8 *src)
1098 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1102 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1103 u8 *dst, const u8 *src)
1105 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1109 static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
1111 return (struct crypto_hash *)tfm;
1114 static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
1116 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1117 CRYPTO_ALG_TYPE_HASH_MASK);
1118 return __crypto_hash_cast(tfm);
1121 static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1124 type &= ~CRYPTO_ALG_TYPE_MASK;
1125 mask &= ~CRYPTO_ALG_TYPE_MASK;
1126 type |= CRYPTO_ALG_TYPE_HASH;
1127 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1129 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1132 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1137 static inline void crypto_free_hash(struct crypto_hash *tfm)
1139 crypto_free_tfm(crypto_hash_tfm(tfm));
1142 static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1144 type &= ~CRYPTO_ALG_TYPE_MASK;
1145 mask &= ~CRYPTO_ALG_TYPE_MASK;
1146 type |= CRYPTO_ALG_TYPE_HASH;
1147 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1149 return crypto_has_alg(alg_name, type, mask);
1152 static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1154 return &crypto_hash_tfm(tfm)->crt_hash;
1157 static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1159 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1162 static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1164 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1167 static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1169 return crypto_hash_crt(tfm)->digestsize;
1172 static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1174 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1177 static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1179 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1182 static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1184 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1187 static inline int crypto_hash_init(struct hash_desc *desc)
1189 return crypto_hash_crt(desc->tfm)->init(desc);
1192 static inline int crypto_hash_update(struct hash_desc *desc,
1193 struct scatterlist *sg,
1194 unsigned int nbytes)
1196 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1199 static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1201 return crypto_hash_crt(desc->tfm)->final(desc, out);
1204 static inline int crypto_hash_digest(struct hash_desc *desc,
1205 struct scatterlist *sg,
1206 unsigned int nbytes, u8 *out)
1208 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1211 static inline int crypto_hash_setkey(struct crypto_hash *hash,
1212 const u8 *key, unsigned int keylen)
1214 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
1217 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1219 return (struct crypto_comp *)tfm;
1222 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1224 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1225 CRYPTO_ALG_TYPE_MASK);
1226 return __crypto_comp_cast(tfm);
1229 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1232 type &= ~CRYPTO_ALG_TYPE_MASK;
1233 type |= CRYPTO_ALG_TYPE_COMPRESS;
1234 mask |= CRYPTO_ALG_TYPE_MASK;
1236 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1239 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1244 static inline void crypto_free_comp(struct crypto_comp *tfm)
1246 crypto_free_tfm(crypto_comp_tfm(tfm));
1249 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1251 type &= ~CRYPTO_ALG_TYPE_MASK;
1252 type |= CRYPTO_ALG_TYPE_COMPRESS;
1253 mask |= CRYPTO_ALG_TYPE_MASK;
1255 return crypto_has_alg(alg_name, type, mask);
1258 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1260 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1263 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1265 return &crypto_comp_tfm(tfm)->crt_compress;
1268 static inline int crypto_comp_compress(struct crypto_comp *tfm,
1269 const u8 *src, unsigned int slen,
1270 u8 *dst, unsigned int *dlen)
1272 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1273 src, slen, dst, dlen);
1276 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
1277 const u8 *src, unsigned int slen,
1278 u8 *dst, unsigned int *dlen)
1280 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1281 src, slen, dst, dlen);
1284 #endif /* _LINUX_CRYPTO_H */