include: replace linux/module.h with "struct module" wherever possible
[linux-2.6/next.git] / include / linux / crypto.h
blob1e51f9a491aedc2e0822cec53aa9b8da0dcdb5b1
1 /*
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)
14 * any later version.
17 #ifndef _LINUX_CRYPTO_H
18 #define _LINUX_CRYPTO_H
20 #include <linux/atomic.h>
21 #include <linux/kernel.h>
22 #include <linux/list.h>
23 #include <linux/slab.h>
24 #include <linux/string.h>
25 #include <linux/uaccess.h>
28 * Algorithm masks and types.
30 #define CRYPTO_ALG_TYPE_MASK 0x0000000f
31 #define CRYPTO_ALG_TYPE_CIPHER 0x00000001
32 #define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
33 #define CRYPTO_ALG_TYPE_AEAD 0x00000003
34 #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
35 #define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
36 #define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
37 #define CRYPTO_ALG_TYPE_DIGEST 0x00000008
38 #define CRYPTO_ALG_TYPE_HASH 0x00000008
39 #define CRYPTO_ALG_TYPE_SHASH 0x00000009
40 #define CRYPTO_ALG_TYPE_AHASH 0x0000000a
41 #define CRYPTO_ALG_TYPE_RNG 0x0000000c
42 #define CRYPTO_ALG_TYPE_PCOMPRESS 0x0000000f
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
68 * to have passed.
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 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
103 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
105 struct scatterlist;
106 struct crypto_ablkcipher;
107 struct crypto_async_request;
108 struct crypto_aead;
109 struct crypto_blkcipher;
110 struct crypto_hash;
111 struct crypto_rng;
112 struct crypto_tfm;
113 struct crypto_type;
114 struct aead_givcrypt_request;
115 struct skcipher_givcrypt_request;
117 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
119 struct crypto_async_request {
120 struct list_head list;
121 crypto_completion_t complete;
122 void *data;
123 struct crypto_tfm *tfm;
125 u32 flags;
128 struct ablkcipher_request {
129 struct crypto_async_request base;
131 unsigned int nbytes;
133 void *info;
135 struct scatterlist *src;
136 struct scatterlist *dst;
138 void *__ctx[] CRYPTO_MINALIGN_ATTR;
142 * struct aead_request - AEAD request
143 * @base: Common attributes for async crypto requests
144 * @assoclen: Length in bytes of associated data for authentication
145 * @cryptlen: Length of data to be encrypted or decrypted
146 * @iv: Initialisation vector
147 * @assoc: Associated data
148 * @src: Source data
149 * @dst: Destination data
150 * @__ctx: Start of private context data
152 struct aead_request {
153 struct crypto_async_request base;
155 unsigned int assoclen;
156 unsigned int cryptlen;
158 u8 *iv;
160 struct scatterlist *assoc;
161 struct scatterlist *src;
162 struct scatterlist *dst;
164 void *__ctx[] CRYPTO_MINALIGN_ATTR;
167 struct blkcipher_desc {
168 struct crypto_blkcipher *tfm;
169 void *info;
170 u32 flags;
173 struct cipher_desc {
174 struct crypto_tfm *tfm;
175 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
176 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
177 const u8 *src, unsigned int nbytes);
178 void *info;
181 struct hash_desc {
182 struct crypto_hash *tfm;
183 u32 flags;
187 * Algorithms: modular crypto algorithm implementations, managed
188 * via crypto_register_alg() and crypto_unregister_alg().
190 struct ablkcipher_alg {
191 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
192 unsigned int keylen);
193 int (*encrypt)(struct ablkcipher_request *req);
194 int (*decrypt)(struct ablkcipher_request *req);
195 int (*givencrypt)(struct skcipher_givcrypt_request *req);
196 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
198 const char *geniv;
200 unsigned int min_keysize;
201 unsigned int max_keysize;
202 unsigned int ivsize;
205 struct aead_alg {
206 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
207 unsigned int keylen);
208 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
209 int (*encrypt)(struct aead_request *req);
210 int (*decrypt)(struct aead_request *req);
211 int (*givencrypt)(struct aead_givcrypt_request *req);
212 int (*givdecrypt)(struct aead_givcrypt_request *req);
214 const char *geniv;
216 unsigned int ivsize;
217 unsigned int maxauthsize;
220 struct blkcipher_alg {
221 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
222 unsigned int keylen);
223 int (*encrypt)(struct blkcipher_desc *desc,
224 struct scatterlist *dst, struct scatterlist *src,
225 unsigned int nbytes);
226 int (*decrypt)(struct blkcipher_desc *desc,
227 struct scatterlist *dst, struct scatterlist *src,
228 unsigned int nbytes);
230 const char *geniv;
232 unsigned int min_keysize;
233 unsigned int max_keysize;
234 unsigned int ivsize;
237 struct cipher_alg {
238 unsigned int cia_min_keysize;
239 unsigned int cia_max_keysize;
240 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
241 unsigned int keylen);
242 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
243 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
246 struct compress_alg {
247 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
248 unsigned int slen, u8 *dst, unsigned int *dlen);
249 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
250 unsigned int slen, u8 *dst, unsigned int *dlen);
253 struct rng_alg {
254 int (*rng_make_random)(struct crypto_rng *tfm, u8 *rdata,
255 unsigned int dlen);
256 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
258 unsigned int seedsize;
262 #define cra_ablkcipher cra_u.ablkcipher
263 #define cra_aead cra_u.aead
264 #define cra_blkcipher cra_u.blkcipher
265 #define cra_cipher cra_u.cipher
266 #define cra_compress cra_u.compress
267 #define cra_rng cra_u.rng
269 struct crypto_alg {
270 struct list_head cra_list;
271 struct list_head cra_users;
273 u32 cra_flags;
274 unsigned int cra_blocksize;
275 unsigned int cra_ctxsize;
276 unsigned int cra_alignmask;
278 int cra_priority;
279 atomic_t cra_refcnt;
281 char cra_name[CRYPTO_MAX_ALG_NAME];
282 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
284 const struct crypto_type *cra_type;
286 union {
287 struct ablkcipher_alg ablkcipher;
288 struct aead_alg aead;
289 struct blkcipher_alg blkcipher;
290 struct cipher_alg cipher;
291 struct compress_alg compress;
292 struct rng_alg rng;
293 } cra_u;
295 int (*cra_init)(struct crypto_tfm *tfm);
296 void (*cra_exit)(struct crypto_tfm *tfm);
297 void (*cra_destroy)(struct crypto_alg *alg);
299 struct module *cra_module;
303 * Algorithm registration interface.
305 int crypto_register_alg(struct crypto_alg *alg);
306 int crypto_unregister_alg(struct crypto_alg *alg);
309 * Algorithm query interface.
311 int crypto_has_alg(const char *name, u32 type, u32 mask);
314 * Transforms: user-instantiated objects which encapsulate algorithms
315 * and core processing logic. Managed via crypto_alloc_*() and
316 * crypto_free_*(), as well as the various helpers below.
319 struct ablkcipher_tfm {
320 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
321 unsigned int keylen);
322 int (*encrypt)(struct ablkcipher_request *req);
323 int (*decrypt)(struct ablkcipher_request *req);
324 int (*givencrypt)(struct skcipher_givcrypt_request *req);
325 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
327 struct crypto_ablkcipher *base;
329 unsigned int ivsize;
330 unsigned int reqsize;
333 struct aead_tfm {
334 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
335 unsigned int keylen);
336 int (*encrypt)(struct aead_request *req);
337 int (*decrypt)(struct aead_request *req);
338 int (*givencrypt)(struct aead_givcrypt_request *req);
339 int (*givdecrypt)(struct aead_givcrypt_request *req);
341 struct crypto_aead *base;
343 unsigned int ivsize;
344 unsigned int authsize;
345 unsigned int reqsize;
348 struct blkcipher_tfm {
349 void *iv;
350 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
351 unsigned int keylen);
352 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
353 struct scatterlist *src, unsigned int nbytes);
354 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
355 struct scatterlist *src, unsigned int nbytes);
358 struct cipher_tfm {
359 int (*cit_setkey)(struct crypto_tfm *tfm,
360 const u8 *key, unsigned int keylen);
361 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
362 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
365 struct hash_tfm {
366 int (*init)(struct hash_desc *desc);
367 int (*update)(struct hash_desc *desc,
368 struct scatterlist *sg, unsigned int nsg);
369 int (*final)(struct hash_desc *desc, u8 *out);
370 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
371 unsigned int nsg, u8 *out);
372 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
373 unsigned int keylen);
374 unsigned int digestsize;
377 struct compress_tfm {
378 int (*cot_compress)(struct crypto_tfm *tfm,
379 const u8 *src, unsigned int slen,
380 u8 *dst, unsigned int *dlen);
381 int (*cot_decompress)(struct crypto_tfm *tfm,
382 const u8 *src, unsigned int slen,
383 u8 *dst, unsigned int *dlen);
386 struct rng_tfm {
387 int (*rng_gen_random)(struct crypto_rng *tfm, u8 *rdata,
388 unsigned int dlen);
389 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
392 #define crt_ablkcipher crt_u.ablkcipher
393 #define crt_aead crt_u.aead
394 #define crt_blkcipher crt_u.blkcipher
395 #define crt_cipher crt_u.cipher
396 #define crt_hash crt_u.hash
397 #define crt_compress crt_u.compress
398 #define crt_rng crt_u.rng
400 struct crypto_tfm {
402 u32 crt_flags;
404 union {
405 struct ablkcipher_tfm ablkcipher;
406 struct aead_tfm aead;
407 struct blkcipher_tfm blkcipher;
408 struct cipher_tfm cipher;
409 struct hash_tfm hash;
410 struct compress_tfm compress;
411 struct rng_tfm rng;
412 } crt_u;
414 void (*exit)(struct crypto_tfm *tfm);
416 struct crypto_alg *__crt_alg;
418 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
421 struct crypto_ablkcipher {
422 struct crypto_tfm base;
425 struct crypto_aead {
426 struct crypto_tfm base;
429 struct crypto_blkcipher {
430 struct crypto_tfm base;
433 struct crypto_cipher {
434 struct crypto_tfm base;
437 struct crypto_comp {
438 struct crypto_tfm base;
441 struct crypto_hash {
442 struct crypto_tfm base;
445 struct crypto_rng {
446 struct crypto_tfm base;
449 enum {
450 CRYPTOA_UNSPEC,
451 CRYPTOA_ALG,
452 CRYPTOA_TYPE,
453 CRYPTOA_U32,
454 __CRYPTOA_MAX,
457 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
459 /* Maximum number of (rtattr) parameters for each template. */
460 #define CRYPTO_MAX_ATTRS 32
462 struct crypto_attr_alg {
463 char name[CRYPTO_MAX_ALG_NAME];
466 struct crypto_attr_type {
467 u32 type;
468 u32 mask;
471 struct crypto_attr_u32 {
472 u32 num;
476 * Transform user interface.
479 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
480 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
482 static inline void crypto_free_tfm(struct crypto_tfm *tfm)
484 return crypto_destroy_tfm(tfm, tfm);
487 int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
490 * Transform helpers which query the underlying algorithm.
492 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
494 return tfm->__crt_alg->cra_name;
497 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
499 return tfm->__crt_alg->cra_driver_name;
502 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
504 return tfm->__crt_alg->cra_priority;
507 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
509 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
512 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
514 return tfm->__crt_alg->cra_blocksize;
517 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
519 return tfm->__crt_alg->cra_alignmask;
522 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
524 return tfm->crt_flags;
527 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
529 tfm->crt_flags |= flags;
532 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
534 tfm->crt_flags &= ~flags;
537 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
539 return tfm->__crt_ctx;
542 static inline unsigned int crypto_tfm_ctx_alignment(void)
544 struct crypto_tfm *tfm;
545 return __alignof__(tfm->__crt_ctx);
549 * API wrappers.
551 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
552 struct crypto_tfm *tfm)
554 return (struct crypto_ablkcipher *)tfm;
557 static inline u32 crypto_skcipher_type(u32 type)
559 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
560 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
561 return type;
564 static inline u32 crypto_skcipher_mask(u32 mask)
566 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
567 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
568 return mask;
571 struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
572 u32 type, u32 mask);
574 static inline struct crypto_tfm *crypto_ablkcipher_tfm(
575 struct crypto_ablkcipher *tfm)
577 return &tfm->base;
580 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
582 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
585 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
586 u32 mask)
588 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
589 crypto_skcipher_mask(mask));
592 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
593 struct crypto_ablkcipher *tfm)
595 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
598 static inline unsigned int crypto_ablkcipher_ivsize(
599 struct crypto_ablkcipher *tfm)
601 return crypto_ablkcipher_crt(tfm)->ivsize;
604 static inline unsigned int crypto_ablkcipher_blocksize(
605 struct crypto_ablkcipher *tfm)
607 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
610 static inline unsigned int crypto_ablkcipher_alignmask(
611 struct crypto_ablkcipher *tfm)
613 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
616 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
618 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
621 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
622 u32 flags)
624 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
627 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
628 u32 flags)
630 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
633 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
634 const u8 *key, unsigned int keylen)
636 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
638 return crt->setkey(crt->base, key, keylen);
641 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
642 struct ablkcipher_request *req)
644 return __crypto_ablkcipher_cast(req->base.tfm);
647 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
649 struct ablkcipher_tfm *crt =
650 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
651 return crt->encrypt(req);
654 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
656 struct ablkcipher_tfm *crt =
657 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
658 return crt->decrypt(req);
661 static inline unsigned int crypto_ablkcipher_reqsize(
662 struct crypto_ablkcipher *tfm)
664 return crypto_ablkcipher_crt(tfm)->reqsize;
667 static inline void ablkcipher_request_set_tfm(
668 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
670 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
673 static inline struct ablkcipher_request *ablkcipher_request_cast(
674 struct crypto_async_request *req)
676 return container_of(req, struct ablkcipher_request, base);
679 static inline struct ablkcipher_request *ablkcipher_request_alloc(
680 struct crypto_ablkcipher *tfm, gfp_t gfp)
682 struct ablkcipher_request *req;
684 req = kmalloc(sizeof(struct ablkcipher_request) +
685 crypto_ablkcipher_reqsize(tfm), gfp);
687 if (likely(req))
688 ablkcipher_request_set_tfm(req, tfm);
690 return req;
693 static inline void ablkcipher_request_free(struct ablkcipher_request *req)
695 kzfree(req);
698 static inline void ablkcipher_request_set_callback(
699 struct ablkcipher_request *req,
700 u32 flags, crypto_completion_t complete, void *data)
702 req->base.complete = complete;
703 req->base.data = data;
704 req->base.flags = flags;
707 static inline void ablkcipher_request_set_crypt(
708 struct ablkcipher_request *req,
709 struct scatterlist *src, struct scatterlist *dst,
710 unsigned int nbytes, void *iv)
712 req->src = src;
713 req->dst = dst;
714 req->nbytes = nbytes;
715 req->info = iv;
718 static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
720 return (struct crypto_aead *)tfm;
723 struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
725 static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
727 return &tfm->base;
730 static inline void crypto_free_aead(struct crypto_aead *tfm)
732 crypto_free_tfm(crypto_aead_tfm(tfm));
735 static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
737 return &crypto_aead_tfm(tfm)->crt_aead;
740 static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
742 return crypto_aead_crt(tfm)->ivsize;
745 static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
747 return crypto_aead_crt(tfm)->authsize;
750 static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
752 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
755 static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
757 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
760 static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
762 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
765 static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
767 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
770 static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
772 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
775 static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
776 unsigned int keylen)
778 struct aead_tfm *crt = crypto_aead_crt(tfm);
780 return crt->setkey(crt->base, key, keylen);
783 int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
785 static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
787 return __crypto_aead_cast(req->base.tfm);
790 static inline int crypto_aead_encrypt(struct aead_request *req)
792 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
795 static inline int crypto_aead_decrypt(struct aead_request *req)
797 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
800 static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
802 return crypto_aead_crt(tfm)->reqsize;
805 static inline void aead_request_set_tfm(struct aead_request *req,
806 struct crypto_aead *tfm)
808 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
811 static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
812 gfp_t gfp)
814 struct aead_request *req;
816 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
818 if (likely(req))
819 aead_request_set_tfm(req, tfm);
821 return req;
824 static inline void aead_request_free(struct aead_request *req)
826 kzfree(req);
829 static inline void aead_request_set_callback(struct aead_request *req,
830 u32 flags,
831 crypto_completion_t complete,
832 void *data)
834 req->base.complete = complete;
835 req->base.data = data;
836 req->base.flags = flags;
839 static inline void aead_request_set_crypt(struct aead_request *req,
840 struct scatterlist *src,
841 struct scatterlist *dst,
842 unsigned int cryptlen, u8 *iv)
844 req->src = src;
845 req->dst = dst;
846 req->cryptlen = cryptlen;
847 req->iv = iv;
850 static inline void aead_request_set_assoc(struct aead_request *req,
851 struct scatterlist *assoc,
852 unsigned int assoclen)
854 req->assoc = assoc;
855 req->assoclen = assoclen;
858 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
859 struct crypto_tfm *tfm)
861 return (struct crypto_blkcipher *)tfm;
864 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
865 struct crypto_tfm *tfm)
867 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
868 return __crypto_blkcipher_cast(tfm);
871 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
872 const char *alg_name, u32 type, u32 mask)
874 type &= ~CRYPTO_ALG_TYPE_MASK;
875 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
876 mask |= CRYPTO_ALG_TYPE_MASK;
878 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
881 static inline struct crypto_tfm *crypto_blkcipher_tfm(
882 struct crypto_blkcipher *tfm)
884 return &tfm->base;
887 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
889 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
892 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
894 type &= ~CRYPTO_ALG_TYPE_MASK;
895 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
896 mask |= CRYPTO_ALG_TYPE_MASK;
898 return crypto_has_alg(alg_name, type, mask);
901 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
903 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
906 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
907 struct crypto_blkcipher *tfm)
909 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
912 static inline struct blkcipher_alg *crypto_blkcipher_alg(
913 struct crypto_blkcipher *tfm)
915 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
918 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
920 return crypto_blkcipher_alg(tfm)->ivsize;
923 static inline unsigned int crypto_blkcipher_blocksize(
924 struct crypto_blkcipher *tfm)
926 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
929 static inline unsigned int crypto_blkcipher_alignmask(
930 struct crypto_blkcipher *tfm)
932 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
935 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
937 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
940 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
941 u32 flags)
943 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
946 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
947 u32 flags)
949 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
952 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
953 const u8 *key, unsigned int keylen)
955 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
956 key, keylen);
959 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
960 struct scatterlist *dst,
961 struct scatterlist *src,
962 unsigned int nbytes)
964 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
965 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
968 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
969 struct scatterlist *dst,
970 struct scatterlist *src,
971 unsigned int nbytes)
973 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
976 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
977 struct scatterlist *dst,
978 struct scatterlist *src,
979 unsigned int nbytes)
981 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
982 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
985 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
986 struct scatterlist *dst,
987 struct scatterlist *src,
988 unsigned int nbytes)
990 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
993 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
994 const u8 *src, unsigned int len)
996 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
999 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1000 u8 *dst, unsigned int len)
1002 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1005 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1007 return (struct crypto_cipher *)tfm;
1010 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1012 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1013 return __crypto_cipher_cast(tfm);
1016 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1017 u32 type, u32 mask)
1019 type &= ~CRYPTO_ALG_TYPE_MASK;
1020 type |= CRYPTO_ALG_TYPE_CIPHER;
1021 mask |= CRYPTO_ALG_TYPE_MASK;
1023 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1026 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1028 return &tfm->base;
1031 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1033 crypto_free_tfm(crypto_cipher_tfm(tfm));
1036 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1038 type &= ~CRYPTO_ALG_TYPE_MASK;
1039 type |= CRYPTO_ALG_TYPE_CIPHER;
1040 mask |= CRYPTO_ALG_TYPE_MASK;
1042 return crypto_has_alg(alg_name, type, mask);
1045 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1047 return &crypto_cipher_tfm(tfm)->crt_cipher;
1050 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1052 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1055 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1057 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1060 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1062 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1065 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1066 u32 flags)
1068 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1071 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1072 u32 flags)
1074 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1077 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1078 const u8 *key, unsigned int keylen)
1080 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1081 key, keylen);
1084 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1085 u8 *dst, const u8 *src)
1087 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1088 dst, src);
1091 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1092 u8 *dst, const u8 *src)
1094 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1095 dst, src);
1098 static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
1100 return (struct crypto_hash *)tfm;
1103 static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
1105 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1106 CRYPTO_ALG_TYPE_HASH_MASK);
1107 return __crypto_hash_cast(tfm);
1110 static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1111 u32 type, u32 mask)
1113 type &= ~CRYPTO_ALG_TYPE_MASK;
1114 mask &= ~CRYPTO_ALG_TYPE_MASK;
1115 type |= CRYPTO_ALG_TYPE_HASH;
1116 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1118 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1121 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1123 return &tfm->base;
1126 static inline void crypto_free_hash(struct crypto_hash *tfm)
1128 crypto_free_tfm(crypto_hash_tfm(tfm));
1131 static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1133 type &= ~CRYPTO_ALG_TYPE_MASK;
1134 mask &= ~CRYPTO_ALG_TYPE_MASK;
1135 type |= CRYPTO_ALG_TYPE_HASH;
1136 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1138 return crypto_has_alg(alg_name, type, mask);
1141 static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1143 return &crypto_hash_tfm(tfm)->crt_hash;
1146 static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1148 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1151 static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1153 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1156 static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1158 return crypto_hash_crt(tfm)->digestsize;
1161 static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1163 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1166 static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1168 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1171 static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1173 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1176 static inline int crypto_hash_init(struct hash_desc *desc)
1178 return crypto_hash_crt(desc->tfm)->init(desc);
1181 static inline int crypto_hash_update(struct hash_desc *desc,
1182 struct scatterlist *sg,
1183 unsigned int nbytes)
1185 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1188 static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1190 return crypto_hash_crt(desc->tfm)->final(desc, out);
1193 static inline int crypto_hash_digest(struct hash_desc *desc,
1194 struct scatterlist *sg,
1195 unsigned int nbytes, u8 *out)
1197 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1200 static inline int crypto_hash_setkey(struct crypto_hash *hash,
1201 const u8 *key, unsigned int keylen)
1203 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
1206 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1208 return (struct crypto_comp *)tfm;
1211 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1213 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1214 CRYPTO_ALG_TYPE_MASK);
1215 return __crypto_comp_cast(tfm);
1218 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1219 u32 type, u32 mask)
1221 type &= ~CRYPTO_ALG_TYPE_MASK;
1222 type |= CRYPTO_ALG_TYPE_COMPRESS;
1223 mask |= CRYPTO_ALG_TYPE_MASK;
1225 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1228 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1230 return &tfm->base;
1233 static inline void crypto_free_comp(struct crypto_comp *tfm)
1235 crypto_free_tfm(crypto_comp_tfm(tfm));
1238 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1240 type &= ~CRYPTO_ALG_TYPE_MASK;
1241 type |= CRYPTO_ALG_TYPE_COMPRESS;
1242 mask |= CRYPTO_ALG_TYPE_MASK;
1244 return crypto_has_alg(alg_name, type, mask);
1247 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1249 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1252 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1254 return &crypto_comp_tfm(tfm)->crt_compress;
1257 static inline int crypto_comp_compress(struct crypto_comp *tfm,
1258 const u8 *src, unsigned int slen,
1259 u8 *dst, unsigned int *dlen)
1261 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1262 src, slen, dst, dlen);
1265 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
1266 const u8 *src, unsigned int slen,
1267 u8 *dst, unsigned int *dlen)
1269 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1270 src, slen, dst, dlen);
1273 #endif /* _LINUX_CRYPTO_H */