blob: 8454b3cbd71afe9a4cebecd49546f7a585d380ae [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>
Loc Ho004a4032008-05-14 20:41:47 +080018#include <linux/err.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/sched.h>
22#include <linux/slab.h>
23#include <linux/seq_file.h>
Steffen Klassert6238cba2011-09-27 07:41:07 +020024#include <linux/cryptouser.h>
25#include <net/netlink.h>
Loc Ho004a4032008-05-14 20:41:47 +080026
27#include "internal.h"
28
Herbert Xu66f6ce52009-07-15 12:40:40 +080029struct ahash_request_priv {
30 crypto_completion_t complete;
31 void *data;
32 u8 *result;
Herbert Xu27c3e162017-04-10 17:27:57 +080033 u32 flags;
Herbert Xu66f6ce52009-07-15 12:40:40 +080034 void *ubuf[] CRYPTO_MINALIGN_ATTR;
35};
36
Herbert Xu88056ec2009-07-14 12:28:26 +080037static inline struct ahash_alg *crypto_ahash_alg(struct crypto_ahash *hash)
38{
39 return container_of(crypto_hash_alg_common(hash), struct ahash_alg,
40 halg);
41}
42
Herbert Xu20036252008-07-07 22:19:53 +080043static int hash_walk_next(struct crypto_hash_walk *walk)
44{
45 unsigned int alignmask = walk->alignmask;
46 unsigned int offset = walk->offset;
47 unsigned int nbytes = min(walk->entrylen,
48 ((unsigned int)(PAGE_SIZE)) - offset);
49
Cong Wangf0dfc0b2011-11-25 23:14:17 +080050 walk->data = kmap_atomic(walk->pg);
Herbert Xu20036252008-07-07 22:19:53 +080051 walk->data += offset;
52
Szilveszter Ördög23a75ee2010-08-06 09:26:38 +080053 if (offset & alignmask) {
54 unsigned int unaligned = alignmask + 1 - (offset & alignmask);
55 if (nbytes > unaligned)
56 nbytes = unaligned;
57 }
Herbert Xu20036252008-07-07 22:19:53 +080058
59 walk->entrylen -= nbytes;
60 return nbytes;
61}
62
63static int hash_walk_new_entry(struct crypto_hash_walk *walk)
64{
65 struct scatterlist *sg;
66
67 sg = walk->sg;
68 walk->pg = sg_page(sg);
69 walk->offset = sg->offset;
70 walk->entrylen = sg->length;
71
72 if (walk->entrylen > walk->total)
73 walk->entrylen = walk->total;
74 walk->total -= walk->entrylen;
75
76 return hash_walk_next(walk);
77}
78
79int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err)
80{
81 unsigned int alignmask = walk->alignmask;
82 unsigned int nbytes = walk->entrylen;
83
84 walk->data -= walk->offset;
85
86 if (nbytes && walk->offset & alignmask && !err) {
Herbert Xu20036252008-07-07 22:19:53 +080087 walk->offset = ALIGN(walk->offset, alignmask + 1);
88 walk->data += walk->offset;
89
90 nbytes = min(nbytes,
91 ((unsigned int)(PAGE_SIZE)) - walk->offset);
92 walk->entrylen -= nbytes;
93
94 return nbytes;
95 }
96
Cong Wangf0dfc0b2011-11-25 23:14:17 +080097 kunmap_atomic(walk->data);
Herbert Xu20036252008-07-07 22:19:53 +080098 crypto_yield(walk->flags);
99
100 if (err)
101 return err;
102
Herbert Xud315a0e2009-05-31 23:09:22 +1000103 if (nbytes) {
104 walk->offset = 0;
105 walk->pg++;
Herbert Xu20036252008-07-07 22:19:53 +0800106 return hash_walk_next(walk);
Herbert Xud315a0e2009-05-31 23:09:22 +1000107 }
Herbert Xu20036252008-07-07 22:19:53 +0800108
109 if (!walk->total)
110 return 0;
111
112 walk->sg = scatterwalk_sg_next(walk->sg);
113
114 return hash_walk_new_entry(walk);
115}
116EXPORT_SYMBOL_GPL(crypto_hash_walk_done);
117
118int crypto_hash_walk_first(struct ahash_request *req,
119 struct crypto_hash_walk *walk)
120{
121 walk->total = req->nbytes;
122
123 if (!walk->total)
124 return 0;
125
126 walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req));
127 walk->sg = req->src;
128 walk->flags = req->base.flags;
129
130 return hash_walk_new_entry(walk);
131}
132EXPORT_SYMBOL_GPL(crypto_hash_walk_first);
133
Herbert Xu5f7082e2008-08-31 22:21:09 +1000134int crypto_hash_walk_first_compat(struct hash_desc *hdesc,
135 struct crypto_hash_walk *walk,
136 struct scatterlist *sg, unsigned int len)
137{
138 walk->total = len;
139
140 if (!walk->total)
141 return 0;
142
143 walk->alignmask = crypto_hash_alignmask(hdesc->tfm);
144 walk->sg = sg;
145 walk->flags = hdesc->flags;
146
147 return hash_walk_new_entry(walk);
148}
149
Loc Ho004a4032008-05-14 20:41:47 +0800150static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key,
151 unsigned int keylen)
152{
Loc Ho004a4032008-05-14 20:41:47 +0800153 unsigned long alignmask = crypto_ahash_alignmask(tfm);
154 int ret;
155 u8 *buffer, *alignbuffer;
156 unsigned long absize;
157
158 absize = keylen + alignmask;
Herbert Xu093900c2009-07-14 21:48:35 +0800159 buffer = kmalloc(absize, GFP_KERNEL);
Loc Ho004a4032008-05-14 20:41:47 +0800160 if (!buffer)
161 return -ENOMEM;
162
163 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
164 memcpy(alignbuffer, key, keylen);
Herbert Xua70c5222009-07-15 20:39:05 +0800165 ret = tfm->setkey(tfm, alignbuffer, keylen);
Herbert Xu8c32c512009-07-14 21:35:36 +0800166 kzfree(buffer);
Loc Ho004a4032008-05-14 20:41:47 +0800167 return ret;
168}
169
Herbert Xu66f6ce52009-07-15 12:40:40 +0800170int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
Loc Ho004a4032008-05-14 20:41:47 +0800171 unsigned int keylen)
172{
Loc Ho004a4032008-05-14 20:41:47 +0800173 unsigned long alignmask = crypto_ahash_alignmask(tfm);
174
175 if ((unsigned long)key & alignmask)
176 return ahash_setkey_unaligned(tfm, key, keylen);
177
Herbert Xua70c5222009-07-15 20:39:05 +0800178 return tfm->setkey(tfm, key, keylen);
Loc Ho004a4032008-05-14 20:41:47 +0800179}
Herbert Xu66f6ce52009-07-15 12:40:40 +0800180EXPORT_SYMBOL_GPL(crypto_ahash_setkey);
Loc Ho004a4032008-05-14 20:41:47 +0800181
Herbert Xu3751f402008-11-08 08:56:57 +0800182static int ahash_nosetkey(struct crypto_ahash *tfm, const u8 *key,
183 unsigned int keylen)
184{
185 return -ENOSYS;
186}
187
Herbert Xu66f6ce52009-07-15 12:40:40 +0800188static inline unsigned int ahash_align_buffer_size(unsigned len,
189 unsigned long mask)
190{
191 return len + (mask & ~(crypto_tfm_ctx_alignment() - 1));
192}
193
Marek Vasut84ee9de2014-03-14 02:37:05 +0100194static int ahash_save_req(struct ahash_request *req, crypto_completion_t cplt)
Herbert Xu66f6ce52009-07-15 12:40:40 +0800195{
196 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
197 unsigned long alignmask = crypto_ahash_alignmask(tfm);
198 unsigned int ds = crypto_ahash_digestsize(tfm);
199 struct ahash_request_priv *priv;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800200
201 priv = kmalloc(sizeof(*priv) + ahash_align_buffer_size(ds, alignmask),
202 (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
Steffen Klassert5befbd52009-07-24 13:56:31 +0800203 GFP_KERNEL : GFP_ATOMIC);
Herbert Xu66f6ce52009-07-15 12:40:40 +0800204 if (!priv)
205 return -ENOMEM;
206
Marek Vasut43717642014-03-14 02:37:04 +0100207 /*
208 * WARNING: Voodoo programming below!
209 *
210 * The code below is obscure and hard to understand, thus explanation
211 * is necessary. See include/crypto/hash.h and include/linux/crypto.h
212 * to understand the layout of structures used here!
213 *
214 * The code here will replace portions of the ORIGINAL request with
215 * pointers to new code and buffers so the hashing operation can store
216 * the result in aligned buffer. We will call the modified request
217 * an ADJUSTED request.
218 *
219 * The newly mangled request will look as such:
220 *
221 * req {
222 * .result = ADJUSTED[new aligned buffer]
223 * .base.complete = ADJUSTED[pointer to completion function]
224 * .base.data = ADJUSTED[*req (pointer to self)]
225 * .priv = ADJUSTED[new priv] {
226 * .result = ORIGINAL(result)
227 * .complete = ORIGINAL(base.complete)
228 * .data = ORIGINAL(base.data)
229 * }
230 */
231
Herbert Xu66f6ce52009-07-15 12:40:40 +0800232 priv->result = req->result;
233 priv->complete = req->base.complete;
234 priv->data = req->base.data;
Herbert Xu27c3e162017-04-10 17:27:57 +0800235 priv->flags = req->base.flags;
236
Marek Vasut43717642014-03-14 02:37:04 +0100237 /*
238 * WARNING: We do not backup req->priv here! The req->priv
239 * is for internal use of the Crypto API and the
240 * user must _NOT_ _EVER_ depend on it's content!
241 */
Herbert Xu66f6ce52009-07-15 12:40:40 +0800242
243 req->result = PTR_ALIGN((u8 *)priv->ubuf, alignmask + 1);
Marek Vasut84ee9de2014-03-14 02:37:05 +0100244 req->base.complete = cplt;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800245 req->base.data = req;
246 req->priv = priv;
247
Marek Vasut84ee9de2014-03-14 02:37:05 +0100248 return 0;
249}
250
Herbert Xu27c3e162017-04-10 17:27:57 +0800251static void ahash_restore_req(struct ahash_request *req, int err)
Marek Vasut84ee9de2014-03-14 02:37:05 +0100252{
253 struct ahash_request_priv *priv = req->priv;
254
Herbert Xu27c3e162017-04-10 17:27:57 +0800255 if (!err)
256 memcpy(priv->result, req->result,
257 crypto_ahash_digestsize(crypto_ahash_reqtfm(req)));
258
Marek Vasut84ee9de2014-03-14 02:37:05 +0100259 /* Restore the original crypto request. */
260 req->result = priv->result;
Herbert Xu27c3e162017-04-10 17:27:57 +0800261
262 ahash_request_set_callback(req, priv->flags,
263 priv->complete, priv->data);
Marek Vasut84ee9de2014-03-14 02:37:05 +0100264 req->priv = NULL;
265
266 /* Free the req->priv.priv from the ADJUSTED request. */
267 kzfree(priv);
268}
269
Herbert Xu27c3e162017-04-10 17:27:57 +0800270static void ahash_notify_einprogress(struct ahash_request *req)
Marek Vasut84ee9de2014-03-14 02:37:05 +0100271{
272 struct ahash_request_priv *priv = req->priv;
Herbert Xu27c3e162017-04-10 17:27:57 +0800273 struct crypto_async_request oreq;
Marek Vasut84ee9de2014-03-14 02:37:05 +0100274
Herbert Xu27c3e162017-04-10 17:27:57 +0800275 oreq.data = priv->data;
Marek Vasut84ee9de2014-03-14 02:37:05 +0100276
Herbert Xu27c3e162017-04-10 17:27:57 +0800277 priv->complete(&oreq, -EINPROGRESS);
Marek Vasut84ee9de2014-03-14 02:37:05 +0100278}
279
280static void ahash_op_unaligned_done(struct crypto_async_request *req, int err)
281{
282 struct ahash_request *areq = req->data;
283
Herbert Xu27c3e162017-04-10 17:27:57 +0800284 if (err == -EINPROGRESS) {
285 ahash_notify_einprogress(areq);
286 return;
287 }
288
Marek Vasut84ee9de2014-03-14 02:37:05 +0100289 /*
290 * Restore the original request, see ahash_op_unaligned() for what
291 * goes where.
292 *
293 * The "struct ahash_request *req" here is in fact the "req.base"
294 * from the ADJUSTED request from ahash_op_unaligned(), thus as it
295 * is a pointer to self, it is also the ADJUSTED "req" .
296 */
297
298 /* First copy req->result into req->priv.result */
Herbert Xu27c3e162017-04-10 17:27:57 +0800299 ahash_restore_req(areq, err);
Marek Vasut84ee9de2014-03-14 02:37:05 +0100300
301 /* Complete the ORIGINAL request. */
302 areq->base.complete(&areq->base, err);
303}
304
305static int ahash_op_unaligned(struct ahash_request *req,
306 int (*op)(struct ahash_request *))
307{
308 int err;
309
310 err = ahash_save_req(req, ahash_op_unaligned_done);
311 if (err)
312 return err;
313
Herbert Xu66f6ce52009-07-15 12:40:40 +0800314 err = op(req);
Herbert Xu27c3e162017-04-10 17:27:57 +0800315 if (err == -EINPROGRESS ||
316 (err == -EBUSY && (ahash_request_flags(req) &
317 CRYPTO_TFM_REQ_MAY_BACKLOG)))
318 return err;
319
320 ahash_restore_req(req, err);
Herbert Xu66f6ce52009-07-15 12:40:40 +0800321
322 return err;
323}
324
325static int crypto_ahash_op(struct ahash_request *req,
326 int (*op)(struct ahash_request *))
327{
328 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
329 unsigned long alignmask = crypto_ahash_alignmask(tfm);
330
331 if ((unsigned long)req->result & alignmask)
332 return ahash_op_unaligned(req, op);
333
334 return op(req);
335}
336
337int crypto_ahash_final(struct ahash_request *req)
338{
339 return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->final);
340}
341EXPORT_SYMBOL_GPL(crypto_ahash_final);
342
343int crypto_ahash_finup(struct ahash_request *req)
344{
345 return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->finup);
346}
347EXPORT_SYMBOL_GPL(crypto_ahash_finup);
348
349int crypto_ahash_digest(struct ahash_request *req)
350{
351 return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->digest);
352}
353EXPORT_SYMBOL_GPL(crypto_ahash_digest);
354
Herbert Xu66f6ce52009-07-15 12:40:40 +0800355static void ahash_def_finup_done2(struct crypto_async_request *req, int err)
356{
357 struct ahash_request *areq = req->data;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800358
Herbert Xu27c3e162017-04-10 17:27:57 +0800359 if (err == -EINPROGRESS)
360 return;
361
362 ahash_restore_req(areq, err);
Herbert Xu66f6ce52009-07-15 12:40:40 +0800363
Marek Vasut9b01b4e2014-03-14 02:37:06 +0100364 areq->base.complete(&areq->base, err);
Herbert Xu66f6ce52009-07-15 12:40:40 +0800365}
366
367static int ahash_def_finup_finish1(struct ahash_request *req, int err)
368{
369 if (err)
370 goto out;
371
372 req->base.complete = ahash_def_finup_done2;
Herbert Xu27c3e162017-04-10 17:27:57 +0800373
Herbert Xu66f6ce52009-07-15 12:40:40 +0800374 err = crypto_ahash_reqtfm(req)->final(req);
Herbert Xu27c3e162017-04-10 17:27:57 +0800375 if (err == -EINPROGRESS ||
376 (err == -EBUSY && (ahash_request_flags(req) &
377 CRYPTO_TFM_REQ_MAY_BACKLOG)))
378 return err;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800379
380out:
Herbert Xu27c3e162017-04-10 17:27:57 +0800381 ahash_restore_req(req, err);
Herbert Xu66f6ce52009-07-15 12:40:40 +0800382 return err;
383}
384
385static void ahash_def_finup_done1(struct crypto_async_request *req, int err)
386{
387 struct ahash_request *areq = req->data;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800388
Herbert Xu27c3e162017-04-10 17:27:57 +0800389 if (err == -EINPROGRESS) {
390 ahash_notify_einprogress(areq);
391 return;
392 }
393
394 areq->base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
395
Herbert Xu66f6ce52009-07-15 12:40:40 +0800396 err = ahash_def_finup_finish1(areq, err);
Herbert Xu27c3e162017-04-10 17:27:57 +0800397 if (areq->priv)
398 return;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800399
Marek Vasut9b01b4e2014-03-14 02:37:06 +0100400 areq->base.complete(&areq->base, err);
Herbert Xu66f6ce52009-07-15 12:40:40 +0800401}
402
403static int ahash_def_finup(struct ahash_request *req)
404{
405 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
Marek Vasut9b01b4e2014-03-14 02:37:06 +0100406 int err;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800407
Marek Vasut9b01b4e2014-03-14 02:37:06 +0100408 err = ahash_save_req(req, ahash_def_finup_done1);
409 if (err)
410 return err;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800411
Marek Vasut9b01b4e2014-03-14 02:37:06 +0100412 err = tfm->update(req);
Herbert Xu27c3e162017-04-10 17:27:57 +0800413 if (err == -EINPROGRESS ||
414 (err == -EBUSY && (ahash_request_flags(req) &
415 CRYPTO_TFM_REQ_MAY_BACKLOG)))
416 return err;
417
Marek Vasut9b01b4e2014-03-14 02:37:06 +0100418 return ahash_def_finup_finish1(req, err);
Herbert Xu66f6ce52009-07-15 12:40:40 +0800419}
420
421static int ahash_no_export(struct ahash_request *req, void *out)
422{
423 return -ENOSYS;
424}
425
426static int ahash_no_import(struct ahash_request *req, const void *in)
427{
428 return -ENOSYS;
429}
430
Herbert Xu88056ec2009-07-14 12:28:26 +0800431static int crypto_ahash_init_tfm(struct crypto_tfm *tfm)
432{
433 struct crypto_ahash *hash = __crypto_ahash_cast(tfm);
434 struct ahash_alg *alg = crypto_ahash_alg(hash);
Herbert Xu88056ec2009-07-14 12:28:26 +0800435
Herbert Xu66f6ce52009-07-15 12:40:40 +0800436 hash->setkey = ahash_nosetkey;
437 hash->export = ahash_no_export;
438 hash->import = ahash_no_import;
439
Herbert Xu88056ec2009-07-14 12:28:26 +0800440 if (tfm->__crt_alg->cra_type != &crypto_ahash_type)
441 return crypto_init_shash_ops_async(tfm);
442
Herbert Xu88056ec2009-07-14 12:28:26 +0800443 hash->init = alg->init;
444 hash->update = alg->update;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800445 hash->final = alg->final;
446 hash->finup = alg->finup ?: ahash_def_finup;
Herbert Xu88056ec2009-07-14 12:28:26 +0800447 hash->digest = alg->digest;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800448
449 if (alg->setkey)
450 hash->setkey = alg->setkey;
451 if (alg->export)
452 hash->export = alg->export;
453 if (alg->import)
454 hash->import = alg->import;
Herbert Xu88056ec2009-07-14 12:28:26 +0800455
456 return 0;
457}
458
459static unsigned int crypto_ahash_extsize(struct crypto_alg *alg)
460{
461 if (alg->cra_type == &crypto_ahash_type)
462 return alg->cra_ctxsize;
463
464 return sizeof(struct crypto_shash *);
465}
466
Herbert Xu3acc8472011-11-03 23:46:07 +1100467#ifdef CONFIG_NET
Steffen Klassert6238cba2011-09-27 07:41:07 +0200468static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg)
469{
470 struct crypto_report_hash rhash;
471
Mathias Krauseb71a6fa2013-02-05 18:19:13 +0100472 strncpy(rhash.type, "ahash", sizeof(rhash.type));
Steffen Klassert6238cba2011-09-27 07:41:07 +0200473
474 rhash.blocksize = alg->cra_blocksize;
475 rhash.digestsize = __crypto_hash_alg_common(alg)->digestsize;
476
477 NLA_PUT(skb, CRYPTOCFGA_REPORT_HASH,
478 sizeof(struct crypto_report_hash), &rhash);
479
480 return 0;
481
482nla_put_failure:
483 return -EMSGSIZE;
484}
Herbert Xu3acc8472011-11-03 23:46:07 +1100485#else
486static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg)
487{
488 return -ENOSYS;
489}
490#endif
Steffen Klassert6238cba2011-09-27 07:41:07 +0200491
Loc Ho004a4032008-05-14 20:41:47 +0800492static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
493 __attribute__ ((unused));
494static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
495{
496 seq_printf(m, "type : ahash\n");
497 seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
498 "yes" : "no");
499 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
Herbert Xu88056ec2009-07-14 12:28:26 +0800500 seq_printf(m, "digestsize : %u\n",
501 __crypto_hash_alg_common(alg)->digestsize);
Loc Ho004a4032008-05-14 20:41:47 +0800502}
503
504const struct crypto_type crypto_ahash_type = {
Herbert Xu88056ec2009-07-14 12:28:26 +0800505 .extsize = crypto_ahash_extsize,
506 .init_tfm = crypto_ahash_init_tfm,
Loc Ho004a4032008-05-14 20:41:47 +0800507#ifdef CONFIG_PROC_FS
508 .show = crypto_ahash_show,
509#endif
Steffen Klassert6238cba2011-09-27 07:41:07 +0200510 .report = crypto_ahash_report,
Herbert Xu88056ec2009-07-14 12:28:26 +0800511 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
512 .maskset = CRYPTO_ALG_TYPE_AHASH_MASK,
513 .type = CRYPTO_ALG_TYPE_AHASH,
514 .tfmsize = offsetof(struct crypto_ahash, base),
Loc Ho004a4032008-05-14 20:41:47 +0800515};
516EXPORT_SYMBOL_GPL(crypto_ahash_type);
517
Herbert Xu88056ec2009-07-14 12:28:26 +0800518struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
519 u32 mask)
520{
521 return crypto_alloc_tfm(alg_name, &crypto_ahash_type, type, mask);
522}
523EXPORT_SYMBOL_GPL(crypto_alloc_ahash);
524
Herbert Xu01c2dec2009-07-14 14:06:06 +0800525static int ahash_prepare_alg(struct ahash_alg *alg)
526{
527 struct crypto_alg *base = &alg->halg.base;
528
529 if (alg->halg.digestsize > PAGE_SIZE / 8 ||
530 alg->halg.statesize > PAGE_SIZE / 8)
531 return -EINVAL;
532
533 base->cra_type = &crypto_ahash_type;
534 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
535 base->cra_flags |= CRYPTO_ALG_TYPE_AHASH;
536
537 return 0;
538}
539
540int crypto_register_ahash(struct ahash_alg *alg)
541{
542 struct crypto_alg *base = &alg->halg.base;
543 int err;
544
545 err = ahash_prepare_alg(alg);
546 if (err)
547 return err;
548
549 return crypto_register_alg(base);
550}
551EXPORT_SYMBOL_GPL(crypto_register_ahash);
552
553int crypto_unregister_ahash(struct ahash_alg *alg)
554{
555 return crypto_unregister_alg(&alg->halg.base);
556}
557EXPORT_SYMBOL_GPL(crypto_unregister_ahash);
558
559int ahash_register_instance(struct crypto_template *tmpl,
560 struct ahash_instance *inst)
561{
562 int err;
563
564 err = ahash_prepare_alg(&inst->alg);
565 if (err)
566 return err;
567
568 return crypto_register_instance(tmpl, ahash_crypto_instance(inst));
569}
570EXPORT_SYMBOL_GPL(ahash_register_instance);
571
572void ahash_free_instance(struct crypto_instance *inst)
573{
574 crypto_drop_spawn(crypto_instance_ctx(inst));
575 kfree(ahash_instance(inst));
576}
577EXPORT_SYMBOL_GPL(ahash_free_instance);
578
579int crypto_init_ahash_spawn(struct crypto_ahash_spawn *spawn,
580 struct hash_alg_common *alg,
581 struct crypto_instance *inst)
582{
583 return crypto_init_spawn2(&spawn->base, &alg->base, inst,
584 &crypto_ahash_type);
585}
586EXPORT_SYMBOL_GPL(crypto_init_ahash_spawn);
587
588struct hash_alg_common *ahash_attr_alg(struct rtattr *rta, u32 type, u32 mask)
589{
590 struct crypto_alg *alg;
591
592 alg = crypto_attr_alg2(rta, &crypto_ahash_type, type, mask);
593 return IS_ERR(alg) ? ERR_CAST(alg) : __crypto_hash_alg_common(alg);
594}
595EXPORT_SYMBOL_GPL(ahash_attr_alg);
596
Loc Ho004a4032008-05-14 20:41:47 +0800597MODULE_LICENSE("GPL");
598MODULE_DESCRIPTION("Asynchronous cryptographic hash type");