secure_boot_signatures.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 "esp_secure_boot.h"
  19. #include "uECC.h"
  20. #ifdef BOOTLOADER_BUILD
  21. #include "rom/sha.h"
  22. typedef SHA_CTX sha_context;
  23. #else
  24. #include "hwcrypto/sha.h"
  25. #endif
  26. static const char* TAG = "secure_boot";
  27. extern const uint8_t signature_verification_key_start[] asm("_binary_signature_verification_key_bin_start");
  28. extern const uint8_t signature_verification_key_end[] asm("_binary_signature_verification_key_bin_end");
  29. #define SIGNATURE_VERIFICATION_KEYLEN 64
  30. #define DIGEST_LEN 32
  31. esp_err_t esp_secure_boot_verify_signature(uint32_t src_addr, uint32_t length)
  32. {
  33. uint8_t digest[DIGEST_LEN];
  34. const uint8_t *data;
  35. const esp_secure_boot_sig_block_t *sigblock;
  36. ESP_LOGD(TAG, "verifying signature src_addr 0x%x length 0x%x", src_addr, length);
  37. data = bootloader_mmap(src_addr, length + sizeof(esp_secure_boot_sig_block_t));
  38. if(data == NULL) {
  39. ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", src_addr, length+sizeof(esp_secure_boot_sig_block_t));
  40. return ESP_FAIL;
  41. }
  42. // Calculate digest of main image
  43. #ifdef BOOTLOADER_BUILD
  44. bootloader_sha256_handle_t handle = bootloader_sha256_start();
  45. bootloader_sha256_data(handle, data, length);
  46. bootloader_sha256_finish(handle, digest);
  47. #else
  48. /* Use thread-safe esp-idf SHA function */
  49. esp_sha(SHA2_256, data, length, digest);
  50. #endif
  51. // Map the signature block and verify the signature
  52. sigblock = (const esp_secure_boot_sig_block_t *)(data + length);
  53. esp_err_t err = esp_secure_boot_verify_signature_block(sigblock, digest);
  54. bootloader_munmap(data);
  55. return err;
  56. }
  57. esp_err_t esp_secure_boot_verify_signature_block(const esp_secure_boot_sig_block_t *sig_block, const uint8_t *image_digest)
  58. {
  59. ptrdiff_t keylen;
  60. bool is_valid;
  61. keylen = signature_verification_key_end - signature_verification_key_start;
  62. if(keylen != SIGNATURE_VERIFICATION_KEYLEN) {
  63. ESP_LOGE(TAG, "Embedded public verification key has wrong length %d", keylen);
  64. return ESP_FAIL;
  65. }
  66. if (sig_block->version != 0) {
  67. ESP_LOGE(TAG, "image has invalid signature version field 0x%08x", sig_block->version);
  68. return ESP_FAIL;
  69. }
  70. is_valid = uECC_verify(signature_verification_key_start,
  71. image_digest,
  72. DIGEST_LEN,
  73. sig_block->signature,
  74. uECC_secp256r1());
  75. return is_valid ? ESP_OK : ESP_ERR_IMAGE_INVALID;
  76. }