2021-05-10 04:56:51 +02:00
|
|
|
/*
|
|
|
|
|
* SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
2019-04-16 17:01:31 +08:00
|
|
|
#include "bootloader_sha.h"
|
2020-07-13 03:23:12 +08:00
|
|
|
#include "bootloader_flash_priv.h"
|
2019-04-16 17:01:31 +08:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
#include <mbedtls/sha256.h>
|
|
|
|
|
|
2019-07-16 16:33:30 +07:00
|
|
|
bootloader_sha256_handle_t bootloader_sha256_start(void)
|
2019-04-16 17:01:31 +08:00
|
|
|
{
|
|
|
|
|
mbedtls_sha256_context *ctx = (mbedtls_sha256_context *)malloc(sizeof(mbedtls_sha256_context));
|
|
|
|
|
if (!ctx) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
mbedtls_sha256_init(ctx);
|
2021-05-28 18:43:32 +05:30
|
|
|
int ret = mbedtls_sha256_starts(ctx, false);
|
2019-04-16 17:01:31 +08:00
|
|
|
if (ret != 0) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
return ctx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void bootloader_sha256_data(bootloader_sha256_handle_t handle, const void *data, size_t data_len)
|
|
|
|
|
{
|
|
|
|
|
assert(handle != NULL);
|
|
|
|
|
mbedtls_sha256_context *ctx = (mbedtls_sha256_context *)handle;
|
2021-05-28 18:43:32 +05:30
|
|
|
int ret = mbedtls_sha256_update(ctx, data, data_len);
|
2019-04-16 17:01:31 +08:00
|
|
|
assert(ret == 0);
|
2021-02-12 16:01:05 +11:00
|
|
|
(void)ret;
|
2019-04-16 17:01:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void bootloader_sha256_finish(bootloader_sha256_handle_t handle, uint8_t *digest)
|
|
|
|
|
{
|
|
|
|
|
assert(handle != NULL);
|
|
|
|
|
mbedtls_sha256_context *ctx = (mbedtls_sha256_context *)handle;
|
|
|
|
|
if (digest != NULL) {
|
2021-05-28 18:43:32 +05:30
|
|
|
int ret = mbedtls_sha256_finish(ctx, digest);
|
2019-04-16 17:01:31 +08:00
|
|
|
assert(ret == 0);
|
2021-02-12 16:01:05 +11:00
|
|
|
(void)ret;
|
2019-04-16 17:01:31 +08:00
|
|
|
}
|
|
|
|
|
mbedtls_sha256_free(ctx);
|
|
|
|
|
free(handle);
|
2020-03-04 00:28:18 +05:30
|
|
|
handle = NULL;
|
2019-04-16 17:01:31 +08:00
|
|
|
}
|