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> | |
7b5a080b | 13 | #include <linux/seq_file.h> |
42808e5d | 14 | #include <linux/string.h> |
f4d663ce | 15 | #include <net/netlink.h> |
7b5a080b | 16 | |
42808e5d | 17 | #include "hash.h" |
3b2f6df0 | 18 | |
42808e5d HX |
19 | static inline struct crypto_istat_hash *shash_get_stat(struct shash_alg *alg) |
20 | { | |
21 | return hash_get_stat(&alg->halg); | |
22 | } | |
23 | ||
24 | static inline int crypto_shash_errstat(struct shash_alg *alg, int err) | |
25 | { | |
2f1f34c1 EB |
26 | if (!IS_ENABLED(CONFIG_CRYPTO_STATS)) |
27 | return err; | |
28 | ||
29 | if (err && err != -EINPROGRESS && err != -EBUSY) | |
30 | atomic64_inc(&shash_get_stat(alg)->err_cnt); | |
31 | ||
32 | return err; | |
42808e5d HX |
33 | } |
34 | ||
c060e16d EB |
35 | int shash_no_setkey(struct crypto_shash *tfm, const u8 *key, |
36 | unsigned int keylen) | |
57cfe44b HX |
37 | { |
38 | return -ENOSYS; | |
39 | } | |
c060e16d | 40 | EXPORT_SYMBOL_GPL(shash_no_setkey); |
57cfe44b | 41 | |
ba7d7433 EB |
42 | static void shash_set_needkey(struct crypto_shash *tfm, struct shash_alg *alg) |
43 | { | |
c2881789 | 44 | if (crypto_shash_alg_needs_key(alg)) |
ba7d7433 EB |
45 | crypto_shash_set_flags(tfm, CRYPTO_TFM_NEED_KEY); |
46 | } | |
47 | ||
7b5a080b HX |
48 | int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key, |
49 | unsigned int keylen) | |
50 | { | |
51 | struct shash_alg *shash = crypto_shash_alg(tfm); | |
9fa68f62 | 52 | int err; |
7b5a080b | 53 | |
345bfa3c | 54 | err = shash->setkey(tfm, key, keylen); |
ba7d7433 EB |
55 | if (unlikely(err)) { |
56 | shash_set_needkey(tfm, shash); | |
9fa68f62 | 57 | return err; |
ba7d7433 | 58 | } |
7b5a080b | 59 | |
9fa68f62 EB |
60 | crypto_shash_clear_flags(tfm, CRYPTO_TFM_NEED_KEY); |
61 | return 0; | |
7b5a080b HX |
62 | } |
63 | EXPORT_SYMBOL_GPL(crypto_shash_setkey); | |
64 | ||
7b5a080b HX |
65 | int crypto_shash_update(struct shash_desc *desc, const u8 *data, |
66 | unsigned int len) | |
67 | { | |
345bfa3c | 68 | struct shash_alg *shash = crypto_shash_alg(desc->tfm); |
42808e5d HX |
69 | int err; |
70 | ||
71 | if (IS_ENABLED(CONFIG_CRYPTO_STATS)) | |
72 | atomic64_add(len, &shash_get_stat(shash)->hash_tlen); | |
7b5a080b | 73 | |
345bfa3c | 74 | err = shash->update(desc, data, len); |
7b5a080b | 75 | |
42808e5d | 76 | return crypto_shash_errstat(shash, err); |
7b5a080b HX |
77 | } |
78 | EXPORT_SYMBOL_GPL(crypto_shash_update); | |
79 | ||
7b5a080b HX |
80 | int crypto_shash_final(struct shash_desc *desc, u8 *out) |
81 | { | |
345bfa3c | 82 | struct shash_alg *shash = crypto_shash_alg(desc->tfm); |
42808e5d HX |
83 | int err; |
84 | ||
85 | if (IS_ENABLED(CONFIG_CRYPTO_STATS)) | |
86 | atomic64_inc(&shash_get_stat(shash)->hash_cnt); | |
7b5a080b | 87 | |
345bfa3c | 88 | err = shash->final(desc, out); |
7b5a080b | 89 | |
42808e5d | 90 | return crypto_shash_errstat(shash, err); |
7b5a080b HX |
91 | } |
92 | EXPORT_SYMBOL_GPL(crypto_shash_final); | |
93 | ||
313a4074 EB |
94 | static int shash_default_finup(struct shash_desc *desc, const u8 *data, |
95 | unsigned int len, u8 *out) | |
96 | { | |
97 | struct shash_alg *shash = crypto_shash_alg(desc->tfm); | |
98 | ||
99 | return shash->update(desc, data, len) ?: | |
100 | shash->final(desc, out); | |
101 | } | |
102 | ||
7b5a080b HX |
103 | int crypto_shash_finup(struct shash_desc *desc, const u8 *data, |
104 | unsigned int len, u8 *out) | |
105 | { | |
106 | struct crypto_shash *tfm = desc->tfm; | |
107 | struct shash_alg *shash = crypto_shash_alg(tfm); | |
42808e5d HX |
108 | int err; |
109 | ||
110 | if (IS_ENABLED(CONFIG_CRYPTO_STATS)) { | |
111 | struct crypto_istat_hash *istat = shash_get_stat(shash); | |
112 | ||
113 | atomic64_inc(&istat->hash_cnt); | |
114 | atomic64_add(len, &istat->hash_tlen); | |
115 | } | |
7b5a080b | 116 | |
345bfa3c | 117 | err = shash->finup(desc, data, len, out); |
7b5a080b | 118 | |
42808e5d | 119 | return crypto_shash_errstat(shash, err); |
7b5a080b HX |
120 | } |
121 | EXPORT_SYMBOL_GPL(crypto_shash_finup); | |
122 | ||
313a4074 EB |
123 | static int shash_default_digest(struct shash_desc *desc, const u8 *data, |
124 | unsigned int len, u8 *out) | |
125 | { | |
126 | struct shash_alg *shash = crypto_shash_alg(desc->tfm); | |
127 | ||
128 | return shash->init(desc) ?: | |
129 | shash->finup(desc, data, len, out); | |
130 | } | |
131 | ||
7b5a080b HX |
132 | int crypto_shash_digest(struct shash_desc *desc, const u8 *data, |
133 | unsigned int len, u8 *out) | |
134 | { | |
135 | struct crypto_shash *tfm = desc->tfm; | |
136 | struct shash_alg *shash = crypto_shash_alg(tfm); | |
42808e5d | 137 | int err; |
7b5a080b | 138 | |
42808e5d HX |
139 | if (IS_ENABLED(CONFIG_CRYPTO_STATS)) { |
140 | struct crypto_istat_hash *istat = shash_get_stat(shash); | |
9fa68f62 | 141 | |
42808e5d HX |
142 | atomic64_inc(&istat->hash_cnt); |
143 | atomic64_add(len, &istat->hash_tlen); | |
144 | } | |
7b5a080b | 145 | |
42808e5d HX |
146 | if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) |
147 | err = -ENOKEY; | |
42808e5d HX |
148 | else |
149 | err = shash->digest(desc, data, len, out); | |
150 | ||
151 | return crypto_shash_errstat(shash, err); | |
7b5a080b HX |
152 | } |
153 | EXPORT_SYMBOL_GPL(crypto_shash_digest); | |
154 | ||
822a98b8 EB |
155 | int crypto_shash_tfm_digest(struct crypto_shash *tfm, const u8 *data, |
156 | unsigned int len, u8 *out) | |
157 | { | |
158 | SHASH_DESC_ON_STACK(desc, tfm); | |
159 | int err; | |
160 | ||
161 | desc->tfm = tfm; | |
162 | ||
163 | err = crypto_shash_digest(desc, data, len, out); | |
164 | ||
165 | shash_desc_zero(desc); | |
166 | ||
167 | return err; | |
168 | } | |
169 | EXPORT_SYMBOL_GPL(crypto_shash_tfm_digest); | |
170 | ||
08debaa5 | 171 | int crypto_shash_export(struct shash_desc *desc, void *out) |
dec8b786 | 172 | { |
08debaa5 EB |
173 | struct crypto_shash *tfm = desc->tfm; |
174 | struct shash_alg *shash = crypto_shash_alg(tfm); | |
175 | ||
176 | if (shash->export) | |
177 | return shash->export(desc, out); | |
178 | ||
179 | memcpy(out, shash_desc_ctx(desc), crypto_shash_descsize(tfm)); | |
f592682f | 180 | return 0; |
99d27e1c | 181 | } |
08debaa5 | 182 | EXPORT_SYMBOL_GPL(crypto_shash_export); |
dec8b786 | 183 | |
08debaa5 | 184 | int crypto_shash_import(struct shash_desc *desc, const void *in) |
99d27e1c | 185 | { |
08debaa5 EB |
186 | struct crypto_shash *tfm = desc->tfm; |
187 | struct shash_alg *shash = crypto_shash_alg(tfm); | |
188 | ||
189 | if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) | |
190 | return -ENOKEY; | |
191 | ||
192 | if (shash->import) | |
193 | return shash->import(desc, in); | |
194 | ||
195 | memcpy(shash_desc_ctx(desc), in, crypto_shash_descsize(tfm)); | |
f592682f | 196 | return 0; |
dec8b786 | 197 | } |
08debaa5 | 198 | EXPORT_SYMBOL_GPL(crypto_shash_import); |
dec8b786 | 199 | |
fbce6be5 HX |
200 | static void crypto_shash_exit_tfm(struct crypto_tfm *tfm) |
201 | { | |
202 | struct crypto_shash *hash = __crypto_shash_cast(tfm); | |
203 | struct shash_alg *alg = crypto_shash_alg(hash); | |
204 | ||
205 | alg->exit_tfm(hash); | |
206 | } | |
207 | ||
2ca33da1 | 208 | static int crypto_shash_init_tfm(struct crypto_tfm *tfm) |
7b5a080b | 209 | { |
113adefc | 210 | struct crypto_shash *hash = __crypto_shash_cast(tfm); |
9fa68f62 | 211 | struct shash_alg *alg = crypto_shash_alg(hash); |
fbce6be5 | 212 | int err; |
9fa68f62 EB |
213 | |
214 | hash->descsize = alg->descsize; | |
215 | ||
ba7d7433 | 216 | shash_set_needkey(hash, alg); |
113adefc | 217 | |
fbce6be5 HX |
218 | if (alg->exit_tfm) |
219 | tfm->exit = crypto_shash_exit_tfm; | |
220 | ||
221 | if (!alg->init_tfm) | |
222 | return 0; | |
223 | ||
224 | err = alg->init_tfm(hash); | |
225 | if (err) | |
226 | return err; | |
227 | ||
228 | /* ->init_tfm() may have increased the descsize. */ | |
229 | if (WARN_ON_ONCE(hash->descsize > HASH_MAX_DESCSIZE)) { | |
230 | if (alg->exit_tfm) | |
231 | alg->exit_tfm(hash); | |
232 | return -EINVAL; | |
233 | } | |
234 | ||
7b5a080b HX |
235 | return 0; |
236 | } | |
237 | ||
48fb3e57 EB |
238 | static void crypto_shash_free_instance(struct crypto_instance *inst) |
239 | { | |
240 | struct shash_instance *shash = shash_instance(inst); | |
241 | ||
48fb3e57 EB |
242 | shash->free(shash); |
243 | } | |
244 | ||
c0f9e01d HX |
245 | static int __maybe_unused crypto_shash_report( |
246 | struct sk_buff *skb, struct crypto_alg *alg) | |
f4d663ce SK |
247 | { |
248 | struct crypto_report_hash rhash; | |
249 | struct shash_alg *salg = __crypto_shash_alg(alg); | |
250 | ||
37db69e0 EB |
251 | memset(&rhash, 0, sizeof(rhash)); |
252 | ||
253 | strscpy(rhash.type, "shash", sizeof(rhash.type)); | |
9a5467bf | 254 | |
f4d663ce SK |
255 | rhash.blocksize = alg->cra_blocksize; |
256 | rhash.digestsize = salg->digestsize; | |
257 | ||
37db69e0 | 258 | return nla_put(skb, CRYPTOCFGA_REPORT_HASH, sizeof(rhash), &rhash); |
f4d663ce SK |
259 | } |
260 | ||
7b5a080b | 261 | static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg) |
d8c34b94 | 262 | __maybe_unused; |
7b5a080b HX |
263 | static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg) |
264 | { | |
265 | struct shash_alg *salg = __crypto_shash_alg(alg); | |
266 | ||
267 | seq_printf(m, "type : shash\n"); | |
268 | seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); | |
269 | seq_printf(m, "digestsize : %u\n", salg->digestsize); | |
7b5a080b HX |
270 | } |
271 | ||
42808e5d HX |
272 | static int __maybe_unused crypto_shash_report_stat( |
273 | struct sk_buff *skb, struct crypto_alg *alg) | |
274 | { | |
275 | return crypto_hash_report_stat(skb, alg, "shash"); | |
276 | } | |
277 | ||
ecf889b7 | 278 | const struct crypto_type crypto_shash_type = { |
ac611680 | 279 | .extsize = crypto_alg_extsize, |
7b5a080b | 280 | .init_tfm = crypto_shash_init_tfm, |
48fb3e57 | 281 | .free = crypto_shash_free_instance, |
7b5a080b HX |
282 | #ifdef CONFIG_PROC_FS |
283 | .show = crypto_shash_show, | |
284 | #endif | |
b8969a1b | 285 | #if IS_ENABLED(CONFIG_CRYPTO_USER) |
f4d663ce | 286 | .report = crypto_shash_report, |
c0f9e01d | 287 | #endif |
42808e5d HX |
288 | #ifdef CONFIG_CRYPTO_STATS |
289 | .report_stat = crypto_shash_report_stat, | |
290 | #endif | |
7b5a080b HX |
291 | .maskclear = ~CRYPTO_ALG_TYPE_MASK, |
292 | .maskset = CRYPTO_ALG_TYPE_MASK, | |
293 | .type = CRYPTO_ALG_TYPE_SHASH, | |
294 | .tfmsize = offsetof(struct crypto_shash, base), | |
295 | }; | |
296 | ||
fdfad1ff EB |
297 | int crypto_grab_shash(struct crypto_shash_spawn *spawn, |
298 | struct crypto_instance *inst, | |
299 | const char *name, u32 type, u32 mask) | |
300 | { | |
301 | spawn->base.frontend = &crypto_shash_type; | |
302 | return crypto_grab_spawn(&spawn->base, inst, name, type, mask); | |
303 | } | |
304 | EXPORT_SYMBOL_GPL(crypto_grab_shash); | |
305 | ||
7b5a080b HX |
306 | struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type, |
307 | u32 mask) | |
308 | { | |
3f683d61 | 309 | return crypto_alloc_tfm(alg_name, &crypto_shash_type, type, mask); |
7b5a080b HX |
310 | } |
311 | EXPORT_SYMBOL_GPL(crypto_alloc_shash); | |
312 | ||
85cc4243 HR |
313 | int crypto_has_shash(const char *alg_name, u32 type, u32 mask) |
314 | { | |
315 | return crypto_type_has_alg(alg_name, &crypto_shash_type, type, mask); | |
316 | } | |
317 | EXPORT_SYMBOL_GPL(crypto_has_shash); | |
318 | ||
ed3630b8 HX |
319 | struct crypto_shash *crypto_clone_shash(struct crypto_shash *hash) |
320 | { | |
321 | struct crypto_tfm *tfm = crypto_shash_tfm(hash); | |
322 | struct shash_alg *alg = crypto_shash_alg(hash); | |
323 | struct crypto_shash *nhash; | |
324 | int err; | |
325 | ||
326 | if (!crypto_shash_alg_has_setkey(alg)) { | |
327 | tfm = crypto_tfm_get(tfm); | |
328 | if (IS_ERR(tfm)) | |
329 | return ERR_CAST(tfm); | |
330 | ||
331 | return hash; | |
332 | } | |
333 | ||
b7be31b0 | 334 | if (!alg->clone_tfm && (alg->init_tfm || alg->base.cra_init)) |
ed3630b8 HX |
335 | return ERR_PTR(-ENOSYS); |
336 | ||
337 | nhash = crypto_clone_tfm(&crypto_shash_type, tfm); | |
338 | if (IS_ERR(nhash)) | |
339 | return nhash; | |
340 | ||
341 | nhash->descsize = hash->descsize; | |
342 | ||
b7be31b0 HX |
343 | if (alg->clone_tfm) { |
344 | err = alg->clone_tfm(nhash, hash); | |
345 | if (err) { | |
346 | crypto_free_shash(nhash); | |
347 | return ERR_PTR(err); | |
348 | } | |
ed3630b8 HX |
349 | } |
350 | ||
351 | return nhash; | |
352 | } | |
353 | EXPORT_SYMBOL_GPL(crypto_clone_shash); | |
354 | ||
42808e5d | 355 | int hash_prepare_alg(struct hash_alg_common *alg) |
7b5a080b | 356 | { |
42808e5d | 357 | struct crypto_istat_hash *istat = hash_get_stat(alg); |
7b5a080b HX |
358 | struct crypto_alg *base = &alg->base; |
359 | ||
9697b328 | 360 | if (alg->digestsize > HASH_MAX_DIGESTSIZE) |
7b5a080b HX |
361 | return -EINVAL; |
362 | ||
c626910f EB |
363 | /* alignmask is not useful for hashes, so it is not supported. */ |
364 | if (base->cra_alignmask) | |
365 | return -EINVAL; | |
366 | ||
42808e5d HX |
367 | base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK; |
368 | ||
369 | if (IS_ENABLED(CONFIG_CRYPTO_STATS)) | |
370 | memset(istat, 0, sizeof(*istat)); | |
371 | ||
372 | return 0; | |
373 | } | |
374 | ||
375 | static int shash_prepare_alg(struct shash_alg *alg) | |
376 | { | |
377 | struct crypto_alg *base = &alg->halg.base; | |
378 | int err; | |
379 | ||
380 | if (alg->descsize > HASH_MAX_DESCSIZE) | |
381 | return -EINVAL; | |
382 | ||
41a2e94f EB |
383 | if ((alg->export && !alg->import) || (alg->import && !alg->export)) |
384 | return -EINVAL; | |
385 | ||
42808e5d HX |
386 | err = hash_prepare_alg(&alg->halg); |
387 | if (err) | |
388 | return err; | |
389 | ||
7b5a080b | 390 | base->cra_type = &crypto_shash_type; |
7b5a080b | 391 | base->cra_flags |= CRYPTO_ALG_TYPE_SHASH; |
99d27e1c | 392 | |
08debaa5 EB |
393 | /* |
394 | * Handle missing optional functions. For each one we can either | |
395 | * install a default here, or we can leave the pointer as NULL and check | |
396 | * the pointer for NULL in crypto_shash_*(), avoiding an indirect call | |
397 | * when the default behavior is desired. For ->finup and ->digest we | |
398 | * install defaults, since for optimal performance algorithms should | |
399 | * implement these anyway. On the other hand, for ->import and | |
400 | * ->export the common case and best performance comes from the simple | |
401 | * memcpy of the shash_desc_ctx, so when those pointers are NULL we | |
402 | * leave them NULL and provide the memcpy with no indirect call. | |
403 | */ | |
8267adab | 404 | if (!alg->finup) |
313a4074 | 405 | alg->finup = shash_default_finup; |
8267adab | 406 | if (!alg->digest) |
313a4074 | 407 | alg->digest = shash_default_digest; |
08debaa5 | 408 | if (!alg->export) |
42808e5d | 409 | alg->halg.statesize = alg->descsize; |
57cfe44b HX |
410 | if (!alg->setkey) |
411 | alg->setkey = shash_no_setkey; | |
99d27e1c | 412 | |
619a6ebd HX |
413 | return 0; |
414 | } | |
415 | ||
416 | int crypto_register_shash(struct shash_alg *alg) | |
417 | { | |
418 | struct crypto_alg *base = &alg->base; | |
419 | int err; | |
420 | ||
421 | err = shash_prepare_alg(alg); | |
422 | if (err) | |
423 | return err; | |
7b5a080b HX |
424 | |
425 | return crypto_register_alg(base); | |
426 | } | |
427 | EXPORT_SYMBOL_GPL(crypto_register_shash); | |
428 | ||
c6d633a9 | 429 | void crypto_unregister_shash(struct shash_alg *alg) |
7b5a080b | 430 | { |
c6d633a9 | 431 | crypto_unregister_alg(&alg->base); |
7b5a080b HX |
432 | } |
433 | EXPORT_SYMBOL_GPL(crypto_unregister_shash); | |
434 | ||
50fc3e8d JK |
435 | int crypto_register_shashes(struct shash_alg *algs, int count) |
436 | { | |
437 | int i, ret; | |
438 | ||
439 | for (i = 0; i < count; i++) { | |
440 | ret = crypto_register_shash(&algs[i]); | |
441 | if (ret) | |
442 | goto err; | |
443 | } | |
444 | ||
445 | return 0; | |
446 | ||
447 | err: | |
448 | for (--i; i >= 0; --i) | |
449 | crypto_unregister_shash(&algs[i]); | |
450 | ||
451 | return ret; | |
452 | } | |
453 | EXPORT_SYMBOL_GPL(crypto_register_shashes); | |
454 | ||
c6d633a9 | 455 | void crypto_unregister_shashes(struct shash_alg *algs, int count) |
50fc3e8d | 456 | { |
c6d633a9 | 457 | int i; |
50fc3e8d | 458 | |
c6d633a9 EB |
459 | for (i = count - 1; i >= 0; --i) |
460 | crypto_unregister_shash(&algs[i]); | |
50fc3e8d JK |
461 | } |
462 | EXPORT_SYMBOL_GPL(crypto_unregister_shashes); | |
463 | ||
619a6ebd HX |
464 | int shash_register_instance(struct crypto_template *tmpl, |
465 | struct shash_instance *inst) | |
466 | { | |
467 | int err; | |
468 | ||
d4fdc2df EB |
469 | if (WARN_ON(!inst->free)) |
470 | return -EINVAL; | |
471 | ||
619a6ebd HX |
472 | err = shash_prepare_alg(&inst->alg); |
473 | if (err) | |
474 | return err; | |
475 | ||
476 | return crypto_register_instance(tmpl, shash_crypto_instance(inst)); | |
477 | } | |
478 | EXPORT_SYMBOL_GPL(shash_register_instance); | |
479 | ||
a39c66cc | 480 | void shash_free_singlespawn_instance(struct shash_instance *inst) |
2e4fddd8 | 481 | { |
a39c66cc EB |
482 | crypto_drop_spawn(shash_instance_ctx(inst)); |
483 | kfree(inst); | |
2e4fddd8 | 484 | } |
a39c66cc | 485 | EXPORT_SYMBOL_GPL(shash_free_singlespawn_instance); |
2e4fddd8 | 486 | |
7b5a080b HX |
487 | MODULE_LICENSE("GPL"); |
488 | MODULE_DESCRIPTION("Synchronous cryptographic hash type"); |