]>
Commit | Line | Data |
---|---|---|
004a403c LH |
1 | /* |
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 | ||
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> | |
23 | ||
24 | #include "internal.h" | |
25 | ||
26 | static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key, | |
27 | unsigned int keylen) | |
28 | { | |
29 | struct ahash_alg *ahash = crypto_ahash_alg(tfm); | |
30 | unsigned long alignmask = crypto_ahash_alignmask(tfm); | |
31 | int ret; | |
32 | u8 *buffer, *alignbuffer; | |
33 | unsigned long absize; | |
34 | ||
35 | absize = keylen + alignmask; | |
36 | buffer = kmalloc(absize, GFP_ATOMIC); | |
37 | if (!buffer) | |
38 | return -ENOMEM; | |
39 | ||
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); | |
44 | kfree(buffer); | |
45 | return ret; | |
46 | } | |
47 | ||
48 | static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key, | |
49 | unsigned int keylen) | |
50 | { | |
51 | struct ahash_alg *ahash = crypto_ahash_alg(tfm); | |
52 | unsigned long alignmask = crypto_ahash_alignmask(tfm); | |
53 | ||
54 | if ((unsigned long)key & alignmask) | |
55 | return ahash_setkey_unaligned(tfm, key, keylen); | |
56 | ||
57 | return ahash->setkey(tfm, key, keylen); | |
58 | } | |
59 | ||
60 | static unsigned int crypto_ahash_ctxsize(struct crypto_alg *alg, u32 type, | |
61 | u32 mask) | |
62 | { | |
63 | return alg->cra_ctxsize; | |
64 | } | |
65 | ||
66 | static int crypto_init_ahash_ops(struct crypto_tfm *tfm, u32 type, u32 mask) | |
67 | { | |
68 | struct ahash_alg *alg = &tfm->__crt_alg->cra_ahash; | |
69 | struct ahash_tfm *crt = &tfm->crt_ahash; | |
70 | ||
ca786dc7 | 71 | if (alg->digestsize > PAGE_SIZE / 8) |
004a403c LH |
72 | return -EINVAL; |
73 | ||
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; | |
004a403c LH |
79 | crt->digestsize = alg->digestsize; |
80 | ||
81 | return 0; | |
82 | } | |
83 | ||
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) | |
87 | { | |
88 | seq_printf(m, "type : ahash\n"); | |
89 | seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ? | |
90 | "yes" : "no"); | |
91 | seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); | |
92 | seq_printf(m, "digestsize : %u\n", alg->cra_hash.digestsize); | |
93 | } | |
94 | ||
95 | const struct crypto_type crypto_ahash_type = { | |
96 | .ctxsize = crypto_ahash_ctxsize, | |
97 | .init = crypto_init_ahash_ops, | |
98 | #ifdef CONFIG_PROC_FS | |
99 | .show = crypto_ahash_show, | |
100 | #endif | |
101 | }; | |
102 | EXPORT_SYMBOL_GPL(crypto_ahash_type); | |
103 | ||
104 | MODULE_LICENSE("GPL"); | |
105 | MODULE_DESCRIPTION("Asynchronous cryptographic hash type"); |