2 * Copyright (C)2006 USAGI/WIDE Project
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.
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.
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
19 * Kazunori Miyazawa <miyazawa@linux-ipv6.org>
22 #include <crypto/internal/hash.h>
23 #include <linux/err.h>
24 #include <linux/kernel.h>
26 static u_int32_t ks[12] = {0x01010101, 0x01010101, 0x01010101, 0x01010101,
27 0x02020202, 0x02020202, 0x02020202, 0x02020202,
28 0x03030303, 0x03030303, 0x03030303, 0x03030303};
30 * +------------------------
32 * +------------------------
34 * +------------------------
36 * +------------------------
38 * +------------------------
40 * +------------------------
41 * | consts (block size * 3)
42 * +------------------------
44 struct crypto_xcbc_ctx {
45 struct crypto_cipher *child;
54 static int _crypto_xcbc_digest_setkey(struct crypto_shash *parent,
55 struct crypto_xcbc_ctx *ctx)
57 int bs = crypto_shash_blocksize(parent);
61 if ((err = crypto_cipher_setkey(ctx->child, ctx->key, ctx->keylen)))
64 crypto_cipher_encrypt_one(ctx->child, key1, ctx->consts);
66 return crypto_cipher_setkey(ctx->child, key1, bs);
69 static int crypto_xcbc_digest_setkey(struct crypto_shash *parent,
70 const u8 *inkey, unsigned int keylen)
72 struct crypto_xcbc_ctx *ctx = crypto_shash_ctx(parent);
74 if (keylen != crypto_cipher_blocksize(ctx->child))
78 memcpy(ctx->key, inkey, keylen);
79 ctx->consts = (u8*)ks;
81 return _crypto_xcbc_digest_setkey(parent, ctx);
84 static int crypto_xcbc_digest_init(struct shash_desc *pdesc)
86 struct crypto_xcbc_ctx *ctx = crypto_shash_ctx(pdesc->tfm);
87 int bs = crypto_shash_blocksize(pdesc->tfm);
90 memset(ctx->odds, 0, bs);
91 memset(ctx->prev, 0, bs);
96 static int crypto_xcbc_digest_update(struct shash_desc *pdesc, const u8 *p,
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);
104 /* checking the data can fill the block */
105 if ((ctx->len + len) <= bs) {
106 memcpy(ctx->odds + ctx->len, p, len);
111 /* filling odds with new data and encrypting it */
112 memcpy(ctx->odds + ctx->len, p, bs - ctx->len);
113 len -= bs - ctx->len;
116 crypto_xor(ctx->prev, ctx->odds, bs);
117 crypto_cipher_encrypt_one(tfm, ctx->prev, ctx->prev);
119 /* clearing the length */
122 /* encrypting the rest of data */
124 crypto_xor(ctx->prev, p, bs);
125 crypto_cipher_encrypt_one(tfm, ctx->prev, ctx->prev);
130 /* keeping the surplus of blocksize */
132 memcpy(ctx->odds, p, len);
139 static int crypto_xcbc_digest_final(struct shash_desc *pdesc, u8 *out)
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);
147 if (ctx->len == bs) {
150 if ((err = crypto_cipher_setkey(tfm, ctx->key, ctx->keylen)) != 0)
153 crypto_cipher_encrypt_one(tfm, key2,
154 (u8 *)(ctx->consts + bs));
156 crypto_xor(ctx->prev, ctx->odds, bs);
157 crypto_xor(ctx->prev, key2, bs);
158 _crypto_xcbc_digest_setkey(parent, ctx);
160 crypto_cipher_encrypt_one(tfm, out, ctx->prev);
164 u8 *p = ctx->odds + ctx->len;
168 rlen = bs - ctx->len -1;
172 if ((err = crypto_cipher_setkey(tfm, ctx->key, ctx->keylen)) != 0)
175 crypto_cipher_encrypt_one(tfm, key3,
176 (u8 *)(ctx->consts + bs * 2));
178 crypto_xor(ctx->prev, ctx->odds, bs);
179 crypto_xor(ctx->prev, key3, bs);
181 _crypto_xcbc_digest_setkey(parent, ctx);
183 crypto_cipher_encrypt_one(tfm, out, ctx->prev);
189 static int xcbc_init_tfm(struct crypto_tfm *tfm)
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);
197 cipher = crypto_spawn_cipher(spawn);
199 return PTR_ERR(cipher);
209 ctx->odds = (u8*)(ctx+1);
210 ctx->prev = ctx->odds + bs;
211 ctx->key = ctx->prev + bs;
216 static void xcbc_exit_tfm(struct crypto_tfm *tfm)
218 struct crypto_xcbc_ctx *ctx = crypto_tfm_ctx(tfm);
219 crypto_free_cipher(ctx->child);
222 static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
224 struct shash_instance *inst;
225 struct crypto_alg *alg;
228 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
232 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
233 CRYPTO_ALG_TYPE_MASK);
237 switch(alg->cra_blocksize) {
244 inst = shash_alloc_instance("xcbc", alg);
249 err = crypto_init_spawn(shash_instance_ctx(inst), alg,
250 shash_crypto_instance(inst),
251 CRYPTO_ALG_TYPE_MASK);
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;
259 inst->alg.digestsize = alg->cra_blocksize;
260 inst->alg.base.cra_ctxsize = sizeof(struct crypto_xcbc_ctx) +
261 ALIGN(alg->cra_blocksize * 3,
263 inst->alg.base.cra_init = xcbc_init_tfm;
264 inst->alg.base.cra_exit = xcbc_exit_tfm;
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;
271 err = shash_register_instance(tmpl, inst);
274 shash_free_instance(shash_crypto_instance(inst));
282 static struct crypto_template crypto_xcbc_tmpl = {
284 .create = xcbc_create,
285 .free = shash_free_instance,
286 .module = THIS_MODULE,
289 static int __init crypto_xcbc_module_init(void)
291 return crypto_register_template(&crypto_xcbc_tmpl);
294 static void __exit crypto_xcbc_module_exit(void)
296 crypto_unregister_template(&crypto_xcbc_tmpl);
299 module_init(crypto_xcbc_module_init);
300 module_exit(crypto_xcbc_module_exit);
302 MODULE_LICENSE("GPL");
303 MODULE_DESCRIPTION("XCBC keyed hash algorithm");