1 // Copyright 2017 Espressif Systems (Shanghai) PTE LTD
2 //
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 #include "bootloader_sha.h"
15 #include "bootloader_flash_priv.h"
16 #include <stdbool.h>
17 #include <string.h>
18 #include <assert.h>
19 #include <sys/param.h>
20 #include <mbedtls/sha256.h>
21
bootloader_sha256_start(void)22 bootloader_sha256_handle_t bootloader_sha256_start(void)
23 {
24 mbedtls_sha256_context *ctx = (mbedtls_sha256_context *)malloc(sizeof(mbedtls_sha256_context));
25 if (!ctx) {
26 return NULL;
27 }
28 mbedtls_sha256_init(ctx);
29 int ret = mbedtls_sha256_starts(ctx, false);
30 if (ret != 0) {
31 return NULL;
32 }
33 return ctx;
34 }
35
bootloader_sha256_data(bootloader_sha256_handle_t handle,const void * data,size_t data_len)36 void bootloader_sha256_data(bootloader_sha256_handle_t handle, const void *data, size_t data_len)
37 {
38 assert(handle != NULL);
39 mbedtls_sha256_context *ctx = (mbedtls_sha256_context *)handle;
40 int ret = mbedtls_sha256_update(ctx, data, data_len);
41 assert(ret == 0);
42 }
43
bootloader_sha256_finish(bootloader_sha256_handle_t handle,uint8_t * digest)44 void bootloader_sha256_finish(bootloader_sha256_handle_t handle, uint8_t *digest)
45 {
46 assert(handle != NULL);
47 mbedtls_sha256_context *ctx = (mbedtls_sha256_context *)handle;
48 if (digest != NULL) {
49 int ret = mbedtls_sha256_finish(ctx, digest);
50 assert(ret == 0);
51 }
52 mbedtls_sha256_free(ctx);
53 free(handle);
54 handle = NULL;
55 }
56