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 | ||
20036252 HX |
16 | #include <crypto/internal/hash.h> |
17 | #include <crypto/scatterwalk.h> | |
004a403c LH |
18 | #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> | |
24 | ||
25 | #include "internal.h" | |
26 | ||
88056ec3 HX |
27 | static inline struct ahash_alg *crypto_ahash_alg(struct crypto_ahash *hash) |
28 | { | |
29 | return container_of(crypto_hash_alg_common(hash), struct ahash_alg, | |
30 | halg); | |
31 | } | |
32 | ||
20036252 HX |
33 | static int hash_walk_next(struct crypto_hash_walk *walk) |
34 | { | |
35 | unsigned int alignmask = walk->alignmask; | |
36 | unsigned int offset = walk->offset; | |
37 | unsigned int nbytes = min(walk->entrylen, | |
38 | ((unsigned int)(PAGE_SIZE)) - offset); | |
39 | ||
40 | walk->data = crypto_kmap(walk->pg, 0); | |
41 | walk->data += offset; | |
42 | ||
43 | if (offset & alignmask) | |
44 | nbytes = alignmask + 1 - (offset & alignmask); | |
45 | ||
46 | walk->entrylen -= nbytes; | |
47 | return nbytes; | |
48 | } | |
49 | ||
50 | static int hash_walk_new_entry(struct crypto_hash_walk *walk) | |
51 | { | |
52 | struct scatterlist *sg; | |
53 | ||
54 | sg = walk->sg; | |
55 | walk->pg = sg_page(sg); | |
56 | walk->offset = sg->offset; | |
57 | walk->entrylen = sg->length; | |
58 | ||
59 | if (walk->entrylen > walk->total) | |
60 | walk->entrylen = walk->total; | |
61 | walk->total -= walk->entrylen; | |
62 | ||
63 | return hash_walk_next(walk); | |
64 | } | |
65 | ||
66 | int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err) | |
67 | { | |
68 | unsigned int alignmask = walk->alignmask; | |
69 | unsigned int nbytes = walk->entrylen; | |
70 | ||
71 | walk->data -= walk->offset; | |
72 | ||
73 | if (nbytes && walk->offset & alignmask && !err) { | |
74 | walk->offset += alignmask - 1; | |
75 | walk->offset = ALIGN(walk->offset, alignmask + 1); | |
76 | walk->data += walk->offset; | |
77 | ||
78 | nbytes = min(nbytes, | |
79 | ((unsigned int)(PAGE_SIZE)) - walk->offset); | |
80 | walk->entrylen -= nbytes; | |
81 | ||
82 | return nbytes; | |
83 | } | |
84 | ||
85 | crypto_kunmap(walk->data, 0); | |
86 | crypto_yield(walk->flags); | |
87 | ||
88 | if (err) | |
89 | return err; | |
90 | ||
d315a0e0 HX |
91 | if (nbytes) { |
92 | walk->offset = 0; | |
93 | walk->pg++; | |
20036252 | 94 | return hash_walk_next(walk); |
d315a0e0 | 95 | } |
20036252 HX |
96 | |
97 | if (!walk->total) | |
98 | return 0; | |
99 | ||
100 | walk->sg = scatterwalk_sg_next(walk->sg); | |
101 | ||
102 | return hash_walk_new_entry(walk); | |
103 | } | |
104 | EXPORT_SYMBOL_GPL(crypto_hash_walk_done); | |
105 | ||
106 | int crypto_hash_walk_first(struct ahash_request *req, | |
107 | struct crypto_hash_walk *walk) | |
108 | { | |
109 | walk->total = req->nbytes; | |
110 | ||
111 | if (!walk->total) | |
112 | return 0; | |
113 | ||
114 | walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req)); | |
115 | walk->sg = req->src; | |
116 | walk->flags = req->base.flags; | |
117 | ||
118 | return hash_walk_new_entry(walk); | |
119 | } | |
120 | EXPORT_SYMBOL_GPL(crypto_hash_walk_first); | |
121 | ||
5f7082ed HX |
122 | int crypto_hash_walk_first_compat(struct hash_desc *hdesc, |
123 | struct crypto_hash_walk *walk, | |
124 | struct scatterlist *sg, unsigned int len) | |
125 | { | |
126 | walk->total = len; | |
127 | ||
128 | if (!walk->total) | |
129 | return 0; | |
130 | ||
131 | walk->alignmask = crypto_hash_alignmask(hdesc->tfm); | |
132 | walk->sg = sg; | |
133 | walk->flags = hdesc->flags; | |
134 | ||
135 | return hash_walk_new_entry(walk); | |
136 | } | |
137 | ||
004a403c LH |
138 | static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key, |
139 | unsigned int keylen) | |
140 | { | |
141 | struct ahash_alg *ahash = crypto_ahash_alg(tfm); | |
142 | unsigned long alignmask = crypto_ahash_alignmask(tfm); | |
143 | int ret; | |
144 | u8 *buffer, *alignbuffer; | |
145 | unsigned long absize; | |
146 | ||
147 | absize = keylen + alignmask; | |
148 | buffer = kmalloc(absize, GFP_ATOMIC); | |
149 | if (!buffer) | |
150 | return -ENOMEM; | |
151 | ||
152 | alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); | |
153 | memcpy(alignbuffer, key, keylen); | |
154 | ret = ahash->setkey(tfm, alignbuffer, keylen); | |
155 | memset(alignbuffer, 0, keylen); | |
156 | kfree(buffer); | |
157 | return ret; | |
158 | } | |
159 | ||
160 | static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key, | |
161 | unsigned int keylen) | |
162 | { | |
163 | struct ahash_alg *ahash = crypto_ahash_alg(tfm); | |
164 | unsigned long alignmask = crypto_ahash_alignmask(tfm); | |
165 | ||
166 | if ((unsigned long)key & alignmask) | |
167 | return ahash_setkey_unaligned(tfm, key, keylen); | |
168 | ||
169 | return ahash->setkey(tfm, key, keylen); | |
170 | } | |
171 | ||
3751f402 HX |
172 | static int ahash_nosetkey(struct crypto_ahash *tfm, const u8 *key, |
173 | unsigned int keylen) | |
174 | { | |
175 | return -ENOSYS; | |
176 | } | |
177 | ||
004a403c LH |
178 | static int crypto_init_ahash_ops(struct crypto_tfm *tfm, u32 type, u32 mask) |
179 | { | |
88056ec3 HX |
180 | struct old_ahash_alg *alg = &tfm->__crt_alg->cra_ahash; |
181 | struct crypto_ahash *crt = __crypto_ahash_cast(tfm); | |
182 | struct ahash_alg *nalg = crypto_ahash_alg(crt); | |
004a403c | 183 | |
ca786dc7 | 184 | if (alg->digestsize > PAGE_SIZE / 8) |
004a403c LH |
185 | return -EINVAL; |
186 | ||
187 | crt->init = alg->init; | |
188 | crt->update = alg->update; | |
189 | crt->final = alg->final; | |
190 | crt->digest = alg->digest; | |
3751f402 | 191 | crt->setkey = alg->setkey ? ahash_setkey : ahash_nosetkey; |
004a403c LH |
192 | crt->digestsize = alg->digestsize; |
193 | ||
88056ec3 HX |
194 | nalg->setkey = alg->setkey; |
195 | nalg->halg.digestsize = alg->digestsize; | |
196 | ||
004a403c LH |
197 | return 0; |
198 | } | |
199 | ||
88056ec3 HX |
200 | static int crypto_ahash_init_tfm(struct crypto_tfm *tfm) |
201 | { | |
202 | struct crypto_ahash *hash = __crypto_ahash_cast(tfm); | |
203 | struct ahash_alg *alg = crypto_ahash_alg(hash); | |
204 | struct old_ahash_alg *oalg = crypto_old_ahash_alg(hash); | |
205 | ||
206 | if (tfm->__crt_alg->cra_type != &crypto_ahash_type) | |
207 | return crypto_init_shash_ops_async(tfm); | |
208 | ||
209 | if (oalg->init) | |
210 | return crypto_init_ahash_ops(tfm, 0, 0); | |
211 | ||
212 | hash->init = alg->init; | |
213 | hash->update = alg->update; | |
214 | hash->final = alg->final; | |
215 | hash->digest = alg->digest; | |
216 | hash->setkey = alg->setkey ? ahash_setkey : ahash_nosetkey; | |
217 | hash->digestsize = alg->halg.digestsize; | |
218 | ||
219 | return 0; | |
220 | } | |
221 | ||
222 | static unsigned int crypto_ahash_extsize(struct crypto_alg *alg) | |
223 | { | |
224 | if (alg->cra_type == &crypto_ahash_type) | |
225 | return alg->cra_ctxsize; | |
226 | ||
227 | return sizeof(struct crypto_shash *); | |
228 | } | |
229 | ||
004a403c LH |
230 | static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg) |
231 | __attribute__ ((unused)); | |
232 | static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg) | |
233 | { | |
234 | seq_printf(m, "type : ahash\n"); | |
235 | seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ? | |
236 | "yes" : "no"); | |
237 | seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); | |
88056ec3 HX |
238 | seq_printf(m, "digestsize : %u\n", |
239 | __crypto_hash_alg_common(alg)->digestsize); | |
004a403c LH |
240 | } |
241 | ||
242 | const struct crypto_type crypto_ahash_type = { | |
88056ec3 HX |
243 | .extsize = crypto_ahash_extsize, |
244 | .init_tfm = crypto_ahash_init_tfm, | |
004a403c LH |
245 | #ifdef CONFIG_PROC_FS |
246 | .show = crypto_ahash_show, | |
247 | #endif | |
88056ec3 HX |
248 | .maskclear = ~CRYPTO_ALG_TYPE_MASK, |
249 | .maskset = CRYPTO_ALG_TYPE_AHASH_MASK, | |
250 | .type = CRYPTO_ALG_TYPE_AHASH, | |
251 | .tfmsize = offsetof(struct crypto_ahash, base), | |
004a403c LH |
252 | }; |
253 | EXPORT_SYMBOL_GPL(crypto_ahash_type); | |
254 | ||
88056ec3 HX |
255 | struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type, |
256 | u32 mask) | |
257 | { | |
258 | return crypto_alloc_tfm(alg_name, &crypto_ahash_type, type, mask); | |
259 | } | |
260 | EXPORT_SYMBOL_GPL(crypto_alloc_ahash); | |
261 | ||
004a403c LH |
262 | MODULE_LICENSE("GPL"); |
263 | MODULE_DESCRIPTION("Asynchronous cryptographic hash type"); |