blob: 14402ef6d8269cc8db32f87dd63009da4b9cb7a4 [file] [log] [blame]
Loc Ho004a4032008-05-14 20:41:47 +08001/*
2 * Asynchronous Cryptographic Hash operations.
3 *
4 * This is the asynchronous version of hash.c with notification of
5 * completion via a callback.
6 *
7 * Copyright (c) 2008 Loc Ho <lho@amcc.com>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
12 * any later version.
13 *
14 */
15
Herbert Xu20036252008-07-07 22:19:53 +080016#include <crypto/internal/hash.h>
17#include <crypto/scatterwalk.h>
Herbert Xu75ecb232014-05-21 20:56:12 +080018#include <linux/bug.h>
Loc Ho004a4032008-05-14 20:41:47 +080019#include <linux/err.h>
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/sched.h>
23#include <linux/slab.h>
24#include <linux/seq_file.h>
Steffen Klassert6238cba2011-09-27 07:41:07 +020025#include <linux/cryptouser.h>
26#include <net/netlink.h>
Loc Ho004a4032008-05-14 20:41:47 +080027
28#include "internal.h"
29
Herbert Xu66f6ce52009-07-15 12:40:40 +080030struct ahash_request_priv {
31 crypto_completion_t complete;
32 void *data;
33 u8 *result;
Herbert Xuc1047952017-04-10 17:27:57 +080034 u32 flags;
Herbert Xu66f6ce52009-07-15 12:40:40 +080035 void *ubuf[] CRYPTO_MINALIGN_ATTR;
36};
37
Herbert Xu88056ec2009-07-14 12:28:26 +080038static inline struct ahash_alg *crypto_ahash_alg(struct crypto_ahash *hash)
39{
40 return container_of(crypto_hash_alg_common(hash), struct ahash_alg,
41 halg);
42}
43
Herbert Xu20036252008-07-07 22:19:53 +080044static int hash_walk_next(struct crypto_hash_walk *walk)
45{
46 unsigned int alignmask = walk->alignmask;
47 unsigned int offset = walk->offset;
48 unsigned int nbytes = min(walk->entrylen,
49 ((unsigned int)(PAGE_SIZE)) - offset);
50
Herbert Xu75ecb232014-05-21 20:56:12 +080051 if (walk->flags & CRYPTO_ALG_ASYNC)
52 walk->data = kmap(walk->pg);
53 else
54 walk->data = kmap_atomic(walk->pg);
Herbert Xu20036252008-07-07 22:19:53 +080055 walk->data += offset;
56
Szilveszter Ördög23a75ee2010-08-06 09:26:38 +080057 if (offset & alignmask) {
58 unsigned int unaligned = alignmask + 1 - (offset & alignmask);
Joshua I. Jamesb516d512014-12-05 14:44:54 +090059
Szilveszter Ördög23a75ee2010-08-06 09:26:38 +080060 if (nbytes > unaligned)
61 nbytes = unaligned;
62 }
Herbert Xu20036252008-07-07 22:19:53 +080063
64 walk->entrylen -= nbytes;
65 return nbytes;
66}
67
68static int hash_walk_new_entry(struct crypto_hash_walk *walk)
69{
70 struct scatterlist *sg;
71
72 sg = walk->sg;
Herbert Xu20036252008-07-07 22:19:53 +080073 walk->offset = sg->offset;
Herbert Xu13f4bb72016-05-04 17:52:56 +080074 walk->pg = sg_page(walk->sg) + (walk->offset >> PAGE_SHIFT);
75 walk->offset = offset_in_page(walk->offset);
Herbert Xu20036252008-07-07 22:19:53 +080076 walk->entrylen = sg->length;
77
78 if (walk->entrylen > walk->total)
79 walk->entrylen = walk->total;
80 walk->total -= walk->entrylen;
81
82 return hash_walk_next(walk);
83}
84
85int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err)
86{
87 unsigned int alignmask = walk->alignmask;
88 unsigned int nbytes = walk->entrylen;
89
90 walk->data -= walk->offset;
91
92 if (nbytes && walk->offset & alignmask && !err) {
Herbert Xu20036252008-07-07 22:19:53 +080093 walk->offset = ALIGN(walk->offset, alignmask + 1);
94 walk->data += walk->offset;
95
96 nbytes = min(nbytes,
97 ((unsigned int)(PAGE_SIZE)) - walk->offset);
98 walk->entrylen -= nbytes;
99
100 return nbytes;
101 }
102
Herbert Xu75ecb232014-05-21 20:56:12 +0800103 if (walk->flags & CRYPTO_ALG_ASYNC)
104 kunmap(walk->pg);
105 else {
106 kunmap_atomic(walk->data);
107 /*
108 * The may sleep test only makes sense for sync users.
109 * Async users don't need to sleep here anyway.
110 */
111 crypto_yield(walk->flags);
112 }
Herbert Xu20036252008-07-07 22:19:53 +0800113
114 if (err)
115 return err;
116
Herbert Xud315a0e2009-05-31 23:09:22 +1000117 if (nbytes) {
118 walk->offset = 0;
119 walk->pg++;
Herbert Xu20036252008-07-07 22:19:53 +0800120 return hash_walk_next(walk);
Herbert Xud315a0e2009-05-31 23:09:22 +1000121 }
Herbert Xu20036252008-07-07 22:19:53 +0800122
123 if (!walk->total)
124 return 0;
125
Cristian Stoica5be4d4c2015-01-20 10:06:16 +0200126 walk->sg = sg_next(walk->sg);
Herbert Xu20036252008-07-07 22:19:53 +0800127
128 return hash_walk_new_entry(walk);
129}
130EXPORT_SYMBOL_GPL(crypto_hash_walk_done);
131
132int crypto_hash_walk_first(struct ahash_request *req,
133 struct crypto_hash_walk *walk)
134{
135 walk->total = req->nbytes;
136
Tim Chen6d9529c2014-07-10 16:18:08 -0700137 if (!walk->total) {
138 walk->entrylen = 0;
Herbert Xu20036252008-07-07 22:19:53 +0800139 return 0;
Tim Chen6d9529c2014-07-10 16:18:08 -0700140 }
Herbert Xu20036252008-07-07 22:19:53 +0800141
142 walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req));
143 walk->sg = req->src;
Herbert Xu75ecb232014-05-21 20:56:12 +0800144 walk->flags = req->base.flags & CRYPTO_TFM_REQ_MASK;
Herbert Xu20036252008-07-07 22:19:53 +0800145
146 return hash_walk_new_entry(walk);
147}
148EXPORT_SYMBOL_GPL(crypto_hash_walk_first);
149
Herbert Xu75ecb232014-05-21 20:56:12 +0800150int crypto_ahash_walk_first(struct ahash_request *req,
151 struct crypto_hash_walk *walk)
152{
153 walk->total = req->nbytes;
154
Tim Chen6d9529c2014-07-10 16:18:08 -0700155 if (!walk->total) {
156 walk->entrylen = 0;
Herbert Xu75ecb232014-05-21 20:56:12 +0800157 return 0;
Tim Chen6d9529c2014-07-10 16:18:08 -0700158 }
Herbert Xu75ecb232014-05-21 20:56:12 +0800159
160 walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req));
161 walk->sg = req->src;
162 walk->flags = req->base.flags & CRYPTO_TFM_REQ_MASK;
163 walk->flags |= CRYPTO_ALG_ASYNC;
164
165 BUILD_BUG_ON(CRYPTO_TFM_REQ_MASK & CRYPTO_ALG_ASYNC);
166
167 return hash_walk_new_entry(walk);
168}
169EXPORT_SYMBOL_GPL(crypto_ahash_walk_first);
170
Loc Ho004a4032008-05-14 20:41:47 +0800171static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key,
172 unsigned int keylen)
173{
Loc Ho004a4032008-05-14 20:41:47 +0800174 unsigned long alignmask = crypto_ahash_alignmask(tfm);
175 int ret;
176 u8 *buffer, *alignbuffer;
177 unsigned long absize;
178
179 absize = keylen + alignmask;
Herbert Xu093900c2009-07-14 21:48:35 +0800180 buffer = kmalloc(absize, GFP_KERNEL);
Loc Ho004a4032008-05-14 20:41:47 +0800181 if (!buffer)
182 return -ENOMEM;
183
184 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
185 memcpy(alignbuffer, key, keylen);
Herbert Xua70c5222009-07-15 20:39:05 +0800186 ret = tfm->setkey(tfm, alignbuffer, keylen);
Herbert Xu8c32c512009-07-14 21:35:36 +0800187 kzfree(buffer);
Loc Ho004a4032008-05-14 20:41:47 +0800188 return ret;
189}
190
Herbert Xu66f6ce52009-07-15 12:40:40 +0800191int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
Loc Ho004a4032008-05-14 20:41:47 +0800192 unsigned int keylen)
193{
Loc Ho004a4032008-05-14 20:41:47 +0800194 unsigned long alignmask = crypto_ahash_alignmask(tfm);
Eric Biggersadf26e82018-01-03 11:16:27 -0800195 int err;
Loc Ho004a4032008-05-14 20:41:47 +0800196
197 if ((unsigned long)key & alignmask)
Eric Biggersadf26e82018-01-03 11:16:27 -0800198 err = ahash_setkey_unaligned(tfm, key, keylen);
199 else
200 err = tfm->setkey(tfm, key, keylen);
Loc Ho004a4032008-05-14 20:41:47 +0800201
Eric Biggersadf26e82018-01-03 11:16:27 -0800202 if (err)
203 return err;
204
205 crypto_ahash_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
206 return 0;
Loc Ho004a4032008-05-14 20:41:47 +0800207}
Herbert Xu66f6ce52009-07-15 12:40:40 +0800208EXPORT_SYMBOL_GPL(crypto_ahash_setkey);
Loc Ho004a4032008-05-14 20:41:47 +0800209
Herbert Xu3751f402008-11-08 08:56:57 +0800210static int ahash_nosetkey(struct crypto_ahash *tfm, const u8 *key,
211 unsigned int keylen)
212{
213 return -ENOSYS;
214}
215
Herbert Xu66f6ce52009-07-15 12:40:40 +0800216static inline unsigned int ahash_align_buffer_size(unsigned len,
217 unsigned long mask)
218{
219 return len + (mask & ~(crypto_tfm_ctx_alignment() - 1));
220}
221
Marek Vasut1ffc9fb2014-03-14 02:37:05 +0100222static int ahash_save_req(struct ahash_request *req, crypto_completion_t cplt)
Herbert Xu66f6ce52009-07-15 12:40:40 +0800223{
224 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
225 unsigned long alignmask = crypto_ahash_alignmask(tfm);
226 unsigned int ds = crypto_ahash_digestsize(tfm);
227 struct ahash_request_priv *priv;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800228
229 priv = kmalloc(sizeof(*priv) + ahash_align_buffer_size(ds, alignmask),
230 (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
Steffen Klassert5befbd52009-07-24 13:56:31 +0800231 GFP_KERNEL : GFP_ATOMIC);
Herbert Xu66f6ce52009-07-15 12:40:40 +0800232 if (!priv)
233 return -ENOMEM;
234
Marek Vasutab6bf4e2014-03-14 02:37:04 +0100235 /*
236 * WARNING: Voodoo programming below!
237 *
238 * The code below is obscure and hard to understand, thus explanation
239 * is necessary. See include/crypto/hash.h and include/linux/crypto.h
240 * to understand the layout of structures used here!
241 *
242 * The code here will replace portions of the ORIGINAL request with
243 * pointers to new code and buffers so the hashing operation can store
244 * the result in aligned buffer. We will call the modified request
245 * an ADJUSTED request.
246 *
247 * The newly mangled request will look as such:
248 *
249 * req {
250 * .result = ADJUSTED[new aligned buffer]
251 * .base.complete = ADJUSTED[pointer to completion function]
252 * .base.data = ADJUSTED[*req (pointer to self)]
253 * .priv = ADJUSTED[new priv] {
254 * .result = ORIGINAL(result)
255 * .complete = ORIGINAL(base.complete)
256 * .data = ORIGINAL(base.data)
257 * }
258 */
259
Herbert Xu66f6ce52009-07-15 12:40:40 +0800260 priv->result = req->result;
261 priv->complete = req->base.complete;
262 priv->data = req->base.data;
Herbert Xuc1047952017-04-10 17:27:57 +0800263 priv->flags = req->base.flags;
264
Marek Vasutab6bf4e2014-03-14 02:37:04 +0100265 /*
266 * WARNING: We do not backup req->priv here! The req->priv
267 * is for internal use of the Crypto API and the
268 * user must _NOT_ _EVER_ depend on it's content!
269 */
Herbert Xu66f6ce52009-07-15 12:40:40 +0800270
271 req->result = PTR_ALIGN((u8 *)priv->ubuf, alignmask + 1);
Marek Vasut1ffc9fb2014-03-14 02:37:05 +0100272 req->base.complete = cplt;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800273 req->base.data = req;
274 req->priv = priv;
275
Marek Vasut1ffc9fb2014-03-14 02:37:05 +0100276 return 0;
277}
278
Herbert Xuc1047952017-04-10 17:27:57 +0800279static void ahash_restore_req(struct ahash_request *req, int err)
Marek Vasut1ffc9fb2014-03-14 02:37:05 +0100280{
281 struct ahash_request_priv *priv = req->priv;
282
Herbert Xuc1047952017-04-10 17:27:57 +0800283 if (!err)
284 memcpy(priv->result, req->result,
285 crypto_ahash_digestsize(crypto_ahash_reqtfm(req)));
286
Marek Vasut1ffc9fb2014-03-14 02:37:05 +0100287 /* Restore the original crypto request. */
288 req->result = priv->result;
Herbert Xuc1047952017-04-10 17:27:57 +0800289
290 ahash_request_set_callback(req, priv->flags,
291 priv->complete, priv->data);
Marek Vasut1ffc9fb2014-03-14 02:37:05 +0100292 req->priv = NULL;
293
294 /* Free the req->priv.priv from the ADJUSTED request. */
295 kzfree(priv);
296}
297
Herbert Xuc1047952017-04-10 17:27:57 +0800298static void ahash_notify_einprogress(struct ahash_request *req)
Marek Vasut1ffc9fb2014-03-14 02:37:05 +0100299{
300 struct ahash_request_priv *priv = req->priv;
Herbert Xuc1047952017-04-10 17:27:57 +0800301 struct crypto_async_request oreq;
Marek Vasut1ffc9fb2014-03-14 02:37:05 +0100302
Herbert Xuc1047952017-04-10 17:27:57 +0800303 oreq.data = priv->data;
Marek Vasut1ffc9fb2014-03-14 02:37:05 +0100304
Herbert Xuc1047952017-04-10 17:27:57 +0800305 priv->complete(&oreq, -EINPROGRESS);
Marek Vasut1ffc9fb2014-03-14 02:37:05 +0100306}
307
308static void ahash_op_unaligned_done(struct crypto_async_request *req, int err)
309{
310 struct ahash_request *areq = req->data;
311
Herbert Xuc1047952017-04-10 17:27:57 +0800312 if (err == -EINPROGRESS) {
313 ahash_notify_einprogress(areq);
314 return;
315 }
316
Marek Vasut1ffc9fb2014-03-14 02:37:05 +0100317 /*
318 * Restore the original request, see ahash_op_unaligned() for what
319 * goes where.
320 *
321 * The "struct ahash_request *req" here is in fact the "req.base"
322 * from the ADJUSTED request from ahash_op_unaligned(), thus as it
323 * is a pointer to self, it is also the ADJUSTED "req" .
324 */
325
326 /* First copy req->result into req->priv.result */
Herbert Xuc1047952017-04-10 17:27:57 +0800327 ahash_restore_req(areq, err);
Marek Vasut1ffc9fb2014-03-14 02:37:05 +0100328
329 /* Complete the ORIGINAL request. */
330 areq->base.complete(&areq->base, err);
331}
332
333static int ahash_op_unaligned(struct ahash_request *req,
334 int (*op)(struct ahash_request *))
335{
336 int err;
337
338 err = ahash_save_req(req, ahash_op_unaligned_done);
339 if (err)
340 return err;
341
Herbert Xu66f6ce52009-07-15 12:40:40 +0800342 err = op(req);
Herbert Xuc1047952017-04-10 17:27:57 +0800343 if (err == -EINPROGRESS ||
344 (err == -EBUSY && (ahash_request_flags(req) &
345 CRYPTO_TFM_REQ_MAY_BACKLOG)))
346 return err;
347
348 ahash_restore_req(req, err);
Herbert Xu66f6ce52009-07-15 12:40:40 +0800349
350 return err;
351}
352
353static int crypto_ahash_op(struct ahash_request *req,
354 int (*op)(struct ahash_request *))
355{
356 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
357 unsigned long alignmask = crypto_ahash_alignmask(tfm);
358
359 if ((unsigned long)req->result & alignmask)
360 return ahash_op_unaligned(req, op);
361
362 return op(req);
363}
364
365int crypto_ahash_final(struct ahash_request *req)
366{
367 return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->final);
368}
369EXPORT_SYMBOL_GPL(crypto_ahash_final);
370
371int crypto_ahash_finup(struct ahash_request *req)
372{
373 return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->finup);
374}
375EXPORT_SYMBOL_GPL(crypto_ahash_finup);
376
377int crypto_ahash_digest(struct ahash_request *req)
378{
Eric Biggersadf26e82018-01-03 11:16:27 -0800379 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
380
381 if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
382 return -ENOKEY;
383
384 return crypto_ahash_op(req, tfm->digest);
Herbert Xu66f6ce52009-07-15 12:40:40 +0800385}
386EXPORT_SYMBOL_GPL(crypto_ahash_digest);
387
Herbert Xu66f6ce52009-07-15 12:40:40 +0800388static void ahash_def_finup_done2(struct crypto_async_request *req, int err)
389{
390 struct ahash_request *areq = req->data;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800391
Herbert Xuc1047952017-04-10 17:27:57 +0800392 if (err == -EINPROGRESS)
393 return;
394
395 ahash_restore_req(areq, err);
Herbert Xu66f6ce52009-07-15 12:40:40 +0800396
Marek Vasutd4a7a0f2014-03-14 02:37:06 +0100397 areq->base.complete(&areq->base, err);
Herbert Xu66f6ce52009-07-15 12:40:40 +0800398}
399
400static int ahash_def_finup_finish1(struct ahash_request *req, int err)
401{
402 if (err)
403 goto out;
404
405 req->base.complete = ahash_def_finup_done2;
Herbert Xuc1047952017-04-10 17:27:57 +0800406
Herbert Xu66f6ce52009-07-15 12:40:40 +0800407 err = crypto_ahash_reqtfm(req)->final(req);
Herbert Xuc1047952017-04-10 17:27:57 +0800408 if (err == -EINPROGRESS ||
409 (err == -EBUSY && (ahash_request_flags(req) &
410 CRYPTO_TFM_REQ_MAY_BACKLOG)))
411 return err;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800412
413out:
Herbert Xuc1047952017-04-10 17:27:57 +0800414 ahash_restore_req(req, err);
Herbert Xu66f6ce52009-07-15 12:40:40 +0800415 return err;
416}
417
418static void ahash_def_finup_done1(struct crypto_async_request *req, int err)
419{
420 struct ahash_request *areq = req->data;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800421
Herbert Xuc1047952017-04-10 17:27:57 +0800422 if (err == -EINPROGRESS) {
423 ahash_notify_einprogress(areq);
424 return;
425 }
426
427 areq->base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
428
Herbert Xu66f6ce52009-07-15 12:40:40 +0800429 err = ahash_def_finup_finish1(areq, err);
Herbert Xuc1047952017-04-10 17:27:57 +0800430 if (areq->priv)
431 return;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800432
Marek Vasutd4a7a0f2014-03-14 02:37:06 +0100433 areq->base.complete(&areq->base, err);
Herbert Xu66f6ce52009-07-15 12:40:40 +0800434}
435
436static int ahash_def_finup(struct ahash_request *req)
437{
438 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
Marek Vasutd4a7a0f2014-03-14 02:37:06 +0100439 int err;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800440
Marek Vasutd4a7a0f2014-03-14 02:37:06 +0100441 err = ahash_save_req(req, ahash_def_finup_done1);
442 if (err)
443 return err;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800444
Marek Vasutd4a7a0f2014-03-14 02:37:06 +0100445 err = tfm->update(req);
Herbert Xuc1047952017-04-10 17:27:57 +0800446 if (err == -EINPROGRESS ||
447 (err == -EBUSY && (ahash_request_flags(req) &
448 CRYPTO_TFM_REQ_MAY_BACKLOG)))
449 return err;
450
Marek Vasutd4a7a0f2014-03-14 02:37:06 +0100451 return ahash_def_finup_finish1(req, err);
Herbert Xu66f6ce52009-07-15 12:40:40 +0800452}
453
454static int ahash_no_export(struct ahash_request *req, void *out)
455{
456 return -ENOSYS;
457}
458
459static int ahash_no_import(struct ahash_request *req, const void *in)
460{
461 return -ENOSYS;
462}
463
Herbert Xu88056ec2009-07-14 12:28:26 +0800464static int crypto_ahash_init_tfm(struct crypto_tfm *tfm)
465{
466 struct crypto_ahash *hash = __crypto_ahash_cast(tfm);
467 struct ahash_alg *alg = crypto_ahash_alg(hash);
Herbert Xu88056ec2009-07-14 12:28:26 +0800468
Herbert Xu66f6ce52009-07-15 12:40:40 +0800469 hash->setkey = ahash_nosetkey;
470 hash->export = ahash_no_export;
471 hash->import = ahash_no_import;
472
Herbert Xu88056ec2009-07-14 12:28:26 +0800473 if (tfm->__crt_alg->cra_type != &crypto_ahash_type)
474 return crypto_init_shash_ops_async(tfm);
475
Herbert Xu88056ec2009-07-14 12:28:26 +0800476 hash->init = alg->init;
477 hash->update = alg->update;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800478 hash->final = alg->final;
479 hash->finup = alg->finup ?: ahash_def_finup;
Herbert Xu88056ec2009-07-14 12:28:26 +0800480 hash->digest = alg->digest;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800481
Herbert Xua5596d62016-01-08 21:28:26 +0800482 if (alg->setkey) {
Herbert Xu66f6ce52009-07-15 12:40:40 +0800483 hash->setkey = alg->setkey;
Eric Biggersadf26e82018-01-03 11:16:27 -0800484 if (!(alg->halg.base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY))
485 crypto_ahash_set_flags(hash, CRYPTO_TFM_NEED_KEY);
Herbert Xua5596d62016-01-08 21:28:26 +0800486 }
Herbert Xu66f6ce52009-07-15 12:40:40 +0800487 if (alg->export)
488 hash->export = alg->export;
489 if (alg->import)
490 hash->import = alg->import;
Herbert Xu88056ec2009-07-14 12:28:26 +0800491
492 return 0;
493}
494
495static unsigned int crypto_ahash_extsize(struct crypto_alg *alg)
496{
Herbert Xu2495cf22016-06-29 18:03:47 +0800497 if (alg->cra_type != &crypto_ahash_type)
498 return sizeof(struct crypto_shash *);
Herbert Xu88056ec2009-07-14 12:28:26 +0800499
Herbert Xu2495cf22016-06-29 18:03:47 +0800500 return crypto_alg_extsize(alg);
Herbert Xu88056ec2009-07-14 12:28:26 +0800501}
502
Herbert Xu3acc8472011-11-03 23:46:07 +1100503#ifdef CONFIG_NET
Steffen Klassert6238cba2011-09-27 07:41:07 +0200504static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg)
505{
506 struct crypto_report_hash rhash;
507
Mathias Krause9a5467b2013-02-05 18:19:13 +0100508 strncpy(rhash.type, "ahash", sizeof(rhash.type));
Steffen Klassert6238cba2011-09-27 07:41:07 +0200509
510 rhash.blocksize = alg->cra_blocksize;
511 rhash.digestsize = __crypto_hash_alg_common(alg)->digestsize;
512
David S. Miller6662df32012-04-01 20:19:05 -0400513 if (nla_put(skb, CRYPTOCFGA_REPORT_HASH,
514 sizeof(struct crypto_report_hash), &rhash))
515 goto nla_put_failure;
Steffen Klassert6238cba2011-09-27 07:41:07 +0200516 return 0;
517
518nla_put_failure:
519 return -EMSGSIZE;
520}
Herbert Xu3acc8472011-11-03 23:46:07 +1100521#else
522static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg)
523{
524 return -ENOSYS;
525}
526#endif
Steffen Klassert6238cba2011-09-27 07:41:07 +0200527
Loc Ho004a4032008-05-14 20:41:47 +0800528static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
529 __attribute__ ((unused));
530static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
531{
532 seq_printf(m, "type : ahash\n");
533 seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
534 "yes" : "no");
535 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
Herbert Xu88056ec2009-07-14 12:28:26 +0800536 seq_printf(m, "digestsize : %u\n",
537 __crypto_hash_alg_common(alg)->digestsize);
Loc Ho004a4032008-05-14 20:41:47 +0800538}
539
540const struct crypto_type crypto_ahash_type = {
Herbert Xu88056ec2009-07-14 12:28:26 +0800541 .extsize = crypto_ahash_extsize,
542 .init_tfm = crypto_ahash_init_tfm,
Loc Ho004a4032008-05-14 20:41:47 +0800543#ifdef CONFIG_PROC_FS
544 .show = crypto_ahash_show,
545#endif
Steffen Klassert6238cba2011-09-27 07:41:07 +0200546 .report = crypto_ahash_report,
Herbert Xu88056ec2009-07-14 12:28:26 +0800547 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
548 .maskset = CRYPTO_ALG_TYPE_AHASH_MASK,
549 .type = CRYPTO_ALG_TYPE_AHASH,
550 .tfmsize = offsetof(struct crypto_ahash, base),
Loc Ho004a4032008-05-14 20:41:47 +0800551};
552EXPORT_SYMBOL_GPL(crypto_ahash_type);
553
Herbert Xu88056ec2009-07-14 12:28:26 +0800554struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
555 u32 mask)
556{
557 return crypto_alloc_tfm(alg_name, &crypto_ahash_type, type, mask);
558}
559EXPORT_SYMBOL_GPL(crypto_alloc_ahash);
560
Herbert Xu8d18e342016-01-23 13:52:40 +0800561int crypto_has_ahash(const char *alg_name, u32 type, u32 mask)
562{
563 return crypto_type_has_alg(alg_name, &crypto_ahash_type, type, mask);
564}
565EXPORT_SYMBOL_GPL(crypto_has_ahash);
566
Herbert Xu01c2dec2009-07-14 14:06:06 +0800567static int ahash_prepare_alg(struct ahash_alg *alg)
568{
569 struct crypto_alg *base = &alg->halg.base;
570
571 if (alg->halg.digestsize > PAGE_SIZE / 8 ||
Russell King8996eaf2015-10-09 20:43:33 +0100572 alg->halg.statesize > PAGE_SIZE / 8 ||
573 alg->halg.statesize == 0)
Herbert Xu01c2dec2009-07-14 14:06:06 +0800574 return -EINVAL;
575
576 base->cra_type = &crypto_ahash_type;
577 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
578 base->cra_flags |= CRYPTO_ALG_TYPE_AHASH;
579
580 return 0;
581}
582
583int crypto_register_ahash(struct ahash_alg *alg)
584{
585 struct crypto_alg *base = &alg->halg.base;
586 int err;
587
588 err = ahash_prepare_alg(alg);
589 if (err)
590 return err;
591
592 return crypto_register_alg(base);
593}
594EXPORT_SYMBOL_GPL(crypto_register_ahash);
595
596int crypto_unregister_ahash(struct ahash_alg *alg)
597{
598 return crypto_unregister_alg(&alg->halg.base);
599}
600EXPORT_SYMBOL_GPL(crypto_unregister_ahash);
601
602int ahash_register_instance(struct crypto_template *tmpl,
603 struct ahash_instance *inst)
604{
605 int err;
606
607 err = ahash_prepare_alg(&inst->alg);
608 if (err)
609 return err;
610
611 return crypto_register_instance(tmpl, ahash_crypto_instance(inst));
612}
613EXPORT_SYMBOL_GPL(ahash_register_instance);
614
615void ahash_free_instance(struct crypto_instance *inst)
616{
617 crypto_drop_spawn(crypto_instance_ctx(inst));
618 kfree(ahash_instance(inst));
619}
620EXPORT_SYMBOL_GPL(ahash_free_instance);
621
622int crypto_init_ahash_spawn(struct crypto_ahash_spawn *spawn,
623 struct hash_alg_common *alg,
624 struct crypto_instance *inst)
625{
626 return crypto_init_spawn2(&spawn->base, &alg->base, inst,
627 &crypto_ahash_type);
628}
629EXPORT_SYMBOL_GPL(crypto_init_ahash_spawn);
630
631struct hash_alg_common *ahash_attr_alg(struct rtattr *rta, u32 type, u32 mask)
632{
633 struct crypto_alg *alg;
634
635 alg = crypto_attr_alg2(rta, &crypto_ahash_type, type, mask);
636 return IS_ERR(alg) ? ERR_CAST(alg) : __crypto_hash_alg_common(alg);
637}
638EXPORT_SYMBOL_GPL(ahash_attr_alg);
639
Eric Biggersd2b492b2018-01-03 11:16:22 -0800640bool crypto_hash_alg_has_setkey(struct hash_alg_common *halg)
641{
642 struct crypto_alg *alg = &halg->base;
643
644 if (alg->cra_type != &crypto_ahash_type)
645 return crypto_shash_alg_has_setkey(__crypto_shash_alg(alg));
646
647 return __crypto_ahash_alg(alg)->setkey != NULL;
648}
649EXPORT_SYMBOL_GPL(crypto_hash_alg_has_setkey);
650
Loc Ho004a4032008-05-14 20:41:47 +0800651MODULE_LICENSE("GPL");
652MODULE_DESCRIPTION("Asynchronous cryptographic hash type");