bootloader_sha.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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_handle_t bootloader_sha256_start(void)
  14. {
  15. mbedtls_sha256_context *ctx = (mbedtls_sha256_context *)malloc(sizeof(mbedtls_sha256_context));
  16. if (!ctx) {
  17. return NULL;
  18. }
  19. mbedtls_sha256_init(ctx);
  20. int ret = mbedtls_sha256_starts(ctx, false);
  21. if (ret != 0) {
  22. return NULL;
  23. }
  24. return ctx;
  25. }
  26. void bootloader_sha256_data(bootloader_sha256_handle_t handle, const void *data, size_t data_len)
  27. {
  28. assert(handle != NULL);
  29. mbedtls_sha256_context *ctx = (mbedtls_sha256_context *)handle;
  30. int ret = mbedtls_sha256_update(ctx, data, data_len);
  31. assert(ret == 0);
  32. (void)ret;
  33. }
  34. void bootloader_sha256_finish(bootloader_sha256_handle_t handle, uint8_t *digest)
  35. {
  36. assert(handle != NULL);
  37. mbedtls_sha256_context *ctx = (mbedtls_sha256_context *)handle;
  38. if (digest != NULL) {
  39. int ret = mbedtls_sha256_finish(ctx, digest);
  40. assert(ret == 0);
  41. (void)ret;
  42. }
  43. mbedtls_sha256_free(ctx);
  44. free(handle);
  45. handle = NULL;
  46. }