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
41 #define CRYPTO_ALG_TYPE_RNG 0x0000000c
42 #define CRYPTO_ALG_TYPE_SHASH 0x0000000d
44 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
45 #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
46 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
48 #define CRYPTO_ALG_LARVAL 0x00000010
49 #define CRYPTO_ALG_DEAD 0x00000020
50 #define CRYPTO_ALG_DYING 0x00000040
51 #define CRYPTO_ALG_ASYNC 0x00000080
54 * Set this bit if and only if the algorithm requires another algorithm of
55 * the same type to handle corner cases.
57 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
60 * This bit is set for symmetric key ciphers that have already been wrapped
61 * with a generic IV generator to prevent them from being wrapped again.
63 #define CRYPTO_ALG_GENIV 0x00000200
66 * Set if the algorithm has passed automated run-time testing. Note that
67 * if there is no run-time testing for a given algorithm it is considered
71 #define CRYPTO_ALG_TESTED 0x00000400
74 * Transform masks and values (for crt_flags).
76 #define CRYPTO_TFM_REQ_MASK 0x000fff00
77 #define CRYPTO_TFM_RES_MASK 0xfff00000
79 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
80 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
81 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
82 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
83 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
84 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
85 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
86 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
89 * Miscellaneous stuff.
91 #define CRYPTO_MAX_ALG_NAME 64
94 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
95 * declaration) is used to ensure that the crypto_tfm context structure is
96 * aligned correctly for the given architecture so that there are no alignment
97 * faults for C data types. In particular, this is required on platforms such
98 * as arm where pointers are 32-bit aligned but there are data types such as
99 * u64 which require 64-bit alignment.
101 #if defined(ARCH_KMALLOC_MINALIGN)
102 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
103 #elif defined(ARCH_SLAB_MINALIGN)
104 #define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
106 #define CRYPTO_MINALIGN __alignof__(unsigned long long)
109 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
112 struct crypto_ablkcipher;
113 struct crypto_async_request;
115 struct crypto_blkcipher;
121 struct aead_givcrypt_request;
122 struct skcipher_givcrypt_request;
124 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
126 struct crypto_async_request {
127 struct list_head list;
128 crypto_completion_t complete;
130 struct crypto_tfm *tfm;
135 struct ablkcipher_request {
136 struct crypto_async_request base;
142 struct scatterlist *src;
143 struct scatterlist *dst;
145 void *__ctx[] CRYPTO_MINALIGN_ATTR;
148 struct ahash_request {
149 struct crypto_async_request base;
152 struct scatterlist *src;
155 void *__ctx[] CRYPTO_MINALIGN_ATTR;
159 * struct aead_request - AEAD request
160 * @base: Common attributes for async crypto requests
161 * @assoclen: Length in bytes of associated data for authentication
162 * @cryptlen: Length of data to be encrypted or decrypted
163 * @iv: Initialisation vector
164 * @assoc: Associated data
166 * @dst: Destination data
167 * @__ctx: Start of private context data
169 struct aead_request {
170 struct crypto_async_request base;
172 unsigned int assoclen;
173 unsigned int cryptlen;
177 struct scatterlist *assoc;
178 struct scatterlist *src;
179 struct scatterlist *dst;
181 void *__ctx[] CRYPTO_MINALIGN_ATTR;
184 struct blkcipher_desc {
185 struct crypto_blkcipher *tfm;
191 struct crypto_tfm *tfm;
192 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
193 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
194 const u8 *src, unsigned int nbytes);
199 struct crypto_hash *tfm;
204 * Algorithms: modular crypto algorithm implementations, managed
205 * via crypto_register_alg() and crypto_unregister_alg().
207 struct ablkcipher_alg {
208 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
209 unsigned int keylen);
210 int (*encrypt)(struct ablkcipher_request *req);
211 int (*decrypt)(struct ablkcipher_request *req);
212 int (*givencrypt)(struct skcipher_givcrypt_request *req);
213 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
217 unsigned int min_keysize;
218 unsigned int max_keysize;
223 int (*init)(struct ahash_request *req);
224 int (*update)(struct ahash_request *req);
225 int (*final)(struct ahash_request *req);
226 int (*digest)(struct ahash_request *req);
227 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
228 unsigned int keylen);
230 unsigned int digestsize;
234 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
235 unsigned int keylen);
236 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
237 int (*encrypt)(struct aead_request *req);
238 int (*decrypt)(struct aead_request *req);
239 int (*givencrypt)(struct aead_givcrypt_request *req);
240 int (*givdecrypt)(struct aead_givcrypt_request *req);
245 unsigned int maxauthsize;
248 struct blkcipher_alg {
249 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
250 unsigned int keylen);
251 int (*encrypt)(struct blkcipher_desc *desc,
252 struct scatterlist *dst, struct scatterlist *src,
253 unsigned int nbytes);
254 int (*decrypt)(struct blkcipher_desc *desc,
255 struct scatterlist *dst, struct scatterlist *src,
256 unsigned int nbytes);
260 unsigned int min_keysize;
261 unsigned int max_keysize;
266 unsigned int cia_min_keysize;
267 unsigned int cia_max_keysize;
268 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
269 unsigned int keylen);
270 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
271 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
275 unsigned int dia_digestsize;
276 void (*dia_init)(struct crypto_tfm *tfm);
277 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
279 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
280 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
281 unsigned int keylen);
285 int (*init)(struct hash_desc *desc);
286 int (*update)(struct hash_desc *desc, struct scatterlist *sg,
287 unsigned int nbytes);
288 int (*final)(struct hash_desc *desc, u8 *out);
289 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
290 unsigned int nbytes, u8 *out);
291 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
292 unsigned int keylen);
294 unsigned int digestsize;
297 struct compress_alg {
298 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
299 unsigned int slen, u8 *dst, unsigned int *dlen);
300 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
301 unsigned int slen, u8 *dst, unsigned int *dlen);
305 int (*rng_make_random)(struct crypto_rng *tfm, u8 *rdata,
307 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
309 unsigned int seedsize;
313 #define cra_ablkcipher cra_u.ablkcipher
314 #define cra_aead cra_u.aead
315 #define cra_blkcipher cra_u.blkcipher
316 #define cra_cipher cra_u.cipher
317 #define cra_digest cra_u.digest
318 #define cra_hash cra_u.hash
319 #define cra_ahash cra_u.ahash
320 #define cra_compress cra_u.compress
321 #define cra_rng cra_u.rng
324 struct list_head cra_list;
325 struct list_head cra_users;
328 unsigned int cra_blocksize;
329 unsigned int cra_ctxsize;
330 unsigned int cra_alignmask;
335 char cra_name[CRYPTO_MAX_ALG_NAME];
336 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
338 const struct crypto_type *cra_type;
341 struct ablkcipher_alg ablkcipher;
342 struct aead_alg aead;
343 struct blkcipher_alg blkcipher;
344 struct cipher_alg cipher;
345 struct digest_alg digest;
346 struct hash_alg hash;
347 struct ahash_alg ahash;
348 struct compress_alg compress;
352 int (*cra_init)(struct crypto_tfm *tfm);
353 void (*cra_exit)(struct crypto_tfm *tfm);
354 void (*cra_destroy)(struct crypto_alg *alg);
356 struct module *cra_module;
360 * Algorithm registration interface.
362 int crypto_register_alg(struct crypto_alg *alg);
363 int crypto_unregister_alg(struct crypto_alg *alg);
366 * Algorithm query interface.
368 int crypto_has_alg(const char *name, u32 type, u32 mask);
371 * Transforms: user-instantiated objects which encapsulate algorithms
372 * and core processing logic. Managed via crypto_alloc_*() and
373 * crypto_free_*(), as well as the various helpers below.
376 struct ablkcipher_tfm {
377 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
378 unsigned int keylen);
379 int (*encrypt)(struct ablkcipher_request *req);
380 int (*decrypt)(struct ablkcipher_request *req);
381 int (*givencrypt)(struct skcipher_givcrypt_request *req);
382 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
384 struct crypto_ablkcipher *base;
387 unsigned int reqsize;
391 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
392 unsigned int keylen);
393 int (*encrypt)(struct aead_request *req);
394 int (*decrypt)(struct aead_request *req);
395 int (*givencrypt)(struct aead_givcrypt_request *req);
396 int (*givdecrypt)(struct aead_givcrypt_request *req);
398 struct crypto_aead *base;
401 unsigned int authsize;
402 unsigned int reqsize;
405 struct blkcipher_tfm {
407 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
408 unsigned int keylen);
409 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
410 struct scatterlist *src, unsigned int nbytes);
411 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
412 struct scatterlist *src, unsigned int nbytes);
416 int (*cit_setkey)(struct crypto_tfm *tfm,
417 const u8 *key, unsigned int keylen);
418 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
419 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
423 int (*init)(struct hash_desc *desc);
424 int (*update)(struct hash_desc *desc,
425 struct scatterlist *sg, unsigned int nsg);
426 int (*final)(struct hash_desc *desc, u8 *out);
427 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
428 unsigned int nsg, u8 *out);
429 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
430 unsigned int keylen);
431 unsigned int digestsize;
435 int (*init)(struct ahash_request *req);
436 int (*update)(struct ahash_request *req);
437 int (*final)(struct ahash_request *req);
438 int (*digest)(struct ahash_request *req);
439 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
440 unsigned int keylen);
442 unsigned int digestsize;
443 unsigned int reqsize;
446 struct compress_tfm {
447 int (*cot_compress)(struct crypto_tfm *tfm,
448 const u8 *src, unsigned int slen,
449 u8 *dst, unsigned int *dlen);
450 int (*cot_decompress)(struct crypto_tfm *tfm,
451 const u8 *src, unsigned int slen,
452 u8 *dst, unsigned int *dlen);
456 int (*rng_gen_random)(struct crypto_rng *tfm, u8 *rdata,
458 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
461 #define crt_ablkcipher crt_u.ablkcipher
462 #define crt_aead crt_u.aead
463 #define crt_blkcipher crt_u.blkcipher
464 #define crt_cipher crt_u.cipher
465 #define crt_hash crt_u.hash
466 #define crt_ahash crt_u.ahash
467 #define crt_compress crt_u.compress
468 #define crt_rng crt_u.rng
475 struct ablkcipher_tfm ablkcipher;
476 struct aead_tfm aead;
477 struct blkcipher_tfm blkcipher;
478 struct cipher_tfm cipher;
479 struct hash_tfm hash;
480 struct ahash_tfm ahash;
481 struct compress_tfm compress;
485 void (*exit)(struct crypto_tfm *tfm);
487 struct crypto_alg *__crt_alg;
489 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
492 struct crypto_ablkcipher {
493 struct crypto_tfm base;
497 struct crypto_tfm base;
500 struct crypto_blkcipher {
501 struct crypto_tfm base;
504 struct crypto_cipher {
505 struct crypto_tfm base;
509 struct crypto_tfm base;
513 struct crypto_tfm base;
517 struct crypto_tfm base;
528 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
530 /* Maximum number of (rtattr) parameters for each template. */
531 #define CRYPTO_MAX_ATTRS 32
533 struct crypto_attr_alg {
534 char name[CRYPTO_MAX_ALG_NAME];
537 struct crypto_attr_type {
542 struct crypto_attr_u32 {
547 * Transform user interface.
550 struct crypto_tfm *crypto_alloc_tfm(const char *alg_name,
551 const struct crypto_type *frontend,
553 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
554 void crypto_free_tfm(struct crypto_tfm *tfm);
556 int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
559 * Transform helpers which query the underlying algorithm.
561 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
563 return tfm->__crt_alg->cra_name;
566 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
568 return tfm->__crt_alg->cra_driver_name;
571 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
573 return tfm->__crt_alg->cra_priority;
576 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
578 return module_name(tfm->__crt_alg->cra_module);
581 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
583 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
586 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
588 return tfm->__crt_alg->cra_blocksize;
591 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
593 return tfm->__crt_alg->cra_alignmask;
596 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
598 return tfm->crt_flags;
601 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
603 tfm->crt_flags |= flags;
606 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
608 tfm->crt_flags &= ~flags;
611 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
613 return tfm->__crt_ctx;
616 static inline unsigned int crypto_tfm_ctx_alignment(void)
618 struct crypto_tfm *tfm;
619 return __alignof__(tfm->__crt_ctx);
625 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
626 struct crypto_tfm *tfm)
628 return (struct crypto_ablkcipher *)tfm;
631 static inline u32 crypto_skcipher_type(u32 type)
633 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
634 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
638 static inline u32 crypto_skcipher_mask(u32 mask)
640 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
641 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
645 struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
648 static inline struct crypto_tfm *crypto_ablkcipher_tfm(
649 struct crypto_ablkcipher *tfm)
654 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
656 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
659 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
662 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
663 crypto_skcipher_mask(mask));
666 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
667 struct crypto_ablkcipher *tfm)
669 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
672 static inline unsigned int crypto_ablkcipher_ivsize(
673 struct crypto_ablkcipher *tfm)
675 return crypto_ablkcipher_crt(tfm)->ivsize;
678 static inline unsigned int crypto_ablkcipher_blocksize(
679 struct crypto_ablkcipher *tfm)
681 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
684 static inline unsigned int crypto_ablkcipher_alignmask(
685 struct crypto_ablkcipher *tfm)
687 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
690 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
692 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
695 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
698 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
701 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
704 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
707 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
708 const u8 *key, unsigned int keylen)
710 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
712 return crt->setkey(crt->base, key, keylen);
715 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
716 struct ablkcipher_request *req)
718 return __crypto_ablkcipher_cast(req->base.tfm);
721 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
723 struct ablkcipher_tfm *crt =
724 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
725 return crt->encrypt(req);
728 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
730 struct ablkcipher_tfm *crt =
731 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
732 return crt->decrypt(req);
735 static inline unsigned int crypto_ablkcipher_reqsize(
736 struct crypto_ablkcipher *tfm)
738 return crypto_ablkcipher_crt(tfm)->reqsize;
741 static inline void ablkcipher_request_set_tfm(
742 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
744 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
747 static inline struct ablkcipher_request *ablkcipher_request_cast(
748 struct crypto_async_request *req)
750 return container_of(req, struct ablkcipher_request, base);
753 static inline struct ablkcipher_request *ablkcipher_request_alloc(
754 struct crypto_ablkcipher *tfm, gfp_t gfp)
756 struct ablkcipher_request *req;
758 req = kmalloc(sizeof(struct ablkcipher_request) +
759 crypto_ablkcipher_reqsize(tfm), gfp);
762 ablkcipher_request_set_tfm(req, tfm);
767 static inline void ablkcipher_request_free(struct ablkcipher_request *req)
772 static inline void ablkcipher_request_set_callback(
773 struct ablkcipher_request *req,
774 u32 flags, crypto_completion_t complete, void *data)
776 req->base.complete = complete;
777 req->base.data = data;
778 req->base.flags = flags;
781 static inline void ablkcipher_request_set_crypt(
782 struct ablkcipher_request *req,
783 struct scatterlist *src, struct scatterlist *dst,
784 unsigned int nbytes, void *iv)
788 req->nbytes = nbytes;
792 static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
794 return (struct crypto_aead *)tfm;
797 struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
799 static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
804 static inline void crypto_free_aead(struct crypto_aead *tfm)
806 crypto_free_tfm(crypto_aead_tfm(tfm));
809 static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
811 return &crypto_aead_tfm(tfm)->crt_aead;
814 static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
816 return crypto_aead_crt(tfm)->ivsize;
819 static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
821 return crypto_aead_crt(tfm)->authsize;
824 static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
826 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
829 static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
831 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
834 static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
836 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
839 static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
841 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
844 static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
846 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
849 static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
852 struct aead_tfm *crt = crypto_aead_crt(tfm);
854 return crt->setkey(crt->base, key, keylen);
857 int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
859 static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
861 return __crypto_aead_cast(req->base.tfm);
864 static inline int crypto_aead_encrypt(struct aead_request *req)
866 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
869 static inline int crypto_aead_decrypt(struct aead_request *req)
871 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
874 static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
876 return crypto_aead_crt(tfm)->reqsize;
879 static inline void aead_request_set_tfm(struct aead_request *req,
880 struct crypto_aead *tfm)
882 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
885 static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
888 struct aead_request *req;
890 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
893 aead_request_set_tfm(req, tfm);
898 static inline void aead_request_free(struct aead_request *req)
903 static inline void aead_request_set_callback(struct aead_request *req,
905 crypto_completion_t complete,
908 req->base.complete = complete;
909 req->base.data = data;
910 req->base.flags = flags;
913 static inline void aead_request_set_crypt(struct aead_request *req,
914 struct scatterlist *src,
915 struct scatterlist *dst,
916 unsigned int cryptlen, u8 *iv)
920 req->cryptlen = cryptlen;
924 static inline void aead_request_set_assoc(struct aead_request *req,
925 struct scatterlist *assoc,
926 unsigned int assoclen)
929 req->assoclen = assoclen;
932 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
933 struct crypto_tfm *tfm)
935 return (struct crypto_blkcipher *)tfm;
938 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
939 struct crypto_tfm *tfm)
941 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
942 return __crypto_blkcipher_cast(tfm);
945 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
946 const char *alg_name, u32 type, u32 mask)
948 type &= ~CRYPTO_ALG_TYPE_MASK;
949 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
950 mask |= CRYPTO_ALG_TYPE_MASK;
952 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
955 static inline struct crypto_tfm *crypto_blkcipher_tfm(
956 struct crypto_blkcipher *tfm)
961 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
963 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
966 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
968 type &= ~CRYPTO_ALG_TYPE_MASK;
969 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
970 mask |= CRYPTO_ALG_TYPE_MASK;
972 return crypto_has_alg(alg_name, type, mask);
975 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
977 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
980 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
981 struct crypto_blkcipher *tfm)
983 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
986 static inline struct blkcipher_alg *crypto_blkcipher_alg(
987 struct crypto_blkcipher *tfm)
989 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
992 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
994 return crypto_blkcipher_alg(tfm)->ivsize;
997 static inline unsigned int crypto_blkcipher_blocksize(
998 struct crypto_blkcipher *tfm)
1000 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1003 static inline unsigned int crypto_blkcipher_alignmask(
1004 struct crypto_blkcipher *tfm)
1006 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1009 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1011 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1014 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1017 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1020 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1023 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1026 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1027 const u8 *key, unsigned int keylen)
1029 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1033 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1034 struct scatterlist *dst,
1035 struct scatterlist *src,
1036 unsigned int nbytes)
1038 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1039 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1042 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1043 struct scatterlist *dst,
1044 struct scatterlist *src,
1045 unsigned int nbytes)
1047 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1050 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1051 struct scatterlist *dst,
1052 struct scatterlist *src,
1053 unsigned int nbytes)
1055 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1056 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1059 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1060 struct scatterlist *dst,
1061 struct scatterlist *src,
1062 unsigned int nbytes)
1064 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1067 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1068 const u8 *src, unsigned int len)
1070 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1073 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1074 u8 *dst, unsigned int len)
1076 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1079 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1081 return (struct crypto_cipher *)tfm;
1084 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1086 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1087 return __crypto_cipher_cast(tfm);
1090 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1093 type &= ~CRYPTO_ALG_TYPE_MASK;
1094 type |= CRYPTO_ALG_TYPE_CIPHER;
1095 mask |= CRYPTO_ALG_TYPE_MASK;
1097 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1100 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1105 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1107 crypto_free_tfm(crypto_cipher_tfm(tfm));
1110 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1112 type &= ~CRYPTO_ALG_TYPE_MASK;
1113 type |= CRYPTO_ALG_TYPE_CIPHER;
1114 mask |= CRYPTO_ALG_TYPE_MASK;
1116 return crypto_has_alg(alg_name, type, mask);
1119 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1121 return &crypto_cipher_tfm(tfm)->crt_cipher;
1124 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1126 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1129 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1131 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1134 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1136 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1139 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1142 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1145 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1148 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1151 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1152 const u8 *key, unsigned int keylen)
1154 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1158 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1159 u8 *dst, const u8 *src)
1161 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1165 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1166 u8 *dst, const u8 *src)
1168 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1172 static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
1174 return (struct crypto_hash *)tfm;
1177 static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
1179 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1180 CRYPTO_ALG_TYPE_HASH_MASK);
1181 return __crypto_hash_cast(tfm);
1184 static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1187 type &= ~CRYPTO_ALG_TYPE_MASK;
1188 mask &= ~CRYPTO_ALG_TYPE_MASK;
1189 type |= CRYPTO_ALG_TYPE_HASH;
1190 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1192 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1195 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1200 static inline void crypto_free_hash(struct crypto_hash *tfm)
1202 crypto_free_tfm(crypto_hash_tfm(tfm));
1205 static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1207 type &= ~CRYPTO_ALG_TYPE_MASK;
1208 mask &= ~CRYPTO_ALG_TYPE_MASK;
1209 type |= CRYPTO_ALG_TYPE_HASH;
1210 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1212 return crypto_has_alg(alg_name, type, mask);
1215 static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1217 return &crypto_hash_tfm(tfm)->crt_hash;
1220 static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1222 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1225 static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1227 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1230 static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1232 return crypto_hash_crt(tfm)->digestsize;
1235 static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1237 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1240 static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1242 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1245 static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1247 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1250 static inline int crypto_hash_init(struct hash_desc *desc)
1252 return crypto_hash_crt(desc->tfm)->init(desc);
1255 static inline int crypto_hash_update(struct hash_desc *desc,
1256 struct scatterlist *sg,
1257 unsigned int nbytes)
1259 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1262 static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1264 return crypto_hash_crt(desc->tfm)->final(desc, out);
1267 static inline int crypto_hash_digest(struct hash_desc *desc,
1268 struct scatterlist *sg,
1269 unsigned int nbytes, u8 *out)
1271 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1274 static inline int crypto_hash_setkey(struct crypto_hash *hash,
1275 const u8 *key, unsigned int keylen)
1277 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
1280 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1282 return (struct crypto_comp *)tfm;
1285 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1287 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1288 CRYPTO_ALG_TYPE_MASK);
1289 return __crypto_comp_cast(tfm);
1292 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1295 type &= ~CRYPTO_ALG_TYPE_MASK;
1296 type |= CRYPTO_ALG_TYPE_COMPRESS;
1297 mask |= CRYPTO_ALG_TYPE_MASK;
1299 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1302 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1307 static inline void crypto_free_comp(struct crypto_comp *tfm)
1309 crypto_free_tfm(crypto_comp_tfm(tfm));
1312 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1314 type &= ~CRYPTO_ALG_TYPE_MASK;
1315 type |= CRYPTO_ALG_TYPE_COMPRESS;
1316 mask |= CRYPTO_ALG_TYPE_MASK;
1318 return crypto_has_alg(alg_name, type, mask);
1321 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1323 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1326 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1328 return &crypto_comp_tfm(tfm)->crt_compress;
1331 static inline int crypto_comp_compress(struct crypto_comp *tfm,
1332 const u8 *src, unsigned int slen,
1333 u8 *dst, unsigned int *dlen)
1335 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1336 src, slen, dst, dlen);
1339 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
1340 const u8 *src, unsigned int slen,
1341 u8 *dst, unsigned int *dlen)
1343 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1344 src, slen, dst, dlen);
1347 #endif /* _LINUX_CRYPTO_H */