blob: df90b332554cf43fb595623af3e854534a9dc49b [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>
Paul Gortmaker4bb33cc2011-05-27 14:41:48 -040025#include <linux/module.h>
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100026
Adrian Bunk5b375382006-11-17 13:43:04 +110027static u_int32_t ks[12] = {0x01010101, 0x01010101, 0x01010101, 0x01010101,
28 0x02020202, 0x02020202, 0x02020202, 0x02020202,
29 0x03030303, 0x03030303, 0x03030303, 0x03030303};
Herbert Xuac953012009-07-22 14:37:15 +080030
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100031/*
32 * +------------------------
33 * | <parent tfm>
34 * +------------------------
Herbert Xuac953012009-07-22 14:37:15 +080035 * | xcbc_tfm_ctx
36 * +------------------------
37 * | consts (block size * 2)
38 * +------------------------
39 */
40struct xcbc_tfm_ctx {
41 struct crypto_cipher *child;
42 u8 ctx[];
43};
44
45/*
46 * +------------------------
47 * | <shash desc>
48 * +------------------------
49 * | xcbc_desc_ctx
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100050 * +------------------------
51 * | odds (block size)
52 * +------------------------
53 * | prev (block size)
54 * +------------------------
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100055 */
Herbert Xuac953012009-07-22 14:37:15 +080056struct xcbc_desc_ctx {
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100057 unsigned int len;
Herbert Xuac953012009-07-22 14:37:15 +080058 u8 ctx[];
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100059};
60
Herbert Xu3106caa2009-07-12 12:48:32 +080061static int crypto_xcbc_digest_setkey(struct crypto_shash *parent,
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100062 const u8 *inkey, unsigned int keylen)
63{
Herbert Xuac953012009-07-22 14:37:15 +080064 unsigned long alignmask = crypto_shash_alignmask(parent);
65 struct xcbc_tfm_ctx *ctx = crypto_shash_ctx(parent);
66 int bs = crypto_shash_blocksize(parent);
67 u8 *consts = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
68 int err = 0;
69 u8 key1[bs];
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100070
Herbert Xuac953012009-07-22 14:37:15 +080071 if ((err = crypto_cipher_setkey(ctx->child, inkey, keylen)))
72 return err;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100073
Herbert Xuac953012009-07-22 14:37:15 +080074 crypto_cipher_encrypt_one(ctx->child, consts, (u8 *)ks + bs);
75 crypto_cipher_encrypt_one(ctx->child, consts + bs, (u8 *)ks + bs * 2);
76 crypto_cipher_encrypt_one(ctx->child, key1, (u8 *)ks);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100077
Herbert Xuac953012009-07-22 14:37:15 +080078 return crypto_cipher_setkey(ctx->child, key1, bs);
79
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100080}
81
Herbert Xu3106caa2009-07-12 12:48:32 +080082static int crypto_xcbc_digest_init(struct shash_desc *pdesc)
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100083{
Herbert Xuac953012009-07-22 14:37:15 +080084 unsigned long alignmask = crypto_shash_alignmask(pdesc->tfm);
85 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
Herbert Xu3106caa2009-07-12 12:48:32 +080086 int bs = crypto_shash_blocksize(pdesc->tfm);
Herbert Xuac953012009-07-22 14:37:15 +080087 u8 *prev = PTR_ALIGN(&ctx->ctx[0], alignmask + 1) + bs;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100088
89 ctx->len = 0;
Herbert Xuac953012009-07-22 14:37:15 +080090 memset(prev, 0, bs);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100091
92 return 0;
93}
94
Herbert Xu3106caa2009-07-12 12:48:32 +080095static int crypto_xcbc_digest_update(struct shash_desc *pdesc, const u8 *p,
96 unsigned int len)
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +100097{
Herbert Xu3106caa2009-07-12 12:48:32 +080098 struct crypto_shash *parent = pdesc->tfm;
Herbert Xuac953012009-07-22 14:37:15 +080099 unsigned long alignmask = crypto_shash_alignmask(parent);
100 struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
101 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
102 struct crypto_cipher *tfm = tctx->child;
Herbert Xu3106caa2009-07-12 12:48:32 +0800103 int bs = crypto_shash_blocksize(parent);
Herbert Xuac953012009-07-22 14:37:15 +0800104 u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
105 u8 *prev = odds + bs;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000106
Herbert Xu3106caa2009-07-12 12:48:32 +0800107 /* checking the data can fill the block */
108 if ((ctx->len + len) <= bs) {
Herbert Xuac953012009-07-22 14:37:15 +0800109 memcpy(odds + ctx->len, p, len);
Herbert Xu3106caa2009-07-12 12:48:32 +0800110 ctx->len += len;
111 return 0;
112 }
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000113
Herbert Xu3106caa2009-07-12 12:48:32 +0800114 /* filling odds with new data and encrypting it */
Herbert Xuac953012009-07-22 14:37:15 +0800115 memcpy(odds + ctx->len, p, bs - ctx->len);
Herbert Xu3106caa2009-07-12 12:48:32 +0800116 len -= bs - ctx->len;
117 p += bs - ctx->len;
Joy Latten2f40a172008-03-06 19:28:44 +0800118
Herbert Xuac953012009-07-22 14:37:15 +0800119 crypto_xor(prev, odds, bs);
120 crypto_cipher_encrypt_one(tfm, prev, prev);
Joy Latten2f40a172008-03-06 19:28:44 +0800121
Herbert Xu3106caa2009-07-12 12:48:32 +0800122 /* clearing the length */
123 ctx->len = 0;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000124
Herbert Xu3106caa2009-07-12 12:48:32 +0800125 /* encrypting the rest of data */
126 while (len > bs) {
Herbert Xuac953012009-07-22 14:37:15 +0800127 crypto_xor(prev, p, bs);
128 crypto_cipher_encrypt_one(tfm, prev, prev);
Herbert Xu3106caa2009-07-12 12:48:32 +0800129 p += bs;
130 len -= bs;
131 }
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000132
Herbert Xu3106caa2009-07-12 12:48:32 +0800133 /* keeping the surplus of blocksize */
134 if (len) {
Herbert Xuac953012009-07-22 14:37:15 +0800135 memcpy(odds, p, len);
Herbert Xu3106caa2009-07-12 12:48:32 +0800136 ctx->len = len;
Joy Latten1edcf2e2008-04-02 14:36:09 +0800137 }
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000138
139 return 0;
140}
141
Herbert Xu3106caa2009-07-12 12:48:32 +0800142static int crypto_xcbc_digest_final(struct shash_desc *pdesc, u8 *out)
Herbert Xufb469842006-12-10 10:45:28 +1100143{
Herbert Xu3106caa2009-07-12 12:48:32 +0800144 struct crypto_shash *parent = pdesc->tfm;
Herbert Xuac953012009-07-22 14:37:15 +0800145 unsigned long alignmask = crypto_shash_alignmask(parent);
146 struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
147 struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
148 struct crypto_cipher *tfm = tctx->child;
Herbert Xu3106caa2009-07-12 12:48:32 +0800149 int bs = crypto_shash_blocksize(parent);
Herbert Xuac953012009-07-22 14:37:15 +0800150 u8 *consts = PTR_ALIGN(&tctx->ctx[0], alignmask + 1);
151 u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
152 u8 *prev = odds + bs;
153 unsigned int offset = 0;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000154
Herbert Xuac953012009-07-22 14:37:15 +0800155 if (ctx->len != bs) {
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000156 unsigned int rlen;
Herbert Xuac953012009-07-22 14:37:15 +0800157 u8 *p = odds + ctx->len;
158
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000159 *p = 0x80;
160 p++;
161
162 rlen = bs - ctx->len -1;
163 if (rlen)
164 memset(p, 0, rlen);
165
Herbert Xuac953012009-07-22 14:37:15 +0800166 offset += bs;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000167 }
168
Herbert Xuac953012009-07-22 14:37:15 +0800169 crypto_xor(prev, odds, bs);
170 crypto_xor(prev, consts + offset, bs);
171
172 crypto_cipher_encrypt_one(tfm, out, prev);
173
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000174 return 0;
175}
176
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000177static int xcbc_init_tfm(struct crypto_tfm *tfm)
178{
Herbert Xu2e306ee2006-12-17 10:05:58 +1100179 struct crypto_cipher *cipher;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000180 struct crypto_instance *inst = (void *)tfm->__crt_alg;
181 struct crypto_spawn *spawn = crypto_instance_ctx(inst);
Herbert Xuac953012009-07-22 14:37:15 +0800182 struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000183
Herbert Xu2e306ee2006-12-17 10:05:58 +1100184 cipher = crypto_spawn_cipher(spawn);
185 if (IS_ERR(cipher))
186 return PTR_ERR(cipher);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000187
Herbert Xu2e306ee2006-12-17 10:05:58 +1100188 ctx->child = cipher;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000189
190 return 0;
191};
192
193static void xcbc_exit_tfm(struct crypto_tfm *tfm)
194{
Herbert Xuac953012009-07-22 14:37:15 +0800195 struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000196 crypto_free_cipher(ctx->child);
197}
198
Herbert Xu3106caa2009-07-12 12:48:32 +0800199static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000200{
Herbert Xu3106caa2009-07-12 12:48:32 +0800201 struct shash_instance *inst;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000202 struct crypto_alg *alg;
Steffen Klassert36f87a42009-08-20 17:58:04 +1000203 unsigned long alignmask;
Herbert Xuebc610e2007-01-01 18:37:02 +1100204 int err;
205
Herbert Xu3106caa2009-07-12 12:48:32 +0800206 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
Herbert Xuebc610e2007-01-01 18:37:02 +1100207 if (err)
Herbert Xu3106caa2009-07-12 12:48:32 +0800208 return err;
Herbert Xuebc610e2007-01-01 18:37:02 +1100209
210 alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
211 CRYPTO_ALG_TYPE_MASK);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000212 if (IS_ERR(alg))
Herbert Xu3106caa2009-07-12 12:48:32 +0800213 return PTR_ERR(alg);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000214
215 switch(alg->cra_blocksize) {
216 case 16:
217 break;
218 default:
Herbert Xu1b878872008-01-01 15:44:50 +1100219 goto out_put_alg;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000220 }
221
Herbert Xu3106caa2009-07-12 12:48:32 +0800222 inst = shash_alloc_instance("xcbc", alg);
Herbert Xub5ebd442009-07-15 16:53:33 +0800223 err = PTR_ERR(inst);
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000224 if (IS_ERR(inst))
225 goto out_put_alg;
226
Herbert Xu3106caa2009-07-12 12:48:32 +0800227 err = crypto_init_spawn(shash_instance_ctx(inst), alg,
228 shash_crypto_instance(inst),
229 CRYPTO_ALG_TYPE_MASK);
230 if (err)
231 goto out_free_inst;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000232
Steffen Klassert36f87a42009-08-20 17:58:04 +1000233 alignmask = alg->cra_alignmask | 3;
234 inst->alg.base.cra_alignmask = alignmask;
Herbert Xu3106caa2009-07-12 12:48:32 +0800235 inst->alg.base.cra_priority = alg->cra_priority;
236 inst->alg.base.cra_blocksize = alg->cra_blocksize;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000237
Herbert Xu3106caa2009-07-12 12:48:32 +0800238 inst->alg.digestsize = alg->cra_blocksize;
Herbert Xuac953012009-07-22 14:37:15 +0800239 inst->alg.descsize = ALIGN(sizeof(struct xcbc_desc_ctx),
240 crypto_tfm_ctx_alignment()) +
Steffen Klassert36f87a42009-08-20 17:58:04 +1000241 (alignmask &
Herbert Xuac953012009-07-22 14:37:15 +0800242 ~(crypto_tfm_ctx_alignment() - 1)) +
243 alg->cra_blocksize * 2;
244
245 inst->alg.base.cra_ctxsize = ALIGN(sizeof(struct xcbc_tfm_ctx),
Steffen Klassert36f87a42009-08-20 17:58:04 +1000246 alignmask + 1) +
Herbert Xuac953012009-07-22 14:37:15 +0800247 alg->cra_blocksize * 2;
Herbert Xu3106caa2009-07-12 12:48:32 +0800248 inst->alg.base.cra_init = xcbc_init_tfm;
249 inst->alg.base.cra_exit = xcbc_exit_tfm;
250
251 inst->alg.init = crypto_xcbc_digest_init;
252 inst->alg.update = crypto_xcbc_digest_update;
253 inst->alg.final = crypto_xcbc_digest_final;
254 inst->alg.setkey = crypto_xcbc_digest_setkey;
255
256 err = shash_register_instance(tmpl, inst);
257 if (err) {
258out_free_inst:
259 shash_free_instance(shash_crypto_instance(inst));
260 }
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000261
262out_put_alg:
263 crypto_mod_put(alg);
Herbert Xu3106caa2009-07-12 12:48:32 +0800264 return err;
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000265}
266
267static struct crypto_template crypto_xcbc_tmpl = {
268 .name = "xcbc",
Herbert Xu3106caa2009-07-12 12:48:32 +0800269 .create = xcbc_create,
270 .free = shash_free_instance,
Kazunori MIYAZAWA333b0d72006-10-28 13:15:24 +1000271 .module = THIS_MODULE,
272};
273
274static int __init crypto_xcbc_module_init(void)
275{
276 return crypto_register_template(&crypto_xcbc_tmpl);
277}
278
279static void __exit crypto_xcbc_module_exit(void)
280{
281 crypto_unregister_template(&crypto_xcbc_tmpl);
282}
283
284module_init(crypto_xcbc_module_init);
285module_exit(crypto_xcbc_module_exit);
286
287MODULE_LICENSE("GPL");
288MODULE_DESCRIPTION("XCBC keyed hash algorithm");
Kees Cook4943ba12014-11-24 16:32:38 -0800289MODULE_ALIAS_CRYPTO("xcbc");