xref: /optee_os/lib/libmbedtls/core/hmac.c (revision 19a31ec40245ae01a9adcd206eec2a4bb4479fc9)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (C) 2018, ARM Limited
4  * Copyright (C) 2019, Linaro Limited
5  */
6 
7 #include <assert.h>
8 #include <compiler.h>
9 #include <crypto/crypto.h>
10 #include <crypto/crypto_impl.h>
11 #include <kernel/panic.h>
12 #include <mbedtls/md.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <tee_api_types.h>
16 #include <utee_defines.h>
17 #include <util.h>
18 
19 struct mbed_hmac_ctx {
20 	struct crypto_mac_ctx mac_ctx;
21 	mbedtls_md_context_t md_ctx;
22 };
23 
24 static const struct crypto_mac_ops mbed_hmac_ops;
25 
26 static struct mbed_hmac_ctx *to_hmac_ctx(struct crypto_mac_ctx *ctx)
27 {
28 	assert(ctx && ctx->ops == &mbed_hmac_ops);
29 
30 	return container_of(ctx, struct mbed_hmac_ctx, mac_ctx);
31 }
32 
33 static TEE_Result mbed_hmac_init(struct crypto_mac_ctx *ctx,
34 				 const uint8_t *key, size_t len)
35 {
36 	if (mbedtls_md_hmac_starts(&to_hmac_ctx(ctx)->md_ctx, key, len))
37 		return TEE_ERROR_BAD_STATE;
38 
39 	return TEE_SUCCESS;
40 }
41 
42 static TEE_Result mbed_hmac_update(struct crypto_mac_ctx *ctx,
43 				   const uint8_t *data, size_t len)
44 {
45 	if (mbedtls_md_hmac_update(&to_hmac_ctx(ctx)->md_ctx, data, len))
46 		return TEE_ERROR_BAD_STATE;
47 
48 	return TEE_SUCCESS;
49 }
50 
51 static TEE_Result mbed_hmac_final(struct crypto_mac_ctx *ctx, uint8_t *digest,
52 				  size_t len)
53 {
54 	struct mbed_hmac_ctx *c = to_hmac_ctx(ctx);
55 	uint8_t block_digest[TEE_MAX_HASH_SIZE] = { 0 };
56 	uint8_t *tmp_digest = NULL;
57 	size_t hmac_size = 0;
58 
59 	if (len == 0)
60 		return TEE_ERROR_BAD_PARAMETERS;
61 
62 	hmac_size = mbedtls_md_get_size(mbedtls_md_info_from_ctx(&c->md_ctx));
63 	if (hmac_size > len) {
64 		if (hmac_size > sizeof(block_digest))
65 			return TEE_ERROR_BAD_STATE;
66 		tmp_digest = block_digest; /* use a tempory buffer */
67 	} else {
68 		tmp_digest = digest;
69 	}
70 
71 	if (mbedtls_md_hmac_finish(&c->md_ctx, tmp_digest))
72 		return TEE_ERROR_BAD_STATE;
73 
74 	if (hmac_size > len)
75 		memcpy(digest, tmp_digest, len);
76 
77 	return TEE_SUCCESS;
78 }
79 
80 static void mbed_hmac_free_ctx(struct crypto_mac_ctx *ctx)
81 {
82 	struct mbed_hmac_ctx *c = to_hmac_ctx(ctx);
83 
84 	mbedtls_md_free(&c->md_ctx);
85 	free(c);
86 }
87 
88 static void mbed_hmac_copy_state(struct crypto_mac_ctx *dst_ctx,
89 				 struct crypto_mac_ctx *src_ctx)
90 {
91 	struct mbed_hmac_ctx *src = to_hmac_ctx(src_ctx);
92 	struct mbed_hmac_ctx *dst = to_hmac_ctx(dst_ctx);
93 
94 	if (mbedtls_md_clone(&dst->md_ctx, &src->md_ctx))
95 		panic();
96 }
97 
98 static const struct crypto_mac_ops mbed_hmac_ops = {
99 	.init = mbed_hmac_init,
100 	.update = mbed_hmac_update,
101 	.final = mbed_hmac_final,
102 	.free_ctx = mbed_hmac_free_ctx,
103 	.copy_state = mbed_hmac_copy_state,
104 };
105 
106 static TEE_Result mbed_hmac_alloc_ctx(struct crypto_mac_ctx **ctx_ret,
107 				      mbedtls_md_type_t md_type)
108 {
109 	int mbed_res = 0;
110 	struct mbed_hmac_ctx *c = NULL;
111 	const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_type);
112 
113 	if (!md_info)
114 		return TEE_ERROR_NOT_SUPPORTED;
115 
116 	c = calloc(1, sizeof(*c));
117 	if (!c)
118 		return TEE_ERROR_OUT_OF_MEMORY;
119 
120 	c->mac_ctx.ops = &mbed_hmac_ops;
121 	mbed_res = mbedtls_md_setup(&c->md_ctx, md_info, 1);
122 	if (mbed_res) {
123 		free(c);
124 		if (mbed_res == MBEDTLS_ERR_MD_ALLOC_FAILED)
125 			return TEE_ERROR_OUT_OF_MEMORY;
126 		return TEE_ERROR_NOT_SUPPORTED;
127 	}
128 
129 	*ctx_ret = &c->mac_ctx;
130 
131 	return TEE_SUCCESS;
132 }
133 
134 #if defined(CFG_CRYPTO_MD5)
135 TEE_Result crypto_hmac_md5_alloc_ctx(struct crypto_mac_ctx **ctx)
136 {
137 	return mbed_hmac_alloc_ctx(ctx, MBEDTLS_MD_MD5);
138 }
139 #endif
140 
141 #if defined(CFG_CRYPTO_SHA1)
142 TEE_Result crypto_hmac_sha1_alloc_ctx(struct crypto_mac_ctx **ctx)
143 {
144 	return mbed_hmac_alloc_ctx(ctx, MBEDTLS_MD_SHA1);
145 }
146 #endif
147 
148 #if defined(CFG_CRYPTO_SHA224)
149 TEE_Result crypto_hmac_sha224_alloc_ctx(struct crypto_mac_ctx **ctx)
150 {
151 	return mbed_hmac_alloc_ctx(ctx, MBEDTLS_MD_SHA224);
152 }
153 #endif
154 
155 #if defined(CFG_CRYPTO_SHA256)
156 TEE_Result crypto_hmac_sha256_alloc_ctx(struct crypto_mac_ctx **ctx)
157 {
158 	return mbed_hmac_alloc_ctx(ctx, MBEDTLS_MD_SHA256);
159 }
160 #endif
161 
162 #if defined(CFG_CRYPTO_SHA384)
163 TEE_Result crypto_hmac_sha384_alloc_ctx(struct crypto_mac_ctx **ctx)
164 {
165 	return mbed_hmac_alloc_ctx(ctx, MBEDTLS_MD_SHA384);
166 }
167 #endif
168 
169 #if defined(CFG_CRYPTO_SHA512)
170 TEE_Result crypto_hmac_sha512_alloc_ctx(struct crypto_mac_ctx **ctx)
171 {
172 	return mbed_hmac_alloc_ctx(ctx, MBEDTLS_MD_SHA512);
173 }
174 #endif
175