xref: /OK3568_Linux_fs/kernel/crypto/xcbc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C)2006 USAGI/WIDE Project
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Author:
6*4882a593Smuzhiyun  * 	Kazunori Miyazawa <miyazawa@linux-ipv6.org>
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <crypto/internal/cipher.h>
10*4882a593Smuzhiyun #include <crypto/internal/hash.h>
11*4882a593Smuzhiyun #include <linux/err.h>
12*4882a593Smuzhiyun #include <linux/kernel.h>
13*4882a593Smuzhiyun #include <linux/module.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun static u_int32_t ks[12] = {0x01010101, 0x01010101, 0x01010101, 0x01010101,
16*4882a593Smuzhiyun 			   0x02020202, 0x02020202, 0x02020202, 0x02020202,
17*4882a593Smuzhiyun 			   0x03030303, 0x03030303, 0x03030303, 0x03030303};
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun /*
20*4882a593Smuzhiyun  * +------------------------
21*4882a593Smuzhiyun  * | <parent tfm>
22*4882a593Smuzhiyun  * +------------------------
23*4882a593Smuzhiyun  * | xcbc_tfm_ctx
24*4882a593Smuzhiyun  * +------------------------
25*4882a593Smuzhiyun  * | consts (block size * 2)
26*4882a593Smuzhiyun  * +------------------------
27*4882a593Smuzhiyun  */
28*4882a593Smuzhiyun struct xcbc_tfm_ctx {
29*4882a593Smuzhiyun 	struct crypto_cipher *child;
30*4882a593Smuzhiyun 	u8 ctx[];
31*4882a593Smuzhiyun };
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun /*
34*4882a593Smuzhiyun  * +------------------------
35*4882a593Smuzhiyun  * | <shash desc>
36*4882a593Smuzhiyun  * +------------------------
37*4882a593Smuzhiyun  * | xcbc_desc_ctx
38*4882a593Smuzhiyun  * +------------------------
39*4882a593Smuzhiyun  * | odds (block size)
40*4882a593Smuzhiyun  * +------------------------
41*4882a593Smuzhiyun  * | prev (block size)
42*4882a593Smuzhiyun  * +------------------------
43*4882a593Smuzhiyun  */
44*4882a593Smuzhiyun struct xcbc_desc_ctx {
45*4882a593Smuzhiyun 	unsigned int len;
46*4882a593Smuzhiyun 	u8 ctx[];
47*4882a593Smuzhiyun };
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun #define XCBC_BLOCKSIZE	16
50*4882a593Smuzhiyun 
crypto_xcbc_digest_setkey(struct crypto_shash * parent,const u8 * inkey,unsigned int keylen)51*4882a593Smuzhiyun static int crypto_xcbc_digest_setkey(struct crypto_shash *parent,
52*4882a593Smuzhiyun 				     const u8 *inkey, unsigned int keylen)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun 	unsigned long alignmask = crypto_shash_alignmask(parent);
55*4882a593Smuzhiyun 	struct xcbc_tfm_ctx *ctx = crypto_shash_ctx(parent);
56*4882a593Smuzhiyun 	u8 *consts = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
57*4882a593Smuzhiyun 	int err = 0;
58*4882a593Smuzhiyun 	u8 key1[XCBC_BLOCKSIZE];
59*4882a593Smuzhiyun 	int bs = sizeof(key1);
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	if ((err = crypto_cipher_setkey(ctx->child, inkey, keylen)))
62*4882a593Smuzhiyun 		return err;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	crypto_cipher_encrypt_one(ctx->child, consts, (u8 *)ks + bs);
65*4882a593Smuzhiyun 	crypto_cipher_encrypt_one(ctx->child, consts + bs, (u8 *)ks + bs * 2);
66*4882a593Smuzhiyun 	crypto_cipher_encrypt_one(ctx->child, key1, (u8 *)ks);
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	return crypto_cipher_setkey(ctx->child, key1, bs);
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun 
crypto_xcbc_digest_init(struct shash_desc * pdesc)72*4882a593Smuzhiyun static int crypto_xcbc_digest_init(struct shash_desc *pdesc)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun 	unsigned long alignmask = crypto_shash_alignmask(pdesc->tfm);
75*4882a593Smuzhiyun 	struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
76*4882a593Smuzhiyun 	int bs = crypto_shash_blocksize(pdesc->tfm);
77*4882a593Smuzhiyun 	u8 *prev = PTR_ALIGN(&ctx->ctx[0], alignmask + 1) + bs;
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	ctx->len = 0;
80*4882a593Smuzhiyun 	memset(prev, 0, bs);
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	return 0;
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun 
crypto_xcbc_digest_update(struct shash_desc * pdesc,const u8 * p,unsigned int len)85*4882a593Smuzhiyun static int crypto_xcbc_digest_update(struct shash_desc *pdesc, const u8 *p,
86*4882a593Smuzhiyun 				     unsigned int len)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun 	struct crypto_shash *parent = pdesc->tfm;
89*4882a593Smuzhiyun 	unsigned long alignmask = crypto_shash_alignmask(parent);
90*4882a593Smuzhiyun 	struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
91*4882a593Smuzhiyun 	struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
92*4882a593Smuzhiyun 	struct crypto_cipher *tfm = tctx->child;
93*4882a593Smuzhiyun 	int bs = crypto_shash_blocksize(parent);
94*4882a593Smuzhiyun 	u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
95*4882a593Smuzhiyun 	u8 *prev = odds + bs;
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	/* checking the data can fill the block */
98*4882a593Smuzhiyun 	if ((ctx->len + len) <= bs) {
99*4882a593Smuzhiyun 		memcpy(odds + ctx->len, p, len);
100*4882a593Smuzhiyun 		ctx->len += len;
101*4882a593Smuzhiyun 		return 0;
102*4882a593Smuzhiyun 	}
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	/* filling odds with new data and encrypting it */
105*4882a593Smuzhiyun 	memcpy(odds + ctx->len, p, bs - ctx->len);
106*4882a593Smuzhiyun 	len -= bs - ctx->len;
107*4882a593Smuzhiyun 	p += bs - ctx->len;
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	crypto_xor(prev, odds, bs);
110*4882a593Smuzhiyun 	crypto_cipher_encrypt_one(tfm, prev, prev);
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	/* clearing the length */
113*4882a593Smuzhiyun 	ctx->len = 0;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	/* encrypting the rest of data */
116*4882a593Smuzhiyun 	while (len > bs) {
117*4882a593Smuzhiyun 		crypto_xor(prev, p, bs);
118*4882a593Smuzhiyun 		crypto_cipher_encrypt_one(tfm, prev, prev);
119*4882a593Smuzhiyun 		p += bs;
120*4882a593Smuzhiyun 		len -= bs;
121*4882a593Smuzhiyun 	}
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	/* keeping the surplus of blocksize */
124*4882a593Smuzhiyun 	if (len) {
125*4882a593Smuzhiyun 		memcpy(odds, p, len);
126*4882a593Smuzhiyun 		ctx->len = len;
127*4882a593Smuzhiyun 	}
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	return 0;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun 
crypto_xcbc_digest_final(struct shash_desc * pdesc,u8 * out)132*4882a593Smuzhiyun static int crypto_xcbc_digest_final(struct shash_desc *pdesc, u8 *out)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun 	struct crypto_shash *parent = pdesc->tfm;
135*4882a593Smuzhiyun 	unsigned long alignmask = crypto_shash_alignmask(parent);
136*4882a593Smuzhiyun 	struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
137*4882a593Smuzhiyun 	struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
138*4882a593Smuzhiyun 	struct crypto_cipher *tfm = tctx->child;
139*4882a593Smuzhiyun 	int bs = crypto_shash_blocksize(parent);
140*4882a593Smuzhiyun 	u8 *consts = PTR_ALIGN(&tctx->ctx[0], alignmask + 1);
141*4882a593Smuzhiyun 	u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
142*4882a593Smuzhiyun 	u8 *prev = odds + bs;
143*4882a593Smuzhiyun 	unsigned int offset = 0;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	if (ctx->len != bs) {
146*4882a593Smuzhiyun 		unsigned int rlen;
147*4882a593Smuzhiyun 		u8 *p = odds + ctx->len;
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 		*p = 0x80;
150*4882a593Smuzhiyun 		p++;
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 		rlen = bs - ctx->len -1;
153*4882a593Smuzhiyun 		if (rlen)
154*4882a593Smuzhiyun 			memset(p, 0, rlen);
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 		offset += bs;
157*4882a593Smuzhiyun 	}
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	crypto_xor(prev, odds, bs);
160*4882a593Smuzhiyun 	crypto_xor(prev, consts + offset, bs);
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	crypto_cipher_encrypt_one(tfm, out, prev);
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	return 0;
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun 
xcbc_init_tfm(struct crypto_tfm * tfm)167*4882a593Smuzhiyun static int xcbc_init_tfm(struct crypto_tfm *tfm)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun 	struct crypto_cipher *cipher;
170*4882a593Smuzhiyun 	struct crypto_instance *inst = (void *)tfm->__crt_alg;
171*4882a593Smuzhiyun 	struct crypto_cipher_spawn *spawn = crypto_instance_ctx(inst);
172*4882a593Smuzhiyun 	struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	cipher = crypto_spawn_cipher(spawn);
175*4882a593Smuzhiyun 	if (IS_ERR(cipher))
176*4882a593Smuzhiyun 		return PTR_ERR(cipher);
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	ctx->child = cipher;
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 	return 0;
181*4882a593Smuzhiyun };
182*4882a593Smuzhiyun 
xcbc_exit_tfm(struct crypto_tfm * tfm)183*4882a593Smuzhiyun static void xcbc_exit_tfm(struct crypto_tfm *tfm)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun 	struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
186*4882a593Smuzhiyun 	crypto_free_cipher(ctx->child);
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun 
xcbc_create(struct crypto_template * tmpl,struct rtattr ** tb)189*4882a593Smuzhiyun static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun 	struct shash_instance *inst;
192*4882a593Smuzhiyun 	struct crypto_cipher_spawn *spawn;
193*4882a593Smuzhiyun 	struct crypto_alg *alg;
194*4882a593Smuzhiyun 	unsigned long alignmask;
195*4882a593Smuzhiyun 	u32 mask;
196*4882a593Smuzhiyun 	int err;
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask);
199*4882a593Smuzhiyun 	if (err)
200*4882a593Smuzhiyun 		return err;
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
203*4882a593Smuzhiyun 	if (!inst)
204*4882a593Smuzhiyun 		return -ENOMEM;
205*4882a593Smuzhiyun 	spawn = shash_instance_ctx(inst);
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	err = crypto_grab_cipher(spawn, shash_crypto_instance(inst),
208*4882a593Smuzhiyun 				 crypto_attr_alg_name(tb[1]), 0, mask);
209*4882a593Smuzhiyun 	if (err)
210*4882a593Smuzhiyun 		goto err_free_inst;
211*4882a593Smuzhiyun 	alg = crypto_spawn_cipher_alg(spawn);
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	err = -EINVAL;
214*4882a593Smuzhiyun 	if (alg->cra_blocksize != XCBC_BLOCKSIZE)
215*4882a593Smuzhiyun 		goto err_free_inst;
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg);
218*4882a593Smuzhiyun 	if (err)
219*4882a593Smuzhiyun 		goto err_free_inst;
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	alignmask = alg->cra_alignmask | 3;
222*4882a593Smuzhiyun 	inst->alg.base.cra_alignmask = alignmask;
223*4882a593Smuzhiyun 	inst->alg.base.cra_priority = alg->cra_priority;
224*4882a593Smuzhiyun 	inst->alg.base.cra_blocksize = alg->cra_blocksize;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	inst->alg.digestsize = alg->cra_blocksize;
227*4882a593Smuzhiyun 	inst->alg.descsize = ALIGN(sizeof(struct xcbc_desc_ctx),
228*4882a593Smuzhiyun 				   crypto_tfm_ctx_alignment()) +
229*4882a593Smuzhiyun 			     (alignmask &
230*4882a593Smuzhiyun 			      ~(crypto_tfm_ctx_alignment() - 1)) +
231*4882a593Smuzhiyun 			     alg->cra_blocksize * 2;
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	inst->alg.base.cra_ctxsize = ALIGN(sizeof(struct xcbc_tfm_ctx),
234*4882a593Smuzhiyun 					   alignmask + 1) +
235*4882a593Smuzhiyun 				     alg->cra_blocksize * 2;
236*4882a593Smuzhiyun 	inst->alg.base.cra_init = xcbc_init_tfm;
237*4882a593Smuzhiyun 	inst->alg.base.cra_exit = xcbc_exit_tfm;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	inst->alg.init = crypto_xcbc_digest_init;
240*4882a593Smuzhiyun 	inst->alg.update = crypto_xcbc_digest_update;
241*4882a593Smuzhiyun 	inst->alg.final = crypto_xcbc_digest_final;
242*4882a593Smuzhiyun 	inst->alg.setkey = crypto_xcbc_digest_setkey;
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	inst->free = shash_free_singlespawn_instance;
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	err = shash_register_instance(tmpl, inst);
247*4882a593Smuzhiyun 	if (err) {
248*4882a593Smuzhiyun err_free_inst:
249*4882a593Smuzhiyun 		shash_free_singlespawn_instance(inst);
250*4882a593Smuzhiyun 	}
251*4882a593Smuzhiyun 	return err;
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun static struct crypto_template crypto_xcbc_tmpl = {
255*4882a593Smuzhiyun 	.name = "xcbc",
256*4882a593Smuzhiyun 	.create = xcbc_create,
257*4882a593Smuzhiyun 	.module = THIS_MODULE,
258*4882a593Smuzhiyun };
259*4882a593Smuzhiyun 
crypto_xcbc_module_init(void)260*4882a593Smuzhiyun static int __init crypto_xcbc_module_init(void)
261*4882a593Smuzhiyun {
262*4882a593Smuzhiyun 	return crypto_register_template(&crypto_xcbc_tmpl);
263*4882a593Smuzhiyun }
264*4882a593Smuzhiyun 
crypto_xcbc_module_exit(void)265*4882a593Smuzhiyun static void __exit crypto_xcbc_module_exit(void)
266*4882a593Smuzhiyun {
267*4882a593Smuzhiyun 	crypto_unregister_template(&crypto_xcbc_tmpl);
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun subsys_initcall(crypto_xcbc_module_init);
271*4882a593Smuzhiyun module_exit(crypto_xcbc_module_exit);
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun MODULE_LICENSE("GPL");
274*4882a593Smuzhiyun MODULE_DESCRIPTION("XCBC keyed hash algorithm");
275*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("xcbc");
276*4882a593Smuzhiyun MODULE_IMPORT_NS(CRYPTO_INTERNAL);
277