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 | ||
194 | int crypto_shash_finup(struct shash_desc *desc, const u8 *data, | |
195 | unsigned int len, u8 *out) | |
196 | { | |
197 | struct crypto_shash *tfm = desc->tfm; | |
198 | struct shash_alg *shash = crypto_shash_alg(tfm); | |
199 | unsigned long alignmask = crypto_shash_alignmask(tfm); | |
42808e5d HX |
200 | int err; |
201 | ||
202 | if (IS_ENABLED(CONFIG_CRYPTO_STATS)) { | |
203 | struct crypto_istat_hash *istat = shash_get_stat(shash); | |
204 | ||
205 | atomic64_inc(&istat->hash_cnt); | |
206 | atomic64_add(len, &istat->hash_tlen); | |
207 | } | |
7b5a080b | 208 | |
8267adab | 209 | if (((unsigned long)data | (unsigned long)out) & alignmask) |
42808e5d HX |
210 | err = shash_finup_unaligned(desc, data, len, out); |
211 | else | |
212 | err = shash->finup(desc, data, len, out); | |
213 | ||
7b5a080b | 214 | |
42808e5d | 215 | return crypto_shash_errstat(shash, err); |
7b5a080b HX |
216 | } |
217 | EXPORT_SYMBOL_GPL(crypto_shash_finup); | |
218 | ||
219 | static int shash_digest_unaligned(struct shash_desc *desc, const u8 *data, | |
220 | unsigned int len, u8 *out) | |
221 | { | |
222 | return crypto_shash_init(desc) ?: | |
42808e5d HX |
223 | shash_update_unaligned(desc, data, len) ?: |
224 | shash_final_unaligned(desc, out); | |
7b5a080b HX |
225 | } |
226 | ||
227 | int crypto_shash_digest(struct shash_desc *desc, const u8 *data, | |
228 | unsigned int len, u8 *out) | |
229 | { | |
230 | struct crypto_shash *tfm = desc->tfm; | |
231 | struct shash_alg *shash = crypto_shash_alg(tfm); | |
232 | unsigned long alignmask = crypto_shash_alignmask(tfm); | |
42808e5d | 233 | int err; |
7b5a080b | 234 | |
42808e5d HX |
235 | if (IS_ENABLED(CONFIG_CRYPTO_STATS)) { |
236 | struct crypto_istat_hash *istat = shash_get_stat(shash); | |
9fa68f62 | 237 | |
42808e5d HX |
238 | atomic64_inc(&istat->hash_cnt); |
239 | atomic64_add(len, &istat->hash_tlen); | |
240 | } | |
7b5a080b | 241 | |
42808e5d HX |
242 | if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) |
243 | err = -ENOKEY; | |
244 | else if (((unsigned long)data | (unsigned long)out) & alignmask) | |
245 | err = shash_digest_unaligned(desc, data, len, out); | |
246 | else | |
247 | err = shash->digest(desc, data, len, out); | |
248 | ||
249 | return crypto_shash_errstat(shash, err); | |
7b5a080b HX |
250 | } |
251 | EXPORT_SYMBOL_GPL(crypto_shash_digest); | |
252 | ||
822a98b8 EB |
253 | int crypto_shash_tfm_digest(struct crypto_shash *tfm, const u8 *data, |
254 | unsigned int len, u8 *out) | |
255 | { | |
256 | SHASH_DESC_ON_STACK(desc, tfm); | |
257 | int err; | |
258 | ||
259 | desc->tfm = tfm; | |
260 | ||
261 | err = crypto_shash_digest(desc, data, len, out); | |
262 | ||
263 | shash_desc_zero(desc); | |
264 | ||
265 | return err; | |
266 | } | |
267 | EXPORT_SYMBOL_GPL(crypto_shash_tfm_digest); | |
268 | ||
f592682f | 269 | static int shash_default_export(struct shash_desc *desc, void *out) |
dec8b786 | 270 | { |
f592682f HX |
271 | memcpy(out, shash_desc_ctx(desc), crypto_shash_descsize(desc->tfm)); |
272 | return 0; | |
99d27e1c | 273 | } |
dec8b786 | 274 | |
f592682f | 275 | static int shash_default_import(struct shash_desc *desc, const void *in) |
99d27e1c | 276 | { |
f592682f HX |
277 | memcpy(shash_desc_ctx(desc), in, crypto_shash_descsize(desc->tfm)); |
278 | return 0; | |
dec8b786 | 279 | } |
dec8b786 | 280 | |
3b2f6df0 HX |
281 | static int shash_async_setkey(struct crypto_ahash *tfm, const u8 *key, |
282 | unsigned int keylen) | |
283 | { | |
284 | struct crypto_shash **ctx = crypto_ahash_ctx(tfm); | |
285 | ||
286 | return crypto_shash_setkey(*ctx, key, keylen); | |
287 | } | |
288 | ||
289 | static int shash_async_init(struct ahash_request *req) | |
290 | { | |
291 | struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req)); | |
292 | struct shash_desc *desc = ahash_request_ctx(req); | |
293 | ||
294 | desc->tfm = *ctx; | |
3b2f6df0 HX |
295 | |
296 | return crypto_shash_init(desc); | |
297 | } | |
298 | ||
7eddf95e | 299 | int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc) |
3b2f6df0 | 300 | { |
3b2f6df0 HX |
301 | struct crypto_hash_walk walk; |
302 | int nbytes; | |
303 | ||
304 | for (nbytes = crypto_hash_walk_first(req, &walk); nbytes > 0; | |
305 | nbytes = crypto_hash_walk_done(&walk, nbytes)) | |
306 | nbytes = crypto_shash_update(desc, walk.data, nbytes); | |
307 | ||
308 | return nbytes; | |
309 | } | |
7eddf95e HX |
310 | EXPORT_SYMBOL_GPL(shash_ahash_update); |
311 | ||
312 | static int shash_async_update(struct ahash_request *req) | |
313 | { | |
314 | return shash_ahash_update(req, ahash_request_ctx(req)); | |
315 | } | |
3b2f6df0 HX |
316 | |
317 | static int shash_async_final(struct ahash_request *req) | |
318 | { | |
319 | return crypto_shash_final(ahash_request_ctx(req), req->result); | |
320 | } | |
321 | ||
66f6ce5e HX |
322 | int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc) |
323 | { | |
324 | struct crypto_hash_walk walk; | |
325 | int nbytes; | |
326 | ||
cbc86b91 HX |
327 | nbytes = crypto_hash_walk_first(req, &walk); |
328 | if (!nbytes) | |
329 | return crypto_shash_final(desc, req->result); | |
330 | ||
331 | do { | |
66f6ce5e HX |
332 | nbytes = crypto_hash_walk_last(&walk) ? |
333 | crypto_shash_finup(desc, walk.data, nbytes, | |
334 | req->result) : | |
335 | crypto_shash_update(desc, walk.data, nbytes); | |
cbc86b91 HX |
336 | nbytes = crypto_hash_walk_done(&walk, nbytes); |
337 | } while (nbytes > 0); | |
66f6ce5e HX |
338 | |
339 | return nbytes; | |
340 | } | |
341 | EXPORT_SYMBOL_GPL(shash_ahash_finup); | |
342 | ||
343 | static int shash_async_finup(struct ahash_request *req) | |
344 | { | |
345 | struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req)); | |
346 | struct shash_desc *desc = ahash_request_ctx(req); | |
347 | ||
348 | desc->tfm = *ctx; | |
66f6ce5e HX |
349 | |
350 | return shash_ahash_finup(req, desc); | |
351 | } | |
352 | ||
7eddf95e | 353 | int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc) |
3b2f6df0 | 354 | { |
3b2f6df0 | 355 | unsigned int nbytes = req->nbytes; |
b61907bb HX |
356 | struct scatterlist *sg; |
357 | unsigned int offset; | |
3b2f6df0 HX |
358 | int err; |
359 | ||
b61907bb HX |
360 | if (nbytes && |
361 | (sg = req->src, offset = sg->offset, | |
67cb60e4 | 362 | nbytes <= min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset))) { |
3b2f6df0 HX |
363 | void *data; |
364 | ||
aa969515 | 365 | data = kmap_local_page(sg_page(sg)); |
3b2f6df0 HX |
366 | err = crypto_shash_digest(desc, data + offset, nbytes, |
367 | req->result); | |
aa969515 | 368 | kunmap_local(data); |
7eddf95e HX |
369 | } else |
370 | err = crypto_shash_init(desc) ?: | |
66f6ce5e | 371 | shash_ahash_finup(req, desc); |
3b2f6df0 | 372 | |
7eddf95e HX |
373 | return err; |
374 | } | |
375 | EXPORT_SYMBOL_GPL(shash_ahash_digest); | |
3b2f6df0 | 376 | |
7eddf95e HX |
377 | static int shash_async_digest(struct ahash_request *req) |
378 | { | |
379 | struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req)); | |
380 | struct shash_desc *desc = ahash_request_ctx(req); | |
3b2f6df0 | 381 | |
7eddf95e | 382 | desc->tfm = *ctx; |
3b2f6df0 | 383 | |
7eddf95e | 384 | return shash_ahash_digest(req, desc); |
3b2f6df0 HX |
385 | } |
386 | ||
66f6ce5e HX |
387 | static int shash_async_export(struct ahash_request *req, void *out) |
388 | { | |
389 | return crypto_shash_export(ahash_request_ctx(req), out); | |
390 | } | |
391 | ||
392 | static int shash_async_import(struct ahash_request *req, const void *in) | |
393 | { | |
90246e79 HX |
394 | struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req)); |
395 | struct shash_desc *desc = ahash_request_ctx(req); | |
396 | ||
397 | desc->tfm = *ctx; | |
90246e79 HX |
398 | |
399 | return crypto_shash_import(desc, in); | |
66f6ce5e HX |
400 | } |
401 | ||
3b2f6df0 HX |
402 | static void crypto_exit_shash_ops_async(struct crypto_tfm *tfm) |
403 | { | |
404 | struct crypto_shash **ctx = crypto_tfm_ctx(tfm); | |
405 | ||
406 | crypto_free_shash(*ctx); | |
407 | } | |
408 | ||
88056ec3 | 409 | int crypto_init_shash_ops_async(struct crypto_tfm *tfm) |
3b2f6df0 HX |
410 | { |
411 | struct crypto_alg *calg = tfm->__crt_alg; | |
66f6ce5e | 412 | struct shash_alg *alg = __crypto_shash_alg(calg); |
88056ec3 | 413 | struct crypto_ahash *crt = __crypto_ahash_cast(tfm); |
3b2f6df0 HX |
414 | struct crypto_shash **ctx = crypto_tfm_ctx(tfm); |
415 | struct crypto_shash *shash; | |
416 | ||
417 | if (!crypto_mod_get(calg)) | |
418 | return -EAGAIN; | |
419 | ||
3f683d61 | 420 | shash = crypto_create_tfm(calg, &crypto_shash_type); |
3b2f6df0 HX |
421 | if (IS_ERR(shash)) { |
422 | crypto_mod_put(calg); | |
423 | return PTR_ERR(shash); | |
424 | } | |
425 | ||
426 | *ctx = shash; | |
427 | tfm->exit = crypto_exit_shash_ops_async; | |
428 | ||
429 | crt->init = shash_async_init; | |
430 | crt->update = shash_async_update; | |
66f6ce5e HX |
431 | crt->final = shash_async_final; |
432 | crt->finup = shash_async_finup; | |
3b2f6df0 | 433 | crt->digest = shash_async_digest; |
ba7d7433 EB |
434 | if (crypto_shash_alg_has_setkey(alg)) |
435 | crt->setkey = shash_async_setkey; | |
00420a65 | 436 | |
9fa68f62 EB |
437 | crypto_ahash_set_flags(crt, crypto_shash_get_flags(shash) & |
438 | CRYPTO_TFM_NEED_KEY); | |
66f6ce5e | 439 | |
2b091e32 EB |
440 | crt->export = shash_async_export; |
441 | crt->import = shash_async_import; | |
3b2f6df0 | 442 | |
3b2f6df0 HX |
443 | crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash); |
444 | ||
445 | return 0; | |
446 | } | |
447 | ||
fbce6be5 HX |
448 | static void crypto_shash_exit_tfm(struct crypto_tfm *tfm) |
449 | { | |
450 | struct crypto_shash *hash = __crypto_shash_cast(tfm); | |
451 | struct shash_alg *alg = crypto_shash_alg(hash); | |
452 | ||
453 | alg->exit_tfm(hash); | |
454 | } | |
455 | ||
2ca33da1 | 456 | static int crypto_shash_init_tfm(struct crypto_tfm *tfm) |
7b5a080b | 457 | { |
113adefc | 458 | struct crypto_shash *hash = __crypto_shash_cast(tfm); |
9fa68f62 | 459 | struct shash_alg *alg = crypto_shash_alg(hash); |
fbce6be5 | 460 | int err; |
9fa68f62 EB |
461 | |
462 | hash->descsize = alg->descsize; | |
463 | ||
ba7d7433 | 464 | shash_set_needkey(hash, alg); |
113adefc | 465 | |
fbce6be5 HX |
466 | if (alg->exit_tfm) |
467 | tfm->exit = crypto_shash_exit_tfm; | |
468 | ||
469 | if (!alg->init_tfm) | |
470 | return 0; | |
471 | ||
472 | err = alg->init_tfm(hash); | |
473 | if (err) | |
474 | return err; | |
475 | ||
476 | /* ->init_tfm() may have increased the descsize. */ | |
477 | if (WARN_ON_ONCE(hash->descsize > HASH_MAX_DESCSIZE)) { | |
478 | if (alg->exit_tfm) | |
479 | alg->exit_tfm(hash); | |
480 | return -EINVAL; | |
481 | } | |
482 | ||
7b5a080b HX |
483 | return 0; |
484 | } | |
485 | ||
48fb3e57 EB |
486 | static void crypto_shash_free_instance(struct crypto_instance *inst) |
487 | { | |
488 | struct shash_instance *shash = shash_instance(inst); | |
489 | ||
48fb3e57 EB |
490 | shash->free(shash); |
491 | } | |
492 | ||
3acc8473 | 493 | #ifdef CONFIG_NET |
f4d663ce SK |
494 | static int crypto_shash_report(struct sk_buff *skb, struct crypto_alg *alg) |
495 | { | |
496 | struct crypto_report_hash rhash; | |
497 | struct shash_alg *salg = __crypto_shash_alg(alg); | |
498 | ||
37db69e0 EB |
499 | memset(&rhash, 0, sizeof(rhash)); |
500 | ||
501 | strscpy(rhash.type, "shash", sizeof(rhash.type)); | |
9a5467bf | 502 | |
f4d663ce SK |
503 | rhash.blocksize = alg->cra_blocksize; |
504 | rhash.digestsize = salg->digestsize; | |
505 | ||
37db69e0 | 506 | return nla_put(skb, CRYPTOCFGA_REPORT_HASH, sizeof(rhash), &rhash); |
f4d663ce | 507 | } |
3acc8473 HX |
508 | #else |
509 | static int crypto_shash_report(struct sk_buff *skb, struct crypto_alg *alg) | |
510 | { | |
511 | return -ENOSYS; | |
512 | } | |
513 | #endif | |
f4d663ce | 514 | |
7b5a080b | 515 | static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg) |
d8c34b94 | 516 | __maybe_unused; |
7b5a080b HX |
517 | static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg) |
518 | { | |
519 | struct shash_alg *salg = __crypto_shash_alg(alg); | |
520 | ||
521 | seq_printf(m, "type : shash\n"); | |
522 | seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); | |
523 | seq_printf(m, "digestsize : %u\n", salg->digestsize); | |
7b5a080b HX |
524 | } |
525 | ||
42808e5d HX |
526 | static int __maybe_unused crypto_shash_report_stat( |
527 | struct sk_buff *skb, struct crypto_alg *alg) | |
528 | { | |
529 | return crypto_hash_report_stat(skb, alg, "shash"); | |
530 | } | |
531 | ||
7b5a080b | 532 | static const struct crypto_type crypto_shash_type = { |
ac611680 | 533 | .extsize = crypto_alg_extsize, |
7b5a080b | 534 | .init_tfm = crypto_shash_init_tfm, |
48fb3e57 | 535 | .free = crypto_shash_free_instance, |
7b5a080b HX |
536 | #ifdef CONFIG_PROC_FS |
537 | .show = crypto_shash_show, | |
538 | #endif | |
f4d663ce | 539 | .report = crypto_shash_report, |
42808e5d HX |
540 | #ifdef CONFIG_CRYPTO_STATS |
541 | .report_stat = crypto_shash_report_stat, | |
542 | #endif | |
7b5a080b HX |
543 | .maskclear = ~CRYPTO_ALG_TYPE_MASK, |
544 | .maskset = CRYPTO_ALG_TYPE_MASK, | |
545 | .type = CRYPTO_ALG_TYPE_SHASH, | |
546 | .tfmsize = offsetof(struct crypto_shash, base), | |
547 | }; | |
548 | ||
fdfad1ff EB |
549 | int crypto_grab_shash(struct crypto_shash_spawn *spawn, |
550 | struct crypto_instance *inst, | |
551 | const char *name, u32 type, u32 mask) | |
552 | { | |
553 | spawn->base.frontend = &crypto_shash_type; | |
554 | return crypto_grab_spawn(&spawn->base, inst, name, type, mask); | |
555 | } | |
556 | EXPORT_SYMBOL_GPL(crypto_grab_shash); | |
557 | ||
7b5a080b HX |
558 | struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type, |
559 | u32 mask) | |
560 | { | |
3f683d61 | 561 | return crypto_alloc_tfm(alg_name, &crypto_shash_type, type, mask); |
7b5a080b HX |
562 | } |
563 | EXPORT_SYMBOL_GPL(crypto_alloc_shash); | |
564 | ||
85cc4243 HR |
565 | int crypto_has_shash(const char *alg_name, u32 type, u32 mask) |
566 | { | |
567 | return crypto_type_has_alg(alg_name, &crypto_shash_type, type, mask); | |
568 | } | |
569 | EXPORT_SYMBOL_GPL(crypto_has_shash); | |
570 | ||
42808e5d | 571 | int hash_prepare_alg(struct hash_alg_common *alg) |
7b5a080b | 572 | { |
42808e5d | 573 | struct crypto_istat_hash *istat = hash_get_stat(alg); |
7b5a080b HX |
574 | struct crypto_alg *base = &alg->base; |
575 | ||
b68a7ec1 | 576 | if (alg->digestsize > HASH_MAX_DIGESTSIZE || |
b68a7ec1 | 577 | alg->statesize > HASH_MAX_STATESIZE) |
7b5a080b HX |
578 | return -EINVAL; |
579 | ||
42808e5d HX |
580 | base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK; |
581 | ||
582 | if (IS_ENABLED(CONFIG_CRYPTO_STATS)) | |
583 | memset(istat, 0, sizeof(*istat)); | |
584 | ||
585 | return 0; | |
586 | } | |
587 | ||
588 | static int shash_prepare_alg(struct shash_alg *alg) | |
589 | { | |
590 | struct crypto_alg *base = &alg->halg.base; | |
591 | int err; | |
592 | ||
593 | if (alg->descsize > HASH_MAX_DESCSIZE) | |
594 | return -EINVAL; | |
595 | ||
1c799571 HX |
596 | if (base->cra_alignmask > MAX_SHASH_ALIGNMASK) |
597 | return -EINVAL; | |
598 | ||
41a2e94f EB |
599 | if ((alg->export && !alg->import) || (alg->import && !alg->export)) |
600 | return -EINVAL; | |
601 | ||
42808e5d HX |
602 | err = hash_prepare_alg(&alg->halg); |
603 | if (err) | |
604 | return err; | |
605 | ||
7b5a080b | 606 | base->cra_type = &crypto_shash_type; |
7b5a080b | 607 | base->cra_flags |= CRYPTO_ALG_TYPE_SHASH; |
99d27e1c | 608 | |
8267adab HX |
609 | if (!alg->finup) |
610 | alg->finup = shash_finup_unaligned; | |
611 | if (!alg->digest) | |
612 | alg->digest = shash_digest_unaligned; | |
f592682f HX |
613 | if (!alg->export) { |
614 | alg->export = shash_default_export; | |
615 | alg->import = shash_default_import; | |
42808e5d | 616 | alg->halg.statesize = alg->descsize; |
f592682f | 617 | } |
57cfe44b HX |
618 | if (!alg->setkey) |
619 | alg->setkey = shash_no_setkey; | |
99d27e1c | 620 | |
619a6ebd HX |
621 | return 0; |
622 | } | |
623 | ||
624 | int crypto_register_shash(struct shash_alg *alg) | |
625 | { | |
626 | struct crypto_alg *base = &alg->base; | |
627 | int err; | |
628 | ||
629 | err = shash_prepare_alg(alg); | |
630 | if (err) | |
631 | return err; | |
7b5a080b HX |
632 | |
633 | return crypto_register_alg(base); | |
634 | } | |
635 | EXPORT_SYMBOL_GPL(crypto_register_shash); | |
636 | ||
c6d633a9 | 637 | void crypto_unregister_shash(struct shash_alg *alg) |
7b5a080b | 638 | { |
c6d633a9 | 639 | crypto_unregister_alg(&alg->base); |
7b5a080b HX |
640 | } |
641 | EXPORT_SYMBOL_GPL(crypto_unregister_shash); | |
642 | ||
50fc3e8d JK |
643 | int crypto_register_shashes(struct shash_alg *algs, int count) |
644 | { | |
645 | int i, ret; | |
646 | ||
647 | for (i = 0; i < count; i++) { | |
648 | ret = crypto_register_shash(&algs[i]); | |
649 | if (ret) | |
650 | goto err; | |
651 | } | |
652 | ||
653 | return 0; | |
654 | ||
655 | err: | |
656 | for (--i; i >= 0; --i) | |
657 | crypto_unregister_shash(&algs[i]); | |
658 | ||
659 | return ret; | |
660 | } | |
661 | EXPORT_SYMBOL_GPL(crypto_register_shashes); | |
662 | ||
c6d633a9 | 663 | void crypto_unregister_shashes(struct shash_alg *algs, int count) |
50fc3e8d | 664 | { |
c6d633a9 | 665 | int i; |
50fc3e8d | 666 | |
c6d633a9 EB |
667 | for (i = count - 1; i >= 0; --i) |
668 | crypto_unregister_shash(&algs[i]); | |
50fc3e8d JK |
669 | } |
670 | EXPORT_SYMBOL_GPL(crypto_unregister_shashes); | |
671 | ||
619a6ebd HX |
672 | int shash_register_instance(struct crypto_template *tmpl, |
673 | struct shash_instance *inst) | |
674 | { | |
675 | int err; | |
676 | ||
d4fdc2df EB |
677 | if (WARN_ON(!inst->free)) |
678 | return -EINVAL; | |
679 | ||
619a6ebd HX |
680 | err = shash_prepare_alg(&inst->alg); |
681 | if (err) | |
682 | return err; | |
683 | ||
684 | return crypto_register_instance(tmpl, shash_crypto_instance(inst)); | |
685 | } | |
686 | EXPORT_SYMBOL_GPL(shash_register_instance); | |
687 | ||
a39c66cc | 688 | void shash_free_singlespawn_instance(struct shash_instance *inst) |
2e4fddd8 | 689 | { |
a39c66cc EB |
690 | crypto_drop_spawn(shash_instance_ctx(inst)); |
691 | kfree(inst); | |
2e4fddd8 | 692 | } |
a39c66cc | 693 | EXPORT_SYMBOL_GPL(shash_free_singlespawn_instance); |
2e4fddd8 | 694 | |
7b5a080b HX |
695 | MODULE_LICENSE("GPL"); |
696 | MODULE_DESCRIPTION("Synchronous cryptographic hash type"); |