1 /*
2 * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #include "bootloader_sha.h"
7 #include "bootloader_flash_priv.h"
8 #include <stdbool.h>
9 #include <string.h>
10 #include <assert.h>
11 #include <sys/param.h>
12 #include <mbedtls/sha256.h>
13
bootloader_sha256_start(void)14 bootloader_sha256_handle_t bootloader_sha256_start(void)
15 {
16 mbedtls_sha256_context *ctx = (mbedtls_sha256_context *)malloc(sizeof(mbedtls_sha256_context));
17 if (!ctx) {
18 return NULL;
19 }
20 mbedtls_sha256_init(ctx);
21 int ret = mbedtls_sha256_starts_ret(ctx, false);
22 if (ret != 0) {
23 return NULL;
24 }
25 return ctx;
26 }
27
bootloader_sha256_data(bootloader_sha256_handle_t handle,const void * data,size_t data_len)28 void bootloader_sha256_data(bootloader_sha256_handle_t handle, const void *data, size_t data_len)
29 {
30 assert(handle != NULL);
31 mbedtls_sha256_context *ctx = (mbedtls_sha256_context *)handle;
32 int ret = mbedtls_sha256_update_ret(ctx, data, data_len);
33 assert(ret == 0);
34 (void)ret;
35 }
36
bootloader_sha256_finish(bootloader_sha256_handle_t handle,uint8_t * digest)37 void bootloader_sha256_finish(bootloader_sha256_handle_t handle, uint8_t *digest)
38 {
39 assert(handle != NULL);
40 mbedtls_sha256_context *ctx = (mbedtls_sha256_context *)handle;
41 if (digest != NULL) {
42 int ret = mbedtls_sha256_finish_ret(ctx, digest);
43 assert(ret == 0);
44 (void)ret;
45 }
46 mbedtls_sha256_free(ctx);
47 free(handle);
48 handle = NULL;
49 }
50