xcbc.c 7.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright (C)2006 USAGI/WIDE Project
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Author:
 * 	Kazunori Miyazawa <miyazawa@linux-ipv6.org>
 */

Herbert Xu's avatar
Herbert Xu committed
22
#include <crypto/internal/hash.h>
23 24
#include <linux/err.h>
#include <linux/kernel.h>
25
#include <linux/module.h>
26

27 28 29
static u_int32_t ks[12] = {0x01010101, 0x01010101, 0x01010101, 0x01010101,
			   0x02020202, 0x02020202, 0x02020202, 0x02020202,
			   0x03030303, 0x03030303, 0x03030303, 0x03030303};
30

31 32 33 34
/*
 * +------------------------
 * | <parent tfm>
 * +------------------------
35
 * | xcbc_tfm_ctx
36
 * +------------------------
37
 * | consts (block size * 2)
38
 * +------------------------
39 40 41 42 43 44 45
 */
struct xcbc_tfm_ctx {
	struct crypto_cipher *child;
	u8 ctx[];
};

/*
46
 * +------------------------
47
 * | <shash desc>
48
 * +------------------------
49 50 51 52 53
 * | xcbc_desc_ctx
 * +------------------------
 * | odds (block size)
 * +------------------------
 * | prev (block size)
54 55
 * +------------------------
 */
56
struct xcbc_desc_ctx {
57
	unsigned int len;
58
	u8 ctx[];
59 60
};

61 62
static int crypto_xcbc_digest_setkey(struct crypto_shash *parent,
				     const u8 *inkey, unsigned int keylen)
63
{
64 65
	unsigned long alignmask = crypto_shash_alignmask(parent);
	struct xcbc_tfm_ctx *ctx = crypto_shash_ctx(parent);
Herbert Xu's avatar
Herbert Xu committed
66
	int bs = crypto_shash_blocksize(parent);
67
	u8 *consts = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
68 69 70
	int err = 0;
	u8 key1[bs];

71 72
	if ((err = crypto_cipher_setkey(ctx->child, inkey, keylen)))
		return err;
73

74 75 76
	crypto_cipher_encrypt_one(ctx->child, consts, (u8 *)ks + bs);
	crypto_cipher_encrypt_one(ctx->child, consts + bs, (u8 *)ks + bs * 2);
	crypto_cipher_encrypt_one(ctx->child, key1, (u8 *)ks);
77 78 79 80 81

	return crypto_cipher_setkey(ctx->child, key1, bs);

}

Herbert Xu's avatar
Herbert Xu committed
82
static int crypto_xcbc_digest_init(struct shash_desc *pdesc)
83
{
84 85
	unsigned long alignmask = crypto_shash_alignmask(pdesc->tfm);
	struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
Herbert Xu's avatar
Herbert Xu committed
86
	int bs = crypto_shash_blocksize(pdesc->tfm);
87
	u8 *prev = PTR_ALIGN(&ctx->ctx[0], alignmask + 1) + bs;
88 89

	ctx->len = 0;
90
	memset(prev, 0, bs);
91 92 93 94

	return 0;
}

Herbert Xu's avatar
Herbert Xu committed
95 96
static int crypto_xcbc_digest_update(struct shash_desc *pdesc, const u8 *p,
				     unsigned int len)
97
{
Herbert Xu's avatar
Herbert Xu committed
98
	struct crypto_shash *parent = pdesc->tfm;
99 100 101 102
	unsigned long alignmask = crypto_shash_alignmask(parent);
	struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
	struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
	struct crypto_cipher *tfm = tctx->child;
Herbert Xu's avatar
Herbert Xu committed
103
	int bs = crypto_shash_blocksize(parent);
104 105
	u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
	u8 *prev = odds + bs;
Herbert Xu's avatar
Herbert Xu committed
106 107 108

	/* checking the data can fill the block */
	if ((ctx->len + len) <= bs) {
109
		memcpy(odds + ctx->len, p, len);
Herbert Xu's avatar
Herbert Xu committed
110 111
		ctx->len += len;
		return 0;
112
	}
113

Herbert Xu's avatar
Herbert Xu committed
114
	/* filling odds with new data and encrypting it */
115
	memcpy(odds + ctx->len, p, bs - ctx->len);
Herbert Xu's avatar
Herbert Xu committed
116 117
	len -= bs - ctx->len;
	p += bs - ctx->len;
118

119 120
	crypto_xor(prev, odds, bs);
	crypto_cipher_encrypt_one(tfm, prev, prev);
Herbert Xu's avatar
Herbert Xu committed
121 122 123 124 125 126

	/* clearing the length */
	ctx->len = 0;

	/* encrypting the rest of data */
	while (len > bs) {
127 128
		crypto_xor(prev, p, bs);
		crypto_cipher_encrypt_one(tfm, prev, prev);
Herbert Xu's avatar
Herbert Xu committed
129 130 131 132 133 134
		p += bs;
		len -= bs;
	}

	/* keeping the surplus of blocksize */
	if (len) {
135
		memcpy(odds, p, len);
Herbert Xu's avatar
Herbert Xu committed
136 137 138 139
		ctx->len = len;
	}

	return 0;
140 141
}

