blob: 9d502e67a5c013661e0c1f5aad44f62643ebb0e0 [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
Herbert Xu3106caa2009-07-12 12:48:32 +080022#include <crypto/internal/hash.h>
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100023#include <linux/err.h>
24#include <linux/kernel.h>
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100025
Adrian Bunk5b375382006-11-17 13:43:04 +110026static u_int32_t ks[12] = {0x01010101, 0x01010101, 0x01010101, 0x01010101,
27 0x02020202, 0x02020202, 0x02020202, 0x02020202,
28 0x03030303, 0x03030303, 0x03030303, 0x03030303};
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100029/*
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 */
44struct crypto_xcbc_ctx {
Herbert Xu6b701dd2006-12-24 09:59:42 +110045 struct crypto_cipher *child;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100046 u8 *odds;
47 u8 *prev;
48 u8 *key;
49 u8 *consts;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100050 unsigned int keylen;
51 unsigned int len;
52};
53
Herbert Xu3106caa2009-07-12 12:48:32 +080054static int _crypto_xcbc_digest_setkey(struct crypto_shash *parent,
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100055 struct crypto_xcbc_ctx *ctx)
56{
Herbert Xu3106caa2009-07-12 12:48:32 +080057 int bs = crypto_shash_blocksize(parent);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100058 int err = 0;
59 u8 key1[bs];
60
61 if ((err = crypto_cipher_setkey(ctx->child, ctx->key, ctx->keylen)))
62 return err;
63
Herbert Xu6b701dd2006-12-24 09:59:42 +110064 crypto_cipher_encrypt_one(ctx->child, key1, ctx->consts);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100065
66 return crypto_cipher_setkey(ctx->child, key1, bs);
67}
68
Herbert Xu3106caa2009-07-12 12:48:32 +080069static int crypto_xcbc_digest_setkey(struct crypto_shash *parent,
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100070 const u8 *inkey, unsigned int keylen)
71{
Herbert Xu3106caa2009-07-12 12:48:32 +080072 struct crypto_xcbc_ctx *ctx = crypto_shash_ctx(parent);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100073
Herbert Xu6b701dd2006-12-24 09:59:42 +110074 if (keylen != crypto_cipher_blocksize(ctx->child))
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100075 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
Herbert Xu3106caa2009-07-12 12:48:32 +080084static int crypto_xcbc_digest_init(struct shash_desc *pdesc)
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100085{
Herbert Xu3106caa2009-07-12 12:48:32 +080086 struct crypto_xcbc_ctx *ctx = crypto_shash_ctx(pdesc->tfm);
87 int bs = crypto_shash_blocksize(pdesc->tfm);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100088
89 ctx->len = 0;
90 memset(ctx->odds, 0, bs);
91 memset(ctx->prev, 0, bs);
92
93 return 0;
94}
95
Herbert Xu3106caa2009-07-12 12:48:32 +080096static int crypto_xcbc_digest_update(struct shash_desc *pdesc, const u8 *p,
97 unsigned int len)
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100098{
Herbert Xu3106caa2009-07-12 12:48:32 +080099 struct crypto_shash *parent = pdesc->tfm;
100 struct crypto_xcbc_ctx *ctx = crypto_shash_ctx(parent);
Herbert Xu6b701dd2006-12-24 09:59:42 +1100101 struct crypto_cipher *tfm = ctx->child;
Herbert Xu3106caa2009-07-12 12:48:32 +0800102 int bs = crypto_shash_blocksize(parent);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000103
Herbert Xu3106caa2009-07-12 12:48:32 +0800104 /* 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 }
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000110
Herbert Xu3106caa2009-07-12 12:48:32 +0800111 /* 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;
Joy Latten2f40a172008-03-06 19:28:44 +0800115
Herbert Xub588ef62009-07-22 13:04:37 +0800116 crypto_xor(ctx->prev, ctx->odds, bs);
Herbert Xu3106caa2009-07-12 12:48:32 +0800117 crypto_cipher_encrypt_one(tfm, ctx->prev, ctx->prev);
Joy Latten2f40a172008-03-06 19:28:44 +0800118
Herbert Xu3106caa2009-07-12 12:48:32 +0800119 /* clearing the length */
120 ctx->len = 0;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000121
Herbert Xu3106caa2009-07-12 12:48:32 +0800122 /* encrypting the rest of data */
123 while (len > bs) {
Herbert Xub588ef62009-07-22 13:04:37 +0800124 crypto_xor(ctx->prev, p, bs);
Herbert Xu3106caa2009-07-12 12:48:32 +0800125 crypto_cipher_encrypt_one(tfm, ctx->prev, ctx->prev);
126 p += bs;
127 len -= bs;
128 }
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000129
Herbert Xu3106caa2009-07-12 12:48:32 +0800130 /* keeping the surplus of blocksize */
131 if (len) {
132 memcpy(ctx->odds, p, len);
133 ctx->len = len;
Joy Latten1edcf2e2008-04-02 14:36:09 +0800134 }
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000135
136 return 0;
137}
138
Herbert Xu3106caa2009-07-12 12:48:32 +0800139static int crypto_xcbc_digest_final(struct shash_desc *pdesc, u8 *out)
Herbert Xufb469842006-12-10 10:45:28 +1100140{
Herbert Xu3106caa2009-07-12 12:48:32 +0800141 struct crypto_shash *parent = pdesc->tfm;
142 struct crypto_xcbc_ctx *ctx = crypto_shash_ctx(parent);
Herbert Xu6b701dd2006-12-24 09:59:42 +1100143 struct crypto_cipher *tfm = ctx->child;
Herbert Xu3106caa2009-07-12 12:48:32 +0800144 int bs = crypto_shash_blocksize(parent);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000145 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
Herbert Xu6b701dd2006-12-24 09:59:42 +1100153 crypto_cipher_encrypt_one(tfm, key2,
154 (u8 *)(ctx->consts + bs));
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000155
Herbert Xub588ef62009-07-22 13:04:37 +0800156 crypto_xor(ctx->prev, ctx->odds, bs);
157 crypto_xor(ctx->prev, key2, bs);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000158 _crypto_xcbc_digest_setkey(parent, ctx);
159
Herbert Xu6b701dd2006-12-24 09:59:42 +1100160 crypto_cipher_encrypt_one(tfm, out, ctx->prev);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000161 } 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
Herbert Xu6b701dd2006-12-24 09:59:42 +1100175 crypto_cipher_encrypt_one(tfm, key3,
176 (u8 *)(ctx->consts + bs * 2));
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000177
Herbert Xub588ef62009-07-22 13:04:37 +0800178 crypto_xor(ctx->prev, ctx->odds, bs);
179 crypto_xor(ctx->prev, key3, bs);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000180
181 _crypto_xcbc_digest_setkey(parent, ctx);
182
Herbert Xu6b701dd2006-12-24 09:59:42 +1100183 crypto_cipher_encrypt_one(tfm, out, ctx->prev);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000184 }
185
186 return 0;
187}
188
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000189static int xcbc_init_tfm(struct crypto_tfm *tfm)
190{
Herbert Xu2e306ee2006-12-17 10:05:58 +1100191 struct crypto_cipher *cipher;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000192 struct crypto_instance *inst = (void *)tfm->__crt_alg;
193 struct crypto_spawn *spawn = crypto_instance_ctx(inst);
Herbert Xu3106caa2009-07-12 12:48:32 +0800194 struct crypto_xcbc_ctx *ctx = crypto_tfm_ctx(tfm);
195 int bs = crypto_tfm_alg_blocksize(tfm);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000196
Herbert Xu2e306ee2006-12-17 10:05:58 +1100197 cipher = crypto_spawn_cipher(spawn);
198 if (IS_ERR(cipher))
199 return PTR_ERR(cipher);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000200
201 switch(bs) {
202 case 16:
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000203 break;
204 default:
205 return -EINVAL;
206 }
207
Herbert Xu2e306ee2006-12-17 10:05:58 +1100208 ctx->child = cipher;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000209 ctx->odds = (u8*)(ctx+1);
210 ctx->prev = ctx->odds + bs;
211 ctx->key = ctx->prev + bs;
212
213 return 0;
214};
215
216static void xcbc_exit_tfm(struct crypto_tfm *tfm)
217{
Herbert Xu3106caa2009-07-12 12:48:32 +0800218 struct crypto_xcbc_ctx *ctx = crypto_tfm_ctx(tfm);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000219 crypto_free_cipher(ctx->child);
220}
221
Herbert Xu3106caa2009-07-12 12:48:32 +0800222static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000223{
Herbert Xu3106caa2009-07-12 12:48:32 +0800224 struct shash_instance *inst;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000225 struct crypto_alg *alg;
Herbert Xuebc610e2007-01-01 18:37:02 +1100226 int err;
227
Herbert Xu3106caa2009-07-12 12:48:32 +0800228 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
Herbert Xuebc610e2007-01-01 18:37:02 +1100229 if (err)
Herbert Xu3106caa2009-07-12 12:48:32 +0800230 return err;
Herbert Xuebc610e2007-01-01 18:37:02 +1100231
232 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
233 CRYPTO_ALG_TYPE_MASK);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000234 if (IS_ERR(alg))
Herbert Xu3106caa2009-07-12 12:48:32 +0800235 return PTR_ERR(alg);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000236
237 switch(alg->cra_blocksize) {
238 case 16:
239 break;
240 default:
Herbert Xu1b878872008-01-01 15:44:50 +1100241 goto out_put_alg;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000242 }
243
Herbert Xu3106caa2009-07-12 12:48:32 +0800244 inst = shash_alloc_instance("xcbc", alg);
Herbert Xub5ebd442009-07-15 16:53:33 +0800245 err = PTR_ERR(inst);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000246 if (IS_ERR(inst))
247 goto out_put_alg;
248
Herbert Xu3106caa2009-07-12 12:48:32 +0800249 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;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000254
Herbert Xu3106caa2009-07-12 12:48:32 +0800255 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;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000258
Herbert Xu3106caa2009-07-12 12:48:32 +0800259 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) {
273out_free_inst:
274 shash_free_instance(shash_crypto_instance(inst));
275 }
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000276
277out_put_alg:
278 crypto_mod_put(alg);
Herbert Xu3106caa2009-07-12 12:48:32 +0800279 return err;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000280}
281
282static struct crypto_template crypto_xcbc_tmpl = {
283 .name = "xcbc",
Herbert Xu3106caa2009-07-12 12:48:32 +0800284 .create = xcbc_create,
285 .free = shash_free_instance,
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000286 .module = THIS_MODULE,
287};
288
289static int __init crypto_xcbc_module_init(void)
290{
291 return crypto_register_template(&crypto_xcbc_tmpl);
292}
293
294static void __exit crypto_xcbc_module_exit(void)
295{
296 crypto_unregister_template(&crypto_xcbc_tmpl);
297}
298
299module_init(crypto_xcbc_module_init);
300module_exit(crypto_xcbc_module_exit);
301
302MODULE_LICENSE("GPL");
303MODULE_DESCRIPTION("XCBC keyed hash algorithm");