2 * Asynchronous Cryptographic Hash operations.
4 * This is the asynchronous version of hash.c with notification of
5 * completion via a callback.
7 * Copyright (c) 2008 Loc Ho <lho@amcc.com>
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)
16 #include <crypto/algapi.h>
17 #include <linux/err.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <linux/seq_file.h>
26 static int ahash_setkey_unaligned(struct crypto_ahash
*tfm
, const u8
*key
,
29 struct ahash_alg
*ahash
= crypto_ahash_alg(tfm
);
30 unsigned long alignmask
= crypto_ahash_alignmask(tfm
);
32 u8
*buffer
, *alignbuffer
;
35 absize
= keylen
+ alignmask
;
36 buffer
= kmalloc(absize
, GFP_ATOMIC
);
40 alignbuffer
= (u8
*)ALIGN((unsigned long)buffer
, alignmask
+ 1);
41 memcpy(alignbuffer
, key
, keylen
);
42 ret
= ahash
->setkey(tfm
, alignbuffer
, keylen
);
43 memset(alignbuffer
, 0, keylen
);
48 static int ahash_setkey(struct crypto_ahash
*tfm
, const u8
*key
,
51 struct ahash_alg
*ahash
= crypto_ahash_alg(tfm
);
52 unsigned long alignmask
= crypto_ahash_alignmask(tfm
);
54 if ((unsigned long)key
& alignmask
)
55 return ahash_setkey_unaligned(tfm
, key
, keylen
);
57 return ahash
->setkey(tfm
, key
, keylen
);
60 static unsigned int crypto_ahash_ctxsize(struct crypto_alg
*alg
, u32 type
,
63 return alg
->cra_ctxsize
;
66 static int crypto_init_ahash_ops(struct crypto_tfm
*tfm
, u32 type
, u32 mask
)
68 struct ahash_alg
*alg
= &tfm
->__crt_alg
->cra_ahash
;
69 struct ahash_tfm
*crt
= &tfm
->crt_ahash
;
71 if (alg
->digestsize
> PAGE_SIZE
/ 8)
74 crt
->init
= alg
->init
;
75 crt
->update
= alg
->update
;
76 crt
->final
= alg
->final
;
77 crt
->digest
= alg
->digest
;
78 crt
->setkey
= ahash_setkey
;
79 crt
->digestsize
= alg
->digestsize
;
84 static void crypto_ahash_show(struct seq_file
*m
, struct crypto_alg
*alg
)
85 __attribute__ ((unused
));
86 static void crypto_ahash_show(struct seq_file
*m
, struct crypto_alg
*alg
)
88 seq_printf(m
, "type : ahash\n");
89 seq_printf(m
, "async : %s\n", alg
->cra_flags
& CRYPTO_ALG_ASYNC
?
91 seq_printf(m
, "blocksize : %u\n", alg
->cra_blocksize
);
92 seq_printf(m
, "digestsize : %u\n", alg
->cra_hash
.digestsize
);
95 const struct crypto_type crypto_ahash_type
= {
96 .ctxsize
= crypto_ahash_ctxsize
,
97 .init
= crypto_init_ahash_ops
,
99 .show
= crypto_ahash_show
,
102 EXPORT_SYMBOL_GPL(crypto_ahash_type
);
104 MODULE_LICENSE("GPL");
105 MODULE_DESCRIPTION("Asynchronous cryptographic hash type");