18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * CMAC: Cipher Block Mode for Authentication
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright © 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Based on work by:
88c2ecf20Sopenharmony_ci *  Copyright © 2013 Tom St Denis <tstdenis@elliptictech.com>
98c2ecf20Sopenharmony_ci * Based on crypto/xcbc.c:
108c2ecf20Sopenharmony_ci *  Copyright © 2006 USAGI/WIDE Project,
118c2ecf20Sopenharmony_ci *   Author: Kazunori Miyazawa <miyazawa@linux-ipv6.org>
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <crypto/internal/hash.h>
158c2ecf20Sopenharmony_ci#include <linux/err.h>
168c2ecf20Sopenharmony_ci#include <linux/kernel.h>
178c2ecf20Sopenharmony_ci#include <linux/module.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/*
208c2ecf20Sopenharmony_ci * +------------------------
218c2ecf20Sopenharmony_ci * | <parent tfm>
228c2ecf20Sopenharmony_ci * +------------------------
238c2ecf20Sopenharmony_ci * | cmac_tfm_ctx
248c2ecf20Sopenharmony_ci * +------------------------
258c2ecf20Sopenharmony_ci * | consts (block size * 2)
268c2ecf20Sopenharmony_ci * +------------------------
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_cistruct cmac_tfm_ctx {
298c2ecf20Sopenharmony_ci	struct crypto_cipher *child;
308c2ecf20Sopenharmony_ci	u8 ctx[];
318c2ecf20Sopenharmony_ci};
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/*
348c2ecf20Sopenharmony_ci * +------------------------
358c2ecf20Sopenharmony_ci * | <shash desc>
368c2ecf20Sopenharmony_ci * +------------------------
378c2ecf20Sopenharmony_ci * | cmac_desc_ctx
388c2ecf20Sopenharmony_ci * +------------------------
398c2ecf20Sopenharmony_ci * | odds (block size)
408c2ecf20Sopenharmony_ci * +------------------------
418c2ecf20Sopenharmony_ci * | prev (block size)
428c2ecf20Sopenharmony_ci * +------------------------
438c2ecf20Sopenharmony_ci */
448c2ecf20Sopenharmony_cistruct cmac_desc_ctx {
458c2ecf20Sopenharmony_ci	unsigned int len;
468c2ecf20Sopenharmony_ci	u8 ctx[];
478c2ecf20Sopenharmony_ci};
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistatic int crypto_cmac_digest_setkey(struct crypto_shash *parent,
508c2ecf20Sopenharmony_ci				     const u8 *inkey, unsigned int keylen)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	unsigned long alignmask = crypto_shash_alignmask(parent);
538c2ecf20Sopenharmony_ci	struct cmac_tfm_ctx *ctx = crypto_shash_ctx(parent);
548c2ecf20Sopenharmony_ci	unsigned int bs = crypto_shash_blocksize(parent);
558c2ecf20Sopenharmony_ci	__be64 *consts = PTR_ALIGN((void *)ctx->ctx,
568c2ecf20Sopenharmony_ci				   (alignmask | (__alignof__(__be64) - 1)) + 1);
578c2ecf20Sopenharmony_ci	u64 _const[2];
588c2ecf20Sopenharmony_ci	int i, err = 0;
598c2ecf20Sopenharmony_ci	u8 msb_mask, gfmask;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	err = crypto_cipher_setkey(ctx->child, inkey, keylen);
628c2ecf20Sopenharmony_ci	if (err)
638c2ecf20Sopenharmony_ci		return err;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	/* encrypt the zero block */
668c2ecf20Sopenharmony_ci	memset(consts, 0, bs);
678c2ecf20Sopenharmony_ci	crypto_cipher_encrypt_one(ctx->child, (u8 *)consts, (u8 *)consts);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	switch (bs) {
708c2ecf20Sopenharmony_ci	case 16:
718c2ecf20Sopenharmony_ci		gfmask = 0x87;
728c2ecf20Sopenharmony_ci		_const[0] = be64_to_cpu(consts[1]);
738c2ecf20Sopenharmony_ci		_const[1] = be64_to_cpu(consts[0]);
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci		/* gf(2^128) multiply zero-ciphertext with u and u^2 */
768c2ecf20Sopenharmony_ci		for (i = 0; i < 4; i += 2) {
778c2ecf20Sopenharmony_ci			msb_mask = ((s64)_const[1] >> 63) & gfmask;
788c2ecf20Sopenharmony_ci			_const[1] = (_const[1] << 1) | (_const[0] >> 63);
798c2ecf20Sopenharmony_ci			_const[0] = (_const[0] << 1) ^ msb_mask;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci			consts[i + 0] = cpu_to_be64(_const[1]);
828c2ecf20Sopenharmony_ci			consts[i + 1] = cpu_to_be64(_const[0]);
838c2ecf20Sopenharmony_ci		}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci		break;
868c2ecf20Sopenharmony_ci	case 8:
878c2ecf20Sopenharmony_ci		gfmask = 0x1B;
888c2ecf20Sopenharmony_ci		_const[0] = be64_to_cpu(consts[0]);
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci		/* gf(2^64) multiply zero-ciphertext with u and u^2 */
918c2ecf20Sopenharmony_ci		for (i = 0; i < 2; i++) {
928c2ecf20Sopenharmony_ci			msb_mask = ((s64)_const[0] >> 63) & gfmask;
938c2ecf20Sopenharmony_ci			_const[0] = (_const[0] << 1) ^ msb_mask;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci			consts[i] = cpu_to_be64(_const[0]);
968c2ecf20Sopenharmony_ci		}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci		break;
998c2ecf20Sopenharmony_ci	}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	return 0;
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic int crypto_cmac_digest_init(struct shash_desc *pdesc)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	unsigned long alignmask = crypto_shash_alignmask(pdesc->tfm);
1078c2ecf20Sopenharmony_ci	struct cmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
1088c2ecf20Sopenharmony_ci	int bs = crypto_shash_blocksize(pdesc->tfm);
1098c2ecf20Sopenharmony_ci	u8 *prev = PTR_ALIGN((void *)ctx->ctx, alignmask + 1) + bs;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	ctx->len = 0;
1128c2ecf20Sopenharmony_ci	memset(prev, 0, bs);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	return 0;
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic int crypto_cmac_digest_update(struct shash_desc *pdesc, const u8 *p,
1188c2ecf20Sopenharmony_ci				     unsigned int len)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci	struct crypto_shash *parent = pdesc->tfm;
1218c2ecf20Sopenharmony_ci	unsigned long alignmask = crypto_shash_alignmask(parent);
1228c2ecf20Sopenharmony_ci	struct cmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
1238c2ecf20Sopenharmony_ci	struct cmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
1248c2ecf20Sopenharmony_ci	struct crypto_cipher *tfm = tctx->child;
1258c2ecf20Sopenharmony_ci	int bs = crypto_shash_blocksize(parent);
1268c2ecf20Sopenharmony_ci	u8 *odds = PTR_ALIGN((void *)ctx->ctx, alignmask + 1);
1278c2ecf20Sopenharmony_ci	u8 *prev = odds + bs;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	/* checking the data can fill the block */
1308c2ecf20Sopenharmony_ci	if ((ctx->len + len) <= bs) {
1318c2ecf20Sopenharmony_ci		memcpy(odds + ctx->len, p, len);
1328c2ecf20Sopenharmony_ci		ctx->len += len;
1338c2ecf20Sopenharmony_ci		return 0;
1348c2ecf20Sopenharmony_ci	}
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	/* filling odds with new data and encrypting it */
1378c2ecf20Sopenharmony_ci	memcpy(odds + ctx->len, p, bs - ctx->len);
1388c2ecf20Sopenharmony_ci	len -= bs - ctx->len;
1398c2ecf20Sopenharmony_ci	p += bs - ctx->len;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	crypto_xor(prev, odds, bs);
1428c2ecf20Sopenharmony_ci	crypto_cipher_encrypt_one(tfm, prev, prev);
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	/* clearing the length */
1458c2ecf20Sopenharmony_ci	ctx->len = 0;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	/* encrypting the rest of data */
1488c2ecf20Sopenharmony_ci	while (len > bs) {
1498c2ecf20Sopenharmony_ci		crypto_xor(prev, p, bs);
1508c2ecf20Sopenharmony_ci		crypto_cipher_encrypt_one(tfm, prev, prev);
1518c2ecf20Sopenharmony_ci		p += bs;
1528c2ecf20Sopenharmony_ci		len -= bs;
1538c2ecf20Sopenharmony_ci	}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	/* keeping the surplus of blocksize */
1568c2ecf20Sopenharmony_ci	if (len) {
1578c2ecf20Sopenharmony_ci		memcpy(odds, p, len);
1588c2ecf20Sopenharmony_ci		ctx->len = len;
1598c2ecf20Sopenharmony_ci	}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	return 0;
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic int crypto_cmac_digest_final(struct shash_desc *pdesc, u8 *out)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	struct crypto_shash *parent = pdesc->tfm;
1678c2ecf20Sopenharmony_ci	unsigned long alignmask = crypto_shash_alignmask(parent);
1688c2ecf20Sopenharmony_ci	struct cmac_tfm_ctx *tctx = crypto_shash_ctx(parent);
1698c2ecf20Sopenharmony_ci	struct cmac_desc_ctx *ctx = shash_desc_ctx(pdesc);
1708c2ecf20Sopenharmony_ci	struct crypto_cipher *tfm = tctx->child;
1718c2ecf20Sopenharmony_ci	int bs = crypto_shash_blocksize(parent);
1728c2ecf20Sopenharmony_ci	u8 *consts = PTR_ALIGN((void *)tctx->ctx,
1738c2ecf20Sopenharmony_ci			       (alignmask | (__alignof__(__be64) - 1)) + 1);
1748c2ecf20Sopenharmony_ci	u8 *odds = PTR_ALIGN((void *)ctx->ctx, alignmask + 1);
1758c2ecf20Sopenharmony_ci	u8 *prev = odds + bs;
1768c2ecf20Sopenharmony_ci	unsigned int offset = 0;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	if (ctx->len != bs) {
1798c2ecf20Sopenharmony_ci		unsigned int rlen;
1808c2ecf20Sopenharmony_ci		u8 *p = odds + ctx->len;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci		*p = 0x80;
1838c2ecf20Sopenharmony_ci		p++;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci		rlen = bs - ctx->len - 1;
1868c2ecf20Sopenharmony_ci		if (rlen)
1878c2ecf20Sopenharmony_ci			memset(p, 0, rlen);
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci		offset += bs;
1908c2ecf20Sopenharmony_ci	}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	crypto_xor(prev, odds, bs);
1938c2ecf20Sopenharmony_ci	crypto_xor(prev, consts + offset, bs);
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	crypto_cipher_encrypt_one(tfm, out, prev);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	return 0;
1988c2ecf20Sopenharmony_ci}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistatic int cmac_init_tfm(struct crypto_tfm *tfm)
2018c2ecf20Sopenharmony_ci{
2028c2ecf20Sopenharmony_ci	struct crypto_cipher *cipher;
2038c2ecf20Sopenharmony_ci	struct crypto_instance *inst = (void *)tfm->__crt_alg;
2048c2ecf20Sopenharmony_ci	struct crypto_cipher_spawn *spawn = crypto_instance_ctx(inst);
2058c2ecf20Sopenharmony_ci	struct cmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	cipher = crypto_spawn_cipher(spawn);
2088c2ecf20Sopenharmony_ci	if (IS_ERR(cipher))
2098c2ecf20Sopenharmony_ci		return PTR_ERR(cipher);
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	ctx->child = cipher;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	return 0;
2148c2ecf20Sopenharmony_ci};
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_cistatic void cmac_exit_tfm(struct crypto_tfm *tfm)
2178c2ecf20Sopenharmony_ci{
2188c2ecf20Sopenharmony_ci	struct cmac_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
2198c2ecf20Sopenharmony_ci	crypto_free_cipher(ctx->child);
2208c2ecf20Sopenharmony_ci}
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_cistatic int cmac_create(struct crypto_template *tmpl, struct rtattr **tb)
2238c2ecf20Sopenharmony_ci{
2248c2ecf20Sopenharmony_ci	struct shash_instance *inst;
2258c2ecf20Sopenharmony_ci	struct crypto_cipher_spawn *spawn;
2268c2ecf20Sopenharmony_ci	struct crypto_alg *alg;
2278c2ecf20Sopenharmony_ci	unsigned long alignmask;
2288c2ecf20Sopenharmony_ci	u32 mask;
2298c2ecf20Sopenharmony_ci	int err;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask);
2328c2ecf20Sopenharmony_ci	if (err)
2338c2ecf20Sopenharmony_ci		return err;
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
2368c2ecf20Sopenharmony_ci	if (!inst)
2378c2ecf20Sopenharmony_ci		return -ENOMEM;
2388c2ecf20Sopenharmony_ci	spawn = shash_instance_ctx(inst);
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	err = crypto_grab_cipher(spawn, shash_crypto_instance(inst),
2418c2ecf20Sopenharmony_ci				 crypto_attr_alg_name(tb[1]), 0, mask);
2428c2ecf20Sopenharmony_ci	if (err)
2438c2ecf20Sopenharmony_ci		goto err_free_inst;
2448c2ecf20Sopenharmony_ci	alg = crypto_spawn_cipher_alg(spawn);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	switch (alg->cra_blocksize) {
2478c2ecf20Sopenharmony_ci	case 16:
2488c2ecf20Sopenharmony_ci	case 8:
2498c2ecf20Sopenharmony_ci		break;
2508c2ecf20Sopenharmony_ci	default:
2518c2ecf20Sopenharmony_ci		err = -EINVAL;
2528c2ecf20Sopenharmony_ci		goto err_free_inst;
2538c2ecf20Sopenharmony_ci	}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg);
2568c2ecf20Sopenharmony_ci	if (err)
2578c2ecf20Sopenharmony_ci		goto err_free_inst;
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	alignmask = alg->cra_alignmask;
2608c2ecf20Sopenharmony_ci	inst->alg.base.cra_alignmask = alignmask;
2618c2ecf20Sopenharmony_ci	inst->alg.base.cra_priority = alg->cra_priority;
2628c2ecf20Sopenharmony_ci	inst->alg.base.cra_blocksize = alg->cra_blocksize;
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	inst->alg.digestsize = alg->cra_blocksize;
2658c2ecf20Sopenharmony_ci	inst->alg.descsize =
2668c2ecf20Sopenharmony_ci		ALIGN(sizeof(struct cmac_desc_ctx), crypto_tfm_ctx_alignment())
2678c2ecf20Sopenharmony_ci		+ (alignmask & ~(crypto_tfm_ctx_alignment() - 1))
2688c2ecf20Sopenharmony_ci		+ alg->cra_blocksize * 2;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	inst->alg.base.cra_ctxsize =
2718c2ecf20Sopenharmony_ci		ALIGN(sizeof(struct cmac_tfm_ctx), crypto_tfm_ctx_alignment())
2728c2ecf20Sopenharmony_ci		+ ((alignmask | (__alignof__(__be64) - 1)) &
2738c2ecf20Sopenharmony_ci		   ~(crypto_tfm_ctx_alignment() - 1))
2748c2ecf20Sopenharmony_ci		+ alg->cra_blocksize * 2;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	inst->alg.base.cra_init = cmac_init_tfm;
2778c2ecf20Sopenharmony_ci	inst->alg.base.cra_exit = cmac_exit_tfm;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	inst->alg.init = crypto_cmac_digest_init;
2808c2ecf20Sopenharmony_ci	inst->alg.update = crypto_cmac_digest_update;
2818c2ecf20Sopenharmony_ci	inst->alg.final = crypto_cmac_digest_final;
2828c2ecf20Sopenharmony_ci	inst->alg.setkey = crypto_cmac_digest_setkey;
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	inst->free = shash_free_singlespawn_instance;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	err = shash_register_instance(tmpl, inst);
2878c2ecf20Sopenharmony_ci	if (err) {
2888c2ecf20Sopenharmony_cierr_free_inst:
2898c2ecf20Sopenharmony_ci		shash_free_singlespawn_instance(inst);
2908c2ecf20Sopenharmony_ci	}
2918c2ecf20Sopenharmony_ci	return err;
2928c2ecf20Sopenharmony_ci}
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_cistatic struct crypto_template crypto_cmac_tmpl = {
2958c2ecf20Sopenharmony_ci	.name = "cmac",
2968c2ecf20Sopenharmony_ci	.create = cmac_create,
2978c2ecf20Sopenharmony_ci	.module = THIS_MODULE,
2988c2ecf20Sopenharmony_ci};
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_cistatic int __init crypto_cmac_module_init(void)
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	return crypto_register_template(&crypto_cmac_tmpl);
3038c2ecf20Sopenharmony_ci}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_cistatic void __exit crypto_cmac_module_exit(void)
3068c2ecf20Sopenharmony_ci{
3078c2ecf20Sopenharmony_ci	crypto_unregister_template(&crypto_cmac_tmpl);
3088c2ecf20Sopenharmony_ci}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_cisubsys_initcall(crypto_cmac_module_init);
3118c2ecf20Sopenharmony_cimodule_exit(crypto_cmac_module_exit);
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
3148c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("CMAC keyed hash algorithm");
3158c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("cmac");
316