xref: /optee_os/lib/libmbedtls/core/hash.c (revision cb03400251f98aed22a2664509e3ed9e183800b0)
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_accel.h>
10 #include <crypto/crypto.h>
11 #include <crypto/crypto_impl.h>
12 #include <kernel/panic.h>
13 #include <mbedtls/md.h>
14 #include <mbedtls/platform_util.h>
15 #include <mbedtls/sha1.h>
16 #include <mbedtls/sha256.h>
17 #include <mbedtls/sha512.h>
18 #include <stdlib.h>
19 #include <string_ext.h>
20 #include <string.h>
21 #include <tee_api_types.h>
22 #include <utee_defines.h>
23 #include <util.h>
24 
25 struct mbed_hash_ctx {
26 	struct crypto_hash_ctx hash_ctx;
27 	mbedtls_md_context_t md_ctx;
28 };
29 
30 static const struct crypto_hash_ops mbed_hash_ops;
31 
32 static struct mbed_hash_ctx *to_hash_ctx(struct crypto_hash_ctx *ctx)
33 {
34 	assert(ctx && ctx->ops == &mbed_hash_ops);
35 
36 	return container_of(ctx, struct mbed_hash_ctx, hash_ctx);
37 }
38 
39 static TEE_Result mbed_hash_init(struct crypto_hash_ctx *ctx)
40 {
41 	if (mbedtls_md_starts(&to_hash_ctx(ctx)->md_ctx))
42 		return TEE_ERROR_BAD_STATE;
43 
44 	return TEE_SUCCESS;
45 }
46 
47 static TEE_Result mbed_hash_update(struct crypto_hash_ctx *ctx,
48 				   const uint8_t *data, size_t len)
49 {
50 	if (mbedtls_md_update(&to_hash_ctx(ctx)->md_ctx, data, len))
51 		return TEE_ERROR_BAD_STATE;
52 
53 	return TEE_SUCCESS;
54 }
55 
56 static TEE_Result mbed_hash_final(struct crypto_hash_ctx *ctx, uint8_t *digest,
57 				  size_t len)
58 {
59 	struct mbed_hash_ctx *hc = to_hash_ctx(ctx);
60 	uint8_t block_digest[TEE_MAX_HASH_SIZE] = { 0 };
61 	uint8_t *tmp_digest = NULL;
62 	size_t hash_size = 0;
63 
64 	if (len == 0)
65 		return TEE_ERROR_BAD_PARAMETERS;
66 
67 	hash_size = mbedtls_md_get_size(mbedtls_md_info_from_ctx(&hc->md_ctx));
68 	if (hash_size > len) {
69 		if (hash_size > sizeof(block_digest))
70 			return TEE_ERROR_BAD_STATE;
71 		tmp_digest = block_digest; /* use a tempory buffer */
72 	} else {
73 		tmp_digest = digest;
74 	}
75 
76 	if (mbedtls_md_finish(&hc->md_ctx, tmp_digest))
77 		return TEE_ERROR_BAD_STATE;
78 
79 	if (hash_size > len)
80 		memcpy(digest, tmp_digest, len);
81 
82 	return TEE_SUCCESS;
83 }
84 
85 static void mbed_hash_free_ctx(struct crypto_hash_ctx *ctx)
86 {
87 	struct mbed_hash_ctx *hc = to_hash_ctx(ctx);
88 
89 	mbedtls_md_free(&hc->md_ctx);
90 	free(hc);
91 }
92 
93 static void mbed_hash_copy_state(struct crypto_hash_ctx *dst_ctx,
94 				 struct crypto_hash_ctx *src_ctx)
95 {
96 	struct mbed_hash_ctx *src = to_hash_ctx(src_ctx);
97 	struct mbed_hash_ctx *dst = to_hash_ctx(dst_ctx);
98 
99 	if (mbedtls_md_clone(&dst->md_ctx, &src->md_ctx))
100 		panic();
101 }
102 
103 static const struct crypto_hash_ops mbed_hash_ops = {
104 	.init = mbed_hash_init,
105 	.update = mbed_hash_update,
106 	.final = mbed_hash_final,
107 	.free_ctx = mbed_hash_free_ctx,
108 	.copy_state = mbed_hash_copy_state,
109 };
110 
111 static TEE_Result mbed_hash_alloc_ctx(struct crypto_hash_ctx **ctx_ret,
112 				      mbedtls_md_type_t md_type)
113 {
114 	int mbed_res = 0;
115 	struct mbed_hash_ctx *hc = NULL;
116 	const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_type);
117 
118 	if (!md_info)
119 		return TEE_ERROR_NOT_SUPPORTED;
120 
121 	hc = calloc(1, sizeof(*hc));
122 	if (!hc)
123 		return TEE_ERROR_OUT_OF_MEMORY;
124 
125 	hc->hash_ctx.ops = &mbed_hash_ops;
126 	mbed_res = mbedtls_md_setup(&hc->md_ctx, md_info, 0);
127 	if (mbed_res) {
128 		free(hc);
129 		if (mbed_res == MBEDTLS_ERR_MD_ALLOC_FAILED)
130 			return TEE_ERROR_OUT_OF_MEMORY;
131 		return TEE_ERROR_NOT_SUPPORTED;
132 	}
133 
134 	*ctx_ret = &hc->hash_ctx;
135 
136 	return TEE_SUCCESS;
137 }
138 
139 #if defined(CFG_CRYPTO_MD5)
140 TEE_Result crypto_md5_alloc_ctx(struct crypto_hash_ctx **ctx)
141 {
142 	return mbed_hash_alloc_ctx(ctx, MBEDTLS_MD_MD5);
143 }
144 #endif
145 
146 #if defined(CFG_CRYPTO_SHA1)
147 TEE_Result crypto_sha1_alloc_ctx(struct crypto_hash_ctx **ctx)
148 {
149 	return mbed_hash_alloc_ctx(ctx, MBEDTLS_MD_SHA1);
150 }
151 #endif
152 
153 #if defined(CFG_CRYPTO_SHA224)
154 TEE_Result crypto_sha224_alloc_ctx(struct crypto_hash_ctx **ctx)
155 {
156 	return mbed_hash_alloc_ctx(ctx, MBEDTLS_MD_SHA224);
157 }
158 #endif
159 
160 #if defined(CFG_CRYPTO_SHA256)
161 TEE_Result crypto_sha256_alloc_ctx(struct crypto_hash_ctx **ctx)
162 {
163 	return mbed_hash_alloc_ctx(ctx, MBEDTLS_MD_SHA256);
164 }
165 #endif
166 
167 #if defined(CFG_CRYPTO_SHA384)
168 TEE_Result crypto_sha384_alloc_ctx(struct crypto_hash_ctx **ctx)
169 {
170 	return mbed_hash_alloc_ctx(ctx, MBEDTLS_MD_SHA384);
171 }
172 #endif
173 
174 #if defined(CFG_CRYPTO_SHA512)
175 TEE_Result crypto_sha512_alloc_ctx(struct crypto_hash_ctx **ctx)
176 {
177 	return mbed_hash_alloc_ctx(ctx, MBEDTLS_MD_SHA512);
178 }
179 #endif
180 
181 #if defined(CFG_CRYPTO_SHA256)
182 TEE_Result hash_sha256_check(const uint8_t *hash, const uint8_t *data,
183 			     size_t data_size)
184 {
185 	mbedtls_sha256_context hs;
186 	uint8_t digest[TEE_SHA256_HASH_SIZE] = { 0 };
187 
188 	memset(&hs, 0, sizeof(hs));
189 	mbedtls_sha256_init(&hs);
190 	mbedtls_sha256_starts(&hs, 0);
191 	mbedtls_sha256_update(&hs, data, data_size);
192 	mbedtls_sha256_finish(&hs, digest);
193 	mbedtls_sha256_free(&hs);
194 
195 	if (consttime_memcmp(digest, hash, sizeof(digest)))
196 		return TEE_ERROR_SECURITY;
197 	return TEE_SUCCESS;
198 }
199 #endif
200 
201 #if defined(MBEDTLS_SHA1_PROCESS_ALT)
202 int mbedtls_internal_sha1_process(mbedtls_sha1_context *ctx,
203 				  const unsigned char data[64])
204 {
205 	crypto_accel_sha1_compress(ctx->state, data, 1);
206 
207 	return 0;
208 }
209 #endif /*MBEDTLS_SHA1_PROCESS_ALT*/
210 
211 #if defined(MBEDTLS_SHA256_PROCESS_ALT)
212 int mbedtls_internal_sha256_process(mbedtls_sha256_context *ctx,
213 				    const unsigned char data[64])
214 {
215 	crypto_accel_sha256_compress(ctx->state, data, 1);
216 
217 	return 0;
218 }
219 #endif /*MBEDTLS_SHA256_PROCESS_ALT*/
220 
221 #if defined(MBEDTLS_SHA512_PROCESS_ALT)
222 int mbedtls_internal_sha512_process(mbedtls_sha512_context *ctx,
223 				    const unsigned char data[64])
224 {
225 	crypto_accel_sha512_compress(ctx->state, data, 1);
226 
227 	return 0;
228 }
229 #endif /*MBEDTLS_SHA512_PROCESS_ALT*/
230