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_COMPRESS 0x00000005
37 #define CRYPTO_ALG_TYPE_AEAD 0x00000006
39 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
41 #define CRYPTO_ALG_LARVAL 0x00000010
42 #define CRYPTO_ALG_DEAD 0x00000020
43 #define CRYPTO_ALG_DYING 0x00000040
44 #define CRYPTO_ALG_ASYNC 0x00000080
47 * Set this bit if and only if the algorithm requires another algorithm of
48 * the same type to handle corner cases.
50 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
53 * Transform masks and values (for crt_flags).
55 #define CRYPTO_TFM_REQ_MASK 0x000fff00
56 #define CRYPTO_TFM_RES_MASK 0xfff00000
58 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
59 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
60 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
61 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
62 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
63 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
64 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
65 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
68 * Miscellaneous stuff.
70 #define CRYPTO_MAX_ALG_NAME 64
73 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
74 * declaration) is used to ensure that the crypto_tfm context structure is
75 * aligned correctly for the given architecture so that there are no alignment
76 * faults for C data types. In particular, this is required on platforms such
77 * as arm where pointers are 32-bit aligned but there are data types such as
78 * u64 which require 64-bit alignment.
80 #if defined(ARCH_KMALLOC_MINALIGN)
81 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
82 #elif defined(ARCH_SLAB_MINALIGN)
83 #define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
86 #ifdef CRYPTO_MINALIGN
87 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
89 #define CRYPTO_MINALIGN_ATTR
93 struct crypto_ablkcipher;
94 struct crypto_async_request;
96 struct crypto_blkcipher;
101 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
103 struct crypto_async_request {
104 struct list_head list;
105 crypto_completion_t complete;
107 struct crypto_tfm *tfm;
112 struct ablkcipher_request {
113 struct crypto_async_request base;
119 struct scatterlist *src;
120 struct scatterlist *dst;
122 void *__ctx[] CRYPTO_MINALIGN_ATTR;
126 * struct aead_request - AEAD request
127 * @base: Common attributes for async crypto requests
128 * @assoclen: Length in bytes of associated data for authentication
129 * @cryptlen: Length of data to be encrypted or decrypted
130 * @iv: Initialisation vector
131 * @assoc: Associated data
133 * @dst: Destination data
134 * @__ctx: Start of private context data
136 struct aead_request {
137 struct crypto_async_request base;
139 unsigned int assoclen;
140 unsigned int cryptlen;
144 struct scatterlist *assoc;
145 struct scatterlist *src;
146 struct scatterlist *dst;
148 void *__ctx[] CRYPTO_MINALIGN_ATTR;
151 struct blkcipher_desc {
152 struct crypto_blkcipher *tfm;
158 struct crypto_tfm *tfm;
159 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
160 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
161 const u8 *src, unsigned int nbytes);
166 struct crypto_hash *tfm;
171 * Algorithms: modular crypto algorithm implementations, managed
172 * via crypto_register_alg() and crypto_unregister_alg().
174 struct ablkcipher_alg {
175 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
176 unsigned int keylen);
177 int (*encrypt)(struct ablkcipher_request *req);
178 int (*decrypt)(struct ablkcipher_request *req);
180 unsigned int min_keysize;
181 unsigned int max_keysize;
186 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
187 unsigned int keylen);
188 int (*encrypt)(struct aead_request *req);
189 int (*decrypt)(struct aead_request *req);
192 unsigned int authsize;
195 struct blkcipher_alg {
196 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
197 unsigned int keylen);
198 int (*encrypt)(struct blkcipher_desc *desc,
199 struct scatterlist *dst, struct scatterlist *src,
200 unsigned int nbytes);
201 int (*decrypt)(struct blkcipher_desc *desc,
202 struct scatterlist *dst, struct scatterlist *src,
203 unsigned int nbytes);
205 unsigned int min_keysize;
206 unsigned int max_keysize;
211 unsigned int cia_min_keysize;
212 unsigned int cia_max_keysize;
213 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
214 unsigned int keylen);
215 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
216 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
220 unsigned int dia_digestsize;
221 void (*dia_init)(struct crypto_tfm *tfm);
222 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
224 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
225 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
226 unsigned int keylen);
230 int (*init)(struct hash_desc *desc);
231 int (*update)(struct hash_desc *desc, struct scatterlist *sg,
232 unsigned int nbytes);
233 int (*final)(struct hash_desc *desc, u8 *out);
234 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
235 unsigned int nbytes, u8 *out);
236 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
237 unsigned int keylen);
239 unsigned int digestsize;
242 struct compress_alg {
243 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
244 unsigned int slen, u8 *dst, unsigned int *dlen);
245 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
246 unsigned int slen, u8 *dst, unsigned int *dlen);
249 #define cra_ablkcipher cra_u.ablkcipher
250 #define cra_aead cra_u.aead
251 #define cra_blkcipher cra_u.blkcipher
252 #define cra_cipher cra_u.cipher
253 #define cra_digest cra_u.digest
254 #define cra_hash cra_u.hash
255 #define cra_compress cra_u.compress
258 struct list_head cra_list;
259 struct list_head cra_users;
262 unsigned int cra_blocksize;
263 unsigned int cra_ctxsize;
264 unsigned int cra_alignmask;
269 char cra_name[CRYPTO_MAX_ALG_NAME];
270 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
272 const struct crypto_type *cra_type;
275 struct ablkcipher_alg ablkcipher;
276 struct aead_alg aead;
277 struct blkcipher_alg blkcipher;
278 struct cipher_alg cipher;
279 struct digest_alg digest;
280 struct hash_alg hash;
281 struct compress_alg compress;
284 int (*cra_init)(struct crypto_tfm *tfm);
285 void (*cra_exit)(struct crypto_tfm *tfm);
286 void (*cra_destroy)(struct crypto_alg *alg);
288 struct module *cra_module;
292 * Algorithm registration interface.
294 int crypto_register_alg(struct crypto_alg *alg);
295 int crypto_unregister_alg(struct crypto_alg *alg);
298 * Algorithm query interface.
301 int crypto_has_alg(const char *name, u32 type, u32 mask);
303 static inline int crypto_has_alg(const char *name, u32 type, u32 mask)
310 * Transforms: user-instantiated objects which encapsulate algorithms
311 * and core processing logic. Managed via crypto_alloc_*() and
312 * crypto_free_*(), as well as the various helpers below.
315 struct ablkcipher_tfm {
316 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
317 unsigned int keylen);
318 int (*encrypt)(struct ablkcipher_request *req);
319 int (*decrypt)(struct ablkcipher_request *req);
321 unsigned int reqsize;
325 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
326 unsigned int keylen);
327 int (*encrypt)(struct aead_request *req);
328 int (*decrypt)(struct aead_request *req);
330 unsigned int authsize;
331 unsigned int reqsize;
334 struct blkcipher_tfm {
336 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
337 unsigned int keylen);
338 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
339 struct scatterlist *src, unsigned int nbytes);
340 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
341 struct scatterlist *src, unsigned int nbytes);
345 int (*cit_setkey)(struct crypto_tfm *tfm,
346 const u8 *key, unsigned int keylen);
347 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
348 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
352 int (*init)(struct hash_desc *desc);
353 int (*update)(struct hash_desc *desc,
354 struct scatterlist *sg, unsigned int nsg);
355 int (*final)(struct hash_desc *desc, u8 *out);
356 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
357 unsigned int nsg, u8 *out);
358 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
359 unsigned int keylen);
360 unsigned int digestsize;
363 struct compress_tfm {
364 int (*cot_compress)(struct crypto_tfm *tfm,
365 const u8 *src, unsigned int slen,
366 u8 *dst, unsigned int *dlen);
367 int (*cot_decompress)(struct crypto_tfm *tfm,
368 const u8 *src, unsigned int slen,
369 u8 *dst, unsigned int *dlen);
372 #define crt_ablkcipher crt_u.ablkcipher
373 #define crt_aead crt_u.aead
374 #define crt_blkcipher crt_u.blkcipher
375 #define crt_cipher crt_u.cipher
376 #define crt_hash crt_u.hash
377 #define crt_compress crt_u.compress
384 struct ablkcipher_tfm ablkcipher;
385 struct aead_tfm aead;
386 struct blkcipher_tfm blkcipher;
387 struct cipher_tfm cipher;
388 struct hash_tfm hash;
389 struct compress_tfm compress;
392 struct crypto_alg *__crt_alg;
394 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
397 struct crypto_ablkcipher {
398 struct crypto_tfm base;
402 struct crypto_tfm base;
405 struct crypto_blkcipher {
406 struct crypto_tfm base;
409 struct crypto_cipher {
410 struct crypto_tfm base;
414 struct crypto_tfm base;
418 struct crypto_tfm base;
429 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
431 /* Maximum number of (rtattr) parameters for each template. */
432 #define CRYPTO_MAX_ATTRS 32
434 struct crypto_attr_alg {
435 char name[CRYPTO_MAX_ALG_NAME];
438 struct crypto_attr_type {
443 struct crypto_attr_u32 {
448 * Transform user interface.
451 struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
452 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
453 void crypto_free_tfm(struct crypto_tfm *tfm);
456 * Transform helpers which query the underlying algorithm.
458 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
460 return tfm->__crt_alg->cra_name;
463 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
465 return tfm->__crt_alg->cra_driver_name;
468 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
470 return tfm->__crt_alg->cra_priority;
473 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
475 return module_name(tfm->__crt_alg->cra_module);
478 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
480 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
483 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
485 return tfm->__crt_alg->cra_blocksize;
488 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
490 return tfm->__crt_alg->cra_alignmask;
493 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
495 return tfm->crt_flags;
498 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
500 tfm->crt_flags |= flags;
503 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
505 tfm->crt_flags &= ~flags;
508 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
510 return tfm->__crt_ctx;
513 static inline unsigned int crypto_tfm_ctx_alignment(void)
515 struct crypto_tfm *tfm;
516 return __alignof__(tfm->__crt_ctx);
522 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
523 struct crypto_tfm *tfm)
525 return (struct crypto_ablkcipher *)tfm;
528 static inline struct crypto_ablkcipher *crypto_alloc_ablkcipher(
529 const char *alg_name, u32 type, u32 mask)
531 type &= ~CRYPTO_ALG_TYPE_MASK;
532 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
533 mask |= CRYPTO_ALG_TYPE_MASK;
535 return __crypto_ablkcipher_cast(
536 crypto_alloc_base(alg_name, type, mask));
539 static inline struct crypto_tfm *crypto_ablkcipher_tfm(
540 struct crypto_ablkcipher *tfm)
545 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
547 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
550 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
553 type &= ~CRYPTO_ALG_TYPE_MASK;
554 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
555 mask |= CRYPTO_ALG_TYPE_MASK;
557 return crypto_has_alg(alg_name, type, mask);
560 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
561 struct crypto_ablkcipher *tfm)
563 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
566 static inline unsigned int crypto_ablkcipher_ivsize(
567 struct crypto_ablkcipher *tfm)
569 return crypto_ablkcipher_crt(tfm)->ivsize;
572 static inline unsigned int crypto_ablkcipher_blocksize(
573 struct crypto_ablkcipher *tfm)
575 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
578 static inline unsigned int crypto_ablkcipher_alignmask(
579 struct crypto_ablkcipher *tfm)
581 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
584 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
586 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
589 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
592 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
595 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
598 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
601 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
602 const u8 *key, unsigned int keylen)
604 return crypto_ablkcipher_crt(tfm)->setkey(tfm, key, keylen);
607 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
608 struct ablkcipher_request *req)
610 return __crypto_ablkcipher_cast(req->base.tfm);
613 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
615 struct ablkcipher_tfm *crt =
616 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
617 return crt->encrypt(req);
620 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
622 struct ablkcipher_tfm *crt =
623 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
624 return crt->decrypt(req);
627 static inline unsigned int crypto_ablkcipher_reqsize(
628 struct crypto_ablkcipher *tfm)
630 return crypto_ablkcipher_crt(tfm)->reqsize;
633 static inline void ablkcipher_request_set_tfm(
634 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
636 req->base.tfm = crypto_ablkcipher_tfm(tfm);
639 static inline struct ablkcipher_request *ablkcipher_request_cast(
640 struct crypto_async_request *req)
642 return container_of(req, struct ablkcipher_request, base);
645 static inline struct ablkcipher_request *ablkcipher_request_alloc(
646 struct crypto_ablkcipher *tfm, gfp_t gfp)
648 struct ablkcipher_request *req;
650 req = kmalloc(sizeof(struct ablkcipher_request) +
651 crypto_ablkcipher_reqsize(tfm), gfp);
654 ablkcipher_request_set_tfm(req, tfm);
659 static inline void ablkcipher_request_free(struct ablkcipher_request *req)
664 static inline void ablkcipher_request_set_callback(
665 struct ablkcipher_request *req,
666 u32 flags, crypto_completion_t complete, void *data)
668 req->base.complete = complete;
669 req->base.data = data;
670 req->base.flags = flags;
673 static inline void ablkcipher_request_set_crypt(
674 struct ablkcipher_request *req,
675 struct scatterlist *src, struct scatterlist *dst,
676 unsigned int nbytes, void *iv)
680 req->nbytes = nbytes;
684 static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
686 return (struct crypto_aead *)tfm;
689 static inline struct crypto_aead *crypto_alloc_aead(const char *alg_name,
692 type &= ~CRYPTO_ALG_TYPE_MASK;
693 type |= CRYPTO_ALG_TYPE_AEAD;
694 mask |= CRYPTO_ALG_TYPE_MASK;
696 return __crypto_aead_cast(crypto_alloc_base(alg_name, type, mask));
699 static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
704 static inline void crypto_free_aead(struct crypto_aead *tfm)
706 crypto_free_tfm(crypto_aead_tfm(tfm));
709 static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
711 return &crypto_aead_tfm(tfm)->crt_aead;
714 static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
716 return crypto_aead_crt(tfm)->ivsize;
719 static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
721 return crypto_aead_crt(tfm)->authsize;
724 static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
726 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
729 static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
731 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
734 static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
736 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
739 static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
741 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
744 static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
746 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
749 static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
752 return crypto_aead_crt(tfm)->setkey(tfm, key, keylen);
755 static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
757 return __crypto_aead_cast(req->base.tfm);
760 static inline int crypto_aead_encrypt(struct aead_request *req)
762 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
765 static inline int crypto_aead_decrypt(struct aead_request *req)
767 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
770 static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
772 return crypto_aead_crt(tfm)->reqsize;
775 static inline void aead_request_set_tfm(struct aead_request *req,
776 struct crypto_aead *tfm)
778 req->base.tfm = crypto_aead_tfm(tfm);
781 static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
784 struct aead_request *req;
786 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
789 aead_request_set_tfm(req, tfm);
794 static inline void aead_request_free(struct aead_request *req)
799 static inline void aead_request_set_callback(struct aead_request *req,
801 crypto_completion_t complete,
804 req->base.complete = complete;
805 req->base.data = data;
806 req->base.flags = flags;
809 static inline void aead_request_set_crypt(struct aead_request *req,
810 struct scatterlist *src,
811 struct scatterlist *dst,
812 unsigned int cryptlen, u8 *iv)
816 req->cryptlen = cryptlen;
820 static inline void aead_request_set_assoc(struct aead_request *req,
821 struct scatterlist *assoc,
822 unsigned int assoclen)
825 req->assoclen = assoclen;
828 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
829 struct crypto_tfm *tfm)
831 return (struct crypto_blkcipher *)tfm;
834 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
835 struct crypto_tfm *tfm)
837 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
838 return __crypto_blkcipher_cast(tfm);
841 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
842 const char *alg_name, u32 type, u32 mask)
844 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
845 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
846 mask |= CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC;
848 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
851 static inline struct crypto_tfm *crypto_blkcipher_tfm(
852 struct crypto_blkcipher *tfm)
857 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
859 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
862 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
864 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
865 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
866 mask |= CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC;
868 return crypto_has_alg(alg_name, type, mask);
871 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
873 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
876 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
877 struct crypto_blkcipher *tfm)
879 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
882 static inline struct blkcipher_alg *crypto_blkcipher_alg(
883 struct crypto_blkcipher *tfm)
885 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
888 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
890 return crypto_blkcipher_alg(tfm)->ivsize;
893 static inline unsigned int crypto_blkcipher_blocksize(
894 struct crypto_blkcipher *tfm)
896 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
899 static inline unsigned int crypto_blkcipher_alignmask(
900 struct crypto_blkcipher *tfm)
902 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
905 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
907 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
910 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
913 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
916 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
919 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
922 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
923 const u8 *key, unsigned int keylen)
925 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
929 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
930 struct scatterlist *dst,
931 struct scatterlist *src,
934 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
935 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
938 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
939 struct scatterlist *dst,
940 struct scatterlist *src,
943 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
946 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
947 struct scatterlist *dst,
948 struct scatterlist *src,
951 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
952 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
955 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
956 struct scatterlist *dst,
957 struct scatterlist *src,
960 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
963 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
964 const u8 *src, unsigned int len)
966 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
969 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
970 u8 *dst, unsigned int len)
972 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
975 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
977 return (struct crypto_cipher *)tfm;
980 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
982 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
983 return __crypto_cipher_cast(tfm);
986 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
989 type &= ~CRYPTO_ALG_TYPE_MASK;
990 type |= CRYPTO_ALG_TYPE_CIPHER;
991 mask |= CRYPTO_ALG_TYPE_MASK;
993 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
996 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1001 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1003 crypto_free_tfm(crypto_cipher_tfm(tfm));
1006 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1008 type &= ~CRYPTO_ALG_TYPE_MASK;
1009 type |= CRYPTO_ALG_TYPE_CIPHER;
1010 mask |= CRYPTO_ALG_TYPE_MASK;
1012 return crypto_has_alg(alg_name, type, mask);
1015 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1017 return &crypto_cipher_tfm(tfm)->crt_cipher;
1020 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1022 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1025 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1027 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1030 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1032 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1035 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1038 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1041 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1044 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1047 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1048 const u8 *key, unsigned int keylen)
1050 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1054 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1055 u8 *dst, const u8 *src)
1057 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1061 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1062 u8 *dst, const u8 *src)
1064 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1068 static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
1070 return (struct crypto_hash *)tfm;
1073 static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
1075 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1076 CRYPTO_ALG_TYPE_HASH_MASK);
1077 return __crypto_hash_cast(tfm);
1080 static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1083 type &= ~CRYPTO_ALG_TYPE_MASK;
1084 type |= CRYPTO_ALG_TYPE_HASH;
1085 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1087 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1090 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1095 static inline void crypto_free_hash(struct crypto_hash *tfm)
1097 crypto_free_tfm(crypto_hash_tfm(tfm));
1100 static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1102 type &= ~CRYPTO_ALG_TYPE_MASK;
1103 type |= CRYPTO_ALG_TYPE_HASH;
1104 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1106 return crypto_has_alg(alg_name, type, mask);
1109 static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1111 return &crypto_hash_tfm(tfm)->crt_hash;
1114 static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1116 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1119 static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1121 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1124 static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1126 return crypto_hash_crt(tfm)->digestsize;
1129 static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1131 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1134 static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1136 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1139 static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1141 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1144 static inline int crypto_hash_init(struct hash_desc *desc)
1146 return crypto_hash_crt(desc->tfm)->init(desc);
1149 static inline int crypto_hash_update(struct hash_desc *desc,
1150 struct scatterlist *sg,
1151 unsigned int nbytes)
1153 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1156 static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1158 return crypto_hash_crt(desc->tfm)->final(desc, out);
1161 static inline int crypto_hash_digest(struct hash_desc *desc,
1162 struct scatterlist *sg,
1163 unsigned int nbytes, u8 *out)
1165 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1168 static inline int crypto_hash_setkey(struct crypto_hash *hash,
1169 const u8 *key, unsigned int keylen)
1171 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
1174 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1176 return (struct crypto_comp *)tfm;
1179 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1181 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1182 CRYPTO_ALG_TYPE_MASK);
1183 return __crypto_comp_cast(tfm);
1186 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1189 type &= ~CRYPTO_ALG_TYPE_MASK;
1190 type |= CRYPTO_ALG_TYPE_COMPRESS;
1191 mask |= CRYPTO_ALG_TYPE_MASK;
1193 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1196 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1201 static inline void crypto_free_comp(struct crypto_comp *tfm)
1203 crypto_free_tfm(crypto_comp_tfm(tfm));
1206 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1208 type &= ~CRYPTO_ALG_TYPE_MASK;
1209 type |= CRYPTO_ALG_TYPE_COMPRESS;
1210 mask |= CRYPTO_ALG_TYPE_MASK;
1212 return crypto_has_alg(alg_name, type, mask);
1215 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1217 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1220 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1222 return &crypto_comp_tfm(tfm)->crt_compress;
1225 static inline int crypto_comp_compress(struct crypto_comp *tfm,
1226 const u8 *src, unsigned int slen,
1227 u8 *dst, unsigned int *dlen)
1229 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1230 src, slen, dst, dlen);
1233 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
1234 const u8 *src, unsigned int slen,
1235 u8 *dst, unsigned int *dlen)
1237 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1238 src, slen, dst, dlen);
1241 #endif /* _LINUX_CRYPTO_H */