check_firmware.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <Arduino.h>
  2. #include "check_firmware.h"
  3. #include "monocypher.h"
  4. #include "parameters.h"
  5. #include <string.h>
  6. #include "util.h"
  7. bool CheckFirmware::check_partition(const uint8_t *flash, uint32_t flash_len,
  8. const uint8_t *lead_bytes, uint32_t lead_length,
  9. const app_descriptor_t *ad, const uint8_t public_key[32])
  10. {
  11. crypto_check_ctx ctx {};
  12. crypto_check_ctx_abstract *actx = (crypto_check_ctx_abstract*)&ctx;
  13. crypto_check_init(actx, ad->sign_signature, public_key);
  14. if (lead_length > 0) {
  15. crypto_check_update(actx, lead_bytes, lead_length);
  16. }
  17. crypto_check_update(actx, &flash[lead_length], flash_len-lead_length);
  18. return crypto_check_final(actx) == 0;
  19. }
  20. bool CheckFirmware::check_OTA_partition(const esp_partition_t *part, const uint8_t *lead_bytes, uint32_t lead_length, uint32_t &board_id)
  21. {
  22. Serial.printf("Checking partition %s\n", part->label);
  23. spi_flash_mmap_handle_t handle;
  24. const void *ptr = nullptr;
  25. auto ret = esp_partition_mmap(part, 0, part->size, SPI_FLASH_MMAP_DATA, &ptr, &handle);
  26. if (ret != ESP_OK) {
  27. Serial.printf("mmap failed\n");
  28. return false;
  29. }
  30. const uint8_t sig_rev[] = APP_DESCRIPTOR_REV;
  31. uint8_t sig[8];
  32. for (uint8_t i=0; i<8; i++) {
  33. sig[i] = sig_rev[7-i];
  34. }
  35. const app_descriptor_t *ad = (app_descriptor_t *)memmem(ptr, part->size, sig, sizeof(sig));
  36. if (ad == nullptr) {
  37. Serial.printf("app_descriptor not found\n");
  38. spi_flash_munmap(handle);
  39. return false;
  40. }
  41. Serial.printf("app descriptor at 0x%x size=%u id=%u\n", unsigned(ad)-unsigned(ptr), ad->image_size, ad->board_id);
  42. const uint32_t img_len = uint32_t(uintptr_t(ad) - uintptr_t(ptr));
  43. if (ad->image_size != img_len) {
  44. Serial.printf("app_descriptor bad size %u\n", ad->image_size);
  45. spi_flash_munmap(handle);
  46. return false;
  47. }
  48. board_id = ad->board_id;
  49. if (g.no_public_keys()) {
  50. Serial.printf("No public keys - accepting firmware\n");
  51. spi_flash_munmap(handle);
  52. return true;
  53. }
  54. for (uint8_t i=0; i<MAX_PUBLIC_KEYS; i++) {
  55. uint8_t key[32];
  56. if (!g.get_public_key(i, key)) {
  57. continue;
  58. }
  59. if (check_partition((const uint8_t *)ptr, img_len, lead_bytes, lead_length, ad, key)) {
  60. Serial.printf("check firmware good for key %u\n", i);
  61. spi_flash_munmap(handle);
  62. return true;
  63. }
  64. Serial.printf("check failed key %u\n", i);
  65. }
  66. spi_flash_munmap(handle);
  67. Serial.printf("firmware failed checks\n");
  68. return false;
  69. }
  70. bool CheckFirmware::check_OTA_next(const uint8_t *lead_bytes, uint32_t lead_length)
  71. {
  72. const auto *running_part = esp_ota_get_running_partition();
  73. if (running_part == nullptr) {
  74. Serial.printf("No running OTA partition\n");
  75. return false;
  76. }
  77. const auto *part = esp_ota_get_next_update_partition(running_part);
  78. if (part == nullptr) {
  79. Serial.printf("No next OTA partition\n");
  80. return false;
  81. }
  82. uint32_t board_id=0;
  83. bool sig_ok = check_OTA_partition(part, lead_bytes, lead_length, board_id);
  84. // if app descriptor has a board ID and the ID is wrong then reject
  85. if (board_id != 0 && board_id != BOARD_ID) {
  86. return false;
  87. }
  88. if (g.lock_level == 0) {
  89. // if unlocked then accept any firmware
  90. return true;
  91. }
  92. return sig_ok;
  93. }
  94. bool CheckFirmware::check_OTA_running(void)
  95. {
  96. const auto *running_part = esp_ota_get_running_partition();
  97. if (running_part == nullptr) {
  98. Serial.printf("No running OTA partition\n");
  99. return false;
  100. }
  101. uint32_t board_id=0;
  102. return check_OTA_partition(running_part, nullptr, 0, board_id);
  103. }
  104. esp_err_t esp_partition_read_raw(const esp_partition_t* partition,
  105. size_t src_offset, void* dst, size_t size);