Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
7b5a080b HX |
2 | /* |
3 | * Synchronous Cryptographic Hash operations. | |
4 | * | |
5 | * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au> | |
7b5a080b HX |
6 | */ |
7 | ||
3b2f6df0 | 8 | #include <crypto/scatterwalk.h> |
42808e5d | 9 | #include <linux/cryptouser.h> |
7b5a080b HX |
10 | #include <linux/err.h> |
11 | #include <linux/kernel.h> | |
12 | #include <linux/module.h> | |
13 | #include <linux/slab.h> | |
14 | #include <linux/seq_file.h> | |
42808e5d | 15 | #include <linux/string.h> |
f4d663ce | 16 | #include <net/netlink.h> |
7b5a080b | 17 | |
42808e5d | 18 | #include "hash.h" |
3b2f6df0 | 19 | |
1c799571 HX |
20 | #define MAX_SHASH_ALIGNMASK 63 |
21 | ||
3f683d61 HX |
22 | static const struct crypto_type crypto_shash_type; |
23 | ||
42808e5d HX |
24 | static inline struct crypto_istat_hash *shash_get_stat(struct shash_alg *alg) |
25 | { | |
26 | return hash_get_stat(&alg->halg); | |
27 | } | |
28 | ||
29 | static inline int crypto_shash_errstat(struct shash_alg *alg, int err) | |
30 | { | |
31 | return crypto_hash_errstat(&alg->halg, err); | |
32 | } | |
33 | ||
c060e16d EB |
34 | int shash_no_setkey(struct crypto_shash *tfm, const u8 *key, |
35 | unsigned int keylen) | |
57cfe44b HX |
36 | { |
37 | return -ENOSYS; | |
38 | } | |
c060e16d | 39 | EXPORT_SYMBOL_GPL(shash_no_setkey); |
57cfe44b | 40 | |
7b5a080b HX |
41 | static int shash_setkey_unaligned(struct crypto_shash *tfm, const u8 *key, |
42 | unsigned int keylen) | |
43 | { | |
44 | struct shash_alg *shash = crypto_shash_alg(tfm); | |
45 | unsigned long alignmask = crypto_shash_alignmask(tfm); | |
46 | unsigned long absize; | |
47 | u8 *buffer, *alignbuffer; | |
48 | int err; | |
49 | ||
18eb8ea6 | 50 | absize = keylen + (alignmask & ~(crypto_tfm_ctx_alignment() - 1)); |
9039f3ef | 51 | buffer = kmalloc(absize, GFP_ATOMIC); |
7b5a080b HX |
52 | if (!buffer) |
53 | return -ENOMEM; | |
54 | ||
55 | alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); | |
56 | memcpy(alignbuffer, key, keylen); | |
57 | err = shash->setkey(tfm, alignbuffer, keylen); | |
453431a5 | 58 | kfree_sensitive(buffer); |
7b5a080b HX |
59 | return err; |
60 | } | |
61 | ||
ba7d7433 EB |
62 | static void shash_set_needkey(struct crypto_shash *tfm, struct shash_alg *alg) |
63 | { | |
c2881789 | 64 | if (crypto_shash_alg_needs_key(alg)) |
ba7d7433 EB |
65 | crypto_shash_set_flags(tfm, CRYPTO_TFM_NEED_KEY); |
66 | } | |
67 | ||
7b5a080b HX |
68 | int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key, |
69 | unsigned int keylen) | |
70 | { | |
71 | struct shash_alg *shash = crypto_shash_alg(tfm); | |
72 | unsigned long alignmask = crypto_shash_alignmask(tfm); | |
9fa68f62 | 73 | int err; |
7b5a080b HX |
74 | |
75 | if ((unsigned long)key & alignmask) | |
9fa68f62 EB |
76 | err = shash_setkey_unaligned(tfm, key, keylen); |
77 | else | |
78 | err = shash->setkey(tfm, key, keylen); | |
79 | ||
ba7d7433 EB |
80 | if (unlikely(err)) { |
81 | shash_set_needkey(tfm, shash); | |
9fa68f62 | 82 | return err; |
ba7d7433 | 83 | } |
7b5a080b | 84 | |
9fa68f62 EB |
85 | crypto_shash_clear_flags(tfm, CRYPTO_TFM_NEED_KEY); |
86 | return 0; | |
7b5a080b HX |
87 | } |
88 | EXPORT_SYMBOL_GPL(crypto_shash_setkey); | |
89 | ||
7b5a080b HX |
90 | static int shash_update_unaligned(struct shash_desc *desc, const u8 *data, |
91 | unsigned int len) | |
92 | { | |
93 | struct crypto_shash *tfm = desc->tfm; | |
94 | struct shash_alg *shash = crypto_shash_alg(tfm); | |
95 | unsigned long alignmask = crypto_shash_alignmask(tfm); | |
96 | unsigned int unaligned_len = alignmask + 1 - | |
97 | ((unsigned long)data & alignmask); | |
f3569fd6 KC |
98 | /* |
99 | * We cannot count on __aligned() working for large values: | |
100 | * https://patchwork.kernel.org/patch/9507697/ | |
101 | */ | |
1c799571 | 102 | u8 ubuf[MAX_SHASH_ALIGNMASK * 2]; |
0e2d3a12 | 103 | u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1); |
8c32c516 | 104 | int err; |
7b5a080b | 105 | |
f3569fd6 KC |
106 | if (WARN_ON(buf + unaligned_len > ubuf + sizeof(ubuf))) |
107 | return -EINVAL; | |
108 | ||
f4f68993 YS |
109 | if (unaligned_len > len) |
110 | unaligned_len = len; | |
111 | ||
7b5a080b | 112 | memcpy(buf, data, unaligned_len); |
8c32c516 HX |
113 | err = shash->update(desc, buf, unaligned_len); |
114 | memset(buf, 0, unaligned_len); | |
7b5a080b | 115 | |
8c32c516 | 116 | return err ?: |
7b5a080b HX |
117 | shash->update(desc, data + unaligned_len, len - unaligned_len); |
118 | } | |
119 | ||
120 | int crypto_shash_update(struct shash_desc *desc, const u8 *data, | |
121 | unsigned int len) | |
122 | { | |
123 | struct crypto_shash *tfm = desc->tfm; | |
124 | struct shash_alg *shash = crypto_shash_alg(tfm); | |
125 | unsigned long alignmask = crypto_shash_alignmask(tfm); | |
42808e5d HX |
126 | int err; |
127 | ||
128 | if (IS_ENABLED(CONFIG_CRYPTO_STATS)) | |
129 | atomic64_add(len, &shash_get_stat(shash)->hash_tlen); | |
7b5a080b HX |
130 | |
131 | if ((unsigned long)data & alignmask) | |
42808e5d HX |
132 | err = shash_update_unaligned(desc, data, len); |
133 | else | |
134 | err = shash->update(desc, data, len); | |
7b5a080b | 135 | |
42808e5d | 136 | return crypto_shash_errstat(shash, err); |
7b5a080b HX |
137 | } |
138 | EXPORT_SYMBOL_GPL(crypto_shash_update); | |
139 | ||
140 | static int shash_final_unaligned(struct shash_desc *desc, u8 *out) | |
141 | { | |
142 | struct crypto_shash *tfm = desc->tfm; | |
143 | unsigned long alignmask = crypto_shash_alignmask(tfm); | |
144 | struct shash_alg *shash = crypto_shash_alg(tfm); | |
145 | unsigned int ds = crypto_shash_digestsize(tfm); | |
f3569fd6 KC |
146 | /* |
147 | * We cannot count on __aligned() working for large values: | |
148 | * https://patchwork.kernel.org/patch/9507697/ | |
149 | */ | |
1c799571 | 150 | u8 ubuf[MAX_SHASH_ALIGNMASK + HASH_MAX_DIGESTSIZE]; |
0e2d3a12 | 151 | u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1); |
7b5a080b HX |
152 | int err; |
153 | ||
f3569fd6 KC |
154 | if (WARN_ON(buf + ds > ubuf + sizeof(ubuf))) |
155 | return -EINVAL; | |
156 | ||
7b5a080b | 157 | err = shash->final(desc, buf); |
8c32c516 HX |
158 | if (err) |
159 | goto out; | |
160 | ||
7b5a080b | 161 | memcpy(out, buf, ds); |
8c32c516 HX |
162 | |
163 | out: | |
164 | memset(buf, 0, ds); | |
7b5a080b HX |
165 | return err; |
166 | } | |
167 | ||
168 | int crypto_shash_final(struct shash_desc *desc, u8 *out) | |
169 | { | |
170 | struct crypto_shash *tfm = desc->tfm; | |
171 | struct shash_alg *shash = crypto_shash_alg(tfm); | |
172 | unsigned long alignmask = crypto_shash_alignmask(tfm); | |
42808e5d HX |
173 | int err; |
174 | ||
175 | if (IS_ENABLED(CONFIG_CRYPTO_STATS)) | |
176 | atomic64_inc(&shash_get_stat(shash)->hash_cnt); | |
7b5a080b HX |
177 | |
178 | if ((unsigned long)out & alignmask) | |
42808e5d HX |
179 | err = shash_final_unaligned(desc, out); |
180 | else | |
181 | err = shash->final(desc, out); | |
7b5a080b | 182 | |
42808e5d | 183 | return crypto_shash_errstat(shash, err); |
7b5a080b HX |
184 | } |
185 | EXPORT_SYMBOL_GPL(crypto_shash_final); | |
186 | ||
187 | static int shash_finup_unaligned(struct shash_desc *desc, const u8 *data, | |
188 | unsigned int len, u8 *out) | |
189 | { | |
42808e5d HX |
190 | return shash_update_unaligned(desc, data, len) ?: |
191 | shash_final_unaligned(desc, out); | |
7b5a080b HX |
192 | } |
193 | ||
313a4074 EB |
194 | static int shash_default_finup(struct shash_desc *desc, const u8 *data, |
195 | unsigned int len, u8 *out) | |
196 | { | |
197 | struct shash_alg *shash = crypto_shash_alg(desc->tfm); | |
198 | ||
199 | return shash->update(desc, data, len) ?: | |
200 | shash->final(desc, out); | |
201 | } | |
202 | ||
7b5a080b HX |
203 | int crypto_shash_finup(struct shash_desc *desc, const u8 *data, |
204 | unsigned int len, u8 *out) | |
205 | { | |
206 | struct crypto_shash *tfm = desc->tfm; | |
207 | struct shash_alg *shash = crypto_shash_alg(tfm); | |
208 | unsigned long alignmask = crypto_shash_alignmask(tfm); | |
42808e5d HX |
209 | int err; |
210 | ||
211 | if (IS_ENABLED(CONFIG_CRYPTO_STATS)) { | |
212 | struct crypto_istat_hash *istat = shash_get_stat(shash); | |
213 | ||
214 | atomic64_inc(&istat->hash_cnt); | |
215 | atomic64_add(len, &istat->hash_tlen); | |
216 | } | |
7b5a080b | 217 | |
8267adab | 218 | if (((unsigned long)data | (unsigned long)out) & alignmask) |
42808e5d HX |
219 | err = shash_finup_unaligned(desc, data, len, out); |
220 | else | |
221 | err = shash->finup(desc, data, len, out); | |
222 | ||
7b5a080b | 223 | |
42808e5d | 224 | return crypto_shash_errstat(shash, err); |
7b5a080b HX |
225 | } |
226 | EXPORT_SYMBOL_GPL(crypto_shash_finup); | |
227 | ||
313a4074 EB |
228 | static int shash_default_digest(struct shash_desc *desc, const u8 *data, |
229 | unsigned int len, u8 *out) | |
230 | { | |
231 | struct shash_alg *shash = crypto_shash_alg(desc->tfm); | |
232 | ||
233 | return shash->init(desc) ?: | |
234 | shash->finup(desc, data, len, out); | |
235 | } | |
236 | ||
7b5a080b HX |
237 | int crypto_shash_digest(struct shash_desc *desc, const u8 *data, |
238 | unsigned int len, u8 *out) | |
239 | { | |
240 | struct crypto_shash *tfm = desc->tfm; | |
241 | struct shash_alg *shash = crypto_shash_alg(tfm); | |
242 | unsigned long alignmask = crypto_shash_alignmask(tfm); | |
42808e5d | 243 | int err; |
7b5a080b | 244 | |
42808e5d HX |
245 | if (IS_ENABLED(CONFIG_CRYPTO_STATS)) { |
246 | struct crypto_istat_hash *istat = shash_get_stat(shash); | |
9fa68f62 | 247 | |
42808e5d HX |
248 | atomic64_inc(&istat->hash_cnt); |
249 | atomic64_add(len, &istat->hash_tlen); | |
250 | } | |
7b5a080b | 251 | |
42808e5d HX |
252 | if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) |
253 | err = -ENOKEY; | |
254 | else if (((unsigned long)data | (unsigned long)out) & alignmask) | |
2e02c25a EB |
255 | err = shash->init(desc) ?: |
256 | shash_finup_unaligned(desc, data, len, out); | |
42808e5d HX |
257 | else |
258 | err = shash->digest(desc, data, len, out); | |
259 | ||
260 | return crypto_shash_errstat(shash, err); | |
7b5a080b HX |
261 | } |
262 | EXPORT_SYMBOL_GPL(crypto_shash_digest); | |
263 | ||
822a98b8 EB |
264 | int crypto_shash_tfm_digest(struct crypto_shash *tfm, const u8 *data, |
265 | unsigned int len, u8 *out) | |
266 | { | |
267 | SHASH_DESC_ON_STACK(desc, tfm); | |
268 | int err; | |
269 | ||
270 | desc->tfm = tfm; | |
271 | ||
272 | err = crypto_shash_digest(desc, data, len, out); | |
273 | ||
274 | shash_desc_zero(desc); | |
275 | ||
276 | return err; | |
277 | } | |
278 | EXPORT_SYMBOL_GPL(crypto_shash_tfm_digest); | |
279 | ||
08debaa5 | 280 | int crypto_shash_export(struct shash_desc *desc, void *out) |
dec8b786 | 281 | { |
08debaa5 EB |
282 | struct crypto_shash *tfm = desc->tfm; |
283 | struct shash_alg *shash = crypto_shash_alg(tfm); | |
284 | ||
285 | if (shash->export) | |
286 | return shash->export(desc, out); | |
287 | ||
288 | memcpy(out, shash_desc_ctx(desc), crypto_shash_descsize(tfm)); | |
f592682f | 289 | return 0; |
99d27e1c | 290 | } |
08debaa5 | 291 | EXPORT_SYMBOL_GPL(crypto_shash_export); |
dec8b786 | 292 | |
08debaa5 | 293 | int crypto_shash_import(struct shash_desc *desc, const void *in) |
99d27e1c | 294 | { |
08debaa5 EB |
295 | struct crypto_shash *tfm = desc->tfm; |
296 | struct shash_alg *shash = crypto_shash_alg(tfm); | |
297 | ||
298 | if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) | |
299 | return -ENOKEY; | |
300 | ||
301 | if (shash->import) | |
302 | return shash->import(desc, in); | |
303 | ||
304 | memcpy(shash_desc_ctx(desc), in, crypto_shash_descsize(tfm)); | |
f592682f | 305 | return 0; |
dec8b786 | 306 | } |
08debaa5 | 307 | EXPORT_SYMBOL_GPL(crypto_shash_import); |
dec8b786 | 308 | |
3b2f6df0 HX |
309 | static int shash_async_setkey(struct crypto_ahash *tfm, const u8 *key, |
310 | unsigned int keylen) | |
311 | { | |
312 | struct crypto_shash **ctx = crypto_ahash_ctx(tfm); | |
313 | ||
314 | return crypto_shash_setkey(*ctx, key, keylen); | |
315 | } | |
316 | ||
317 | static int shash_async_init(struct ahash_request *req) | |
318 | { | |
319 | struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req)); | |
320 | struct shash_desc *desc = ahash_request_ctx(req); | |
321 | ||
322 | desc->tfm = *ctx; | |
3b2f6df0 HX |
323 | |
324 | return crypto_shash_init(desc); | |
325 | } | |
326 | ||
7eddf95e | 327 | int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc) |
3b2f6df0 | 328 | { |
3b2f6df0 HX |
329 | struct crypto_hash_walk walk; |
330 | int nbytes; | |
331 | ||
332 | for (nbytes = crypto_hash_walk_first(req, &walk); nbytes > 0; | |
333 | nbytes = crypto_hash_walk_done(&walk, nbytes)) | |
334 | nbytes = crypto_shash_update(desc, walk.data, nbytes); | |
335 | ||
336 | return nbytes; | |
337 | } | |
7eddf95e HX |
338 | EXPORT_SYMBOL_GPL(shash_ahash_update); |
339 | ||
340 | static int shash_async_update(struct ahash_request *req) | |
341 | { | |
342 | return shash_ahash_update(req, ahash_request_ctx(req)); | |
343 | } | |
3b2f6df0 HX |
344 | |
345 | static int shash_async_final(struct ahash_request *req) | |
346 | { | |
347 | return crypto_shash_final(ahash_request_ctx(req), req->result); | |
348 | } | |
349 | ||
66f6ce5e HX |
350 | int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc) |
351 | { | |
352 | struct crypto_hash_walk walk; | |
353 | int nbytes; | |
354 | ||
cbc86b91 HX |
355 | nbytes = crypto_hash_walk_first(req, &walk); |
356 | if (!nbytes) | |
357 | return crypto_shash_final(desc, req->result); | |
358 | ||
359 | do { | |
66f6ce5e HX |
360 | nbytes = crypto_hash_walk_last(&walk) ? |
361 | crypto_shash_finup(desc, walk.data, nbytes, | |
362 | req->result) : | |
363 | crypto_shash_update(desc, walk.data, nbytes); | |
cbc86b91 HX |
364 | nbytes = crypto_hash_walk_done(&walk, nbytes); |
365 | } while (nbytes > 0); | |
66f6ce5e HX |
366 | |
367 | return nbytes; | |
368 | } | |
369 | EXPORT_SYMBOL_GPL(shash_ahash_finup); | |
370 | ||
371 | static int shash_async_finup(struct ahash_request *req) | |
372 | { | |
373 | struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req)); | |
374 | struct shash_desc *desc = ahash_request_ctx(req); | |
375 | ||
376 | desc->tfm = *ctx; | |
66f6ce5e HX |
377 | |
378 | return shash_ahash_finup(req, desc); | |
379 | } | |
380 | ||
7eddf95e | 381 | int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc) |
3b2f6df0 | 382 | { |
3b2f6df0 | 383 | unsigned int nbytes = req->nbytes; |
b61907bb HX |
384 | struct scatterlist *sg; |
385 | unsigned int offset; | |
3b2f6df0 HX |
386 | int err; |
387 | ||
b61907bb HX |
388 | if (nbytes && |
389 | (sg = req->src, offset = sg->offset, | |
67cb60e4 | 390 | nbytes <= min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset))) { |
3b2f6df0 HX |
391 | void *data; |
392 | ||
aa969515 | 393 | data = kmap_local_page(sg_page(sg)); |
3b2f6df0 HX |
394 | err = crypto_shash_digest(desc, data + offset, nbytes, |
395 | req->result); | |
aa969515 | 396 | kunmap_local(data); |
7eddf95e HX |
397 | } else |
398 | err = crypto_shash_init(desc) ?: | |
66f6ce5e | 399 | shash_ahash_finup(req, desc); |
3b2f6df0 | 400 | |
7eddf95e HX |
401 | return err; |
402 | } | |
403 | EXPORT_SYMBOL_GPL(shash_ahash_digest); | |
3b2f6df0 | 404 | |
7eddf95e HX |
405 | static int shash_async_digest(struct ahash_request *req) |
406 | { | |
407 | struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req)); | |
408 | struct shash_desc *desc = ahash_request_ctx(req); | |
3b2f6df0 | 409 | |
7eddf95e | 410 | desc->tfm = *ctx; |
3b2f6df0 | 411 | |
7eddf95e | 412 | return shash_ahash_digest(req, desc); |
3b2f6df0 HX |
413 | } |
414 | ||
66f6ce5e HX |
415 | static int shash_async_export(struct ahash_request *req, void *out) |
416 | { | |
417 | return crypto_shash_export(ahash_request_ctx(req), out); | |
418 | } | |
419 | ||
420 | static int shash_async_import(struct ahash_request *req, const void *in) | |
421 | { | |
90246e79 HX |
422 | struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req)); |
423 | struct shash_desc *desc = ahash_request_ctx(req); | |
424 | ||
425 | desc->tfm = *ctx; | |
90246e79 HX |
426 | |
427 | return crypto_shash_import(desc, in); | |
66f6ce5e HX |
428 | } |
429 | ||
3b2f6df0 HX |
430 | static void crypto_exit_shash_ops_async(struct crypto_tfm *tfm) |
431 | { | |
432 | struct crypto_shash **ctx = crypto_tfm_ctx(tfm); | |
433 | ||
434 | crypto_free_shash(*ctx); | |
435 | } | |
436 | ||
88056ec3 | 437 | int crypto_init_shash_ops_async(struct crypto_tfm *tfm) |
3b2f6df0 HX |
438 | { |
439 | struct crypto_alg *calg = tfm->__crt_alg; | |
66f6ce5e | 440 | struct shash_alg *alg = __crypto_shash_alg(calg); |
88056ec3 | 441 | struct crypto_ahash *crt = __crypto_ahash_cast(tfm); |
3b2f6df0 HX |
442 | struct crypto_shash **ctx = crypto_tfm_ctx(tfm); |
443 | struct crypto_shash *shash; | |
444 | ||
445 | if (!crypto_mod_get(calg)) | |
446 | return -EAGAIN; | |
447 | ||
3f683d61 | 448 | shash = crypto_create_tfm(calg, &crypto_shash_type); |
3b2f6df0 HX |
449 | if (IS_ERR(shash)) { |
450 | crypto_mod_put(calg); | |
451 | return PTR_ERR(shash); | |
452 | } | |
453 | ||
454 | *ctx = shash; | |
455 | tfm->exit = crypto_exit_shash_ops_async; | |
456 | ||
457 | crt->init = shash_async_init; | |
458 | crt->update = shash_async_update; | |
66f6ce5e HX |
459 | crt->final = shash_async_final; |
460 | crt->finup = shash_async_finup; | |
3b2f6df0 | 461 | crt->digest = shash_async_digest; |
ba7d7433 EB |
462 | if (crypto_shash_alg_has_setkey(alg)) |
463 | crt->setkey = shash_async_setkey; | |
00420a65 | 464 | |
9fa68f62 EB |
465 | crypto_ahash_set_flags(crt, crypto_shash_get_flags(shash) & |
466 | CRYPTO_TFM_NEED_KEY); | |
66f6ce5e | 467 | |
2b091e32 EB |
468 | crt->export = shash_async_export; |
469 | crt->import = shash_async_import; | |
3b2f6df0 | 470 | |
3b2f6df0 HX |
471 | crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash); |
472 | ||
473 | return 0; | |
474 | } | |
475 | ||
ed3630b8 HX |
476 | struct crypto_ahash *crypto_clone_shash_ops_async(struct crypto_ahash *nhash, |
477 | struct crypto_ahash *hash) | |
478 | { | |
479 | struct crypto_shash **nctx = crypto_ahash_ctx(nhash); | |
480 | struct crypto_shash **ctx = crypto_ahash_ctx(hash); | |
481 | struct crypto_shash *shash; | |
482 | ||
483 | shash = crypto_clone_shash(*ctx); | |
484 | if (IS_ERR(shash)) { | |
485 | crypto_free_ahash(nhash); | |
486 | return ERR_CAST(shash); | |
487 | } | |
488 | ||
489 | *nctx = shash; | |
490 | ||
491 | return nhash; | |
492 | } | |
493 | ||
fbce6be5 HX |
494 | static void crypto_shash_exit_tfm(struct crypto_tfm *tfm) |
495 | { | |
496 | struct crypto_shash *hash = __crypto_shash_cast(tfm); | |
497 | struct shash_alg *alg = crypto_shash_alg(hash); | |
498 | ||
499 | alg->exit_tfm(hash); | |
500 | } | |
501 | ||
2ca33da1 | 502 | static int crypto_shash_init_tfm(struct crypto_tfm *tfm) |
7b5a080b | 503 | { |
113adefc | 504 | struct crypto_shash *hash = __crypto_shash_cast(tfm); |
9fa68f62 | 505 | struct shash_alg *alg = crypto_shash_alg(hash); |
fbce6be5 | 506 | int err; |
9fa68f62 EB |
507 | |
508 | hash->descsize = alg->descsize; | |
509 | ||
ba7d7433 | 510 | shash_set_needkey(hash, alg); |
113adefc | 511 | |
fbce6be5 HX |
512 | if (alg->exit_tfm) |
513 | tfm->exit = crypto_shash_exit_tfm; | |
514 | ||
515 | if (!alg->init_tfm) | |
516 | return 0; | |
517 | ||
518 | err = alg->init_tfm(hash); | |
519 | if (err) | |
520 | return err; | |
521 | ||
522 | /* ->init_tfm() may have increased the descsize. */ | |
523 | if (WARN_ON_ONCE(hash->descsize > HASH_MAX_DESCSIZE)) { | |
524 | if (alg->exit_tfm) | |
525 | alg->exit_tfm(hash); | |
526 | return -EINVAL; | |
527 | } | |
528 | ||
7b5a080b HX |
529 | return 0; |
530 | } | |
531 | ||
48fb3e57 EB |
532 | static void crypto_shash_free_instance(struct crypto_instance *inst) |
533 | { | |
534 | struct shash_instance *shash = shash_instance(inst); | |
535 | ||
48fb3e57 EB |
536 | shash->free(shash); |
537 | } | |
538 | ||
c0f9e01d HX |
539 | static int __maybe_unused crypto_shash_report( |
540 | struct sk_buff *skb, struct crypto_alg *alg) | |
f4d663ce SK |
541 | { |
542 | struct crypto_report_hash rhash; | |
543 | struct shash_alg *salg = __crypto_shash_alg(alg); | |
544 | ||
37db69e0 EB |
545 | memset(&rhash, 0, sizeof(rhash)); |
546 | ||
547 | strscpy(rhash.type, "shash", sizeof(rhash.type)); | |
9a5467bf | 548 | |
f4d663ce SK |
549 | rhash.blocksize = alg->cra_blocksize; |
550 | rhash.digestsize = salg->digestsize; | |
551 | ||
37db69e0 | 552 | return nla_put(skb, CRYPTOCFGA_REPORT_HASH, sizeof(rhash), &rhash); |
f4d663ce SK |
553 | } |
554 | ||
7b5a080b | 555 | static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg) |
d8c34b94 | 556 | __maybe_unused; |
7b5a080b HX |
557 | static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg) |
558 | { | |
559 | struct shash_alg *salg = __crypto_shash_alg(alg); | |
560 | ||
561 | seq_printf(m, "type : shash\n"); | |
562 | seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); | |
563 | seq_printf(m, "digestsize : %u\n", salg->digestsize); | |
7b5a080b HX |
564 | } |
565 | ||
42808e5d HX |
566 | static int __maybe_unused crypto_shash_report_stat( |
567 | struct sk_buff *skb, struct crypto_alg *alg) | |
568 | { | |
569 | return crypto_hash_report_stat(skb, alg, "shash"); | |
570 | } | |
571 | ||
7b5a080b | 572 | static const struct crypto_type crypto_shash_type = { |
ac611680 | 573 | .extsize = crypto_alg_extsize, |
7b5a080b | 574 | .init_tfm = crypto_shash_init_tfm, |
48fb3e57 | 575 | .free = crypto_shash_free_instance, |
7b5a080b HX |
576 | #ifdef CONFIG_PROC_FS |
577 | .show = crypto_shash_show, | |
578 | #endif | |
b8969a1b | 579 | #if IS_ENABLED(CONFIG_CRYPTO_USER) |
f4d663ce | 580 | .report = crypto_shash_report, |
c0f9e01d | 581 | #endif |
42808e5d HX |
582 | #ifdef CONFIG_CRYPTO_STATS |
583 | .report_stat = crypto_shash_report_stat, | |
584 | #endif | |
7b5a080b HX |
585 | .maskclear = ~CRYPTO_ALG_TYPE_MASK, |
586 | .maskset = CRYPTO_ALG_TYPE_MASK, | |
587 | .type = CRYPTO_ALG_TYPE_SHASH, | |
588 | .tfmsize = offsetof(struct crypto_shash, base), | |
589 | }; | |
590 | ||
fdfad1ff EB |
591 | int crypto_grab_shash(struct crypto_shash_spawn *spawn, |
592 | struct crypto_instance *inst, | |
593 | const char *name, u32 type, u32 mask) | |
594 | { | |
595 | spawn->base.frontend = &crypto_shash_type; | |
596 | return crypto_grab_spawn(&spawn->base, inst, name, type, mask); | |
597 | } | |
598 | EXPORT_SYMBOL_GPL(crypto_grab_shash); | |
599 | ||
7b5a080b HX |
600 | struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type, |
601 | u32 mask) | |
602 | { | |
3f683d61 | 603 | return crypto_alloc_tfm(alg_name, &crypto_shash_type, type, mask); |
7b5a080b HX |
604 | } |
605 | EXPORT_SYMBOL_GPL(crypto_alloc_shash); | |
606 | ||
85cc4243 HR |
607 | int crypto_has_shash(const char *alg_name, u32 type, u32 mask) |
608 | { | |
609 | return crypto_type_has_alg(alg_name, &crypto_shash_type, type, mask); | |
610 | } | |
611 | EXPORT_SYMBOL_GPL(crypto_has_shash); | |
612 | ||
ed3630b8 HX |
613 | struct crypto_shash *crypto_clone_shash(struct crypto_shash *hash) |
614 | { | |
615 | struct crypto_tfm *tfm = crypto_shash_tfm(hash); | |
616 | struct shash_alg *alg = crypto_shash_alg(hash); | |
617 | struct crypto_shash *nhash; | |
618 | int err; | |
619 | ||
620 | if (!crypto_shash_alg_has_setkey(alg)) { | |
621 | tfm = crypto_tfm_get(tfm); | |
622 | if (IS_ERR(tfm)) | |
623 | return ERR_CAST(tfm); | |
624 | ||
625 | return hash; | |
626 | } | |
627 | ||
b7be31b0 | 628 | if (!alg->clone_tfm && (alg->init_tfm || alg->base.cra_init)) |
ed3630b8 HX |
629 | return ERR_PTR(-ENOSYS); |
630 | ||
631 | nhash = crypto_clone_tfm(&crypto_shash_type, tfm); | |
632 | if (IS_ERR(nhash)) | |
633 | return nhash; | |
634 | ||
635 | nhash->descsize = hash->descsize; | |
636 | ||
b7be31b0 HX |
637 | if (alg->clone_tfm) { |
638 | err = alg->clone_tfm(nhash, hash); | |
639 | if (err) { | |
640 | crypto_free_shash(nhash); | |
641 | return ERR_PTR(err); | |
642 | } | |
ed3630b8 HX |
643 | } |
644 | ||
645 | return nhash; | |
646 | } | |
647 | EXPORT_SYMBOL_GPL(crypto_clone_shash); | |
648 | ||
42808e5d | 649 | int hash_prepare_alg(struct hash_alg_common *alg) |
7b5a080b | 650 | { |
42808e5d | 651 | struct crypto_istat_hash *istat = hash_get_stat(alg); |
7b5a080b HX |
652 | struct crypto_alg *base = &alg->base; |
653 | ||
9697b328 | 654 | if (alg->digestsize > HASH_MAX_DIGESTSIZE) |
7b5a080b HX |
655 | return -EINVAL; |
656 | ||
42808e5d HX |
657 | base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK; |
658 | ||
659 | if (IS_ENABLED(CONFIG_CRYPTO_STATS)) | |
660 | memset(istat, 0, sizeof(*istat)); | |
661 | ||
662 | return 0; | |
663 | } | |
664 | ||
665 | static int shash_prepare_alg(struct shash_alg *alg) | |
666 | { | |
667 | struct crypto_alg *base = &alg->halg.base; | |
668 | int err; | |
669 | ||
670 | if (alg->descsize > HASH_MAX_DESCSIZE) | |
671 | return -EINVAL; | |
672 | ||
1c799571 HX |
673 | if (base->cra_alignmask > MAX_SHASH_ALIGNMASK) |
674 | return -EINVAL; | |
675 | ||
41a2e94f EB |
676 | if ((alg->export && !alg->import) || (alg->import && !alg->export)) |
677 | return -EINVAL; | |
678 | ||
42808e5d HX |
679 | err = hash_prepare_alg(&alg->halg); |
680 | if (err) | |
681 | return err; | |
682 | ||
7b5a080b | 683 | base->cra_type = &crypto_shash_type; |
7b5a080b | 684 | base->cra_flags |= CRYPTO_ALG_TYPE_SHASH; |
99d27e1c | 685 | |
08debaa5 EB |
686 | /* |
687 | * Handle missing optional functions. For each one we can either | |
688 | * install a default here, or we can leave the pointer as NULL and check | |
689 | * the pointer for NULL in crypto_shash_*(), avoiding an indirect call | |
690 | * when the default behavior is desired. For ->finup and ->digest we | |
691 | * install defaults, since for optimal performance algorithms should | |
692 | * implement these anyway. On the other hand, for ->import and | |
693 | * ->export the common case and best performance comes from the simple | |
694 | * memcpy of the shash_desc_ctx, so when those pointers are NULL we | |
695 | * leave them NULL and provide the memcpy with no indirect call. | |
696 | */ | |
8267adab | 697 | if (!alg->finup) |
313a4074 | 698 | alg->finup = shash_default_finup; |
8267adab | 699 | if (!alg->digest) |
313a4074 | 700 | alg->digest = shash_default_digest; |
08debaa5 | 701 | if (!alg->export) |
42808e5d | 702 | alg->halg.statesize = alg->descsize; |
57cfe44b HX |
703 | if (!alg->setkey) |
704 | alg->setkey = shash_no_setkey; | |
99d27e1c | 705 | |
619a6ebd HX |
706 | return 0; |
707 | } | |
708 | ||
709 | int crypto_register_shash(struct shash_alg *alg) | |
710 | { | |
711 | struct crypto_alg *base = &alg->base; | |
712 | int err; | |
713 | ||
714 | err = shash_prepare_alg(alg); | |
715 | if (err) | |
716 | return err; | |
7b5a080b HX |
717 | |
718 | return crypto_register_alg(base); | |
719 | } | |
720 | EXPORT_SYMBOL_GPL(crypto_register_shash); | |
721 | ||
c6d633a9 | 722 | void crypto_unregister_shash(struct shash_alg *alg) |
7b5a080b | 723 | { |
c6d633a9 | 724 | crypto_unregister_alg(&alg->base); |
7b5a080b HX |
725 | } |
726 | EXPORT_SYMBOL_GPL(crypto_unregister_shash); | |
727 | ||
50fc3e8d JK |
728 | int crypto_register_shashes(struct shash_alg *algs, int count) |
729 | { | |
730 | int i, ret; | |
731 | ||
732 | for (i = 0; i < count; i++) { | |
733 | ret = crypto_register_shash(&algs[i]); | |
734 | if (ret) | |
735 | goto err; | |
736 | } | |
737 | ||
738 | return 0; | |
739 | ||
740 | err: | |
741 | for (--i; i >= 0; --i) | |
742 | crypto_unregister_shash(&algs[i]); | |
743 | ||
744 | return ret; | |
745 | } | |
746 | EXPORT_SYMBOL_GPL(crypto_register_shashes); | |
747 | ||
c6d633a9 | 748 | void crypto_unregister_shashes(struct shash_alg *algs, int count) |
50fc3e8d | 749 | { |
c6d633a9 | 750 | int i; |
50fc3e8d | 751 | |
c6d633a9 EB |
752 | for (i = count - 1; i >= 0; --i) |
753 | crypto_unregister_shash(&algs[i]); | |
50fc3e8d JK |
754 | } |
755 | EXPORT_SYMBOL_GPL(crypto_unregister_shashes); | |
756 | ||
619a6ebd HX |
757 | int shash_register_instance(struct crypto_template *tmpl, |
758 | struct shash_instance *inst) | |
759 | { | |
760 | int err; | |
761 | ||
d4fdc2df EB |
762 | if (WARN_ON(!inst->free)) |
763 | return -EINVAL; | |
764 | ||
619a6ebd HX |
765 | err = shash_prepare_alg(&inst->alg); |
766 | if (err) | |
767 | return err; | |
768 | ||
769 | return crypto_register_instance(tmpl, shash_crypto_instance(inst)); | |
770 | } | |
771 | EXPORT_SYMBOL_GPL(shash_register_instance); | |
772 | ||
a39c66cc | 773 | void shash_free_singlespawn_instance(struct shash_instance *inst) |
2e4fddd8 | 774 | { |
a39c66cc EB |
775 | crypto_drop_spawn(shash_instance_ctx(inst)); |
776 | kfree(inst); | |
2e4fddd8 | 777 | } |
a39c66cc | 778 | EXPORT_SYMBOL_GPL(shash_free_singlespawn_instance); |
2e4fddd8 | 779 | |
7b5a080b HX |
780 | MODULE_LICENSE("GPL"); |
781 | MODULE_DESCRIPTION("Synchronous cryptographic hash type"); |