diff options
author | Herbert Xu <herbert@gondor.apana.org.au> | 2008-08-31 01:47:27 -0400 |
---|---|---|
committer | Herbert Xu <herbert@gondor.apana.org.au> | 2008-12-24 19:01:26 -0500 |
commit | 7b5a080b3c46f0cac71c0d0262634c6517d4ee4f (patch) | |
tree | 41ba9e7051d1ecd47eb5cd371209229db3202ab6 /include | |
parent | 7b0bac64cd5b74d6f1147524c26216de13a501fd (diff) |
crypto: hash - Add shash interface
The shash interface replaces the current synchronous hash interface.
It improves over hash in two ways. Firstly shash is reentrant,
meaning that the same tfm may be used by two threads simultaneously
as all hashing state is stored in a local descriptor.
The other enhancement is that shash no longer takes scatter list
entries. This is because shash is specifically designed for
synchronous algorithms and as such scatter lists are unnecessary.
All existing hash users will be converted to shash once the
algorithms have been completely converted.
There is also a new finup function that combines update with final.
This will be extended to ahash once the algorithm conversion is
done.
This is also the first time that an algorithm type has their own
registration function. Existing algorithm types will be converted
to this way in due course.
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'include')
-rw-r--r-- | include/crypto/hash.h | 104 | ||||
-rw-r--r-- | include/crypto/internal/hash.h | 8 | ||||
-rw-r--r-- | include/linux/crypto.h | 1 |
3 files changed, 113 insertions, 0 deletions
diff --git a/include/crypto/hash.h b/include/crypto/hash.h index ee48ef8fb2ea..f9b51d408953 100644 --- a/include/crypto/hash.h +++ b/include/crypto/hash.h | |||
@@ -15,10 +15,39 @@ | |||
15 | 15 | ||
16 | #include <linux/crypto.h> | 16 | #include <linux/crypto.h> |
17 | 17 | ||
18 | struct shash_desc { | ||
19 | struct crypto_shash *tfm; | ||
20 | u32 flags; | ||
21 | |||
22 | void *__ctx[] CRYPTO_MINALIGN_ATTR; | ||
23 | }; | ||
24 | |||
25 | struct shash_alg { | ||
26 | int (*init)(struct shash_desc *desc); | ||
27 | int (*update)(struct shash_desc *desc, const u8 *data, | ||
28 | unsigned int len); | ||
29 | int (*final)(struct shash_desc *desc, u8 *out); | ||
30 | int (*finup)(struct shash_desc *desc, const u8 *data, | ||
31 | unsigned int len, u8 *out); | ||
32 | int (*digest)(struct shash_desc *desc, const u8 *data, | ||
33 | unsigned int len, u8 *out); | ||
34 | int (*setkey)(struct crypto_shash *tfm, const u8 *key, | ||
35 | unsigned int keylen); | ||
36 | |||
37 | unsigned int descsize; | ||
38 | unsigned int digestsize; | ||
39 | |||
40 | struct crypto_alg base; | ||
41 | }; | ||
42 | |||
18 | struct crypto_ahash { | 43 | struct crypto_ahash { |
19 | struct crypto_tfm base; | 44 | struct crypto_tfm base; |
20 | }; | 45 | }; |
21 | 46 | ||
47 | struct crypto_shash { | ||
48 | struct crypto_tfm base; | ||
49 | }; | ||
50 | |||
22 | static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm) | 51 | static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm) |
23 | { | 52 | { |
24 | return (struct crypto_ahash *)tfm; | 53 | return (struct crypto_ahash *)tfm; |
@@ -169,4 +198,79 @@ static inline void ahash_request_set_crypt(struct ahash_request *req, | |||
169 | req->result = result; | 198 | req->result = result; |
170 | } | 199 | } |
171 | 200 | ||
201 | struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type, | ||
202 | u32 mask); | ||
203 | |||
204 | static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm) | ||
205 | { | ||
206 | return &tfm->base; | ||
207 | } | ||
208 | |||
209 | static inline void crypto_free_shash(struct crypto_shash *tfm) | ||
210 | { | ||
211 | crypto_free_tfm(crypto_shash_tfm(tfm)); | ||
212 | } | ||
213 | |||
214 | static inline unsigned int crypto_shash_alignmask( | ||
215 | struct crypto_shash *tfm) | ||
216 | { | ||
217 | return crypto_tfm_alg_alignmask(crypto_shash_tfm(tfm)); | ||
218 | } | ||
219 | |||
220 | static inline struct shash_alg *__crypto_shash_alg(struct crypto_alg *alg) | ||
221 | { | ||
222 | return container_of(alg, struct shash_alg, base); | ||
223 | } | ||
224 | |||
225 | static inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm) | ||
226 | { | ||
227 | return __crypto_shash_alg(crypto_shash_tfm(tfm)->__crt_alg); | ||
228 | } | ||
229 | |||
230 | static inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm) | ||
231 | { | ||
232 | return crypto_shash_alg(tfm)->digestsize; | ||
233 | } | ||
234 | |||
235 | static inline u32 crypto_shash_get_flags(struct crypto_shash *tfm) | ||
236 | { | ||
237 | return crypto_tfm_get_flags(crypto_shash_tfm(tfm)); | ||
238 | } | ||
239 | |||
240 | static inline void crypto_shash_set_flags(struct crypto_shash *tfm, u32 flags) | ||
241 | { | ||
242 | crypto_tfm_set_flags(crypto_shash_tfm(tfm), flags); | ||
243 | } | ||
244 | |||
245 | static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags) | ||
246 | { | ||
247 | crypto_tfm_clear_flags(crypto_shash_tfm(tfm), flags); | ||
248 | } | ||
249 | |||
250 | static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm) | ||
251 | { | ||
252 | return crypto_shash_alg(tfm)->descsize; | ||
253 | } | ||
254 | |||
255 | static inline void *shash_desc_ctx(struct shash_desc *desc) | ||
256 | { | ||
257 | return desc->__ctx; | ||
258 | } | ||
259 | |||
260 | int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key, | ||
261 | unsigned int keylen); | ||
262 | int crypto_shash_digest(struct shash_desc *desc, const u8 *data, | ||
263 | unsigned int len, u8 *out); | ||
264 | |||
265 | static inline int crypto_shash_init(struct shash_desc *desc) | ||
266 | { | ||
267 | return crypto_shash_alg(desc->tfm)->init(desc); | ||
268 | } | ||
269 | |||
270 | int crypto_shash_update(struct shash_desc *desc, const u8 *data, | ||
271 | unsigned int len); | ||
272 | int crypto_shash_final(struct shash_desc *desc, u8 *out); | ||
273 | int crypto_shash_finup(struct shash_desc *desc, const u8 *data, | ||
274 | unsigned int len, u8 *out); | ||
275 | |||
172 | #endif /* _CRYPTO_HASH_H */ | 276 | #endif /* _CRYPTO_HASH_H */ |
diff --git a/include/crypto/internal/hash.h b/include/crypto/internal/hash.h index 917ae57bad4a..32d3a8ed06de 100644 --- a/include/crypto/internal/hash.h +++ b/include/crypto/internal/hash.h | |||
@@ -40,6 +40,9 @@ int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err); | |||
40 | int crypto_hash_walk_first(struct ahash_request *req, | 40 | int crypto_hash_walk_first(struct ahash_request *req, |
41 | struct crypto_hash_walk *walk); | 41 | struct crypto_hash_walk *walk); |
42 | 42 | ||
43 | int crypto_register_shash(struct shash_alg *alg); | ||
44 | int crypto_unregister_shash(struct shash_alg *alg); | ||
45 | |||
43 | static inline void *crypto_ahash_ctx(struct crypto_ahash *tfm) | 46 | static inline void *crypto_ahash_ctx(struct crypto_ahash *tfm) |
44 | { | 47 | { |
45 | return crypto_tfm_ctx(&tfm->base); | 48 | return crypto_tfm_ctx(&tfm->base); |
@@ -74,5 +77,10 @@ static inline int ahash_tfm_in_queue(struct crypto_queue *queue, | |||
74 | return crypto_tfm_in_queue(queue, crypto_ahash_tfm(tfm)); | 77 | return crypto_tfm_in_queue(queue, crypto_ahash_tfm(tfm)); |
75 | } | 78 | } |
76 | 79 | ||
80 | static inline void *crypto_shash_ctx(struct crypto_shash *tfm) | ||
81 | { | ||
82 | return crypto_tfm_ctx(&tfm->base); | ||
83 | } | ||
84 | |||
77 | #endif /* _CRYPTO_INTERNAL_HASH_H */ | 85 | #endif /* _CRYPTO_INTERNAL_HASH_H */ |
78 | 86 | ||
diff --git a/include/linux/crypto.h b/include/linux/crypto.h index ffaaa418cf59..ee95c748695c 100644 --- a/include/linux/crypto.h +++ b/include/linux/crypto.h | |||
@@ -39,6 +39,7 @@ | |||
39 | #define CRYPTO_ALG_TYPE_HASH 0x00000009 | 39 | #define CRYPTO_ALG_TYPE_HASH 0x00000009 |
40 | #define CRYPTO_ALG_TYPE_AHASH 0x0000000a | 40 | #define CRYPTO_ALG_TYPE_AHASH 0x0000000a |
41 | #define CRYPTO_ALG_TYPE_RNG 0x0000000c | 41 | #define CRYPTO_ALG_TYPE_RNG 0x0000000c |
42 | #define CRYPTO_ALG_TYPE_SHASH 0x0000000d | ||
42 | 43 | ||
43 | #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e | 44 | #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e |
44 | #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c | 45 | #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c |