blob: 317e9f08fc045ca7cee745c7418b606b11020ad8 [file] [log] [blame]
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +10001/*
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 <linux/crypto.h>
23#include <linux/err.h>
Herbert Xufb469842006-12-10 10:45:28 +110024#include <linux/hardirq.h>
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100025#include <linux/kernel.h>
26#include <linux/mm.h>
27#include <linux/rtnetlink.h>
28#include <linux/slab.h>
29#include <linux/scatterlist.h>
30#include "internal.h"
31
Adrian Bunk5b375382006-11-17 13:43:04 +110032static u_int32_t ks[12] = {0x01010101, 0x01010101, 0x01010101, 0x01010101,
33 0x02020202, 0x02020202, 0x02020202, 0x02020202,
34 0x03030303, 0x03030303, 0x03030303, 0x03030303};
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100035/*
36 * +------------------------
37 * | <parent tfm>
38 * +------------------------
39 * | crypto_xcbc_ctx
40 * +------------------------
41 * | odds (block size)
42 * +------------------------
43 * | prev (block size)
44 * +------------------------
45 * | key (block size)
46 * +------------------------
47 * | consts (block size * 3)
48 * +------------------------
49 */
50struct crypto_xcbc_ctx {
51 struct crypto_tfm *child;
52 u8 *odds;
53 u8 *prev;
54 u8 *key;
55 u8 *consts;
56 void (*xor)(u8 *a, const u8 *b, unsigned int bs);
57 unsigned int keylen;
58 unsigned int len;
59};
60
61static void xor_128(u8 *a, const u8 *b, unsigned int bs)
62{
63 ((u32 *)a)[0] ^= ((u32 *)b)[0];
64 ((u32 *)a)[1] ^= ((u32 *)b)[1];
65 ((u32 *)a)[2] ^= ((u32 *)b)[2];
66 ((u32 *)a)[3] ^= ((u32 *)b)[3];
67}
68
69static int _crypto_xcbc_digest_setkey(struct crypto_hash *parent,
70 struct crypto_xcbc_ctx *ctx)
71{
72 int bs = crypto_hash_blocksize(parent);
73 int err = 0;
74 u8 key1[bs];
75
76 if ((err = crypto_cipher_setkey(ctx->child, ctx->key, ctx->keylen)))
77 return err;
78
79 ctx->child->__crt_alg->cra_cipher.cia_encrypt(ctx->child, key1,
80 ctx->consts);
81
82 return crypto_cipher_setkey(ctx->child, key1, bs);
83}
84
85static int crypto_xcbc_digest_setkey(struct crypto_hash *parent,
86 const u8 *inkey, unsigned int keylen)
87{
88 struct crypto_xcbc_ctx *ctx = crypto_hash_ctx_aligned(parent);
89
90 if (keylen != crypto_tfm_alg_blocksize(ctx->child))
91 return -EINVAL;
92
93 ctx->keylen = keylen;
94 memcpy(ctx->key, inkey, keylen);
95 ctx->consts = (u8*)ks;
96
97 return _crypto_xcbc_digest_setkey(parent, ctx);
98}
99
Adrian Bunk5b375382006-11-17 13:43:04 +1100100static int crypto_xcbc_digest_init(struct hash_desc *pdesc)
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000101{
102 struct crypto_xcbc_ctx *ctx = crypto_hash_ctx_aligned(pdesc->tfm);
103 int bs = crypto_hash_blocksize(pdesc->tfm);
104
105 ctx->len = 0;
106 memset(ctx->odds, 0, bs);
107 memset(ctx->prev, 0, bs);
108
109 return 0;
110}
111
Herbert Xufb469842006-12-10 10:45:28 +1100112static int crypto_xcbc_digest_update2(struct hash_desc *pdesc,
113 struct scatterlist *sg,
114 unsigned int nbytes)
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000115{
116 struct crypto_hash *parent = pdesc->tfm;
117 struct crypto_xcbc_ctx *ctx = crypto_hash_ctx_aligned(parent);
118 struct crypto_tfm *tfm = ctx->child;
119 int bs = crypto_hash_blocksize(parent);
120 unsigned int i = 0;
121
122 do {
123
124 struct page *pg = sg[i].page;
125 unsigned int offset = sg[i].offset;
126 unsigned int slen = sg[i].length;
127
128 while (slen > 0) {
129 unsigned int len = min(slen, ((unsigned int)(PAGE_SIZE)) - offset);
130 char *p = crypto_kmap(pg, 0) + offset;
131
132 /* checking the data can fill the block */
133 if ((ctx->len + len) <= bs) {
134 memcpy(ctx->odds + ctx->len, p, len);
135 ctx->len += len;
136 slen -= len;
137
138 /* checking the rest of the page */
139 if (len + offset >= PAGE_SIZE) {
140 offset = 0;
141 pg++;
142 } else
143 offset += len;
144
145 crypto_kunmap(p, 0);
146 crypto_yield(tfm->crt_flags);
147 continue;
148 }
149
150 /* filling odds with new data and encrypting it */
151 memcpy(ctx->odds + ctx->len, p, bs - ctx->len);
152 len -= bs - ctx->len;
153 p += bs - ctx->len;
154
155 ctx->xor(ctx->prev, ctx->odds, bs);
156 tfm->__crt_alg->cra_cipher.cia_encrypt(tfm, ctx->prev, ctx->prev);
157
158 /* clearing the length */
159 ctx->len = 0;
160
161 /* encrypting the rest of data */
162 while (len > bs) {
163 ctx->xor(ctx->prev, p, bs);
164 tfm->__crt_alg->cra_cipher.cia_encrypt(tfm, ctx->prev, ctx->prev);
165 p += bs;
166 len -= bs;
167 }
168
169 /* keeping the surplus of blocksize */
170 if (len) {
171 memcpy(ctx->odds, p, len);
172 ctx->len = len;
173 }
174 crypto_kunmap(p, 0);
175 crypto_yield(tfm->crt_flags);
176 slen -= min(slen, ((unsigned int)(PAGE_SIZE)) - offset);
177 offset = 0;
178 pg++;
179 }
180 nbytes-=sg[i].length;
181 i++;
182 } while (nbytes>0);
183
184 return 0;
185}
186
Herbert Xufb469842006-12-10 10:45:28 +1100187static int crypto_xcbc_digest_update(struct hash_desc *pdesc,
188 struct scatterlist *sg,
189 unsigned int nbytes)
190{
191 if (WARN_ON_ONCE(in_irq()))
192 return -EDEADLK;
193 return crypto_xcbc_digest_update2(pdesc, sg, nbytes);
194}
195
Adrian Bunk5b375382006-11-17 13:43:04 +1100196static int crypto_xcbc_digest_final(struct hash_desc *pdesc, u8 *out)
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000197{
198 struct crypto_hash *parent = pdesc->tfm;
199 struct crypto_xcbc_ctx *ctx = crypto_hash_ctx_aligned(parent);
200 struct crypto_tfm *tfm = ctx->child;
201 int bs = crypto_hash_blocksize(parent);
202 int err = 0;
203
204 if (ctx->len == bs) {
205 u8 key2[bs];
206
207 if ((err = crypto_cipher_setkey(tfm, ctx->key, ctx->keylen)) != 0)
208 return err;
209
210 tfm->__crt_alg->cra_cipher.cia_encrypt(tfm, key2, (const u8*)(ctx->consts+bs));
211
212 ctx->xor(ctx->prev, ctx->odds, bs);
213 ctx->xor(ctx->prev, key2, bs);
214 _crypto_xcbc_digest_setkey(parent, ctx);
215
216 tfm->__crt_alg->cra_cipher.cia_encrypt(tfm, out, ctx->prev);
217 } else {
218 u8 key3[bs];
219 unsigned int rlen;
220 u8 *p = ctx->odds + ctx->len;
221 *p = 0x80;
222 p++;
223
224 rlen = bs - ctx->len -1;
225 if (rlen)
226 memset(p, 0, rlen);
227
228 if ((err = crypto_cipher_setkey(tfm, ctx->key, ctx->keylen)) != 0)
229 return err;
230
231 tfm->__crt_alg->cra_cipher.cia_encrypt(tfm, key3, (const u8*)(ctx->consts+bs*2));
232
233 ctx->xor(ctx->prev, ctx->odds, bs);
234 ctx->xor(ctx->prev, key3, bs);
235
236 _crypto_xcbc_digest_setkey(parent, ctx);
237
238 tfm->__crt_alg->cra_cipher.cia_encrypt(tfm, out, ctx->prev);
239 }
240
241 return 0;
242}
243
244static int crypto_xcbc_digest(struct hash_desc *pdesc,
245 struct scatterlist *sg, unsigned int nbytes, u8 *out)
246{
Herbert Xufb469842006-12-10 10:45:28 +1100247 if (WARN_ON_ONCE(in_irq()))
248 return -EDEADLK;
249
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000250 crypto_xcbc_digest_init(pdesc);
Herbert Xufb469842006-12-10 10:45:28 +1100251 crypto_xcbc_digest_update2(pdesc, sg, nbytes);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000252 return crypto_xcbc_digest_final(pdesc, out);
253}
254
255static int xcbc_init_tfm(struct crypto_tfm *tfm)
256{
257 struct crypto_instance *inst = (void *)tfm->__crt_alg;
258 struct crypto_spawn *spawn = crypto_instance_ctx(inst);
259 struct crypto_xcbc_ctx *ctx = crypto_hash_ctx_aligned(__crypto_hash_cast(tfm));
260 int bs = crypto_hash_blocksize(__crypto_hash_cast(tfm));
261
262 tfm = crypto_spawn_tfm(spawn);
263 if (IS_ERR(tfm))
264 return PTR_ERR(tfm);
265
266 switch(bs) {
267 case 16:
268 ctx->xor = xor_128;
269 break;
270 default:
271 return -EINVAL;
272 }
273
274 ctx->child = crypto_cipher_cast(tfm);
275 ctx->odds = (u8*)(ctx+1);
276 ctx->prev = ctx->odds + bs;
277 ctx->key = ctx->prev + bs;
278
279 return 0;
280};
281
282static void xcbc_exit_tfm(struct crypto_tfm *tfm)
283{
284 struct crypto_xcbc_ctx *ctx = crypto_hash_ctx_aligned(__crypto_hash_cast(tfm));
285 crypto_free_cipher(ctx->child);
286}
287
288static struct crypto_instance *xcbc_alloc(void *param, unsigned int len)
289{
290 struct crypto_instance *inst;
291 struct crypto_alg *alg;
292 alg = crypto_get_attr_alg(param, len, CRYPTO_ALG_TYPE_CIPHER,
293 CRYPTO_ALG_TYPE_HASH_MASK | CRYPTO_ALG_ASYNC);
294 if (IS_ERR(alg))
295 return ERR_PTR(PTR_ERR(alg));
296
297 switch(alg->cra_blocksize) {
298 case 16:
299 break;
300 default:
301 return ERR_PTR(PTR_ERR(alg));
302 }
303
304 inst = crypto_alloc_instance("xcbc", alg);
305 if (IS_ERR(inst))
306 goto out_put_alg;
307
308 inst->alg.cra_flags = CRYPTO_ALG_TYPE_HASH;
309 inst->alg.cra_priority = alg->cra_priority;
310 inst->alg.cra_blocksize = alg->cra_blocksize;
311 inst->alg.cra_alignmask = alg->cra_alignmask;
312 inst->alg.cra_type = &crypto_hash_type;
313
314 inst->alg.cra_hash.digestsize =
315 (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
316 CRYPTO_ALG_TYPE_HASH ? alg->cra_hash.digestsize :
317 alg->cra_blocksize;
318 inst->alg.cra_ctxsize = sizeof(struct crypto_xcbc_ctx) +
319 ALIGN(inst->alg.cra_blocksize * 3, sizeof(void *));
320 inst->alg.cra_init = xcbc_init_tfm;
321 inst->alg.cra_exit = xcbc_exit_tfm;
322
323 inst->alg.cra_hash.init = crypto_xcbc_digest_init;
324 inst->alg.cra_hash.update = crypto_xcbc_digest_update;
325 inst->alg.cra_hash.final = crypto_xcbc_digest_final;
326 inst->alg.cra_hash.digest = crypto_xcbc_digest;
327 inst->alg.cra_hash.setkey = crypto_xcbc_digest_setkey;
328
329out_put_alg:
330 crypto_mod_put(alg);
331 return inst;
332}
333
334static void xcbc_free(struct crypto_instance *inst)
335{
336 crypto_drop_spawn(crypto_instance_ctx(inst));
337 kfree(inst);
338}
339
340static struct crypto_template crypto_xcbc_tmpl = {
341 .name = "xcbc",
342 .alloc = xcbc_alloc,
343 .free = xcbc_free,
344 .module = THIS_MODULE,
345};
346
347static int __init crypto_xcbc_module_init(void)
348{
349 return crypto_register_template(&crypto_xcbc_tmpl);
350}
351
352static void __exit crypto_xcbc_module_exit(void)
353{
354 crypto_unregister_template(&crypto_xcbc_tmpl);
355}
356
357module_init(crypto_xcbc_module_init);
358module_exit(crypto_xcbc_module_exit);
359
360MODULE_LICENSE("GPL");
361MODULE_DESCRIPTION("XCBC keyed hash algorithm");