bootloader_sha.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2017 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "bootloader_sha.h"
  14. #include <stdbool.h>
  15. #include <string.h>
  16. #include <assert.h>
  17. #include <sys/param.h>
  18. #include <mbedtls/sha256.h>
  19. bootloader_sha256_handle_t bootloader_sha256_start(void)
  20. {
  21. mbedtls_sha256_context *ctx = (mbedtls_sha256_context *)malloc(sizeof(mbedtls_sha256_context));
  22. if (!ctx) {
  23. return NULL;
  24. }
  25. mbedtls_sha256_init(ctx);
  26. int ret = mbedtls_sha256_starts_ret(ctx, false);
  27. if (ret != 0) {
  28. return NULL;
  29. }
  30. return ctx;
  31. }
  32. void bootloader_sha256_data(bootloader_sha256_handle_t handle, const void *data, size_t data_len)
  33. {
  34. assert(handle != NULL);
  35. mbedtls_sha256_context *ctx = (mbedtls_sha256_context *)handle;
  36. int ret = mbedtls_sha256_update_ret(ctx, data, data_len);
  37. assert(ret == 0);
  38. }
  39. void bootloader_sha256_finish(bootloader_sha256_handle_t handle, uint8_t *digest)
  40. {
  41. assert(handle != NULL);
  42. mbedtls_sha256_context *ctx = (mbedtls_sha256_context *)handle;
  43. if (digest != NULL) {
  44. int ret = mbedtls_sha256_finish_ret(ctx, digest);
  45. assert(ret == 0);
  46. }
  47. mbedtls_sha256_free(ctx);
  48. free(handle);
  49. handle = NULL;
  50. }