test_flash_encryption.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include <stdio.h>
  2. #include <freertos/FreeRTOS.h>
  3. #include <freertos/task.h>
  4. #include <freertos/semphr.h>
  5. #include <unity.h>
  6. #include <test_utils.h>
  7. #include <esp_spi_flash.h>
  8. #include <esp_attr.h>
  9. #include <esp_flash_encrypt.h>
  10. #ifdef CONFIG_SECURE_FLASH_ENC_ENABLED
  11. static void test_encrypted_write(size_t offset, const uint8_t *data, size_t length);
  12. static void test_encrypted_write_new_impl(size_t offset, const uint8_t *data, size_t length);
  13. static void verify_erased_flash(size_t offset, size_t length);
  14. static size_t start;
  15. static void setup_tests()
  16. {
  17. if (start == 0) {
  18. const esp_partition_t *part = get_test_data_partition();
  19. start = part->address;
  20. printf("Test data partition @ 0x%x\n", start);
  21. }
  22. }
  23. TEST_CASE("test 16 byte encrypted writes", "[flash_encryption][test_env=UT_T1_FlashEncryption]")
  24. {
  25. setup_tests();
  26. TEST_ASSERT_EQUAL_HEX(ESP_OK,
  27. spi_flash_erase_sector(start / SPI_FLASH_SEC_SIZE));
  28. uint8_t fortyeight_bytes[0x30]; // 0, 1, 2, 3, 4... 47
  29. for(int i = 0; i < sizeof(fortyeight_bytes); i++) {
  30. fortyeight_bytes[i] = i;
  31. }
  32. /* Verify unaligned start or length fails */
  33. TEST_ASSERT_EQUAL_HEX(ESP_ERR_INVALID_ARG,
  34. spi_flash_write_encrypted(start+1, fortyeight_bytes, 32));
  35. TEST_ASSERT_EQUAL_HEX(ESP_ERR_INVALID_SIZE,
  36. spi_flash_write_encrypted(start, fortyeight_bytes, 15));
  37. /* ensure nothing happened to the flash yet */
  38. verify_erased_flash(start, 0x20);
  39. /* Write 32 byte block, this is the "normal" encrypted write */
  40. test_encrypted_write(start, fortyeight_bytes, 0x20);
  41. verify_erased_flash(start + 0x20, 0x20);
  42. /* Slip in an unaligned spi_flash_read_encrypted() test */
  43. uint8_t buf[0x10];
  44. spi_flash_read_encrypted(start+0x10, buf, 0x10);
  45. TEST_ASSERT_EQUAL_HEX8_ARRAY(fortyeight_bytes+0x10, buf, 16);
  46. /* Write 16 bytes unaligned */
  47. test_encrypted_write(start + 0x30, fortyeight_bytes, 0x10);
  48. /* the 16 byte regions before and after the 16 bytes we just wrote should still be 0xFF */
  49. verify_erased_flash(start + 0x20, 0x10);
  50. verify_erased_flash(start + 0x40, 0x10);
  51. /* Write 48 bytes starting at a 32-byte aligned offset */
  52. test_encrypted_write(start + 0x40, fortyeight_bytes, 0x30);
  53. /* 16 bytes after this write should still be 0xFF -unencrypted- */
  54. verify_erased_flash(start + 0x70, 0x10);
  55. /* Write 48 bytes starting at a 16-byte aligned offset */
  56. test_encrypted_write(start + 0x90, fortyeight_bytes, 0x30);
  57. /* 16 bytes after this write should still be 0xFF -unencrypted- */
  58. verify_erased_flash(start + 0x120, 0x10);
  59. }
  60. static void test_encrypted_write(size_t offset, const uint8_t *data, size_t length)
  61. {
  62. uint8_t readback[length];
  63. printf("encrypt %d bytes at 0x%x\n", length, offset);
  64. TEST_ASSERT_EQUAL_HEX(ESP_OK,
  65. spi_flash_write_encrypted(offset, data, length));
  66. TEST_ASSERT_EQUAL_HEX(ESP_OK,
  67. spi_flash_read_encrypted(offset, readback, length));
  68. TEST_ASSERT_EQUAL_HEX8_ARRAY(data, readback, length);
  69. }
  70. TEST_CASE("test 16 byte encrypted writes (esp_flash)", "[flash_encryption][esp_flash_enc][test_env=UT_T1_FlashEncryption]")
  71. {
  72. setup_tests();
  73. TEST_ASSERT_EQUAL_HEX(ESP_OK,
  74. spi_flash_erase_sector(start / SPI_FLASH_SEC_SIZE));
  75. uint8_t fortyeight_bytes[0x30]; // 0, 1, 2, 3, 4... 47
  76. for(int i = 0; i < sizeof(fortyeight_bytes); i++) {
  77. fortyeight_bytes[i] = i;
  78. }
  79. /* Verify unaligned start or length fails */
  80. TEST_ASSERT_EQUAL_HEX(ESP_ERR_INVALID_ARG,
  81. esp_flash_write_encrypted(NULL, start+1, fortyeight_bytes, 32));
  82. TEST_ASSERT_EQUAL_HEX(ESP_ERR_INVALID_SIZE,
  83. esp_flash_write_encrypted(NULL, start, fortyeight_bytes, 15));
  84. /* ensure nothing happened to the flash yet */
  85. verify_erased_flash(start, 0x20);
  86. /* Write 32 byte block, this is the "normal" encrypted write */
  87. test_encrypted_write_new_impl(start, fortyeight_bytes, 0x20);
  88. verify_erased_flash(start + 0x20, 0x20);
  89. /* Slip in an unaligned esp_flash_read_encrypted() test */
  90. uint8_t buf[0x10];
  91. esp_flash_read_encrypted(NULL, start+0x10, buf, 0x10);
  92. TEST_ASSERT_EQUAL_HEX8_ARRAY(fortyeight_bytes+0x10, buf, 16);
  93. /* Write 16 bytes unaligned */
  94. test_encrypted_write_new_impl(start + 0x30, fortyeight_bytes, 0x10);
  95. /* the 16 byte regions before and after the 16 bytes we just wrote should still be 0xFF */
  96. verify_erased_flash(start + 0x20, 0x10);
  97. verify_erased_flash(start + 0x40, 0x10);
  98. /* Write 48 bytes starting at a 32-byte aligned offset */
  99. test_encrypted_write_new_impl(start + 0x40, fortyeight_bytes, 0x30);
  100. /* 16 bytes after this write should still be 0xFF -unencrypted- */
  101. verify_erased_flash(start + 0x70, 0x10);
  102. /* Write 48 bytes starting at a 16-byte aligned offset */
  103. test_encrypted_write_new_impl(start + 0x90, fortyeight_bytes, 0x30);
  104. /* 16 bytes after this write should still be 0xFF -unencrypted- */
  105. verify_erased_flash(start + 0x120, 0x10);
  106. }
  107. static void test_encrypted_write_new_impl(size_t offset, const uint8_t *data, size_t length)
  108. {
  109. uint8_t readback[length];
  110. printf("encrypt %d bytes at 0x%x\n", length, offset);
  111. TEST_ASSERT_EQUAL_HEX(ESP_OK,
  112. esp_flash_write_encrypted(NULL, offset, data, length));
  113. TEST_ASSERT_EQUAL_HEX(ESP_OK,
  114. esp_flash_read_encrypted(NULL, offset, readback, length));
  115. TEST_ASSERT_EQUAL_HEX8_ARRAY(data, readback, length);
  116. }
  117. static void verify_erased_flash(size_t offset, size_t length)
  118. {
  119. uint8_t readback[length];
  120. printf("verify erased 0x%x - 0x%x\n", offset, offset + length);
  121. TEST_ASSERT_EQUAL_HEX(ESP_OK,
  122. spi_flash_read(offset, readback, length));
  123. for (int i = 0; i < length; i++) {
  124. char message[32];
  125. sprintf(message, "unerased flash @ 0x%08x", offset + i);
  126. TEST_ASSERT_EQUAL_HEX_MESSAGE(0xFF, readback[i], message);
  127. }
  128. }
  129. #endif // CONFIG_SECURE_FLASH_ENC_ENABLED