diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2009-09-11 12:38:37 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2009-09-11 12:38:37 -0400 |
commit | 332a3392188e0ad966543c87b8da2b9d246f301d (patch) | |
tree | ac0d570590bffdd1924426adc5b255857d2f3297 /crypto/shash.c | |
parent | a9c86d42599519f3d83b5f46bdab25046fe47b84 (diff) | |
parent | 81bd5f6c966cf2f137c2759dfc78abdffcff055e (diff) |
Merge git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
* git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6: (102 commits)
crypto: sha-s390 - Fix warnings in import function
crypto: vmac - New hash algorithm for intel_txt support
crypto: api - Do not displace newly registered algorithms
crypto: ansi_cprng - Fix module initialization
crypto: xcbc - Fix alignment calculation of xcbc_tfm_ctx
crypto: fips - Depend on ansi_cprng
crypto: blkcipher - Do not use eseqiv on stream ciphers
crypto: ctr - Use chainiv on raw counter mode
Revert crypto: fips - Select CPRNG
crypto: rng - Fix typo
crypto: talitos - add support for 36 bit addressing
crypto: talitos - align locks on cache lines
crypto: talitos - simplify hmac data size calculation
crypto: mv_cesa - Add support for Orion5X crypto engine
crypto: cryptd - Add support to access underlaying shash
crypto: gcm - Use GHASH digest algorithm
crypto: ghash - Add GHASH digest algorithm for GCM
crypto: authenc - Convert to ahash
crypto: api - Fix aligned ctx helper
crypto: hmac - Prehash ipad/opad
...
Diffstat (limited to 'crypto/shash.c')
-rw-r--r-- | crypto/shash.c | 270 |
1 files changed, 201 insertions, 69 deletions
diff --git a/crypto/shash.c b/crypto/shash.c index 2ccc8b0076ce..91f7b9d83881 100644 --- a/crypto/shash.c +++ b/crypto/shash.c | |||
@@ -22,6 +22,12 @@ | |||
22 | 22 | ||
23 | static const struct crypto_type crypto_shash_type; | 23 | static const struct crypto_type crypto_shash_type; |
24 | 24 | ||
25 | static int shash_no_setkey(struct crypto_shash *tfm, const u8 *key, | ||
26 | unsigned int keylen) | ||
27 | { | ||
28 | return -ENOSYS; | ||
29 | } | ||
30 | |||
25 | static int shash_setkey_unaligned(struct crypto_shash *tfm, const u8 *key, | 31 | static int shash_setkey_unaligned(struct crypto_shash *tfm, const u8 *key, |
26 | unsigned int keylen) | 32 | unsigned int keylen) |
27 | { | 33 | { |
@@ -39,8 +45,7 @@ static int shash_setkey_unaligned(struct crypto_shash *tfm, const u8 *key, | |||
39 | alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); | 45 | alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); |
40 | memcpy(alignbuffer, key, keylen); | 46 | memcpy(alignbuffer, key, keylen); |
41 | err = shash->setkey(tfm, alignbuffer, keylen); | 47 | err = shash->setkey(tfm, alignbuffer, keylen); |
42 | memset(alignbuffer, 0, keylen); | 48 | kzfree(buffer); |
43 | kfree(buffer); | ||
44 | return err; | 49 | return err; |
45 | } | 50 | } |
46 | 51 | ||
@@ -50,9 +55,6 @@ int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key, | |||
50 | struct shash_alg *shash = crypto_shash_alg(tfm); | 55 | struct shash_alg *shash = crypto_shash_alg(tfm); |
51 | unsigned long alignmask = crypto_shash_alignmask(tfm); | 56 | unsigned long alignmask = crypto_shash_alignmask(tfm); |
52 | 57 | ||
53 | if (!shash->setkey) | ||
54 | return -ENOSYS; | ||
55 | |||
56 | if ((unsigned long)key & alignmask) | 58 | if ((unsigned long)key & alignmask) |
57 | return shash_setkey_unaligned(tfm, key, keylen); | 59 | return shash_setkey_unaligned(tfm, key, keylen); |
58 | 60 | ||
@@ -74,15 +76,19 @@ static int shash_update_unaligned(struct shash_desc *desc, const u8 *data, | |||
74 | unsigned long alignmask = crypto_shash_alignmask(tfm); | 76 | unsigned long alignmask = crypto_shash_alignmask(tfm); |
75 | unsigned int unaligned_len = alignmask + 1 - | 77 | unsigned int unaligned_len = alignmask + 1 - |
76 | ((unsigned long)data & alignmask); | 78 | ((unsigned long)data & alignmask); |
77 | u8 buf[shash_align_buffer_size(unaligned_len, alignmask)] | 79 | u8 ubuf[shash_align_buffer_size(unaligned_len, alignmask)] |
78 | __attribute__ ((aligned)); | 80 | __attribute__ ((aligned)); |
81 | u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1); | ||
82 | int err; | ||
79 | 83 | ||
80 | if (unaligned_len > len) | 84 | if (unaligned_len > len) |
81 | unaligned_len = len; | 85 | unaligned_len = len; |
82 | 86 | ||
83 | memcpy(buf, data, unaligned_len); | 87 | memcpy(buf, data, unaligned_len); |
88 | err = shash->update(desc, buf, unaligned_len); | ||
89 | memset(buf, 0, unaligned_len); | ||
84 | 90 | ||
85 | return shash->update(desc, buf, unaligned_len) ?: | 91 | return err ?: |
86 | shash->update(desc, data + unaligned_len, len - unaligned_len); | 92 | shash->update(desc, data + unaligned_len, len - unaligned_len); |
87 | } | 93 | } |
88 | 94 | ||
@@ -106,12 +112,19 @@ static int shash_final_unaligned(struct shash_desc *desc, u8 *out) | |||
106 | unsigned long alignmask = crypto_shash_alignmask(tfm); | 112 | unsigned long alignmask = crypto_shash_alignmask(tfm); |
107 | struct shash_alg *shash = crypto_shash_alg(tfm); | 113 | struct shash_alg *shash = crypto_shash_alg(tfm); |
108 | unsigned int ds = crypto_shash_digestsize(tfm); | 114 | unsigned int ds = crypto_shash_digestsize(tfm); |
109 | u8 buf[shash_align_buffer_size(ds, alignmask)] | 115 | u8 ubuf[shash_align_buffer_size(ds, alignmask)] |
110 | __attribute__ ((aligned)); | 116 | __attribute__ ((aligned)); |
117 | u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1); | ||
111 | int err; | 118 | int err; |
112 | 119 | ||
113 | err = shash->final(desc, buf); | 120 | err = shash->final(desc, buf); |
121 | if (err) | ||
122 | goto out; | ||
123 | |||
114 | memcpy(out, buf, ds); | 124 | memcpy(out, buf, ds); |
125 | |||
126 | out: | ||
127 | memset(buf, 0, ds); | ||
115 | return err; | 128 | return err; |
116 | } | 129 | } |
117 | 130 | ||
@@ -142,8 +155,7 @@ int crypto_shash_finup(struct shash_desc *desc, const u8 *data, | |||
142 | struct shash_alg *shash = crypto_shash_alg(tfm); | 155 | struct shash_alg *shash = crypto_shash_alg(tfm); |
143 | unsigned long alignmask = crypto_shash_alignmask(tfm); | 156 | unsigned long alignmask = crypto_shash_alignmask(tfm); |
144 | 157 | ||
145 | if (((unsigned long)data | (unsigned long)out) & alignmask || | 158 | if (((unsigned long)data | (unsigned long)out) & alignmask) |
146 | !shash->finup) | ||
147 | return shash_finup_unaligned(desc, data, len, out); | 159 | return shash_finup_unaligned(desc, data, len, out); |
148 | 160 | ||
149 | return shash->finup(desc, data, len, out); | 161 | return shash->finup(desc, data, len, out); |
@@ -154,8 +166,7 @@ static int shash_digest_unaligned(struct shash_desc *desc, const u8 *data, | |||
154 | unsigned int len, u8 *out) | 166 | unsigned int len, u8 *out) |
155 | { | 167 | { |
156 | return crypto_shash_init(desc) ?: | 168 | return crypto_shash_init(desc) ?: |
157 | crypto_shash_update(desc, data, len) ?: | 169 | crypto_shash_finup(desc, data, len, out); |
158 | crypto_shash_final(desc, out); | ||
159 | } | 170 | } |
160 | 171 | ||
161 | int crypto_shash_digest(struct shash_desc *desc, const u8 *data, | 172 | int crypto_shash_digest(struct shash_desc *desc, const u8 *data, |
@@ -165,27 +176,24 @@ int crypto_shash_digest(struct shash_desc *desc, const u8 *data, | |||
165 | struct shash_alg *shash = crypto_shash_alg(tfm); | 176 | struct shash_alg *shash = crypto_shash_alg(tfm); |
166 | unsigned long alignmask = crypto_shash_alignmask(tfm); | 177 | unsigned long alignmask = crypto_shash_alignmask(tfm); |
167 | 178 | ||
168 | if (((unsigned long)data | (unsigned long)out) & alignmask || | 179 | if (((unsigned long)data | (unsigned long)out) & alignmask) |
169 | !shash->digest) | ||
170 | return shash_digest_unaligned(desc, data, len, out); | 180 | return shash_digest_unaligned(desc, data, len, out); |
171 | 181 | ||
172 | return shash->digest(desc, data, len, out); | 182 | return shash->digest(desc, data, len, out); |
173 | } | 183 | } |
174 | EXPORT_SYMBOL_GPL(crypto_shash_digest); | 184 | EXPORT_SYMBOL_GPL(crypto_shash_digest); |
175 | 185 | ||
176 | int crypto_shash_import(struct shash_desc *desc, const u8 *in) | 186 | static int shash_default_export(struct shash_desc *desc, void *out) |
177 | { | 187 | { |
178 | struct crypto_shash *tfm = desc->tfm; | 188 | memcpy(out, shash_desc_ctx(desc), crypto_shash_descsize(desc->tfm)); |
179 | struct shash_alg *alg = crypto_shash_alg(tfm); | 189 | return 0; |
180 | 190 | } | |
181 | memcpy(shash_desc_ctx(desc), in, crypto_shash_descsize(tfm)); | ||
182 | |||
183 | if (alg->reinit) | ||
184 | alg->reinit(desc); | ||
185 | 191 | ||
192 | static int shash_default_import(struct shash_desc *desc, const void *in) | ||
193 | { | ||
194 | memcpy(shash_desc_ctx(desc), in, crypto_shash_descsize(desc->tfm)); | ||
186 | return 0; | 195 | return 0; |
187 | } | 196 | } |
188 | EXPORT_SYMBOL_GPL(crypto_shash_import); | ||
189 | 197 | ||
190 | static int shash_async_setkey(struct crypto_ahash *tfm, const u8 *key, | 198 | static int shash_async_setkey(struct crypto_ahash *tfm, const u8 *key, |
191 | unsigned int keylen) | 199 | unsigned int keylen) |
@@ -206,9 +214,8 @@ static int shash_async_init(struct ahash_request *req) | |||
206 | return crypto_shash_init(desc); | 214 | return crypto_shash_init(desc); |
207 | } | 215 | } |
208 | 216 | ||
209 | static int shash_async_update(struct ahash_request *req) | 217 | int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc) |
210 | { | 218 | { |
211 | struct shash_desc *desc = ahash_request_ctx(req); | ||
212 | struct crypto_hash_walk walk; | 219 | struct crypto_hash_walk walk; |
213 | int nbytes; | 220 | int nbytes; |
214 | 221 | ||
@@ -218,13 +225,51 @@ static int shash_async_update(struct ahash_request *req) | |||
218 | 225 | ||
219 | return nbytes; | 226 | return nbytes; |
220 | } | 227 | } |
228 | EXPORT_SYMBOL_GPL(shash_ahash_update); | ||
229 | |||
230 | static int shash_async_update(struct ahash_request *req) | ||
231 | { | ||
232 | return shash_ahash_update(req, ahash_request_ctx(req)); | ||
233 | } | ||
221 | 234 | ||
222 | static int shash_async_final(struct ahash_request *req) | 235 | static int shash_async_final(struct ahash_request *req) |
223 | { | 236 | { |
224 | return crypto_shash_final(ahash_request_ctx(req), req->result); | 237 | return crypto_shash_final(ahash_request_ctx(req), req->result); |
225 | } | 238 | } |
226 | 239 | ||
227 | static int shash_async_digest(struct ahash_request *req) | 240 | int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc) |
241 | { | ||
242 | struct crypto_hash_walk walk; | ||
243 | int nbytes; | ||
244 | |||
245 | nbytes = crypto_hash_walk_first(req, &walk); | ||
246 | if (!nbytes) | ||
247 | return crypto_shash_final(desc, req->result); | ||
248 | |||
249 | do { | ||
250 | nbytes = crypto_hash_walk_last(&walk) ? | ||
251 | crypto_shash_finup(desc, walk.data, nbytes, | ||
252 | req->result) : | ||
253 | crypto_shash_update(desc, walk.data, nbytes); | ||
254 | nbytes = crypto_hash_walk_done(&walk, nbytes); | ||
255 | } while (nbytes > 0); | ||
256 | |||
257 | return nbytes; | ||
258 | } | ||
259 | EXPORT_SYMBOL_GPL(shash_ahash_finup); | ||
260 | |||
261 | static int shash_async_finup(struct ahash_request *req) | ||
262 | { | ||
263 | struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req)); | ||
264 | struct shash_desc *desc = ahash_request_ctx(req); | ||
265 | |||
266 | desc->tfm = *ctx; | ||
267 | desc->flags = req->base.flags; | ||
268 | |||
269 | return shash_ahash_finup(req, desc); | ||
270 | } | ||
271 | |||
272 | int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc) | ||
228 | { | 273 | { |
229 | struct scatterlist *sg = req->src; | 274 | struct scatterlist *sg = req->src; |
230 | unsigned int offset = sg->offset; | 275 | unsigned int offset = sg->offset; |
@@ -232,34 +277,40 @@ static int shash_async_digest(struct ahash_request *req) | |||
232 | int err; | 277 | int err; |
233 | 278 | ||
234 | if (nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset)) { | 279 | if (nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset)) { |
235 | struct crypto_shash **ctx = | ||
236 | crypto_ahash_ctx(crypto_ahash_reqtfm(req)); | ||
237 | struct shash_desc *desc = ahash_request_ctx(req); | ||
238 | void *data; | 280 | void *data; |
239 | 281 | ||
240 | desc->tfm = *ctx; | ||
241 | desc->flags = req->base.flags; | ||
242 | |||
243 | data = crypto_kmap(sg_page(sg), 0); | 282 | data = crypto_kmap(sg_page(sg), 0); |
244 | err = crypto_shash_digest(desc, data + offset, nbytes, | 283 | err = crypto_shash_digest(desc, data + offset, nbytes, |
245 | req->result); | 284 | req->result); |
246 | crypto_kunmap(data, 0); | 285 | crypto_kunmap(data, 0); |
247 | crypto_yield(desc->flags); | 286 | crypto_yield(desc->flags); |
248 | goto out; | 287 | } else |
249 | } | 288 | err = crypto_shash_init(desc) ?: |
289 | shash_ahash_finup(req, desc); | ||
250 | 290 | ||
251 | err = shash_async_init(req); | 291 | return err; |
252 | if (err) | 292 | } |
253 | goto out; | 293 | EXPORT_SYMBOL_GPL(shash_ahash_digest); |
254 | 294 | ||
255 | err = shash_async_update(req); | 295 | static int shash_async_digest(struct ahash_request *req) |
256 | if (err) | 296 | { |
257 | goto out; | 297 | struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req)); |
298 | struct shash_desc *desc = ahash_request_ctx(req); | ||
258 | 299 | ||
259 | err = shash_async_final(req); | 300 | desc->tfm = *ctx; |
301 | desc->flags = req->base.flags; | ||
260 | 302 | ||
261 | out: | 303 | return shash_ahash_digest(req, desc); |
262 | return err; | 304 | } |
305 | |||
306 | static int shash_async_export(struct ahash_request *req, void *out) | ||
307 | { | ||
308 | return crypto_shash_export(ahash_request_ctx(req), out); | ||
309 | } | ||
310 | |||
311 | static int shash_async_import(struct ahash_request *req, const void *in) | ||
312 | { | ||
313 | return crypto_shash_import(ahash_request_ctx(req), in); | ||
263 | } | 314 | } |
264 | 315 | ||
265 | static void crypto_exit_shash_ops_async(struct crypto_tfm *tfm) | 316 | static void crypto_exit_shash_ops_async(struct crypto_tfm *tfm) |
@@ -269,11 +320,11 @@ static void crypto_exit_shash_ops_async(struct crypto_tfm *tfm) | |||
269 | crypto_free_shash(*ctx); | 320 | crypto_free_shash(*ctx); |
270 | } | 321 | } |
271 | 322 | ||
272 | static int crypto_init_shash_ops_async(struct crypto_tfm *tfm) | 323 | int crypto_init_shash_ops_async(struct crypto_tfm *tfm) |
273 | { | 324 | { |
274 | struct crypto_alg *calg = tfm->__crt_alg; | 325 | struct crypto_alg *calg = tfm->__crt_alg; |
275 | struct shash_alg *alg = __crypto_shash_alg(calg); | 326 | struct shash_alg *alg = __crypto_shash_alg(calg); |
276 | struct ahash_tfm *crt = &tfm->crt_ahash; | 327 | struct crypto_ahash *crt = __crypto_ahash_cast(tfm); |
277 | struct crypto_shash **ctx = crypto_tfm_ctx(tfm); | 328 | struct crypto_shash **ctx = crypto_tfm_ctx(tfm); |
278 | struct crypto_shash *shash; | 329 | struct crypto_shash *shash; |
279 | 330 | ||
@@ -291,11 +342,17 @@ static int crypto_init_shash_ops_async(struct crypto_tfm *tfm) | |||
291 | 342 | ||
292 | crt->init = shash_async_init; | 343 | crt->init = shash_async_init; |
293 | crt->update = shash_async_update; | 344 | crt->update = shash_async_update; |
294 | crt->final = shash_async_final; | 345 | crt->final = shash_async_final; |
346 | crt->finup = shash_async_finup; | ||
295 | crt->digest = shash_async_digest; | 347 | crt->digest = shash_async_digest; |
296 | crt->setkey = shash_async_setkey; | ||
297 | 348 | ||
298 | crt->digestsize = alg->digestsize; | 349 | if (alg->setkey) |
350 | crt->setkey = shash_async_setkey; | ||
351 | if (alg->export) | ||
352 | crt->export = shash_async_export; | ||
353 | if (alg->import) | ||
354 | crt->import = shash_async_import; | ||
355 | |||
299 | crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash); | 356 | crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash); |
300 | 357 | ||
301 | return 0; | 358 | return 0; |
@@ -304,14 +361,16 @@ static int crypto_init_shash_ops_async(struct crypto_tfm *tfm) | |||
304 | static int shash_compat_setkey(struct crypto_hash *tfm, const u8 *key, | 361 | static int shash_compat_setkey(struct crypto_hash *tfm, const u8 *key, |
305 | unsigned int keylen) | 362 | unsigned int keylen) |
306 | { | 363 | { |
307 | struct shash_desc *desc = crypto_hash_ctx(tfm); | 364 | struct shash_desc **descp = crypto_hash_ctx(tfm); |
365 | struct shash_desc *desc = *descp; | ||
308 | 366 | ||
309 | return crypto_shash_setkey(desc->tfm, key, keylen); | 367 | return crypto_shash_setkey(desc->tfm, key, keylen); |
310 | } | 368 | } |
311 | 369 | ||
312 | static int shash_compat_init(struct hash_desc *hdesc) | 370 | static int shash_compat_init(struct hash_desc *hdesc) |
313 | { | 371 | { |
314 | struct shash_desc *desc = crypto_hash_ctx(hdesc->tfm); | 372 | struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm); |
373 | struct shash_desc *desc = *descp; | ||
315 | 374 | ||
316 | desc->flags = hdesc->flags; | 375 | desc->flags = hdesc->flags; |
317 | 376 | ||
@@ -321,7 +380,8 @@ static int shash_compat_init(struct hash_desc *hdesc) | |||
321 | static int shash_compat_update(struct hash_desc *hdesc, struct scatterlist *sg, | 380 | static int shash_compat_update(struct hash_desc *hdesc, struct scatterlist *sg, |
322 | unsigned int len) | 381 | unsigned int len) |
323 | { | 382 | { |
324 | struct shash_desc *desc = crypto_hash_ctx(hdesc->tfm); | 383 | struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm); |
384 | struct shash_desc *desc = *descp; | ||
325 | struct crypto_hash_walk walk; | 385 | struct crypto_hash_walk walk; |
326 | int nbytes; | 386 | int nbytes; |
327 | 387 | ||
@@ -334,7 +394,9 @@ static int shash_compat_update(struct hash_desc *hdesc, struct scatterlist *sg, | |||
334 | 394 | ||
335 | static int shash_compat_final(struct hash_desc *hdesc, u8 *out) | 395 | static int shash_compat_final(struct hash_desc *hdesc, u8 *out) |
336 | { | 396 | { |
337 | return crypto_shash_final(crypto_hash_ctx(hdesc->tfm), out); | 397 | struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm); |
398 | |||
399 | return crypto_shash_final(*descp, out); | ||
338 | } | 400 | } |
339 | 401 | ||
340 | static int shash_compat_digest(struct hash_desc *hdesc, struct scatterlist *sg, | 402 | static int shash_compat_digest(struct hash_desc *hdesc, struct scatterlist *sg, |
@@ -344,7 +406,8 @@ static int shash_compat_digest(struct hash_desc *hdesc, struct scatterlist *sg, | |||
344 | int err; | 406 | int err; |
345 | 407 | ||
346 | if (nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset)) { | 408 | if (nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset)) { |
347 | struct shash_desc *desc = crypto_hash_ctx(hdesc->tfm); | 409 | struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm); |
410 | struct shash_desc *desc = *descp; | ||
348 | void *data; | 411 | void *data; |
349 | 412 | ||
350 | desc->flags = hdesc->flags; | 413 | desc->flags = hdesc->flags; |
@@ -372,9 +435,11 @@ out: | |||
372 | 435 | ||
373 | static void crypto_exit_shash_ops_compat(struct crypto_tfm *tfm) | 436 | static void crypto_exit_shash_ops_compat(struct crypto_tfm *tfm) |
374 | { | 437 | { |
375 | struct shash_desc *desc= crypto_tfm_ctx(tfm); | 438 | struct shash_desc **descp = crypto_tfm_ctx(tfm); |
439 | struct shash_desc *desc = *descp; | ||
376 | 440 | ||
377 | crypto_free_shash(desc->tfm); | 441 | crypto_free_shash(desc->tfm); |
442 | kzfree(desc); | ||
378 | } | 443 | } |
379 | 444 | ||
380 | static int crypto_init_shash_ops_compat(struct crypto_tfm *tfm) | 445 | static int crypto_init_shash_ops_compat(struct crypto_tfm *tfm) |
@@ -382,8 +447,9 @@ static int crypto_init_shash_ops_compat(struct crypto_tfm *tfm) | |||
382 | struct hash_tfm *crt = &tfm->crt_hash; | 447 | struct hash_tfm *crt = &tfm->crt_hash; |
383 | struct crypto_alg *calg = tfm->__crt_alg; | 448 | struct crypto_alg *calg = tfm->__crt_alg; |
384 | struct shash_alg *alg = __crypto_shash_alg(calg); | 449 | struct shash_alg *alg = __crypto_shash_alg(calg); |
385 | struct shash_desc *desc = crypto_tfm_ctx(tfm); | 450 | struct shash_desc **descp = crypto_tfm_ctx(tfm); |
386 | struct crypto_shash *shash; | 451 | struct crypto_shash *shash; |
452 | struct shash_desc *desc; | ||
387 | 453 | ||
388 | if (!crypto_mod_get(calg)) | 454 | if (!crypto_mod_get(calg)) |
389 | return -EAGAIN; | 455 | return -EAGAIN; |
@@ -394,6 +460,14 @@ static int crypto_init_shash_ops_compat(struct crypto_tfm *tfm) | |||
394 | return PTR_ERR(shash); | 460 | return PTR_ERR(shash); |
395 | } | 461 | } |
396 | 462 | ||
463 | desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(shash), | ||
464 | GFP_KERNEL); | ||
465 | if (!desc) { | ||
466 | crypto_free_shash(shash); | ||
467 | return -ENOMEM; | ||
468 | } | ||
469 | |||
470 | *descp = desc; | ||
397 | desc->tfm = shash; | 471 | desc->tfm = shash; |
398 | tfm->exit = crypto_exit_shash_ops_compat; | 472 | tfm->exit = crypto_exit_shash_ops_compat; |
399 | 473 | ||
@@ -413,8 +487,6 @@ static int crypto_init_shash_ops(struct crypto_tfm *tfm, u32 type, u32 mask) | |||
413 | switch (mask & CRYPTO_ALG_TYPE_MASK) { | 487 | switch (mask & CRYPTO_ALG_TYPE_MASK) { |
414 | case CRYPTO_ALG_TYPE_HASH_MASK: | 488 | case CRYPTO_ALG_TYPE_HASH_MASK: |
415 | return crypto_init_shash_ops_compat(tfm); | 489 | return crypto_init_shash_ops_compat(tfm); |
416 | case CRYPTO_ALG_TYPE_AHASH_MASK: | ||
417 | return crypto_init_shash_ops_async(tfm); | ||
418 | } | 490 | } |
419 | 491 | ||
420 | return -EINVAL; | 492 | return -EINVAL; |
@@ -423,26 +495,23 @@ static int crypto_init_shash_ops(struct crypto_tfm *tfm, u32 type, u32 mask) | |||
423 | static unsigned int crypto_shash_ctxsize(struct crypto_alg *alg, u32 type, | 495 | static unsigned int crypto_shash_ctxsize(struct crypto_alg *alg, u32 type, |
424 | u32 mask) | 496 | u32 mask) |
425 | { | 497 | { |
426 | struct shash_alg *salg = __crypto_shash_alg(alg); | ||
427 | |||
428 | switch (mask & CRYPTO_ALG_TYPE_MASK) { | 498 | switch (mask & CRYPTO_ALG_TYPE_MASK) { |
429 | case CRYPTO_ALG_TYPE_HASH_MASK: | 499 | case CRYPTO_ALG_TYPE_HASH_MASK: |
430 | return sizeof(struct shash_desc) + salg->descsize; | 500 | return sizeof(struct shash_desc *); |
431 | case CRYPTO_ALG_TYPE_AHASH_MASK: | ||
432 | return sizeof(struct crypto_shash *); | ||
433 | } | 501 | } |
434 | 502 | ||
435 | return 0; | 503 | return 0; |
436 | } | 504 | } |
437 | 505 | ||
438 | static int crypto_shash_init_tfm(struct crypto_tfm *tfm, | 506 | static int crypto_shash_init_tfm(struct crypto_tfm *tfm) |
439 | const struct crypto_type *frontend) | ||
440 | { | 507 | { |
508 | struct crypto_shash *hash = __crypto_shash_cast(tfm); | ||
509 | |||
510 | hash->descsize = crypto_shash_alg(hash)->descsize; | ||
441 | return 0; | 511 | return 0; |
442 | } | 512 | } |
443 | 513 | ||
444 | static unsigned int crypto_shash_extsize(struct crypto_alg *alg, | 514 | static unsigned int crypto_shash_extsize(struct crypto_alg *alg) |
445 | const struct crypto_type *frontend) | ||
446 | { | 515 | { |
447 | return alg->cra_ctxsize; | 516 | return alg->cra_ctxsize; |
448 | } | 517 | } |
@@ -456,7 +525,6 @@ static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg) | |||
456 | seq_printf(m, "type : shash\n"); | 525 | seq_printf(m, "type : shash\n"); |
457 | seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); | 526 | seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); |
458 | seq_printf(m, "digestsize : %u\n", salg->digestsize); | 527 | seq_printf(m, "digestsize : %u\n", salg->digestsize); |
459 | seq_printf(m, "descsize : %u\n", salg->descsize); | ||
460 | } | 528 | } |
461 | 529 | ||
462 | static const struct crypto_type crypto_shash_type = { | 530 | static const struct crypto_type crypto_shash_type = { |
@@ -480,18 +548,43 @@ struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type, | |||
480 | } | 548 | } |
481 | EXPORT_SYMBOL_GPL(crypto_alloc_shash); | 549 | EXPORT_SYMBOL_GPL(crypto_alloc_shash); |
482 | 550 | ||
483 | int crypto_register_shash(struct shash_alg *alg) | 551 | static int shash_prepare_alg(struct shash_alg *alg) |
484 | { | 552 | { |
485 | struct crypto_alg *base = &alg->base; | 553 | struct crypto_alg *base = &alg->base; |
486 | 554 | ||
487 | if (alg->digestsize > PAGE_SIZE / 8 || | 555 | if (alg->digestsize > PAGE_SIZE / 8 || |
488 | alg->descsize > PAGE_SIZE / 8) | 556 | alg->descsize > PAGE_SIZE / 8 || |
557 | alg->statesize > PAGE_SIZE / 8) | ||
489 | return -EINVAL; | 558 | return -EINVAL; |
490 | 559 | ||
491 | base->cra_type = &crypto_shash_type; | 560 | base->cra_type = &crypto_shash_type; |
492 | base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK; | 561 | base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK; |
493 | base->cra_flags |= CRYPTO_ALG_TYPE_SHASH; | 562 | base->cra_flags |= CRYPTO_ALG_TYPE_SHASH; |
494 | 563 | ||
564 | if (!alg->finup) | ||
565 | alg->finup = shash_finup_unaligned; | ||
566 | if (!alg->digest) | ||
567 | alg->digest = shash_digest_unaligned; | ||
568 | if (!alg->export) { | ||
569 | alg->export = shash_default_export; | ||
570 | alg->import = shash_default_import; | ||
571 | alg->statesize = alg->descsize; | ||
572 | } | ||
573 | if (!alg->setkey) | ||
574 | alg->setkey = shash_no_setkey; | ||
575 | |||
576 | return 0; | ||
577 | } | ||
578 | |||
579 | int crypto_register_shash(struct shash_alg *alg) | ||
580 | { | ||
581 | struct crypto_alg *base = &alg->base; | ||
582 | int err; | ||
583 | |||
584 | err = shash_prepare_alg(alg); | ||
585 | if (err) | ||
586 | return err; | ||
587 | |||
495 | return crypto_register_alg(base); | 588 | return crypto_register_alg(base); |
496 | } | 589 | } |
497 | EXPORT_SYMBOL_GPL(crypto_register_shash); | 590 | EXPORT_SYMBOL_GPL(crypto_register_shash); |
@@ -502,5 +595,44 @@ int crypto_unregister_shash(struct shash_alg *alg) | |||
502 | } | 595 | } |
503 | EXPORT_SYMBOL_GPL(crypto_unregister_shash); | 596 | EXPORT_SYMBOL_GPL(crypto_unregister_shash); |
504 | 597 | ||
598 | int shash_register_instance(struct crypto_template *tmpl, | ||
599 | struct shash_instance *inst) | ||
600 | { | ||
601 | int err; | ||
602 | |||
603 | err = shash_prepare_alg(&inst->alg); | ||
604 | if (err) | ||
605 | return err; | ||
606 | |||
607 | return crypto_register_instance(tmpl, shash_crypto_instance(inst)); | ||
608 | } | ||
609 | EXPORT_SYMBOL_GPL(shash_register_instance); | ||
610 | |||
611 | void shash_free_instance(struct crypto_instance *inst) | ||
612 | { | ||
613 | crypto_drop_spawn(crypto_instance_ctx(inst)); | ||
614 | kfree(shash_instance(inst)); | ||
615 | } | ||
616 | EXPORT_SYMBOL_GPL(shash_free_instance); | ||
617 | |||
618 | int crypto_init_shash_spawn(struct crypto_shash_spawn *spawn, | ||
619 | struct shash_alg *alg, | ||
620 | struct crypto_instance *inst) | ||
621 | { | ||
622 | return crypto_init_spawn2(&spawn->base, &alg->base, inst, | ||
623 | &crypto_shash_type); | ||
624 | } | ||
625 | EXPORT_SYMBOL_GPL(crypto_init_shash_spawn); | ||
626 | |||
627 | struct shash_alg *shash_attr_alg(struct rtattr *rta, u32 type, u32 mask) | ||
628 | { | ||
629 | struct crypto_alg *alg; | ||
630 | |||
631 | alg = crypto_attr_alg2(rta, &crypto_shash_type, type, mask); | ||
632 | return IS_ERR(alg) ? ERR_CAST(alg) : | ||
633 | container_of(alg, struct shash_alg, base); | ||
634 | } | ||
635 | EXPORT_SYMBOL_GPL(shash_attr_alg); | ||
636 | |||
505 | MODULE_LICENSE("GPL"); | 637 | MODULE_LICENSE("GPL"); |
506 | MODULE_DESCRIPTION("Synchronous cryptographic hash type"); | 638 | MODULE_DESCRIPTION("Synchronous cryptographic hash type"); |