aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/xcbc.c
diff options
context:
space:
mode:
authorHerbert Xu <herbert@gondor.apana.org.au>2009-07-12 00:48:32 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2009-07-14 00:58:06 -0400
commit3106caab617c75c9a47706af3a8017318207be2d (patch)
tree494cee518195e267e87b491dd913212b3d57ecb1 /crypto/xcbc.c
parent8bd1209cfff246ce6dfae86837467a01236f9cb6 (diff)
crypto: xcbc - Switch to shash
This patch converts the xcbc algorithm to the new shash type. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'crypto/xcbc.c')
-rw-r--r--crypto/xcbc.c235
1 files changed, 87 insertions, 148 deletions
diff --git a/crypto/xcbc.c b/crypto/xcbc.c
index b63b633e549c..b2cc8551b99f 100644
--- a/crypto/xcbc.c
+++ b/crypto/xcbc.c
@@ -19,15 +19,9 @@
19 * Kazunori Miyazawa <miyazawa@linux-ipv6.org> 19 * Kazunori Miyazawa <miyazawa@linux-ipv6.org>
20 */ 20 */
21 21
22#include <crypto/scatterwalk.h> 22#include <crypto/internal/hash.h>
23#include <linux/crypto.h>
24#include <linux/err.h> 23#include <linux/err.h>
25#include <linux/hardirq.h>
26#include <linux/kernel.h> 24#include <linux/kernel.h>
27#include <linux/mm.h>
28#include <linux/rtnetlink.h>
29#include <linux/slab.h>
30#include <linux/scatterlist.h>
31 25
32static u_int32_t ks[12] = {0x01010101, 0x01010101, 0x01010101, 0x01010101, 26static u_int32_t ks[12] = {0x01010101, 0x01010101, 0x01010101, 0x01010101,
33 0x02020202, 0x02020202, 0x02020202, 0x02020202, 27 0x02020202, 0x02020202, 0x02020202, 0x02020202,
@@ -66,10 +60,10 @@ static void xor_128(u8 *a, const u8 *b, unsigned int bs)
66 ((u32 *)a)[3] ^= ((u32 *)b)[3]; 60 ((u32 *)a)[3] ^= ((u32 *)b)[3];
67} 61}
68 62
69static int _crypto_xcbc_digest_setkey(struct crypto_hash *parent, 63static int _crypto_xcbc_digest_setkey(struct crypto_shash *parent,
70 struct crypto_xcbc_ctx *ctx) 64 struct crypto_xcbc_ctx *ctx)
71{ 65{
72 int bs = crypto_hash_blocksize(parent); 66 int bs = crypto_shash_blocksize(parent);
73 int err = 0; 67 int err = 0;
74 u8 key1[bs]; 68 u8 key1[bs];
75 69
@@ -81,10 +75,10 @@ static int _crypto_xcbc_digest_setkey(struct crypto_hash *parent,
81 return crypto_cipher_setkey(ctx->child, key1, bs); 75 return crypto_cipher_setkey(ctx->child, key1, bs);
82} 76}
83 77
84static int crypto_xcbc_digest_setkey(struct crypto_hash *parent, 78static int crypto_xcbc_digest_setkey(struct crypto_shash *parent,
85 const u8 *inkey, unsigned int keylen) 79 const u8 *inkey, unsigned int keylen)
86{ 80{
87 struct crypto_xcbc_ctx *ctx = crypto_hash_ctx_aligned(parent); 81 struct crypto_xcbc_ctx *ctx = crypto_shash_ctx(parent);
88 82
89 if (keylen != crypto_cipher_blocksize(ctx->child)) 83 if (keylen != crypto_cipher_blocksize(ctx->child))
90 return -EINVAL; 84 return -EINVAL;
@@ -96,10 +90,10 @@ static int crypto_xcbc_digest_setkey(struct crypto_hash *parent,
96 return _crypto_xcbc_digest_setkey(parent, ctx); 90 return _crypto_xcbc_digest_setkey(parent, ctx);
97} 91}
98 92
99static int crypto_xcbc_digest_init(struct hash_desc *pdesc) 93static int crypto_xcbc_digest_init(struct shash_desc *pdesc)
100{ 94{
101 struct crypto_xcbc_ctx *ctx = crypto_hash_ctx_aligned(pdesc->tfm); 95 struct crypto_xcbc_ctx *ctx = crypto_shash_ctx(pdesc->tfm);
102 int bs = crypto_hash_blocksize(pdesc->tfm); 96 int bs = crypto_shash_blocksize(pdesc->tfm);
103 97
104 ctx->len = 0; 98 ctx->len = 0;
105 memset(ctx->odds, 0, bs); 99 memset(ctx->odds, 0, bs);
@@ -108,102 +102,55 @@ static int crypto_xcbc_digest_init(struct hash_desc *pdesc)
108 return 0; 102 return 0;
109} 103}
110 104
111static int crypto_xcbc_digest_update2(struct hash_desc *pdesc, 105static int crypto_xcbc_digest_update(struct shash_desc *pdesc, const u8 *p,
112 struct scatterlist *sg, 106 unsigned int len)
113 unsigned int nbytes)
114{ 107{
115 struct crypto_hash *parent = pdesc->tfm; 108 struct crypto_shash *parent = pdesc->tfm;
116 struct crypto_xcbc_ctx *ctx = crypto_hash_ctx_aligned(parent); 109 struct crypto_xcbc_ctx *ctx = crypto_shash_ctx(parent);
117 struct crypto_cipher *tfm = ctx->child; 110 struct crypto_cipher *tfm = ctx->child;
118 int bs = crypto_hash_blocksize(parent); 111 int bs = crypto_shash_blocksize(parent);
119 112
120 for (;;) { 113 /* checking the data can fill the block */
121 struct page *pg = sg_page(sg); 114 if ((ctx->len + len) <= bs) {
122 unsigned int offset = sg->offset; 115 memcpy(ctx->odds + ctx->len, p, len);
123 unsigned int slen = sg->length; 116 ctx->len += len;
124 117 return 0;
125 if (unlikely(slen > nbytes))
126 slen = nbytes;
127
128 nbytes -= slen;
129
130 while (slen > 0) {
131 unsigned int len = min(slen, ((unsigned int)(PAGE_SIZE)) - offset);
132 char *p = crypto_kmap(pg, 0) + offset;
133
134 /* checking the data can fill the block */
135 if ((ctx->len + len) <= bs) {
136 memcpy(ctx->odds + ctx->len, p, len);
137 ctx->len += len;
138 slen -= len;
139
140 /* checking the rest of the page */
141 if (len + offset >= PAGE_SIZE) {
142 offset = 0;
143 pg++;
144 } else
145 offset += len;
146
147 crypto_kunmap(p, 0);
148 crypto_yield(pdesc->flags);
149 continue;
150 }
151
152 /* filling odds with new data and encrypting it */
153 memcpy(ctx->odds + ctx->len, p, bs - ctx->len);
154 len -= bs - ctx->len;
155 p += bs - ctx->len;
156
157 ctx->xor(ctx->prev, ctx->odds, bs);
158 crypto_cipher_encrypt_one(tfm, ctx->prev, ctx->prev);
159
160 /* clearing the length */
161 ctx->len = 0;
162
163 /* encrypting the rest of data */
164 while (len > bs) {
165 ctx->xor(ctx->prev, p, bs);
166 crypto_cipher_encrypt_one(tfm, ctx->prev,
167 ctx->prev);
168 p += bs;
169 len -= bs;
170 }
171
172 /* keeping the surplus of blocksize */
173 if (len) {
174 memcpy(ctx->odds, p, len);
175 ctx->len = len;
176 }
177 crypto_kunmap(p, 0);
178 crypto_yield(pdesc->flags);
179 slen -= min(slen, ((unsigned int)(PAGE_SIZE)) - offset);
180 offset = 0;
181 pg++;
182 }
183
184 if (!nbytes)
185 break;
186 sg = scatterwalk_sg_next(sg);
187 } 118 }
188 119
189 return 0; 120 /* filling odds with new data and encrypting it */
190} 121 memcpy(ctx->odds + ctx->len, p, bs - ctx->len);
122 len -= bs - ctx->len;
123 p += bs - ctx->len;
191 124
192static int crypto_xcbc_digest_update(struct hash_desc *pdesc, 125 ctx->xor(ctx->prev, ctx->odds, bs);
193 struct scatterlist *sg, 126 crypto_cipher_encrypt_one(tfm, ctx->prev, ctx->prev);
194 unsigned int nbytes) 127
195{ 128 /* clearing the length */
196 if (WARN_ON_ONCE(in_irq())) 129 ctx->len = 0;
197 return -EDEADLK; 130
198 return crypto_xcbc_digest_update2(pdesc, sg, nbytes); 131 /* encrypting the rest of data */
132 while (len > bs) {
133 ctx->xor(ctx->prev, p, bs);
134 crypto_cipher_encrypt_one(tfm, ctx->prev, ctx->prev);
135 p += bs;
136 len -= bs;
137 }
138
139 /* keeping the surplus of blocksize */
140 if (len) {
141 memcpy(ctx->odds, p, len);
142 ctx->len = len;
143 }
144
145 return 0;
199} 146}
200 147
201static int crypto_xcbc_digest_final(struct hash_desc *pdesc, u8 *out) 148static int crypto_xcbc_digest_final(struct shash_desc *pdesc, u8 *out)
202{ 149{
203 struct crypto_hash *parent = pdesc->tfm; 150 struct crypto_shash *parent = pdesc->tfm;
204 struct crypto_xcbc_ctx *ctx = crypto_hash_ctx_aligned(parent); 151 struct crypto_xcbc_ctx *ctx = crypto_shash_ctx(parent);
205 struct crypto_cipher *tfm = ctx->child; 152 struct crypto_cipher *tfm = ctx->child;
206 int bs = crypto_hash_blocksize(parent); 153 int bs = crypto_shash_blocksize(parent);
207 int err = 0; 154 int err = 0;
208 155
209 if (ctx->len == bs) { 156 if (ctx->len == bs) {
@@ -248,24 +195,13 @@ static int crypto_xcbc_digest_final(struct hash_desc *pdesc, u8 *out)
248 return 0; 195 return 0;
249} 196}
250 197
251static int crypto_xcbc_digest(struct hash_desc *pdesc,
252 struct scatterlist *sg, unsigned int nbytes, u8 *out)
253{
254 if (WARN_ON_ONCE(in_irq()))
255 return -EDEADLK;
256
257 crypto_xcbc_digest_init(pdesc);
258 crypto_xcbc_digest_update2(pdesc, sg, nbytes);
259 return crypto_xcbc_digest_final(pdesc, out);
260}
261
262static int xcbc_init_tfm(struct crypto_tfm *tfm) 198static int xcbc_init_tfm(struct crypto_tfm *tfm)
263{ 199{
264 struct crypto_cipher *cipher; 200 struct crypto_cipher *cipher;
265 struct crypto_instance *inst = (void *)tfm->__crt_alg; 201 struct crypto_instance *inst = (void *)tfm->__crt_alg;
266 struct crypto_spawn *spawn = crypto_instance_ctx(inst); 202 struct crypto_spawn *spawn = crypto_instance_ctx(inst);
267 struct crypto_xcbc_ctx *ctx = crypto_hash_ctx_aligned(__crypto_hash_cast(tfm)); 203 struct crypto_xcbc_ctx *ctx = crypto_tfm_ctx(tfm);
268 int bs = crypto_hash_blocksize(__crypto_hash_cast(tfm)); 204 int bs = crypto_tfm_alg_blocksize(tfm);
269 205
270 cipher = crypto_spawn_cipher(spawn); 206 cipher = crypto_spawn_cipher(spawn);
271 if (IS_ERR(cipher)) 207 if (IS_ERR(cipher))
@@ -289,70 +225,73 @@ static int xcbc_init_tfm(struct crypto_tfm *tfm)
289 225
290static void xcbc_exit_tfm(struct crypto_tfm *tfm) 226static void xcbc_exit_tfm(struct crypto_tfm *tfm)
291{ 227{
292 struct crypto_xcbc_ctx *ctx = crypto_hash_ctx_aligned(__crypto_hash_cast(tfm)); 228 struct crypto_xcbc_ctx *ctx = crypto_tfm_ctx(tfm);
293 crypto_free_cipher(ctx->child); 229 crypto_free_cipher(ctx->child);
294} 230}
295 231
296static struct crypto_instance *xcbc_alloc(struct rtattr **tb) 232static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
297{ 233{
298 struct crypto_instance *inst; 234 struct shash_instance *inst;
299 struct crypto_alg *alg; 235 struct crypto_alg *alg;
300 int err; 236 int err;
301 237
302 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_HASH); 238 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
303 if (err) 239 if (err)
304 return ERR_PTR(err); 240 return err;
305 241
306 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER, 242 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
307 CRYPTO_ALG_TYPE_MASK); 243 CRYPTO_ALG_TYPE_MASK);
308 if (IS_ERR(alg)) 244 if (IS_ERR(alg))
309 return ERR_CAST(alg); 245 return PTR_ERR(alg);
310 246
311 switch(alg->cra_blocksize) { 247 switch(alg->cra_blocksize) {
312 case 16: 248 case 16:
313 break; 249 break;
314 default: 250 default:
315 inst = ERR_PTR(-EINVAL);
316 goto out_put_alg; 251 goto out_put_alg;
317 } 252 }
318 253
319 inst = crypto_alloc_instance("xcbc", alg); 254 inst = shash_alloc_instance("xcbc", alg);
320 if (IS_ERR(inst)) 255 if (IS_ERR(inst))
321 goto out_put_alg; 256 goto out_put_alg;
322 257
323 inst->alg.cra_flags = CRYPTO_ALG_TYPE_HASH; 258 err = crypto_init_spawn(shash_instance_ctx(inst), alg,
324 inst->alg.cra_priority = alg->cra_priority; 259 shash_crypto_instance(inst),
325 inst->alg.cra_blocksize = alg->cra_blocksize; 260 CRYPTO_ALG_TYPE_MASK);
326 inst->alg.cra_alignmask = alg->cra_alignmask; 261 if (err)
327 inst->alg.cra_type = &crypto_hash_type; 262 goto out_free_inst;
328 263
329 inst->alg.cra_hash.digestsize = alg->cra_blocksize; 264 inst->alg.base.cra_priority = alg->cra_priority;
330 inst->alg.cra_ctxsize = sizeof(struct crypto_xcbc_ctx) + 265 inst->alg.base.cra_blocksize = alg->cra_blocksize;
331 ALIGN(inst->alg.cra_blocksize * 3, sizeof(void *)); 266 inst->alg.base.cra_alignmask = alg->cra_alignmask;
332 inst->alg.cra_init = xcbc_init_tfm; 267
333 inst->alg.cra_exit = xcbc_exit_tfm; 268 inst->alg.digestsize = alg->cra_blocksize;
334 269 inst->alg.base.cra_ctxsize = sizeof(struct crypto_xcbc_ctx) +
335 inst->alg.cra_hash.init = crypto_xcbc_digest_init; 270 ALIGN(alg->cra_blocksize * 3,
336 inst->alg.cra_hash.update = crypto_xcbc_digest_update; 271 sizeof(void *));
337 inst->alg.cra_hash.final = crypto_xcbc_digest_final; 272 inst->alg.base.cra_init = xcbc_init_tfm;
338 inst->alg.cra_hash.digest = crypto_xcbc_digest; 273 inst->alg.base.cra_exit = xcbc_exit_tfm;
339 inst->alg.cra_hash.setkey = crypto_xcbc_digest_setkey; 274
275 inst->alg.init = crypto_xcbc_digest_init;
276 inst->alg.update = crypto_xcbc_digest_update;
277 inst->alg.final = crypto_xcbc_digest_final;
278 inst->alg.setkey = crypto_xcbc_digest_setkey;
279
280 err = shash_register_instance(tmpl, inst);
281 if (err) {
282out_free_inst:
283 shash_free_instance(shash_crypto_instance(inst));
284 }
340 285
341out_put_alg: 286out_put_alg:
342 crypto_mod_put(alg); 287 crypto_mod_put(alg);
343 return inst; 288 return err;
344}
345
346static void xcbc_free(struct crypto_instance *inst)
347{
348 crypto_drop_spawn(crypto_instance_ctx(inst));
349 kfree(inst);
350} 289}
351 290
352static struct crypto_template crypto_xcbc_tmpl = { 291static struct crypto_template crypto_xcbc_tmpl = {
353 .name = "xcbc", 292 .name = "xcbc",
354 .alloc = xcbc_alloc, 293 .create = xcbc_create,
355 .free = xcbc_free, 294 .free = shash_free_instance,
356 .module = THIS_MODULE, 295 .module = THIS_MODULE,
357}; 296};
358 297