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 <stdbool.h>
8 #include <string.h>
9 #include <assert.h>
10 #include <sys/param.h>
11
12 #include "esp32s2/rom/sha.h"
13
14 static SHA_CTX ctx;
15
16 // Words per SHA256 block
17 // static const size_t BLOCK_WORDS = (64/sizeof(uint32_t));
18 // Words in final SHA256 digest
19 // static const size_t DIGEST_WORDS = (32/sizeof(uint32_t));
20
bootloader_sha256_start()21 bootloader_sha256_handle_t bootloader_sha256_start()
22 {
23 // Enable SHA hardware
24 ets_sha_enable();
25 ets_sha_init(&ctx, SHA2_256);
26 return &ctx; // Meaningless non-NULL value
27 }
28
bootloader_sha256_data(bootloader_sha256_handle_t handle,const void * data,size_t data_len)29 void bootloader_sha256_data(bootloader_sha256_handle_t handle, const void *data, size_t data_len)
30 {
31 assert(handle != NULL);
32 assert(data_len % 4 == 0);
33 ets_sha_update(&ctx, data, data_len, false);
34 }
35
bootloader_sha256_finish(bootloader_sha256_handle_t handle,uint8_t * digest)36 void bootloader_sha256_finish(bootloader_sha256_handle_t handle, uint8_t *digest)
37 {
38 assert(handle != NULL);
39
40 if (digest == NULL) {
41 bzero(&ctx, sizeof(ctx));
42 return;
43 }
44 ets_sha_finish(&ctx, digest);
45 }
46