secure_boot_signatures.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2015-2016 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 "sdkconfig.h"
  14. #include "bootloader_flash.h"
  15. #include "bootloader_sha.h"
  16. #include "esp_log.h"
  17. #include "esp_image_format.h"
  18. #include "esp32s2/rom/secure_boot.h"
  19. static const char* TAG = "secure_boot";
  20. #define DIGEST_LEN 32
  21. esp_err_t esp_secure_boot_verify_signature(uint32_t src_addr, uint32_t length)
  22. {
  23. ets_secure_boot_key_digests_t trusted_keys = { 0 };
  24. uint8_t digest[DIGEST_LEN];
  25. uint8_t verified_digest[DIGEST_LEN] = { 0 }; /* Note: this function doesn't do any anti-FI checks on this buffer */
  26. const uint8_t *data;
  27. ESP_LOGD(TAG, "verifying signature src_addr 0x%x length 0x%x", src_addr, length);
  28. if ((src_addr + length) % 4096 != 0) {
  29. ESP_LOGE(TAG, "addr 0x%x length 0x%x doesn't end on a sector boundary", src_addr, length);
  30. return ESP_ERR_INVALID_ARG;
  31. }
  32. data = bootloader_mmap(src_addr, length + sizeof(struct ets_secure_boot_sig_block));
  33. if (data == NULL) {
  34. ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", src_addr, length+sizeof(ets_secure_boot_signature_t));
  35. return ESP_FAIL;
  36. }
  37. // Calculate digest of main image
  38. #ifdef BOOTLOADER_BUILD
  39. bootloader_sha256_handle_t handle = bootloader_sha256_start();
  40. bootloader_sha256_data(handle, data, length);
  41. bootloader_sha256_finish(handle, digest);
  42. #else
  43. /* Use thread-safe esp-idf SHA function */
  44. esp_sha(SHA2_256, data, length, digest);
  45. #endif
  46. int r = ets_secure_boot_read_key_digests(&trusted_keys);
  47. if (r == ETS_OK) {
  48. const ets_secure_boot_signature_t *sig = (const ets_secure_boot_signature_t *)(data + length);
  49. // TODO: calling this function in IDF app context is unsafe
  50. r = ets_secure_boot_verify_signature(sig, digest, &trusted_keys, verified_digest);
  51. }
  52. bootloader_munmap(data);
  53. return (r == ETS_OK) ? ESP_OK : ESP_FAIL;
  54. }
  55. esp_err_t esp_secure_boot_verify_rsa_signature_block(const ets_secure_boot_signature_t *sig_block, const uint8_t *image_digest, uint8_t *verified_digest)
  56. {
  57. ets_secure_boot_key_digests_t trusted_keys;
  58. int r = ets_secure_boot_read_key_digests(&trusted_keys);
  59. if (r != 0) {
  60. ESP_LOGE(TAG, "No trusted key digests were found in efuse!");
  61. } else {
  62. ESP_LOGD(TAG, "Verifying with RSA-PSS...");
  63. // TODO: calling this function in IDF app context is unsafe
  64. r = ets_secure_boot_verify_signature(sig_block, image_digest, &trusted_keys, verified_digest);
  65. }
  66. return (r == 0) ? ESP_OK : ESP_ERR_IMAGE_INVALID;
  67. }