bootloader_sha.c 1.1 KB

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