USB: correct error handling in cdc-wdm
[wandboard.git] / include / linux / crypto.h
blobec29fa268b94e278e5125380f8e2b1faa20c10c9
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 #if defined(ARCH_KMALLOC_MINALIGN)
103 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
104 #elif defined(ARCH_SLAB_MINALIGN)
105 #define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
106 #else
107 #define CRYPTO_MINALIGN __alignof__(unsigned long long)
108 #endif
110 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
112 struct scatterlist;
113 struct crypto_ablkcipher;
114 struct crypto_async_request;
115 struct crypto_aead;
116 struct crypto_blkcipher;
117 struct crypto_hash;
118 struct crypto_ahash;
119 struct crypto_rng;
120 struct crypto_tfm;
121 struct crypto_type;
122 struct aead_givcrypt_request;
123 struct skcipher_givcrypt_request;
125 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
127 struct crypto_async_request {
128 struct list_head list;
129 crypto_completion_t complete;
130 void *data;
131 struct crypto_tfm *tfm;
133 u32 flags;
136 struct ablkcipher_request {
137 struct crypto_async_request base;
139 unsigned int nbytes;
141 void *info;
143 struct scatterlist *src;
144 struct scatterlist *dst;
146 void *__ctx[] CRYPTO_MINALIGN_ATTR;
149 struct ahash_request {
150 struct crypto_async_request base;
152 unsigned int nbytes;
153 struct scatterlist *src;
154 u8 *result;
156 void *__ctx[] CRYPTO_MINALIGN_ATTR;
160 * struct aead_request - AEAD request
161 * @base: Common attributes for async crypto requests
162 * @assoclen: Length in bytes of associated data for authentication
163 * @cryptlen: Length of data to be encrypted or decrypted
164 * @iv: Initialisation vector
165 * @assoc: Associated data
166 * @src: Source data
167 * @dst: Destination data
168 * @__ctx: Start of private context data
170 struct aead_request {
171 struct crypto_async_request base;
173 unsigned int assoclen;
174 unsigned int cryptlen;
176 u8 *iv;
178 struct scatterlist *assoc;
179 struct scatterlist *src;
180 struct scatterlist *dst;
182 void *__ctx[] CRYPTO_MINALIGN_ATTR;
185 struct blkcipher_desc {
186 struct crypto_blkcipher *tfm;
187 void *info;
188 u32 flags;
191 struct cipher_desc {
192 struct crypto_tfm *tfm;
193 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
194 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
195 const u8 *src, unsigned int nbytes);
196 void *info;
199 struct hash_desc {
200 struct crypto_hash *tfm;
201 u32 flags;
205 * Algorithms: modular crypto algorithm implementations, managed
206 * via crypto_register_alg() and crypto_unregister_alg().
208 struct ablkcipher_alg {
209 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
210 unsigned int keylen);
211 int (*encrypt)(struct ablkcipher_request *req);
212 int (*decrypt)(struct ablkcipher_request *req);
213 int (*givencrypt)(struct skcipher_givcrypt_request *req);
214 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
216 const char *geniv;
218 unsigned int min_keysize;
219 unsigned int max_keysize;
220 unsigned int ivsize;
223 struct ahash_alg {
224 int (*init)(struct ahash_request *req);
225 int (*reinit)(struct ahash_request *req);
226 int (*update)(struct ahash_request *req);
227 int (*final)(struct ahash_request *req);
228 int (*digest)(struct ahash_request *req);
229 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
230 unsigned int keylen);
232 unsigned int digestsize;
235 struct aead_alg {
236 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
237 unsigned int keylen);
238 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
239 int (*encrypt)(struct aead_request *req);
240 int (*decrypt)(struct aead_request *req);
241 int (*givencrypt)(struct aead_givcrypt_request *req);
242 int (*givdecrypt)(struct aead_givcrypt_request *req);
244 const char *geniv;
246 unsigned int ivsize;
247 unsigned int maxauthsize;
250 struct blkcipher_alg {
251 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
252 unsigned int keylen);
253 int (*encrypt)(struct blkcipher_desc *desc,
254 struct scatterlist *dst, struct scatterlist *src,
255 unsigned int nbytes);
256 int (*decrypt)(struct blkcipher_desc *desc,
257 struct scatterlist *dst, struct scatterlist *src,
258 unsigned int nbytes);
260 const char *geniv;
262 unsigned int min_keysize;
263 unsigned int max_keysize;
264 unsigned int ivsize;
267 struct cipher_alg {
268 unsigned int cia_min_keysize;
269 unsigned int cia_max_keysize;
270 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
271 unsigned int keylen);
272 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
273 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
276 struct digest_alg {
277 unsigned int dia_digestsize;
278 void (*dia_init)(struct crypto_tfm *tfm);
279 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
280 unsigned int len);
281 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
282 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
283 unsigned int keylen);
286 struct hash_alg {
287 int (*init)(struct hash_desc *desc);
288 int (*update)(struct hash_desc *desc, struct scatterlist *sg,
289 unsigned int nbytes);
290 int (*final)(struct hash_desc *desc, u8 *out);
291 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
292 unsigned int nbytes, u8 *out);
293 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
294 unsigned int keylen);
296 unsigned int digestsize;
299 struct compress_alg {
300 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
301 unsigned int slen, u8 *dst, unsigned int *dlen);
302 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
303 unsigned int slen, u8 *dst, unsigned int *dlen);
306 struct rng_alg {
307 int (*rng_make_random)(struct crypto_rng *tfm, u8 *rdata,
308 unsigned int dlen);
309 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
311 unsigned int seedsize;
315 #define cra_ablkcipher cra_u.ablkcipher
316 #define cra_aead cra_u.aead
317 #define cra_blkcipher cra_u.blkcipher
318 #define cra_cipher cra_u.cipher
319 #define cra_digest cra_u.digest
320 #define cra_hash cra_u.hash
321 #define cra_ahash cra_u.ahash
322 #define cra_compress cra_u.compress
323 #define cra_rng cra_u.rng
325 struct crypto_alg {
326 struct list_head cra_list;
327 struct list_head cra_users;
329 u32 cra_flags;
330 unsigned int cra_blocksize;
331 unsigned int cra_ctxsize;
332 unsigned int cra_alignmask;
334 int cra_priority;
335 atomic_t cra_refcnt;
337 char cra_name[CRYPTO_MAX_ALG_NAME];
338 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
340 const struct crypto_type *cra_type;
342 union {
343 struct ablkcipher_alg ablkcipher;
344 struct aead_alg aead;
345 struct blkcipher_alg blkcipher;
346 struct cipher_alg cipher;
347 struct digest_alg digest;
348 struct hash_alg hash;
349 struct ahash_alg ahash;
350 struct compress_alg compress;
351 struct rng_alg rng;
352 } cra_u;
354 int (*cra_init)(struct crypto_tfm *tfm);
355 void (*cra_exit)(struct crypto_tfm *tfm);
356 void (*cra_destroy)(struct crypto_alg *alg);
358 struct module *cra_module;
362 * Algorithm registration interface.
364 int crypto_register_alg(struct crypto_alg *alg);
365 int crypto_unregister_alg(struct crypto_alg *alg);
368 * Algorithm query interface.
370 int crypto_has_alg(const char *name, u32 type, u32 mask);
373 * Transforms: user-instantiated objects which encapsulate algorithms
374 * and core processing logic. Managed via crypto_alloc_*() and
375 * crypto_free_*(), as well as the various helpers below.
378 struct ablkcipher_tfm {
379 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
380 unsigned int keylen);
381 int (*encrypt)(struct ablkcipher_request *req);
382 int (*decrypt)(struct ablkcipher_request *req);
383 int (*givencrypt)(struct skcipher_givcrypt_request *req);
384 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
386 struct crypto_ablkcipher *base;
388 unsigned int ivsize;
389 unsigned int reqsize;
392 struct aead_tfm {
393 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
394 unsigned int keylen);
395 int (*encrypt)(struct aead_request *req);
396 int (*decrypt)(struct aead_request *req);
397 int (*givencrypt)(struct aead_givcrypt_request *req);
398 int (*givdecrypt)(struct aead_givcrypt_request *req);
400 struct crypto_aead *base;
402 unsigned int ivsize;
403 unsigned int authsize;
404 unsigned int reqsize;
407 struct blkcipher_tfm {
408 void *iv;
409 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
410 unsigned int keylen);
411 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
412 struct scatterlist *src, unsigned int nbytes);
413 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
414 struct scatterlist *src, unsigned int nbytes);
417 struct cipher_tfm {
418 int (*cit_setkey)(struct crypto_tfm *tfm,
419 const u8 *key, unsigned int keylen);
420 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
421 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
424 struct hash_tfm {
425 int (*init)(struct hash_desc *desc);
426 int (*update)(struct hash_desc *desc,
427 struct scatterlist *sg, unsigned int nsg);
428 int (*final)(struct hash_desc *desc, u8 *out);
429 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
430 unsigned int nsg, u8 *out);
431 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
432 unsigned int keylen);
433 unsigned int digestsize;
436 struct ahash_tfm {
437 int (*init)(struct ahash_request *req);
438 int (*update)(struct ahash_request *req);
439 int (*final)(struct ahash_request *req);
440 int (*digest)(struct ahash_request *req);
441 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
442 unsigned int keylen);
444 unsigned int digestsize;
445 unsigned int reqsize;
448 struct compress_tfm {
449 int (*cot_compress)(struct crypto_tfm *tfm,
450 const u8 *src, unsigned int slen,
451 u8 *dst, unsigned int *dlen);
452 int (*cot_decompress)(struct crypto_tfm *tfm,
453 const u8 *src, unsigned int slen,
454 u8 *dst, unsigned int *dlen);
457 struct rng_tfm {
458 int (*rng_gen_random)(struct crypto_rng *tfm, u8 *rdata,
459 unsigned int dlen);
460 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
463 #define crt_ablkcipher crt_u.ablkcipher
464 #define crt_aead crt_u.aead
465 #define crt_blkcipher crt_u.blkcipher
466 #define crt_cipher crt_u.cipher
467 #define crt_hash crt_u.hash
468 #define crt_ahash crt_u.ahash
469 #define crt_compress crt_u.compress
470 #define crt_rng crt_u.rng
472 struct crypto_tfm {
474 u32 crt_flags;
476 union {
477 struct ablkcipher_tfm ablkcipher;
478 struct aead_tfm aead;
479 struct blkcipher_tfm blkcipher;
480 struct cipher_tfm cipher;
481 struct hash_tfm hash;
482 struct ahash_tfm ahash;
483 struct compress_tfm compress;
484 struct rng_tfm rng;
485 } crt_u;
487 void (*exit)(struct crypto_tfm *tfm);
489 struct crypto_alg *__crt_alg;
491 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
494 struct crypto_ablkcipher {
495 struct crypto_tfm base;
498 struct crypto_aead {
499 struct crypto_tfm base;
502 struct crypto_blkcipher {
503 struct crypto_tfm base;
506 struct crypto_cipher {
507 struct crypto_tfm base;
510 struct crypto_comp {
511 struct crypto_tfm base;
514 struct crypto_hash {
515 struct crypto_tfm base;
518 struct crypto_rng {
519 struct crypto_tfm base;
522 enum {
523 CRYPTOA_UNSPEC,
524 CRYPTOA_ALG,
525 CRYPTOA_TYPE,
526 CRYPTOA_U32,
527 __CRYPTOA_MAX,
530 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
532 /* Maximum number of (rtattr) parameters for each template. */
533 #define CRYPTO_MAX_ATTRS 32
535 struct crypto_attr_alg {
536 char name[CRYPTO_MAX_ALG_NAME];
539 struct crypto_attr_type {
540 u32 type;
541 u32 mask;
544 struct crypto_attr_u32 {
545 u32 num;
549 * Transform user interface.
552 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
553 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
555 static inline void crypto_free_tfm(struct crypto_tfm *tfm)
557 return crypto_destroy_tfm(tfm, tfm);
560 int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
563 * Transform helpers which query the underlying algorithm.
565 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
567 return tfm->__crt_alg->cra_name;
570 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
572 return tfm->__crt_alg->cra_driver_name;
575 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
577 return tfm->__crt_alg->cra_priority;
580 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
582 return module_name(tfm->__crt_alg->cra_module);
585 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
587 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
590 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
592 return tfm->__crt_alg->cra_blocksize;
595 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
597 return tfm->__crt_alg->cra_alignmask;
600 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
602 return tfm->crt_flags;
605 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
607 tfm->crt_flags |= flags;
610 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
612 tfm->crt_flags &= ~flags;
615 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
617 return tfm->__crt_ctx;
620 static inline unsigned int crypto_tfm_ctx_alignment(void)
622 struct crypto_tfm *tfm;
623 return __alignof__(tfm->__crt_ctx);
627 * API wrappers.
629 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
630 struct crypto_tfm *tfm)
632 return (struct crypto_ablkcipher *)tfm;
635 static inline u32 crypto_skcipher_type(u32 type)
637 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
638 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
639 return type;
642 static inline u32 crypto_skcipher_mask(u32 mask)
644 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
645 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
646 return mask;
649 struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
650 u32 type, u32 mask);
652 static inline struct crypto_tfm *crypto_ablkcipher_tfm(
653 struct crypto_ablkcipher *tfm)
655 return &tfm->base;
658 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
660 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
663 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
664 u32 mask)
666 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
667 crypto_skcipher_mask(mask));
670 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
671 struct crypto_ablkcipher *tfm)
673 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
676 static inline unsigned int crypto_ablkcipher_ivsize(
677 struct crypto_ablkcipher *tfm)
679 return crypto_ablkcipher_crt(tfm)->ivsize;
682 static inline unsigned int crypto_ablkcipher_blocksize(
683 struct crypto_ablkcipher *tfm)
685 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
688 static inline unsigned int crypto_ablkcipher_alignmask(
689 struct crypto_ablkcipher *tfm)
691 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
694 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
696 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
699 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
700 u32 flags)
702 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
705 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
706 u32 flags)
708 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
711 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
712 const u8 *key, unsigned int keylen)
714 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
716 return crt->setkey(crt->base, key, keylen);
719 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
720 struct ablkcipher_request *req)
722 return __crypto_ablkcipher_cast(req->base.tfm);
725 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
727 struct ablkcipher_tfm *crt =
728 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
729 return crt->encrypt(req);
732 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
734 struct ablkcipher_tfm *crt =
735 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
736 return crt->decrypt(req);
739 static inline unsigned int crypto_ablkcipher_reqsize(
740 struct crypto_ablkcipher *tfm)
742 return crypto_ablkcipher_crt(tfm)->reqsize;
745 static inline void ablkcipher_request_set_tfm(
746 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
748 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
751 static inline struct ablkcipher_request *ablkcipher_request_cast(
752 struct crypto_async_request *req)
754 return container_of(req, struct ablkcipher_request, base);
757 static inline struct ablkcipher_request *ablkcipher_request_alloc(
758 struct crypto_ablkcipher *tfm, gfp_t gfp)
760 struct ablkcipher_request *req;
762 req = kmalloc(sizeof(struct ablkcipher_request) +
763 crypto_ablkcipher_reqsize(tfm), gfp);
765 if (likely(req))
766 ablkcipher_request_set_tfm(req, tfm);
768 return req;
771 static inline void ablkcipher_request_free(struct ablkcipher_request *req)
773 kfree(req);
776 static inline void ablkcipher_request_set_callback(
777 struct ablkcipher_request *req,
778 u32 flags, crypto_completion_t complete, void *data)
780 req->base.complete = complete;
781 req->base.data = data;
782 req->base.flags = flags;
785 static inline void ablkcipher_request_set_crypt(
786 struct ablkcipher_request *req,
787 struct scatterlist *src, struct scatterlist *dst,
788 unsigned int nbytes, void *iv)
790 req->src = src;
791 req->dst = dst;
792 req->nbytes = nbytes;
793 req->info = iv;
796 static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
798 return (struct crypto_aead *)tfm;
801 struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
803 static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
805 return &tfm->base;
808 static inline void crypto_free_aead(struct crypto_aead *tfm)
810 crypto_free_tfm(crypto_aead_tfm(tfm));
813 static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
815 return &crypto_aead_tfm(tfm)->crt_aead;
818 static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
820 return crypto_aead_crt(tfm)->ivsize;
823 static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
825 return crypto_aead_crt(tfm)->authsize;
828 static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
830 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
833 static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
835 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
838 static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
840 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
843 static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
845 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
848 static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
850 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
853 static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
854 unsigned int keylen)
856 struct aead_tfm *crt = crypto_aead_crt(tfm);
858 return crt->setkey(crt->base, key, keylen);
861 int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
863 static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
865 return __crypto_aead_cast(req->base.tfm);
868 static inline int crypto_aead_encrypt(struct aead_request *req)
870 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
873 static inline int crypto_aead_decrypt(struct aead_request *req)
875 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
878 static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
880 return crypto_aead_crt(tfm)->reqsize;
883 static inline void aead_request_set_tfm(struct aead_request *req,
884 struct crypto_aead *tfm)
886 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
889 static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
890 gfp_t gfp)
892 struct aead_request *req;
894 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
896 if (likely(req))
897 aead_request_set_tfm(req, tfm);
899 return req;
902 static inline void aead_request_free(struct aead_request *req)
904 kfree(req);
907 static inline void aead_request_set_callback(struct aead_request *req,
908 u32 flags,
909 crypto_completion_t complete,
910 void *data)
912 req->base.complete = complete;
913 req->base.data = data;
914 req->base.flags = flags;
917 static inline void aead_request_set_crypt(struct aead_request *req,
918 struct scatterlist *src,
919 struct scatterlist *dst,
920 unsigned int cryptlen, u8 *iv)
922 req->src = src;
923 req->dst = dst;
924 req->cryptlen = cryptlen;
925 req->iv = iv;
928 static inline void aead_request_set_assoc(struct aead_request *req,
929 struct scatterlist *assoc,
930 unsigned int assoclen)
932 req->assoc = assoc;
933 req->assoclen = assoclen;
936 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
937 struct crypto_tfm *tfm)
939 return (struct crypto_blkcipher *)tfm;
942 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
943 struct crypto_tfm *tfm)
945 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
946 return __crypto_blkcipher_cast(tfm);
949 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
950 const char *alg_name, u32 type, u32 mask)
952 type &= ~CRYPTO_ALG_TYPE_MASK;
953 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
954 mask |= CRYPTO_ALG_TYPE_MASK;
956 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
959 static inline struct crypto_tfm *crypto_blkcipher_tfm(
960 struct crypto_blkcipher *tfm)
962 return &tfm->base;
965 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
967 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
970 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
972 type &= ~CRYPTO_ALG_TYPE_MASK;
973 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
974 mask |= CRYPTO_ALG_TYPE_MASK;
976 return crypto_has_alg(alg_name, type, mask);
979 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
981 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
984 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
985 struct crypto_blkcipher *tfm)
987 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
990 static inline struct blkcipher_alg *crypto_blkcipher_alg(
991 struct crypto_blkcipher *tfm)
993 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
996 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
998 return crypto_blkcipher_alg(tfm)->ivsize;
1001 static inline unsigned int crypto_blkcipher_blocksize(
1002 struct crypto_blkcipher *tfm)
1004 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1007 static inline unsigned int crypto_blkcipher_alignmask(
1008 struct crypto_blkcipher *tfm)
1010 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1013 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1015 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1018 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1019 u32 flags)
1021 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1024 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1025 u32 flags)
1027 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1030 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1031 const u8 *key, unsigned int keylen)
1033 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1034 key, keylen);
1037 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1038 struct scatterlist *dst,
1039 struct scatterlist *src,
1040 unsigned int nbytes)
1042 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1043 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1046 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1047 struct scatterlist *dst,
1048 struct scatterlist *src,
1049 unsigned int nbytes)
1051 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1054 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1055 struct scatterlist *dst,
1056 struct scatterlist *src,
1057 unsigned int nbytes)
1059 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1060 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1063 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1064 struct scatterlist *dst,
1065 struct scatterlist *src,
1066 unsigned int nbytes)
1068 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1071 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1072 const u8 *src, unsigned int len)
1074 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1077 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1078 u8 *dst, unsigned int len)
1080 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1083 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1085 return (struct crypto_cipher *)tfm;
1088 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1090 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1091 return __crypto_cipher_cast(tfm);
1094 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1095 u32 type, u32 mask)
1097 type &= ~CRYPTO_ALG_TYPE_MASK;
1098 type |= CRYPTO_ALG_TYPE_CIPHER;
1099 mask |= CRYPTO_ALG_TYPE_MASK;
1101 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1104 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1106 return &tfm->base;
1109 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1111 crypto_free_tfm(crypto_cipher_tfm(tfm));
1114 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1116 type &= ~CRYPTO_ALG_TYPE_MASK;
1117 type |= CRYPTO_ALG_TYPE_CIPHER;
1118 mask |= CRYPTO_ALG_TYPE_MASK;
1120 return crypto_has_alg(alg_name, type, mask);
1123 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1125 return &crypto_cipher_tfm(tfm)->crt_cipher;
1128 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1130 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1133 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1135 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1138 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1140 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1143 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1144 u32 flags)
1146 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1149 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1150 u32 flags)
1152 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1155 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1156 const u8 *key, unsigned int keylen)
1158 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1159 key, keylen);
1162 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1163 u8 *dst, const u8 *src)
1165 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1166 dst, src);
1169 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1170 u8 *dst, const u8 *src)
1172 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1173 dst, src);
1176 static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
1178 return (struct crypto_hash *)tfm;
1181 static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
1183 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1184 CRYPTO_ALG_TYPE_HASH_MASK);
1185 return __crypto_hash_cast(tfm);
1188 static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1189 u32 type, u32 mask)
1191 type &= ~CRYPTO_ALG_TYPE_MASK;
1192 mask &= ~CRYPTO_ALG_TYPE_MASK;
1193 type |= CRYPTO_ALG_TYPE_HASH;
1194 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1196 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1199 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1201 return &tfm->base;
1204 static inline void crypto_free_hash(struct crypto_hash *tfm)
1206 crypto_free_tfm(crypto_hash_tfm(tfm));
1209 static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1211 type &= ~CRYPTO_ALG_TYPE_MASK;
1212 mask &= ~CRYPTO_ALG_TYPE_MASK;
1213 type |= CRYPTO_ALG_TYPE_HASH;
1214 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1216 return crypto_has_alg(alg_name, type, mask);
1219 static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1221 return &crypto_hash_tfm(tfm)->crt_hash;
1224 static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1226 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1229 static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1231 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1234 static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1236 return crypto_hash_crt(tfm)->digestsize;
1239 static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1241 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1244 static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1246 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1249 static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1251 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1254 static inline int crypto_hash_init(struct hash_desc *desc)
1256 return crypto_hash_crt(desc->tfm)->init(desc);
1259 static inline int crypto_hash_update(struct hash_desc *desc,
1260 struct scatterlist *sg,
1261 unsigned int nbytes)
1263 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1266 static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1268 return crypto_hash_crt(desc->tfm)->final(desc, out);
1271 static inline int crypto_hash_digest(struct hash_desc *desc,
1272 struct scatterlist *sg,
1273 unsigned int nbytes, u8 *out)
1275 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1278 static inline int crypto_hash_setkey(struct crypto_hash *hash,
1279 const u8 *key, unsigned int keylen)
1281 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
1284 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1286 return (struct crypto_comp *)tfm;
1289 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1291 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1292 CRYPTO_ALG_TYPE_MASK);
1293 return __crypto_comp_cast(tfm);
1296 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1297 u32 type, u32 mask)
1299 type &= ~CRYPTO_ALG_TYPE_MASK;
1300 type |= CRYPTO_ALG_TYPE_COMPRESS;
1301 mask |= CRYPTO_ALG_TYPE_MASK;
1303 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1306 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1308 return &tfm->base;
1311 static inline void crypto_free_comp(struct crypto_comp *tfm)
1313 crypto_free_tfm(crypto_comp_tfm(tfm));
1316 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1318 type &= ~CRYPTO_ALG_TYPE_MASK;
1319 type |= CRYPTO_ALG_TYPE_COMPRESS;
1320 mask |= CRYPTO_ALG_TYPE_MASK;
1322 return crypto_has_alg(alg_name, type, mask);
1325 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1327 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1330 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1332 return &crypto_comp_tfm(tfm)->crt_compress;
1335 static inline int crypto_comp_compress(struct crypto_comp *tfm,
1336 const u8 *src, unsigned int slen,
1337 u8 *dst, unsigned int *dlen)
1339 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1340 src, slen, dst, dlen);
1343 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
1344 const u8 *src, unsigned int slen,
1345 u8 *dst, unsigned int *dlen)
1347 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1348 src, slen, dst, dlen);
1351 #endif /* _LINUX_CRYPTO_H */