crypto: xcbc - Use crypto_xor
[safe/jmp/linux-2.6] / crypto / xcbc.c
1 /*
2  * Copyright (C)2006 USAGI/WIDE Project
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  * Author:
19  *      Kazunori Miyazawa <miyazawa@linux-ipv6.org>
20  */
21
22 #include <crypto/internal/hash.h>
23 #include <linux/err.h>
24 #include <linux/kernel.h>
25
26 static u_int32_t ks[12] = {0x01010101, 0x01010101, 0x01010101, 0x01010101,
27                            0x02020202, 0x02020202, 0x02020202, 0x02020202,
28                            0x03030303, 0x03030303, 0x03030303, 0x03030303};
29 /*
30  * +------------------------
31  * | <parent tfm>
32  * +------------------------
33  * | crypto_xcbc_ctx
34  * +------------------------
35  * | odds (block size)
36  * +------------------------
37  * | prev (block size)
38  * +------------------------
39  * | key (block size)
40  * +------------------------
41  * | consts (block size * 3)
42  * +------------------------
43  */
44 struct crypto_xcbc_ctx {
45         struct crypto_cipher *child;
46         u8 *odds;
47         u8 *prev;
48         u8 *key;
49         u8 *consts;
50         unsigned int keylen;
51         unsigned int len;
52 };
53
54 static int _crypto_xcbc_digest_setkey(struct crypto_shash *parent,
55                                       struct crypto_xcbc_ctx *ctx)
56 {
57         int bs = crypto_shash_blocksize(parent);
58         int err = 0;
59         u8 key1[bs];
60
61         if ((err = crypto_cipher_setkey(ctx->child, ctx->key, ctx->keylen)))
62             return err;
63
64         crypto_cipher_encrypt_one(ctx->child, key1, ctx->consts);
65
66         return crypto_cipher_setkey(ctx->child, key1, bs);
67 }
68
69 static int crypto_xcbc_digest_setkey(struct crypto_shash *parent,
70                                      const u8 *inkey, unsigned int keylen)
71 {
72         struct crypto_xcbc_ctx *ctx = crypto_shash_ctx(parent);
73
74         if (keylen != crypto_cipher_blocksize(ctx->child))
75                 return -EINVAL;
76
77         ctx->keylen = keylen;
78         memcpy(ctx->key, inkey, keylen);
79         ctx->consts = (u8*)ks;
80
81         return _crypto_xcbc_digest_setkey(parent, ctx);
82 }
83
84 static int crypto_xcbc_digest_init(struct shash_desc *pdesc)
85 {
86         struct crypto_xcbc_ctx *ctx = crypto_shash_ctx(pdesc->tfm);
87         int bs = crypto_shash_blocksize(pdesc->tfm);
88
89         ctx->len = 0;
90         memset(ctx->odds, 0, bs);
91         memset(ctx->prev, 0, bs);
92
93         return 0;
94 }
95
96 static int crypto_xcbc_digest_update(struct shash_desc *pdesc, const u8 *p,
97                                      unsigned int len)
98 {
99         struct crypto_shash *parent = pdesc->tfm;
100         struct crypto_xcbc_ctx *ctx = crypto_shash_ctx(parent);
101         struct crypto_cipher *tfm = ctx->child;
102         int bs = crypto_shash_blocksize(parent);
103
104         /* checking the data can fill the block */
105         if ((ctx->len + len) <= bs) {
106                 memcpy(ctx->odds + ctx->len, p, len);
107                 ctx->len += len;
108                 return 0;
109         }
110
111         /* filling odds with new data and encrypting it */
112         memcpy(ctx->odds + ctx->len, p, bs - ctx->len);
113         len -= bs - ctx->len;
114         p += bs - ctx->len;
115
116         crypto_xor(ctx->prev, ctx->odds, bs);
117         crypto_cipher_encrypt_one(tfm, ctx->prev, ctx->prev);
118
119         /* clearing the length */
120         ctx->len = 0;
121
122         /* encrypting the rest of data */
123         while (len > bs) {
124                 crypto_xor(ctx->prev, p, bs);
125                 crypto_cipher_encrypt_one(tfm, ctx->prev, ctx->prev);
126                 p += bs;
127                 len -= bs;
128         }
129
130         /* keeping the surplus of blocksize */
131         if (len) {
132                 memcpy(ctx->odds, p, len);
133                 ctx->len = len;
134         }
135
136         return 0;
137 }
138
139 static int crypto_xcbc_digest_final(struct shash_desc *pdesc, u8 *out)
140 {
141         struct crypto_shash *parent = pdesc->tfm;
142         struct crypto_xcbc_ctx *ctx = crypto_shash_ctx(parent);
143         struct crypto_cipher *tfm = ctx->child;
144         int bs = crypto_shash_blocksize(parent);
145         int err = 0;
146
147         if (ctx->len == bs) {
148                 u8 key2[bs];
149
150                 if ((err = crypto_cipher_setkey(tfm, ctx->key, ctx->keylen)) != 0)
151                         return err;
152
153                 crypto_cipher_encrypt_one(tfm, key2,
154                                           (u8 *)(ctx->consts + bs));
155
156                 crypto_xor(ctx->prev, ctx->odds, bs);
157                 crypto_xor(ctx->prev, key2, bs);
158                 _crypto_xcbc_digest_setkey(parent, ctx);
159
160                 crypto_cipher_encrypt_one(tfm, out, ctx->prev);
161         } else {
162                 u8 key3[bs];
163                 unsigned int rlen;
164                 u8 *p = ctx->odds + ctx->len;
165                 *p = 0x80;
166                 p++;
167
168                 rlen = bs - ctx->len -1;
169                 if (rlen)
170                         memset(p, 0, rlen);
171
172                 if ((err = crypto_cipher_setkey(tfm, ctx->key, ctx->keylen)) != 0)
173                         return err;
174
175                 crypto_cipher_encrypt_one(tfm, key3,
176                                           (u8 *)(ctx->consts + bs * 2));
177
178                 crypto_xor(ctx->prev, ctx->odds, bs);
179                 crypto_xor(ctx->prev, key3, bs);
180
181                 _crypto_xcbc_digest_setkey(parent, ctx);
182
183                 crypto_cipher_encrypt_one(tfm, out, ctx->prev);
184         }
185
186         return 0;
187 }
188
189 static int xcbc_init_tfm(struct crypto_tfm *tfm)
190 {
191         struct crypto_cipher *cipher;
192         struct crypto_instance *inst = (void *)tfm->__crt_alg;
193         struct crypto_spawn *spawn = crypto_instance_ctx(inst);
194         struct crypto_xcbc_ctx *ctx = crypto_tfm_ctx(tfm);
195         int bs = crypto_tfm_alg_blocksize(tfm);
196
197         cipher = crypto_spawn_cipher(spawn);
198         if (IS_ERR(cipher))
199                 return PTR_ERR(cipher);
200
201         switch(bs) {
202         case 16:
203                 break;
204         default:
205                 return -EINVAL;
206         }
207
208         ctx->child = cipher;
209         ctx->odds = (u8*)(ctx+1);
210         ctx->prev = ctx->odds + bs;
211         ctx->key = ctx->prev + bs;
212
213         return 0;
214 };
215
216 static void xcbc_exit_tfm(struct crypto_tfm *tfm)
217 {
218         struct crypto_xcbc_ctx *ctx = crypto_tfm_ctx(tfm);
219         crypto_free_cipher(ctx->child);
220 }
221
222 static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
223 {
224         struct shash_instance *inst;
225         struct crypto_alg *alg;
226         int err;
227
228         err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
229         if (err)
230                 return err;
231
232         alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
233                                   CRYPTO_ALG_TYPE_MASK);
234         if (IS_ERR(alg))
235                 return PTR_ERR(alg);
236
237         switch(alg->cra_blocksize) {
238         case 16:
239                 break;
240         default:
241                 goto out_put_alg;
242         }
243
244         inst = shash_alloc_instance("xcbc", alg);
245         err = PTR_ERR(inst);
246         if (IS_ERR(inst))
247                 goto out_put_alg;
248
249         err = crypto_init_spawn(shash_instance_ctx(inst), alg,
250                                 shash_crypto_instance(inst),
251                                 CRYPTO_ALG_TYPE_MASK);
252         if (err)
253                 goto out_free_inst;
254
255         inst->alg.base.cra_priority = alg->cra_priority;
256         inst->alg.base.cra_blocksize = alg->cra_blocksize;
257         inst->alg.base.cra_alignmask = alg->cra_alignmask;
258
259         inst->alg.digestsize = alg->cra_blocksize;
260         inst->alg.base.cra_ctxsize = sizeof(struct crypto_xcbc_ctx) +
261                                      ALIGN(alg->cra_blocksize * 3,
262                                            sizeof(void *));
263         inst->alg.base.cra_init = xcbc_init_tfm;
264         inst->alg.base.cra_exit = xcbc_exit_tfm;
265
266         inst->alg.init = crypto_xcbc_digest_init;
267         inst->alg.update = crypto_xcbc_digest_update;
268         inst->alg.final = crypto_xcbc_digest_final;
269         inst->alg.setkey = crypto_xcbc_digest_setkey;
270
271         err = shash_register_instance(tmpl, inst);
272         if (err) {
273 out_free_inst:
274                 shash_free_instance(shash_crypto_instance(inst));
275         }
276
277 out_put_alg:
278         crypto_mod_put(alg);
279         return err;
280 }
281
282 static struct crypto_template crypto_xcbc_tmpl = {
283         .name = "xcbc",
284         .create = xcbc_create,
285         .free = shash_free_instance,
286         .module = THIS_MODULE,
287 };
288
289 static int __init crypto_xcbc_module_init(void)
290 {
291         return crypto_register_template(&crypto_xcbc_tmpl);
292 }
293
294 static void __exit crypto_xcbc_module_exit(void)
295 {
296         crypto_unregister_template(&crypto_xcbc_tmpl);
297 }
298
299 module_init(crypto_xcbc_module_init);
300 module_exit(crypto_xcbc_module_exit);
301
302 MODULE_LICENSE("GPL");
303 MODULE_DESCRIPTION("XCBC keyed hash algorithm");