blob: bb7b67fba3495ccafbb2bc3a714b1dae6bae47a8 [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};
Herbert Xuac953012009-07-22 14:37:15 +080029
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100030/*
31 * +------------------------
32 * | <parent tfm>
33 * +------------------------
Herbert Xuac953012009-07-22 14:37:15 +080034 * | xcbc_tfm_ctx
35 * +------------------------
36 * | consts (block size * 2)
37 * +------------------------
38 */
39struct xcbc_tfm_ctx {
40 struct crypto_cipher *child;
41 u8 ctx[];
42};
43
44/*
45 * +------------------------
46 * | <shash desc>
47 * +------------------------
48 * | xcbc_desc_ctx
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100049 * +------------------------
50 * | odds (block size)
51 * +------------------------
52 * | prev (block size)
53 * +------------------------
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100054 */
Herbert Xuac953012009-07-22 14:37:15 +080055struct xcbc_desc_ctx {
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100056 unsigned int len;
Herbert Xuac953012009-07-22 14:37:15 +080057 u8 ctx[];
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100058};
59
Herbert Xu3106caa2009-07-12 12:48:32 +080060static int crypto_xcbc_digest_setkey(struct crypto_shash *parent,
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100061 const u8 *inkey, unsigned int keylen)
62{
Herbert Xuac953012009-07-22 14:37:15 +080063 unsigned long alignmask = crypto_shash_alignmask(parent);
64 struct xcbc_tfm_ctx *ctx = crypto_shash_ctx(parent);
65 int bs = crypto_shash_blocksize(parent);
66 u8 *consts = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
67 int err = 0;
68 u8 key1[bs];
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100069
Herbert Xuac953012009-07-22 14:37:15 +080070 if ((err = crypto_cipher_setkey(ctx->child, inkey, keylen)))
71 return err;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100072
Herbert Xuac953012009-07-22 14:37:15 +080073 crypto_cipher_encrypt_one(ctx->child, consts, (u8 *)ks + bs);
74 crypto_cipher_encrypt_one(ctx->child, consts + bs, (u8 *)ks + bs * 2);
75 crypto_cipher_encrypt_one(ctx->child, key1, (u8 *)ks);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100076
Herbert Xuac953012009-07-22 14:37:15 +080077 return crypto_cipher_setkey(ctx->child, key1, bs);
78
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100079}
80
Herbert Xu3106caa2009-07-12 12:48:32 +080081static int crypto_xcbc_digest_init(struct shash_desc *pdesc)
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100082{
Herbert Xuac953012009-07-22 14:37:15 +080083 unsigned long alignmask = crypto_shash_alignmask(pdesc->tfm);
84 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
Herbert Xu3106caa2009-07-12 12:48:32 +080085 int bs = crypto_shash_blocksize(pdesc->tfm);
Herbert Xuac953012009-07-22 14:37:15 +080086 u8 *prev = PTR_ALIGN(&ctx->ctx[0], alignmask + 1) + bs;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100087
88 ctx->len = 0;
Herbert Xuac953012009-07-22 14:37:15 +080089 memset(prev, 0, bs);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100090
91 return 0;
92}
93
Herbert Xu3106caa2009-07-12 12:48:32 +080094static int crypto_xcbc_digest_update(struct shash_desc *pdesc, const u8 *p,
95 unsigned int len)
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100096{
Herbert Xu3106caa2009-07-12 12:48:32 +080097 struct crypto_shash *parent = pdesc->tfm;
Herbert Xuac953012009-07-22 14:37:15 +080098 unsigned long alignmask = crypto_shash_alignmask(parent);
99 struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
100 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
101 struct crypto_cipher *tfm = tctx->child;
Herbert Xu3106caa2009-07-12 12:48:32 +0800102 int bs = crypto_shash_blocksize(parent);
Herbert Xuac953012009-07-22 14:37:15 +0800103 u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
104 u8 *prev = odds + bs;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000105
Herbert Xu3106caa2009-07-12 12:48:32 +0800106 /* checking the data can fill the block */
107 if ((ctx->len + len) <= bs) {
Herbert Xuac953012009-07-22 14:37:15 +0800108 memcpy(odds + ctx->len, p, len);
Herbert Xu3106caa2009-07-12 12:48:32 +0800109 ctx->len += len;
110 return 0;
111 }
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000112
Herbert Xu3106caa2009-07-12 12:48:32 +0800113 /* filling odds with new data and encrypting it */
Herbert Xuac953012009-07-22 14:37:15 +0800114 memcpy(odds + ctx->len, p, bs - ctx->len);
Herbert Xu3106caa2009-07-12 12:48:32 +0800115 len -= bs - ctx->len;
116 p += bs - ctx->len;
Joy Latten2f40a172008-03-06 19:28:44 +0800117
Herbert Xuac953012009-07-22 14:37:15 +0800118 crypto_xor(prev, odds, bs);
119 crypto_cipher_encrypt_one(tfm, prev, prev);
Joy Latten2f40a172008-03-06 19:28:44 +0800120
Herbert Xu3106caa2009-07-12 12:48:32 +0800121 /* clearing the length */
122 ctx->len = 0;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000123
Herbert Xu3106caa2009-07-12 12:48:32 +0800124 /* encrypting the rest of data */
125 while (len > bs) {
Herbert Xuac953012009-07-22 14:37:15 +0800126 crypto_xor(prev, p, bs);
127 crypto_cipher_encrypt_one(tfm, prev, prev);
Herbert Xu3106caa2009-07-12 12:48:32 +0800128 p += bs;
129 len -= bs;
130 }
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000131
Herbert Xu3106caa2009-07-12 12:48:32 +0800132 /* keeping the surplus of blocksize */
133 if (len) {
Herbert Xuac953012009-07-22 14:37:15 +0800134 memcpy(odds, p, len);
Herbert Xu3106caa2009-07-12 12:48:32 +0800135 ctx->len = len;
Joy Latten1edcf2e2008-04-02 14:36:09 +0800136 }
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000137
138 return 0;
139}
140
Herbert Xu3106caa2009-07-12 12:48:32 +0800141static int crypto_xcbc_digest_final(struct shash_desc *pdesc, u8 *out)
Herbert Xufb469842006-12-10 10:45:28 +1100142{
Herbert Xu3106caa2009-07-12 12:48:32 +0800143 struct crypto_shash *parent = pdesc->tfm;
Herbert Xuac953012009-07-22 14:37:15 +0800144 unsigned long alignmask = crypto_shash_alignmask(parent);
145 struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
146 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
147 struct crypto_cipher *tfm = tctx->child;
Herbert Xu3106caa2009-07-12 12:48:32 +0800148 int bs = crypto_shash_blocksize(parent);
Herbert Xuac953012009-07-22 14:37:15 +0800149 u8 *consts = PTR_ALIGN(&tctx->ctx[0], alignmask + 1);
150 u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
151 u8 *prev = odds + bs;
152 unsigned int offset = 0;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000153
Herbert Xuac953012009-07-22 14:37:15 +0800154 if (ctx->len != bs) {
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000155 unsigned int rlen;
Herbert Xuac953012009-07-22 14:37:15 +0800156 u8 *p = odds + ctx->len;
157
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000158 *p = 0x80;
159 p++;
160
161 rlen = bs - ctx->len -1;
162 if (rlen)
163 memset(p, 0, rlen);
164
Herbert Xuac953012009-07-22 14:37:15 +0800165 offset += bs;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000166 }
167
Herbert Xuac953012009-07-22 14:37:15 +0800168 crypto_xor(prev, odds, bs);
169 crypto_xor(prev, consts + offset, bs);
170
171 crypto_cipher_encrypt_one(tfm, out, prev);
172
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000173 return 0;
174}
175
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000176static int xcbc_init_tfm(struct crypto_tfm *tfm)
177{
Herbert Xu2e306ee2006-12-17 10:05:58 +1100178 struct crypto_cipher *cipher;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000179 struct crypto_instance *inst = (void *)tfm->__crt_alg;
180 struct crypto_spawn *spawn = crypto_instance_ctx(inst);
Herbert Xuac953012009-07-22 14:37:15 +0800181 struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000182
Herbert Xu2e306ee2006-12-17 10:05:58 +1100183 cipher = crypto_spawn_cipher(spawn);
184 if (IS_ERR(cipher))
185 return PTR_ERR(cipher);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000186
Herbert Xu2e306ee2006-12-17 10:05:58 +1100187 ctx->child = cipher;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000188
189 return 0;
190};
191
192static void xcbc_exit_tfm(struct crypto_tfm *tfm)
193{
Herbert Xuac953012009-07-22 14:37:15 +0800194 struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000195 crypto_free_cipher(ctx->child);
196}
197
Herbert Xu3106caa2009-07-12 12:48:32 +0800198static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000199{
Herbert Xu3106caa2009-07-12 12:48:32 +0800200 struct shash_instance *inst;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000201 struct crypto_alg *alg;
Steffen Klassert36f87a42009-08-20 17:58:04 +1000202 unsigned long alignmask;
Herbert Xuebc610e2007-01-01 18:37:02 +1100203 int err;
204
Herbert Xu3106caa2009-07-12 12:48:32 +0800205 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
Herbert Xuebc610e2007-01-01 18:37:02 +1100206 if (err)
Herbert Xu3106caa2009-07-12 12:48:32 +0800207 return err;
Herbert Xuebc610e2007-01-01 18:37:02 +1100208
209 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
210 CRYPTO_ALG_TYPE_MASK);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000211 if (IS_ERR(alg))
Herbert Xu3106caa2009-07-12 12:48:32 +0800212 return PTR_ERR(alg);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000213
214 switch(alg->cra_blocksize) {
215 case 16:
216 break;
217 default:
Herbert Xu1b878872008-01-01 15:44:50 +1100218 goto out_put_alg;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000219 }
220
Herbert Xu3106caa2009-07-12 12:48:32 +0800221 inst = shash_alloc_instance("xcbc", alg);
Herbert Xub5ebd442009-07-15 16:53:33 +0800222 err = PTR_ERR(inst);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000223 if (IS_ERR(inst))
224 goto out_put_alg;
225
Herbert Xu3106caa2009-07-12 12:48:32 +0800226 err = crypto_init_spawn(shash_instance_ctx(inst), alg,
227 shash_crypto_instance(inst),
228 CRYPTO_ALG_TYPE_MASK);
229 if (err)
230 goto out_free_inst;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000231
Steffen Klassert36f87a42009-08-20 17:58:04 +1000232 alignmask = alg->cra_alignmask | 3;
233 inst->alg.base.cra_alignmask = alignmask;
Herbert Xu3106caa2009-07-12 12:48:32 +0800234 inst->alg.base.cra_priority = alg->cra_priority;
235 inst->alg.base.cra_blocksize = alg->cra_blocksize;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000236
Herbert Xu3106caa2009-07-12 12:48:32 +0800237 inst->alg.digestsize = alg->cra_blocksize;
Herbert Xuac953012009-07-22 14:37:15 +0800238 inst->alg.descsize = ALIGN(sizeof(struct xcbc_desc_ctx),
239 crypto_tfm_ctx_alignment()) +
Steffen Klassert36f87a42009-08-20 17:58:04 +1000240 (alignmask &
Herbert Xuac953012009-07-22 14:37:15 +0800241 ~(crypto_tfm_ctx_alignment() - 1)) +
242 alg->cra_blocksize * 2;
243
244 inst->alg.base.cra_ctxsize = ALIGN(sizeof(struct xcbc_tfm_ctx),
Steffen Klassert36f87a42009-08-20 17:58:04 +1000245 alignmask + 1) +
Herbert Xuac953012009-07-22 14:37:15 +0800246 alg->cra_blocksize * 2;
Herbert Xu3106caa2009-07-12 12:48:32 +0800247 inst->alg.base.cra_init = xcbc_init_tfm;
248 inst->alg.base.cra_exit = xcbc_exit_tfm;
249
250 inst->alg.init = crypto_xcbc_digest_init;
251 inst->alg.update = crypto_xcbc_digest_update;
252 inst->alg.final = crypto_xcbc_digest_final;
253 inst->alg.setkey = crypto_xcbc_digest_setkey;
254
255 err = shash_register_instance(tmpl, inst);
256 if (err) {
257out_free_inst:
258 shash_free_instance(shash_crypto_instance(inst));
259 }
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000260
261out_put_alg:
262 crypto_mod_put(alg);
Herbert Xu3106caa2009-07-12 12:48:32 +0800263 return err;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000264}
265
266static struct crypto_template crypto_xcbc_tmpl = {
267 .name = "xcbc",
Herbert Xu3106caa2009-07-12 12:48:32 +0800268 .create = xcbc_create,
269 .free = shash_free_instance,
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000270 .module = THIS_MODULE,
271};
272
273static int __init crypto_xcbc_module_init(void)
274{
275 return crypto_register_template(&crypto_xcbc_tmpl);
276}
277
278static void __exit crypto_xcbc_module_exit(void)
279{
280 crypto_unregister_template(&crypto_xcbc_tmpl);
281}
282
283module_init(crypto_xcbc_module_init);
284module_exit(crypto_xcbc_module_exit);
285
286MODULE_LICENSE("GPL");
287MODULE_DESCRIPTION("XCBC keyed hash algorithm");