18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C)2006 USAGI/WIDE Project
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Author:
68c2ecf20Sopenharmony_ci * 	Kazunori Miyazawa <miyazawa@linux-ipv6.org>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <crypto/internal/hash.h>
108c2ecf20Sopenharmony_ci#include <linux/err.h>
118c2ecf20Sopenharmony_ci#include <linux/kernel.h>
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_cistatic u_int32_t ks[12] = {0x01010101, 0x01010101, 0x01010101, 0x01010101,
158c2ecf20Sopenharmony_ci			   0x02020202, 0x02020202, 0x02020202, 0x02020202,
168c2ecf20Sopenharmony_ci			   0x03030303, 0x03030303, 0x03030303, 0x03030303};
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci/*
198c2ecf20Sopenharmony_ci * +------------------------
208c2ecf20Sopenharmony_ci * | <parent tfm>
218c2ecf20Sopenharmony_ci * +------------------------
228c2ecf20Sopenharmony_ci * | xcbc_tfm_ctx
238c2ecf20Sopenharmony_ci * +------------------------
248c2ecf20Sopenharmony_ci * | consts (block size * 2)
258c2ecf20Sopenharmony_ci * +------------------------
268c2ecf20Sopenharmony_ci */
278c2ecf20Sopenharmony_cistruct xcbc_tfm_ctx {
288c2ecf20Sopenharmony_ci	struct crypto_cipher *child;
298c2ecf20Sopenharmony_ci	u8 ctx[];
308c2ecf20Sopenharmony_ci};
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/*
338c2ecf20Sopenharmony_ci * +------------------------
348c2ecf20Sopenharmony_ci * | <shash desc>
358c2ecf20Sopenharmony_ci * +------------------------
368c2ecf20Sopenharmony_ci * | xcbc_desc_ctx
378c2ecf20Sopenharmony_ci * +------------------------
388c2ecf20Sopenharmony_ci * | odds (block size)
398c2ecf20Sopenharmony_ci * +------------------------
408c2ecf20Sopenharmony_ci * | prev (block size)
418c2ecf20Sopenharmony_ci * +------------------------
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_cistruct xcbc_desc_ctx {
448c2ecf20Sopenharmony_ci	unsigned int len;
458c2ecf20Sopenharmony_ci	u8 ctx[];
468c2ecf20Sopenharmony_ci};
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#define XCBC_BLOCKSIZE	16
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic int crypto_xcbc_digest_setkey(struct crypto_shash *parent,
518c2ecf20Sopenharmony_ci				     const u8 *inkey, unsigned int keylen)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	unsigned long alignmask = crypto_shash_alignmask(parent);
548c2ecf20Sopenharmony_ci	struct xcbc_tfm_ctx *ctx = crypto_shash_ctx(parent);
558c2ecf20Sopenharmony_ci	u8 *consts = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
568c2ecf20Sopenharmony_ci	int err = 0;
578c2ecf20Sopenharmony_ci	u8 key1[XCBC_BLOCKSIZE];
588c2ecf20Sopenharmony_ci	int bs = sizeof(key1);
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	if ((err = crypto_cipher_setkey(ctx->child, inkey, keylen)))
618c2ecf20Sopenharmony_ci		return err;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	crypto_cipher_encrypt_one(ctx->child, consts, (u8 *)ks + bs);
648c2ecf20Sopenharmony_ci	crypto_cipher_encrypt_one(ctx->child, consts + bs, (u8 *)ks + bs * 2);
658c2ecf20Sopenharmony_ci	crypto_cipher_encrypt_one(ctx->child, key1, (u8 *)ks);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	return crypto_cipher_setkey(ctx->child, key1, bs);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistatic int crypto_xcbc_digest_init(struct shash_desc *pdesc)
728c2ecf20Sopenharmony_ci{
738c2ecf20Sopenharmony_ci	unsigned long alignmask = crypto_shash_alignmask(pdesc->tfm);
748c2ecf20Sopenharmony_ci	struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
758c2ecf20Sopenharmony_ci	int bs = crypto_shash_blocksize(pdesc->tfm);
768c2ecf20Sopenharmony_ci	u8 *prev = PTR_ALIGN(&ctx->ctx[0], alignmask + 1) + bs;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	ctx->len = 0;
798c2ecf20Sopenharmony_ci	memset(prev, 0, bs);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	return 0;
828c2ecf20Sopenharmony_ci}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cistatic int crypto_xcbc_digest_update(struct shash_desc *pdesc, const u8 *p,
858c2ecf20Sopenharmony_ci				     unsigned int len)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	struct crypto_shash *parent = pdesc->tfm;
888c2ecf20Sopenharmony_ci	unsigned long alignmask = crypto_shash_alignmask(parent);
898c2ecf20Sopenharmony_ci	struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
908c2ecf20Sopenharmony_ci	struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
918c2ecf20Sopenharmony_ci	struct crypto_cipher *tfm = tctx->child;
928c2ecf20Sopenharmony_ci	int bs = crypto_shash_blocksize(parent);
938c2ecf20Sopenharmony_ci	u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
948c2ecf20Sopenharmony_ci	u8 *prev = odds + bs;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	/* checking the data can fill the block */
978c2ecf20Sopenharmony_ci	if ((ctx->len + len) <= bs) {
988c2ecf20Sopenharmony_ci		memcpy(odds + ctx->len, p, len);
998c2ecf20Sopenharmony_ci		ctx->len += len;
1008c2ecf20Sopenharmony_ci		return 0;
1018c2ecf20Sopenharmony_ci	}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	/* filling odds with new data and encrypting it */
1048c2ecf20Sopenharmony_ci	memcpy(odds + ctx->len, p, bs - ctx->len);
1058c2ecf20Sopenharmony_ci	len -= bs - ctx->len;
1068c2ecf20Sopenharmony_ci	p += bs - ctx->len;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	crypto_xor(prev, odds, bs);
1098c2ecf20Sopenharmony_ci	crypto_cipher_encrypt_one(tfm, prev, prev);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	/* clearing the length */
1128c2ecf20Sopenharmony_ci	ctx->len = 0;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	/* encrypting the rest of data */
1158c2ecf20Sopenharmony_ci	while (len > bs) {
1168c2ecf20Sopenharmony_ci		crypto_xor(prev, p, bs);
1178c2ecf20Sopenharmony_ci		crypto_cipher_encrypt_one(tfm, prev, prev);
1188c2ecf20Sopenharmony_ci		p += bs;
1198c2ecf20Sopenharmony_ci		len -= bs;
1208c2ecf20Sopenharmony_ci	}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	/* keeping the surplus of blocksize */
1238c2ecf20Sopenharmony_ci	if (len) {
1248c2ecf20Sopenharmony_ci		memcpy(odds, p, len);
1258c2ecf20Sopenharmony_ci		ctx->len = len;
1268c2ecf20Sopenharmony_ci	}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	return 0;
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic int crypto_xcbc_digest_final(struct shash_desc *pdesc, u8 *out)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	struct crypto_shash *parent = pdesc->tfm;
1348c2ecf20Sopenharmony_ci	unsigned long alignmask = crypto_shash_alignmask(parent);
1358c2ecf20Sopenharmony_ci	struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
1368c2ecf20Sopenharmony_ci	struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
1378c2ecf20Sopenharmony_ci	struct crypto_cipher *tfm = tctx->child;
1388c2ecf20Sopenharmony_ci	int bs = crypto_shash_blocksize(parent);
1398c2ecf20Sopenharmony_ci	u8 *consts = PTR_ALIGN(&tctx->ctx[0], alignmask + 1);
1408c2ecf20Sopenharmony_ci	u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
1418c2ecf20Sopenharmony_ci	u8 *prev = odds + bs;
1428c2ecf20Sopenharmony_ci	unsigned int offset = 0;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	if (ctx->len != bs) {
1458c2ecf20Sopenharmony_ci		unsigned int rlen;
1468c2ecf20Sopenharmony_ci		u8 *p = odds + ctx->len;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci		*p = 0x80;
1498c2ecf20Sopenharmony_ci		p++;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci		rlen = bs - ctx->len -1;
1528c2ecf20Sopenharmony_ci		if (rlen)
1538c2ecf20Sopenharmony_ci			memset(p, 0, rlen);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci		offset += bs;
1568c2ecf20Sopenharmony_ci	}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	crypto_xor(prev, odds, bs);
1598c2ecf20Sopenharmony_ci	crypto_xor(prev, consts + offset, bs);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	crypto_cipher_encrypt_one(tfm, out, prev);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	return 0;
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cistatic int xcbc_init_tfm(struct crypto_tfm *tfm)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	struct crypto_cipher *cipher;
1698c2ecf20Sopenharmony_ci	struct crypto_instance *inst = (void *)tfm->__crt_alg;
1708c2ecf20Sopenharmony_ci	struct crypto_cipher_spawn *spawn = crypto_instance_ctx(inst);
1718c2ecf20Sopenharmony_ci	struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	cipher = crypto_spawn_cipher(spawn);
1748c2ecf20Sopenharmony_ci	if (IS_ERR(cipher))
1758c2ecf20Sopenharmony_ci		return PTR_ERR(cipher);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	ctx->child = cipher;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	return 0;
1808c2ecf20Sopenharmony_ci};
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_cistatic void xcbc_exit_tfm(struct crypto_tfm *tfm)
1838c2ecf20Sopenharmony_ci{
1848c2ecf20Sopenharmony_ci	struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
1858c2ecf20Sopenharmony_ci	crypto_free_cipher(ctx->child);
1868c2ecf20Sopenharmony_ci}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_cistatic int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
1898c2ecf20Sopenharmony_ci{
1908c2ecf20Sopenharmony_ci	struct shash_instance *inst;
1918c2ecf20Sopenharmony_ci	struct crypto_cipher_spawn *spawn;
1928c2ecf20Sopenharmony_ci	struct crypto_alg *alg;
1938c2ecf20Sopenharmony_ci	unsigned long alignmask;
1948c2ecf20Sopenharmony_ci	u32 mask;
1958c2ecf20Sopenharmony_ci	int err;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask);
1988c2ecf20Sopenharmony_ci	if (err)
1998c2ecf20Sopenharmony_ci		return err;
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
2028c2ecf20Sopenharmony_ci	if (!inst)
2038c2ecf20Sopenharmony_ci		return -ENOMEM;
2048c2ecf20Sopenharmony_ci	spawn = shash_instance_ctx(inst);
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	err = crypto_grab_cipher(spawn, shash_crypto_instance(inst),
2078c2ecf20Sopenharmony_ci				 crypto_attr_alg_name(tb[1]), 0, mask);
2088c2ecf20Sopenharmony_ci	if (err)
2098c2ecf20Sopenharmony_ci		goto err_free_inst;
2108c2ecf20Sopenharmony_ci	alg = crypto_spawn_cipher_alg(spawn);
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	err = -EINVAL;
2138c2ecf20Sopenharmony_ci	if (alg->cra_blocksize != XCBC_BLOCKSIZE)
2148c2ecf20Sopenharmony_ci		goto err_free_inst;
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg);
2178c2ecf20Sopenharmony_ci	if (err)
2188c2ecf20Sopenharmony_ci		goto err_free_inst;
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	alignmask = alg->cra_alignmask | 3;
2218c2ecf20Sopenharmony_ci	inst->alg.base.cra_alignmask = alignmask;
2228c2ecf20Sopenharmony_ci	inst->alg.base.cra_priority = alg->cra_priority;
2238c2ecf20Sopenharmony_ci	inst->alg.base.cra_blocksize = alg->cra_blocksize;
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	inst->alg.digestsize = alg->cra_blocksize;
2268c2ecf20Sopenharmony_ci	inst->alg.descsize = ALIGN(sizeof(struct xcbc_desc_ctx),
2278c2ecf20Sopenharmony_ci				   crypto_tfm_ctx_alignment()) +
2288c2ecf20Sopenharmony_ci			     (alignmask &
2298c2ecf20Sopenharmony_ci			      ~(crypto_tfm_ctx_alignment() - 1)) +
2308c2ecf20Sopenharmony_ci			     alg->cra_blocksize * 2;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	inst->alg.base.cra_ctxsize = ALIGN(sizeof(struct xcbc_tfm_ctx),
2338c2ecf20Sopenharmony_ci					   alignmask + 1) +
2348c2ecf20Sopenharmony_ci				     alg->cra_blocksize * 2;
2358c2ecf20Sopenharmony_ci	inst->alg.base.cra_init = xcbc_init_tfm;
2368c2ecf20Sopenharmony_ci	inst->alg.base.cra_exit = xcbc_exit_tfm;
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	inst->alg.init = crypto_xcbc_digest_init;
2398c2ecf20Sopenharmony_ci	inst->alg.update = crypto_xcbc_digest_update;
2408c2ecf20Sopenharmony_ci	inst->alg.final = crypto_xcbc_digest_final;
2418c2ecf20Sopenharmony_ci	inst->alg.setkey = crypto_xcbc_digest_setkey;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	inst->free = shash_free_singlespawn_instance;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	err = shash_register_instance(tmpl, inst);
2468c2ecf20Sopenharmony_ci	if (err) {
2478c2ecf20Sopenharmony_cierr_free_inst:
2488c2ecf20Sopenharmony_ci		shash_free_singlespawn_instance(inst);
2498c2ecf20Sopenharmony_ci	}
2508c2ecf20Sopenharmony_ci	return err;
2518c2ecf20Sopenharmony_ci}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_cistatic struct crypto_template crypto_xcbc_tmpl = {
2548c2ecf20Sopenharmony_ci	.name = "xcbc",
2558c2ecf20Sopenharmony_ci	.create = xcbc_create,
2568c2ecf20Sopenharmony_ci	.module = THIS_MODULE,
2578c2ecf20Sopenharmony_ci};
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_cistatic int __init crypto_xcbc_module_init(void)
2608c2ecf20Sopenharmony_ci{
2618c2ecf20Sopenharmony_ci	return crypto_register_template(&crypto_xcbc_tmpl);
2628c2ecf20Sopenharmony_ci}
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_cistatic void __exit crypto_xcbc_module_exit(void)
2658c2ecf20Sopenharmony_ci{
2668c2ecf20Sopenharmony_ci	crypto_unregister_template(&crypto_xcbc_tmpl);
2678c2ecf20Sopenharmony_ci}
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_cisubsys_initcall(crypto_xcbc_module_init);
2708c2ecf20Sopenharmony_cimodule_exit(crypto_xcbc_module_exit);
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
2738c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("XCBC keyed hash algorithm");
2748c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("xcbc");
275