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;
102 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
104 struct crypto_async_request {
105 struct list_head list;
106 crypto_completion_t complete;
108 struct crypto_tfm *tfm;
113 struct ablkcipher_request {
114 struct crypto_async_request base;
120 struct scatterlist *src;
121 struct scatterlist *dst;
123 void *__ctx[] CRYPTO_MINALIGN_ATTR;
127 * struct aead_request - AEAD request
128 * @base: Common attributes for async crypto requests
129 * @assoclen: Length in bytes of associated data for authentication
130 * @cryptlen: Length of data to be encrypted or decrypted
131 * @iv: Initialisation vector
132 * @assoc: Associated data
134 * @dst: Destination data
135 * @__ctx: Start of private context data
137 struct aead_request {
138 struct crypto_async_request base;
140 unsigned int assoclen;
141 unsigned int cryptlen;
145 struct scatterlist *assoc;
146 struct scatterlist *src;
147 struct scatterlist *dst;
149 void *__ctx[] CRYPTO_MINALIGN_ATTR;
152 struct blkcipher_desc {
153 struct crypto_blkcipher *tfm;
159 struct crypto_tfm *tfm;
160 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
161 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
162 const u8 *src, unsigned int nbytes);
167 struct crypto_hash *tfm;
172 * Algorithms: modular crypto algorithm implementations, managed
173 * via crypto_register_alg() and crypto_unregister_alg().
175 struct ablkcipher_alg {
176 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
177 unsigned int keylen);
178 int (*encrypt)(struct ablkcipher_request *req);
179 int (*decrypt)(struct ablkcipher_request *req);
181 struct crypto_queue *queue;
183 unsigned int min_keysize;
184 unsigned int max_keysize;
189 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
190 unsigned int keylen);
191 int (*encrypt)(struct aead_request *req);
192 int (*decrypt)(struct aead_request *req);
195 unsigned int authsize;
198 struct blkcipher_alg {
199 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
200 unsigned int keylen);
201 int (*encrypt)(struct blkcipher_desc *desc,
202 struct scatterlist *dst, struct scatterlist *src,
203 unsigned int nbytes);
204 int (*decrypt)(struct blkcipher_desc *desc,
205 struct scatterlist *dst, struct scatterlist *src,
206 unsigned int nbytes);
208 unsigned int min_keysize;
209 unsigned int max_keysize;
214 unsigned int cia_min_keysize;
215 unsigned int cia_max_keysize;
216 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
217 unsigned int keylen);
218 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
219 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
223 unsigned int dia_digestsize;
224 void (*dia_init)(struct crypto_tfm *tfm);
225 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
227 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
228 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
229 unsigned int keylen);
233 int (*init)(struct hash_desc *desc);
234 int (*update)(struct hash_desc *desc, struct scatterlist *sg,
235 unsigned int nbytes);
236 int (*final)(struct hash_desc *desc, u8 *out);
237 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
238 unsigned int nbytes, u8 *out);
239 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
240 unsigned int keylen);
242 unsigned int digestsize;
245 struct compress_alg {
246 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
247 unsigned int slen, u8 *dst, unsigned int *dlen);
248 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
249 unsigned int slen, u8 *dst, unsigned int *dlen);
252 #define cra_ablkcipher cra_u.ablkcipher
253 #define cra_aead cra_u.aead
254 #define cra_blkcipher cra_u.blkcipher
255 #define cra_cipher cra_u.cipher
256 #define cra_digest cra_u.digest
257 #define cra_hash cra_u.hash
258 #define cra_compress cra_u.compress
261 struct list_head cra_list;
262 struct list_head cra_users;
265 unsigned int cra_blocksize;
266 unsigned int cra_ctxsize;
267 unsigned int cra_alignmask;
272 char cra_name[CRYPTO_MAX_ALG_NAME];
273 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
275 const struct crypto_type *cra_type;
278 struct ablkcipher_alg ablkcipher;
279 struct aead_alg aead;
280 struct blkcipher_alg blkcipher;
281 struct cipher_alg cipher;
282 struct digest_alg digest;
283 struct hash_alg hash;
284 struct compress_alg compress;
287 int (*cra_init)(struct crypto_tfm *tfm);
288 void (*cra_exit)(struct crypto_tfm *tfm);
289 void (*cra_destroy)(struct crypto_alg *alg);
291 struct module *cra_module;
295 * Algorithm registration interface.
297 int crypto_register_alg(struct crypto_alg *alg);
298 int crypto_unregister_alg(struct crypto_alg *alg);
301 * Algorithm query interface.
304 int crypto_has_alg(const char *name, u32 type, u32 mask);
306 static inline int crypto_has_alg(const char *name, u32 type, u32 mask)
313 * Transforms: user-instantiated objects which encapsulate algorithms
314 * and core processing logic. Managed via crypto_alloc_*() and
315 * crypto_free_*(), as well as the various helpers below.
318 struct ablkcipher_tfm {
319 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
320 unsigned int keylen);
321 int (*encrypt)(struct ablkcipher_request *req);
322 int (*decrypt)(struct ablkcipher_request *req);
324 unsigned int reqsize;
328 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
329 unsigned int keylen);
330 int (*encrypt)(struct aead_request *req);
331 int (*decrypt)(struct aead_request *req);
333 unsigned int authsize;
334 unsigned int reqsize;
337 struct blkcipher_tfm {
339 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
340 unsigned int keylen);
341 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
342 struct scatterlist *src, unsigned int nbytes);
343 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
344 struct scatterlist *src, unsigned int nbytes);
348 int (*cit_setkey)(struct crypto_tfm *tfm,
349 const u8 *key, unsigned int keylen);
350 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
351 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
355 int (*init)(struct hash_desc *desc);
356 int (*update)(struct hash_desc *desc,
357 struct scatterlist *sg, unsigned int nsg);
358 int (*final)(struct hash_desc *desc, u8 *out);
359 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
360 unsigned int nsg, u8 *out);
361 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
362 unsigned int keylen);
363 unsigned int digestsize;
366 struct compress_tfm {
367 int (*cot_compress)(struct crypto_tfm *tfm,
368 const u8 *src, unsigned int slen,
369 u8 *dst, unsigned int *dlen);
370 int (*cot_decompress)(struct crypto_tfm *tfm,
371 const u8 *src, unsigned int slen,
372 u8 *dst, unsigned int *dlen);
375 #define crt_ablkcipher crt_u.ablkcipher
376 #define crt_aead crt_u.aead
377 #define crt_blkcipher crt_u.blkcipher
378 #define crt_cipher crt_u.cipher
379 #define crt_hash crt_u.hash
380 #define crt_compress crt_u.compress
387 struct ablkcipher_tfm ablkcipher;
388 struct aead_tfm aead;
389 struct blkcipher_tfm blkcipher;
390 struct cipher_tfm cipher;
391 struct hash_tfm hash;
392 struct compress_tfm compress;
395 struct crypto_alg *__crt_alg;
397 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
400 struct crypto_ablkcipher {
401 struct crypto_tfm base;
405 struct crypto_tfm base;
408 struct crypto_blkcipher {
409 struct crypto_tfm base;
412 struct crypto_cipher {
413 struct crypto_tfm base;
417 struct crypto_tfm base;
421 struct crypto_tfm base;
431 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
433 struct crypto_attr_alg {
434 char name[CRYPTO_MAX_ALG_NAME];
437 struct crypto_attr_type {
443 * Transform user interface.
446 struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
447 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
448 void crypto_free_tfm(struct crypto_tfm *tfm);
451 * Transform helpers which query the underlying algorithm.
453 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
455 return tfm->__crt_alg->cra_name;
458 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
460 return tfm->__crt_alg->cra_driver_name;
463 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
465 return tfm->__crt_alg->cra_priority;
468 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
470 return module_name(tfm->__crt_alg->cra_module);
473 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
475 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
478 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
480 return tfm->__crt_alg->cra_blocksize;
483 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
485 return tfm->__crt_alg->cra_alignmask;
488 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
490 return tfm->crt_flags;
493 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
495 tfm->crt_flags |= flags;
498 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
500 tfm->crt_flags &= ~flags;
503 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
505 return tfm->__crt_ctx;
508 static inline unsigned int crypto_tfm_ctx_alignment(void)
510 struct crypto_tfm *tfm;
511 return __alignof__(tfm->__crt_ctx);
517 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
518 struct crypto_tfm *tfm)
520 return (struct crypto_ablkcipher *)tfm;
523 static inline struct crypto_ablkcipher *crypto_alloc_ablkcipher(
524 const char *alg_name, u32 type, u32 mask)
526 type &= ~CRYPTO_ALG_TYPE_MASK;
527 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
528 mask |= CRYPTO_ALG_TYPE_MASK;
530 return __crypto_ablkcipher_cast(
531 crypto_alloc_base(alg_name, type, mask));
534 static inline struct crypto_tfm *crypto_ablkcipher_tfm(
535 struct crypto_ablkcipher *tfm)
540 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
542 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
545 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
548 type &= ~CRYPTO_ALG_TYPE_MASK;
549 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
550 mask |= CRYPTO_ALG_TYPE_MASK;
552 return crypto_has_alg(alg_name, type, mask);
555 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
556 struct crypto_ablkcipher *tfm)
558 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
561 static inline unsigned int crypto_ablkcipher_ivsize(
562 struct crypto_ablkcipher *tfm)
564 return crypto_ablkcipher_crt(tfm)->ivsize;
567 static inline unsigned int crypto_ablkcipher_blocksize(
568 struct crypto_ablkcipher *tfm)
570 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
573 static inline unsigned int crypto_ablkcipher_alignmask(
574 struct crypto_ablkcipher *tfm)
576 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
579 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
581 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
584 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
587 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
590 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
593 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
596 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
597 const u8 *key, unsigned int keylen)
599 return crypto_ablkcipher_crt(tfm)->setkey(tfm, key, keylen);
602 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
603 struct ablkcipher_request *req)
605 return __crypto_ablkcipher_cast(req->base.tfm);
608 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
610 struct ablkcipher_tfm *crt =
611 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
612 return crt->encrypt(req);
615 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
617 struct ablkcipher_tfm *crt =
618 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
619 return crt->decrypt(req);
622 static inline int crypto_ablkcipher_reqsize(struct crypto_ablkcipher *tfm)
624 return crypto_ablkcipher_crt(tfm)->reqsize;
627 static inline void ablkcipher_request_set_tfm(
628 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
630 req->base.tfm = crypto_ablkcipher_tfm(tfm);
633 static inline struct ablkcipher_request *ablkcipher_request_cast(
634 struct crypto_async_request *req)
636 return container_of(req, struct ablkcipher_request, base);
639 static inline struct ablkcipher_request *ablkcipher_request_alloc(
640 struct crypto_ablkcipher *tfm, gfp_t gfp)
642 struct ablkcipher_request *req;
644 req = kmalloc(sizeof(struct ablkcipher_request) +
645 crypto_ablkcipher_reqsize(tfm), gfp);
648 ablkcipher_request_set_tfm(req, tfm);
653 static inline void ablkcipher_request_free(struct ablkcipher_request *req)
658 static inline void ablkcipher_request_set_callback(
659 struct ablkcipher_request *req,
660 u32 flags, crypto_completion_t complete, void *data)
662 req->base.complete = complete;
663 req->base.data = data;
664 req->base.flags = flags;
667 static inline void ablkcipher_request_set_crypt(
668 struct ablkcipher_request *req,
669 struct scatterlist *src, struct scatterlist *dst,
670 unsigned int nbytes, void *iv)
674 req->nbytes = nbytes;
678 static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
680 return (struct crypto_aead *)tfm;
683 static inline struct crypto_aead *crypto_alloc_aead(const char *alg_name,
686 type &= ~CRYPTO_ALG_TYPE_MASK;
687 type |= CRYPTO_ALG_TYPE_AEAD;
688 mask |= CRYPTO_ALG_TYPE_MASK;
690 return __crypto_aead_cast(crypto_alloc_base(alg_name, type, mask));
693 static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
698 static inline void crypto_free_aead(struct crypto_aead *tfm)
700 crypto_free_tfm(crypto_aead_tfm(tfm));
703 static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
705 return &crypto_aead_tfm(tfm)->crt_aead;
708 static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
710 return crypto_aead_crt(tfm)->ivsize;
713 static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
715 return crypto_aead_crt(tfm)->authsize;
718 static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
720 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
723 static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
725 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
728 static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
730 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
733 static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
735 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
738 static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
740 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
743 static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
746 return crypto_aead_crt(tfm)->setkey(tfm, key, keylen);
749 static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
751 return __crypto_aead_cast(req->base.tfm);
754 static inline int crypto_aead_encrypt(struct aead_request *req)
756 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
759 static inline int crypto_aead_decrypt(struct aead_request *req)
761 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
764 static inline int crypto_aead_reqsize(struct crypto_aead *tfm)
766 return crypto_aead_crt(tfm)->reqsize;
769 static inline void aead_request_set_tfm(struct aead_request *req,
770 struct crypto_aead *tfm)
772 req->base.tfm = crypto_aead_tfm(tfm);
775 static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
778 struct aead_request *req;
780 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
783 aead_request_set_tfm(req, tfm);
788 static inline void aead_request_free(struct aead_request *req)
793 static inline void aead_request_set_callback(struct aead_request *req,
795 crypto_completion_t complete,
798 req->base.complete = complete;
799 req->base.data = data;
800 req->base.flags = flags;
803 static inline void aead_request_set_crypt(struct aead_request *req,
804 struct scatterlist *src,
805 struct scatterlist *dst,
806 unsigned int cryptlen, u8 *iv)
810 req->cryptlen = cryptlen;
814 static inline void aead_request_set_assoc(struct aead_request *req,
815 struct scatterlist *assoc,
816 unsigned int assoclen)
819 req->assoclen = assoclen;
822 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
823 struct crypto_tfm *tfm)
825 return (struct crypto_blkcipher *)tfm;
828 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
829 struct crypto_tfm *tfm)
831 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
832 return __crypto_blkcipher_cast(tfm);
835 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
836 const char *alg_name, u32 type, u32 mask)
838 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
839 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
840 mask |= CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC;
842 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
845 static inline struct crypto_tfm *crypto_blkcipher_tfm(
846 struct crypto_blkcipher *tfm)
851 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
853 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
856 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
858 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
859 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
860 mask |= CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC;
862 return crypto_has_alg(alg_name, type, mask);
865 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
867 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
870 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
871 struct crypto_blkcipher *tfm)
873 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
876 static inline struct blkcipher_alg *crypto_blkcipher_alg(
877 struct crypto_blkcipher *tfm)
879 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
882 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
884 return crypto_blkcipher_alg(tfm)->ivsize;
887 static inline unsigned int crypto_blkcipher_blocksize(
888 struct crypto_blkcipher *tfm)
890 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
893 static inline unsigned int crypto_blkcipher_alignmask(
894 struct crypto_blkcipher *tfm)
896 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
899 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
901 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
904 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
907 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
910 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
913 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
916 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
917 const u8 *key, unsigned int keylen)
919 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
923 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
924 struct scatterlist *dst,
925 struct scatterlist *src,
928 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
929 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
932 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
933 struct scatterlist *dst,
934 struct scatterlist *src,
937 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
940 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
941 struct scatterlist *dst,
942 struct scatterlist *src,
945 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
946 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
949 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
950 struct scatterlist *dst,
951 struct scatterlist *src,
954 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
957 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
958 const u8 *src, unsigned int len)
960 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
963 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
964 u8 *dst, unsigned int len)
966 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
969 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
971 return (struct crypto_cipher *)tfm;
974 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
976 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
977 return __crypto_cipher_cast(tfm);
980 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
983 type &= ~CRYPTO_ALG_TYPE_MASK;
984 type |= CRYPTO_ALG_TYPE_CIPHER;
985 mask |= CRYPTO_ALG_TYPE_MASK;
987 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
990 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
995 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
997 crypto_free_tfm(crypto_cipher_tfm(tfm));
1000 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1002 type &= ~CRYPTO_ALG_TYPE_MASK;
1003 type |= CRYPTO_ALG_TYPE_CIPHER;
1004 mask |= CRYPTO_ALG_TYPE_MASK;
1006 return crypto_has_alg(alg_name, type, mask);
1009 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1011 return &crypto_cipher_tfm(tfm)->crt_cipher;
1014 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1016 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1019 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1021 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1024 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1026 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1029 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1032 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1035 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1038 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1041 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1042 const u8 *key, unsigned int keylen)
1044 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1048 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1049 u8 *dst, const u8 *src)
1051 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1055 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1056 u8 *dst, const u8 *src)
1058 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1062 static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
1064 return (struct crypto_hash *)tfm;
1067 static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
1069 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1070 CRYPTO_ALG_TYPE_HASH_MASK);
1071 return __crypto_hash_cast(tfm);
1074 static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1077 type &= ~CRYPTO_ALG_TYPE_MASK;
1078 type |= CRYPTO_ALG_TYPE_HASH;
1079 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1081 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1084 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1089 static inline void crypto_free_hash(struct crypto_hash *tfm)
1091 crypto_free_tfm(crypto_hash_tfm(tfm));
1094 static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1096 type &= ~CRYPTO_ALG_TYPE_MASK;
1097 type |= CRYPTO_ALG_TYPE_HASH;
1098 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1100 return crypto_has_alg(alg_name, type, mask);
1103 static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1105 return &crypto_hash_tfm(tfm)->crt_hash;
1108 static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1110 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1113 static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1115 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1118 static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1120 return crypto_hash_crt(tfm)->digestsize;
1123 static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1125 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1128 static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1130 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1133 static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1135 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1138 static inline int crypto_hash_init(struct hash_desc *desc)
1140 return crypto_hash_crt(desc->tfm)->init(desc);
1143 static inline int crypto_hash_update(struct hash_desc *desc,
1144 struct scatterlist *sg,
1145 unsigned int nbytes)
1147 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1150 static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1152 return crypto_hash_crt(desc->tfm)->final(desc, out);
1155 static inline int crypto_hash_digest(struct hash_desc *desc,
1156 struct scatterlist *sg,
1157 unsigned int nbytes, u8 *out)
1159 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1162 static inline int crypto_hash_setkey(struct crypto_hash *hash,
1163 const u8 *key, unsigned int keylen)
1165 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
1168 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1170 return (struct crypto_comp *)tfm;
1173 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1175 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1176 CRYPTO_ALG_TYPE_MASK);
1177 return __crypto_comp_cast(tfm);
1180 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1183 type &= ~CRYPTO_ALG_TYPE_MASK;
1184 type |= CRYPTO_ALG_TYPE_COMPRESS;
1185 mask |= CRYPTO_ALG_TYPE_MASK;
1187 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1190 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1195 static inline void crypto_free_comp(struct crypto_comp *tfm)
1197 crypto_free_tfm(crypto_comp_tfm(tfm));
1200 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1202 type &= ~CRYPTO_ALG_TYPE_MASK;
1203 type |= CRYPTO_ALG_TYPE_COMPRESS;
1204 mask |= CRYPTO_ALG_TYPE_MASK;
1206 return crypto_has_alg(alg_name, type, mask);
1209 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1211 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1214 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1216 return &crypto_comp_tfm(tfm)->crt_compress;
1219 static inline int crypto_comp_compress(struct crypto_comp *tfm,
1220 const u8 *src, unsigned int slen,
1221 u8 *dst, unsigned int *dlen)
1223 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1224 src, slen, dst, dlen);
1227 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
1228 const u8 *src, unsigned int slen,
1229 u8 *dst, unsigned int *dlen)
1231 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1232 src, slen, dst, dlen);
1235 #endif /* _LINUX_CRYPTO_H */