1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2014-2019, Linaro Limited
4  */
5 
6 #include <assert.h>
7 #include <crypto/crypto.h>
8 #include <crypto/crypto_impl.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <tee_api_types.h>
12 #include <tomcrypt_private.h>
13 #include <utee_defines.h>
14 #include <util.h>
15 
16 /******************************************************************************
17  * Message digest functions
18  ******************************************************************************/
19 
20 struct ltc_hash_ctx {
21 	struct crypto_hash_ctx ctx;
22 	const struct ltc_hash_descriptor *descr;
23 	hash_state state;
24 };
25 
26 static const struct crypto_hash_ops ltc_hash_ops;
27 
to_hash_ctx(struct crypto_hash_ctx * ctx)28 static struct ltc_hash_ctx *to_hash_ctx(struct crypto_hash_ctx *ctx)
29 {
30 	assert(ctx && ctx->ops == &ltc_hash_ops);
31 
32 	return container_of(ctx, struct ltc_hash_ctx, ctx);
33 }
34 
ltc_hash_init(struct crypto_hash_ctx * ctx)35 static TEE_Result ltc_hash_init(struct crypto_hash_ctx *ctx)
36 {
37 	struct ltc_hash_ctx *hc = to_hash_ctx(ctx);
38 
39 	if (hc->descr->init(&hc->state) == CRYPT_OK)
40 		return TEE_SUCCESS;
41 	else
42 		return TEE_ERROR_BAD_STATE;
43 }
44 
ltc_hash_update(struct crypto_hash_ctx * ctx,const uint8_t * data,size_t len)45 static TEE_Result ltc_hash_update(struct crypto_hash_ctx *ctx,
46 				  const uint8_t *data, size_t len)
47 {
48 	struct ltc_hash_ctx *hc = to_hash_ctx(ctx);
49 
50 	if (hc->descr->process(&hc->state, data, len) == CRYPT_OK)
51 		return TEE_SUCCESS;
52 	else
53 		return TEE_ERROR_BAD_STATE;
54 }
55 
ltc_hash_final(struct crypto_hash_ctx * ctx,uint8_t * digest,size_t len)56 static TEE_Result ltc_hash_final(struct crypto_hash_ctx *ctx, uint8_t *digest,
57 				 size_t len)
58 {
59 	struct ltc_hash_ctx *hc = to_hash_ctx(ctx);
60 	size_t hash_size = hc->descr->hashsize;
61 	uint8_t block_digest[TEE_MAX_HASH_SIZE] = { 0 };
62 	uint8_t *tmp_digest = NULL;
63 
64 	if (len == 0)
65 		return TEE_ERROR_BAD_PARAMETERS;
66 
67 	if (hash_size > len) {
68 		if (hash_size > sizeof(block_digest))
69 			return TEE_ERROR_BAD_STATE;
70 		tmp_digest = block_digest; /* use a tempory buffer */
71 	} else {
72 		tmp_digest = digest;
73 	}
74 
75 	if (hc->descr->done(&hc->state, tmp_digest) == CRYPT_OK) {
76 		if (hash_size > len)
77 			memcpy(digest, tmp_digest, len);
78 	} else {
79 		return TEE_ERROR_BAD_STATE;
80 	}
81 
82 	return TEE_SUCCESS;
83 }
84 
ltc_hash_free_ctx(struct crypto_hash_ctx * ctx)85 static void ltc_hash_free_ctx(struct crypto_hash_ctx *ctx)
86 {
87 	free(to_hash_ctx(ctx));
88 }
89 
ltc_hash_copy_state(struct crypto_hash_ctx * dst_ctx,struct crypto_hash_ctx * src_ctx)90 static void ltc_hash_copy_state(struct crypto_hash_ctx *dst_ctx,
91 				struct crypto_hash_ctx *src_ctx)
92 {
93 	struct ltc_hash_ctx *src = to_hash_ctx(src_ctx);
94 	struct ltc_hash_ctx *dst = to_hash_ctx(dst_ctx);
95 
96 	assert(src->descr == dst->descr);
97 	dst->state = src->state;
98 }
99 
100 static const struct crypto_hash_ops ltc_hash_ops = {
101 	.init = ltc_hash_init,
102 	.update = ltc_hash_update,
103 	.final = ltc_hash_final,
104 	.free_ctx = ltc_hash_free_ctx,
105 	.copy_state = ltc_hash_copy_state,
106 };
107 
ltc_hash_alloc_ctx(struct crypto_hash_ctx ** ctx_ret,int ltc_hash_idx)108 static TEE_Result ltc_hash_alloc_ctx(struct crypto_hash_ctx **ctx_ret,
109 				     int ltc_hash_idx)
110 {
111 	struct ltc_hash_ctx *ctx = NULL;
112 
113 	if (ltc_hash_idx < 0)
114 		return TEE_ERROR_NOT_SUPPORTED;
115 
116 	ctx = calloc(1, sizeof(*ctx));
117 	if (!ctx)
118 		return TEE_ERROR_OUT_OF_MEMORY;
119 
120 	ctx->ctx.ops = &ltc_hash_ops;
121 	ctx->descr = hash_descriptor[ltc_hash_idx];
122 
123 	*ctx_ret = &ctx->ctx;
124 
125 	return TEE_SUCCESS;
126 }
127 
128 #if defined(_CFG_CORE_LTC_MD5)
crypto_md5_alloc_ctx(struct crypto_hash_ctx ** ctx)129 TEE_Result crypto_md5_alloc_ctx(struct crypto_hash_ctx **ctx)
130 {
131 	return ltc_hash_alloc_ctx(ctx, find_hash("md5"));
132 }
133 #endif
134 
135 #if defined(_CFG_CORE_LTC_SHA1)
crypto_sha1_alloc_ctx(struct crypto_hash_ctx ** ctx)136 TEE_Result crypto_sha1_alloc_ctx(struct crypto_hash_ctx **ctx)
137 {
138 	return ltc_hash_alloc_ctx(ctx, find_hash("sha1"));
139 }
140 #endif
141 
142 #if defined(_CFG_CORE_LTC_SHA224)
crypto_sha224_alloc_ctx(struct crypto_hash_ctx ** ctx)143 TEE_Result crypto_sha224_alloc_ctx(struct crypto_hash_ctx **ctx)
144 {
145 	return ltc_hash_alloc_ctx(ctx, find_hash("sha224"));
146 }
147 #endif
148 
149 #if defined(_CFG_CORE_LTC_SHA256)
crypto_sha256_alloc_ctx(struct crypto_hash_ctx ** ctx)150 TEE_Result crypto_sha256_alloc_ctx(struct crypto_hash_ctx **ctx)
151 {
152 	return ltc_hash_alloc_ctx(ctx, find_hash("sha256"));
153 }
154 #endif
155 
156 #if defined(_CFG_CORE_LTC_SHA384)
crypto_sha384_alloc_ctx(struct crypto_hash_ctx ** ctx)157 TEE_Result crypto_sha384_alloc_ctx(struct crypto_hash_ctx **ctx)
158 {
159 	return ltc_hash_alloc_ctx(ctx, find_hash("sha384"));
160 }
161 #endif
162 
163 #if defined(_CFG_CORE_LTC_SHA512)
crypto_sha512_alloc_ctx(struct crypto_hash_ctx ** ctx)164 TEE_Result crypto_sha512_alloc_ctx(struct crypto_hash_ctx **ctx)
165 {
166 	return ltc_hash_alloc_ctx(ctx, find_hash("sha512"));
167 }
168 #endif
169 
170 #if defined(_CFG_CORE_LTC_SHA256)
hash_sha256_check(const uint8_t * hash,const uint8_t * data,size_t data_size)171 TEE_Result hash_sha256_check(const uint8_t *hash, const uint8_t *data,
172 		size_t data_size)
173 {
174 	hash_state hs;
175 	uint8_t digest[TEE_SHA256_HASH_SIZE];
176 
177 	if (sha256_init(&hs) != CRYPT_OK)
178 		return TEE_ERROR_GENERIC;
179 	if (sha256_process(&hs, data, data_size) != CRYPT_OK)
180 		return TEE_ERROR_GENERIC;
181 	if (sha256_done(&hs, digest) != CRYPT_OK)
182 		return TEE_ERROR_GENERIC;
183 	if (consttime_memcmp(digest, hash, sizeof(digest)) != 0)
184 		return TEE_ERROR_SECURITY;
185 	return TEE_SUCCESS;
186 }
187 #endif
188 
189 #if defined(_CFG_CORE_LTC_SHA512_256)
hash_sha512_256_compute(uint8_t * digest,const uint8_t * data,size_t data_size)190 TEE_Result hash_sha512_256_compute(uint8_t *digest, const uint8_t *data,
191 		size_t data_size)
192 {
193 	hash_state hs;
194 
195 	if (sha512_256_init(&hs) != CRYPT_OK)
196 		return TEE_ERROR_GENERIC;
197 	if (sha512_256_process(&hs, data, data_size) != CRYPT_OK)
198 		return TEE_ERROR_GENERIC;
199 	if (sha512_256_done(&hs, digest) != CRYPT_OK)
200 		return TEE_ERROR_GENERIC;
201 
202 	return TEE_SUCCESS;
203 }
204 #endif
205 
206