diff options
author | Herbert Xu <herbert@gondor.apana.org.au> | 2006-08-19 08:24:23 -0400 |
---|---|---|
committer | Herbert Xu <herbert@gondor.apana.org.au> | 2006-09-20 21:46:17 -0400 |
commit | 055bcee3102dc35f019b69df9c2618e9d6dd1c09 (patch) | |
tree | 3f7c68abbbb5041d570e4cb8588f3943530bc0b7 /include/linux | |
parent | 7226bc877a22244e8003924031435a4bffd52654 (diff) |
[CRYPTO] digest: Added user API for new hash type
The existing digest user interface is inadequate for support asynchronous
operations. For one it doesn't return a value to indicate success or
failure, nor does it take a per-operation descriptor which is essential
for the issuing of requests while other requests are still outstanding.
This patch is the first in a series of steps to remodel the interface
for asynchronous operations.
For the ease of transition the new interface will be known as "hash"
while the old one will remain as "digest".
This patch also changes sg_next to allow chaining.
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'include/linux')
-rw-r--r-- | include/linux/crypto.h | 172 |
1 files changed, 138 insertions, 34 deletions
diff --git a/include/linux/crypto.h b/include/linux/crypto.h index 0be666b50463..40c0aab8ad4c 100644 --- a/include/linux/crypto.h +++ b/include/linux/crypto.h | |||
@@ -31,8 +31,11 @@ | |||
31 | #define CRYPTO_ALG_TYPE_MASK 0x0000000f | 31 | #define CRYPTO_ALG_TYPE_MASK 0x0000000f |
32 | #define CRYPTO_ALG_TYPE_CIPHER 0x00000001 | 32 | #define CRYPTO_ALG_TYPE_CIPHER 0x00000001 |
33 | #define CRYPTO_ALG_TYPE_DIGEST 0x00000002 | 33 | #define CRYPTO_ALG_TYPE_DIGEST 0x00000002 |
34 | #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000003 | 34 | #define CRYPTO_ALG_TYPE_HASH 0x00000003 |
35 | #define CRYPTO_ALG_TYPE_COMPRESS 0x00000004 | 35 | #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004 |
36 | #define CRYPTO_ALG_TYPE_COMPRESS 0x00000005 | ||
37 | |||
38 | #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e | ||
36 | 39 | ||
37 | #define CRYPTO_ALG_LARVAL 0x00000010 | 40 | #define CRYPTO_ALG_LARVAL 0x00000010 |
38 | #define CRYPTO_ALG_DEAD 0x00000020 | 41 | #define CRYPTO_ALG_DEAD 0x00000020 |
@@ -90,6 +93,7 @@ | |||
90 | 93 | ||
91 | struct scatterlist; | 94 | struct scatterlist; |
92 | struct crypto_blkcipher; | 95 | struct crypto_blkcipher; |
96 | struct crypto_hash; | ||
93 | struct crypto_tfm; | 97 | struct crypto_tfm; |
94 | struct crypto_type; | 98 | struct crypto_type; |
95 | 99 | ||
@@ -107,6 +111,11 @@ struct cipher_desc { | |||
107 | void *info; | 111 | void *info; |
108 | }; | 112 | }; |
109 | 113 | ||
114 | struct hash_desc { | ||
115 | struct crypto_hash *tfm; | ||
116 | u32 flags; | ||
117 | }; | ||
118 | |||
110 | /* | 119 | /* |
111 | * Algorithms: modular crypto algorithm implementations, managed | 120 | * Algorithms: modular crypto algorithm implementations, managed |
112 | * via crypto_register_alg() and crypto_unregister_alg(). | 121 | * via crypto_register_alg() and crypto_unregister_alg(). |
@@ -158,6 +167,19 @@ struct digest_alg { | |||
158 | unsigned int keylen); | 167 | unsigned int keylen); |
159 | }; | 168 | }; |
160 | 169 | ||
170 | struct hash_alg { | ||
171 | int (*init)(struct hash_desc *desc); | ||
172 | int (*update)(struct hash_desc *desc, struct scatterlist *sg, | ||
173 | unsigned int nbytes); | ||
174 | int (*final)(struct hash_desc *desc, u8 *out); | ||
175 | int (*digest)(struct hash_desc *desc, struct scatterlist *sg, | ||
176 | unsigned int nbytes, u8 *out); | ||
177 | int (*setkey)(struct crypto_hash *tfm, const u8 *key, | ||
178 | unsigned int keylen); | ||
179 | |||
180 | unsigned int digestsize; | ||
181 | }; | ||
182 | |||
161 | struct compress_alg { | 183 | struct compress_alg { |
162 | int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src, | 184 | int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src, |
163 | unsigned int slen, u8 *dst, unsigned int *dlen); | 185 | unsigned int slen, u8 *dst, unsigned int *dlen); |
@@ -168,6 +190,7 @@ struct compress_alg { | |||
168 | #define cra_blkcipher cra_u.blkcipher | 190 | #define cra_blkcipher cra_u.blkcipher |
169 | #define cra_cipher cra_u.cipher | 191 | #define cra_cipher cra_u.cipher |
170 | #define cra_digest cra_u.digest | 192 | #define cra_digest cra_u.digest |
193 | #define cra_hash cra_u.hash | ||
171 | #define cra_compress cra_u.compress | 194 | #define cra_compress cra_u.compress |
172 | 195 | ||
173 | struct crypto_alg { | 196 | struct crypto_alg { |
@@ -191,6 +214,7 @@ struct crypto_alg { | |||
191 | struct blkcipher_alg blkcipher; | 214 | struct blkcipher_alg blkcipher; |
192 | struct cipher_alg cipher; | 215 | struct cipher_alg cipher; |
193 | struct digest_alg digest; | 216 | struct digest_alg digest; |
217 | struct hash_alg hash; | ||
194 | struct compress_alg compress; | 218 | struct compress_alg compress; |
195 | } cra_u; | 219 | } cra_u; |
196 | 220 | ||
@@ -262,18 +286,19 @@ struct cipher_tfm { | |||
262 | void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); | 286 | void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); |
263 | }; | 287 | }; |
264 | 288 | ||
265 | struct digest_tfm { | 289 | struct hash_tfm { |
266 | void (*dit_init)(struct crypto_tfm *tfm); | 290 | int (*init)(struct hash_desc *desc); |
267 | void (*dit_update)(struct crypto_tfm *tfm, | 291 | int (*update)(struct hash_desc *desc, |
268 | struct scatterlist *sg, unsigned int nsg); | 292 | struct scatterlist *sg, unsigned int nsg); |
269 | void (*dit_final)(struct crypto_tfm *tfm, u8 *out); | 293 | int (*final)(struct hash_desc *desc, u8 *out); |
270 | void (*dit_digest)(struct crypto_tfm *tfm, struct scatterlist *sg, | 294 | int (*digest)(struct hash_desc *desc, struct scatterlist *sg, |
271 | unsigned int nsg, u8 *out); | 295 | unsigned int nsg, u8 *out); |
272 | int (*dit_setkey)(struct crypto_tfm *tfm, | 296 | int (*setkey)(struct crypto_hash *tfm, const u8 *key, |
273 | const u8 *key, unsigned int keylen); | 297 | unsigned int keylen); |
274 | #ifdef CONFIG_CRYPTO_HMAC | 298 | #ifdef CONFIG_CRYPTO_HMAC |
275 | void *dit_hmac_block; | 299 | void *hmac_block; |
276 | #endif | 300 | #endif |
301 | unsigned int digestsize; | ||
277 | }; | 302 | }; |
278 | 303 | ||
279 | struct compress_tfm { | 304 | struct compress_tfm { |
@@ -287,7 +312,7 @@ struct compress_tfm { | |||
287 | 312 | ||
288 | #define crt_blkcipher crt_u.blkcipher | 313 | #define crt_blkcipher crt_u.blkcipher |
289 | #define crt_cipher crt_u.cipher | 314 | #define crt_cipher crt_u.cipher |
290 | #define crt_digest crt_u.digest | 315 | #define crt_hash crt_u.hash |
291 | #define crt_compress crt_u.compress | 316 | #define crt_compress crt_u.compress |
292 | 317 | ||
293 | struct crypto_tfm { | 318 | struct crypto_tfm { |
@@ -297,7 +322,7 @@ struct crypto_tfm { | |||
297 | union { | 322 | union { |
298 | struct blkcipher_tfm blkcipher; | 323 | struct blkcipher_tfm blkcipher; |
299 | struct cipher_tfm cipher; | 324 | struct cipher_tfm cipher; |
300 | struct digest_tfm digest; | 325 | struct hash_tfm hash; |
301 | struct compress_tfm compress; | 326 | struct compress_tfm compress; |
302 | } crt_u; | 327 | } crt_u; |
303 | 328 | ||
@@ -312,6 +337,10 @@ struct crypto_blkcipher { | |||
312 | struct crypto_tfm base; | 337 | struct crypto_tfm base; |
313 | }; | 338 | }; |
314 | 339 | ||
340 | struct crypto_hash { | ||
341 | struct crypto_tfm base; | ||
342 | }; | ||
343 | |||
315 | enum { | 344 | enum { |
316 | CRYPTOA_UNSPEC, | 345 | CRYPTOA_UNSPEC, |
317 | CRYPTOA_ALG, | 346 | CRYPTOA_ALG, |
@@ -647,39 +676,114 @@ static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm, | |||
647 | dst, src); | 676 | dst, src); |
648 | } | 677 | } |
649 | 678 | ||
650 | static inline void crypto_digest_init(struct crypto_tfm *tfm) | 679 | void crypto_digest_init(struct crypto_tfm *tfm); |
680 | void crypto_digest_update(struct crypto_tfm *tfm, | ||
681 | struct scatterlist *sg, unsigned int nsg); | ||
682 | void crypto_digest_final(struct crypto_tfm *tfm, u8 *out); | ||
683 | void crypto_digest_digest(struct crypto_tfm *tfm, | ||
684 | struct scatterlist *sg, unsigned int nsg, u8 *out); | ||
685 | |||
686 | static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm) | ||
651 | { | 687 | { |
652 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST); | 688 | return (struct crypto_hash *)tfm; |
653 | tfm->crt_digest.dit_init(tfm); | ||
654 | } | 689 | } |
655 | 690 | ||
656 | static inline void crypto_digest_update(struct crypto_tfm *tfm, | 691 | static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm) |
657 | struct scatterlist *sg, | ||
658 | unsigned int nsg) | ||
659 | { | 692 | { |
660 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST); | 693 | BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) & |
661 | tfm->crt_digest.dit_update(tfm, sg, nsg); | 694 | CRYPTO_ALG_TYPE_HASH_MASK); |
695 | return __crypto_hash_cast(tfm); | ||
662 | } | 696 | } |
663 | 697 | ||
664 | static inline void crypto_digest_final(struct crypto_tfm *tfm, u8 *out) | 698 | static inline int crypto_digest_setkey(struct crypto_tfm *tfm, |
699 | const u8 *key, unsigned int keylen) | ||
665 | { | 700 | { |
666 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST); | 701 | return tfm->crt_hash.setkey(crypto_hash_cast(tfm), key, keylen); |
667 | tfm->crt_digest.dit_final(tfm, out); | ||
668 | } | 702 | } |
669 | 703 | ||
670 | static inline void crypto_digest_digest(struct crypto_tfm *tfm, | 704 | static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name, |
671 | struct scatterlist *sg, | 705 | u32 type, u32 mask) |
672 | unsigned int nsg, u8 *out) | ||
673 | { | 706 | { |
674 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST); | 707 | type &= ~CRYPTO_ALG_TYPE_MASK; |
675 | tfm->crt_digest.dit_digest(tfm, sg, nsg, out); | 708 | type |= CRYPTO_ALG_TYPE_HASH; |
709 | mask |= CRYPTO_ALG_TYPE_HASH_MASK; | ||
710 | |||
711 | return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask)); | ||
676 | } | 712 | } |
677 | 713 | ||
678 | static inline int crypto_digest_setkey(struct crypto_tfm *tfm, | 714 | static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm) |
679 | const u8 *key, unsigned int keylen) | ||
680 | { | 715 | { |
681 | BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST); | 716 | return &tfm->base; |
682 | return tfm->crt_digest.dit_setkey(tfm, key, keylen); | 717 | } |
718 | |||
719 | static inline void crypto_free_hash(struct crypto_hash *tfm) | ||
720 | { | ||
721 | crypto_free_tfm(crypto_hash_tfm(tfm)); | ||
722 | } | ||
723 | |||
724 | static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm) | ||
725 | { | ||
726 | return &crypto_hash_tfm(tfm)->crt_hash; | ||
727 | } | ||
728 | |||
729 | static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm) | ||
730 | { | ||
731 | return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm)); | ||
732 | } | ||
733 | |||
734 | static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm) | ||
735 | { | ||
736 | return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm)); | ||
737 | } | ||
738 | |||
739 | static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm) | ||
740 | { | ||
741 | return crypto_hash_crt(tfm)->digestsize; | ||
742 | } | ||
743 | |||
744 | static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm) | ||
745 | { | ||
746 | return crypto_tfm_get_flags(crypto_hash_tfm(tfm)); | ||
747 | } | ||
748 | |||
749 | static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags) | ||
750 | { | ||
751 | crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags); | ||
752 | } | ||
753 | |||
754 | static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags) | ||
755 | { | ||
756 | crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags); | ||
757 | } | ||
758 | |||
759 | static inline int crypto_hash_init(struct hash_desc *desc) | ||
760 | { | ||
761 | return crypto_hash_crt(desc->tfm)->init(desc); | ||
762 | } | ||
763 | |||
764 | static inline int crypto_hash_update(struct hash_desc *desc, | ||
765 | struct scatterlist *sg, | ||
766 | unsigned int nbytes) | ||
767 | { | ||
768 | return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes); | ||
769 | } | ||
770 | |||
771 | static inline int crypto_hash_final(struct hash_desc *desc, u8 *out) | ||
772 | { | ||
773 | return crypto_hash_crt(desc->tfm)->final(desc, out); | ||
774 | } | ||
775 | |||
776 | static inline int crypto_hash_digest(struct hash_desc *desc, | ||
777 | struct scatterlist *sg, | ||
778 | unsigned int nbytes, u8 *out) | ||
779 | { | ||
780 | return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out); | ||
781 | } | ||
782 | |||
783 | static inline int crypto_hash_setkey(struct crypto_hash *hash, | ||
784 | const u8 *key, unsigned int keylen) | ||
785 | { | ||
786 | return crypto_hash_crt(hash)->setkey(hash, key, keylen); | ||
683 | } | 787 | } |
684 | 788 | ||
685 | static int crypto_cipher_encrypt(struct crypto_tfm *tfm, | 789 | static int crypto_cipher_encrypt(struct crypto_tfm *tfm, |