RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / include / linux / crypto.h
blob9b825753bbfb1b9f0ca57e28099a74700b685294
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_DIGEST 0x00000002
34 #define CRYPTO_ALG_TYPE_HASH 0x00000003
35 #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
36 #define CRYPTO_ALG_TYPE_COMPRESS 0x00000005
38 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
40 #define CRYPTO_ALG_LARVAL 0x00000010
41 #define CRYPTO_ALG_DEAD 0x00000020
42 #define CRYPTO_ALG_DYING 0x00000040
43 #define CRYPTO_ALG_ASYNC 0x00000080
46 * Set this bit if and only if the algorithm requires another algorithm of
47 * the same type to handle corner cases.
49 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
52 * Transform masks and values (for crt_flags).
54 #define CRYPTO_TFM_REQ_MASK 0x000fff00
55 #define CRYPTO_TFM_RES_MASK 0xfff00000
57 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
58 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
59 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
60 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
61 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
62 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
63 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
64 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
67 * Miscellaneous stuff.
69 #define CRYPTO_MAX_ALG_NAME 64
72 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
73 * declaration) is used to ensure that the crypto_tfm context structure is
74 * aligned correctly for the given architecture so that there are no alignment
75 * faults for C data types. In particular, this is required on platforms such
76 * as arm where pointers are 32-bit aligned but there are data types such as
77 * u64 which require 64-bit alignment.
79 #if defined(ARCH_KMALLOC_MINALIGN)
80 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
81 #elif defined(ARCH_SLAB_MINALIGN)
82 #define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
83 #endif
85 #ifdef CRYPTO_MINALIGN
86 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
87 #else
88 #define CRYPTO_MINALIGN_ATTR
89 #endif
91 struct scatterlist;
92 struct crypto_ablkcipher;
93 struct crypto_async_request;
94 struct crypto_blkcipher;
95 struct crypto_hash;
96 struct crypto_queue;
97 struct crypto_tfm;
98 struct crypto_type;
100 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
102 struct crypto_async_request {
103 struct list_head list;
104 crypto_completion_t complete;
105 void *data;
106 struct crypto_tfm *tfm;
108 u32 flags;
111 struct ablkcipher_request {
112 struct crypto_async_request base;
114 unsigned int nbytes;
116 void *info;
118 struct scatterlist *src;
119 struct scatterlist *dst;
121 void *__ctx[] CRYPTO_MINALIGN_ATTR;
124 struct blkcipher_desc {
125 struct crypto_blkcipher *tfm;
126 void *info;
127 u32 flags;
130 struct cipher_desc {
131 struct crypto_tfm *tfm;
132 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
133 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
134 const u8 *src, unsigned int nbytes);
135 void *info;
138 struct hash_desc {
139 struct crypto_hash *tfm;
140 u32 flags;
144 * Algorithms: modular crypto algorithm implementations, managed
145 * via crypto_register_alg() and crypto_unregister_alg().
147 struct ablkcipher_alg {
148 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
149 unsigned int keylen);
150 int (*encrypt)(struct ablkcipher_request *req);
151 int (*decrypt)(struct ablkcipher_request *req);
153 struct crypto_queue *queue;
155 unsigned int min_keysize;
156 unsigned int max_keysize;
157 unsigned int ivsize;
160 struct blkcipher_alg {
161 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
162 unsigned int keylen);
163 int (*encrypt)(struct blkcipher_desc *desc,
164 struct scatterlist *dst, struct scatterlist *src,
165 unsigned int nbytes);
166 int (*decrypt)(struct blkcipher_desc *desc,
167 struct scatterlist *dst, struct scatterlist *src,
168 unsigned int nbytes);
170 unsigned int min_keysize;
171 unsigned int max_keysize;
172 unsigned int ivsize;
175 struct cipher_alg {
176 unsigned int cia_min_keysize;
177 unsigned int cia_max_keysize;
178 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
179 unsigned int keylen);
180 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
181 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
184 struct digest_alg {
185 unsigned int dia_digestsize;
186 void (*dia_init)(struct crypto_tfm *tfm);
187 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
188 unsigned int len);
189 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
190 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
191 unsigned int keylen);
194 struct hash_alg {
195 int (*init)(struct hash_desc *desc);
196 int (*update)(struct hash_desc *desc, struct scatterlist *sg,
197 unsigned int nbytes);
198 int (*final)(struct hash_desc *desc, u8 *out);
199 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
200 unsigned int nbytes, u8 *out);
201 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
202 unsigned int keylen);
204 unsigned int digestsize;
207 struct compress_alg {
208 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
209 unsigned int slen, u8 *dst, unsigned int *dlen);
210 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
211 unsigned int slen, u8 *dst, unsigned int *dlen);
214 #define cra_ablkcipher cra_u.ablkcipher
215 #define cra_blkcipher cra_u.blkcipher
216 #define cra_cipher cra_u.cipher
217 #define cra_digest cra_u.digest
218 #define cra_hash cra_u.hash
219 #define cra_compress cra_u.compress
221 struct crypto_alg {
222 struct list_head cra_list;
223 struct list_head cra_users;
225 u32 cra_flags;
226 unsigned int cra_blocksize;
227 unsigned int cra_ctxsize;
228 unsigned int cra_alignmask;
230 int cra_priority;
231 atomic_t cra_refcnt;
233 char cra_name[CRYPTO_MAX_ALG_NAME];
234 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
236 const struct crypto_type *cra_type;
238 union {
239 struct ablkcipher_alg ablkcipher;
240 struct blkcipher_alg blkcipher;
241 struct cipher_alg cipher;
242 struct digest_alg digest;
243 struct hash_alg hash;
244 struct compress_alg compress;
245 } cra_u;
247 int (*cra_init)(struct crypto_tfm *tfm);
248 void (*cra_exit)(struct crypto_tfm *tfm);
249 void (*cra_destroy)(struct crypto_alg *alg);
251 struct module *cra_module;
255 * Algorithm registration interface.
257 int crypto_register_alg(struct crypto_alg *alg);
258 int crypto_unregister_alg(struct crypto_alg *alg);
261 * Algorithm query interface.
263 #ifdef CONFIG_CRYPTO
264 int crypto_alg_available(const char *name, u32 flags)
265 __deprecated_for_modules;
266 int crypto_has_alg(const char *name, u32 type, u32 mask);
267 #else
268 static int crypto_alg_available(const char *name, u32 flags)
269 __deprecated_for_modules;
270 static inline int crypto_has_alg(const char *name, u32 type, u32 mask)
272 return 0;
274 #endif
277 * Transforms: user-instantiated objects which encapsulate algorithms
278 * and core processing logic. Managed via crypto_alloc_*() and
279 * crypto_free_*(), as well as the various helpers below.
282 struct ablkcipher_tfm {
283 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
284 unsigned int keylen);
285 int (*encrypt)(struct ablkcipher_request *req);
286 int (*decrypt)(struct ablkcipher_request *req);
287 unsigned int ivsize;
288 unsigned int reqsize;
291 struct blkcipher_tfm {
292 void *iv;
293 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
294 unsigned int keylen);
295 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
296 struct scatterlist *src, unsigned int nbytes);
297 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
298 struct scatterlist *src, unsigned int nbytes);
301 struct cipher_tfm {
302 void *cit_iv;
303 unsigned int cit_ivsize;
304 u32 cit_mode;
305 int (*cit_setkey)(struct crypto_tfm *tfm,
306 const u8 *key, unsigned int keylen);
307 int (*cit_encrypt)(struct crypto_tfm *tfm,
308 struct scatterlist *dst,
309 struct scatterlist *src,
310 unsigned int nbytes);
311 int (*cit_encrypt_iv)(struct crypto_tfm *tfm,
312 struct scatterlist *dst,
313 struct scatterlist *src,
314 unsigned int nbytes, u8 *iv);
315 int (*cit_decrypt)(struct crypto_tfm *tfm,
316 struct scatterlist *dst,
317 struct scatterlist *src,
318 unsigned int nbytes);
319 int (*cit_decrypt_iv)(struct crypto_tfm *tfm,
320 struct scatterlist *dst,
321 struct scatterlist *src,
322 unsigned int nbytes, u8 *iv);
323 void (*cit_xor_block)(u8 *dst, const u8 *src);
324 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
325 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
328 struct hash_tfm {
329 int (*init)(struct hash_desc *desc);
330 int (*update)(struct hash_desc *desc,
331 struct scatterlist *sg, unsigned int nsg);
332 int (*final)(struct hash_desc *desc, u8 *out);
333 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
334 unsigned int nsg, u8 *out);
335 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
336 unsigned int keylen);
337 unsigned int digestsize;
340 struct compress_tfm {
341 int (*cot_compress)(struct crypto_tfm *tfm,
342 const u8 *src, unsigned int slen,
343 u8 *dst, unsigned int *dlen);
344 int (*cot_decompress)(struct crypto_tfm *tfm,
345 const u8 *src, unsigned int slen,
346 u8 *dst, unsigned int *dlen);
349 #define crt_ablkcipher crt_u.ablkcipher
350 #define crt_blkcipher crt_u.blkcipher
351 #define crt_cipher crt_u.cipher
352 #define crt_hash crt_u.hash
353 #define crt_compress crt_u.compress
355 struct crypto_tfm {
357 u32 crt_flags;
359 union {
360 struct ablkcipher_tfm ablkcipher;
361 struct blkcipher_tfm blkcipher;
362 struct cipher_tfm cipher;
363 struct hash_tfm hash;
364 struct compress_tfm compress;
365 } crt_u;
367 struct crypto_alg *__crt_alg;
369 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
372 struct crypto_ablkcipher {
373 struct crypto_tfm base;
376 struct crypto_blkcipher {
377 struct crypto_tfm base;
380 struct crypto_cipher {
381 struct crypto_tfm base;
384 struct crypto_comp {
385 struct crypto_tfm base;
388 struct crypto_hash {
389 struct crypto_tfm base;
392 enum {
393 CRYPTOA_UNSPEC,
394 CRYPTOA_ALG,
395 CRYPTOA_TYPE,
396 __CRYPTOA_MAX,
399 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
401 struct crypto_attr_alg {
402 char name[CRYPTO_MAX_ALG_NAME];
405 struct crypto_attr_type {
406 u32 type;
407 u32 mask;
411 * Transform user interface.
414 struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
415 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
416 void crypto_free_tfm(struct crypto_tfm *tfm);
419 * Transform helpers which query the underlying algorithm.
421 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
423 return tfm->__crt_alg->cra_name;
426 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
428 return tfm->__crt_alg->cra_driver_name;
431 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
433 return tfm->__crt_alg->cra_priority;
436 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
438 return module_name(tfm->__crt_alg->cra_module);
441 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
443 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
446 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
448 return tfm->__crt_alg->cra_blocksize;
451 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
453 return tfm->__crt_alg->cra_alignmask;
456 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
458 return tfm->crt_flags;
461 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
463 tfm->crt_flags |= flags;
466 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
468 tfm->crt_flags &= ~flags;
471 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
473 return tfm->__crt_ctx;
476 static inline unsigned int crypto_tfm_ctx_alignment(void)
478 struct crypto_tfm *tfm;
479 return __alignof__(tfm->__crt_ctx);
483 * API wrappers.
485 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
486 struct crypto_tfm *tfm)
488 return (struct crypto_ablkcipher *)tfm;
491 static inline struct crypto_ablkcipher *crypto_alloc_ablkcipher(
492 const char *alg_name, u32 type, u32 mask)
494 type &= ~CRYPTO_ALG_TYPE_MASK;
495 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
496 mask |= CRYPTO_ALG_TYPE_MASK;
498 return __crypto_ablkcipher_cast(
499 crypto_alloc_base(alg_name, type, mask));
502 static inline struct crypto_tfm *crypto_ablkcipher_tfm(
503 struct crypto_ablkcipher *tfm)
505 return &tfm->base;
508 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
510 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
513 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
514 u32 mask)
516 type &= ~CRYPTO_ALG_TYPE_MASK;
517 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
518 mask |= CRYPTO_ALG_TYPE_MASK;
520 return crypto_has_alg(alg_name, type, mask);
523 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
524 struct crypto_ablkcipher *tfm)
526 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
529 static inline unsigned int crypto_ablkcipher_ivsize(
530 struct crypto_ablkcipher *tfm)
532 return crypto_ablkcipher_crt(tfm)->ivsize;
535 static inline unsigned int crypto_ablkcipher_blocksize(
536 struct crypto_ablkcipher *tfm)
538 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
541 static inline unsigned int crypto_ablkcipher_alignmask(
542 struct crypto_ablkcipher *tfm)
544 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
547 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
549 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
552 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
553 u32 flags)
555 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
558 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
559 u32 flags)
561 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
564 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
565 const u8 *key, unsigned int keylen)
567 return crypto_ablkcipher_crt(tfm)->setkey(tfm, key, keylen);
570 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
571 struct ablkcipher_request *req)
573 return __crypto_ablkcipher_cast(req->base.tfm);
576 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
578 struct ablkcipher_tfm *crt =
579 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
580 return crt->encrypt(req);
583 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
585 struct ablkcipher_tfm *crt =
586 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
587 return crt->decrypt(req);
590 static inline int crypto_ablkcipher_reqsize(struct crypto_ablkcipher *tfm)
592 return crypto_ablkcipher_crt(tfm)->reqsize;
595 static inline void ablkcipher_request_set_tfm(
596 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
598 req->base.tfm = crypto_ablkcipher_tfm(tfm);
601 static inline struct ablkcipher_request *ablkcipher_request_cast(
602 struct crypto_async_request *req)
604 return container_of(req, struct ablkcipher_request, base);
607 static inline struct ablkcipher_request *ablkcipher_request_alloc(
608 struct crypto_ablkcipher *tfm, gfp_t gfp)
610 struct ablkcipher_request *req;
612 req = kmalloc(sizeof(struct ablkcipher_request) +
613 crypto_ablkcipher_reqsize(tfm), gfp);
615 if (likely(req))
616 ablkcipher_request_set_tfm(req, tfm);
618 return req;
621 static inline void ablkcipher_request_free(struct ablkcipher_request *req)
623 kfree(req);
626 static inline void ablkcipher_request_set_callback(
627 struct ablkcipher_request *req,
628 u32 flags, crypto_completion_t complete, void *data)
630 req->base.complete = complete;
631 req->base.data = data;
632 req->base.flags = flags;
635 static inline void ablkcipher_request_set_crypt(
636 struct ablkcipher_request *req,
637 struct scatterlist *src, struct scatterlist *dst,
638 unsigned int nbytes, void *iv)
640 req->src = src;
641 req->dst = dst;
642 req->nbytes = nbytes;
643 req->info = iv;
646 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
647 struct crypto_tfm *tfm)
649 return (struct crypto_blkcipher *)tfm;
652 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
653 struct crypto_tfm *tfm)
655 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
656 return __crypto_blkcipher_cast(tfm);
659 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
660 const char *alg_name, u32 type, u32 mask)
662 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
663 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
664 mask |= CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC;
666 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
669 static inline struct crypto_tfm *crypto_blkcipher_tfm(
670 struct crypto_blkcipher *tfm)
672 return &tfm->base;
675 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
677 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
680 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
682 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
683 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
684 mask |= CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC;
686 return crypto_has_alg(alg_name, type, mask);
689 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
691 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
694 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
695 struct crypto_blkcipher *tfm)
697 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
700 static inline struct blkcipher_alg *crypto_blkcipher_alg(
701 struct crypto_blkcipher *tfm)
703 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
706 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
708 return crypto_blkcipher_alg(tfm)->ivsize;
711 static inline unsigned int crypto_blkcipher_blocksize(
712 struct crypto_blkcipher *tfm)
714 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
717 static inline unsigned int crypto_blkcipher_alignmask(
718 struct crypto_blkcipher *tfm)
720 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
723 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
725 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
728 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
729 u32 flags)
731 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
734 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
735 u32 flags)
737 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
740 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
741 const u8 *key, unsigned int keylen)
743 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
744 key, keylen);
747 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
748 struct scatterlist *dst,
749 struct scatterlist *src,
750 unsigned int nbytes)
752 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
753 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
756 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
757 struct scatterlist *dst,
758 struct scatterlist *src,
759 unsigned int nbytes)
761 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
764 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
765 struct scatterlist *dst,
766 struct scatterlist *src,
767 unsigned int nbytes)
769 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
770 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
773 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
774 struct scatterlist *dst,
775 struct scatterlist *src,
776 unsigned int nbytes)
778 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
781 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
782 const u8 *src, unsigned int len)
784 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
787 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
788 u8 *dst, unsigned int len)
790 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
793 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
795 return (struct crypto_cipher *)tfm;
798 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
800 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
801 return __crypto_cipher_cast(tfm);
804 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
805 u32 type, u32 mask)
807 type &= ~CRYPTO_ALG_TYPE_MASK;
808 type |= CRYPTO_ALG_TYPE_CIPHER;
809 mask |= CRYPTO_ALG_TYPE_MASK;
811 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
814 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
816 return &tfm->base;
819 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
821 crypto_free_tfm(crypto_cipher_tfm(tfm));
824 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
826 type &= ~CRYPTO_ALG_TYPE_MASK;
827 type |= CRYPTO_ALG_TYPE_CIPHER;
828 mask |= CRYPTO_ALG_TYPE_MASK;
830 return crypto_has_alg(alg_name, type, mask);
833 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
835 return &crypto_cipher_tfm(tfm)->crt_cipher;
838 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
840 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
843 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
845 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
848 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
850 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
853 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
854 u32 flags)
856 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
859 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
860 u32 flags)
862 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
865 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
866 const u8 *key, unsigned int keylen)
868 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
869 key, keylen);
872 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
873 u8 *dst, const u8 *src)
875 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
876 dst, src);
879 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
880 u8 *dst, const u8 *src)
882 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
883 dst, src);
886 void crypto_digest_init(struct crypto_tfm *tfm) __deprecated_for_modules;
887 void crypto_digest_update(struct crypto_tfm *tfm,
888 struct scatterlist *sg, unsigned int nsg)
889 __deprecated_for_modules;
890 void crypto_digest_final(struct crypto_tfm *tfm, u8 *out)
891 __deprecated_for_modules;
892 void crypto_digest_digest(struct crypto_tfm *tfm,
893 struct scatterlist *sg, unsigned int nsg, u8 *out)
894 __deprecated_for_modules;
896 static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
898 return (struct crypto_hash *)tfm;
901 static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
903 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
904 CRYPTO_ALG_TYPE_HASH_MASK);
905 return __crypto_hash_cast(tfm);
908 static int crypto_digest_setkey(struct crypto_tfm *tfm, const u8 *key,
909 unsigned int keylen) __deprecated;
910 static inline int crypto_digest_setkey(struct crypto_tfm *tfm,
911 const u8 *key, unsigned int keylen)
913 return tfm->crt_hash.setkey(crypto_hash_cast(tfm), key, keylen);
916 static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
917 u32 type, u32 mask)
919 type &= ~CRYPTO_ALG_TYPE_MASK;
920 type |= CRYPTO_ALG_TYPE_HASH;
921 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
923 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
926 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
928 return &tfm->base;
931 static inline void crypto_free_hash(struct crypto_hash *tfm)
933 crypto_free_tfm(crypto_hash_tfm(tfm));
936 static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
938 type &= ~CRYPTO_ALG_TYPE_MASK;
939 type |= CRYPTO_ALG_TYPE_HASH;
940 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
942 return crypto_has_alg(alg_name, type, mask);
945 static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
947 return &crypto_hash_tfm(tfm)->crt_hash;
950 static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
952 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
955 static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
957 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
960 static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
962 return crypto_hash_crt(tfm)->digestsize;
965 static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
967 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
970 static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
972 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
975 static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
977 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
980 static inline int crypto_hash_init(struct hash_desc *desc)
982 return crypto_hash_crt(desc->tfm)->init(desc);
985 static inline int crypto_hash_update(struct hash_desc *desc,
986 struct scatterlist *sg,
987 unsigned int nbytes)
989 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
992 static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
994 return crypto_hash_crt(desc->tfm)->final(desc, out);
997 static inline int crypto_hash_digest(struct hash_desc *desc,
998 struct scatterlist *sg,
999 unsigned int nbytes, u8 *out)
1001 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1004 static inline int crypto_hash_setkey(struct crypto_hash *hash,
1005 const u8 *key, unsigned int keylen)
1007 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
1010 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1012 return (struct crypto_comp *)tfm;
1015 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1017 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1018 CRYPTO_ALG_TYPE_MASK);
1019 return __crypto_comp_cast(tfm);
1022 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1023 u32 type, u32 mask)
1025 type &= ~CRYPTO_ALG_TYPE_MASK;
1026 type |= CRYPTO_ALG_TYPE_COMPRESS;
1027 mask |= CRYPTO_ALG_TYPE_MASK;
1029 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1032 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1034 return &tfm->base;
1037 static inline void crypto_free_comp(struct crypto_comp *tfm)
1039 crypto_free_tfm(crypto_comp_tfm(tfm));
1042 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1044 type &= ~CRYPTO_ALG_TYPE_MASK;
1045 type |= CRYPTO_ALG_TYPE_COMPRESS;
1046 mask |= CRYPTO_ALG_TYPE_MASK;
1048 return crypto_has_alg(alg_name, type, mask);
1051 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1053 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1056 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1058 return &crypto_comp_tfm(tfm)->crt_compress;
1061 static inline int crypto_comp_compress(struct crypto_comp *tfm,
1062 const u8 *src, unsigned int slen,
1063 u8 *dst, unsigned int *dlen)
1065 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1066 src, slen, dst, dlen);
1069 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
1070 const u8 *src, unsigned int slen,
1071 u8 *dst, unsigned int *dlen)
1073 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1074 src, slen, dst, dlen);
1077 #endif /* _LINUX_CRYPTO_H */