2 * Scatterlist Cryptographic API.
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
6 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
8 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
9 * and Nettle, by Niels Möller.
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
17 #ifndef _LINUX_CRYPTO_H
18 #define _LINUX_CRYPTO_H
20 #include <asm/atomic.h>
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/list.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/uaccess.h>
29 * Algorithm masks and types.
31 #define CRYPTO_ALG_TYPE_MASK 0x0000000f
32 #define CRYPTO_ALG_TYPE_CIPHER 0x00000001
33 #define CRYPTO_ALG_TYPE_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_MODE_MASK 0x000000ff
55 #define CRYPTO_TFM_REQ_MASK 0x000fff00
56 #define CRYPTO_TFM_RES_MASK 0xfff00000
58 #define CRYPTO_TFM_MODE_ECB 0x00000001
59 #define CRYPTO_TFM_MODE_CBC 0x00000002
60 #define CRYPTO_TFM_MODE_CFB 0x00000004
61 #define CRYPTO_TFM_MODE_CTR 0x00000008
63 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
64 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
65 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
66 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
67 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
68 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
69 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
72 * Miscellaneous stuff.
74 #define CRYPTO_UNSPEC 0
75 #define CRYPTO_MAX_ALG_NAME 64
77 #define CRYPTO_DIR_ENCRYPT 1
78 #define CRYPTO_DIR_DECRYPT 0
81 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
82 * declaration) is used to ensure that the crypto_tfm context structure is
83 * aligned correctly for the given architecture so that there are no alignment
84 * faults for C data types. In particular, this is required on platforms such
85 * as arm where pointers are 32-bit aligned but there are data types such as
86 * u64 which require 64-bit alignment.
88 #if defined(ARCH_KMALLOC_MINALIGN)
89 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
90 #elif defined(ARCH_SLAB_MINALIGN)
91 #define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
94 #ifdef CRYPTO_MINALIGN
95 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
97 #define CRYPTO_MINALIGN_ATTR
101 struct crypto_blkcipher;
106 struct blkcipher_desc {
107 struct crypto_blkcipher *tfm;
113 struct crypto_tfm *tfm;
114 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
115 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
116 const u8 *src, unsigned int nbytes);
121 struct crypto_hash *tfm;
126 * Algorithms: modular crypto algorithm implementations, managed
127 * via crypto_register_alg() and crypto_unregister_alg().
129 struct blkcipher_alg {
130 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
131 unsigned int keylen);
132 int (*encrypt)(struct blkcipher_desc *desc,
133 struct scatterlist *dst, struct scatterlist *src,
134 unsigned int nbytes);
135 int (*decrypt)(struct blkcipher_desc *desc,
136 struct scatterlist *dst, struct scatterlist *src,
137 unsigned int nbytes);
139 unsigned int min_keysize;
140 unsigned int max_keysize;
145 unsigned int cia_min_keysize;
146 unsigned int cia_max_keysize;
147 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
148 unsigned int keylen);
149 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
150 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
152 unsigned int (*cia_encrypt_ecb)(const struct cipher_desc *desc,
153 u8 *dst, const u8 *src,
154 unsigned int nbytes) __deprecated;
155 unsigned int (*cia_decrypt_ecb)(const struct cipher_desc *desc,
156 u8 *dst, const u8 *src,
157 unsigned int nbytes) __deprecated;
158 unsigned int (*cia_encrypt_cbc)(const struct cipher_desc *desc,
159 u8 *dst, const u8 *src,
160 unsigned int nbytes) __deprecated;
161 unsigned int (*cia_decrypt_cbc)(const struct cipher_desc *desc,
162 u8 *dst, const u8 *src,
163 unsigned int nbytes) __deprecated;
167 unsigned int dia_digestsize;
168 void (*dia_init)(struct crypto_tfm *tfm);
169 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
171 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
172 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
173 unsigned int keylen);
177 int (*init)(struct hash_desc *desc);
178 int (*update)(struct hash_desc *desc, struct scatterlist *sg,
179 unsigned int nbytes);
180 int (*final)(struct hash_desc *desc, u8 *out);
181 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
182 unsigned int nbytes, u8 *out);
183 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
184 unsigned int keylen);
186 unsigned int digestsize;
189 struct compress_alg {
190 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
191 unsigned int slen, u8 *dst, unsigned int *dlen);
192 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
193 unsigned int slen, u8 *dst, unsigned int *dlen);
196 #define cra_blkcipher cra_u.blkcipher
197 #define cra_cipher cra_u.cipher
198 #define cra_digest cra_u.digest
199 #define cra_hash cra_u.hash
200 #define cra_compress cra_u.compress
203 struct list_head cra_list;
204 struct list_head cra_users;
207 unsigned int cra_blocksize;
208 unsigned int cra_ctxsize;
209 unsigned int cra_alignmask;
214 char cra_name[CRYPTO_MAX_ALG_NAME];
215 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
217 const struct crypto_type *cra_type;
220 struct blkcipher_alg blkcipher;
221 struct cipher_alg cipher;
222 struct digest_alg digest;
223 struct hash_alg hash;
224 struct compress_alg compress;
227 int (*cra_init)(struct crypto_tfm *tfm);
228 void (*cra_exit)(struct crypto_tfm *tfm);
229 void (*cra_destroy)(struct crypto_alg *alg);
231 struct module *cra_module;
235 * Algorithm registration interface.
237 int crypto_register_alg(struct crypto_alg *alg);
238 int crypto_unregister_alg(struct crypto_alg *alg);
241 * Algorithm query interface.
244 int crypto_alg_available(const char *name, u32 flags);
245 int crypto_has_alg(const char *name, u32 type, u32 mask);
247 static inline int crypto_alg_available(const char *name, u32 flags)
252 static inline int crypto_has_alg(const char *name, u32 type, u32 mask)
259 * Transforms: user-instantiated objects which encapsulate algorithms
260 * and core processing logic. Managed via crypto_alloc_*() and
261 * crypto_free_*(), as well as the various helpers below.
264 struct blkcipher_tfm {
266 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
267 unsigned int keylen);
268 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
269 struct scatterlist *src, unsigned int nbytes);
270 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
271 struct scatterlist *src, unsigned int nbytes);
276 unsigned int cit_ivsize;
278 int (*cit_setkey)(struct crypto_tfm *tfm,
279 const u8 *key, unsigned int keylen);
280 int (*cit_encrypt)(struct crypto_tfm *tfm,
281 struct scatterlist *dst,
282 struct scatterlist *src,
283 unsigned int nbytes);
284 int (*cit_encrypt_iv)(struct crypto_tfm *tfm,
285 struct scatterlist *dst,
286 struct scatterlist *src,
287 unsigned int nbytes, u8 *iv);
288 int (*cit_decrypt)(struct crypto_tfm *tfm,
289 struct scatterlist *dst,
290 struct scatterlist *src,
291 unsigned int nbytes);
292 int (*cit_decrypt_iv)(struct crypto_tfm *tfm,
293 struct scatterlist *dst,
294 struct scatterlist *src,
295 unsigned int nbytes, u8 *iv);
296 void (*cit_xor_block)(u8 *dst, const u8 *src);
297 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
298 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
302 int (*init)(struct hash_desc *desc);
303 int (*update)(struct hash_desc *desc,
304 struct scatterlist *sg, unsigned int nsg);
305 int (*final)(struct hash_desc *desc, u8 *out);
306 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
307 unsigned int nsg, u8 *out);
308 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
309 unsigned int keylen);
310 unsigned int digestsize;
313 struct compress_tfm {
314 int (*cot_compress)(struct crypto_tfm *tfm,
315 const u8 *src, unsigned int slen,
316 u8 *dst, unsigned int *dlen);
317 int (*cot_decompress)(struct crypto_tfm *tfm,
318 const u8 *src, unsigned int slen,
319 u8 *dst, unsigned int *dlen);
322 #define crt_blkcipher crt_u.blkcipher
323 #define crt_cipher crt_u.cipher
324 #define crt_hash crt_u.hash
325 #define crt_compress crt_u.compress
332 struct blkcipher_tfm blkcipher;
333 struct cipher_tfm cipher;
334 struct hash_tfm hash;
335 struct compress_tfm compress;
338 struct crypto_alg *__crt_alg;
340 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
343 #define crypto_cipher crypto_tfm
344 #define crypto_comp crypto_tfm
346 struct crypto_blkcipher {
347 struct crypto_tfm base;
351 struct crypto_tfm base;
359 struct crypto_attr_alg {
360 char name[CRYPTO_MAX_ALG_NAME];
364 * Transform user interface.
367 struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
368 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
369 void crypto_free_tfm(struct crypto_tfm *tfm);
372 * Transform helpers which query the underlying algorithm.
374 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
376 return tfm->__crt_alg->cra_name;
379 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
381 return tfm->__crt_alg->cra_driver_name;
384 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
386 return tfm->__crt_alg->cra_priority;
389 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
391 return module_name(tfm->__crt_alg->cra_module);
394 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
396 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
399 static unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
401 static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
403 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
404 return tfm->__crt_alg->cra_cipher.cia_min_keysize;
407 static unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
409 static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
411 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
412 return tfm->__crt_alg->cra_cipher.cia_max_keysize;
415 static unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm) __deprecated;
416 static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm)
418 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
419 return tfm->crt_cipher.cit_ivsize;
422 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
424 return tfm->__crt_alg->cra_blocksize;
427 static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm *tfm)
429 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
430 return tfm->__crt_alg->cra_digest.dia_digestsize;
433 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
435 return tfm->__crt_alg->cra_alignmask;
438 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
440 return tfm->crt_flags;
443 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
445 tfm->crt_flags |= flags;
448 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
450 tfm->crt_flags &= ~flags;
453 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
455 return tfm->__crt_ctx;
458 static inline unsigned int crypto_tfm_ctx_alignment(void)
460 struct crypto_tfm *tfm;
461 return __alignof__(tfm->__crt_ctx);
467 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
468 struct crypto_tfm *tfm)
470 return (struct crypto_blkcipher *)tfm;
473 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
474 struct crypto_tfm *tfm)
476 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
477 return __crypto_blkcipher_cast(tfm);
480 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
481 const char *alg_name, u32 type, u32 mask)
483 type &= ~CRYPTO_ALG_TYPE_MASK;
484 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
485 mask |= CRYPTO_ALG_TYPE_MASK;
487 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
490 static inline struct crypto_tfm *crypto_blkcipher_tfm(
491 struct crypto_blkcipher *tfm)
496 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
498 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
501 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
503 type &= ~CRYPTO_ALG_TYPE_MASK;
504 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
505 mask |= CRYPTO_ALG_TYPE_MASK;
507 return crypto_has_alg(alg_name, type, mask);
510 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
512 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
515 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
516 struct crypto_blkcipher *tfm)
518 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
521 static inline struct blkcipher_alg *crypto_blkcipher_alg(
522 struct crypto_blkcipher *tfm)
524 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
527 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
529 return crypto_blkcipher_alg(tfm)->ivsize;
532 static inline unsigned int crypto_blkcipher_blocksize(
533 struct crypto_blkcipher *tfm)
535 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
538 static inline unsigned int crypto_blkcipher_alignmask(
539 struct crypto_blkcipher *tfm)
541 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
544 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
546 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
549 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
552 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
555 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
558 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
561 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
562 const u8 *key, unsigned int keylen)
564 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
568 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
569 struct scatterlist *dst,
570 struct scatterlist *src,
573 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
574 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
577 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
578 struct scatterlist *dst,
579 struct scatterlist *src,
582 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
585 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
586 struct scatterlist *dst,
587 struct scatterlist *src,
590 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
591 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
594 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
595 struct scatterlist *dst,
596 struct scatterlist *src,
599 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
602 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
603 const u8 *src, unsigned int len)
605 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
608 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
609 u8 *dst, unsigned int len)
611 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
614 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
616 return (struct crypto_cipher *)tfm;
619 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
621 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
622 return __crypto_cipher_cast(tfm);
625 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
628 type &= ~CRYPTO_ALG_TYPE_MASK;
629 type |= CRYPTO_ALG_TYPE_CIPHER;
630 mask |= CRYPTO_ALG_TYPE_MASK;
632 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
635 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
640 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
642 crypto_free_tfm(crypto_cipher_tfm(tfm));
645 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
647 type &= ~CRYPTO_ALG_TYPE_MASK;
648 type |= CRYPTO_ALG_TYPE_CIPHER;
649 mask |= CRYPTO_ALG_TYPE_MASK;
651 return crypto_has_alg(alg_name, type, mask);
654 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
656 return &crypto_cipher_tfm(tfm)->crt_cipher;
659 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
661 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
664 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
666 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
669 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
671 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
674 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
677 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
680 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
683 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
686 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
687 const u8 *key, unsigned int keylen)
689 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
693 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
694 u8 *dst, const u8 *src)
696 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
700 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
701 u8 *dst, const u8 *src)
703 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
707 void crypto_digest_init(struct crypto_tfm *tfm);
708 void crypto_digest_update(struct crypto_tfm *tfm,
709 struct scatterlist *sg, unsigned int nsg);
710 void crypto_digest_final(struct crypto_tfm *tfm, u8 *out);
711 void crypto_digest_digest(struct crypto_tfm *tfm,
712 struct scatterlist *sg, unsigned int nsg, u8 *out);
714 static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
716 return (struct crypto_hash *)tfm;
719 static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
721 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
722 CRYPTO_ALG_TYPE_HASH_MASK);
723 return __crypto_hash_cast(tfm);
726 static inline int crypto_digest_setkey(struct crypto_tfm *tfm,
727 const u8 *key, unsigned int keylen)
729 return tfm->crt_hash.setkey(crypto_hash_cast(tfm), key, keylen);
732 static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
735 type &= ~CRYPTO_ALG_TYPE_MASK;
736 type |= CRYPTO_ALG_TYPE_HASH;
737 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
739 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
742 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
747 static inline void crypto_free_hash(struct crypto_hash *tfm)
749 crypto_free_tfm(crypto_hash_tfm(tfm));
752 static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
754 type &= ~CRYPTO_ALG_TYPE_MASK;
755 type |= CRYPTO_ALG_TYPE_HASH;
756 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
758 return crypto_has_alg(alg_name, type, mask);
761 static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
763 return &crypto_hash_tfm(tfm)->crt_hash;
766 static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
768 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
771 static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
773 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
776 static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
778 return crypto_hash_crt(tfm)->digestsize;
781 static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
783 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
786 static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
788 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
791 static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
793 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
796 static inline int crypto_hash_init(struct hash_desc *desc)
798 return crypto_hash_crt(desc->tfm)->init(desc);
801 static inline int crypto_hash_update(struct hash_desc *desc,
802 struct scatterlist *sg,
805 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
808 static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
810 return crypto_hash_crt(desc->tfm)->final(desc, out);
813 static inline int crypto_hash_digest(struct hash_desc *desc,
814 struct scatterlist *sg,
815 unsigned int nbytes, u8 *out)
817 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
820 static inline int crypto_hash_setkey(struct crypto_hash *hash,
821 const u8 *key, unsigned int keylen)
823 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
826 static int crypto_cipher_encrypt(struct crypto_tfm *tfm,
827 struct scatterlist *dst,
828 struct scatterlist *src,
829 unsigned int nbytes) __deprecated;
830 static inline int crypto_cipher_encrypt(struct crypto_tfm *tfm,
831 struct scatterlist *dst,
832 struct scatterlist *src,
835 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
836 return tfm->crt_cipher.cit_encrypt(tfm, dst, src, nbytes);
839 static int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
840 struct scatterlist *dst,
841 struct scatterlist *src,
842 unsigned int nbytes, u8 *iv) __deprecated;
843 static inline int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
844 struct scatterlist *dst,
845 struct scatterlist *src,
846 unsigned int nbytes, u8 *iv)
848 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
849 return tfm->crt_cipher.cit_encrypt_iv(tfm, dst, src, nbytes, iv);
852 static int crypto_cipher_decrypt(struct crypto_tfm *tfm,
853 struct scatterlist *dst,
854 struct scatterlist *src,
855 unsigned int nbytes) __deprecated;
856 static inline int crypto_cipher_decrypt(struct crypto_tfm *tfm,
857 struct scatterlist *dst,
858 struct scatterlist *src,
861 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
862 return tfm->crt_cipher.cit_decrypt(tfm, dst, src, nbytes);
865 static int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
866 struct scatterlist *dst,
867 struct scatterlist *src,
868 unsigned int nbytes, u8 *iv) __deprecated;
869 static inline int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
870 struct scatterlist *dst,
871 struct scatterlist *src,
872 unsigned int nbytes, u8 *iv)
874 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
875 return tfm->crt_cipher.cit_decrypt_iv(tfm, dst, src, nbytes, iv);
878 static void crypto_cipher_set_iv(struct crypto_tfm *tfm,
879 const u8 *src, unsigned int len) __deprecated;
880 static inline void crypto_cipher_set_iv(struct crypto_tfm *tfm,
881 const u8 *src, unsigned int len)
883 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
884 memcpy(tfm->crt_cipher.cit_iv, src, len);
887 static void crypto_cipher_get_iv(struct crypto_tfm *tfm,
888 u8 *dst, unsigned int len) __deprecated;
889 static inline void crypto_cipher_get_iv(struct crypto_tfm *tfm,
890 u8 *dst, unsigned int len)
892 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
893 memcpy(dst, tfm->crt_cipher.cit_iv, len);
896 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
898 return (struct crypto_comp *)tfm;
901 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
903 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
904 CRYPTO_ALG_TYPE_MASK);
905 return __crypto_comp_cast(tfm);
908 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
911 type &= ~CRYPTO_ALG_TYPE_MASK;
912 type |= CRYPTO_ALG_TYPE_COMPRESS;
913 mask |= CRYPTO_ALG_TYPE_MASK;
915 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
918 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
923 static inline void crypto_free_comp(struct crypto_comp *tfm)
925 crypto_free_tfm(crypto_comp_tfm(tfm));
928 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
930 type &= ~CRYPTO_ALG_TYPE_MASK;
931 type |= CRYPTO_ALG_TYPE_COMPRESS;
932 mask |= CRYPTO_ALG_TYPE_MASK;
934 return crypto_has_alg(alg_name, type, mask);
937 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
939 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
942 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
944 return &crypto_comp_tfm(tfm)->crt_compress;
947 static inline int crypto_comp_compress(struct crypto_comp *tfm,
948 const u8 *src, unsigned int slen,
949 u8 *dst, unsigned int *dlen)
951 return crypto_comp_crt(tfm)->cot_compress(tfm, src, slen, dst, dlen);
954 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
955 const u8 *src, unsigned int slen,
956 u8 *dst, unsigned int *dlen)
958 return crypto_comp_crt(tfm)->cot_decompress(tfm, src, slen, dst, dlen);
961 #endif /* _LINUX_CRYPTO_H */