xref: /linux/crypto/shash.c (revision 714ca27e9bf4608fcb1f627cd5599441f448771e)
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 <linux/cryptouser.h>
10 #include <linux/err.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/seq_file.h>
14 #include <linux/string.h>
15 #include <net/netlink.h>
16 
17 #include "hash.h"
18 
19 static inline bool crypto_shash_block_only(struct crypto_shash *tfm)
20 {
21 	return crypto_shash_alg(tfm)->base.cra_flags &
22 	       CRYPTO_AHASH_ALG_BLOCK_ONLY;
23 }
24 
25 static inline bool crypto_shash_final_nonzero(struct crypto_shash *tfm)
26 {
27 	return crypto_shash_alg(tfm)->base.cra_flags &
28 	       CRYPTO_AHASH_ALG_FINAL_NONZERO;
29 }
30 
31 static inline bool crypto_shash_finup_max(struct crypto_shash *tfm)
32 {
33 	return crypto_shash_alg(tfm)->base.cra_flags &
34 	       CRYPTO_AHASH_ALG_FINUP_MAX;
35 }
36 
37 int shash_no_setkey(struct crypto_shash *tfm, const u8 *key,
38 		    unsigned int keylen)
39 {
40 	return -ENOSYS;
41 }
42 EXPORT_SYMBOL_GPL(shash_no_setkey);
43 
44 static void shash_set_needkey(struct crypto_shash *tfm, struct shash_alg *alg)
45 {
46 	if (crypto_shash_alg_needs_key(alg))
47 		crypto_shash_set_flags(tfm, CRYPTO_TFM_NEED_KEY);
48 }
49 
50 int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
51 			unsigned int keylen)
52 {
53 	struct shash_alg *shash = crypto_shash_alg(tfm);
54 	int err;
55 
56 	err = shash->setkey(tfm, key, keylen);
57 	if (unlikely(err)) {
58 		shash_set_needkey(tfm, shash);
59 		return err;
60 	}
61 
62 	crypto_shash_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
63 	return 0;
64 }
65 EXPORT_SYMBOL_GPL(crypto_shash_setkey);
66 
67 static int __crypto_shash_init(struct shash_desc *desc)
68 {
69 	struct crypto_shash *tfm = desc->tfm;
70 
71 	if (crypto_shash_block_only(tfm)) {
72 		u8 *buf = shash_desc_ctx(desc);
73 
74 		buf += crypto_shash_descsize(tfm) - 1;
75 		*buf = 0;
76 	}
77 
78 	return crypto_shash_alg(tfm)->init(desc);
79 }
80 
81 int crypto_shash_init(struct shash_desc *desc)
82 {
83 	if (crypto_shash_get_flags(desc->tfm) & CRYPTO_TFM_NEED_KEY)
84 		return -ENOKEY;
85 	return __crypto_shash_init(desc);
86 }
87 EXPORT_SYMBOL_GPL(crypto_shash_init);
88 
89 static int shash_default_finup(struct shash_desc *desc, const u8 *data,
90 			       unsigned int len, u8 *out)
91 {
92 	struct shash_alg *shash = crypto_shash_alg(desc->tfm);
93 
94 	return shash->update(desc, data, len) ?:
95 	       shash->final(desc, out);
96 }
97 
98 static int crypto_shash_op_and_zero(
99 	int (*op)(struct shash_desc *desc, const u8 *data,
100 		  unsigned int len, u8 *out),
101 	struct shash_desc *desc, const u8 *data, unsigned int len, u8 *out)
102 {
103 	int err;
104 
105 	err = op(desc, data, len, out);
106 	memset(shash_desc_ctx(desc), 0, crypto_shash_descsize(desc->tfm));
107 	return err;
108 }
109 
110 int crypto_shash_finup(struct shash_desc *restrict desc, const u8 *data,
111 		       unsigned int len, u8 *restrict out)
112 {
113 	struct crypto_shash *tfm = desc->tfm;
114 	u8 *blenp = shash_desc_ctx(desc);
115 	bool finup_max, nonzero;
116 	unsigned int bs;
117 	int err;
118 	u8 *buf;
119 
120 	if (!crypto_shash_block_only(tfm)) {
121 		if (out)
122 			goto finup;
123 		return crypto_shash_alg(tfm)->update(desc, data, len);
124 	}
125 
126 	finup_max = out && crypto_shash_finup_max(tfm);
127 
128 	/* Retain extra block for final nonzero algorithms. */
129 	nonzero = crypto_shash_final_nonzero(tfm);
130 
131 	/*
132 	 * The partial block buffer follows the algorithm desc context.
133 	 * The byte following that contains the length.
134 	 */
135 	blenp += crypto_shash_descsize(tfm) - 1;
136 	bs = crypto_shash_blocksize(tfm);
137 	buf = blenp - bs;
138 
139 	if (likely(!*blenp && finup_max))
140 		goto finup;
141 
142 	while ((*blenp + len) >= bs + nonzero) {
143 		unsigned int nbytes = len - nonzero;
144 		const u8 *src = data;
145 
146 		if (*blenp) {
147 			memcpy(buf + *blenp, data, bs - *blenp);
148 			nbytes = bs;
149 			src = buf;
150 		}
151 
152 		err = crypto_shash_alg(tfm)->update(desc, src, nbytes);
153 		if (err < 0)
154 			return err;
155 
156 		data += nbytes - err - *blenp;
157 		len -= nbytes - err - *blenp;
158 		*blenp = 0;
159 	}
160 
161 	if (*blenp || !out) {
162 		memcpy(buf + *blenp, data, len);
163 		*blenp += len;
164 		if (!out)
165 			return 0;
166 		data = buf;
167 		len = *blenp;
168 	}
169 
170 finup:
171 	return crypto_shash_op_and_zero(crypto_shash_alg(tfm)->finup, desc,
172 					data, len, out);
173 }
174 EXPORT_SYMBOL_GPL(crypto_shash_finup);
175 
176 static int shash_default_digest(struct shash_desc *desc, const u8 *data,
177 				unsigned int len, u8 *out)
178 {
179 	return __crypto_shash_init(desc) ?:
180 	       crypto_shash_finup(desc, data, len, out);
181 }
182 
183 int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
184 			unsigned int len, u8 *out)
185 {
186 	struct crypto_shash *tfm = desc->tfm;
187 
188 	if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
189 		return -ENOKEY;
190 
191 	return crypto_shash_op_and_zero(crypto_shash_alg(tfm)->digest, desc,
192 					data, len, out);
193 }
194 EXPORT_SYMBOL_GPL(crypto_shash_digest);
195 
196 int crypto_shash_tfm_digest(struct crypto_shash *tfm, const u8 *data,
197 			    unsigned int len, u8 *out)
198 {
199 	SHASH_DESC_ON_STACK(desc, tfm);
200 
201 	desc->tfm = tfm;
202 	return crypto_shash_digest(desc, data, len, out);
203 }
204 EXPORT_SYMBOL_GPL(crypto_shash_tfm_digest);
205 
206 int crypto_shash_export_core(struct shash_desc *desc, void *out)
207 {
208 	int (*export)(struct shash_desc *desc, void *out);
209 	struct crypto_shash *tfm = desc->tfm;
210 	u8 *buf = shash_desc_ctx(desc);
211 	unsigned int plen, ss;
212 
213 	plen = crypto_shash_blocksize(tfm) + 1;
214 	ss = crypto_shash_statesize(tfm);
215 	if (crypto_shash_block_only(tfm))
216 		ss -= plen;
217 	export = crypto_shash_alg(tfm)->export;
218 	if (!export) {
219 		memcpy(out, buf, ss);
220 		return 0;
221 	}
222 
223 	return export(desc, out);
224 }
225 EXPORT_SYMBOL_GPL(crypto_shash_export_core);
226 
227 int crypto_shash_export(struct shash_desc *desc, void *out)
228 {
229 	struct crypto_shash *tfm = desc->tfm;
230 
231 	if (crypto_shash_block_only(tfm)) {
232 		unsigned int plen = crypto_shash_blocksize(tfm) + 1;
233 		unsigned int descsize = crypto_shash_descsize(tfm);
234 		unsigned int ss = crypto_shash_statesize(tfm);
235 		u8 *buf = shash_desc_ctx(desc);
236 
237 		memcpy(out + ss - plen, buf + descsize - plen, plen);
238 	}
239 	return crypto_shash_export_core(desc, out);
240 }
241 EXPORT_SYMBOL_GPL(crypto_shash_export);
242 
243 int crypto_shash_import_core(struct shash_desc *desc, const void *in)
244 {
245 	int (*import)(struct shash_desc *desc, const void *in);
246 	struct crypto_shash *tfm = desc->tfm;
247 	unsigned int descsize, plen, ss;
248 	u8 *buf = shash_desc_ctx(desc);
249 
250 	if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
251 		return -ENOKEY;
252 
253 	plen = crypto_shash_blocksize(tfm) + 1;
254 	descsize = crypto_shash_descsize(tfm);
255 	ss = crypto_shash_statesize(tfm);
256 	buf[descsize - 1] = 0;
257 	if (crypto_shash_block_only(tfm))
258 		ss -= plen;
259 	import = crypto_shash_alg(tfm)->import;
260 	if (!import) {
261 		memcpy(buf, in, ss);
262 		return 0;
263 	}
264 
265 	return import(desc, in);
266 }
267 EXPORT_SYMBOL_GPL(crypto_shash_import_core);
268 
269 int crypto_shash_import(struct shash_desc *desc, const void *in)
270 {
271 	struct crypto_shash *tfm = desc->tfm;
272 	int err;
273 
274 	err = crypto_shash_import_core(desc, in);
275 	if (crypto_shash_block_only(tfm)) {
276 		unsigned int plen = crypto_shash_blocksize(tfm) + 1;
277 		unsigned int descsize = crypto_shash_descsize(tfm);
278 		unsigned int ss = crypto_shash_statesize(tfm);
279 		u8 *buf = shash_desc_ctx(desc);
280 
281 		memcpy(buf + descsize - plen, in + ss - plen, plen);
282 		if (buf[descsize - 1] >= plen)
283 			err = -EOVERFLOW;
284 	}
285 	return err;
286 }
287 EXPORT_SYMBOL_GPL(crypto_shash_import);
288 
289 static void crypto_shash_exit_tfm(struct crypto_tfm *tfm)
290 {
291 	struct crypto_shash *hash = __crypto_shash_cast(tfm);
292 	struct shash_alg *alg = crypto_shash_alg(hash);
293 
294 	alg->exit_tfm(hash);
295 }
296 
297 static int crypto_shash_init_tfm(struct crypto_tfm *tfm)
298 {
299 	struct crypto_shash *hash = __crypto_shash_cast(tfm);
300 	struct shash_alg *alg = crypto_shash_alg(hash);
301 
302 	shash_set_needkey(hash, alg);
303 
304 	if (alg->exit_tfm)
305 		tfm->exit = crypto_shash_exit_tfm;
306 
307 	if (!alg->init_tfm)
308 		return 0;
309 
310 	return alg->init_tfm(hash);
311 }
312 
313 static void crypto_shash_free_instance(struct crypto_instance *inst)
314 {
315 	struct shash_instance *shash = shash_instance(inst);
316 
317 	shash->free(shash);
318 }
319 
320 static int __maybe_unused crypto_shash_report(
321 	struct sk_buff *skb, struct crypto_alg *alg)
322 {
323 	struct crypto_report_hash rhash;
324 	struct shash_alg *salg = __crypto_shash_alg(alg);
325 
326 	memset(&rhash, 0, sizeof(rhash));
327 
328 	strscpy(rhash.type, "shash", sizeof(rhash.type));
329 
330 	rhash.blocksize = alg->cra_blocksize;
331 	rhash.digestsize = salg->digestsize;
332 
333 	return nla_put(skb, CRYPTOCFGA_REPORT_HASH, sizeof(rhash), &rhash);
334 }
335 
336 static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
337 	__maybe_unused;
338 static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
339 {
340 	struct shash_alg *salg = __crypto_shash_alg(alg);
341 
342 	seq_printf(m, "type         : shash\n");
343 	seq_printf(m, "blocksize    : %u\n", alg->cra_blocksize);
344 	seq_printf(m, "digestsize   : %u\n", salg->digestsize);
345 }
346 
347 const struct crypto_type crypto_shash_type = {
348 	.extsize = crypto_alg_extsize,
349 	.init_tfm = crypto_shash_init_tfm,
350 	.free = crypto_shash_free_instance,
351 #ifdef CONFIG_PROC_FS
352 	.show = crypto_shash_show,
353 #endif
354 #if IS_ENABLED(CONFIG_CRYPTO_USER)
355 	.report = crypto_shash_report,
356 #endif
357 	.maskclear = ~CRYPTO_ALG_TYPE_MASK,
358 	.maskset = CRYPTO_ALG_TYPE_MASK,
359 	.type = CRYPTO_ALG_TYPE_SHASH,
360 	.tfmsize = offsetof(struct crypto_shash, base),
361 	.algsize = offsetof(struct shash_alg, base),
362 };
363 
364 int crypto_grab_shash(struct crypto_shash_spawn *spawn,
365 		      struct crypto_instance *inst,
366 		      const char *name, u32 type, u32 mask)
367 {
368 	spawn->base.frontend = &crypto_shash_type;
369 	return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
370 }
371 EXPORT_SYMBOL_GPL(crypto_grab_shash);
372 
373 struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
374 					u32 mask)
375 {
376 	return crypto_alloc_tfm(alg_name, &crypto_shash_type, type, mask);
377 }
378 EXPORT_SYMBOL_GPL(crypto_alloc_shash);
379 
380 int crypto_has_shash(const char *alg_name, u32 type, u32 mask)
381 {
382 	return crypto_type_has_alg(alg_name, &crypto_shash_type, type, mask);
383 }
384 EXPORT_SYMBOL_GPL(crypto_has_shash);
385 
386 struct crypto_shash *crypto_clone_shash(struct crypto_shash *hash)
387 {
388 	struct crypto_tfm *tfm = crypto_shash_tfm(hash);
389 	struct shash_alg *alg = crypto_shash_alg(hash);
390 	struct crypto_shash *nhash;
391 	int err;
392 
393 	if (!crypto_shash_alg_has_setkey(alg)) {
394 		tfm = crypto_tfm_get(tfm);
395 		if (IS_ERR(tfm))
396 			return ERR_CAST(tfm);
397 
398 		return hash;
399 	}
400 
401 	if (!alg->clone_tfm && (alg->init_tfm || alg->base.cra_init))
402 		return ERR_PTR(-ENOSYS);
403 
404 	nhash = crypto_clone_tfm(&crypto_shash_type, tfm);
405 	if (IS_ERR(nhash))
406 		return nhash;
407 
408 	if (alg->clone_tfm) {
409 		err = alg->clone_tfm(nhash, hash);
410 		if (err) {
411 			crypto_free_shash(nhash);
412 			return ERR_PTR(err);
413 		}
414 	}
415 
416 	if (alg->exit_tfm)
417 		crypto_shash_tfm(nhash)->exit = crypto_shash_exit_tfm;
418 
419 	return nhash;
420 }
421 EXPORT_SYMBOL_GPL(crypto_clone_shash);
422 
423 int hash_prepare_alg(struct hash_alg_common *alg)
424 {
425 	struct crypto_alg *base = &alg->base;
426 
427 	if (alg->digestsize > HASH_MAX_DIGESTSIZE)
428 		return -EINVAL;
429 
430 	/* alignmask is not useful for hashes, so it is not supported. */
431 	if (base->cra_alignmask)
432 		return -EINVAL;
433 
434 	base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
435 
436 	return 0;
437 }
438 
439 static int shash_prepare_alg(struct shash_alg *alg)
440 {
441 	struct crypto_alg *base = &alg->halg.base;
442 	int err;
443 
444 	if ((alg->export && !alg->import) || (alg->import && !alg->export))
445 		return -EINVAL;
446 
447 	err = hash_prepare_alg(&alg->halg);
448 	if (err)
449 		return err;
450 
451 	base->cra_type = &crypto_shash_type;
452 	base->cra_flags |= CRYPTO_ALG_TYPE_SHASH;
453 	base->cra_flags |= CRYPTO_ALG_REQ_VIRT;
454 
455 	/*
456 	 * Handle missing optional functions.  For each one we can either
457 	 * install a default here, or we can leave the pointer as NULL and check
458 	 * the pointer for NULL in crypto_shash_*(), avoiding an indirect call
459 	 * when the default behavior is desired.  For ->finup and ->digest we
460 	 * install defaults, since for optimal performance algorithms should
461 	 * implement these anyway.  On the other hand, for ->import and
462 	 * ->export the common case and best performance comes from the simple
463 	 * memcpy of the shash_desc_ctx, so when those pointers are NULL we
464 	 * leave them NULL and provide the memcpy with no indirect call.
465 	 */
466 	if (!alg->finup)
467 		alg->finup = shash_default_finup;
468 	if (!alg->digest)
469 		alg->digest = shash_default_digest;
470 	if (!alg->export && !alg->halg.statesize)
471 		alg->halg.statesize = alg->descsize;
472 	if (!alg->setkey)
473 		alg->setkey = shash_no_setkey;
474 
475 	if (base->cra_flags & CRYPTO_AHASH_ALG_BLOCK_ONLY) {
476 		BUILD_BUG_ON(MAX_ALGAPI_BLOCKSIZE >= 256);
477 		alg->descsize += base->cra_blocksize + 1;
478 		alg->statesize += base->cra_blocksize + 1;
479 	}
480 
481 	if (alg->descsize > HASH_MAX_DESCSIZE)
482 		return -EINVAL;
483 	if (alg->statesize > HASH_MAX_STATESIZE)
484 		return -EINVAL;
485 
486 	return 0;
487 }
488 
489 int crypto_register_shash(struct shash_alg *alg)
490 {
491 	struct crypto_alg *base = &alg->base;
492 	int err;
493 
494 	err = shash_prepare_alg(alg);
495 	if (err)
496 		return err;
497 
498 	return crypto_register_alg(base);
499 }
500 EXPORT_SYMBOL_GPL(crypto_register_shash);
501 
502 void crypto_unregister_shash(struct shash_alg *alg)
503 {
504 	crypto_unregister_alg(&alg->base);
505 }
506 EXPORT_SYMBOL_GPL(crypto_unregister_shash);
507 
508 int crypto_register_shashes(struct shash_alg *algs, int count)
509 {
510 	int i, ret;
511 
512 	for (i = 0; i < count; i++) {
513 		ret = crypto_register_shash(&algs[i]);
514 		if (ret)
515 			goto err;
516 	}
517 
518 	return 0;
519 
520 err:
521 	for (--i; i >= 0; --i)
522 		crypto_unregister_shash(&algs[i]);
523 
524 	return ret;
525 }
526 EXPORT_SYMBOL_GPL(crypto_register_shashes);
527 
528 void crypto_unregister_shashes(struct shash_alg *algs, int count)
529 {
530 	int i;
531 
532 	for (i = count - 1; i >= 0; --i)
533 		crypto_unregister_shash(&algs[i]);
534 }
535 EXPORT_SYMBOL_GPL(crypto_unregister_shashes);
536 
537 int shash_register_instance(struct crypto_template *tmpl,
538 			    struct shash_instance *inst)
539 {
540 	int err;
541 
542 	if (WARN_ON(!inst->free))
543 		return -EINVAL;
544 
545 	err = shash_prepare_alg(&inst->alg);
546 	if (err)
547 		return err;
548 
549 	return crypto_register_instance(tmpl, shash_crypto_instance(inst));
550 }
551 EXPORT_SYMBOL_GPL(shash_register_instance);
552 
553 void shash_free_singlespawn_instance(struct shash_instance *inst)
554 {
555 	crypto_drop_spawn(shash_instance_ctx(inst));
556 	kfree(inst);
557 }
558 EXPORT_SYMBOL_GPL(shash_free_singlespawn_instance);
559 
560 MODULE_LICENSE("GPL");
561 MODULE_DESCRIPTION("Synchronous cryptographic hash type");
562