]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - crypto/shash.c
tools build: Use $(shell ) instead of `` to get embedded libperl's ccopts
[mirror_ubuntu-focal-kernel.git] / crypto / shash.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Synchronous Cryptographic Hash operations.
4 *
5 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
6 */
7
8 #include <crypto/scatterwalk.h>
9 #include <crypto/internal/hash.h>
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>
15 #include <linux/cryptouser.h>
16 #include <net/netlink.h>
17 #include <linux/compiler.h>
18
19 #include "internal.h"
20
21 static const struct crypto_type crypto_shash_type;
22
23 static int shash_no_setkey(struct crypto_shash *tfm, const u8 *key,
24 unsigned int keylen)
25 {
26 return -ENOSYS;
27 }
28
29 /*
30 * Check whether an shash algorithm has a setkey function.
31 *
32 * For CFI compatibility, this must not be an inline function. This is because
33 * when CFI is enabled, modules won't get the same address for shash_no_setkey
34 * (if it were exported, which inlining would require) as the core kernel will.
35 */
36 bool crypto_shash_alg_has_setkey(struct shash_alg *alg)
37 {
38 return alg->setkey != shash_no_setkey;
39 }
40 EXPORT_SYMBOL_GPL(crypto_shash_alg_has_setkey);
41
42 static int shash_setkey_unaligned(struct crypto_shash *tfm, const u8 *key,
43 unsigned int keylen)
44 {
45 struct shash_alg *shash = crypto_shash_alg(tfm);
46 unsigned long alignmask = crypto_shash_alignmask(tfm);
47 unsigned long absize;
48 u8 *buffer, *alignbuffer;
49 int err;
50
51 absize = keylen + (alignmask & ~(crypto_tfm_ctx_alignment() - 1));
52 buffer = kmalloc(absize, GFP_ATOMIC);
53 if (!buffer)
54 return -ENOMEM;
55
56 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
57 memcpy(alignbuffer, key, keylen);
58 err = shash->setkey(tfm, alignbuffer, keylen);
59 kzfree(buffer);
60 return err;
61 }
62
63 static void shash_set_needkey(struct crypto_shash *tfm, struct shash_alg *alg)
64 {
65 if (crypto_shash_alg_has_setkey(alg) &&
66 !(alg->base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY))
67 crypto_shash_set_flags(tfm, CRYPTO_TFM_NEED_KEY);
68 }
69
70 int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
71 unsigned int keylen)
72 {
73 struct shash_alg *shash = crypto_shash_alg(tfm);
74 unsigned long alignmask = crypto_shash_alignmask(tfm);
75 int err;
76
77 if ((unsigned long)key & alignmask)
78 err = shash_setkey_unaligned(tfm, key, keylen);
79 else
80 err = shash->setkey(tfm, key, keylen);
81
82 if (unlikely(err)) {
83 shash_set_needkey(tfm, shash);
84 return err;
85 }
86
87 crypto_shash_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
88 return 0;
89 }
90 EXPORT_SYMBOL_GPL(crypto_shash_setkey);
91
92 static int shash_update_unaligned(struct shash_desc *desc, const u8 *data,
93 unsigned int len)
94 {
95 struct crypto_shash *tfm = desc->tfm;
96 struct shash_alg *shash = crypto_shash_alg(tfm);
97 unsigned long alignmask = crypto_shash_alignmask(tfm);
98 unsigned int unaligned_len = alignmask + 1 -
99 ((unsigned long)data & alignmask);
100 /*
101 * We cannot count on __aligned() working for large values:
102 * https://patchwork.kernel.org/patch/9507697/
103 */
104 u8 ubuf[MAX_ALGAPI_ALIGNMASK * 2];
105 u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1);
106 int err;
107
108 if (WARN_ON(buf + unaligned_len > ubuf + sizeof(ubuf)))
109 return -EINVAL;
110
111 if (unaligned_len > len)
112 unaligned_len = len;
113
114 memcpy(buf, data, unaligned_len);
115 err = shash->update(desc, buf, unaligned_len);
116 memset(buf, 0, unaligned_len);
117
118 return err ?:
119 shash->update(desc, data + unaligned_len, len - unaligned_len);
120 }
121
122 int crypto_shash_update(struct shash_desc *desc, const u8 *data,
123 unsigned int len)
124 {
125 struct crypto_shash *tfm = desc->tfm;
126 struct shash_alg *shash = crypto_shash_alg(tfm);
127 unsigned long alignmask = crypto_shash_alignmask(tfm);
128
129 if ((unsigned long)data & alignmask)
130 return shash_update_unaligned(desc, data, len);
131
132 return shash->update(desc, data, len);
133 }
134 EXPORT_SYMBOL_GPL(crypto_shash_update);
135
136 static int shash_final_unaligned(struct shash_desc *desc, u8 *out)
137 {
138 struct crypto_shash *tfm = desc->tfm;
139 unsigned long alignmask = crypto_shash_alignmask(tfm);
140 struct shash_alg *shash = crypto_shash_alg(tfm);
141 unsigned int ds = crypto_shash_digestsize(tfm);
142 /*
143 * We cannot count on __aligned() working for large values:
144 * https://patchwork.kernel.org/patch/9507697/
145 */
146 u8 ubuf[MAX_ALGAPI_ALIGNMASK + HASH_MAX_DIGESTSIZE];
147 u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1);
148 int err;
149
150 if (WARN_ON(buf + ds > ubuf + sizeof(ubuf)))
151 return -EINVAL;
152
153 err = shash->final(desc, buf);
154 if (err)
155 goto out;
156
157 memcpy(out, buf, ds);
158
159 out:
160 memset(buf, 0, ds);
161 return err;
162 }
163
164 int crypto_shash_final(struct shash_desc *desc, u8 *out)
165 {
166 struct crypto_shash *tfm = desc->tfm;
167 struct shash_alg *shash = crypto_shash_alg(tfm);
168 unsigned long alignmask = crypto_shash_alignmask(tfm);
169
170 if ((unsigned long)out & alignmask)
171 return shash_final_unaligned(desc, out);
172
173 return shash->final(desc, out);
174 }
175 EXPORT_SYMBOL_GPL(crypto_shash_final);
176
177 static int shash_finup_unaligned(struct shash_desc *desc, const u8 *data,
178 unsigned int len, u8 *out)
179 {
180 return crypto_shash_update(desc, data, len) ?:
181 crypto_shash_final(desc, out);
182 }
183
184 int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
185 unsigned int len, u8 *out)
186 {
187 struct crypto_shash *tfm = desc->tfm;
188 struct shash_alg *shash = crypto_shash_alg(tfm);
189 unsigned long alignmask = crypto_shash_alignmask(tfm);
190
191 if (((unsigned long)data | (unsigned long)out) & alignmask)
192 return shash_finup_unaligned(desc, data, len, out);
193
194 return shash->finup(desc, data, len, out);
195 }
196 EXPORT_SYMBOL_GPL(crypto_shash_finup);
197
198 static int shash_digest_unaligned(struct shash_desc *desc, const u8 *data,
199 unsigned int len, u8 *out)
200 {
201 return crypto_shash_init(desc) ?:
202 crypto_shash_finup(desc, data, len, out);
203 }
204
205 int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
206 unsigned int len, u8 *out)
207 {
208 struct crypto_shash *tfm = desc->tfm;
209 struct shash_alg *shash = crypto_shash_alg(tfm);
210 unsigned long alignmask = crypto_shash_alignmask(tfm);
211
212 if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
213 return -ENOKEY;
214
215 if (((unsigned long)data | (unsigned long)out) & alignmask)
216 return shash_digest_unaligned(desc, data, len, out);
217
218 return shash->digest(desc, data, len, out);
219 }
220 EXPORT_SYMBOL_GPL(crypto_shash_digest);
221
222 static int shash_default_export(struct shash_desc *desc, void *out)
223 {
224 memcpy(out, shash_desc_ctx(desc), crypto_shash_descsize(desc->tfm));
225 return 0;
226 }
227
228 static int shash_default_import(struct shash_desc *desc, const void *in)
229 {
230 memcpy(shash_desc_ctx(desc), in, crypto_shash_descsize(desc->tfm));
231 return 0;
232 }
233
234 static int shash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
235 unsigned int keylen)
236 {
237 struct crypto_shash **ctx = crypto_ahash_ctx(tfm);
238
239 return crypto_shash_setkey(*ctx, key, keylen);
240 }
241
242 static int shash_async_init(struct ahash_request *req)
243 {
244 struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
245 struct shash_desc *desc = ahash_request_ctx(req);
246
247 desc->tfm = *ctx;
248
249 return crypto_shash_init(desc);
250 }
251
252 int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc)
253 {
254 struct crypto_hash_walk walk;
255 int nbytes;
256
257 for (nbytes = crypto_hash_walk_first(req, &walk); nbytes > 0;
258 nbytes = crypto_hash_walk_done(&walk, nbytes))
259 nbytes = crypto_shash_update(desc, walk.data, nbytes);
260
261 return nbytes;
262 }
263 EXPORT_SYMBOL_GPL(shash_ahash_update);
264
265 static int shash_async_update(struct ahash_request *req)
266 {
267 return shash_ahash_update(req, ahash_request_ctx(req));
268 }
269
270 static int shash_async_final(struct ahash_request *req)
271 {
272 return crypto_shash_final(ahash_request_ctx(req), req->result);
273 }
274
275 int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc)
276 {
277 struct crypto_hash_walk walk;
278 int nbytes;
279
280 nbytes = crypto_hash_walk_first(req, &walk);
281 if (!nbytes)
282 return crypto_shash_final(desc, req->result);
283
284 do {
285 nbytes = crypto_hash_walk_last(&walk) ?
286 crypto_shash_finup(desc, walk.data, nbytes,
287 req->result) :
288 crypto_shash_update(desc, walk.data, nbytes);
289 nbytes = crypto_hash_walk_done(&walk, nbytes);
290 } while (nbytes > 0);
291
292 return nbytes;
293 }
294 EXPORT_SYMBOL_GPL(shash_ahash_finup);
295
296 static int shash_async_finup(struct ahash_request *req)
297 {
298 struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
299 struct shash_desc *desc = ahash_request_ctx(req);
300
301 desc->tfm = *ctx;
302
303 return shash_ahash_finup(req, desc);
304 }
305
306 int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc)
307 {
308 unsigned int nbytes = req->nbytes;
309 struct scatterlist *sg;
310 unsigned int offset;
311 int err;
312
313 if (nbytes &&
314 (sg = req->src, offset = sg->offset,
315 nbytes <= min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset))) {
316 void *data;
317
318 data = kmap_atomic(sg_page(sg));
319 err = crypto_shash_digest(desc, data + offset, nbytes,
320 req->result);
321 kunmap_atomic(data);
322 } else
323 err = crypto_shash_init(desc) ?:
324 shash_ahash_finup(req, desc);
325
326 return err;
327 }
328 EXPORT_SYMBOL_GPL(shash_ahash_digest);
329
330 static int shash_async_digest(struct ahash_request *req)
331 {
332 struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
333 struct shash_desc *desc = ahash_request_ctx(req);
334
335 desc->tfm = *ctx;
336
337 return shash_ahash_digest(req, desc);
338 }
339
340 static int shash_async_export(struct ahash_request *req, void *out)
341 {
342 return crypto_shash_export(ahash_request_ctx(req), out);
343 }
344
345 static int shash_async_import(struct ahash_request *req, const void *in)
346 {
347 struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
348 struct shash_desc *desc = ahash_request_ctx(req);
349
350 desc->tfm = *ctx;
351
352 return crypto_shash_import(desc, in);
353 }
354
355 static void crypto_exit_shash_ops_async(struct crypto_tfm *tfm)
356 {
357 struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
358
359 crypto_free_shash(*ctx);
360 }
361
362 int crypto_init_shash_ops_async(struct crypto_tfm *tfm)
363 {
364 struct crypto_alg *calg = tfm->__crt_alg;
365 struct shash_alg *alg = __crypto_shash_alg(calg);
366 struct crypto_ahash *crt = __crypto_ahash_cast(tfm);
367 struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
368 struct crypto_shash *shash;
369
370 if (!crypto_mod_get(calg))
371 return -EAGAIN;
372
373 shash = crypto_create_tfm(calg, &crypto_shash_type);
374 if (IS_ERR(shash)) {
375 crypto_mod_put(calg);
376 return PTR_ERR(shash);
377 }
378
379 *ctx = shash;
380 tfm->exit = crypto_exit_shash_ops_async;
381
382 crt->init = shash_async_init;
383 crt->update = shash_async_update;
384 crt->final = shash_async_final;
385 crt->finup = shash_async_finup;
386 crt->digest = shash_async_digest;
387 if (crypto_shash_alg_has_setkey(alg))
388 crt->setkey = shash_async_setkey;
389
390 crypto_ahash_set_flags(crt, crypto_shash_get_flags(shash) &
391 CRYPTO_TFM_NEED_KEY);
392
393 crt->export = shash_async_export;
394 crt->import = shash_async_import;
395
396 crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash);
397
398 return 0;
399 }
400
401 static int crypto_shash_init_tfm(struct crypto_tfm *tfm)
402 {
403 struct crypto_shash *hash = __crypto_shash_cast(tfm);
404 struct shash_alg *alg = crypto_shash_alg(hash);
405
406 hash->descsize = alg->descsize;
407
408 shash_set_needkey(hash, alg);
409
410 return 0;
411 }
412
413 #ifdef CONFIG_NET
414 static int crypto_shash_report(struct sk_buff *skb, struct crypto_alg *alg)
415 {
416 struct crypto_report_hash rhash;
417 struct shash_alg *salg = __crypto_shash_alg(alg);
418
419 memset(&rhash, 0, sizeof(rhash));
420
421 strscpy(rhash.type, "shash", sizeof(rhash.type));
422
423 rhash.blocksize = alg->cra_blocksize;
424 rhash.digestsize = salg->digestsize;
425
426 return nla_put(skb, CRYPTOCFGA_REPORT_HASH, sizeof(rhash), &rhash);
427 }
428 #else
429 static int crypto_shash_report(struct sk_buff *skb, struct crypto_alg *alg)
430 {
431 return -ENOSYS;
432 }
433 #endif
434
435 static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
436 __maybe_unused;
437 static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
438 {
439 struct shash_alg *salg = __crypto_shash_alg(alg);
440
441 seq_printf(m, "type : shash\n");
442 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
443 seq_printf(m, "digestsize : %u\n", salg->digestsize);
444 }
445
446 static const struct crypto_type crypto_shash_type = {
447 .extsize = crypto_alg_extsize,
448 .init_tfm = crypto_shash_init_tfm,
449 #ifdef CONFIG_PROC_FS
450 .show = crypto_shash_show,
451 #endif
452 .report = crypto_shash_report,
453 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
454 .maskset = CRYPTO_ALG_TYPE_MASK,
455 .type = CRYPTO_ALG_TYPE_SHASH,
456 .tfmsize = offsetof(struct crypto_shash, base),
457 };
458
459 struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
460 u32 mask)
461 {
462 return crypto_alloc_tfm(alg_name, &crypto_shash_type, type, mask);
463 }
464 EXPORT_SYMBOL_GPL(crypto_alloc_shash);
465
466 static int shash_prepare_alg(struct shash_alg *alg)
467 {
468 struct crypto_alg *base = &alg->base;
469
470 if (alg->digestsize > HASH_MAX_DIGESTSIZE ||
471 alg->descsize > HASH_MAX_DESCSIZE ||
472 alg->statesize > HASH_MAX_STATESIZE)
473 return -EINVAL;
474
475 if ((alg->export && !alg->import) || (alg->import && !alg->export))
476 return -EINVAL;
477
478 base->cra_type = &crypto_shash_type;
479 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
480 base->cra_flags |= CRYPTO_ALG_TYPE_SHASH;
481
482 if (!alg->finup)
483 alg->finup = shash_finup_unaligned;
484 if (!alg->digest)
485 alg->digest = shash_digest_unaligned;
486 if (!alg->export) {
487 alg->export = shash_default_export;
488 alg->import = shash_default_import;
489 alg->statesize = alg->descsize;
490 }
491 if (!alg->setkey)
492 alg->setkey = shash_no_setkey;
493
494 return 0;
495 }
496
497 int crypto_register_shash(struct shash_alg *alg)
498 {
499 struct crypto_alg *base = &alg->base;
500 int err;
501
502 err = shash_prepare_alg(alg);
503 if (err)
504 return err;
505
506 return crypto_register_alg(base);
507 }
508 EXPORT_SYMBOL_GPL(crypto_register_shash);
509
510 int crypto_unregister_shash(struct shash_alg *alg)
511 {
512 return crypto_unregister_alg(&alg->base);
513 }
514 EXPORT_SYMBOL_GPL(crypto_unregister_shash);
515
516 int crypto_register_shashes(struct shash_alg *algs, int count)
517 {
518 int i, ret;
519
520 for (i = 0; i < count; i++) {
521 ret = crypto_register_shash(&algs[i]);
522 if (ret)
523 goto err;
524 }
525
526 return 0;
527
528 err:
529 for (--i; i >= 0; --i)
530 crypto_unregister_shash(&algs[i]);
531
532 return ret;
533 }
534 EXPORT_SYMBOL_GPL(crypto_register_shashes);
535
536 int crypto_unregister_shashes(struct shash_alg *algs, int count)
537 {
538 int i, ret;
539
540 for (i = count - 1; i >= 0; --i) {
541 ret = crypto_unregister_shash(&algs[i]);
542 if (ret)
543 pr_err("Failed to unregister %s %s: %d\n",
544 algs[i].base.cra_driver_name,
545 algs[i].base.cra_name, ret);
546 }
547
548 return 0;
549 }
550 EXPORT_SYMBOL_GPL(crypto_unregister_shashes);
551
552 int shash_register_instance(struct crypto_template *tmpl,
553 struct shash_instance *inst)
554 {
555 int err;
556
557 err = shash_prepare_alg(&inst->alg);
558 if (err)
559 return err;
560
561 return crypto_register_instance(tmpl, shash_crypto_instance(inst));
562 }
563 EXPORT_SYMBOL_GPL(shash_register_instance);
564
565 void shash_free_instance(struct crypto_instance *inst)
566 {
567 crypto_drop_spawn(crypto_instance_ctx(inst));
568 kfree(shash_instance(inst));
569 }
570 EXPORT_SYMBOL_GPL(shash_free_instance);
571
572 int crypto_init_shash_spawn(struct crypto_shash_spawn *spawn,
573 struct shash_alg *alg,
574 struct crypto_instance *inst)
575 {
576 return crypto_init_spawn2(&spawn->base, &alg->base, inst,
577 &crypto_shash_type);
578 }
579 EXPORT_SYMBOL_GPL(crypto_init_shash_spawn);
580
581 struct shash_alg *shash_attr_alg(struct rtattr *rta, u32 type, u32 mask)
582 {
583 struct crypto_alg *alg;
584
585 alg = crypto_attr_alg2(rta, &crypto_shash_type, type, mask);
586 return IS_ERR(alg) ? ERR_CAST(alg) :
587 container_of(alg, struct shash_alg, base);
588 }
589 EXPORT_SYMBOL_GPL(shash_attr_alg);
590
591 MODULE_LICENSE("GPL");
592 MODULE_DESCRIPTION("Synchronous cryptographic hash type");