xcbc.c 6.64 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2 3 4 5 6 7 8
/*
 * Copyright (C)2006 USAGI/WIDE Project
 *
 * Author:
 * 	Kazunori Miyazawa <miyazawa@linux-ipv6.org>
 */

Herbert Xu's avatar
Herbert Xu committed
9
#include <crypto/internal/hash.h>
10 11
#include <linux/err.h>
#include <linux/kernel.h>
12
#include <linux/module.h>
13

14 15 16
static u_int32_t ks[12] = {0x01010101, 0x01010101, 0x01010101, 0x01010101,
			   0x02020202, 0x02020202, 0x02020202, 0x02020202,
			   0x03030303, 0x03030303, 0x03030303, 0x03030303};
17

18 19 20 21
/*
 * +------------------------
 * | <parent tfm>
 * +------------------------
22
 * | xcbc_tfm_ctx
23
 * +------------------------
24
 * | consts (block size * 2)
25
 * +------------------------
26 27 28 29 30 31 32
 */
struct xcbc_tfm_ctx {
	struct crypto_cipher *child;
	u8 ctx[];
};

/*
33
 * +------------------------
34
 * | <shash desc>
35
 * +------------------------
36 37 38 39 40
 * | xcbc_desc_ctx
 * +------------------------
 * | odds (block size)
 * +------------------------
 * | prev (block size)
41 42
 * +------------------------
 */
43
struct xcbc_desc_ctx {
44
	unsigned int len;
45
	u8 ctx[];
46 47
};

48 49
#define XCBC_BLOCKSIZE	16

50 51
static int crypto_xcbc_digest_setkey(struct crypto_shash *parent,
				     const u8 *inkey, unsigned int keylen)
52
{
53 54 55
	unsigned long alignmask = crypto_shash_alignmask(parent);
	struct xcbc_tfm_ctx *ctx = crypto_shash_ctx(parent);
	u8 *consts = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
56
	int err = 0;
57 58
	u8 key1[XCBC_BLOCKSIZE];
	int bs = sizeof(key1);
59

60 61
	if ((err = crypto_cipher_setkey(ctx->child, inkey, keylen)))
		return err;
62

63 64 65
	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);
66 67 68 69 70

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

}

Herbert Xu's avatar
Herbert Xu committed
71
static int crypto_xcbc_digest_init(struct shash_desc *pdesc)
72
{
73 74
	unsigned long alignmask = crypto_shash_alignmask(pdesc->tfm);
	struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
Herbert Xu's avatar
Herbert Xu committed
75
	int bs = crypto_shash_blocksize(pdesc->tfm);
76
	u8 *prev = PTR_ALIGN(&ctx->ctx[0], alignmask + 1) + bs;
77 78

	ctx->len = 0;
79
	memset(prev, 0, bs);
80 81 82 83

	return 0;
}

Herbert Xu's avatar
Herbert Xu committed
84 85
static int crypto_xcbc_digest_update(struct shash_desc *pdesc, const u8 *p,
				     unsigned int len)
86
{
Herbert Xu's avatar
Herbert Xu committed
87
	struct crypto_shash *parent = pdesc->tfm;
88 89 90 91
	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
92
	int bs = crypto_shash_blocksize(parent);
93 94
	u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
	u8 *prev = odds + bs;
Herbert Xu's avatar
Herbert Xu committed
95 96 97

	/* checking the data can fill the block */
	if ((ctx->len + len) <= bs) {
98
		memcpy(odds + ctx->len, p, len);
Herbert Xu's avatar
Herbert Xu committed
99 100
		ctx->len += len;
		return 0;
101
	}
102

Herbert Xu's avatar
Herbert Xu committed
103
	/* filling odds with new data and encrypting it */
104
	memcpy(odds + ctx->len, p, bs - ctx->len);
Herbert Xu's avatar
Herbert Xu committed
105 106
	len -= bs - ctx->len;
	p += bs - ctx->len;
107

108 109
	crypto_xor(prev, odds, bs);
	crypto_cipher_encrypt_one(tfm, prev, prev);
Herbert Xu's avatar
Herbert Xu committed
110 111 112 113 114 115

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

	/* encrypting the rest of data */
	while (len > bs) {
116 117
		crypto_xor(prev, p, bs);
		crypto_cipher_encrypt_one(tfm, prev, prev);
Herbert Xu's avatar
Herbert Xu committed
118 119 120 121 122 123
		p += bs;
		len -= bs;
	}

	/* keeping the surplus of blocksize */
	if (len) {
124
		memcpy(odds, p, len);
Herbert Xu's avatar
Herbert Xu committed
125 126 127 128
		ctx->len = len;
	}

	return 0;
129 130
}