Herbert Xu's avatar
Herbert Xu committed
142
static int crypto_xcbc_digest_final(struct shash_desc *pdesc, u8 *out)
143
{
Herbert Xu's avatar
Herbert Xu committed
144
	struct crypto_shash *parent = pdesc->tfm;
145 146 147 148
	unsigned long alignmask = crypto_shash_alignmask(parent);
	struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
	struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
	struct crypto_cipher *tfm = tctx->child;
Herbert Xu's avatar
Herbert Xu committed
149
	int bs = crypto_shash_blocksize(parent);
150 151 152 153
	u8 *consts = PTR_ALIGN(&tctx->ctx[0], alignmask + 1);
	u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
	u8 *prev = odds + bs;
	unsigned int offset = 0;
154

155
	if (ctx->len != bs) {
156
		unsigned int rlen;
157 158
		u8 *p = odds + ctx->len;

159 160 161 162 163 164 165
		*p = 0x80;
		p++;

		rlen = bs - ctx->len -1;
		if (rlen)
			memset(p, 0, rlen);

166 167
		offset += bs;
	}
168

169 170
	crypto_xor(prev, odds, bs);
	crypto_xor(prev, consts + offset, bs);
171

172
	crypto_cipher_encrypt_one(tfm, out, prev);
173 174 175 176 177 178

	return 0;
}

static int xcbc_init_tfm(struct crypto_tfm *tfm)
{
179
	struct crypto_cipher *cipher;
180 181
	struct crypto_instance *inst = (void *)tfm->__crt_alg;
	struct crypto_spawn *spawn = crypto_instance_ctx(inst);
182
	struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
183

184 185 186
	cipher = crypto_spawn_cipher(spawn);
	if (IS_ERR(cipher))
		return PTR_ERR(cipher);
187

188
	ctx->child = cipher;
189 190 191 192 193 194

	return 0;
};

static void xcbc_exit_tfm(struct crypto_tfm *tfm)
{
195
	struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
196 197 198
	crypto_free_cipher(ctx->child);
}

Herbert Xu's avatar
Herbert Xu committed
199
static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
200
{
Herbert Xu's avatar
Herbert Xu committed
201
	struct shash_instance *inst;
202
	struct crypto_alg *alg;
203
	unsigned long alignmask;
204 205
	int err;

Herbert Xu's avatar
Herbert Xu committed
206
	err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
207
	if (err)
Herbert Xu's avatar
Herbert Xu committed
208
		return err;
209 210 211

	alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
				  CRYPTO_ALG_TYPE_MASK);
212
	if (IS_ERR(alg))
Herbert Xu's avatar
Herbert Xu committed
213
		return PTR_ERR(alg);
214 215 216 217 218

	switch(alg->cra_blocksize) {
	case 16:
		break;
	default:
219
		goto out_put_alg;
220 221
	}

Herbert Xu's avatar
Herbert Xu committed
222
	inst = shash_alloc_instance("xcbc", alg);
223
	err = PTR_ERR(inst);
224 225 226
	if (IS_ERR(inst))
		goto out_put_alg;

Herbert Xu's avatar
Herbert Xu committed
227 228 229 230 231 232
	err = crypto_init_spawn(shash_instance_ctx(inst), alg,
				shash_crypto_instance(inst),
				CRYPTO_ALG_TYPE_MASK);
	if (err)
		goto out_free_inst;

233 234
	alignmask = alg->cra_alignmask | 3;
	inst->alg.base.cra_alignmask = alignmask;
Herbert Xu's avatar
Herbert Xu committed
235 236 237 238
	inst->alg.base.cra_priority = alg->cra_priority;
	inst->alg.base.cra_blocksize = alg->cra_blocksize;

	inst->alg.digestsize = alg->cra_blocksize;
239 240
	inst->alg.descsize = ALIGN(sizeof(struct xcbc_desc_ctx),
				   crypto_tfm_ctx_alignment()) +
241
			     (alignmask &
242 243 244 245
			      ~(crypto_tfm_ctx_alignment() - 1)) +
			     alg->cra_blocksize * 2;

	inst->alg.base.cra_ctxsize = ALIGN(sizeof(struct xcbc_tfm_ctx),
246
					   alignmask + 1) +
247
				     alg->cra_blocksize * 2;
Herbert Xu's avatar
Herbert Xu committed
248 249 250 251 252 253 254 255 256 257 258 259 260
	inst->alg.base.cra_init = xcbc_init_tfm;
	inst->alg.base.cra_exit = xcbc_exit_tfm;

	inst->alg.init = crypto_xcbc_digest_init;
	inst->alg.update = crypto_xcbc_digest_update;
	inst->alg.final = crypto_xcbc_digest_final;
	inst->alg.setkey = crypto_xcbc_digest_setkey;

	err = shash_register_instance(tmpl, inst);
	if (err) {
out_free_inst:
		shash_free_instance(shash_crypto_instance(inst));
	}
261 262 263

out_put_alg:
	crypto_mod_put(alg);
Herbert Xu's avatar
Herbert Xu committed
264
	return err;
265 266 267 268
}

static struct crypto_template crypto_xcbc_tmpl = {
	.name = "xcbc",
Herbert Xu's avatar
Herbert Xu committed
269 270
	.create = xcbc_create,
	.free = shash_free_instance,
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
	.module = THIS_MODULE,
};

static int __init crypto_xcbc_module_init(void)
{
	return crypto_register_template(&crypto_xcbc_tmpl);
}

static void __exit crypto_xcbc_module_exit(void)
{
	crypto_unregister_template(&crypto_xcbc_tmpl);
}

module_init(crypto_xcbc_module_init);
module_exit(crypto_xcbc_module_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("XCBC keyed hash algorithm");
289
MODULE_ALIAS_CRYPTO("xcbc");