• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "mbedtls/compat-2.x.h"
17 #ifdef HKS_CONFIG_FILE
18 #include HKS_CONFIG_FILE
19 #else
20 #include "hks_config.h"
21 #endif
22 
23 #ifdef _CUT_AUTHENTICATE_
24 #undef HKS_SUPPORT_HASH_C
25 #endif /* _CUT_AUTHENTICATE_ */
26 
27 #ifdef HKS_SUPPORT_HASH_C
28 
29 #include "hks_mbedtls_hash.h"
30 
31 #include <mbedtls/md5.h>
32 #include <mbedtls/sha1.h>
33 #include <mbedtls/sha256.h>
34 #include <mbedtls/sha512.h>
35 
36 #include "hks_common_check.h"
37 #include "hks_log.h"
38 #include "hks_mem.h"
39 #include "hks_mbedtls_common.h"
40 #include "hks_template.h"
41 
42 struct HksMbedtlsHashCtx {
43     uint8_t *append;
44     uint32_t mAlg;
45 } HksMbedtlsHashCtx;
46 
HksMbedtlsHashMd5Init(void ** ctx,uint32_t alg)47 static int32_t HksMbedtlsHashMd5Init(void **ctx, uint32_t alg)
48 {
49     int32_t ret = 0;
50     mbedtls_md5_context *context = (mbedtls_md5_context *)HksMalloc(sizeof(mbedtls_md5_context));
51     HKS_IF_NULL_LOGE_RETURN(context, HKS_ERROR_MALLOC_FAIL, "malloc fail")
52 
53     mbedtls_md5_init(context);
54 
55     ret = mbedtls_md5_starts_ret(context);
56     if (ret != HKS_MBEDTLS_SUCCESS) {
57         HKS_LOG_E("Mbedtls Hash Md5 init starts_ret fail");
58         mbedtls_md5_free(context);
59         if (context != NULL) {
60             HksFree(context);
61         }
62         return HKS_ERROR_CRYPTO_ENGINE_ERROR;
63     }
64 
65     struct HksMbedtlsHashCtx *outCtx = (struct HksMbedtlsHashCtx *)HksMalloc(sizeof(HksMbedtlsHashCtx));
66     if (outCtx == NULL) {
67         HKS_LOG_E("Mbedtls Hash Md5 init malloc fail");
68         mbedtls_md5_free(context);
69         if (context != NULL) {
70             HksFree(context);
71         }
72         return HKS_ERROR_MALLOC_FAIL;
73     }
74 
75     outCtx->append = (void *)context;
76     outCtx->mAlg = alg;
77     *ctx = (void *)outCtx;
78     return HKS_SUCCESS;
79 }
80 
HksMbedtlsHashMd5Update(struct HksMbedtlsHashCtx * ctx,const unsigned char * input,size_t ilen)81 static int32_t HksMbedtlsHashMd5Update(struct HksMbedtlsHashCtx *ctx, const unsigned char *input, size_t ilen)
82 {
83     int32_t ret = 0;
84     mbedtls_md5_context *context = (mbedtls_md5_context *)ctx->append;
85 
86     HKS_IF_NULL_RETURN(context, HKS_ERROR_NULL_POINTER)
87 
88     ret = mbedtls_md5_update_ret(context, input, ilen);
89     if (ret != HKS_MBEDTLS_SUCCESS) {
90         HKS_LOG_E("Mbedtls Hash Md5 update fail");
91         return HKS_ERROR_CRYPTO_ENGINE_ERROR;
92     }
93 
94     return HKS_SUCCESS;
95 }
96 
HksMbedtlsHashMd5Final(struct HksMbedtlsHashCtx * ctx,const struct HksBlob * msg,struct HksBlob * hash)97 static int32_t HksMbedtlsHashMd5Final(struct HksMbedtlsHashCtx *ctx, const struct HksBlob *msg, struct HksBlob *hash)
98 {
99     int32_t ret = 0;
100     mbedtls_md5_context *context = (mbedtls_md5_context *)ctx->append;
101 
102     HKS_IF_NULL_RETURN(context, HKS_ERROR_NULL_POINTER)
103 
104     do {
105         if (msg->size != 0) {
106             ret = mbedtls_md5_update_ret(context, msg->data, msg->size);
107             if (ret != HKS_MBEDTLS_SUCCESS) {
108                 HKS_LOG_E("Mbedtls Hash Md5 finish last data fail");
109                 ret = HKS_ERROR_CRYPTO_ENGINE_ERROR;
110                 break;
111             }
112         }
113 
114         ret = mbedtls_md5_finish_ret(context, hash->data);
115         if (ret != HKS_MBEDTLS_SUCCESS) {
116             HKS_LOG_E("Mbedtls Hash Md5 finish fail");
117             ret = HKS_ERROR_CRYPTO_ENGINE_ERROR;
118             break;
119         }
120 
121         ret = HKS_SUCCESS;
122     } while (0);
123 
124     mbedtls_md5_free(context);
125     HksFree(context);
126     ctx->append = NULL;
127 
128     return ret;
129 }
130 
HksMbedtlsHashSha1Init(void ** ctx,uint32_t alg)131 static int32_t HksMbedtlsHashSha1Init(void **ctx, uint32_t alg)
132 {
133     int32_t ret = 0;
134     mbedtls_sha1_context *context = (mbedtls_sha1_context *)HksMalloc(sizeof(mbedtls_sha1_context));
135     HKS_IF_NULL_LOGE_RETURN(context, HKS_ERROR_MALLOC_FAIL, "malloc fail")
136     mbedtls_sha1_init(context);
137 
138     ret = mbedtls_sha1_starts_ret(context);
139     if (ret != HKS_MBEDTLS_SUCCESS) {
140         HKS_LOG_E("Mbedtls Hash Sha1 init start_rat fail");
141         mbedtls_sha1_free(context);
142         if (context != NULL) {
143             HksFree(context);
144         }
145         return HKS_ERROR_CRYPTO_ENGINE_ERROR;
146     }
147 
148     struct HksMbedtlsHashCtx *outCtx = (struct HksMbedtlsHashCtx *)HksMalloc(sizeof(HksMbedtlsHashCtx));
149     if (outCtx == NULL) {
150         HKS_LOG_E("Mbedtls Hash Sha1 init malloc fail");
151         mbedtls_sha1_free(context);
152         if (context != NULL) {
153             HksFree(context);
154         }
155         return HKS_ERROR_MALLOC_FAIL;
156     }
157 
158     outCtx->append = (void *)context;
159     outCtx->mAlg = alg;
160     *ctx = (void *)outCtx;
161 
162     return HKS_SUCCESS;
163 }
164 
HksMbedtlsHashSha1Update(struct HksMbedtlsHashCtx * ctx,const unsigned char * input,size_t ilen)165 static int32_t HksMbedtlsHashSha1Update(struct HksMbedtlsHashCtx *ctx, const unsigned char *input, size_t ilen)
166 {
167     mbedtls_sha1_context *context = (mbedtls_sha1_context *)ctx->append;
168     HKS_IF_NULL_RETURN(context, HKS_ERROR_NULL_POINTER)
169 
170     if (ilen == 0 || input == NULL) {
171         HKS_LOG_E("Mbedtls Hash sha1 input param error");
172         return HKS_ERROR_INVALID_ARGUMENT;
173     }
174 
175     int32_t ret = mbedtls_sha1_update_ret(context, input, ilen);
176     if (ret != HKS_MBEDTLS_SUCCESS) {
177         HKS_LOG_E("Mbedtls Hash sha1 update fail");
178         return HKS_ERROR_CRYPTO_ENGINE_ERROR;
179     }
180 
181     return HKS_SUCCESS;
182 }
183 
HksMbedtlsHashSha1Final(struct HksMbedtlsHashCtx * ctx,const struct HksBlob * msg,struct HksBlob * hash)184 static int32_t HksMbedtlsHashSha1Final(struct HksMbedtlsHashCtx *ctx, const struct HksBlob *msg, struct HksBlob *hash)
185 {
186     mbedtls_sha1_context *context = (mbedtls_sha1_context *)ctx->append;
187     HKS_IF_NULL_RETURN(context, HKS_ERROR_NULL_POINTER)
188 
189     int32_t ret = 0;
190     do {
191         if (hash->data == NULL) {
192             HKS_LOG_E("Mbedtls Hash sha1 output param error");
193             ret = HKS_ERROR_INVALID_ARGUMENT;
194             break;
195         }
196 
197         if (msg->size != 0) {
198             ret = mbedtls_sha1_update_ret(context, msg->data, msg->size);
199             if (ret != HKS_MBEDTLS_SUCCESS) {
200                 HKS_LOG_E("Mbedtls Hash sha1 finish last data fail");
201                 ret = HKS_ERROR_CRYPTO_ENGINE_ERROR;
202                 break;
203             }
204         }
205 
206         ret = mbedtls_sha1_finish_ret(context, hash->data);
207         if (ret != HKS_MBEDTLS_SUCCESS) {
208             HKS_LOG_E("Mbedtls Hash sha1 finish fail");
209             ret = HKS_ERROR_CRYPTO_ENGINE_ERROR;
210             break;
211         }
212 
213         ret = HKS_SUCCESS;
214     } while (0);
215     mbedtls_sha1_free(context);
216     HksFree(context);
217     ctx->append = NULL;
218     return ret;
219 }
220 
HksMbedtlsHashSha256Init(void ** ctx,int is224,uint32_t alg)221 static int32_t HksMbedtlsHashSha256Init(void **ctx, int is224, uint32_t alg)
222 {
223     mbedtls_sha256_context *context = (mbedtls_sha256_context *)HksMalloc(sizeof(mbedtls_sha256_context));
224     HKS_IF_NULL_LOGE_RETURN(context, HKS_ERROR_MALLOC_FAIL, "malloc fail")
225 
226     if (is224 != 0 && is224 != 1) {
227         HKS_LOG_E("Mbedtls Hash not sha224 & not sha256 ");
228         if (context != NULL) {
229             HksFree(context);
230         }
231         return HKS_ERROR_INVALID_ARGUMENT;
232     }
233 
234     mbedtls_sha256_init(context);
235 
236     int32_t ret = mbedtls_sha256_starts_ret(context, is224);
237     if (ret != HKS_MBEDTLS_SUCCESS) {
238         HKS_LOG_E("Mbedtls Hash sha256 init fail");
239         mbedtls_sha256_free(context);
240         if (context != NULL) {
241             HksFree(context);
242         }
243         return HKS_ERROR_CRYPTO_ENGINE_ERROR;
244     }
245 
246     struct HksMbedtlsHashCtx *outCtx = (struct HksMbedtlsHashCtx *)HksMalloc(sizeof(HksMbedtlsHashCtx));
247     if (outCtx == NULL) {
248         HKS_LOG_E("Mbedtls Hash sha256 init fail_outCtx malloc fail");
249         mbedtls_sha256_free(context);
250         if (context != NULL) {
251             HksFree(context);
252         }
253         return HKS_ERROR_MALLOC_FAIL;
254     }
255 
256     outCtx->append = (void *)context;
257     outCtx->mAlg = alg;
258     *ctx = (void *)outCtx;
259     return HKS_SUCCESS;
260 }
261 
HksMbedtlsHashSha256Update(struct HksMbedtlsHashCtx * ctx,const unsigned char * input,size_t ilen)262 static int32_t HksMbedtlsHashSha256Update(struct HksMbedtlsHashCtx *ctx, const unsigned char *input, size_t ilen)
263 {
264     mbedtls_sha256_context *context = (mbedtls_sha256_context *)ctx->append;
265     HKS_IF_NULL_RETURN(context, HKS_FAILURE)
266 
267     if (ilen == 0 || input == NULL) {
268         return HKS_FAILURE;
269     }
270 
271     int32_t ret = mbedtls_sha256_update_ret(context, input, ilen);
272     if (ret != HKS_MBEDTLS_SUCCESS) {
273         HKS_LOG_E("Mbedtls Hash sha256 update fail");
274         return HKS_ERROR_CRYPTO_ENGINE_ERROR;
275     }
276 
277     return HKS_SUCCESS;
278 }
279 
HksMbedtlsHashSha256Final(struct HksMbedtlsHashCtx * ctx,const struct HksBlob * msg,struct HksBlob * hash)280 static int32_t HksMbedtlsHashSha256Final(struct HksMbedtlsHashCtx *ctx, const struct HksBlob *msg,
281     struct HksBlob *hash)
282 {
283     mbedtls_sha256_context *context = (mbedtls_sha256_context *)ctx->append;
284     HKS_IF_NULL_RETURN(context, HKS_ERROR_NULL_POINTER)
285 
286     int32_t ret = 0;
287     do {
288         if (hash->data == NULL) {
289             HKS_LOG_E("Mbedtls Hash sha256 output param error");
290             ret = HKS_ERROR_INVALID_ARGUMENT;
291             break;
292         }
293 
294         if (msg->size != 0) {
295             ret = mbedtls_sha256_update_ret(context, msg->data, msg->size);
296             if (ret != HKS_MBEDTLS_SUCCESS) {
297                 HKS_LOG_E("Mbedtls Hash sha256 update fail");
298                 ret = HKS_ERROR_CRYPTO_ENGINE_ERROR;
299                 break;
300             }
301         }
302 
303         ret = mbedtls_sha256_finish_ret(context, hash->data);
304         if (ret != HKS_MBEDTLS_SUCCESS) {
305             HKS_LOG_E("Mbedtls Hash sha256 finish fail");
306             ret = HKS_ERROR_CRYPTO_ENGINE_ERROR;
307             break;
308         }
309         ret = HKS_SUCCESS;
310     } while (0);
311 
312     mbedtls_sha256_free(context);
313     HksFree(context);
314     ctx->append = NULL;
315 
316     return ret;
317 }
318 
HksMbedtlsHashSha512Init(void ** ctx,int is384,uint32_t alg)319 static int32_t HksMbedtlsHashSha512Init(void **ctx, int is384, uint32_t alg)
320 {
321     mbedtls_sha512_context *context = (mbedtls_sha512_context *)HksMalloc(sizeof(mbedtls_sha512_context));
322     HKS_IF_NULL_LOGE_RETURN(context, HKS_ERROR_MALLOC_FAIL, "malloc fail")
323 
324     if (is384 != 0 && is384 != 1) {
325         HKS_LOG_E("Mbedtls Hash not sha384 & not sha512 ");
326         if (context != NULL) {
327             HksFree(context);
328         }
329         return HKS_ERROR_BAD_STATE;
330     }
331 
332     mbedtls_sha512_init(context);
333 
334     int32_t ret = mbedtls_sha512_starts_ret(context, is384);
335     if (ret != HKS_MBEDTLS_SUCCESS) {
336         HKS_LOG_E("Mbedtls Hash sha512 init fail");
337         mbedtls_sha512_free(context);
338         if (context != NULL) {
339             HksFree(context);
340         }
341         return HKS_ERROR_CRYPTO_ENGINE_ERROR;
342     }
343 
344     struct HksMbedtlsHashCtx *outCtx = (struct HksMbedtlsHashCtx *)HksMalloc(sizeof(HksMbedtlsHashCtx));
345     if (outCtx == NULL) {
346         HKS_LOG_E("Mbedtls Hash Md5 init fail_outCtx malloc fail");
347         mbedtls_sha512_free(context);
348         if (context != NULL) {
349             HksFree(context);
350         }
351         return HKS_ERROR_MALLOC_FAIL;
352     }
353 
354     outCtx->append = (void *)context;
355     outCtx->mAlg = alg;
356     *ctx = (void *)outCtx;
357     return HKS_SUCCESS;
358 }
359 
HksMbedtlsHashSha512Update(struct HksMbedtlsHashCtx * ctx,const unsigned char * input,size_t ilen)360 static int32_t HksMbedtlsHashSha512Update(struct HksMbedtlsHashCtx *ctx, const unsigned char *input, size_t ilen)
361 {
362     mbedtls_sha512_context *context = (mbedtls_sha512_context *)ctx->append;
363     HKS_IF_NULL_RETURN(context, HKS_ERROR_NULL_POINTER)
364 
365     if (ilen == 0 || input == NULL) {
366         HKS_LOG_E("Mbedtls Hash sha512 input param error");
367         return HKS_ERROR_INVALID_ARGUMENT;
368     }
369 
370     int32_t ret = mbedtls_sha512_update_ret(context, input, ilen);
371     if (ret != HKS_MBEDTLS_SUCCESS) {
372         HKS_LOG_E("Mbedtls Hash sha512 update fail");
373         return HKS_ERROR_CRYPTO_ENGINE_ERROR;
374     }
375 
376     return HKS_SUCCESS;
377 }
378 
HksMbedtlsHashSha512Final(struct HksMbedtlsHashCtx * ctx,const struct HksBlob * msg,struct HksBlob * hash)379 static int32_t HksMbedtlsHashSha512Final(struct HksMbedtlsHashCtx *ctx, const struct HksBlob *msg,
380     struct HksBlob *hash)
381 {
382     mbedtls_sha512_context *context = (mbedtls_sha512_context *)ctx->append;
383     HKS_IF_NULL_RETURN(context, HKS_ERROR_NULL_POINTER)
384 
385     int32_t ret = 0;
386     do {
387         if (hash->data == NULL) {
388             HKS_LOG_E("Mbedtls Hash sha512 output param error");
389             ret = HKS_ERROR_INVALID_ARGUMENT;
390             break;
391         }
392 
393         if (msg->size != 0) {
394             ret = mbedtls_sha512_update_ret(context, msg->data, msg->size);
395             if (ret != HKS_MBEDTLS_SUCCESS) {
396                 HKS_LOG_E("Mbedtls Hash sha512 finish last data fail");
397                 ret = HKS_ERROR_CRYPTO_ENGINE_ERROR;
398                 break;
399             }
400         }
401 
402         ret = mbedtls_sha512_finish_ret(context, hash->data);
403         if (ret != HKS_MBEDTLS_SUCCESS) {
404             HKS_LOG_E("Mbedtls Hash sha512 finish fail");
405             ret = HKS_ERROR_CRYPTO_ENGINE_ERROR;
406             break;
407         }
408 
409         ret = HKS_SUCCESS;
410     } while (0);
411 
412     mbedtls_sha512_free(context);
413     HksFree(context);
414     ctx->append = NULL;
415     return ret;
416 }
417 
HksMbedtlsMd5HashFreeCtx(void ** cryptoCtx)418 static void HksMbedtlsMd5HashFreeCtx(void **cryptoCtx)
419 {
420     if (cryptoCtx == NULL || *cryptoCtx == NULL) {
421         HKS_LOG_E("Mbedtls Hash freeCtx param error");
422         return;
423     }
424     struct HksMbedtlsHashCtx *hashCtx = (struct HksMbedtlsHashCtx *)*cryptoCtx;
425     if (hashCtx->append != NULL) {
426         mbedtls_md5_free((mbedtls_md5_context *)hashCtx->append);
427     }
428 }
429 
HksMbedtlsSHA1HashFreeCtx(void ** cryptoCtx)430 static void HksMbedtlsSHA1HashFreeCtx(void **cryptoCtx)
431 {
432     if (cryptoCtx == NULL || *cryptoCtx == NULL) {
433         HKS_LOG_E("Mbedtls Hash freeCtx param error");
434         return;
435     }
436     struct HksMbedtlsHashCtx *hashCtx = (struct HksMbedtlsHashCtx *)*cryptoCtx;
437     if (hashCtx->append != NULL) {
438         mbedtls_sha1_free((mbedtls_sha1_context *)hashCtx->append);
439     }
440 }
441 
HksMbedtlsSha224Sha256HashFreeCtx(void ** cryptoCtx)442 static void HksMbedtlsSha224Sha256HashFreeCtx(void **cryptoCtx)
443 {
444     if (cryptoCtx == NULL || *cryptoCtx == NULL) {
445         HKS_LOG_E("Mbedtls Hash freeCtx param error");
446         return;
447     }
448     struct HksMbedtlsHashCtx *hashCtx = (struct HksMbedtlsHashCtx *)*cryptoCtx;
449     if (hashCtx->append != NULL) {
450         mbedtls_sha256_free((mbedtls_sha256_context *)hashCtx->append);
451     }
452 }
453 
HksMbedtlsSha384Sha512HashFreeCtx(void ** cryptoCtx)454 static void HksMbedtlsSha384Sha512HashFreeCtx(void **cryptoCtx)
455 {
456     if (cryptoCtx == NULL || *cryptoCtx == NULL) {
457         HKS_LOG_E("Mbedtls Hash freeCtx param error");
458         return;
459     }
460     struct HksMbedtlsHashCtx *hashCtx = (struct HksMbedtlsHashCtx *)*cryptoCtx;
461     if (hashCtx->append != NULL) {
462         mbedtls_sha512_free((mbedtls_sha512_context *)hashCtx->append);
463     }
464 }
465 
HksMbedtlsHashInit(void ** cryptoCtx,uint32_t digestAlg)466 int32_t HksMbedtlsHashInit(void **cryptoCtx, uint32_t digestAlg)
467 {
468     int32_t ret = 0;
469     switch (digestAlg) {
470         case HKS_DIGEST_MD5:
471             ret = HksMbedtlsHashMd5Init(cryptoCtx, digestAlg);
472             break;
473         case HKS_DIGEST_SHA1:
474             ret = HksMbedtlsHashSha1Init(cryptoCtx, digestAlg);
475             break;
476         case HKS_DIGEST_SHA224:
477             ret = HksMbedtlsHashSha256Init(cryptoCtx, 1, digestAlg); /* 0 for SHA-224 */
478             break;
479         case HKS_DIGEST_SHA256:
480             ret = HksMbedtlsHashSha256Init(cryptoCtx, 0, digestAlg); /* 0 for SHA-256 */
481             break;
482         case HKS_DIGEST_SHA384:
483             ret = HksMbedtlsHashSha512Init(cryptoCtx, 1, digestAlg); /* 1 for SHA-384 */
484             break;
485         case HKS_DIGEST_SHA512:
486             ret = HksMbedtlsHashSha512Init(cryptoCtx, 0, digestAlg); /* 0 for SHA-512 */
487             break;
488         default:
489             return HKS_ERROR_INVALID_DIGEST;
490     }
491 
492     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "Mbedtls hash init failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret)
493 
494     return HKS_SUCCESS;
495 }
496 
HksMbedtlsHashUpdate(void * cryptoCtx,const struct HksBlob * msg)497 int32_t HksMbedtlsHashUpdate(void *cryptoCtx, const struct HksBlob *msg)
498 {
499     int32_t ret = 0;
500     struct HksMbedtlsHashCtx *hashCtx = (struct HksMbedtlsHashCtx *)cryptoCtx;
501 
502     switch (hashCtx->mAlg) {
503         case HKS_DIGEST_MD5:
504             ret = HksMbedtlsHashMd5Update(hashCtx, msg->data, msg->size);
505             break;
506         case HKS_DIGEST_SHA1:
507             ret = HksMbedtlsHashSha1Update(hashCtx, msg->data, msg->size);
508             break;
509         case HKS_DIGEST_SHA224:
510         case HKS_DIGEST_SHA256:
511             ret = HksMbedtlsHashSha256Update(hashCtx, msg->data, msg->size);
512             break;
513         case HKS_DIGEST_SHA384:
514         case HKS_DIGEST_SHA512:
515             ret = HksMbedtlsHashSha512Update(hashCtx, msg->data, msg->size);
516             break;
517         default:
518             return HKS_ERROR_INVALID_DIGEST;
519     }
520 
521     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "Mbedtls hash update failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret)
522 
523     return HKS_SUCCESS;
524 }
525 
HksMbedtlsHashFinal(void ** cryptoCtx,const struct HksBlob * msg,struct HksBlob * hash)526 int32_t HksMbedtlsHashFinal(void **cryptoCtx, const struct HksBlob *msg, struct HksBlob *hash)
527 {
528     int32_t ret = 0;
529     struct HksMbedtlsHashCtx *hashCtx = (struct HksMbedtlsHashCtx *)*cryptoCtx;
530     HKS_IF_NULL_RETURN(hashCtx, HKS_ERROR_INVALID_ARGUMENT)
531 
532     switch (hashCtx->mAlg) {
533         case HKS_DIGEST_MD5:
534             ret = HksMbedtlsHashMd5Final(hashCtx, msg, hash);
535             break;
536         case HKS_DIGEST_SHA1:
537             ret = HksMbedtlsHashSha1Final(hashCtx, msg, hash);
538             break;
539         case HKS_DIGEST_SHA224:
540         case HKS_DIGEST_SHA256:
541             ret = HksMbedtlsHashSha256Final(hashCtx, msg, hash);
542             break;
543         case HKS_DIGEST_SHA384:
544         case HKS_DIGEST_SHA512:
545             ret = HksMbedtlsHashSha512Final(hashCtx, msg, hash);
546             break;
547         default:
548             HksMbedtlsHashFreeCtx(cryptoCtx);
549             return HKS_ERROR_INVALID_DIGEST;
550     }
551 
552     if (ret != HKS_SUCCESS) {
553         HKS_LOG_E("Mbedtls hash final failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
554         HksFree(*cryptoCtx);
555         *cryptoCtx = NULL;
556         return ret;
557     }
558 
559     ret = HksGetDigestLen(hashCtx->mAlg, &(hash->size));
560     HKS_IF_NOT_SUCC_LOGE(ret, "Get digest len failed!")
561 
562     HksMbedtlsHashFreeCtx(cryptoCtx);
563     return ret;
564 }
565 
HksMbedtlsHashFreeCtx(void ** cryptoCtx)566 void HksMbedtlsHashFreeCtx(void **cryptoCtx)
567 {
568     if (cryptoCtx == NULL || *cryptoCtx == NULL) {
569         HKS_LOG_E("Mbedtls Hash freeCtx param error");
570         return;
571     }
572     struct HksMbedtlsHashCtx *hashCtx = (struct HksMbedtlsHashCtx *)*cryptoCtx;
573     switch (hashCtx->mAlg) {
574         case HKS_DIGEST_MD5:
575             HksMbedtlsMd5HashFreeCtx(cryptoCtx);
576             break;
577         case HKS_DIGEST_SHA1:
578             HksMbedtlsSHA1HashFreeCtx(cryptoCtx);
579             break;
580         case HKS_DIGEST_SHA224:
581         case HKS_DIGEST_SHA256:
582             HksMbedtlsSha224Sha256HashFreeCtx(cryptoCtx);
583             break;
584         case HKS_DIGEST_SHA384:
585         case HKS_DIGEST_SHA512:
586             HksMbedtlsSha384Sha512HashFreeCtx(cryptoCtx);
587             break;
588         default:
589             break;
590     }
591 
592     if (*cryptoCtx != NULL) {
593         if (hashCtx->append != NULL) {
594             HksFree(hashCtx->append);
595             hashCtx->append = NULL;
596         }
597 
598         HksFree(*cryptoCtx);
599         *cryptoCtx = NULL;
600     }
601 }
602 
HksMbedtlsHash(uint32_t alg,const struct HksBlob * msg,struct HksBlob * hash)603 int32_t HksMbedtlsHash(uint32_t alg, const struct HksBlob *msg, struct HksBlob *hash)
604 {
605     int32_t ret = 0;
606     switch (alg) {
607 #ifdef HKS_SUPPORT_HASH_MD5
608         case HKS_DIGEST_MD5:
609             ret = mbedtls_md5_ret(msg->data, msg->size, hash->data);
610             break;
611 #endif
612 #ifdef HKS_SUPPORT_HASH_SHA1
613         case HKS_DIGEST_SHA1:
614             ret = mbedtls_sha1_ret(msg->data, msg->size, hash->data);
615             break;
616 #endif
617 #ifdef HKS_SUPPORT_HASH_SHA224
618         case HKS_DIGEST_SHA224:
619             ret = mbedtls_sha256_ret(msg->data, msg->size, hash->data, 1); /* 0 for SHA-224 */
620             break;
621 #endif
622 #ifdef HKS_SUPPORT_HASH_SHA256
623         case HKS_DIGEST_SHA256:
624             ret = mbedtls_sha256_ret(msg->data, msg->size, hash->data, 0); /* 0 for SHA-256 */
625             break;
626 #endif
627 #ifdef HKS_SUPPORT_HASH_SHA384
628         case HKS_DIGEST_SHA384:
629             ret = mbedtls_sha512_ret(msg->data, msg->size, hash->data, 1); /* 1 for SHA-384 */
630             break;
631 #endif
632 #ifdef HKS_SUPPORT_HASH_SHA512
633         case HKS_DIGEST_SHA512:
634             ret = mbedtls_sha512_ret(msg->data, msg->size, hash->data, 0); /* 0 for SHA-512 */
635             break;
636 #endif
637         default:
638             return HKS_ERROR_INVALID_DIGEST;
639     }
640 
641     if (ret != HKS_MBEDTLS_SUCCESS) {
642         HKS_LOG_E("Mbedtls hash failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
643         return HKS_ERROR_CRYPTO_ENGINE_ERROR;
644     }
645 
646     ret = HksGetDigestLen(alg, &(hash->size));
647     HKS_IF_NOT_SUCC_LOGE(ret, "Get digest len failed!")
648 
649     return ret;
650 }
651 #endif /* HKS_SUPPORT_HASH_C */
652