blob: f6a36a52d738b44251deaeaa2dd18ebee39da421 [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;
34 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
Herbert Xu75ecb232014-05-21 20:56:12 +080050 if (walk->flags & CRYPTO_ALG_ASYNC)
51 walk->data = kmap(walk->pg);
52 else
53 walk->data = kmap_atomic(walk->pg);
Herbert Xu20036252008-07-07 22:19:53 +080054 walk->data += offset;
55
Szilveszter Ördög23a75ee2010-08-06 09:26:38 +080056 if (offset & alignmask) {
57 unsigned int unaligned = alignmask + 1 - (offset & alignmask);
58 if (nbytes > unaligned)
59 nbytes = unaligned;
60 }
Herbert Xu20036252008-07-07 22:19:53 +080061
62 walk->entrylen -= nbytes;
63 return nbytes;
64}
65
66static int hash_walk_new_entry(struct crypto_hash_walk *walk)
67{
68 struct scatterlist *sg;
69
70 sg = walk->sg;
71 walk->pg = sg_page(sg);
72 walk->offset = sg->offset;
73 walk->entrylen = sg->length;
74
75 if (walk->entrylen > walk->total)
76 walk->entrylen = walk->total;
77 walk->total -= walk->entrylen;
78
79 return hash_walk_next(walk);
80}
81
82int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err)
83{
84 unsigned int alignmask = walk->alignmask;
85 unsigned int nbytes = walk->entrylen;
86
87 walk->data -= walk->offset;
88
89 if (nbytes && walk->offset & alignmask && !err) {
Herbert Xu20036252008-07-07 22:19:53 +080090 walk->offset = ALIGN(walk->offset, alignmask + 1);
91 walk->data += walk->offset;
92
93 nbytes = min(nbytes,
94 ((unsigned int)(PAGE_SIZE)) - walk->offset);
95 walk->entrylen -= nbytes;
96
97 return nbytes;
98 }
99
Herbert Xu75ecb232014-05-21 20:56:12 +0800100 if (walk->flags & CRYPTO_ALG_ASYNC)
101 kunmap(walk->pg);
102 else {
103 kunmap_atomic(walk->data);
104 /*
105 * The may sleep test only makes sense for sync users.
106 * Async users don't need to sleep here anyway.
107 */
108 crypto_yield(walk->flags);
109 }
Herbert Xu20036252008-07-07 22:19:53 +0800110
111 if (err)
112 return err;
113
Herbert Xud315a0e2009-05-31 23:09:22 +1000114 if (nbytes) {
115 walk->offset = 0;
116 walk->pg++;
Herbert Xu20036252008-07-07 22:19:53 +0800117 return hash_walk_next(walk);
Herbert Xud315a0e2009-05-31 23:09:22 +1000118 }
Herbert Xu20036252008-07-07 22:19:53 +0800119
120 if (!walk->total)
121 return 0;
122
123 walk->sg = scatterwalk_sg_next(walk->sg);
124
125 return hash_walk_new_entry(walk);
126}
127EXPORT_SYMBOL_GPL(crypto_hash_walk_done);
128
129int crypto_hash_walk_first(struct ahash_request *req,
130 struct crypto_hash_walk *walk)
131{
132 walk->total = req->nbytes;
133
Tim Chen6d9529c2014-07-10 16:18:08 -0700134 if (!walk->total) {
135 walk->entrylen = 0;
Herbert Xu20036252008-07-07 22:19:53 +0800136 return 0;
Tim Chen6d9529c2014-07-10 16:18:08 -0700137 }
Herbert Xu20036252008-07-07 22:19:53 +0800138
139 walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req));
140 walk->sg = req->src;
Herbert Xu75ecb232014-05-21 20:56:12 +0800141 walk->flags = req->base.flags & CRYPTO_TFM_REQ_MASK;
Herbert Xu20036252008-07-07 22:19:53 +0800142
143 return hash_walk_new_entry(walk);
144}
145EXPORT_SYMBOL_GPL(crypto_hash_walk_first);
146
Herbert Xu75ecb232014-05-21 20:56:12 +0800147int crypto_ahash_walk_first(struct ahash_request *req,
148 struct crypto_hash_walk *walk)
149{
150 walk->total = req->nbytes;
151
Tim Chen6d9529c2014-07-10 16:18:08 -0700152 if (!walk->total) {
153 walk->entrylen = 0;
Herbert Xu75ecb232014-05-21 20:56:12 +0800154 return 0;
Tim Chen6d9529c2014-07-10 16:18:08 -0700155 }
Herbert Xu75ecb232014-05-21 20:56:12 +0800156
157 walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req));
158 walk->sg = req->src;
159 walk->flags = req->base.flags & CRYPTO_TFM_REQ_MASK;
160 walk->flags |= CRYPTO_ALG_ASYNC;
161
162 BUILD_BUG_ON(CRYPTO_TFM_REQ_MASK & CRYPTO_ALG_ASYNC);
163
164 return hash_walk_new_entry(walk);
165}
166EXPORT_SYMBOL_GPL(crypto_ahash_walk_first);
167
Herbert Xu5f7082e2008-08-31 22:21:09 +1000168int crypto_hash_walk_first_compat(struct hash_desc *hdesc,
169 struct crypto_hash_walk *walk,
170 struct scatterlist *sg, unsigned int len)
171{
172 walk->total = len;
173
Tim Chen6d9529c2014-07-10 16:18:08 -0700174 if (!walk->total) {
175 walk->entrylen = 0;
Herbert Xu5f7082e2008-08-31 22:21:09 +1000176 return 0;
Tim Chen6d9529c2014-07-10 16:18:08 -0700177 }
Herbert Xu5f7082e2008-08-31 22:21:09 +1000178
179 walk->alignmask = crypto_hash_alignmask(hdesc->tfm);
180 walk->sg = sg;
Herbert Xu75ecb232014-05-21 20:56:12 +0800181 walk->flags = hdesc->flags & CRYPTO_TFM_REQ_MASK;
Herbert Xu5f7082e2008-08-31 22:21:09 +1000182
183 return hash_walk_new_entry(walk);
184}
185
Loc Ho004a4032008-05-14 20:41:47 +0800186static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key,
187 unsigned int keylen)
188{
Loc Ho004a4032008-05-14 20:41:47 +0800189 unsigned long alignmask = crypto_ahash_alignmask(tfm);
190 int ret;
191 u8 *buffer, *alignbuffer;
192 unsigned long absize;
193
194 absize = keylen + alignmask;
Herbert Xu093900c2009-07-14 21:48:35 +0800195 buffer = kmalloc(absize, GFP_KERNEL);
Loc Ho004a4032008-05-14 20:41:47 +0800196 if (!buffer)
197 return -ENOMEM;
198
199 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
200 memcpy(alignbuffer, key, keylen);
Herbert Xua70c5222009-07-15 20:39:05 +0800201 ret = tfm->setkey(tfm, alignbuffer, keylen);
Herbert Xu8c32c512009-07-14 21:35:36 +0800202 kzfree(buffer);
Loc Ho004a4032008-05-14 20:41:47 +0800203 return ret;
204}
205
Herbert Xu66f6ce52009-07-15 12:40:40 +0800206int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
Loc Ho004a4032008-05-14 20:41:47 +0800207 unsigned int keylen)
208{
Loc Ho004a4032008-05-14 20:41:47 +0800209 unsigned long alignmask = crypto_ahash_alignmask(tfm);
210
211 if ((unsigned long)key & alignmask)
212 return ahash_setkey_unaligned(tfm, key, keylen);
213
Herbert Xua70c5222009-07-15 20:39:05 +0800214 return tfm->setkey(tfm, key, keylen);
Loc Ho004a4032008-05-14 20:41:47 +0800215}
Herbert Xu66f6ce52009-07-15 12:40:40 +0800216EXPORT_SYMBOL_GPL(crypto_ahash_setkey);
Loc Ho004a4032008-05-14 20:41:47 +0800217
Herbert Xu3751f402008-11-08 08:56:57 +0800218static int ahash_nosetkey(struct crypto_ahash *tfm, const u8 *key,
219 unsigned int keylen)
220{
221 return -ENOSYS;
222}
223
Herbert Xu66f6ce52009-07-15 12:40:40 +0800224static inline unsigned int ahash_align_buffer_size(unsigned len,
225 unsigned long mask)
226{
227 return len + (mask & ~(crypto_tfm_ctx_alignment() - 1));
228}
229
Marek Vasut1ffc9fb2014-03-14 02:37:05 +0100230static int ahash_save_req(struct ahash_request *req, crypto_completion_t cplt)
Herbert Xu66f6ce52009-07-15 12:40:40 +0800231{
232 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
233 unsigned long alignmask = crypto_ahash_alignmask(tfm);
234 unsigned int ds = crypto_ahash_digestsize(tfm);
235 struct ahash_request_priv *priv;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800236
237 priv = kmalloc(sizeof(*priv) + ahash_align_buffer_size(ds, alignmask),
238 (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
Steffen Klassert5befbd52009-07-24 13:56:31 +0800239 GFP_KERNEL : GFP_ATOMIC);
Herbert Xu66f6ce52009-07-15 12:40:40 +0800240 if (!priv)
241 return -ENOMEM;
242
Marek Vasutab6bf4e2014-03-14 02:37:04 +0100243 /*
244 * WARNING: Voodoo programming below!
245 *
246 * The code below is obscure and hard to understand, thus explanation
247 * is necessary. See include/crypto/hash.h and include/linux/crypto.h
248 * to understand the layout of structures used here!
249 *
250 * The code here will replace portions of the ORIGINAL request with
251 * pointers to new code and buffers so the hashing operation can store
252 * the result in aligned buffer. We will call the modified request
253 * an ADJUSTED request.
254 *
255 * The newly mangled request will look as such:
256 *
257 * req {
258 * .result = ADJUSTED[new aligned buffer]
259 * .base.complete = ADJUSTED[pointer to completion function]
260 * .base.data = ADJUSTED[*req (pointer to self)]
261 * .priv = ADJUSTED[new priv] {
262 * .result = ORIGINAL(result)
263 * .complete = ORIGINAL(base.complete)
264 * .data = ORIGINAL(base.data)
265 * }
266 */
267
Herbert Xu66f6ce52009-07-15 12:40:40 +0800268 priv->result = req->result;
269 priv->complete = req->base.complete;
270 priv->data = req->base.data;
Marek Vasutab6bf4e2014-03-14 02:37:04 +0100271 /*
272 * WARNING: We do not backup req->priv here! The req->priv
273 * is for internal use of the Crypto API and the
274 * user must _NOT_ _EVER_ depend on it's content!
275 */
Herbert Xu66f6ce52009-07-15 12:40:40 +0800276
277 req->result = PTR_ALIGN((u8 *)priv->ubuf, alignmask + 1);
Marek Vasut1ffc9fb2014-03-14 02:37:05 +0100278 req->base.complete = cplt;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800279 req->base.data = req;
280 req->priv = priv;
281
Marek Vasut1ffc9fb2014-03-14 02:37:05 +0100282 return 0;
283}
284
285static void ahash_restore_req(struct ahash_request *req)
286{
287 struct ahash_request_priv *priv = req->priv;
288
289 /* Restore the original crypto request. */
290 req->result = priv->result;
291 req->base.complete = priv->complete;
292 req->base.data = priv->data;
293 req->priv = NULL;
294
295 /* Free the req->priv.priv from the ADJUSTED request. */
296 kzfree(priv);
297}
298
299static void ahash_op_unaligned_finish(struct ahash_request *req, int err)
300{
301 struct ahash_request_priv *priv = req->priv;
302
303 if (err == -EINPROGRESS)
304 return;
305
306 if (!err)
307 memcpy(priv->result, req->result,
308 crypto_ahash_digestsize(crypto_ahash_reqtfm(req)));
309
310 ahash_restore_req(req);
311}
312
313static void ahash_op_unaligned_done(struct crypto_async_request *req, int err)
314{
315 struct ahash_request *areq = req->data;
316
317 /*
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 */
327 ahash_op_unaligned_finish(areq, err);
328
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);
343 ahash_op_unaligned_finish(req, err);
344
345 return err;
346}
347
348static int crypto_ahash_op(struct ahash_request *req,
349 int (*op)(struct ahash_request *))
350{
351 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
352 unsigned long alignmask = crypto_ahash_alignmask(tfm);
353
354 if ((unsigned long)req->result & alignmask)
355 return ahash_op_unaligned(req, op);
356
357 return op(req);
358}
359
360int crypto_ahash_final(struct ahash_request *req)
361{
362 return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->final);
363}
364EXPORT_SYMBOL_GPL(crypto_ahash_final);
365
366int crypto_ahash_finup(struct ahash_request *req)
367{
368 return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->finup);
369}
370EXPORT_SYMBOL_GPL(crypto_ahash_finup);
371
372int crypto_ahash_digest(struct ahash_request *req)
373{
374 return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->digest);
375}
376EXPORT_SYMBOL_GPL(crypto_ahash_digest);
377
378static void ahash_def_finup_finish2(struct ahash_request *req, int err)
379{
380 struct ahash_request_priv *priv = req->priv;
381
382 if (err == -EINPROGRESS)
383 return;
384
385 if (!err)
386 memcpy(priv->result, req->result,
387 crypto_ahash_digestsize(crypto_ahash_reqtfm(req)));
388
Marek Vasutd4a7a0f2014-03-14 02:37:06 +0100389 ahash_restore_req(req);
Herbert Xu66f6ce52009-07-15 12:40:40 +0800390}
391
392static void ahash_def_finup_done2(struct crypto_async_request *req, int err)
393{
394 struct ahash_request *areq = req->data;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800395
396 ahash_def_finup_finish2(areq, err);
397
Marek Vasutd4a7a0f2014-03-14 02:37:06 +0100398 areq->base.complete(&areq->base, err);
Herbert Xu66f6ce52009-07-15 12:40:40 +0800399}
400
401static int ahash_def_finup_finish1(struct ahash_request *req, int err)
402{
403 if (err)
404 goto out;
405
406 req->base.complete = ahash_def_finup_done2;
407 req->base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
408 err = crypto_ahash_reqtfm(req)->final(req);
409
410out:
411 ahash_def_finup_finish2(req, err);
412 return err;
413}
414
415static void ahash_def_finup_done1(struct crypto_async_request *req, int err)
416{
417 struct ahash_request *areq = req->data;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800418
419 err = ahash_def_finup_finish1(areq, err);
420
Marek Vasutd4a7a0f2014-03-14 02:37:06 +0100421 areq->base.complete(&areq->base, err);
Herbert Xu66f6ce52009-07-15 12:40:40 +0800422}
423
424static int ahash_def_finup(struct ahash_request *req)
425{
426 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
Marek Vasutd4a7a0f2014-03-14 02:37:06 +0100427 int err;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800428
Marek Vasutd4a7a0f2014-03-14 02:37:06 +0100429 err = ahash_save_req(req, ahash_def_finup_done1);
430 if (err)
431 return err;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800432
Marek Vasutd4a7a0f2014-03-14 02:37:06 +0100433 err = tfm->update(req);
434 return ahash_def_finup_finish1(req, err);
Herbert Xu66f6ce52009-07-15 12:40:40 +0800435}
436
437static int ahash_no_export(struct ahash_request *req, void *out)
438{
439 return -ENOSYS;
440}
441
442static int ahash_no_import(struct ahash_request *req, const void *in)
443{
444 return -ENOSYS;
445}
446
Herbert Xu88056ec2009-07-14 12:28:26 +0800447static int crypto_ahash_init_tfm(struct crypto_tfm *tfm)
448{
449 struct crypto_ahash *hash = __crypto_ahash_cast(tfm);
450 struct ahash_alg *alg = crypto_ahash_alg(hash);
Herbert Xu88056ec2009-07-14 12:28:26 +0800451
Herbert Xu66f6ce52009-07-15 12:40:40 +0800452 hash->setkey = ahash_nosetkey;
453 hash->export = ahash_no_export;
454 hash->import = ahash_no_import;
455
Herbert Xu88056ec2009-07-14 12:28:26 +0800456 if (tfm->__crt_alg->cra_type != &crypto_ahash_type)
457 return crypto_init_shash_ops_async(tfm);
458
Herbert Xu88056ec2009-07-14 12:28:26 +0800459 hash->init = alg->init;
460 hash->update = alg->update;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800461 hash->final = alg->final;
462 hash->finup = alg->finup ?: ahash_def_finup;
Herbert Xu88056ec2009-07-14 12:28:26 +0800463 hash->digest = alg->digest;
Herbert Xu66f6ce52009-07-15 12:40:40 +0800464
465 if (alg->setkey)
466 hash->setkey = alg->setkey;
467 if (alg->export)
468 hash->export = alg->export;
469 if (alg->import)
470 hash->import = alg->import;
Herbert Xu88056ec2009-07-14 12:28:26 +0800471
472 return 0;
473}
474
475static unsigned int crypto_ahash_extsize(struct crypto_alg *alg)
476{
477 if (alg->cra_type == &crypto_ahash_type)
478 return alg->cra_ctxsize;
479
480 return sizeof(struct crypto_shash *);
481}
482
Herbert Xu3acc8472011-11-03 23:46:07 +1100483#ifdef CONFIG_NET
Steffen Klassert6238cba2011-09-27 07:41:07 +0200484static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg)
485{
486 struct crypto_report_hash rhash;
487
Mathias Krause9a5467b2013-02-05 18:19:13 +0100488 strncpy(rhash.type, "ahash", sizeof(rhash.type));
Steffen Klassert6238cba2011-09-27 07:41:07 +0200489
490 rhash.blocksize = alg->cra_blocksize;
491 rhash.digestsize = __crypto_hash_alg_common(alg)->digestsize;
492
David S. Miller6662df32012-04-01 20:19:05 -0400493 if (nla_put(skb, CRYPTOCFGA_REPORT_HASH,
494 sizeof(struct crypto_report_hash), &rhash))
495 goto nla_put_failure;
Steffen Klassert6238cba2011-09-27 07:41:07 +0200496 return 0;
497
498nla_put_failure:
499 return -EMSGSIZE;
500}
Herbert Xu3acc8472011-11-03 23:46:07 +1100501#else
502static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg)
503{
504 return -ENOSYS;
505}
506#endif
Steffen Klassert6238cba2011-09-27 07:41:07 +0200507
Loc Ho004a4032008-05-14 20:41:47 +0800508static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
509 __attribute__ ((unused));
510static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
511{
512 seq_printf(m, "type : ahash\n");
513 seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
514 "yes" : "no");
515 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
Herbert Xu88056ec2009-07-14 12:28:26 +0800516 seq_printf(m, "digestsize : %u\n",
517 __crypto_hash_alg_common(alg)->digestsize);
Loc Ho004a4032008-05-14 20:41:47 +0800518}
519
520const struct crypto_type crypto_ahash_type = {
Herbert Xu88056ec2009-07-14 12:28:26 +0800521 .extsize = crypto_ahash_extsize,
522 .init_tfm = crypto_ahash_init_tfm,
Loc Ho004a4032008-05-14 20:41:47 +0800523#ifdef CONFIG_PROC_FS
524 .show = crypto_ahash_show,
525#endif
Steffen Klassert6238cba2011-09-27 07:41:07 +0200526 .report = crypto_ahash_report,
Herbert Xu88056ec2009-07-14 12:28:26 +0800527 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
528 .maskset = CRYPTO_ALG_TYPE_AHASH_MASK,
529 .type = CRYPTO_ALG_TYPE_AHASH,
530 .tfmsize = offsetof(struct crypto_ahash, base),
Loc Ho004a4032008-05-14 20:41:47 +0800531};
532EXPORT_SYMBOL_GPL(crypto_ahash_type);
533
Herbert Xu88056ec2009-07-14 12:28:26 +0800534struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
535 u32 mask)
536{
537 return crypto_alloc_tfm(alg_name, &crypto_ahash_type, type, mask);
538}
539EXPORT_SYMBOL_GPL(crypto_alloc_ahash);
540
Herbert Xu01c2dec2009-07-14 14:06:06 +0800541static int ahash_prepare_alg(struct ahash_alg *alg)
542{
543 struct crypto_alg *base = &alg->halg.base;
544
545 if (alg->halg.digestsize > PAGE_SIZE / 8 ||
546 alg->halg.statesize > PAGE_SIZE / 8)
547 return -EINVAL;
548
549 base->cra_type = &crypto_ahash_type;
550 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
551 base->cra_flags |= CRYPTO_ALG_TYPE_AHASH;
552
553 return 0;
554}
555
556int crypto_register_ahash(struct ahash_alg *alg)
557{
558 struct crypto_alg *base = &alg->halg.base;
559 int err;
560
561 err = ahash_prepare_alg(alg);
562 if (err)
563 return err;
564
565 return crypto_register_alg(base);
566}
567EXPORT_SYMBOL_GPL(crypto_register_ahash);
568
569int crypto_unregister_ahash(struct ahash_alg *alg)
570{
571 return crypto_unregister_alg(&alg->halg.base);
572}
573EXPORT_SYMBOL_GPL(crypto_unregister_ahash);
574
575int ahash_register_instance(struct crypto_template *tmpl,
576 struct ahash_instance *inst)
577{
578 int err;
579
580 err = ahash_prepare_alg(&inst->alg);
581 if (err)
582 return err;
583
584 return crypto_register_instance(tmpl, ahash_crypto_instance(inst));
585}
586EXPORT_SYMBOL_GPL(ahash_register_instance);
587
588void ahash_free_instance(struct crypto_instance *inst)
589{
590 crypto_drop_spawn(crypto_instance_ctx(inst));
591 kfree(ahash_instance(inst));
592}
593EXPORT_SYMBOL_GPL(ahash_free_instance);
594
595int crypto_init_ahash_spawn(struct crypto_ahash_spawn *spawn,
596 struct hash_alg_common *alg,
597 struct crypto_instance *inst)
598{
599 return crypto_init_spawn2(&spawn->base, &alg->base, inst,
600 &crypto_ahash_type);
601}
602EXPORT_SYMBOL_GPL(crypto_init_ahash_spawn);
603
604struct hash_alg_common *ahash_attr_alg(struct rtattr *rta, u32 type, u32 mask)
605{
606 struct crypto_alg *alg;
607
608 alg = crypto_attr_alg2(rta, &crypto_ahash_type, type, mask);
609 return IS_ERR(alg) ? ERR_CAST(alg) : __crypto_hash_alg_common(alg);
610}
611EXPORT_SYMBOL_GPL(ahash_attr_alg);
612
Loc Ho004a4032008-05-14 20:41:47 +0800613MODULE_LICENSE("GPL");
614MODULE_DESCRIPTION("Asynchronous cryptographic hash type");