18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Cryptographic API.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * HMAC: Keyed-Hashing for Message Authentication (RFC2104).
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
88c2ecf20Sopenharmony_ci * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * The HMAC implementation is derived from USAGI.
118c2ecf20Sopenharmony_ci * Copyright (c) 2002 Kazunori Miyazawa <miyazawa@linux-ipv6.org> / USAGI
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <crypto/hmac.h>
158c2ecf20Sopenharmony_ci#include <crypto/internal/hash.h>
168c2ecf20Sopenharmony_ci#include <crypto/scatterwalk.h>
178c2ecf20Sopenharmony_ci#include <linux/err.h>
188c2ecf20Sopenharmony_ci#include <linux/init.h>
198c2ecf20Sopenharmony_ci#include <linux/kernel.h>
208c2ecf20Sopenharmony_ci#include <linux/module.h>
218c2ecf20Sopenharmony_ci#include <linux/scatterlist.h>
228c2ecf20Sopenharmony_ci#include <linux/string.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cistruct hmac_ctx {
258c2ecf20Sopenharmony_ci	struct crypto_shash *hash;
268c2ecf20Sopenharmony_ci};
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic inline void *align_ptr(void *p, unsigned int align)
298c2ecf20Sopenharmony_ci{
308c2ecf20Sopenharmony_ci	return (void *)ALIGN((unsigned long)p, align);
318c2ecf20Sopenharmony_ci}
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic inline struct hmac_ctx *hmac_ctx(struct crypto_shash *tfm)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	return align_ptr(crypto_shash_ctx_aligned(tfm) +
368c2ecf20Sopenharmony_ci			 crypto_shash_statesize(tfm) * 2,
378c2ecf20Sopenharmony_ci			 crypto_tfm_ctx_alignment());
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic int hmac_setkey(struct crypto_shash *parent,
418c2ecf20Sopenharmony_ci		       const u8 *inkey, unsigned int keylen)
428c2ecf20Sopenharmony_ci{
438c2ecf20Sopenharmony_ci	int bs = crypto_shash_blocksize(parent);
448c2ecf20Sopenharmony_ci	int ds = crypto_shash_digestsize(parent);
458c2ecf20Sopenharmony_ci	int ss = crypto_shash_statesize(parent);
468c2ecf20Sopenharmony_ci	char *ipad = crypto_shash_ctx_aligned(parent);
478c2ecf20Sopenharmony_ci	char *opad = ipad + ss;
488c2ecf20Sopenharmony_ci	struct hmac_ctx *ctx = align_ptr(opad + ss,
498c2ecf20Sopenharmony_ci					 crypto_tfm_ctx_alignment());
508c2ecf20Sopenharmony_ci	struct crypto_shash *hash = ctx->hash;
518c2ecf20Sopenharmony_ci	SHASH_DESC_ON_STACK(shash, hash);
528c2ecf20Sopenharmony_ci	unsigned int i;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	shash->tfm = hash;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	if (keylen > bs) {
578c2ecf20Sopenharmony_ci		int err;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci		err = crypto_shash_digest(shash, inkey, keylen, ipad);
608c2ecf20Sopenharmony_ci		if (err)
618c2ecf20Sopenharmony_ci			return err;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci		keylen = ds;
648c2ecf20Sopenharmony_ci	} else
658c2ecf20Sopenharmony_ci		memcpy(ipad, inkey, keylen);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	memset(ipad + keylen, 0, bs - keylen);
688c2ecf20Sopenharmony_ci	memcpy(opad, ipad, bs);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	for (i = 0; i < bs; i++) {
718c2ecf20Sopenharmony_ci		ipad[i] ^= HMAC_IPAD_VALUE;
728c2ecf20Sopenharmony_ci		opad[i] ^= HMAC_OPAD_VALUE;
738c2ecf20Sopenharmony_ci	}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	return crypto_shash_init(shash) ?:
768c2ecf20Sopenharmony_ci	       crypto_shash_update(shash, ipad, bs) ?:
778c2ecf20Sopenharmony_ci	       crypto_shash_export(shash, ipad) ?:
788c2ecf20Sopenharmony_ci	       crypto_shash_init(shash) ?:
798c2ecf20Sopenharmony_ci	       crypto_shash_update(shash, opad, bs) ?:
808c2ecf20Sopenharmony_ci	       crypto_shash_export(shash, opad);
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistatic int hmac_export(struct shash_desc *pdesc, void *out)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	struct shash_desc *desc = shash_desc_ctx(pdesc);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	return crypto_shash_export(desc, out);
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistatic int hmac_import(struct shash_desc *pdesc, const void *in)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	struct shash_desc *desc = shash_desc_ctx(pdesc);
938c2ecf20Sopenharmony_ci	struct hmac_ctx *ctx = hmac_ctx(pdesc->tfm);
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	desc->tfm = ctx->hash;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	return crypto_shash_import(desc, in);
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic int hmac_init(struct shash_desc *pdesc)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	return hmac_import(pdesc, crypto_shash_ctx_aligned(pdesc->tfm));
1038c2ecf20Sopenharmony_ci}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_cistatic int hmac_update(struct shash_desc *pdesc,
1068c2ecf20Sopenharmony_ci		       const u8 *data, unsigned int nbytes)
1078c2ecf20Sopenharmony_ci{
1088c2ecf20Sopenharmony_ci	struct shash_desc *desc = shash_desc_ctx(pdesc);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	return crypto_shash_update(desc, data, nbytes);
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistatic int hmac_final(struct shash_desc *pdesc, u8 *out)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	struct crypto_shash *parent = pdesc->tfm;
1168c2ecf20Sopenharmony_ci	int ds = crypto_shash_digestsize(parent);
1178c2ecf20Sopenharmony_ci	int ss = crypto_shash_statesize(parent);
1188c2ecf20Sopenharmony_ci	char *opad = crypto_shash_ctx_aligned(parent) + ss;
1198c2ecf20Sopenharmony_ci	struct shash_desc *desc = shash_desc_ctx(pdesc);
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	return crypto_shash_final(desc, out) ?:
1228c2ecf20Sopenharmony_ci	       crypto_shash_import(desc, opad) ?:
1238c2ecf20Sopenharmony_ci	       crypto_shash_finup(desc, out, ds, out);
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic int hmac_finup(struct shash_desc *pdesc, const u8 *data,
1278c2ecf20Sopenharmony_ci		      unsigned int nbytes, u8 *out)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	struct crypto_shash *parent = pdesc->tfm;
1318c2ecf20Sopenharmony_ci	int ds = crypto_shash_digestsize(parent);
1328c2ecf20Sopenharmony_ci	int ss = crypto_shash_statesize(parent);
1338c2ecf20Sopenharmony_ci	char *opad = crypto_shash_ctx_aligned(parent) + ss;
1348c2ecf20Sopenharmony_ci	struct shash_desc *desc = shash_desc_ctx(pdesc);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	return crypto_shash_finup(desc, data, nbytes, out) ?:
1378c2ecf20Sopenharmony_ci	       crypto_shash_import(desc, opad) ?:
1388c2ecf20Sopenharmony_ci	       crypto_shash_finup(desc, out, ds, out);
1398c2ecf20Sopenharmony_ci}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_cistatic int hmac_init_tfm(struct crypto_shash *parent)
1428c2ecf20Sopenharmony_ci{
1438c2ecf20Sopenharmony_ci	struct crypto_shash *hash;
1448c2ecf20Sopenharmony_ci	struct shash_instance *inst = shash_alg_instance(parent);
1458c2ecf20Sopenharmony_ci	struct crypto_shash_spawn *spawn = shash_instance_ctx(inst);
1468c2ecf20Sopenharmony_ci	struct hmac_ctx *ctx = hmac_ctx(parent);
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	hash = crypto_spawn_shash(spawn);
1498c2ecf20Sopenharmony_ci	if (IS_ERR(hash))
1508c2ecf20Sopenharmony_ci		return PTR_ERR(hash);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	parent->descsize = sizeof(struct shash_desc) +
1538c2ecf20Sopenharmony_ci			   crypto_shash_descsize(hash);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	ctx->hash = hash;
1568c2ecf20Sopenharmony_ci	return 0;
1578c2ecf20Sopenharmony_ci}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_cistatic void hmac_exit_tfm(struct crypto_shash *parent)
1608c2ecf20Sopenharmony_ci{
1618c2ecf20Sopenharmony_ci	struct hmac_ctx *ctx = hmac_ctx(parent);
1628c2ecf20Sopenharmony_ci	crypto_free_shash(ctx->hash);
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistatic int hmac_create(struct crypto_template *tmpl, struct rtattr **tb)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci	struct shash_instance *inst;
1688c2ecf20Sopenharmony_ci	struct crypto_shash_spawn *spawn;
1698c2ecf20Sopenharmony_ci	struct crypto_alg *alg;
1708c2ecf20Sopenharmony_ci	struct shash_alg *salg;
1718c2ecf20Sopenharmony_ci	u32 mask;
1728c2ecf20Sopenharmony_ci	int err;
1738c2ecf20Sopenharmony_ci	int ds;
1748c2ecf20Sopenharmony_ci	int ss;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH, &mask);
1778c2ecf20Sopenharmony_ci	if (err)
1788c2ecf20Sopenharmony_ci		return err;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
1818c2ecf20Sopenharmony_ci	if (!inst)
1828c2ecf20Sopenharmony_ci		return -ENOMEM;
1838c2ecf20Sopenharmony_ci	spawn = shash_instance_ctx(inst);
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	err = crypto_grab_shash(spawn, shash_crypto_instance(inst),
1868c2ecf20Sopenharmony_ci				crypto_attr_alg_name(tb[1]), 0, mask);
1878c2ecf20Sopenharmony_ci	if (err)
1888c2ecf20Sopenharmony_ci		goto err_free_inst;
1898c2ecf20Sopenharmony_ci	salg = crypto_spawn_shash_alg(spawn);
1908c2ecf20Sopenharmony_ci	alg = &salg->base;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	/* The underlying hash algorithm must not require a key */
1938c2ecf20Sopenharmony_ci	err = -EINVAL;
1948c2ecf20Sopenharmony_ci	if (crypto_shash_alg_needs_key(salg))
1958c2ecf20Sopenharmony_ci		goto err_free_inst;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	ds = salg->digestsize;
1988c2ecf20Sopenharmony_ci	ss = salg->statesize;
1998c2ecf20Sopenharmony_ci	if (ds > alg->cra_blocksize ||
2008c2ecf20Sopenharmony_ci	    ss < alg->cra_blocksize)
2018c2ecf20Sopenharmony_ci		goto err_free_inst;
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	err = crypto_inst_setname(shash_crypto_instance(inst), tmpl->name, alg);
2048c2ecf20Sopenharmony_ci	if (err)
2058c2ecf20Sopenharmony_ci		goto err_free_inst;
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	inst->alg.base.cra_priority = alg->cra_priority;
2088c2ecf20Sopenharmony_ci	inst->alg.base.cra_blocksize = alg->cra_blocksize;
2098c2ecf20Sopenharmony_ci	inst->alg.base.cra_alignmask = alg->cra_alignmask;
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	ss = ALIGN(ss, alg->cra_alignmask + 1);
2128c2ecf20Sopenharmony_ci	inst->alg.digestsize = ds;
2138c2ecf20Sopenharmony_ci	inst->alg.statesize = ss;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	inst->alg.base.cra_ctxsize = sizeof(struct hmac_ctx) +
2168c2ecf20Sopenharmony_ci				     ALIGN(ss * 2, crypto_tfm_ctx_alignment());
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	inst->alg.init = hmac_init;
2198c2ecf20Sopenharmony_ci	inst->alg.update = hmac_update;
2208c2ecf20Sopenharmony_ci	inst->alg.final = hmac_final;
2218c2ecf20Sopenharmony_ci	inst->alg.finup = hmac_finup;
2228c2ecf20Sopenharmony_ci	inst->alg.export = hmac_export;
2238c2ecf20Sopenharmony_ci	inst->alg.import = hmac_import;
2248c2ecf20Sopenharmony_ci	inst->alg.setkey = hmac_setkey;
2258c2ecf20Sopenharmony_ci	inst->alg.init_tfm = hmac_init_tfm;
2268c2ecf20Sopenharmony_ci	inst->alg.exit_tfm = hmac_exit_tfm;
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	inst->free = shash_free_singlespawn_instance;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	err = shash_register_instance(tmpl, inst);
2318c2ecf20Sopenharmony_ci	if (err) {
2328c2ecf20Sopenharmony_cierr_free_inst:
2338c2ecf20Sopenharmony_ci		shash_free_singlespawn_instance(inst);
2348c2ecf20Sopenharmony_ci	}
2358c2ecf20Sopenharmony_ci	return err;
2368c2ecf20Sopenharmony_ci}
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_cistatic struct crypto_template hmac_tmpl = {
2398c2ecf20Sopenharmony_ci	.name = "hmac",
2408c2ecf20Sopenharmony_ci	.create = hmac_create,
2418c2ecf20Sopenharmony_ci	.module = THIS_MODULE,
2428c2ecf20Sopenharmony_ci};
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_cistatic int __init hmac_module_init(void)
2458c2ecf20Sopenharmony_ci{
2468c2ecf20Sopenharmony_ci	return crypto_register_template(&hmac_tmpl);
2478c2ecf20Sopenharmony_ci}
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_cistatic void __exit hmac_module_exit(void)
2508c2ecf20Sopenharmony_ci{
2518c2ecf20Sopenharmony_ci	crypto_unregister_template(&hmac_tmpl);
2528c2ecf20Sopenharmony_ci}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_cisubsys_initcall(hmac_module_init);
2558c2ecf20Sopenharmony_cimodule_exit(hmac_module_exit);
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
2588c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("HMAC hash algorithm");
2598c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("hmac");
260