2 * Scatterlist Cryptographic API.
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
6 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
8 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
9 * and Nettle, by Niels Möller.
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
17 #ifndef _LINUX_CRYPTO_H
18 #define _LINUX_CRYPTO_H
20 #include <asm/atomic.h>
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/list.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/uaccess.h>
29 * Algorithm masks and types.
31 #define CRYPTO_ALG_TYPE_MASK 0x0000000f
32 #define CRYPTO_ALG_TYPE_CIPHER 0x00000001
33 #define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
34 #define CRYPTO_ALG_TYPE_AEAD 0x00000003
35 #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
36 #define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
37 #define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
38 #define CRYPTO_ALG_TYPE_DIGEST 0x00000008
39 #define CRYPTO_ALG_TYPE_HASH 0x00000009
40 #define CRYPTO_ALG_TYPE_AHASH 0x0000000a
42 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
43 #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
44 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
46 #define CRYPTO_ALG_LARVAL 0x00000010
47 #define CRYPTO_ALG_DEAD 0x00000020
48 #define CRYPTO_ALG_DYING 0x00000040
49 #define CRYPTO_ALG_ASYNC 0x00000080
52 * Set this bit if and only if the algorithm requires another algorithm of
53 * the same type to handle corner cases.
55 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
58 * This bit is set for symmetric key ciphers that have already been wrapped
59 * with a generic IV generator to prevent them from being wrapped again.
61 #define CRYPTO_ALG_GENIV 0x00000200
64 * Set if the algorithm has passed automated run-time testing. Note that
65 * if there is no run-time testing for a given algorithm it is considered
69 #define CRYPTO_ALG_TESTED 0x00000400
72 * Transform masks and values (for crt_flags).
74 #define CRYPTO_TFM_REQ_MASK 0x000fff00
75 #define CRYPTO_TFM_RES_MASK 0xfff00000
77 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
78 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
79 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
80 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
81 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
82 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
83 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
84 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
87 * Miscellaneous stuff.
89 #define CRYPTO_MAX_ALG_NAME 64
92 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
93 * declaration) is used to ensure that the crypto_tfm context structure is
94 * aligned correctly for the given architecture so that there are no alignment
95 * faults for C data types. In particular, this is required on platforms such
96 * as arm where pointers are 32-bit aligned but there are data types such as
97 * u64 which require 64-bit alignment.
99 #if defined(ARCH_KMALLOC_MINALIGN)
100 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
101 #elif defined(ARCH_SLAB_MINALIGN)
102 #define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
104 #define CRYPTO_MINALIGN __alignof__(unsigned long long)
107 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
110 struct crypto_ablkcipher;
111 struct crypto_async_request;
113 struct crypto_blkcipher;
118 struct aead_givcrypt_request;
119 struct skcipher_givcrypt_request;
121 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
123 struct crypto_async_request {
124 struct list_head list;
125 crypto_completion_t complete;
127 struct crypto_tfm *tfm;
132 struct ablkcipher_request {
133 struct crypto_async_request base;
139 struct scatterlist *src;
140 struct scatterlist *dst;
142 void *__ctx[] CRYPTO_MINALIGN_ATTR;
145 struct ahash_request {
146 struct crypto_async_request base;
149 struct scatterlist *src;
152 void *__ctx[] CRYPTO_MINALIGN_ATTR;
156 * struct aead_request - AEAD request
157 * @base: Common attributes for async crypto requests
158 * @assoclen: Length in bytes of associated data for authentication
159 * @cryptlen: Length of data to be encrypted or decrypted
160 * @iv: Initialisation vector
161 * @assoc: Associated data
163 * @dst: Destination data
164 * @__ctx: Start of private context data
166 struct aead_request {
167 struct crypto_async_request base;
169 unsigned int assoclen;
170 unsigned int cryptlen;
174 struct scatterlist *assoc;
175 struct scatterlist *src;
176 struct scatterlist *dst;
178 void *__ctx[] CRYPTO_MINALIGN_ATTR;
181 struct blkcipher_desc {
182 struct crypto_blkcipher *tfm;
188 struct crypto_tfm *tfm;
189 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
190 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
191 const u8 *src, unsigned int nbytes);
196 struct crypto_hash *tfm;
201 * Algorithms: modular crypto algorithm implementations, managed
202 * via crypto_register_alg() and crypto_unregister_alg().
204 struct ablkcipher_alg {
205 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
206 unsigned int keylen);
207 int (*encrypt)(struct ablkcipher_request *req);
208 int (*decrypt)(struct ablkcipher_request *req);
209 int (*givencrypt)(struct skcipher_givcrypt_request *req);
210 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
214 unsigned int min_keysize;
215 unsigned int max_keysize;
220 int (*init)(struct ahash_request *req);
221 int (*update)(struct ahash_request *req);
222 int (*final)(struct ahash_request *req);
223 int (*digest)(struct ahash_request *req);
224 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
225 unsigned int keylen);
227 unsigned int digestsize;
231 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
232 unsigned int keylen);
233 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
234 int (*encrypt)(struct aead_request *req);
235 int (*decrypt)(struct aead_request *req);
236 int (*givencrypt)(struct aead_givcrypt_request *req);
237 int (*givdecrypt)(struct aead_givcrypt_request *req);
242 unsigned int maxauthsize;
245 struct blkcipher_alg {
246 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
247 unsigned int keylen);
248 int (*encrypt)(struct blkcipher_desc *desc,
249 struct scatterlist *dst, struct scatterlist *src,
250 unsigned int nbytes);
251 int (*decrypt)(struct blkcipher_desc *desc,
252 struct scatterlist *dst, struct scatterlist *src,
253 unsigned int nbytes);
257 unsigned int min_keysize;
258 unsigned int max_keysize;
263 unsigned int cia_min_keysize;
264 unsigned int cia_max_keysize;
265 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
266 unsigned int keylen);
267 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
268 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
272 unsigned int dia_digestsize;
273 void (*dia_init)(struct crypto_tfm *tfm);
274 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
276 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
277 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
278 unsigned int keylen);
282 int (*init)(struct hash_desc *desc);
283 int (*update)(struct hash_desc *desc, struct scatterlist *sg,
284 unsigned int nbytes);
285 int (*final)(struct hash_desc *desc, u8 *out);
286 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
287 unsigned int nbytes, u8 *out);
288 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
289 unsigned int keylen);
291 unsigned int digestsize;
294 struct compress_alg {
295 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
296 unsigned int slen, u8 *dst, unsigned int *dlen);
297 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
298 unsigned int slen, u8 *dst, unsigned int *dlen);
301 #define cra_ablkcipher cra_u.ablkcipher
302 #define cra_aead cra_u.aead
303 #define cra_blkcipher cra_u.blkcipher
304 #define cra_cipher cra_u.cipher
305 #define cra_digest cra_u.digest
306 #define cra_hash cra_u.hash
307 #define cra_ahash cra_u.ahash
308 #define cra_compress cra_u.compress
311 struct list_head cra_list;
312 struct list_head cra_users;
315 unsigned int cra_blocksize;
316 unsigned int cra_ctxsize;
317 unsigned int cra_alignmask;
322 char cra_name[CRYPTO_MAX_ALG_NAME];
323 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
325 const struct crypto_type *cra_type;
328 struct ablkcipher_alg ablkcipher;
329 struct aead_alg aead;
330 struct blkcipher_alg blkcipher;
331 struct cipher_alg cipher;
332 struct digest_alg digest;
333 struct hash_alg hash;
334 struct ahash_alg ahash;
335 struct compress_alg compress;
338 int (*cra_init)(struct crypto_tfm *tfm);
339 void (*cra_exit)(struct crypto_tfm *tfm);
340 void (*cra_destroy)(struct crypto_alg *alg);
342 struct module *cra_module;
346 * Algorithm registration interface.
348 int crypto_register_alg(struct crypto_alg *alg);
349 int crypto_unregister_alg(struct crypto_alg *alg);
352 * Algorithm query interface.
354 int crypto_has_alg(const char *name, u32 type, u32 mask);
357 * Transforms: user-instantiated objects which encapsulate algorithms
358 * and core processing logic. Managed via crypto_alloc_*() and
359 * crypto_free_*(), as well as the various helpers below.
362 struct ablkcipher_tfm {
363 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
364 unsigned int keylen);
365 int (*encrypt)(struct ablkcipher_request *req);
366 int (*decrypt)(struct ablkcipher_request *req);
367 int (*givencrypt)(struct skcipher_givcrypt_request *req);
368 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
370 struct crypto_ablkcipher *base;
373 unsigned int reqsize;
377 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
378 unsigned int keylen);
379 int (*encrypt)(struct aead_request *req);
380 int (*decrypt)(struct aead_request *req);
381 int (*givencrypt)(struct aead_givcrypt_request *req);
382 int (*givdecrypt)(struct aead_givcrypt_request *req);
384 struct crypto_aead *base;
387 unsigned int authsize;
388 unsigned int reqsize;
391 struct blkcipher_tfm {
393 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
394 unsigned int keylen);
395 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
396 struct scatterlist *src, unsigned int nbytes);
397 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
398 struct scatterlist *src, unsigned int nbytes);
402 int (*cit_setkey)(struct crypto_tfm *tfm,
403 const u8 *key, unsigned int keylen);
404 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
405 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
409 int (*init)(struct hash_desc *desc);
410 int (*update)(struct hash_desc *desc,
411 struct scatterlist *sg, unsigned int nsg);
412 int (*final)(struct hash_desc *desc, u8 *out);
413 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
414 unsigned int nsg, u8 *out);
415 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
416 unsigned int keylen);
417 unsigned int digestsize;
421 int (*init)(struct ahash_request *req);
422 int (*update)(struct ahash_request *req);
423 int (*final)(struct ahash_request *req);
424 int (*digest)(struct ahash_request *req);
425 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
426 unsigned int keylen);
428 unsigned int digestsize;
429 unsigned int reqsize;
432 struct compress_tfm {
433 int (*cot_compress)(struct crypto_tfm *tfm,
434 const u8 *src, unsigned int slen,
435 u8 *dst, unsigned int *dlen);
436 int (*cot_decompress)(struct crypto_tfm *tfm,
437 const u8 *src, unsigned int slen,
438 u8 *dst, unsigned int *dlen);
441 #define crt_ablkcipher crt_u.ablkcipher
442 #define crt_aead crt_u.aead
443 #define crt_blkcipher crt_u.blkcipher
444 #define crt_cipher crt_u.cipher
445 #define crt_hash crt_u.hash
446 #define crt_ahash crt_u.ahash
447 #define crt_compress crt_u.compress
454 struct ablkcipher_tfm ablkcipher;
455 struct aead_tfm aead;
456 struct blkcipher_tfm blkcipher;
457 struct cipher_tfm cipher;
458 struct hash_tfm hash;
459 struct ahash_tfm ahash;
460 struct compress_tfm compress;
463 struct crypto_alg *__crt_alg;
465 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
468 struct crypto_ablkcipher {
469 struct crypto_tfm base;
473 struct crypto_tfm base;
476 struct crypto_blkcipher {
477 struct crypto_tfm base;
480 struct crypto_cipher {
481 struct crypto_tfm base;
485 struct crypto_tfm base;
489 struct crypto_tfm base;
500 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
502 /* Maximum number of (rtattr) parameters for each template. */
503 #define CRYPTO_MAX_ATTRS 32
505 struct crypto_attr_alg {
506 char name[CRYPTO_MAX_ALG_NAME];
509 struct crypto_attr_type {
514 struct crypto_attr_u32 {
519 * Transform user interface.
522 struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
523 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
524 void crypto_free_tfm(struct crypto_tfm *tfm);
526 int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
529 * Transform helpers which query the underlying algorithm.
531 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
533 return tfm->__crt_alg->cra_name;
536 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
538 return tfm->__crt_alg->cra_driver_name;
541 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
543 return tfm->__crt_alg->cra_priority;
546 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
548 return module_name(tfm->__crt_alg->cra_module);
551 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
553 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
556 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
558 return tfm->__crt_alg->cra_blocksize;
561 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
563 return tfm->__crt_alg->cra_alignmask;
566 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
568 return tfm->crt_flags;
571 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
573 tfm->crt_flags |= flags;
576 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
578 tfm->crt_flags &= ~flags;
581 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
583 return tfm->__crt_ctx;
586 static inline unsigned int crypto_tfm_ctx_alignment(void)
588 struct crypto_tfm *tfm;
589 return __alignof__(tfm->__crt_ctx);
595 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
596 struct crypto_tfm *tfm)
598 return (struct crypto_ablkcipher *)tfm;
601 static inline u32 crypto_skcipher_type(u32 type)
603 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
604 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
608 static inline u32 crypto_skcipher_mask(u32 mask)
610 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
611 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
615 struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
618 static inline struct crypto_tfm *crypto_ablkcipher_tfm(
619 struct crypto_ablkcipher *tfm)
624 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
626 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
629 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
632 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
633 crypto_skcipher_mask(mask));
636 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
637 struct crypto_ablkcipher *tfm)
639 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
642 static inline unsigned int crypto_ablkcipher_ivsize(
643 struct crypto_ablkcipher *tfm)
645 return crypto_ablkcipher_crt(tfm)->ivsize;
648 static inline unsigned int crypto_ablkcipher_blocksize(
649 struct crypto_ablkcipher *tfm)
651 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
654 static inline unsigned int crypto_ablkcipher_alignmask(
655 struct crypto_ablkcipher *tfm)
657 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
660 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
662 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
665 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
668 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
671 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
674 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
677 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
678 const u8 *key, unsigned int keylen)
680 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
682 return crt->setkey(crt->base, key, keylen);
685 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
686 struct ablkcipher_request *req)
688 return __crypto_ablkcipher_cast(req->base.tfm);
691 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
693 struct ablkcipher_tfm *crt =
694 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
695 return crt->encrypt(req);
698 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
700 struct ablkcipher_tfm *crt =
701 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
702 return crt->decrypt(req);
705 static inline unsigned int crypto_ablkcipher_reqsize(
706 struct crypto_ablkcipher *tfm)
708 return crypto_ablkcipher_crt(tfm)->reqsize;
711 static inline void ablkcipher_request_set_tfm(
712 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
714 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
717 static inline struct ablkcipher_request *ablkcipher_request_cast(
718 struct crypto_async_request *req)
720 return container_of(req, struct ablkcipher_request, base);
723 static inline struct ablkcipher_request *ablkcipher_request_alloc(
724 struct crypto_ablkcipher *tfm, gfp_t gfp)
726 struct ablkcipher_request *req;
728 req = kmalloc(sizeof(struct ablkcipher_request) +
729 crypto_ablkcipher_reqsize(tfm), gfp);
732 ablkcipher_request_set_tfm(req, tfm);
737 static inline void ablkcipher_request_free(struct ablkcipher_request *req)
742 static inline void ablkcipher_request_set_callback(
743 struct ablkcipher_request *req,
744 u32 flags, crypto_completion_t complete, void *data)
746 req->base.complete = complete;
747 req->base.data = data;
748 req->base.flags = flags;
751 static inline void ablkcipher_request_set_crypt(
752 struct ablkcipher_request *req,
753 struct scatterlist *src, struct scatterlist *dst,
754 unsigned int nbytes, void *iv)
758 req->nbytes = nbytes;
762 static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
764 return (struct crypto_aead *)tfm;
767 struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
769 static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
774 static inline void crypto_free_aead(struct crypto_aead *tfm)
776 crypto_free_tfm(crypto_aead_tfm(tfm));
779 static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
781 return &crypto_aead_tfm(tfm)->crt_aead;
784 static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
786 return crypto_aead_crt(tfm)->ivsize;
789 static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
791 return crypto_aead_crt(tfm)->authsize;
794 static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
796 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
799 static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
801 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
804 static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
806 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
809 static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
811 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
814 static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
816 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
819 static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
822 struct aead_tfm *crt = crypto_aead_crt(tfm);
824 return crt->setkey(crt->base, key, keylen);
827 int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
829 static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
831 return __crypto_aead_cast(req->base.tfm);
834 static inline int crypto_aead_encrypt(struct aead_request *req)
836 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
839 static inline int crypto_aead_decrypt(struct aead_request *req)
841 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
844 static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
846 return crypto_aead_crt(tfm)->reqsize;
849 static inline void aead_request_set_tfm(struct aead_request *req,
850 struct crypto_aead *tfm)
852 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
855 static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
858 struct aead_request *req;
860 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
863 aead_request_set_tfm(req, tfm);
868 static inline void aead_request_free(struct aead_request *req)
873 static inline void aead_request_set_callback(struct aead_request *req,
875 crypto_completion_t complete,
878 req->base.complete = complete;
879 req->base.data = data;
880 req->base.flags = flags;
883 static inline void aead_request_set_crypt(struct aead_request *req,
884 struct scatterlist *src,
885 struct scatterlist *dst,
886 unsigned int cryptlen, u8 *iv)
890 req->cryptlen = cryptlen;
894 static inline void aead_request_set_assoc(struct aead_request *req,
895 struct scatterlist *assoc,
896 unsigned int assoclen)
899 req->assoclen = assoclen;
902 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
903 struct crypto_tfm *tfm)
905 return (struct crypto_blkcipher *)tfm;
908 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
909 struct crypto_tfm *tfm)
911 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
912 return __crypto_blkcipher_cast(tfm);
915 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
916 const char *alg_name, u32 type, u32 mask)
918 type &= ~CRYPTO_ALG_TYPE_MASK;
919 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
920 mask |= CRYPTO_ALG_TYPE_MASK;
922 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
925 static inline struct crypto_tfm *crypto_blkcipher_tfm(
926 struct crypto_blkcipher *tfm)
931 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
933 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
936 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
938 type &= ~CRYPTO_ALG_TYPE_MASK;
939 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
940 mask |= CRYPTO_ALG_TYPE_MASK;
942 return crypto_has_alg(alg_name, type, mask);
945 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
947 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
950 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
951 struct crypto_blkcipher *tfm)
953 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
956 static inline struct blkcipher_alg *crypto_blkcipher_alg(
957 struct crypto_blkcipher *tfm)
959 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
962 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
964 return crypto_blkcipher_alg(tfm)->ivsize;
967 static inline unsigned int crypto_blkcipher_blocksize(
968 struct crypto_blkcipher *tfm)
970 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
973 static inline unsigned int crypto_blkcipher_alignmask(
974 struct crypto_blkcipher *tfm)
976 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
979 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
981 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
984 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
987 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
990 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
993 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
996 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
997 const u8 *key, unsigned int keylen)
999 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1003 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1004 struct scatterlist *dst,
1005 struct scatterlist *src,
1006 unsigned int nbytes)
1008 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1009 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1012 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1013 struct scatterlist *dst,
1014 struct scatterlist *src,
1015 unsigned int nbytes)
1017 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1020 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1021 struct scatterlist *dst,
1022 struct scatterlist *src,
1023 unsigned int nbytes)
1025 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1026 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1029 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1030 struct scatterlist *dst,
1031 struct scatterlist *src,
1032 unsigned int nbytes)
1034 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1037 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1038 const u8 *src, unsigned int len)
1040 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1043 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1044 u8 *dst, unsigned int len)
1046 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1049 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1051 return (struct crypto_cipher *)tfm;
1054 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1056 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1057 return __crypto_cipher_cast(tfm);
1060 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1063 type &= ~CRYPTO_ALG_TYPE_MASK;
1064 type |= CRYPTO_ALG_TYPE_CIPHER;
1065 mask |= CRYPTO_ALG_TYPE_MASK;
1067 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1070 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1075 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1077 crypto_free_tfm(crypto_cipher_tfm(tfm));
1080 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1082 type &= ~CRYPTO_ALG_TYPE_MASK;
1083 type |= CRYPTO_ALG_TYPE_CIPHER;
1084 mask |= CRYPTO_ALG_TYPE_MASK;
1086 return crypto_has_alg(alg_name, type, mask);
1089 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1091 return &crypto_cipher_tfm(tfm)->crt_cipher;
1094 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1096 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1099 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1101 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1104 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1106 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1109 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1112 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1115 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1118 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1121 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1122 const u8 *key, unsigned int keylen)
1124 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1128 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1129 u8 *dst, const u8 *src)
1131 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1135 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1136 u8 *dst, const u8 *src)
1138 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1142 static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
1144 return (struct crypto_hash *)tfm;
1147 static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
1149 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1150 CRYPTO_ALG_TYPE_HASH_MASK);
1151 return __crypto_hash_cast(tfm);
1154 static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1157 type &= ~CRYPTO_ALG_TYPE_MASK;
1158 mask &= ~CRYPTO_ALG_TYPE_MASK;
1159 type |= CRYPTO_ALG_TYPE_HASH;
1160 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1162 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1165 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1170 static inline void crypto_free_hash(struct crypto_hash *tfm)
1172 crypto_free_tfm(crypto_hash_tfm(tfm));
1175 static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1177 type &= ~CRYPTO_ALG_TYPE_MASK;
1178 mask &= ~CRYPTO_ALG_TYPE_MASK;
1179 type |= CRYPTO_ALG_TYPE_HASH;
1180 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1182 return crypto_has_alg(alg_name, type, mask);
1185 static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1187 return &crypto_hash_tfm(tfm)->crt_hash;
1190 static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1192 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1195 static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1197 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1200 static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1202 return crypto_hash_crt(tfm)->digestsize;
1205 static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1207 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1210 static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1212 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1215 static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1217 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1220 static inline int crypto_hash_init(struct hash_desc *desc)
1222 return crypto_hash_crt(desc->tfm)->init(desc);
1225 static inline int crypto_hash_update(struct hash_desc *desc,
1226 struct scatterlist *sg,
1227 unsigned int nbytes)
1229 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1232 static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1234 return crypto_hash_crt(desc->tfm)->final(desc, out);
1237 static inline int crypto_hash_digest(struct hash_desc *desc,
1238 struct scatterlist *sg,
1239 unsigned int nbytes, u8 *out)
1241 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1244 static inline int crypto_hash_setkey(struct crypto_hash *hash,
1245 const u8 *key, unsigned int keylen)
1247 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
1250 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1252 return (struct crypto_comp *)tfm;
1255 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1257 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1258 CRYPTO_ALG_TYPE_MASK);
1259 return __crypto_comp_cast(tfm);
1262 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1265 type &= ~CRYPTO_ALG_TYPE_MASK;
1266 type |= CRYPTO_ALG_TYPE_COMPRESS;
1267 mask |= CRYPTO_ALG_TYPE_MASK;
1269 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1272 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1277 static inline void crypto_free_comp(struct crypto_comp *tfm)
1279 crypto_free_tfm(crypto_comp_tfm(tfm));
1282 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1284 type &= ~CRYPTO_ALG_TYPE_MASK;
1285 type |= CRYPTO_ALG_TYPE_COMPRESS;
1286 mask |= CRYPTO_ALG_TYPE_MASK;
1288 return crypto_has_alg(alg_name, type, mask);
1291 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1293 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1296 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1298 return &crypto_comp_tfm(tfm)->crt_compress;
1301 static inline int crypto_comp_compress(struct crypto_comp *tfm,
1302 const u8 *src, unsigned int slen,
1303 u8 *dst, unsigned int *dlen)
1305 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1306 src, slen, dst, dlen);
1309 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
1310 const u8 *src, unsigned int slen,
1311 u8 *dst, unsigned int *dlen)
1313 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1314 src, slen, dst, dlen);
1317 #endif /* _LINUX_CRYPTO_H */