GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / include / linux / crypto.h
bloba3338f540cec9d23a465363208de2d704886f64a
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 <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 0x00000008
40 #define CRYPTO_ALG_TYPE_SHASH 0x00000009
41 #define CRYPTO_ALG_TYPE_AHASH 0x0000000a
42 #define CRYPTO_ALG_TYPE_RNG 0x0000000c
43 #define CRYPTO_ALG_TYPE_PCOMPRESS 0x0000000f
45 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
46 #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
47 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
49 #define CRYPTO_ALG_LARVAL 0x00000010
50 #define CRYPTO_ALG_DEAD 0x00000020
51 #define CRYPTO_ALG_DYING 0x00000040
52 #define CRYPTO_ALG_ASYNC 0x00000080
55 * Set this bit if and only if the algorithm requires another algorithm of
56 * the same type to handle corner cases.
58 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
61 * This bit is set for symmetric key ciphers that have already been wrapped
62 * with a generic IV generator to prevent them from being wrapped again.
64 #define CRYPTO_ALG_GENIV 0x00000200
67 * Set if the algorithm has passed automated run-time testing. Note that
68 * if there is no run-time testing for a given algorithm it is considered
69 * to have passed.
72 #define CRYPTO_ALG_TESTED 0x00000400
75 * Transform masks and values (for crt_flags).
77 #define CRYPTO_TFM_REQ_MASK 0x000fff00
78 #define CRYPTO_TFM_RES_MASK 0xfff00000
80 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
81 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
82 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
83 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
84 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
85 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
86 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
87 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
90 * Miscellaneous stuff.
92 #define CRYPTO_MAX_ALG_NAME 64
95 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
96 * declaration) is used to ensure that the crypto_tfm context structure is
97 * aligned correctly for the given architecture so that there are no alignment
98 * faults for C data types. In particular, this is required on platforms such
99 * as arm where pointers are 32-bit aligned but there are data types such as
100 * u64 which require 64-bit alignment.
102 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
104 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
106 struct scatterlist;
107 struct crypto_ablkcipher;
108 struct crypto_async_request;
109 struct crypto_aead;
110 struct crypto_blkcipher;
111 struct crypto_hash;
112 struct crypto_rng;
113 struct crypto_tfm;
114 struct crypto_type;
115 struct aead_givcrypt_request;
116 struct skcipher_givcrypt_request;
118 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
120 struct crypto_async_request {
121 struct list_head list;
122 crypto_completion_t complete;
123 void *data;
124 struct crypto_tfm *tfm;
126 u32 flags;
129 struct ablkcipher_request {
130 struct crypto_async_request base;
132 unsigned int nbytes;
134 void *info;
136 struct scatterlist *src;
137 struct scatterlist *dst;
139 void *__ctx[] CRYPTO_MINALIGN_ATTR;
143 * struct aead_request - AEAD request
144 * @base: Common attributes for async crypto requests
145 * @assoclen: Length in bytes of associated data for authentication
146 * @cryptlen: Length of data to be encrypted or decrypted
147 * @iv: Initialisation vector
148 * @assoc: Associated data
149 * @src: Source data
150 * @dst: Destination data
151 * @__ctx: Start of private context data
153 struct aead_request {
154 struct crypto_async_request base;
156 unsigned int assoclen;
157 unsigned int cryptlen;
159 u8 *iv;
161 struct scatterlist *assoc;
162 struct scatterlist *src;
163 struct scatterlist *dst;
165 void *__ctx[] CRYPTO_MINALIGN_ATTR;
168 struct blkcipher_desc {
169 struct crypto_blkcipher *tfm;
170 void *info;
171 u32 flags;
174 struct cipher_desc {
175 struct crypto_tfm *tfm;
176 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
177 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
178 const u8 *src, unsigned int nbytes);
179 void *info;
182 struct hash_desc {
183 struct crypto_hash *tfm;
184 u32 flags;
188 * Algorithms: modular crypto algorithm implementations, managed
189 * via crypto_register_alg() and crypto_unregister_alg().
191 struct ablkcipher_alg {
192 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
193 unsigned int keylen);
194 int (*encrypt)(struct ablkcipher_request *req);
195 int (*decrypt)(struct ablkcipher_request *req);
196 int (*givencrypt)(struct skcipher_givcrypt_request *req);
197 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
199 const char *geniv;
201 unsigned int min_keysize;
202 unsigned int max_keysize;
203 unsigned int ivsize;
206 struct aead_alg {
207 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
208 unsigned int keylen);
209 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
210 int (*encrypt)(struct aead_request *req);
211 int (*decrypt)(struct aead_request *req);
212 int (*givencrypt)(struct aead_givcrypt_request *req);
213 int (*givdecrypt)(struct aead_givcrypt_request *req);
215 const char *geniv;
217 unsigned int ivsize;
218 unsigned int maxauthsize;
221 struct blkcipher_alg {
222 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
223 unsigned int keylen);
224 int (*encrypt)(struct blkcipher_desc *desc,
225 struct scatterlist *dst, struct scatterlist *src,
226 unsigned int nbytes);
227 int (*decrypt)(struct blkcipher_desc *desc,
228 struct scatterlist *dst, struct scatterlist *src,
229 unsigned int nbytes);
231 const char *geniv;
233 unsigned int min_keysize;
234 unsigned int max_keysize;
235 unsigned int ivsize;
238 struct cipher_alg {
239 unsigned int cia_min_keysize;
240 unsigned int cia_max_keysize;
241 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
242 unsigned int keylen);
243 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
244 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
247 struct compress_alg {
248 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
249 unsigned int slen, u8 *dst, unsigned int *dlen);
250 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
251 unsigned int slen, u8 *dst, unsigned int *dlen);
254 struct rng_alg {
255 int (*rng_make_random)(struct crypto_rng *tfm, u8 *rdata,
256 unsigned int dlen);
257 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
259 unsigned int seedsize;
263 #define cra_ablkcipher cra_u.ablkcipher
264 #define cra_aead cra_u.aead
265 #define cra_blkcipher cra_u.blkcipher
266 #define cra_cipher cra_u.cipher
267 #define cra_compress cra_u.compress
268 #define cra_rng cra_u.rng
270 struct crypto_alg {
271 struct list_head cra_list;
272 struct list_head cra_users;
274 u32 cra_flags;
275 unsigned int cra_blocksize;
276 unsigned int cra_ctxsize;
277 unsigned int cra_alignmask;
279 int cra_priority;
280 atomic_t cra_refcnt;
282 char cra_name[CRYPTO_MAX_ALG_NAME];
283 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
285 const struct crypto_type *cra_type;
287 union {
288 struct ablkcipher_alg ablkcipher;
289 struct aead_alg aead;
290 struct blkcipher_alg blkcipher;
291 struct cipher_alg cipher;
292 struct compress_alg compress;
293 struct rng_alg rng;
294 } cra_u;
296 int (*cra_init)(struct crypto_tfm *tfm);
297 void (*cra_exit)(struct crypto_tfm *tfm);
298 void (*cra_destroy)(struct crypto_alg *alg);
300 struct module *cra_module;
304 * Algorithm registration interface.
306 int crypto_register_alg(struct crypto_alg *alg);
307 int crypto_unregister_alg(struct crypto_alg *alg);
310 * Algorithm query interface.
312 #ifdef CONFIG_CRYPTO
313 int crypto_alg_available(const char *name, u32 flags)
314 __deprecated_for_modules;
315 int crypto_has_alg(const char *name, u32 type, u32 mask);
316 #else
317 static int crypto_alg_available(const char *name, u32 flags)
318 __deprecated_for_modules;
319 static inline int crypto_has_alg(const char *name, u32 type, u32 mask)
321 return 0;
323 #endif
326 * Transforms: user-instantiated objects which encapsulate algorithms
327 * and core processing logic. Managed via crypto_alloc_*() and
328 * crypto_free_*(), as well as the various helpers below.
331 struct ablkcipher_tfm {
332 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
333 unsigned int keylen);
334 int (*encrypt)(struct ablkcipher_request *req);
335 int (*decrypt)(struct ablkcipher_request *req);
336 int (*givencrypt)(struct skcipher_givcrypt_request *req);
337 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
339 struct crypto_ablkcipher *base;
341 unsigned int ivsize;
342 unsigned int reqsize;
345 struct aead_tfm {
346 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
347 unsigned int keylen);
348 int (*encrypt)(struct aead_request *req);
349 int (*decrypt)(struct aead_request *req);
350 int (*givencrypt)(struct aead_givcrypt_request *req);
351 int (*givdecrypt)(struct aead_givcrypt_request *req);
353 struct crypto_aead *base;
355 unsigned int ivsize;
356 unsigned int authsize;
357 unsigned int reqsize;
360 struct blkcipher_tfm {
361 void *iv;
362 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
363 unsigned int keylen);
364 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
365 struct scatterlist *src, unsigned int nbytes);
366 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
367 struct scatterlist *src, unsigned int nbytes);
370 struct cipher_tfm {
371 int (*cit_setkey)(struct crypto_tfm *tfm,
372 const u8 *key, unsigned int keylen);
373 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
374 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
377 struct hash_tfm {
378 int (*init)(struct hash_desc *desc);
379 int (*update)(struct hash_desc *desc,
380 struct scatterlist *sg, unsigned int nsg);
381 int (*final)(struct hash_desc *desc, u8 *out);
382 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
383 unsigned int nsg, u8 *out);
384 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
385 unsigned int keylen);
386 unsigned int digestsize;
389 struct compress_tfm {
390 int (*cot_compress)(struct crypto_tfm *tfm,
391 const u8 *src, unsigned int slen,
392 u8 *dst, unsigned int *dlen);
393 int (*cot_decompress)(struct crypto_tfm *tfm,
394 const u8 *src, unsigned int slen,
395 u8 *dst, unsigned int *dlen);
398 struct rng_tfm {
399 int (*rng_gen_random)(struct crypto_rng *tfm, u8 *rdata,
400 unsigned int dlen);
401 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
404 #define crt_ablkcipher crt_u.ablkcipher
405 #define crt_aead crt_u.aead
406 #define crt_blkcipher crt_u.blkcipher
407 #define crt_cipher crt_u.cipher
408 #define crt_hash crt_u.hash
409 #define crt_compress crt_u.compress
410 #define crt_rng crt_u.rng
412 struct crypto_tfm {
414 u32 crt_flags;
416 union {
417 struct ablkcipher_tfm ablkcipher;
418 struct aead_tfm aead;
419 struct blkcipher_tfm blkcipher;
420 struct cipher_tfm cipher;
421 struct hash_tfm hash;
422 struct compress_tfm compress;
423 struct rng_tfm rng;
424 } crt_u;
426 void (*exit)(struct crypto_tfm *tfm);
428 struct crypto_alg *__crt_alg;
430 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
433 struct crypto_ablkcipher {
434 struct crypto_tfm base;
437 struct crypto_aead {
438 struct crypto_tfm base;
441 struct crypto_blkcipher {
442 struct crypto_tfm base;
445 struct crypto_cipher {
446 struct crypto_tfm base;
449 struct crypto_comp {
450 struct crypto_tfm base;
453 struct crypto_hash {
454 struct crypto_tfm base;
457 struct crypto_rng {
458 struct crypto_tfm base;
461 enum {
462 CRYPTOA_UNSPEC,
463 CRYPTOA_ALG,
464 CRYPTOA_TYPE,
465 CRYPTOA_U32,
466 __CRYPTOA_MAX,
469 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
471 /* Maximum number of (rtattr) parameters for each template. */
472 #define CRYPTO_MAX_ATTRS 32
474 struct crypto_attr_alg {
475 char name[CRYPTO_MAX_ALG_NAME];
478 struct crypto_attr_type {
479 u32 type;
480 u32 mask;
483 struct crypto_attr_u32 {
484 u32 num;
488 * Transform user interface.
491 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
492 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
494 static inline void crypto_free_tfm(struct crypto_tfm *tfm)
496 return crypto_destroy_tfm(tfm, tfm);
499 int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
502 * Transform helpers which query the underlying algorithm.
504 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
506 return tfm->__crt_alg->cra_name;
509 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
511 return tfm->__crt_alg->cra_driver_name;
514 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
516 return tfm->__crt_alg->cra_priority;
519 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
521 return module_name(tfm->__crt_alg->cra_module);
524 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
526 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
529 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
531 return tfm->__crt_alg->cra_blocksize;
534 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
536 return tfm->__crt_alg->cra_alignmask;
539 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
541 return tfm->crt_flags;
544 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
546 tfm->crt_flags |= flags;
549 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
551 tfm->crt_flags &= ~flags;
554 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
556 return tfm->__crt_ctx;
559 static inline unsigned int crypto_tfm_ctx_alignment(void)
561 struct crypto_tfm *tfm;
562 return __alignof__(tfm->__crt_ctx);
566 * API wrappers.
568 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
569 struct crypto_tfm *tfm)
571 return (struct crypto_ablkcipher *)tfm;
574 static inline u32 crypto_skcipher_type(u32 type)
576 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
577 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
578 return type;
581 static inline u32 crypto_skcipher_mask(u32 mask)
583 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
584 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
585 return mask;
588 struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
589 u32 type, u32 mask);
591 static inline struct crypto_tfm *crypto_ablkcipher_tfm(
592 struct crypto_ablkcipher *tfm)
594 return &tfm->base;
597 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
599 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
602 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
603 u32 mask)
605 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
606 crypto_skcipher_mask(mask));
609 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
610 struct crypto_ablkcipher *tfm)
612 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
615 static inline unsigned int crypto_ablkcipher_ivsize(
616 struct crypto_ablkcipher *tfm)
618 return crypto_ablkcipher_crt(tfm)->ivsize;
621 static inline unsigned int crypto_ablkcipher_blocksize(
622 struct crypto_ablkcipher *tfm)
624 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
627 static inline unsigned int crypto_ablkcipher_alignmask(
628 struct crypto_ablkcipher *tfm)
630 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
633 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
635 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
638 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
639 u32 flags)
641 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
644 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
645 u32 flags)
647 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
650 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
651 const u8 *key, unsigned int keylen)
653 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
655 return crt->setkey(crt->base, key, keylen);
658 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
659 struct ablkcipher_request *req)
661 return __crypto_ablkcipher_cast(req->base.tfm);
664 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
666 struct ablkcipher_tfm *crt =
667 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
668 return crt->encrypt(req);
671 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
673 struct ablkcipher_tfm *crt =
674 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
675 return crt->decrypt(req);
678 static inline unsigned int crypto_ablkcipher_reqsize(
679 struct crypto_ablkcipher *tfm)
681 return crypto_ablkcipher_crt(tfm)->reqsize;
684 static inline void ablkcipher_request_set_tfm(
685 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
687 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
690 static inline struct ablkcipher_request *ablkcipher_request_cast(
691 struct crypto_async_request *req)
693 return container_of(req, struct ablkcipher_request, base);
696 static inline struct ablkcipher_request *ablkcipher_request_alloc(
697 struct crypto_ablkcipher *tfm, gfp_t gfp)
699 struct ablkcipher_request *req;
701 req = kmalloc(sizeof(struct ablkcipher_request) +
702 crypto_ablkcipher_reqsize(tfm), gfp);
704 if (likely(req))
705 ablkcipher_request_set_tfm(req, tfm);
707 return req;
710 static inline void ablkcipher_request_free(struct ablkcipher_request *req)
712 kzfree(req);
715 static inline void ablkcipher_request_set_callback(
716 struct ablkcipher_request *req,
717 u32 flags, crypto_completion_t complete, void *data)
719 req->base.complete = complete;
720 req->base.data = data;
721 req->base.flags = flags;
724 static inline void ablkcipher_request_set_crypt(
725 struct ablkcipher_request *req,
726 struct scatterlist *src, struct scatterlist *dst,
727 unsigned int nbytes, void *iv)
729 req->src = src;
730 req->dst = dst;
731 req->nbytes = nbytes;
732 req->info = iv;
735 static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
737 return (struct crypto_aead *)tfm;
740 struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
742 static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
744 return &tfm->base;
747 static inline void crypto_free_aead(struct crypto_aead *tfm)
749 crypto_free_tfm(crypto_aead_tfm(tfm));
752 static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
754 return &crypto_aead_tfm(tfm)->crt_aead;
757 static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
759 return crypto_aead_crt(tfm)->ivsize;
762 static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
764 return crypto_aead_crt(tfm)->authsize;
767 static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
769 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
772 static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
774 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
777 static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
779 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
782 static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
784 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
787 static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
789 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
792 static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
793 unsigned int keylen)
795 struct aead_tfm *crt = crypto_aead_crt(tfm);
797 return crt->setkey(crt->base, key, keylen);
800 int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
802 static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
804 return __crypto_aead_cast(req->base.tfm);
807 static inline int crypto_aead_encrypt(struct aead_request *req)
809 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
812 static inline int crypto_aead_decrypt(struct aead_request *req)
814 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
817 static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
819 return crypto_aead_crt(tfm)->reqsize;
822 static inline void aead_request_set_tfm(struct aead_request *req,
823 struct crypto_aead *tfm)
825 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
828 static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
829 gfp_t gfp)
831 struct aead_request *req;
833 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
835 if (likely(req))
836 aead_request_set_tfm(req, tfm);
838 return req;
841 static inline void aead_request_free(struct aead_request *req)
843 kzfree(req);
846 static inline void aead_request_set_callback(struct aead_request *req,
847 u32 flags,
848 crypto_completion_t complete,
849 void *data)
851 req->base.complete = complete;
852 req->base.data = data;
853 req->base.flags = flags;
856 static inline void aead_request_set_crypt(struct aead_request *req,
857 struct scatterlist *src,
858 struct scatterlist *dst,
859 unsigned int cryptlen, u8 *iv)
861 req->src = src;
862 req->dst = dst;
863 req->cryptlen = cryptlen;
864 req->iv = iv;
867 static inline void aead_request_set_assoc(struct aead_request *req,
868 struct scatterlist *assoc,
869 unsigned int assoclen)
871 req->assoc = assoc;
872 req->assoclen = assoclen;
875 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
876 struct crypto_tfm *tfm)
878 return (struct crypto_blkcipher *)tfm;
881 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
882 struct crypto_tfm *tfm)
884 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
885 return __crypto_blkcipher_cast(tfm);
888 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
889 const char *alg_name, u32 type, u32 mask)
891 type &= ~CRYPTO_ALG_TYPE_MASK;
892 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
893 mask |= CRYPTO_ALG_TYPE_MASK;
895 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
898 static inline struct crypto_tfm *crypto_blkcipher_tfm(
899 struct crypto_blkcipher *tfm)
901 return &tfm->base;
904 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
906 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
909 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
911 type &= ~CRYPTO_ALG_TYPE_MASK;
912 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
913 mask |= CRYPTO_ALG_TYPE_MASK;
915 return crypto_has_alg(alg_name, type, mask);
918 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
920 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
923 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
924 struct crypto_blkcipher *tfm)
926 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
929 static inline struct blkcipher_alg *crypto_blkcipher_alg(
930 struct crypto_blkcipher *tfm)
932 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
935 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
937 return crypto_blkcipher_alg(tfm)->ivsize;
940 static inline unsigned int crypto_blkcipher_blocksize(
941 struct crypto_blkcipher *tfm)
943 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
946 static inline unsigned int crypto_blkcipher_alignmask(
947 struct crypto_blkcipher *tfm)
949 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
952 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
954 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
957 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
958 u32 flags)
960 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
963 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
964 u32 flags)
966 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
969 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
970 const u8 *key, unsigned int keylen)
972 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
973 key, keylen);
976 static inline int crypto_blkcipher_encrypt(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)->encrypt(desc, dst, src, nbytes);
985 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
986 struct scatterlist *dst,
987 struct scatterlist *src,
988 unsigned int nbytes)
990 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
993 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
994 struct scatterlist *dst,
995 struct scatterlist *src,
996 unsigned int nbytes)
998 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
999 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1002 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1003 struct scatterlist *dst,
1004 struct scatterlist *src,
1005 unsigned int nbytes)
1007 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1010 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1011 const u8 *src, unsigned int len)
1013 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1016 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1017 u8 *dst, unsigned int len)
1019 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1022 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1024 return (struct crypto_cipher *)tfm;
1027 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1029 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1030 return __crypto_cipher_cast(tfm);
1033 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1034 u32 type, u32 mask)
1036 type &= ~CRYPTO_ALG_TYPE_MASK;
1037 type |= CRYPTO_ALG_TYPE_CIPHER;
1038 mask |= CRYPTO_ALG_TYPE_MASK;
1040 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1043 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1045 return &tfm->base;
1048 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1050 crypto_free_tfm(crypto_cipher_tfm(tfm));
1053 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1055 type &= ~CRYPTO_ALG_TYPE_MASK;
1056 type |= CRYPTO_ALG_TYPE_CIPHER;
1057 mask |= CRYPTO_ALG_TYPE_MASK;
1059 return crypto_has_alg(alg_name, type, mask);
1062 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1064 return &crypto_cipher_tfm(tfm)->crt_cipher;
1067 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1069 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1072 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1074 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1077 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1079 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1082 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1083 u32 flags)
1085 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1088 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1089 u32 flags)
1091 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1094 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1095 const u8 *key, unsigned int keylen)
1097 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1098 key, keylen);
1101 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1102 u8 *dst, const u8 *src)
1104 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1105 dst, src);
1108 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1109 u8 *dst, const u8 *src)
1111 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1112 dst, src);
1115 void crypto_digest_init(struct crypto_tfm *tfm) __deprecated_for_modules;
1116 void crypto_digest_update(struct crypto_tfm *tfm,
1117 struct scatterlist *sg, unsigned int nsg)
1118 __deprecated_for_modules;
1119 void crypto_digest_final(struct crypto_tfm *tfm, u8 *out)
1120 __deprecated_for_modules;
1121 void crypto_digest_digest(struct crypto_tfm *tfm,
1122 struct scatterlist *sg, unsigned int nsg, u8 *out)
1123 __deprecated_for_modules;
1125 static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
1127 return (struct crypto_hash *)tfm;
1130 static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
1132 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1133 CRYPTO_ALG_TYPE_HASH_MASK);
1134 return __crypto_hash_cast(tfm);
1137 static int crypto_digest_setkey(struct crypto_tfm *tfm, const u8 *key,
1138 unsigned int keylen) __deprecated;
1139 static inline int crypto_digest_setkey(struct crypto_tfm *tfm,
1140 const u8 *key, unsigned int keylen)
1142 return tfm->crt_hash.setkey(crypto_hash_cast(tfm), key, keylen);
1145 static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1146 u32 type, u32 mask)
1148 type &= ~CRYPTO_ALG_TYPE_MASK;
1149 mask &= ~CRYPTO_ALG_TYPE_MASK;
1150 type |= CRYPTO_ALG_TYPE_HASH;
1151 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1153 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1156 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1158 return &tfm->base;
1161 static inline void crypto_free_hash(struct crypto_hash *tfm)
1163 crypto_free_tfm(crypto_hash_tfm(tfm));
1166 static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1168 type &= ~CRYPTO_ALG_TYPE_MASK;
1169 mask &= ~CRYPTO_ALG_TYPE_MASK;
1170 type |= CRYPTO_ALG_TYPE_HASH;
1171 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1173 return crypto_has_alg(alg_name, type, mask);
1176 static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1178 return &crypto_hash_tfm(tfm)->crt_hash;
1181 static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1183 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1186 static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1188 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1191 static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1193 return crypto_hash_crt(tfm)->digestsize;
1196 static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1198 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1201 static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1203 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1206 static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1208 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1211 static inline int crypto_hash_init(struct hash_desc *desc)
1213 return crypto_hash_crt(desc->tfm)->init(desc);
1216 static inline int crypto_hash_update(struct hash_desc *desc,
1217 struct scatterlist *sg,
1218 unsigned int nbytes)
1220 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1223 static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1225 return crypto_hash_crt(desc->tfm)->final(desc, out);
1228 static inline int crypto_hash_digest(struct hash_desc *desc,
1229 struct scatterlist *sg,
1230 unsigned int nbytes, u8 *out)
1232 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1235 static inline int crypto_hash_setkey(struct crypto_hash *hash,
1236 const u8 *key, unsigned int keylen)
1238 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
1241 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1243 return (struct crypto_comp *)tfm;
1246 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1248 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1249 CRYPTO_ALG_TYPE_MASK);
1250 return __crypto_comp_cast(tfm);
1253 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1254 u32 type, u32 mask)
1256 type &= ~CRYPTO_ALG_TYPE_MASK;
1257 type |= CRYPTO_ALG_TYPE_COMPRESS;
1258 mask |= CRYPTO_ALG_TYPE_MASK;
1260 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1263 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1265 return &tfm->base;
1268 static inline void crypto_free_comp(struct crypto_comp *tfm)
1270 crypto_free_tfm(crypto_comp_tfm(tfm));
1273 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1275 type &= ~CRYPTO_ALG_TYPE_MASK;
1276 type |= CRYPTO_ALG_TYPE_COMPRESS;
1277 mask |= CRYPTO_ALG_TYPE_MASK;
1279 return crypto_has_alg(alg_name, type, mask);
1282 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1284 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1287 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1289 return &crypto_comp_tfm(tfm)->crt_compress;
1292 static inline int crypto_comp_compress(struct crypto_comp *tfm,
1293 const u8 *src, unsigned int slen,
1294 u8 *dst, unsigned int *dlen)
1296 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1297 src, slen, dst, dlen);
1300 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
1301 const u8 *src, unsigned int slen,
1302 u8 *dst, unsigned int *dlen)
1304 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1305 src, slen, dst, dlen);
1308 #endif /* _LINUX_CRYPTO_H */