test_flash_encryption.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. static void test_encrypted_write(size_t offset, const uint8_t *data, size_t length);
  11. static void verify_erased_flash(size_t offset, size_t length);
  12. static size_t start;
  13. static void setup_tests()
  14. {
  15. if (start == 0) {
  16. const esp_partition_t *part = get_test_data_partition();
  17. start = part->address;
  18. printf("Test data partition @ 0x%x\n", start);
  19. }
  20. }
  21. TEST_CASE("test 16 byte encrypted writes", "[spi_flash]")
  22. {
  23. setup_tests();
  24. if (!esp_flash_encryption_enabled()) {
  25. TEST_IGNORE_MESSAGE("flash encryption disabled, skipping spi_flash_write_encrypted() tests");
  26. }
  27. TEST_ASSERT_EQUAL_HEX(ESP_OK,
  28. spi_flash_erase_sector(start / SPI_FLASH_SEC_SIZE));
  29. uint8_t fortyeight_bytes[0x30]; // 0, 1, 2, 3, 4... 47
  30. for(int i = 0; i < sizeof(fortyeight_bytes); i++) {
  31. fortyeight_bytes[i] = i;
  32. }
  33. /* Verify unaligned start or length fails */
  34. TEST_ASSERT_EQUAL_HEX(ESP_ERR_INVALID_ARG,
  35. spi_flash_write_encrypted(start+1, fortyeight_bytes, 32));
  36. TEST_ASSERT_EQUAL_HEX(ESP_ERR_INVALID_SIZE,
  37. spi_flash_write_encrypted(start, fortyeight_bytes, 15));
  38. /* ensure nothing happened to the flash yet */
  39. verify_erased_flash(start, 0x20);
  40. /* Write 32 byte block, this is the "normal" encrypted write */
  41. test_encrypted_write(start, fortyeight_bytes, 0x20);
  42. verify_erased_flash(start + 0x20, 0x20);
  43. /* Slip in an unaligned spi_flash_read_encrypted() test */
  44. uint8_t buf[0x10];
  45. spi_flash_read_encrypted(start+0x10, buf, 0x10);
  46. TEST_ASSERT_EQUAL_HEX8_ARRAY(fortyeight_bytes+0x10, buf, 16);
  47. /* Write 16 bytes unaligned */
  48. test_encrypted_write(start + 0x30, fortyeight_bytes, 0x10);
  49. /* the 16 byte regions before and after the 16 bytes we just wrote should still be 0xFF */
  50. verify_erased_flash(start + 0x20, 0x10);
  51. verify_erased_flash(start + 0x40, 0x10);
  52. /* Write 48 bytes starting at a 32-byte aligned offset */
  53. test_encrypted_write(start + 0x40, fortyeight_bytes, 0x30);
  54. /* 16 bytes after this write should still be 0xFF -unencrypted- */
  55. verify_erased_flash(start + 0x70, 0x10);
  56. /* Write 48 bytes starting at a 16-byte aligned offset */
  57. test_encrypted_write(start + 0x90, fortyeight_bytes, 0x30);
  58. /* 16 bytes after this write should still be 0xFF -unencrypted- */
  59. verify_erased_flash(start + 0x120, 0x10);
  60. }
  61. static void test_encrypted_write(size_t offset, const uint8_t *data, size_t length)
  62. {
  63. uint8_t readback[length];
  64. printf("encrypt %d bytes at 0x%x\n", length, offset);
  65. TEST_ASSERT_EQUAL_HEX(ESP_OK,
  66. spi_flash_write_encrypted(offset, data, length));
  67. TEST_ASSERT_EQUAL_HEX(ESP_OK,
  68. spi_flash_read_encrypted(offset, readback, length));
  69. TEST_ASSERT_EQUAL_HEX8_ARRAY(data, readback, length);
  70. }
  71. static void verify_erased_flash(size_t offset, size_t length)
  72. {
  73. uint8_t readback[length];
  74. printf("verify erased 0x%x - 0x%x\n", offset, offset + length);
  75. TEST_ASSERT_EQUAL_HEX(ESP_OK,
  76. spi_flash_read(offset, readback, length));
  77. for (int i = 0; i < length; i++) {
  78. char message[32];
  79. sprintf(message, "unerased flash @ 0x%08x", offset + i);
  80. TEST_ASSERT_EQUAL_HEX_MESSAGE(0xFF, readback[i], message);
  81. }
  82. }