bootloader_sha.c 916 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2022 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. #include "esp32c2/rom/sha.h"
  12. static SHA_CTX ctx;
  13. bootloader_sha256_handle_t bootloader_sha256_start()
  14. {
  15. // Enable SHA hardware
  16. ets_sha_enable();
  17. ets_sha_init(&ctx, SHA2_256);
  18. return &ctx; // Meaningless non-NULL value
  19. }
  20. void bootloader_sha256_data(bootloader_sha256_handle_t handle, const void *data, size_t data_len)
  21. {
  22. assert(handle != NULL);
  23. assert(data_len % 4 == 0);
  24. ets_sha_update(&ctx, data, data_len, false);
  25. }
  26. void bootloader_sha256_finish(bootloader_sha256_handle_t handle, uint8_t *digest)
  27. {
  28. assert(handle != NULL);
  29. if (digest == NULL) {
  30. bzero(&ctx, sizeof(ctx));
  31. return;
  32. }
  33. ets_sha_finish(&ctx, digest);
  34. }