blob: 7eebf3cde7b7b6f25b1ea9dd34296f1fa640bd51 [file] [log] [blame]
Herbert Xu7b5a080b2008-08-31 15:47:27 +10001/*
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
Herbert Xu3b2f6df2008-08-31 18:52:18 +100013#include <crypto/scatterwalk.h>
Herbert Xu7b5a080b2008-08-31 15:47:27 +100014#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>
Steffen Klassertf4d663c2011-09-27 07:26:10 +020020#include <linux/cryptouser.h>
21#include <net/netlink.h>
Herbert Xu7b5a080b2008-08-31 15:47:27 +100022
Herbert Xu3b2f6df2008-08-31 18:52:18 +100023#include "internal.h"
24
Herbert Xu3f683d62009-02-18 16:56:59 +080025static const struct crypto_type crypto_shash_type;
26
Ard Biesheuvel35e9cf42021-06-10 08:21:50 +020027static int shash_no_setkey(struct crypto_shash *tfm, const u8 *key,
28 unsigned int keylen)
Herbert Xu57cfe442009-07-11 22:17:39 +080029{
30 return -ENOSYS;
31}
Ard Biesheuvel35e9cf42021-06-10 08:21:50 +020032
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 */
40bool crypto_shash_alg_has_setkey(struct shash_alg *alg)
41{
42 return alg->setkey != shash_no_setkey;
43}
44EXPORT_SYMBOL_GPL(crypto_shash_alg_has_setkey);
Herbert Xu57cfe442009-07-11 22:17:39 +080045
Herbert Xu7b5a080b2008-08-31 15:47:27 +100046static 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
Herbert Xu18eb8ea2010-05-19 11:50:58 +100055 absize = keylen + (alignmask & ~(crypto_tfm_ctx_alignment() - 1));
Jia-Ju Bai79d47dd2017-10-03 10:25:22 +080056 buffer = kmalloc(absize, GFP_ATOMIC);
Herbert Xu7b5a080b2008-08-31 15:47:27 +100057 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);
Herbert Xu8c32c512009-07-14 21:35:36 +080063 kzfree(buffer);
Herbert Xu7b5a080b2008-08-31 15:47:27 +100064 return err;
65}
66
Eric Biggersdd39aff2019-01-06 18:47:42 -080067static void shash_set_needkey(struct crypto_shash *tfm, struct shash_alg *alg)
68{
69 if (crypto_shash_alg_has_setkey(alg) &&
70 !(alg->base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY))
71 crypto_shash_set_flags(tfm, CRYPTO_TFM_NEED_KEY);
72}
73
Herbert Xu7b5a080b2008-08-31 15:47:27 +100074int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
75 unsigned int keylen)
76{
77 struct shash_alg *shash = crypto_shash_alg(tfm);
78 unsigned long alignmask = crypto_shash_alignmask(tfm);
Eric Biggersadf26e82018-01-03 11:16:27 -080079 int err;
Herbert Xu7b5a080b2008-08-31 15:47:27 +100080
81 if ((unsigned long)key & alignmask)
Eric Biggersadf26e82018-01-03 11:16:27 -080082 err = shash_setkey_unaligned(tfm, key, keylen);
83 else
84 err = shash->setkey(tfm, key, keylen);
Herbert Xu7b5a080b2008-08-31 15:47:27 +100085
Eric Biggersdd39aff2019-01-06 18:47:42 -080086 if (unlikely(err)) {
87 shash_set_needkey(tfm, shash);
Eric Biggersadf26e82018-01-03 11:16:27 -080088 return err;
Eric Biggersdd39aff2019-01-06 18:47:42 -080089 }
Eric Biggersadf26e82018-01-03 11:16:27 -080090
91 crypto_shash_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
92 return 0;
Herbert Xu7b5a080b2008-08-31 15:47:27 +100093}
94EXPORT_SYMBOL_GPL(crypto_shash_setkey);
95
96static inline unsigned int shash_align_buffer_size(unsigned len,
97 unsigned long mask)
98{
Mark Charlebois66d8ea52012-11-20 21:18:16 +010099 typedef u8 __attribute__ ((aligned)) u8_aligned;
100 return len + (mask & ~(__alignof__(u8_aligned) - 1));
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000101}
102
103static int shash_update_unaligned(struct shash_desc *desc, const u8 *data,
104 unsigned int len)
105{
106 struct crypto_shash *tfm = desc->tfm;
107 struct shash_alg *shash = crypto_shash_alg(tfm);
108 unsigned long alignmask = crypto_shash_alignmask(tfm);
109 unsigned int unaligned_len = alignmask + 1 -
110 ((unsigned long)data & alignmask);
Herbert Xu0e2d3a12009-07-14 21:43:56 +0800111 u8 ubuf[shash_align_buffer_size(unaligned_len, alignmask)]
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000112 __attribute__ ((aligned));
Herbert Xu0e2d3a12009-07-14 21:43:56 +0800113 u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1);
Herbert Xu8c32c512009-07-14 21:35:36 +0800114 int err;
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000115
Yehuda Sadehf4f68992009-03-27 13:03:51 +0800116 if (unaligned_len > len)
117 unaligned_len = len;
118
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000119 memcpy(buf, data, unaligned_len);
Herbert Xu8c32c512009-07-14 21:35:36 +0800120 err = shash->update(desc, buf, unaligned_len);
121 memset(buf, 0, unaligned_len);
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000122
Herbert Xu8c32c512009-07-14 21:35:36 +0800123 return err ?:
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000124 shash->update(desc, data + unaligned_len, len - unaligned_len);
125}
126
127int crypto_shash_update(struct shash_desc *desc, const u8 *data,
128 unsigned int len)
129{
130 struct crypto_shash *tfm = desc->tfm;
131 struct shash_alg *shash = crypto_shash_alg(tfm);
132 unsigned long alignmask = crypto_shash_alignmask(tfm);
133
134 if ((unsigned long)data & alignmask)
135 return shash_update_unaligned(desc, data, len);
136
137 return shash->update(desc, data, len);
138}
139EXPORT_SYMBOL_GPL(crypto_shash_update);
140
141static int shash_final_unaligned(struct shash_desc *desc, u8 *out)
142{
143 struct crypto_shash *tfm = desc->tfm;
144 unsigned long alignmask = crypto_shash_alignmask(tfm);
145 struct shash_alg *shash = crypto_shash_alg(tfm);
146 unsigned int ds = crypto_shash_digestsize(tfm);
Herbert Xu0e2d3a12009-07-14 21:43:56 +0800147 u8 ubuf[shash_align_buffer_size(ds, alignmask)]
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000148 __attribute__ ((aligned));
Herbert Xu0e2d3a12009-07-14 21:43:56 +0800149 u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1);
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000150 int err;
151
152 err = shash->final(desc, buf);
Herbert Xu8c32c512009-07-14 21:35:36 +0800153 if (err)
154 goto out;
155
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000156 memcpy(out, buf, ds);
Herbert Xu8c32c512009-07-14 21:35:36 +0800157
158out:
159 memset(buf, 0, ds);
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000160 return err;
161}
162
163int crypto_shash_final(struct shash_desc *desc, u8 *out)
164{
165 struct crypto_shash *tfm = desc->tfm;
166 struct shash_alg *shash = crypto_shash_alg(tfm);
167 unsigned long alignmask = crypto_shash_alignmask(tfm);
168
169 if ((unsigned long)out & alignmask)
170 return shash_final_unaligned(desc, out);
171
172 return shash->final(desc, out);
173}
174EXPORT_SYMBOL_GPL(crypto_shash_final);
175
176static int shash_finup_unaligned(struct shash_desc *desc, const u8 *data,
177 unsigned int len, u8 *out)
178{
179 return crypto_shash_update(desc, data, len) ?:
180 crypto_shash_final(desc, out);
181}
182
183int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
184 unsigned int len, u8 *out)
185{
186 struct crypto_shash *tfm = desc->tfm;
187 struct shash_alg *shash = crypto_shash_alg(tfm);
188 unsigned long alignmask = crypto_shash_alignmask(tfm);
189
Herbert Xu8267ada2009-07-09 20:36:44 +0800190 if (((unsigned long)data | (unsigned long)out) & alignmask)
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000191 return shash_finup_unaligned(desc, data, len, out);
192
193 return shash->finup(desc, data, len, out);
194}
195EXPORT_SYMBOL_GPL(crypto_shash_finup);
196
197static int shash_digest_unaligned(struct shash_desc *desc, const u8 *data,
198 unsigned int len, u8 *out)
199{
200 return crypto_shash_init(desc) ?:
Herbert Xuf88ad8d2009-07-08 23:32:08 +0800201 crypto_shash_finup(desc, data, len, out);
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000202}
203
204int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
205 unsigned int len, u8 *out)
206{
207 struct crypto_shash *tfm = desc->tfm;
208 struct shash_alg *shash = crypto_shash_alg(tfm);
209 unsigned long alignmask = crypto_shash_alignmask(tfm);
210
Eric Biggersadf26e82018-01-03 11:16:27 -0800211 if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
212 return -ENOKEY;
213
Herbert Xu8267ada2009-07-09 20:36:44 +0800214 if (((unsigned long)data | (unsigned long)out) & alignmask)
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000215 return shash_digest_unaligned(desc, data, len, out);
216
217 return shash->digest(desc, data, len, out);
218}
219EXPORT_SYMBOL_GPL(crypto_shash_digest);
220
Herbert Xuf5926822009-07-22 12:37:06 +0800221static int shash_default_export(struct shash_desc *desc, void *out)
Herbert Xudec8b782008-11-02 21:38:11 +0800222{
Herbert Xuf5926822009-07-22 12:37:06 +0800223 memcpy(out, shash_desc_ctx(desc), crypto_shash_descsize(desc->tfm));
224 return 0;
Herbert Xudec8b782008-11-02 21:38:11 +0800225}
Herbert Xu99d27e12009-07-09 20:30:57 +0800226
Herbert Xuf5926822009-07-22 12:37:06 +0800227static int shash_default_import(struct shash_desc *desc, const void *in)
Herbert Xu99d27e12009-07-09 20:30:57 +0800228{
Herbert Xuf5926822009-07-22 12:37:06 +0800229 memcpy(shash_desc_ctx(desc), in, crypto_shash_descsize(desc->tfm));
230 return 0;
Herbert Xu99d27e12009-07-09 20:30:57 +0800231}
Herbert Xudec8b782008-11-02 21:38:11 +0800232
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000233static int shash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
234 unsigned int keylen)
235{
236 struct crypto_shash **ctx = crypto_ahash_ctx(tfm);
237
238 return crypto_shash_setkey(*ctx, key, keylen);
239}
240
241static int shash_async_init(struct ahash_request *req)
242{
243 struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
244 struct shash_desc *desc = ahash_request_ctx(req);
245
246 desc->tfm = *ctx;
247 desc->flags = req->base.flags;
248
249 return crypto_shash_init(desc);
250}
251
Herbert Xu7eddf952009-07-12 21:25:20 +0800252int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc)
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000253{
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000254 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}
Herbert Xu7eddf952009-07-12 21:25:20 +0800263EXPORT_SYMBOL_GPL(shash_ahash_update);
264
265static int shash_async_update(struct ahash_request *req)
266{
267 return shash_ahash_update(req, ahash_request_ctx(req));
268}
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000269
270static int shash_async_final(struct ahash_request *req)
271{
272 return crypto_shash_final(ahash_request_ctx(req), req->result);
273}
274
Herbert Xu66f6ce52009-07-15 12:40:40 +0800275int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc)
276{
277 struct crypto_hash_walk walk;
278 int nbytes;
279
Herbert Xucbc86b92009-07-15 21:26:41 +0800280 nbytes = crypto_hash_walk_first(req, &walk);
281 if (!nbytes)
282 return crypto_shash_final(desc, req->result);
283
284 do {
Herbert Xu66f6ce52009-07-15 12:40:40 +0800285 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);
Herbert Xucbc86b92009-07-15 21:26:41 +0800289 nbytes = crypto_hash_walk_done(&walk, nbytes);
290 } while (nbytes > 0);
Herbert Xu66f6ce52009-07-15 12:40:40 +0800291
292 return nbytes;
293}
294EXPORT_SYMBOL_GPL(shash_ahash_finup);
295
296static 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 desc->flags = req->base.flags;
303
304 return shash_ahash_finup(req, desc);
305}
306
Herbert Xu7eddf952009-07-12 21:25:20 +0800307int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc)
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000308{
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000309 unsigned int nbytes = req->nbytes;
Herbert Xu91daaef2017-10-09 23:30:02 +0800310 struct scatterlist *sg;
311 unsigned int offset;
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000312 int err;
313
Herbert Xu91daaef2017-10-09 23:30:02 +0800314 if (nbytes &&
315 (sg = req->src, offset = sg->offset,
316 nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset))) {
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000317 void *data;
318
Cong Wangf0dfc0b2011-11-25 23:14:17 +0800319 data = kmap_atomic(sg_page(sg));
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000320 err = crypto_shash_digest(desc, data + offset, nbytes,
321 req->result);
Cong Wangf0dfc0b2011-11-25 23:14:17 +0800322 kunmap_atomic(data);
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000323 crypto_yield(desc->flags);
Herbert Xu7eddf952009-07-12 21:25:20 +0800324 } else
325 err = crypto_shash_init(desc) ?:
Herbert Xu66f6ce52009-07-15 12:40:40 +0800326 shash_ahash_finup(req, desc);
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000327
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000328 return err;
329}
Herbert Xu7eddf952009-07-12 21:25:20 +0800330EXPORT_SYMBOL_GPL(shash_ahash_digest);
331
332static int shash_async_digest(struct ahash_request *req)
333{
334 struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
335 struct shash_desc *desc = ahash_request_ctx(req);
336
337 desc->tfm = *ctx;
338 desc->flags = req->base.flags;
339
340 return shash_ahash_digest(req, desc);
341}
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000342
Herbert Xu66f6ce52009-07-15 12:40:40 +0800343static int shash_async_export(struct ahash_request *req, void *out)
344{
345 return crypto_shash_export(ahash_request_ctx(req), out);
346}
347
348static int shash_async_import(struct ahash_request *req, const void *in)
349{
Herbert Xu90246e72010-11-04 13:00:22 -0500350 struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
351 struct shash_desc *desc = ahash_request_ctx(req);
352
353 desc->tfm = *ctx;
354 desc->flags = req->base.flags;
355
356 return crypto_shash_import(desc, in);
Herbert Xu66f6ce52009-07-15 12:40:40 +0800357}
358
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000359static void crypto_exit_shash_ops_async(struct crypto_tfm *tfm)
360{
361 struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
362
363 crypto_free_shash(*ctx);
364}
365
Herbert Xu88056ec2009-07-14 12:28:26 +0800366int crypto_init_shash_ops_async(struct crypto_tfm *tfm)
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000367{
368 struct crypto_alg *calg = tfm->__crt_alg;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800369 struct shash_alg *alg = __crypto_shash_alg(calg);
Herbert Xu88056ec2009-07-14 12:28:26 +0800370 struct crypto_ahash *crt = __crypto_ahash_cast(tfm);
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000371 struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
372 struct crypto_shash *shash;
373
374 if (!crypto_mod_get(calg))
375 return -EAGAIN;
376
Herbert Xu3f683d62009-02-18 16:56:59 +0800377 shash = crypto_create_tfm(calg, &crypto_shash_type);
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000378 if (IS_ERR(shash)) {
379 crypto_mod_put(calg);
380 return PTR_ERR(shash);
381 }
382
383 *ctx = shash;
384 tfm->exit = crypto_exit_shash_ops_async;
385
386 crt->init = shash_async_init;
387 crt->update = shash_async_update;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800388 crt->final = shash_async_final;
389 crt->finup = shash_async_finup;
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000390 crt->digest = shash_async_digest;
Eric Biggersdd39aff2019-01-06 18:47:42 -0800391 if (crypto_shash_alg_has_setkey(alg))
392 crt->setkey = shash_async_setkey;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800393
Eric Biggersadf26e82018-01-03 11:16:27 -0800394 crypto_ahash_set_flags(crt, crypto_shash_get_flags(shash) &
395 CRYPTO_TFM_NEED_KEY);
Herbert Xu00420a62016-01-27 00:16:37 +0800396
Herbert Xu66f6ce52009-07-15 12:40:40 +0800397 if (alg->export)
398 crt->export = shash_async_export;
Steffen Klassert0044f3e2009-07-24 13:57:13 +0800399 if (alg->import)
Herbert Xu66f6ce52009-07-15 12:40:40 +0800400 crt->import = shash_async_import;
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000401
Herbert Xu3b2f6df2008-08-31 18:52:18 +1000402 crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash);
403
404 return 0;
405}
406
Herbert Xu2ca33da2009-07-13 20:46:25 +0800407static int crypto_shash_init_tfm(struct crypto_tfm *tfm)
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000408{
Herbert Xu113adef2009-07-14 12:50:12 +0800409 struct crypto_shash *hash = __crypto_shash_cast(tfm);
Eric Biggersadf26e82018-01-03 11:16:27 -0800410 struct shash_alg *alg = crypto_shash_alg(hash);
Herbert Xu113adef2009-07-14 12:50:12 +0800411
Eric Biggersadf26e82018-01-03 11:16:27 -0800412 hash->descsize = alg->descsize;
413
Eric Biggersdd39aff2019-01-06 18:47:42 -0800414 shash_set_needkey(hash, alg);
Eric Biggersadf26e82018-01-03 11:16:27 -0800415
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000416 return 0;
417}
418
Herbert Xu3acc8472011-11-03 23:46:07 +1100419#ifdef CONFIG_NET
Steffen Klassertf4d663c2011-09-27 07:26:10 +0200420static int crypto_shash_report(struct sk_buff *skb, struct crypto_alg *alg)
421{
422 struct crypto_report_hash rhash;
423 struct shash_alg *salg = __crypto_shash_alg(alg);
424
Mathias Krause9a5467b2013-02-05 18:19:13 +0100425 strncpy(rhash.type, "shash", sizeof(rhash.type));
426
Steffen Klassertf4d663c2011-09-27 07:26:10 +0200427 rhash.blocksize = alg->cra_blocksize;
428 rhash.digestsize = salg->digestsize;
429
David S. Miller6662df32012-04-01 20:19:05 -0400430 if (nla_put(skb, CRYPTOCFGA_REPORT_HASH,
431 sizeof(struct crypto_report_hash), &rhash))
432 goto nla_put_failure;
Steffen Klassertf4d663c2011-09-27 07:26:10 +0200433 return 0;
434
435nla_put_failure:
436 return -EMSGSIZE;
437}
Herbert Xu3acc8472011-11-03 23:46:07 +1100438#else
439static int crypto_shash_report(struct sk_buff *skb, struct crypto_alg *alg)
440{
441 return -ENOSYS;
442}
443#endif
Steffen Klassertf4d663c2011-09-27 07:26:10 +0200444
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000445static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
446 __attribute__ ((unused));
447static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
448{
449 struct shash_alg *salg = __crypto_shash_alg(alg);
450
451 seq_printf(m, "type : shash\n");
452 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
453 seq_printf(m, "digestsize : %u\n", salg->digestsize);
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000454}
455
456static const struct crypto_type crypto_shash_type = {
Herbert Xuac611682015-04-20 13:39:01 +0800457 .extsize = crypto_alg_extsize,
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000458 .init_tfm = crypto_shash_init_tfm,
459#ifdef CONFIG_PROC_FS
460 .show = crypto_shash_show,
461#endif
Steffen Klassertf4d663c2011-09-27 07:26:10 +0200462 .report = crypto_shash_report,
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000463 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
464 .maskset = CRYPTO_ALG_TYPE_MASK,
465 .type = CRYPTO_ALG_TYPE_SHASH,
466 .tfmsize = offsetof(struct crypto_shash, base),
467};
468
469struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
470 u32 mask)
471{
Herbert Xu3f683d62009-02-18 16:56:59 +0800472 return crypto_alloc_tfm(alg_name, &crypto_shash_type, type, mask);
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000473}
474EXPORT_SYMBOL_GPL(crypto_alloc_shash);
475
Herbert Xu619a6eb2009-07-08 18:46:23 +0800476static int shash_prepare_alg(struct shash_alg *alg)
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000477{
478 struct crypto_alg *base = &alg->base;
479
480 if (alg->digestsize > PAGE_SIZE / 8 ||
Herbert Xu99d27e12009-07-09 20:30:57 +0800481 alg->descsize > PAGE_SIZE / 8 ||
482 alg->statesize > PAGE_SIZE / 8)
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000483 return -EINVAL;
484
485 base->cra_type = &crypto_shash_type;
486 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
487 base->cra_flags |= CRYPTO_ALG_TYPE_SHASH;
Herbert Xu99d27e12009-07-09 20:30:57 +0800488
Herbert Xu8267ada2009-07-09 20:36:44 +0800489 if (!alg->finup)
490 alg->finup = shash_finup_unaligned;
491 if (!alg->digest)
492 alg->digest = shash_digest_unaligned;
Herbert Xuf5926822009-07-22 12:37:06 +0800493 if (!alg->export) {
494 alg->export = shash_default_export;
495 alg->import = shash_default_import;
496 alg->statesize = alg->descsize;
497 }
Herbert Xu57cfe442009-07-11 22:17:39 +0800498 if (!alg->setkey)
499 alg->setkey = shash_no_setkey;
Herbert Xu99d27e12009-07-09 20:30:57 +0800500
Herbert Xu619a6eb2009-07-08 18:46:23 +0800501 return 0;
502}
503
504int crypto_register_shash(struct shash_alg *alg)
505{
506 struct crypto_alg *base = &alg->base;
507 int err;
508
509 err = shash_prepare_alg(alg);
510 if (err)
511 return err;
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000512
513 return crypto_register_alg(base);
514}
515EXPORT_SYMBOL_GPL(crypto_register_shash);
516
517int crypto_unregister_shash(struct shash_alg *alg)
518{
519 return crypto_unregister_alg(&alg->base);
520}
521EXPORT_SYMBOL_GPL(crypto_unregister_shash);
522
Jussi Kivilinna50fc3e82012-07-11 14:20:20 +0300523int crypto_register_shashes(struct shash_alg *algs, int count)
524{
525 int i, ret;
526
527 for (i = 0; i < count; i++) {
528 ret = crypto_register_shash(&algs[i]);
529 if (ret)
530 goto err;
531 }
532
533 return 0;
534
535err:
536 for (--i; i >= 0; --i)
537 crypto_unregister_shash(&algs[i]);
538
539 return ret;
540}
541EXPORT_SYMBOL_GPL(crypto_register_shashes);
542
543int crypto_unregister_shashes(struct shash_alg *algs, int count)
544{
545 int i, ret;
546
547 for (i = count - 1; i >= 0; --i) {
548 ret = crypto_unregister_shash(&algs[i]);
549 if (ret)
550 pr_err("Failed to unregister %s %s: %d\n",
551 algs[i].base.cra_driver_name,
552 algs[i].base.cra_name, ret);
553 }
554
555 return 0;
556}
557EXPORT_SYMBOL_GPL(crypto_unregister_shashes);
558
Herbert Xu619a6eb2009-07-08 18:46:23 +0800559int shash_register_instance(struct crypto_template *tmpl,
560 struct shash_instance *inst)
561{
562 int err;
563
564 err = shash_prepare_alg(&inst->alg);
565 if (err)
566 return err;
567
568 return crypto_register_instance(tmpl, shash_crypto_instance(inst));
569}
570EXPORT_SYMBOL_GPL(shash_register_instance);
571
Herbert Xu2e4fddd2009-07-07 15:17:12 +0800572void shash_free_instance(struct crypto_instance *inst)
573{
574 crypto_drop_spawn(crypto_instance_ctx(inst));
575 kfree(shash_instance(inst));
576}
577EXPORT_SYMBOL_GPL(shash_free_instance);
578
Herbert Xu94296992009-07-08 17:21:37 +0800579int crypto_init_shash_spawn(struct crypto_shash_spawn *spawn,
580 struct shash_alg *alg,
581 struct crypto_instance *inst)
582{
583 return crypto_init_spawn2(&spawn->base, &alg->base, inst,
584 &crypto_shash_type);
585}
586EXPORT_SYMBOL_GPL(crypto_init_shash_spawn);
587
Herbert Xu7d6f5642009-07-08 17:56:28 +0800588struct shash_alg *shash_attr_alg(struct rtattr *rta, u32 type, u32 mask)
589{
590 struct crypto_alg *alg;
591
592 alg = crypto_attr_alg2(rta, &crypto_shash_type, type, mask);
593 return IS_ERR(alg) ? ERR_CAST(alg) :
594 container_of(alg, struct shash_alg, base);
595}
596EXPORT_SYMBOL_GPL(shash_attr_alg);
597
Herbert Xu7b5a080b2008-08-31 15:47:27 +1000598MODULE_LICENSE("GPL");
599MODULE_DESCRIPTION("Synchronous cryptographic hash type");