• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Synchronous Cryptographic Hash operations.
3  *
4  * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; either version 2 of the License, or (at your option)
9  * any later version.
10  *
11  */
12 
13 #include <crypto/scatterwalk.h>
14 #include <crypto/internal/hash.h>
15 #include <linux/err.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/seq_file.h>
20 #include <linux/cryptouser.h>
21 #include <net/netlink.h>
22 
23 #include "internal.h"
24 
25 static const struct crypto_type crypto_shash_type;
26 
shash_no_setkey(struct crypto_shash * tfm,const u8 * key,unsigned int keylen)27 static int shash_no_setkey(struct crypto_shash *tfm, const u8 *key,
28 			   unsigned int keylen)
29 {
30 	return -ENOSYS;
31 }
32 
33 /*
34  * Check whether an shash algorithm has a setkey function.
35  *
36  * For CFI compatibility, this must not be an inline function.  This is because
37  * when CFI is enabled, modules won't get the same address for shash_no_setkey
38  * (if it were exported, which inlining would require) as the core kernel will.
39  */
crypto_shash_alg_has_setkey(struct shash_alg * alg)40 bool crypto_shash_alg_has_setkey(struct shash_alg *alg)
41 {
42 	return alg->setkey != shash_no_setkey;
43 }
44 EXPORT_SYMBOL_GPL(crypto_shash_alg_has_setkey);
45 
shash_setkey_unaligned(struct crypto_shash * tfm,const u8 * key,unsigned int keylen)46 static int shash_setkey_unaligned(struct crypto_shash *tfm, const u8 *key,
47 				  unsigned int keylen)
48 {
49 	struct shash_alg *shash = crypto_shash_alg(tfm);
50 	unsigned long alignmask = crypto_shash_alignmask(tfm);
51 	unsigned long absize;
52 	u8 *buffer, *alignbuffer;
53 	int err;
54 
55 	absize = keylen + (alignmask & ~(crypto_tfm_ctx_alignment() - 1));
56 	buffer = kmalloc(absize, GFP_ATOMIC);
57 	if (!buffer)
58 		return -ENOMEM;
59 
60 	alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
61 	memcpy(alignbuffer, key, keylen);
62 	err = shash->setkey(tfm, alignbuffer, keylen);
63 	kzfree(buffer);
64 	return err;
65 }
66 
crypto_shash_setkey(struct crypto_shash * tfm,const u8 * key,unsigned int keylen)67 int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
68 			unsigned int keylen)
69 {
70 	struct shash_alg *shash = crypto_shash_alg(tfm);
71 	unsigned long alignmask = crypto_shash_alignmask(tfm);
72 
73 	if ((unsigned long)key & alignmask)
74 		return shash_setkey_unaligned(tfm, key, keylen);
75 
76 	return shash->setkey(tfm, key, keylen);
77 }
78 EXPORT_SYMBOL_GPL(crypto_shash_setkey);
79 
shash_align_buffer_size(unsigned len,unsigned long mask)80 static inline unsigned int shash_align_buffer_size(unsigned len,
81 						   unsigned long mask)
82 {
83 	typedef u8 __attribute__ ((aligned)) u8_aligned;
84 	return len + (mask & ~(__alignof__(u8_aligned) - 1));
85 }
86 
shash_update_unaligned(struct shash_desc * desc,const u8 * data,unsigned int len)87 static int shash_update_unaligned(struct shash_desc *desc, const u8 *data,
88 				  unsigned int len)
89 {
90 	struct crypto_shash *tfm = desc->tfm;
91 	struct shash_alg *shash = crypto_shash_alg(tfm);
92 	unsigned long alignmask = crypto_shash_alignmask(tfm);
93 	unsigned int unaligned_len = alignmask + 1 -
94 				     ((unsigned long)data & alignmask);
95 	u8 ubuf[shash_align_buffer_size(unaligned_len, alignmask)]
96 		__attribute__ ((aligned));
97 	u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1);
98 	int err;
99 
100 	if (unaligned_len > len)
101 		unaligned_len = len;
102 
103 	memcpy(buf, data, unaligned_len);
104 	err = shash->update(desc, buf, unaligned_len);
105 	memset(buf, 0, unaligned_len);
106 
107 	return err ?:
108 	       shash->update(desc, data + unaligned_len, len - unaligned_len);
109 }
110 
crypto_shash_update(struct shash_desc * desc,const u8 * data,unsigned int len)111 int crypto_shash_update(struct shash_desc *desc, const u8 *data,
112 			unsigned int len)
113 {
114 	struct crypto_shash *tfm = desc->tfm;
115 	struct shash_alg *shash = crypto_shash_alg(tfm);
116 	unsigned long alignmask = crypto_shash_alignmask(tfm);
117 
118 	if ((unsigned long)data & alignmask)
119 		return shash_update_unaligned(desc, data, len);
120 
121 	return shash->update(desc, data, len);
122 }
123 EXPORT_SYMBOL_GPL(crypto_shash_update);
124 
shash_final_unaligned(struct shash_desc * desc,u8 * out)125 static int shash_final_unaligned(struct shash_desc *desc, u8 *out)
126 {
127 	struct crypto_shash *tfm = desc->tfm;
128 	unsigned long alignmask = crypto_shash_alignmask(tfm);
129 	struct shash_alg *shash = crypto_shash_alg(tfm);
130 	unsigned int ds = crypto_shash_digestsize(tfm);
131 	u8 ubuf[shash_align_buffer_size(ds, alignmask)]
132 		__attribute__ ((aligned));
133 	u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1);
134 	int err;
135 
136 	err = shash->final(desc, buf);
137 	if (err)
138 		goto out;
139 
140 	memcpy(out, buf, ds);
141 
142 out:
143 	memset(buf, 0, ds);
144 	return err;
145 }
146 
crypto_shash_final(struct shash_desc * desc,u8 * out)147 int crypto_shash_final(struct shash_desc *desc, u8 *out)
148 {
149 	struct crypto_shash *tfm = desc->tfm;
150 	struct shash_alg *shash = crypto_shash_alg(tfm);
151 	unsigned long alignmask = crypto_shash_alignmask(tfm);
152 
153 	if ((unsigned long)out & alignmask)
154 		return shash_final_unaligned(desc, out);
155 
156 	return shash->final(desc, out);
157 }
158 EXPORT_SYMBOL_GPL(crypto_shash_final);
159 
shash_finup_unaligned(struct shash_desc * desc,const u8 * data,unsigned int len,u8 * out)160 static int shash_finup_unaligned(struct shash_desc *desc, const u8 *data,
161 				 unsigned int len, u8 *out)
162 {
163 	return crypto_shash_update(desc, data, len) ?:
164 	       crypto_shash_final(desc, out);
165 }
166 
crypto_shash_finup(struct shash_desc * desc,const u8 * data,unsigned int len,u8 * out)167 int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
168 		       unsigned int len, 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);
173 
174 	if (((unsigned long)data | (unsigned long)out) & alignmask)
175 		return shash_finup_unaligned(desc, data, len, out);
176 
177 	return shash->finup(desc, data, len, out);
178 }
179 EXPORT_SYMBOL_GPL(crypto_shash_finup);
180 
shash_digest_unaligned(struct shash_desc * desc,const u8 * data,unsigned int len,u8 * out)181 static int shash_digest_unaligned(struct shash_desc *desc, const u8 *data,
182 				  unsigned int len, u8 *out)
183 {
184 	return crypto_shash_init(desc) ?:
185 	       crypto_shash_finup(desc, data, len, out);
186 }
187 
crypto_shash_digest(struct shash_desc * desc,const u8 * data,unsigned int len,u8 * out)188 int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
189 			unsigned int len, u8 *out)
190 {
191 	struct crypto_shash *tfm = desc->tfm;
192 	struct shash_alg *shash = crypto_shash_alg(tfm);
193 	unsigned long alignmask = crypto_shash_alignmask(tfm);
194 
195 	if (((unsigned long)data | (unsigned long)out) & alignmask)
196 		return shash_digest_unaligned(desc, data, len, out);
197 
198 	return shash->digest(desc, data, len, out);
199 }
200 EXPORT_SYMBOL_GPL(crypto_shash_digest);
201 
shash_default_export(struct shash_desc * desc,void * out)202 static int shash_default_export(struct shash_desc *desc, void *out)
203 {
204 	memcpy(out, shash_desc_ctx(desc), crypto_shash_descsize(desc->tfm));
205 	return 0;
206 }
207 
shash_default_import(struct shash_desc * desc,const void * in)208 static int shash_default_import(struct shash_desc *desc, const void *in)
209 {
210 	memcpy(shash_desc_ctx(desc), in, crypto_shash_descsize(desc->tfm));
211 	return 0;
212 }
213 
shash_async_setkey(struct crypto_ahash * tfm,const u8 * key,unsigned int keylen)214 static int shash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
215 			      unsigned int keylen)
216 {
217 	struct crypto_shash **ctx = crypto_ahash_ctx(tfm);
218 
219 	return crypto_shash_setkey(*ctx, key, keylen);
220 }
221 
shash_async_init(struct ahash_request * req)222 static int shash_async_init(struct ahash_request *req)
223 {
224 	struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
225 	struct shash_desc *desc = ahash_request_ctx(req);
226 
227 	desc->tfm = *ctx;
228 	desc->flags = req->base.flags;
229 
230 	return crypto_shash_init(desc);
231 }
232 
shash_ahash_update(struct ahash_request * req,struct shash_desc * desc)233 int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc)
234 {
235 	struct crypto_hash_walk walk;
236 	int nbytes;
237 
238 	for (nbytes = crypto_hash_walk_first(req, &walk); nbytes > 0;
239 	     nbytes = crypto_hash_walk_done(&walk, nbytes))
240 		nbytes = crypto_shash_update(desc, walk.data, nbytes);
241 
242 	return nbytes;
243 }
244 EXPORT_SYMBOL_GPL(shash_ahash_update);
245 
shash_async_update(struct ahash_request * req)246 static int shash_async_update(struct ahash_request *req)
247 {
248 	return shash_ahash_update(req, ahash_request_ctx(req));
249 }
250 
shash_async_final(struct ahash_request * req)251 static int shash_async_final(struct ahash_request *req)
252 {
253 	return crypto_shash_final(ahash_request_ctx(req), req->result);
254 }
255 
shash_ahash_finup(struct ahash_request * req,struct shash_desc * desc)256 int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc)
257 {
258 	struct crypto_hash_walk walk;
259 	int nbytes;
260 
261 	nbytes = crypto_hash_walk_first(req, &walk);
262 	if (!nbytes)
263 		return crypto_shash_final(desc, req->result);
264 
265 	do {
266 		nbytes = crypto_hash_walk_last(&walk) ?
267 			 crypto_shash_finup(desc, walk.data, nbytes,
268 					    req->result) :
269 			 crypto_shash_update(desc, walk.data, nbytes);
270 		nbytes = crypto_hash_walk_done(&walk, nbytes);
271 	} while (nbytes > 0);
272 
273 	return nbytes;
274 }
275 EXPORT_SYMBOL_GPL(shash_ahash_finup);
276 
shash_async_finup(struct ahash_request * req)277 static int shash_async_finup(struct ahash_request *req)
278 {
279 	struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
280 	struct shash_desc *desc = ahash_request_ctx(req);
281 
282 	desc->tfm = *ctx;
283 	desc->flags = req->base.flags;
284 
285 	return shash_ahash_finup(req, desc);
286 }
287 
shash_ahash_digest(struct ahash_request * req,struct shash_desc * desc)288 int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc)
289 {
290 	unsigned int nbytes = req->nbytes;
291 	struct scatterlist *sg;
292 	unsigned int offset;
293 	int err;
294 
295 	if (nbytes &&
296 	    (sg = req->src, offset = sg->offset,
297 	     nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset))) {
298 		void *data;
299 
300 		data = kmap_atomic(sg_page(sg));
301 		err = crypto_shash_digest(desc, data + offset, nbytes,
302 					  req->result);
303 		kunmap_atomic(data);
304 		crypto_yield(desc->flags);
305 	} else
306 		err = crypto_shash_init(desc) ?:
307 		      shash_ahash_finup(req, desc);
308 
309 	return err;
310 }
311 EXPORT_SYMBOL_GPL(shash_ahash_digest);
312 
shash_async_digest(struct ahash_request * req)313 static int shash_async_digest(struct ahash_request *req)
314 {
315 	struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
316 	struct shash_desc *desc = ahash_request_ctx(req);
317 
318 	desc->tfm = *ctx;
319 	desc->flags = req->base.flags;
320 
321 	return shash_ahash_digest(req, desc);
322 }
323 
shash_async_export(struct ahash_request * req,void * out)324 static int shash_async_export(struct ahash_request *req, void *out)
325 {
326 	return crypto_shash_export(ahash_request_ctx(req), out);
327 }
328 
shash_async_import(struct ahash_request * req,const void * in)329 static int shash_async_import(struct ahash_request *req, const void *in)
330 {
331 	struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
332 	struct shash_desc *desc = ahash_request_ctx(req);
333 
334 	desc->tfm = *ctx;
335 	desc->flags = req->base.flags;
336 
337 	return crypto_shash_import(desc, in);
338 }
339 
crypto_exit_shash_ops_async(struct crypto_tfm * tfm)340 static void crypto_exit_shash_ops_async(struct crypto_tfm *tfm)
341 {
342 	struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
343 
344 	crypto_free_shash(*ctx);
345 }
346 
crypto_init_shash_ops_async(struct crypto_tfm * tfm)347 int crypto_init_shash_ops_async(struct crypto_tfm *tfm)
348 {
349 	struct crypto_alg *calg = tfm->__crt_alg;
350 	struct shash_alg *alg = __crypto_shash_alg(calg);
351 	struct crypto_ahash *crt = __crypto_ahash_cast(tfm);
352 	struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
353 	struct crypto_shash *shash;
354 
355 	if (!crypto_mod_get(calg))
356 		return -EAGAIN;
357 
358 	shash = crypto_create_tfm(calg, &crypto_shash_type);
359 	if (IS_ERR(shash)) {
360 		crypto_mod_put(calg);
361 		return PTR_ERR(shash);
362 	}
363 
364 	*ctx = shash;
365 	tfm->exit = crypto_exit_shash_ops_async;
366 
367 	crt->init = shash_async_init;
368 	crt->update = shash_async_update;
369 	crt->final = shash_async_final;
370 	crt->finup = shash_async_finup;
371 	crt->digest = shash_async_digest;
372 	crt->setkey = shash_async_setkey;
373 
374 	crt->has_setkey = alg->setkey != shash_no_setkey;
375 
376 	if (alg->export)
377 		crt->export = shash_async_export;
378 	if (alg->import)
379 		crt->import = shash_async_import;
380 
381 	crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash);
382 
383 	return 0;
384 }
385 
shash_compat_setkey(struct crypto_hash * tfm,const u8 * key,unsigned int keylen)386 static int shash_compat_setkey(struct crypto_hash *tfm, const u8 *key,
387 			       unsigned int keylen)
388 {
389 	struct shash_desc **descp = crypto_hash_ctx(tfm);
390 	struct shash_desc *desc = *descp;
391 
392 	return crypto_shash_setkey(desc->tfm, key, keylen);
393 }
394 
shash_compat_init(struct hash_desc * hdesc)395 static int shash_compat_init(struct hash_desc *hdesc)
396 {
397 	struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm);
398 	struct shash_desc *desc = *descp;
399 
400 	desc->flags = hdesc->flags;
401 
402 	return crypto_shash_init(desc);
403 }
404 
shash_compat_update(struct hash_desc * hdesc,struct scatterlist * sg,unsigned int len)405 static int shash_compat_update(struct hash_desc *hdesc, struct scatterlist *sg,
406 			       unsigned int len)
407 {
408 	struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm);
409 	struct shash_desc *desc = *descp;
410 	struct crypto_hash_walk walk;
411 	int nbytes;
412 
413 	for (nbytes = crypto_hash_walk_first_compat(hdesc, &walk, sg, len);
414 	     nbytes > 0; nbytes = crypto_hash_walk_done(&walk, nbytes))
415 		nbytes = crypto_shash_update(desc, walk.data, nbytes);
416 
417 	return nbytes;
418 }
419 
shash_compat_final(struct hash_desc * hdesc,u8 * out)420 static int shash_compat_final(struct hash_desc *hdesc, u8 *out)
421 {
422 	struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm);
423 
424 	return crypto_shash_final(*descp, out);
425 }
426 
shash_compat_digest(struct hash_desc * hdesc,struct scatterlist * sg,unsigned int nbytes,u8 * out)427 static int shash_compat_digest(struct hash_desc *hdesc, struct scatterlist *sg,
428 			       unsigned int nbytes, u8 *out)
429 {
430 	unsigned int offset = sg->offset;
431 	int err;
432 
433 	if (nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset)) {
434 		struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm);
435 		struct shash_desc *desc = *descp;
436 		void *data;
437 
438 		desc->flags = hdesc->flags;
439 
440 		data = kmap_atomic(sg_page(sg));
441 		err = crypto_shash_digest(desc, data + offset, nbytes, out);
442 		kunmap_atomic(data);
443 		crypto_yield(desc->flags);
444 		goto out;
445 	}
446 
447 	err = shash_compat_init(hdesc);
448 	if (err)
449 		goto out;
450 
451 	err = shash_compat_update(hdesc, sg, nbytes);
452 	if (err)
453 		goto out;
454 
455 	err = shash_compat_final(hdesc, out);
456 
457 out:
458 	return err;
459 }
460 
crypto_exit_shash_ops_compat(struct crypto_tfm * tfm)461 static void crypto_exit_shash_ops_compat(struct crypto_tfm *tfm)
462 {
463 	struct shash_desc **descp = crypto_tfm_ctx(tfm);
464 	struct shash_desc *desc = *descp;
465 
466 	crypto_free_shash(desc->tfm);
467 	kzfree(desc);
468 }
469 
crypto_init_shash_ops_compat(struct crypto_tfm * tfm)470 static int crypto_init_shash_ops_compat(struct crypto_tfm *tfm)
471 {
472 	struct hash_tfm *crt = &tfm->crt_hash;
473 	struct crypto_alg *calg = tfm->__crt_alg;
474 	struct shash_alg *alg = __crypto_shash_alg(calg);
475 	struct shash_desc **descp = crypto_tfm_ctx(tfm);
476 	struct crypto_shash *shash;
477 	struct shash_desc *desc;
478 
479 	if (!crypto_mod_get(calg))
480 		return -EAGAIN;
481 
482 	shash = crypto_create_tfm(calg, &crypto_shash_type);
483 	if (IS_ERR(shash)) {
484 		crypto_mod_put(calg);
485 		return PTR_ERR(shash);
486 	}
487 
488 	desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(shash),
489 		       GFP_KERNEL);
490 	if (!desc) {
491 		crypto_free_shash(shash);
492 		return -ENOMEM;
493 	}
494 
495 	*descp = desc;
496 	desc->tfm = shash;
497 	tfm->exit = crypto_exit_shash_ops_compat;
498 
499 	crt->init = shash_compat_init;
500 	crt->update = shash_compat_update;
501 	crt->final  = shash_compat_final;
502 	crt->digest = shash_compat_digest;
503 	crt->setkey = shash_compat_setkey;
504 
505 	crt->digestsize = alg->digestsize;
506 
507 	return 0;
508 }
509 
crypto_init_shash_ops(struct crypto_tfm * tfm,u32 type,u32 mask)510 static int crypto_init_shash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
511 {
512 	switch (mask & CRYPTO_ALG_TYPE_MASK) {
513 	case CRYPTO_ALG_TYPE_HASH_MASK:
514 		return crypto_init_shash_ops_compat(tfm);
515 	}
516 
517 	return -EINVAL;
518 }
519 
crypto_shash_ctxsize(struct crypto_alg * alg,u32 type,u32 mask)520 static unsigned int crypto_shash_ctxsize(struct crypto_alg *alg, u32 type,
521 					 u32 mask)
522 {
523 	switch (mask & CRYPTO_ALG_TYPE_MASK) {
524 	case CRYPTO_ALG_TYPE_HASH_MASK:
525 		return sizeof(struct shash_desc *);
526 	}
527 
528 	return 0;
529 }
530 
crypto_shash_init_tfm(struct crypto_tfm * tfm)531 static int crypto_shash_init_tfm(struct crypto_tfm *tfm)
532 {
533 	struct crypto_shash *hash = __crypto_shash_cast(tfm);
534 
535 	hash->descsize = crypto_shash_alg(hash)->descsize;
536 	return 0;
537 }
538 
539 #ifdef CONFIG_NET
crypto_shash_report(struct sk_buff * skb,struct crypto_alg * alg)540 static int crypto_shash_report(struct sk_buff *skb, struct crypto_alg *alg)
541 {
542 	struct crypto_report_hash rhash;
543 	struct shash_alg *salg = __crypto_shash_alg(alg);
544 
545 	strncpy(rhash.type, "shash", sizeof(rhash.type));
546 
547 	rhash.blocksize = alg->cra_blocksize;
548 	rhash.digestsize = salg->digestsize;
549 
550 	if (nla_put(skb, CRYPTOCFGA_REPORT_HASH,
551 		    sizeof(struct crypto_report_hash), &rhash))
552 		goto nla_put_failure;
553 	return 0;
554 
555 nla_put_failure:
556 	return -EMSGSIZE;
557 }
558 #else
crypto_shash_report(struct sk_buff * skb,struct crypto_alg * alg)559 static int crypto_shash_report(struct sk_buff *skb, struct crypto_alg *alg)
560 {
561 	return -ENOSYS;
562 }
563 #endif
564 
565 static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
566 	__attribute__ ((unused));
crypto_shash_show(struct seq_file * m,struct crypto_alg * alg)567 static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
568 {
569 	struct shash_alg *salg = __crypto_shash_alg(alg);
570 
571 	seq_printf(m, "type         : shash\n");
572 	seq_printf(m, "blocksize    : %u\n", alg->cra_blocksize);
573 	seq_printf(m, "digestsize   : %u\n", salg->digestsize);
574 }
575 
576 static const struct crypto_type crypto_shash_type = {
577 	.ctxsize = crypto_shash_ctxsize,
578 	.extsize = crypto_alg_extsize,
579 	.init = crypto_init_shash_ops,
580 	.init_tfm = crypto_shash_init_tfm,
581 #ifdef CONFIG_PROC_FS
582 	.show = crypto_shash_show,
583 #endif
584 	.report = crypto_shash_report,
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 
crypto_alloc_shash(const char * alg_name,u32 type,u32 mask)591 struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
592 					u32 mask)
593 {
594 	return crypto_alloc_tfm(alg_name, &crypto_shash_type, type, mask);
595 }
596 EXPORT_SYMBOL_GPL(crypto_alloc_shash);
597 
shash_prepare_alg(struct shash_alg * alg)598 static int shash_prepare_alg(struct shash_alg *alg)
599 {
600 	struct crypto_alg *base = &alg->base;
601 
602 	if (alg->digestsize > PAGE_SIZE / 8 ||
603 	    alg->descsize > PAGE_SIZE / 8 ||
604 	    alg->statesize > PAGE_SIZE / 8)
605 		return -EINVAL;
606 
607 	base->cra_type = &crypto_shash_type;
608 	base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
609 	base->cra_flags |= CRYPTO_ALG_TYPE_SHASH;
610 
611 	if (!alg->finup)
612 		alg->finup = shash_finup_unaligned;
613 	if (!alg->digest)
614 		alg->digest = shash_digest_unaligned;
615 	if (!alg->export) {
616 		alg->export = shash_default_export;
617 		alg->import = shash_default_import;
618 		alg->statesize = alg->descsize;
619 	}
620 	if (!alg->setkey)
621 		alg->setkey = shash_no_setkey;
622 
623 	return 0;
624 }
625 
crypto_register_shash(struct shash_alg * alg)626 int crypto_register_shash(struct shash_alg *alg)
627 {
628 	struct crypto_alg *base = &alg->base;
629 	int err;
630 
631 	err = shash_prepare_alg(alg);
632 	if (err)
633 		return err;
634 
635 	return crypto_register_alg(base);
636 }
637 EXPORT_SYMBOL_GPL(crypto_register_shash);
638 
crypto_unregister_shash(struct shash_alg * alg)639 int crypto_unregister_shash(struct shash_alg *alg)
640 {
641 	return crypto_unregister_alg(&alg->base);
642 }
643 EXPORT_SYMBOL_GPL(crypto_unregister_shash);
644 
crypto_register_shashes(struct shash_alg * algs,int count)645 int crypto_register_shashes(struct shash_alg *algs, int count)
646 {
647 	int i, ret;
648 
649 	for (i = 0; i < count; i++) {
650 		ret = crypto_register_shash(&algs[i]);
651 		if (ret)
652 			goto err;
653 	}
654 
655 	return 0;
656 
657 err:
658 	for (--i; i >= 0; --i)
659 		crypto_unregister_shash(&algs[i]);
660 
661 	return ret;
662 }
663 EXPORT_SYMBOL_GPL(crypto_register_shashes);
664 
crypto_unregister_shashes(struct shash_alg * algs,int count)665 int crypto_unregister_shashes(struct shash_alg *algs, int count)
666 {
667 	int i, ret;
668 
669 	for (i = count - 1; i >= 0; --i) {
670 		ret = crypto_unregister_shash(&algs[i]);
671 		if (ret)
672 			pr_err("Failed to unregister %s %s: %d\n",
673 			       algs[i].base.cra_driver_name,
674 			       algs[i].base.cra_name, ret);
675 	}
676 
677 	return 0;
678 }
679 EXPORT_SYMBOL_GPL(crypto_unregister_shashes);
680 
shash_register_instance(struct crypto_template * tmpl,struct shash_instance * inst)681 int shash_register_instance(struct crypto_template *tmpl,
682 			    struct shash_instance *inst)
683 {
684 	int err;
685 
686 	err = shash_prepare_alg(&inst->alg);
687 	if (err)
688 		return err;
689 
690 	return crypto_register_instance(tmpl, shash_crypto_instance(inst));
691 }
692 EXPORT_SYMBOL_GPL(shash_register_instance);
693 
shash_free_instance(struct crypto_instance * inst)694 void shash_free_instance(struct crypto_instance *inst)
695 {
696 	crypto_drop_spawn(crypto_instance_ctx(inst));
697 	kfree(shash_instance(inst));
698 }
699 EXPORT_SYMBOL_GPL(shash_free_instance);
700 
crypto_grab_shash(struct crypto_shash_spawn * spawn,const char * name,u32 type,u32 mask)701 int crypto_grab_shash(struct crypto_shash_spawn *spawn,
702 		      const char *name, u32 type, u32 mask)
703 {
704 	spawn->base.frontend = &crypto_shash_type;
705 	return crypto_grab_spawn(&spawn->base, name, type, mask);
706 }
707 EXPORT_SYMBOL_GPL(crypto_grab_shash);
708 
crypto_init_shash_spawn(struct crypto_shash_spawn * spawn,struct shash_alg * alg,struct crypto_instance * inst)709 int crypto_init_shash_spawn(struct crypto_shash_spawn *spawn,
710 			    struct shash_alg *alg,
711 			    struct crypto_instance *inst)
712 {
713 	return crypto_init_spawn2(&spawn->base, &alg->base, inst,
714 				  &crypto_shash_type);
715 }
716 EXPORT_SYMBOL_GPL(crypto_init_shash_spawn);
717 
shash_attr_alg(struct rtattr * rta,u32 type,u32 mask)718 struct shash_alg *shash_attr_alg(struct rtattr *rta, u32 type, u32 mask)
719 {
720 	struct crypto_alg *alg;
721 
722 	alg = crypto_attr_alg2(rta, &crypto_shash_type, type, mask);
723 	return IS_ERR(alg) ? ERR_CAST(alg) :
724 	       container_of(alg, struct shash_alg, base);
725 }
726 EXPORT_SYMBOL_GPL(shash_attr_alg);
727 
728 MODULE_LICENSE("GPL");
729 MODULE_DESCRIPTION("Synchronous cryptographic hash type");
730