Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
004a403c LH |
2 | /* |
3 | * Asynchronous Cryptographic Hash operations. | |
4 | * | |
5 | * This is the asynchronous version of hash.c with notification of | |
6 | * completion via a callback. | |
7 | * | |
8 | * Copyright (c) 2008 Loc Ho <lho@amcc.com> | |
004a403c LH |
9 | */ |
10 | ||
20036252 HX |
11 | #include <crypto/internal/hash.h> |
12 | #include <crypto/scatterwalk.h> | |
004a403c LH |
13 | #include <linux/err.h> |
14 | #include <linux/kernel.h> | |
15 | #include <linux/module.h> | |
16 | #include <linux/sched.h> | |
17 | #include <linux/slab.h> | |
18 | #include <linux/seq_file.h> | |
6238cbae | 19 | #include <linux/cryptouser.h> |
d8c34b94 | 20 | #include <linux/compiler.h> |
6238cbae | 21 | #include <net/netlink.h> |
004a403c LH |
22 | |
23 | #include "internal.h" | |
24 | ||
6d1b41fc EB |
25 | static const struct crypto_type crypto_ahash_type; |
26 | ||
66f6ce5e HX |
27 | struct ahash_request_priv { |
28 | crypto_completion_t complete; | |
29 | void *data; | |
30 | u8 *result; | |
ef0579b6 | 31 | u32 flags; |
66f6ce5e HX |
32 | void *ubuf[] CRYPTO_MINALIGN_ATTR; |
33 | }; | |
34 | ||
88056ec3 HX |
35 | static inline struct ahash_alg *crypto_ahash_alg(struct crypto_ahash *hash) |
36 | { | |
37 | return container_of(crypto_hash_alg_common(hash), struct ahash_alg, | |
38 | halg); | |
39 | } | |
40 | ||
20036252 HX |
41 | static int hash_walk_next(struct crypto_hash_walk *walk) |
42 | { | |
43 | unsigned int alignmask = walk->alignmask; | |
44 | unsigned int offset = walk->offset; | |
45 | unsigned int nbytes = min(walk->entrylen, | |
46 | ((unsigned int)(PAGE_SIZE)) - offset); | |
47 | ||
aa969515 | 48 | walk->data = kmap_local_page(walk->pg); |
20036252 HX |
49 | walk->data += offset; |
50 | ||
23a75eee SÖ |
51 | if (offset & alignmask) { |
52 | unsigned int unaligned = alignmask + 1 - (offset & alignmask); | |
b516d514 | 53 | |
23a75eee SÖ |
54 | if (nbytes > unaligned) |
55 | nbytes = unaligned; | |
56 | } | |
20036252 HX |
57 | |
58 | walk->entrylen -= nbytes; | |
59 | return nbytes; | |
60 | } | |
61 | ||
62 | static int hash_walk_new_entry(struct crypto_hash_walk *walk) | |
63 | { | |
64 | struct scatterlist *sg; | |
65 | ||
66 | sg = walk->sg; | |
20036252 | 67 | walk->offset = sg->offset; |
13f4bb78 HX |
68 | walk->pg = sg_page(walk->sg) + (walk->offset >> PAGE_SHIFT); |
69 | walk->offset = offset_in_page(walk->offset); | |
20036252 HX |
70 | walk->entrylen = sg->length; |
71 | ||
72 | if (walk->entrylen > walk->total) | |
73 | walk->entrylen = walk->total; | |
74 | walk->total -= walk->entrylen; | |
75 | ||
76 | return hash_walk_next(walk); | |
77 | } | |
78 | ||
79 | int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err) | |
80 | { | |
81 | unsigned int alignmask = walk->alignmask; | |
20036252 HX |
82 | |
83 | walk->data -= walk->offset; | |
84 | ||
77568e53 EB |
85 | if (walk->entrylen && (walk->offset & alignmask) && !err) { |
86 | unsigned int nbytes; | |
20036252 | 87 | |
77568e53 EB |
88 | walk->offset = ALIGN(walk->offset, alignmask + 1); |
89 | nbytes = min(walk->entrylen, | |
90 | (unsigned int)(PAGE_SIZE - walk->offset)); | |
900a081f | 91 | if (nbytes) { |
77568e53 | 92 | walk->entrylen -= nbytes; |
900a081f HX |
93 | walk->data += walk->offset; |
94 | return nbytes; | |
95 | } | |
20036252 HX |
96 | } |
97 | ||
aa969515 | 98 | kunmap_local(walk->data); |
8afa25aa | 99 | crypto_yield(walk->flags); |
20036252 HX |
100 | |
101 | if (err) | |
102 | return err; | |
103 | ||
77568e53 | 104 | if (walk->entrylen) { |
d315a0e0 HX |
105 | walk->offset = 0; |
106 | walk->pg++; | |
20036252 | 107 | return hash_walk_next(walk); |
d315a0e0 | 108 | } |
20036252 HX |
109 | |
110 | if (!walk->total) | |
111 | return 0; | |
112 | ||
5be4d4c9 | 113 | walk->sg = sg_next(walk->sg); |
20036252 HX |
114 | |
115 | return hash_walk_new_entry(walk); | |
116 | } | |
117 | EXPORT_SYMBOL_GPL(crypto_hash_walk_done); | |
118 | ||
119 | int crypto_hash_walk_first(struct ahash_request *req, | |
120 | struct crypto_hash_walk *walk) | |
121 | { | |
122 | walk->total = req->nbytes; | |
123 | ||
6d9529c5 TC |
124 | if (!walk->total) { |
125 | walk->entrylen = 0; | |
20036252 | 126 | return 0; |
6d9529c5 | 127 | } |
20036252 HX |
128 | |
129 | walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req)); | |
130 | walk->sg = req->src; | |
8afa25aa | 131 | walk->flags = req->base.flags; |
20036252 HX |
132 | |
133 | return hash_walk_new_entry(walk); | |
134 | } | |
135 | EXPORT_SYMBOL_GPL(crypto_hash_walk_first); | |
136 | ||
004a403c LH |
137 | static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key, |
138 | unsigned int keylen) | |
139 | { | |
004a403c LH |
140 | unsigned long alignmask = crypto_ahash_alignmask(tfm); |
141 | int ret; | |
142 | u8 *buffer, *alignbuffer; | |
143 | unsigned long absize; | |
144 | ||
145 | absize = keylen + alignmask; | |
093900c2 | 146 | buffer = kmalloc(absize, GFP_KERNEL); |
004a403c LH |
147 | if (!buffer) |
148 | return -ENOMEM; | |
149 | ||
150 | alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); | |
151 | memcpy(alignbuffer, key, keylen); | |
a70c5225 | 152 | ret = tfm->setkey(tfm, alignbuffer, keylen); |
453431a5 | 153 | kfree_sensitive(buffer); |
004a403c LH |
154 | return ret; |
155 | } | |
156 | ||
ba7d7433 EB |
157 | static int ahash_nosetkey(struct crypto_ahash *tfm, const u8 *key, |
158 | unsigned int keylen) | |
159 | { | |
160 | return -ENOSYS; | |
161 | } | |
162 | ||
163 | static void ahash_set_needkey(struct crypto_ahash *tfm) | |
164 | { | |
165 | const struct hash_alg_common *alg = crypto_hash_alg_common(tfm); | |
166 | ||
167 | if (tfm->setkey != ahash_nosetkey && | |
168 | !(alg->base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY)) | |
169 | crypto_ahash_set_flags(tfm, CRYPTO_TFM_NEED_KEY); | |
170 | } | |
171 | ||
66f6ce5e | 172 | int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key, |
004a403c LH |
173 | unsigned int keylen) |
174 | { | |
004a403c | 175 | unsigned long alignmask = crypto_ahash_alignmask(tfm); |
9fa68f62 | 176 | int err; |
004a403c LH |
177 | |
178 | if ((unsigned long)key & alignmask) | |
9fa68f62 EB |
179 | err = ahash_setkey_unaligned(tfm, key, keylen); |
180 | else | |
181 | err = tfm->setkey(tfm, key, keylen); | |
182 | ||
ba7d7433 EB |
183 | if (unlikely(err)) { |
184 | ahash_set_needkey(tfm); | |
9fa68f62 | 185 | return err; |
ba7d7433 | 186 | } |
004a403c | 187 | |
9fa68f62 EB |
188 | crypto_ahash_clear_flags(tfm, CRYPTO_TFM_NEED_KEY); |
189 | return 0; | |
004a403c | 190 | } |
66f6ce5e | 191 | EXPORT_SYMBOL_GPL(crypto_ahash_setkey); |
004a403c | 192 | |
d9588045 HX |
193 | static int ahash_save_req(struct ahash_request *req, crypto_completion_t cplt, |
194 | bool has_state) | |
66f6ce5e HX |
195 | { |
196 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); | |
197 | unsigned long alignmask = crypto_ahash_alignmask(tfm); | |
198 | unsigned int ds = crypto_ahash_digestsize(tfm); | |
d9588045 HX |
199 | struct ahash_request *subreq; |
200 | unsigned int subreq_size; | |
201 | unsigned int reqsize; | |
202 | u8 *result; | |
203 | gfp_t gfp; | |
204 | u32 flags; | |
66f6ce5e | 205 | |
d9588045 HX |
206 | subreq_size = sizeof(*subreq); |
207 | reqsize = crypto_ahash_reqsize(tfm); | |
208 | reqsize = ALIGN(reqsize, crypto_tfm_ctx_alignment()); | |
209 | subreq_size += reqsize; | |
210 | subreq_size += ds; | |
211 | subreq_size += alignmask & ~(crypto_tfm_ctx_alignment() - 1); | |
212 | ||
213 | flags = ahash_request_flags(req); | |
214 | gfp = (flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? GFP_KERNEL : GFP_ATOMIC; | |
215 | subreq = kmalloc(subreq_size, gfp); | |
216 | if (!subreq) | |
66f6ce5e HX |
217 | return -ENOMEM; |
218 | ||
d9588045 HX |
219 | ahash_request_set_tfm(subreq, tfm); |
220 | ahash_request_set_callback(subreq, flags, cplt, req); | |
221 | ||
222 | result = (u8 *)(subreq + 1) + reqsize; | |
223 | result = PTR_ALIGN(result, alignmask + 1); | |
224 | ||
225 | ahash_request_set_crypt(subreq, req->src, result, req->nbytes); | |
226 | ||
227 | if (has_state) { | |
228 | void *state; | |
229 | ||
230 | state = kmalloc(crypto_ahash_statesize(tfm), gfp); | |
231 | if (!state) { | |
232 | kfree(subreq); | |
233 | return -ENOMEM; | |
234 | } | |
235 | ||
236 | crypto_ahash_export(req, state); | |
237 | crypto_ahash_import(subreq, state); | |
238 | kfree_sensitive(state); | |
239 | } | |
240 | ||
241 | req->priv = subreq; | |
66f6ce5e | 242 | |
1ffc9fbd MV |
243 | return 0; |
244 | } | |
245 | ||
ef0579b6 | 246 | static void ahash_restore_req(struct ahash_request *req, int err) |
1ffc9fbd | 247 | { |
d9588045 | 248 | struct ahash_request *subreq = req->priv; |
1ffc9fbd | 249 | |
ef0579b6 | 250 | if (!err) |
d9588045 | 251 | memcpy(req->result, subreq->result, |
ef0579b6 HX |
252 | crypto_ahash_digestsize(crypto_ahash_reqtfm(req))); |
253 | ||
1ffc9fbd MV |
254 | req->priv = NULL; |
255 | ||
d9588045 | 256 | kfree_sensitive(subreq); |
1ffc9fbd MV |
257 | } |
258 | ||
255e48eb | 259 | static void ahash_op_unaligned_done(void *data, int err) |
1ffc9fbd | 260 | { |
255e48eb | 261 | struct ahash_request *areq = data; |
1ffc9fbd | 262 | |
d9588045 HX |
263 | if (err == -EINPROGRESS) |
264 | goto out; | |
1ffc9fbd MV |
265 | |
266 | /* First copy req->result into req->priv.result */ | |
ef0579b6 | 267 | ahash_restore_req(areq, err); |
1ffc9fbd | 268 | |
d9588045 | 269 | out: |
1ffc9fbd | 270 | /* Complete the ORIGINAL request. */ |
d9588045 | 271 | ahash_request_complete(areq, err); |
1ffc9fbd MV |
272 | } |
273 | ||
274 | static int ahash_op_unaligned(struct ahash_request *req, | |
d9588045 HX |
275 | int (*op)(struct ahash_request *), |
276 | bool has_state) | |
1ffc9fbd MV |
277 | { |
278 | int err; | |
279 | ||
d9588045 | 280 | err = ahash_save_req(req, ahash_op_unaligned_done, has_state); |
1ffc9fbd MV |
281 | if (err) |
282 | return err; | |
283 | ||
d9588045 | 284 | err = op(req->priv); |
4e5b0ad5 | 285 | if (err == -EINPROGRESS || err == -EBUSY) |
ef0579b6 HX |
286 | return err; |
287 | ||
288 | ahash_restore_req(req, err); | |
66f6ce5e HX |
289 | |
290 | return err; | |
291 | } | |
292 | ||
293 | static int crypto_ahash_op(struct ahash_request *req, | |
d9588045 HX |
294 | int (*op)(struct ahash_request *), |
295 | bool has_state) | |
66f6ce5e HX |
296 | { |
297 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); | |
298 | unsigned long alignmask = crypto_ahash_alignmask(tfm); | |
299 | ||
300 | if ((unsigned long)req->result & alignmask) | |
d9588045 | 301 | return ahash_op_unaligned(req, op, has_state); |
66f6ce5e HX |
302 | |
303 | return op(req); | |
304 | } | |
305 | ||
306 | int crypto_ahash_final(struct ahash_request *req) | |
307 | { | |
f7d76e05 CL |
308 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
309 | struct crypto_alg *alg = tfm->base.__crt_alg; | |
310 | unsigned int nbytes = req->nbytes; | |
cac5818c CL |
311 | int ret; |
312 | ||
f7d76e05 | 313 | crypto_stats_get(alg); |
d9588045 | 314 | ret = crypto_ahash_op(req, crypto_ahash_reqtfm(req)->final, true); |
f7d76e05 | 315 | crypto_stats_ahash_final(nbytes, ret, alg); |
cac5818c | 316 | return ret; |
66f6ce5e HX |
317 | } |
318 | EXPORT_SYMBOL_GPL(crypto_ahash_final); | |
319 | ||
320 | int crypto_ahash_finup(struct ahash_request *req) | |
321 | { | |
f7d76e05 CL |
322 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
323 | struct crypto_alg *alg = tfm->base.__crt_alg; | |
324 | unsigned int nbytes = req->nbytes; | |
cac5818c CL |
325 | int ret; |
326 | ||
f7d76e05 | 327 | crypto_stats_get(alg); |
d9588045 | 328 | ret = crypto_ahash_op(req, crypto_ahash_reqtfm(req)->finup, true); |
f7d76e05 | 329 | crypto_stats_ahash_final(nbytes, ret, alg); |
cac5818c | 330 | return ret; |
66f6ce5e HX |
331 | } |
332 | EXPORT_SYMBOL_GPL(crypto_ahash_finup); | |
333 | ||
334 | int crypto_ahash_digest(struct ahash_request *req) | |
335 | { | |
9fa68f62 | 336 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
f7d76e05 CL |
337 | struct crypto_alg *alg = tfm->base.__crt_alg; |
338 | unsigned int nbytes = req->nbytes; | |
cac5818c | 339 | int ret; |
9fa68f62 | 340 | |
f7d76e05 | 341 | crypto_stats_get(alg); |
9fa68f62 | 342 | if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) |
cac5818c CL |
343 | ret = -ENOKEY; |
344 | else | |
d9588045 | 345 | ret = crypto_ahash_op(req, tfm->digest, false); |
f7d76e05 | 346 | crypto_stats_ahash_final(nbytes, ret, alg); |
cac5818c | 347 | return ret; |
66f6ce5e HX |
348 | } |
349 | EXPORT_SYMBOL_GPL(crypto_ahash_digest); | |
350 | ||
255e48eb | 351 | static void ahash_def_finup_done2(void *data, int err) |
66f6ce5e | 352 | { |
255e48eb | 353 | struct ahash_request *areq = data; |
66f6ce5e HX |
354 | |
355 | if (err == -EINPROGRESS) | |
356 | return; | |
357 | ||
ef0579b6 | 358 | ahash_restore_req(areq, err); |
66f6ce5e | 359 | |
d9588045 | 360 | ahash_request_complete(areq, err); |
66f6ce5e HX |
361 | } |
362 | ||
363 | static int ahash_def_finup_finish1(struct ahash_request *req, int err) | |
364 | { | |
d9588045 HX |
365 | struct ahash_request *subreq = req->priv; |
366 | ||
66f6ce5e HX |
367 | if (err) |
368 | goto out; | |
369 | ||
d9588045 | 370 | subreq->base.complete = ahash_def_finup_done2; |
ef0579b6 | 371 | |
d9588045 | 372 | err = crypto_ahash_reqtfm(req)->final(subreq); |
4e5b0ad5 | 373 | if (err == -EINPROGRESS || err == -EBUSY) |
ef0579b6 | 374 | return err; |
66f6ce5e HX |
375 | |
376 | out: | |
ef0579b6 | 377 | ahash_restore_req(req, err); |
66f6ce5e HX |
378 | return err; |
379 | } | |
380 | ||
255e48eb | 381 | static void ahash_def_finup_done1(void *data, int err) |
66f6ce5e | 382 | { |
255e48eb | 383 | struct ahash_request *areq = data; |
d9588045 | 384 | struct ahash_request *subreq; |
66f6ce5e | 385 | |
d9588045 HX |
386 | if (err == -EINPROGRESS) |
387 | goto out; | |
ef0579b6 | 388 | |
d9588045 HX |
389 | subreq = areq->priv; |
390 | subreq->base.flags &= CRYPTO_TFM_REQ_MAY_BACKLOG; | |
ef0579b6 | 391 | |
66f6ce5e | 392 | err = ahash_def_finup_finish1(areq, err); |
d9588045 | 393 | if (err == -EINPROGRESS || err == -EBUSY) |
ef0579b6 | 394 | return; |
66f6ce5e | 395 | |
d9588045 HX |
396 | out: |
397 | ahash_request_complete(areq, err); | |
66f6ce5e HX |
398 | } |
399 | ||
400 | static int ahash_def_finup(struct ahash_request *req) | |
401 | { | |
402 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); | |
d4a7a0fb | 403 | int err; |
66f6ce5e | 404 | |
d9588045 | 405 | err = ahash_save_req(req, ahash_def_finup_done1, true); |
d4a7a0fb MV |
406 | if (err) |
407 | return err; | |
66f6ce5e | 408 | |
d9588045 | 409 | err = tfm->update(req->priv); |
4e5b0ad5 | 410 | if (err == -EINPROGRESS || err == -EBUSY) |
ef0579b6 HX |
411 | return err; |
412 | ||
d4a7a0fb | 413 | return ahash_def_finup_finish1(req, err); |
66f6ce5e HX |
414 | } |
415 | ||
e73d340d HX |
416 | static void crypto_ahash_exit_tfm(struct crypto_tfm *tfm) |
417 | { | |
418 | struct crypto_ahash *hash = __crypto_ahash_cast(tfm); | |
419 | struct ahash_alg *alg = crypto_ahash_alg(hash); | |
420 | ||
421 | alg->exit_tfm(hash); | |
422 | } | |
423 | ||
88056ec3 HX |
424 | static int crypto_ahash_init_tfm(struct crypto_tfm *tfm) |
425 | { | |
426 | struct crypto_ahash *hash = __crypto_ahash_cast(tfm); | |
427 | struct ahash_alg *alg = crypto_ahash_alg(hash); | |
88056ec3 | 428 | |
66f6ce5e | 429 | hash->setkey = ahash_nosetkey; |
66f6ce5e | 430 | |
88056ec3 HX |
431 | if (tfm->__crt_alg->cra_type != &crypto_ahash_type) |
432 | return crypto_init_shash_ops_async(tfm); | |
433 | ||
88056ec3 HX |
434 | hash->init = alg->init; |
435 | hash->update = alg->update; | |
66f6ce5e HX |
436 | hash->final = alg->final; |
437 | hash->finup = alg->finup ?: ahash_def_finup; | |
88056ec3 | 438 | hash->digest = alg->digest; |
6f221f7e KK |
439 | hash->export = alg->export; |
440 | hash->import = alg->import; | |
66f6ce5e | 441 | |
a5596d63 | 442 | if (alg->setkey) { |
66f6ce5e | 443 | hash->setkey = alg->setkey; |
ba7d7433 | 444 | ahash_set_needkey(hash); |
a5596d63 | 445 | } |
88056ec3 | 446 | |
e73d340d HX |
447 | if (alg->exit_tfm) |
448 | tfm->exit = crypto_ahash_exit_tfm; | |
449 | ||
450 | return alg->init_tfm ? alg->init_tfm(hash) : 0; | |
88056ec3 HX |
451 | } |
452 | ||
453 | static unsigned int crypto_ahash_extsize(struct crypto_alg *alg) | |
454 | { | |
2495cf25 HX |
455 | if (alg->cra_type != &crypto_ahash_type) |
456 | return sizeof(struct crypto_shash *); | |
88056ec3 | 457 | |
2495cf25 | 458 | return crypto_alg_extsize(alg); |
88056ec3 HX |
459 | } |
460 | ||
48fb3e57 EB |
461 | static void crypto_ahash_free_instance(struct crypto_instance *inst) |
462 | { | |
463 | struct ahash_instance *ahash = ahash_instance(inst); | |
464 | ||
48fb3e57 EB |
465 | ahash->free(ahash); |
466 | } | |
467 | ||
3acc8473 | 468 | #ifdef CONFIG_NET |
6238cbae SK |
469 | static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg) |
470 | { | |
471 | struct crypto_report_hash rhash; | |
472 | ||
37db69e0 EB |
473 | memset(&rhash, 0, sizeof(rhash)); |
474 | ||
475 | strscpy(rhash.type, "ahash", sizeof(rhash.type)); | |
6238cbae SK |
476 | |
477 | rhash.blocksize = alg->cra_blocksize; | |
478 | rhash.digestsize = __crypto_hash_alg_common(alg)->digestsize; | |
479 | ||
37db69e0 | 480 | return nla_put(skb, CRYPTOCFGA_REPORT_HASH, sizeof(rhash), &rhash); |
6238cbae | 481 | } |
3acc8473 HX |
482 | #else |
483 | static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg) | |
484 | { | |
485 | return -ENOSYS; | |
486 | } | |
487 | #endif | |
6238cbae | 488 | |
004a403c | 489 | static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg) |
d8c34b94 | 490 | __maybe_unused; |
004a403c LH |
491 | static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg) |
492 | { | |
493 | seq_printf(m, "type : ahash\n"); | |
494 | seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ? | |
495 | "yes" : "no"); | |
496 | seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); | |
88056ec3 HX |
497 | seq_printf(m, "digestsize : %u\n", |
498 | __crypto_hash_alg_common(alg)->digestsize); | |
004a403c LH |
499 | } |
500 | ||
6d1b41fc | 501 | static const struct crypto_type crypto_ahash_type = { |
88056ec3 HX |
502 | .extsize = crypto_ahash_extsize, |
503 | .init_tfm = crypto_ahash_init_tfm, | |
48fb3e57 | 504 | .free = crypto_ahash_free_instance, |
004a403c LH |
505 | #ifdef CONFIG_PROC_FS |
506 | .show = crypto_ahash_show, | |
507 | #endif | |
6238cbae | 508 | .report = crypto_ahash_report, |
88056ec3 HX |
509 | .maskclear = ~CRYPTO_ALG_TYPE_MASK, |
510 | .maskset = CRYPTO_ALG_TYPE_AHASH_MASK, | |
511 | .type = CRYPTO_ALG_TYPE_AHASH, | |
512 | .tfmsize = offsetof(struct crypto_ahash, base), | |
004a403c | 513 | }; |
004a403c | 514 | |
84a9c938 EB |
515 | int crypto_grab_ahash(struct crypto_ahash_spawn *spawn, |
516 | struct crypto_instance *inst, | |
517 | const char *name, u32 type, u32 mask) | |
518 | { | |
519 | spawn->base.frontend = &crypto_ahash_type; | |
520 | return crypto_grab_spawn(&spawn->base, inst, name, type, mask); | |
521 | } | |
522 | EXPORT_SYMBOL_GPL(crypto_grab_ahash); | |
523 | ||
88056ec3 HX |
524 | struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type, |
525 | u32 mask) | |
526 | { | |
527 | return crypto_alloc_tfm(alg_name, &crypto_ahash_type, type, mask); | |
528 | } | |
529 | EXPORT_SYMBOL_GPL(crypto_alloc_ahash); | |
530 | ||
8d18e34c HX |
531 | int crypto_has_ahash(const char *alg_name, u32 type, u32 mask) |
532 | { | |
533 | return crypto_type_has_alg(alg_name, &crypto_ahash_type, type, mask); | |
534 | } | |
535 | EXPORT_SYMBOL_GPL(crypto_has_ahash); | |
536 | ||
01c2dece HX |
537 | static int ahash_prepare_alg(struct ahash_alg *alg) |
538 | { | |
539 | struct crypto_alg *base = &alg->halg.base; | |
540 | ||
b68a7ec1 KC |
541 | if (alg->halg.digestsize > HASH_MAX_DIGESTSIZE || |
542 | alg->halg.statesize > HASH_MAX_STATESIZE || | |
8996eafd | 543 | alg->halg.statesize == 0) |
01c2dece HX |
544 | return -EINVAL; |
545 | ||
546 | base->cra_type = &crypto_ahash_type; | |
547 | base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK; | |
548 | base->cra_flags |= CRYPTO_ALG_TYPE_AHASH; | |
549 | ||
550 | return 0; | |
551 | } | |
552 | ||
553 | int crypto_register_ahash(struct ahash_alg *alg) | |
554 | { | |
555 | struct crypto_alg *base = &alg->halg.base; | |
556 | int err; | |
557 | ||
558 | err = ahash_prepare_alg(alg); | |
559 | if (err) | |
560 | return err; | |
561 | ||
562 | return crypto_register_alg(base); | |
563 | } | |
564 | EXPORT_SYMBOL_GPL(crypto_register_ahash); | |
565 | ||
c6d633a9 | 566 | void crypto_unregister_ahash(struct ahash_alg *alg) |
01c2dece | 567 | { |
c6d633a9 | 568 | crypto_unregister_alg(&alg->halg.base); |
01c2dece HX |
569 | } |
570 | EXPORT_SYMBOL_GPL(crypto_unregister_ahash); | |
571 | ||
6f7473c5 RV |
572 | int crypto_register_ahashes(struct ahash_alg *algs, int count) |
573 | { | |
574 | int i, ret; | |
575 | ||
576 | for (i = 0; i < count; i++) { | |
577 | ret = crypto_register_ahash(&algs[i]); | |
578 | if (ret) | |
579 | goto err; | |
580 | } | |
581 | ||
582 | return 0; | |
583 | ||
584 | err: | |
585 | for (--i; i >= 0; --i) | |
586 | crypto_unregister_ahash(&algs[i]); | |
587 | ||
588 | return ret; | |
589 | } | |
590 | EXPORT_SYMBOL_GPL(crypto_register_ahashes); | |
591 | ||
592 | void crypto_unregister_ahashes(struct ahash_alg *algs, int count) | |
593 | { | |
594 | int i; | |
595 | ||
596 | for (i = count - 1; i >= 0; --i) | |
597 | crypto_unregister_ahash(&algs[i]); | |
598 | } | |
599 | EXPORT_SYMBOL_GPL(crypto_unregister_ahashes); | |
600 | ||
01c2dece HX |
601 | int ahash_register_instance(struct crypto_template *tmpl, |
602 | struct ahash_instance *inst) | |
603 | { | |
604 | int err; | |
605 | ||
d4fdc2df EB |
606 | if (WARN_ON(!inst->free)) |
607 | return -EINVAL; | |
608 | ||
01c2dece HX |
609 | err = ahash_prepare_alg(&inst->alg); |
610 | if (err) | |
611 | return err; | |
612 | ||
613 | return crypto_register_instance(tmpl, ahash_crypto_instance(inst)); | |
614 | } | |
615 | EXPORT_SYMBOL_GPL(ahash_register_instance); | |
616 | ||
cd6ed77a EB |
617 | bool crypto_hash_alg_has_setkey(struct hash_alg_common *halg) |
618 | { | |
619 | struct crypto_alg *alg = &halg->base; | |
620 | ||
621 | if (alg->cra_type != &crypto_ahash_type) | |
622 | return crypto_shash_alg_has_setkey(__crypto_shash_alg(alg)); | |
623 | ||
624 | return __crypto_ahash_alg(alg)->setkey != NULL; | |
625 | } | |
626 | EXPORT_SYMBOL_GPL(crypto_hash_alg_has_setkey); | |
627 | ||
004a403c LH |
628 | MODULE_LICENSE("GPL"); |
629 | MODULE_DESCRIPTION("Asynchronous cryptographic hash type"); |