xref: /optee_os/lib/libmbedtls/core/hmac.c (revision 32b3180828fa15a49ccc86ecb4be9d274c140c89)
12b716cccSEdison Ai // SPDX-License-Identifier: BSD-2-Clause
22b716cccSEdison Ai /*
32b716cccSEdison Ai  * Copyright (C) 2018, ARM Limited
42b716cccSEdison Ai  * Copyright (C) 2019, Linaro Limited
52b716cccSEdison Ai  */
62b716cccSEdison Ai 
72b716cccSEdison Ai #include <assert.h>
82b716cccSEdison Ai #include <compiler.h>
92b716cccSEdison Ai #include <crypto/crypto.h>
102b716cccSEdison Ai #include <crypto/crypto_impl.h>
112b716cccSEdison Ai #include <kernel/panic.h>
122b716cccSEdison Ai #include <mbedtls/md.h>
132b716cccSEdison Ai #include <stdlib.h>
142b716cccSEdison Ai #include <string.h>
152b716cccSEdison Ai #include <tee_api_types.h>
162b716cccSEdison Ai #include <utee_defines.h>
172b716cccSEdison Ai #include <util.h>
182b716cccSEdison Ai 
192b716cccSEdison Ai struct mbed_hmac_ctx {
202b716cccSEdison Ai 	struct crypto_mac_ctx mac_ctx;
212b716cccSEdison Ai 	mbedtls_md_context_t md_ctx;
222b716cccSEdison Ai };
232b716cccSEdison Ai 
242b716cccSEdison Ai static const struct crypto_mac_ops mbed_hmac_ops;
252b716cccSEdison Ai 
to_hmac_ctx(struct crypto_mac_ctx * ctx)262b716cccSEdison Ai static struct mbed_hmac_ctx *to_hmac_ctx(struct crypto_mac_ctx *ctx)
272b716cccSEdison Ai {
282b716cccSEdison Ai 	assert(ctx && ctx->ops == &mbed_hmac_ops);
292b716cccSEdison Ai 
302b716cccSEdison Ai 	return container_of(ctx, struct mbed_hmac_ctx, mac_ctx);
312b716cccSEdison Ai }
322b716cccSEdison Ai 
mbed_hmac_init(struct crypto_mac_ctx * ctx,const uint8_t * key,size_t len)332b716cccSEdison Ai static TEE_Result mbed_hmac_init(struct crypto_mac_ctx *ctx,
342b716cccSEdison Ai 				 const uint8_t *key, size_t len)
352b716cccSEdison Ai {
362b716cccSEdison Ai 	if (mbedtls_md_hmac_starts(&to_hmac_ctx(ctx)->md_ctx, key, len))
372b716cccSEdison Ai 		return TEE_ERROR_BAD_STATE;
382b716cccSEdison Ai 
392b716cccSEdison Ai 	return TEE_SUCCESS;
402b716cccSEdison Ai }
412b716cccSEdison Ai 
mbed_hmac_update(struct crypto_mac_ctx * ctx,const uint8_t * data,size_t len)422b716cccSEdison Ai static TEE_Result mbed_hmac_update(struct crypto_mac_ctx *ctx,
432b716cccSEdison Ai 				   const uint8_t *data, size_t len)
442b716cccSEdison Ai {
452b716cccSEdison Ai 	if (mbedtls_md_hmac_update(&to_hmac_ctx(ctx)->md_ctx, data, len))
462b716cccSEdison Ai 		return TEE_ERROR_BAD_STATE;
472b716cccSEdison Ai 
482b716cccSEdison Ai 	return TEE_SUCCESS;
492b716cccSEdison Ai }
502b716cccSEdison Ai 
mbed_hmac_final(struct crypto_mac_ctx * ctx,uint8_t * digest,size_t len)512b716cccSEdison Ai static TEE_Result mbed_hmac_final(struct crypto_mac_ctx *ctx, uint8_t *digest,
522b716cccSEdison Ai 				  size_t len)
532b716cccSEdison Ai {
542b716cccSEdison Ai 	struct mbed_hmac_ctx *c = to_hmac_ctx(ctx);
552b716cccSEdison Ai 	uint8_t block_digest[TEE_MAX_HASH_SIZE] = { 0 };
562b716cccSEdison Ai 	uint8_t *tmp_digest = NULL;
57*32b31808SJens Wiklander 	size_t hmac_size = 0;
582b716cccSEdison Ai 
592b716cccSEdison Ai 	if (len == 0)
602b716cccSEdison Ai 		return TEE_ERROR_BAD_PARAMETERS;
612b716cccSEdison Ai 
62*32b31808SJens Wiklander 	hmac_size = mbedtls_md_get_size(mbedtls_md_info_from_ctx(&c->md_ctx));
632b716cccSEdison Ai 	if (hmac_size > len) {
642b716cccSEdison Ai 		if (hmac_size > sizeof(block_digest))
652b716cccSEdison Ai 			return TEE_ERROR_BAD_STATE;
662b716cccSEdison Ai 		tmp_digest = block_digest; /* use a tempory buffer */
672b716cccSEdison Ai 	} else {
682b716cccSEdison Ai 		tmp_digest = digest;
692b716cccSEdison Ai 	}
702b716cccSEdison Ai 
712b716cccSEdison Ai 	if (mbedtls_md_hmac_finish(&c->md_ctx, tmp_digest))
722b716cccSEdison Ai 		return TEE_ERROR_BAD_STATE;
732b716cccSEdison Ai 
742b716cccSEdison Ai 	if (hmac_size > len)
752b716cccSEdison Ai 		memcpy(digest, tmp_digest, len);
762b716cccSEdison Ai 
772b716cccSEdison Ai 	return TEE_SUCCESS;
782b716cccSEdison Ai }
792b716cccSEdison Ai 
mbed_hmac_free_ctx(struct crypto_mac_ctx * ctx)802b716cccSEdison Ai static void mbed_hmac_free_ctx(struct crypto_mac_ctx *ctx)
812b716cccSEdison Ai {
822b716cccSEdison Ai 	struct mbed_hmac_ctx *c = to_hmac_ctx(ctx);
832b716cccSEdison Ai 
842b716cccSEdison Ai 	mbedtls_md_free(&c->md_ctx);
852b716cccSEdison Ai 	free(c);
862b716cccSEdison Ai }
872b716cccSEdison Ai 
mbed_hmac_copy_state(struct crypto_mac_ctx * dst_ctx,struct crypto_mac_ctx * src_ctx)882b716cccSEdison Ai static void mbed_hmac_copy_state(struct crypto_mac_ctx *dst_ctx,
892b716cccSEdison Ai 				 struct crypto_mac_ctx *src_ctx)
902b716cccSEdison Ai {
912b716cccSEdison Ai 	struct mbed_hmac_ctx *src = to_hmac_ctx(src_ctx);
922b716cccSEdison Ai 	struct mbed_hmac_ctx *dst = to_hmac_ctx(dst_ctx);
932b716cccSEdison Ai 
942b716cccSEdison Ai 	if (mbedtls_md_clone(&dst->md_ctx, &src->md_ctx))
952b716cccSEdison Ai 		panic();
962b716cccSEdison Ai }
972b716cccSEdison Ai 
982b716cccSEdison Ai static const struct crypto_mac_ops mbed_hmac_ops = {
992b716cccSEdison Ai 	.init = mbed_hmac_init,
1002b716cccSEdison Ai 	.update = mbed_hmac_update,
1012b716cccSEdison Ai 	.final = mbed_hmac_final,
1022b716cccSEdison Ai 	.free_ctx = mbed_hmac_free_ctx,
1032b716cccSEdison Ai 	.copy_state = mbed_hmac_copy_state,
1042b716cccSEdison Ai };
1052b716cccSEdison Ai 
mbed_hmac_alloc_ctx(struct crypto_mac_ctx ** ctx_ret,mbedtls_md_type_t md_type)1062b716cccSEdison Ai static TEE_Result mbed_hmac_alloc_ctx(struct crypto_mac_ctx **ctx_ret,
1072b716cccSEdison Ai 				      mbedtls_md_type_t md_type)
1082b716cccSEdison Ai {
1092b716cccSEdison Ai 	int mbed_res = 0;
1102b716cccSEdison Ai 	struct mbed_hmac_ctx *c = NULL;
1112b716cccSEdison Ai 	const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_type);
1122b716cccSEdison Ai 
1132b716cccSEdison Ai 	if (!md_info)
1142b716cccSEdison Ai 		return TEE_ERROR_NOT_SUPPORTED;
1152b716cccSEdison Ai 
1162b716cccSEdison Ai 	c = calloc(1, sizeof(*c));
1172b716cccSEdison Ai 	if (!c)
1182b716cccSEdison Ai 		return TEE_ERROR_OUT_OF_MEMORY;
1192b716cccSEdison Ai 
1202b716cccSEdison Ai 	c->mac_ctx.ops = &mbed_hmac_ops;
1212b716cccSEdison Ai 	mbed_res = mbedtls_md_setup(&c->md_ctx, md_info, 1);
1222b716cccSEdison Ai 	if (mbed_res) {
1232b716cccSEdison Ai 		free(c);
1242b716cccSEdison Ai 		if (mbed_res == MBEDTLS_ERR_MD_ALLOC_FAILED)
1252b716cccSEdison Ai 			return TEE_ERROR_OUT_OF_MEMORY;
1262b716cccSEdison Ai 		return TEE_ERROR_NOT_SUPPORTED;
1272b716cccSEdison Ai 	}
1282b716cccSEdison Ai 
1292b716cccSEdison Ai 	*ctx_ret = &c->mac_ctx;
1302b716cccSEdison Ai 
1312b716cccSEdison Ai 	return TEE_SUCCESS;
1322b716cccSEdison Ai }
1332b716cccSEdison Ai 
1342b716cccSEdison Ai #if defined(CFG_CRYPTO_MD5)
crypto_hmac_md5_alloc_ctx(struct crypto_mac_ctx ** ctx)1352b716cccSEdison Ai TEE_Result crypto_hmac_md5_alloc_ctx(struct crypto_mac_ctx **ctx)
1362b716cccSEdison Ai {
1372b716cccSEdison Ai 	return mbed_hmac_alloc_ctx(ctx, MBEDTLS_MD_MD5);
1382b716cccSEdison Ai }
1392b716cccSEdison Ai #endif
1402b716cccSEdison Ai 
1412b716cccSEdison Ai #if defined(CFG_CRYPTO_SHA1)
crypto_hmac_sha1_alloc_ctx(struct crypto_mac_ctx ** ctx)1422b716cccSEdison Ai TEE_Result crypto_hmac_sha1_alloc_ctx(struct crypto_mac_ctx **ctx)
1432b716cccSEdison Ai {
1442b716cccSEdison Ai 	return mbed_hmac_alloc_ctx(ctx, MBEDTLS_MD_SHA1);
1452b716cccSEdison Ai }
1462b716cccSEdison Ai #endif
1472b716cccSEdison Ai 
1482b716cccSEdison Ai #if defined(CFG_CRYPTO_SHA224)
crypto_hmac_sha224_alloc_ctx(struct crypto_mac_ctx ** ctx)1492b716cccSEdison Ai TEE_Result crypto_hmac_sha224_alloc_ctx(struct crypto_mac_ctx **ctx)
1502b716cccSEdison Ai {
1512b716cccSEdison Ai 	return mbed_hmac_alloc_ctx(ctx, MBEDTLS_MD_SHA224);
1522b716cccSEdison Ai }
1532b716cccSEdison Ai #endif
1542b716cccSEdison Ai 
1552b716cccSEdison Ai #if defined(CFG_CRYPTO_SHA256)
crypto_hmac_sha256_alloc_ctx(struct crypto_mac_ctx ** ctx)1562b716cccSEdison Ai TEE_Result crypto_hmac_sha256_alloc_ctx(struct crypto_mac_ctx **ctx)
1572b716cccSEdison Ai {
1582b716cccSEdison Ai 	return mbed_hmac_alloc_ctx(ctx, MBEDTLS_MD_SHA256);
1592b716cccSEdison Ai }
1602b716cccSEdison Ai #endif
1612b716cccSEdison Ai 
1622b716cccSEdison Ai #if defined(CFG_CRYPTO_SHA384)
crypto_hmac_sha384_alloc_ctx(struct crypto_mac_ctx ** ctx)1632b716cccSEdison Ai TEE_Result crypto_hmac_sha384_alloc_ctx(struct crypto_mac_ctx **ctx)
1642b716cccSEdison Ai {
1652b716cccSEdison Ai 	return mbed_hmac_alloc_ctx(ctx, MBEDTLS_MD_SHA384);
1662b716cccSEdison Ai }
1672b716cccSEdison Ai #endif
1682b716cccSEdison Ai 
1692b716cccSEdison Ai #if defined(CFG_CRYPTO_SHA512)
crypto_hmac_sha512_alloc_ctx(struct crypto_mac_ctx ** ctx)1702b716cccSEdison Ai TEE_Result crypto_hmac_sha512_alloc_ctx(struct crypto_mac_ctx **ctx)
1712b716cccSEdison Ai {
1722b716cccSEdison Ai 	return mbed_hmac_alloc_ctx(ctx, MBEDTLS_MD_SHA512);
1732b716cccSEdison Ai }
1742b716cccSEdison Ai #endif
175