xref: /optee_os/lib/libmbedtls/core/hash.c (revision dddd6a264f8a9625cdb39b14ab61831e29201807)
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 <mbedtls/sha256.h>
14 #include <stdlib.h>
15 #include <string_ext.h>
16 #include <string.h>
17 #include <tee_api_types.h>
18 #include <utee_defines.h>
19 #include <util.h>
20 
21 struct mbed_hash_ctx {
22 	struct crypto_hash_ctx hash_ctx;
23 	mbedtls_md_context_t md_ctx;
24 };
25 
26 static const struct crypto_hash_ops mbed_hash_ops;
27 
28 static struct mbed_hash_ctx *to_hash_ctx(struct crypto_hash_ctx *ctx)
29 {
30 	assert(ctx && ctx->ops == &mbed_hash_ops);
31 
32 	return container_of(ctx, struct mbed_hash_ctx, hash_ctx);
33 }
34 
35 static TEE_Result mbed_hash_init(struct crypto_hash_ctx *ctx)
36 {
37 	if (mbedtls_md_starts(&to_hash_ctx(ctx)->md_ctx))
38 		return TEE_ERROR_BAD_STATE;
39 
40 	return TEE_SUCCESS;
41 }
42 
43 static TEE_Result mbed_hash_update(struct crypto_hash_ctx *ctx,
44 				   const uint8_t *data, size_t len)
45 {
46 	if (mbedtls_md_update(&to_hash_ctx(ctx)->md_ctx, data, len))
47 		return TEE_ERROR_BAD_STATE;
48 
49 	return TEE_SUCCESS;
50 }
51 
52 static TEE_Result mbed_hash_final(struct crypto_hash_ctx *ctx, uint8_t *digest,
53 				  size_t len)
54 {
55 	struct mbed_hash_ctx *hc = to_hash_ctx(ctx);
56 	size_t hash_size = mbedtls_md_get_size(hc->md_ctx.md_info);
57 	uint8_t block_digest[TEE_MAX_HASH_SIZE] = { 0 };
58 	uint8_t *tmp_digest = NULL;
59 
60 	if (len == 0)
61 		return TEE_ERROR_BAD_PARAMETERS;
62 
63 	if (hash_size > len) {
64 		if (hash_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_finish(&hc->md_ctx, tmp_digest))
72 		return TEE_ERROR_BAD_STATE;
73 
74 	if (hash_size > len)
75 		memcpy(digest, tmp_digest, len);
76 
77 	return TEE_SUCCESS;
78 }
79 
80 static void mbed_hash_free_ctx(struct crypto_hash_ctx *ctx)
81 {
82 	struct mbed_hash_ctx *hc = to_hash_ctx(ctx);
83 
84 	mbedtls_md_free(&hc->md_ctx);
85 	free(hc);
86 }
87 
88 static void mbed_hash_copy_state(struct crypto_hash_ctx *dst_ctx,
89 				 struct crypto_hash_ctx *src_ctx)
90 {
91 	struct mbed_hash_ctx *src = to_hash_ctx(src_ctx);
92 	struct mbed_hash_ctx *dst = to_hash_ctx(dst_ctx);
93 
94 	if (mbedtls_md_clone(&dst->md_ctx, &src->md_ctx))
95 		panic();
96 }
97 
98 static const struct crypto_hash_ops mbed_hash_ops = {
99 	.init = mbed_hash_init,
100 	.update = mbed_hash_update,
101 	.final = mbed_hash_final,
102 	.free_ctx = mbed_hash_free_ctx,
103 	.copy_state = mbed_hash_copy_state,
104 };
105 
106 static TEE_Result mbed_hash_alloc_ctx(struct crypto_hash_ctx **ctx_ret,
107 				      mbedtls_md_type_t md_type)
108 {
109 	int mbed_res = 0;
110 	struct mbed_hash_ctx *hc = 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 	hc = calloc(1, sizeof(*hc));
117 	if (!hc)
118 		return TEE_ERROR_OUT_OF_MEMORY;
119 
120 	hc->hash_ctx.ops = &mbed_hash_ops;
121 	mbed_res = mbedtls_md_setup(&hc->md_ctx, md_info, 0);
122 	if (mbed_res) {
123 		free(hc);
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 = &hc->hash_ctx;
130 
131 	return TEE_SUCCESS;
132 }
133 
134 #if defined(CFG_CRYPTO_MD5)
135 TEE_Result crypto_md5_alloc_ctx(struct crypto_hash_ctx **ctx)
136 {
137 	return mbed_hash_alloc_ctx(ctx, MBEDTLS_MD_MD5);
138 }
139 #endif
140 
141 #if defined(CFG_CRYPTO_SHA1)
142 TEE_Result crypto_sha1_alloc_ctx(struct crypto_hash_ctx **ctx)
143 {
144 	return mbed_hash_alloc_ctx(ctx, MBEDTLS_MD_SHA1);
145 }
146 #endif
147 
148 #if defined(CFG_CRYPTO_SHA224)
149 TEE_Result crypto_sha224_alloc_ctx(struct crypto_hash_ctx **ctx)
150 {
151 	return mbed_hash_alloc_ctx(ctx, MBEDTLS_MD_SHA224);
152 }
153 #endif
154 
155 #if defined(CFG_CRYPTO_SHA256)
156 TEE_Result crypto_sha256_alloc_ctx(struct crypto_hash_ctx **ctx)
157 {
158 	return mbed_hash_alloc_ctx(ctx, MBEDTLS_MD_SHA256);
159 }
160 #endif
161 
162 #if defined(CFG_CRYPTO_SHA384)
163 TEE_Result crypto_sha384_alloc_ctx(struct crypto_hash_ctx **ctx)
164 {
165 	return mbed_hash_alloc_ctx(ctx, MBEDTLS_MD_SHA384);
166 }
167 #endif
168 
169 #if defined(CFG_CRYPTO_SHA512)
170 TEE_Result crypto_sha512_alloc_ctx(struct crypto_hash_ctx **ctx)
171 {
172 	return mbed_hash_alloc_ctx(ctx, MBEDTLS_MD_SHA512);
173 }
174 #endif
175 
176 #if defined(CFG_CRYPTO_SHA256)
177 TEE_Result hash_sha256_check(const uint8_t *hash, const uint8_t *data,
178 			     size_t data_size)
179 {
180 	mbedtls_sha256_context hs;
181 	uint8_t digest[TEE_SHA256_HASH_SIZE] = { 0 };
182 
183 	memset(&hs, 0, sizeof(hs));
184 	mbedtls_sha256_init(&hs);
185 	mbedtls_sha256_starts(&hs, 0);
186 	mbedtls_sha256_update(&hs, data, data_size);
187 	mbedtls_sha256_finish(&hs, digest);
188 	mbedtls_sha256_free(&hs);
189 
190 	if (consttime_memcmp(digest, hash, sizeof(digest)))
191 		return TEE_ERROR_SECURITY;
192 	return TEE_SUCCESS;
193 }
194 #endif
195