Herbert Xu's avatar
Herbert Xu committed
131
static int crypto_xcbc_digest_final(struct shash_desc *pdesc, u8 *out)
132
{
Herbert Xu's avatar
Herbert Xu committed
133
	struct crypto_shash *parent = pdesc->tfm;
134 135 136 137
	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
138
	int bs = crypto_shash_blocksize(parent);
139 140 141 142
	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;
143

144
	if (ctx->len != bs) {
145
		unsigned int rlen;
146 147
		u8 *p = odds + ctx->len;

148 149 150 151 152 153 154
		*p = 0x80;
		p++;

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

155 156
		offset += bs;
	}
157

158 159
	crypto_xor(prev, odds, bs);
	crypto_xor(prev, consts + offset, bs);
160

161
	crypto_cipher_encrypt_one(tfm, out, prev);
162 163 164 165 166 167

	return 0;
}

static int xcbc_init_tfm(struct crypto_tfm *tfm)
{
168
	struct crypto_cipher *cipher;
169
	struct crypto_instance *inst = (void *)tfm->__crt_alg;
170
	struct crypto_cipher_spawn *spawn = crypto_instance_ctx(inst);
171
	struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
172

173 174 175
	cipher = crypto_spawn_cipher(spawn);
	if (IS_ERR(cipher))
		return PTR_ERR(cipher);
176

177
	ctx->child = cipher;
178 179 180 181 182 183

	return 0;
};

static void xcbc_exit_tfm(struct crypto_tfm *tfm)
{
184
	struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
185 186 187
	crypto_free_cipher(ctx->child);
}

Herbert Xu's avatar
Herbert Xu committed
188
static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
189
{
Herbert Xu's avatar
Herbert Xu committed
190
	struct shash_instance *inst;
191
	struct crypto_cipher_spawn *spawn;
192
	struct crypto_alg *alg;
193
	unsigned long alignmask;
194 195
	int err;

Herbert Xu's avatar
Herbert Xu committed
196
	err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
197
	if (err)
Herbert Xu's avatar
Herbert Xu committed
198
		return err;
199

200 201 202 203
	inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
	if (!inst)
		return -ENOMEM;
	spawn = shash_instance_ctx(inst);
204

205 206 207 208 209
	err = crypto_grab_cipher(spawn, shash_crypto_instance(inst),
				 crypto_attr_alg_name(tb[1]), 0, 0);
	if (err)
		goto err_free_inst;
	alg = crypto_spawn_cipher_alg(spawn);
210

211 212 213
	err = -EINVAL;
	if (alg->cra_blocksize != XCBC_BLOCKSIZE)
		goto err_free_inst;
214

215
	err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg);
Herbert Xu's avatar
Herbert Xu committed
216
	if (err)
217
		goto err_free_inst;
Herbert Xu's avatar
Herbert Xu committed
218

219 220
	alignmask = alg->cra_alignmask | 3;
	inst->alg.base.cra_alignmask = alignmask;
Herbert Xu's avatar
Herbert Xu committed
221 222 223 224
	inst->alg.base.cra_priority = alg->cra_priority;
	inst->alg.base.cra_blocksize = alg->cra_blocksize;

	inst->alg.digestsize = alg->cra_blocksize;
225 226
	inst->alg.descsize = ALIGN(sizeof(struct xcbc_desc_ctx),
				   crypto_tfm_ctx_alignment()) +
227
			     (alignmask &
228 229 230 231
			      ~(crypto_tfm_ctx_alignment() - 1)) +
			     alg->cra_blocksize * 2;

	inst->alg.base.cra_ctxsize = ALIGN(sizeof(struct xcbc_tfm_ctx),
232
					   alignmask + 1) +
233
				     alg->cra_blocksize * 2;
Herbert Xu's avatar
Herbert Xu committed
234 235 236 237 238 239 240 241
	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;

242 243
	inst->free = shash_free_singlespawn_instance;

Herbert Xu's avatar
Herbert Xu committed
244 245
	err = shash_register_instance(tmpl, inst);
	if (err) {
246
err_free_inst:
247
		shash_free_singlespawn_instance(inst);
Herbert Xu's avatar
Herbert Xu committed
248 249
	}
	return err;
250 251 252 253
}

static struct crypto_template crypto_xcbc_tmpl = {
	.name = "xcbc",
Herbert Xu's avatar
Herbert Xu committed
254
	.create = xcbc_create,
255 256 257 258 259 260 261 262 263 264 265 266 267
	.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);
}

268
subsys_initcall(crypto_xcbc_module_init);
269 270 271 272
module_exit(crypto_xcbc_module_exit);

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