2017-01-04 06:53:15 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <freertos/FreeRTOS.h>
|
|
|
|
#include <freertos/task.h>
|
|
|
|
#include <freertos/semphr.h>
|
|
|
|
|
|
|
|
#include <unity.h>
|
2017-02-21 02:40:42 +00:00
|
|
|
#include <test_utils.h>
|
2017-01-04 06:53:15 +00:00
|
|
|
#include <esp_spi_flash.h>
|
|
|
|
#include <esp_attr.h>
|
|
|
|
#include <esp_flash_encrypt.h>
|
|
|
|
|
2019-08-23 04:37:55 +00:00
|
|
|
#ifdef CONFIG_SECURE_FLASH_ENC_ENABLED
|
|
|
|
|
2017-01-04 06:53:15 +00:00
|
|
|
static void test_encrypted_write(size_t offset, const uint8_t *data, size_t length);
|
2019-09-05 10:45:45 +00:00
|
|
|
static void test_encrypted_write_new_impl(size_t offset, const uint8_t *data, size_t length);
|
2017-01-04 06:53:15 +00:00
|
|
|
static void verify_erased_flash(size_t offset, size_t length);
|
|
|
|
|
2017-02-21 02:40:42 +00:00
|
|
|
static size_t start;
|
|
|
|
|
2019-07-16 09:33:30 +00:00
|
|
|
static void setup_tests(void)
|
2017-02-21 02:40:42 +00:00
|
|
|
{
|
|
|
|
if (start == 0) {
|
|
|
|
const esp_partition_t *part = get_test_data_partition();
|
|
|
|
start = part->address;
|
|
|
|
printf("Test data partition @ 0x%x\n", start);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 04:37:55 +00:00
|
|
|
TEST_CASE("test 16 byte encrypted writes", "[flash_encryption][test_env=UT_T1_FlashEncryption]")
|
2017-01-04 06:53:15 +00:00
|
|
|
{
|
2017-02-21 02:40:42 +00:00
|
|
|
setup_tests();
|
|
|
|
|
2017-01-04 06:53:15 +00:00
|
|
|
TEST_ASSERT_EQUAL_HEX(ESP_OK,
|
2017-02-21 02:40:42 +00:00
|
|
|
spi_flash_erase_sector(start / SPI_FLASH_SEC_SIZE));
|
2017-01-04 06:53:15 +00:00
|
|
|
|
|
|
|
uint8_t fortyeight_bytes[0x30]; // 0, 1, 2, 3, 4... 47
|
|
|
|
for(int i = 0; i < sizeof(fortyeight_bytes); i++) {
|
|
|
|
fortyeight_bytes[i] = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify unaligned start or length fails */
|
|
|
|
TEST_ASSERT_EQUAL_HEX(ESP_ERR_INVALID_ARG,
|
2017-02-21 02:40:42 +00:00
|
|
|
spi_flash_write_encrypted(start+1, fortyeight_bytes, 32));
|
2017-01-04 06:53:15 +00:00
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_HEX(ESP_ERR_INVALID_SIZE,
|
2017-02-21 02:40:42 +00:00
|
|
|
spi_flash_write_encrypted(start, fortyeight_bytes, 15));
|
2017-01-04 06:53:15 +00:00
|
|
|
|
|
|
|
/* ensure nothing happened to the flash yet */
|
2017-02-21 02:40:42 +00:00
|
|
|
verify_erased_flash(start, 0x20);
|
2017-01-04 06:53:15 +00:00
|
|
|
|
|
|
|
/* Write 32 byte block, this is the "normal" encrypted write */
|
2017-02-21 02:40:42 +00:00
|
|
|
test_encrypted_write(start, fortyeight_bytes, 0x20);
|
|
|
|
verify_erased_flash(start + 0x20, 0x20);
|
2017-01-04 06:53:15 +00:00
|
|
|
|
|
|
|
/* Slip in an unaligned spi_flash_read_encrypted() test */
|
|
|
|
uint8_t buf[0x10];
|
2017-02-21 02:40:42 +00:00
|
|
|
spi_flash_read_encrypted(start+0x10, buf, 0x10);
|
2017-01-04 06:53:15 +00:00
|
|
|
TEST_ASSERT_EQUAL_HEX8_ARRAY(fortyeight_bytes+0x10, buf, 16);
|
|
|
|
|
|
|
|
/* Write 16 bytes unaligned */
|
2017-02-21 02:40:42 +00:00
|
|
|
test_encrypted_write(start + 0x30, fortyeight_bytes, 0x10);
|
2017-01-04 06:53:15 +00:00
|
|
|
/* the 16 byte regions before and after the 16 bytes we just wrote should still be 0xFF */
|
2017-02-21 02:40:42 +00:00
|
|
|
verify_erased_flash(start + 0x20, 0x10);
|
|
|
|
verify_erased_flash(start + 0x40, 0x10);
|
2017-01-04 06:53:15 +00:00
|
|
|
|
|
|
|
/* Write 48 bytes starting at a 32-byte aligned offset */
|
2017-02-21 02:40:42 +00:00
|
|
|
test_encrypted_write(start + 0x40, fortyeight_bytes, 0x30);
|
2017-01-04 06:53:15 +00:00
|
|
|
/* 16 bytes after this write should still be 0xFF -unencrypted- */
|
2017-02-21 02:40:42 +00:00
|
|
|
verify_erased_flash(start + 0x70, 0x10);
|
2017-01-04 06:53:15 +00:00
|
|
|
|
|
|
|
/* Write 48 bytes starting at a 16-byte aligned offset */
|
2017-02-21 02:40:42 +00:00
|
|
|
test_encrypted_write(start + 0x90, fortyeight_bytes, 0x30);
|
2017-01-04 06:53:15 +00:00
|
|
|
/* 16 bytes after this write should still be 0xFF -unencrypted- */
|
2017-02-21 02:40:42 +00:00
|
|
|
verify_erased_flash(start + 0x120, 0x10);
|
2017-01-04 06:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_encrypted_write(size_t offset, const uint8_t *data, size_t length)
|
|
|
|
{
|
|
|
|
uint8_t readback[length];
|
|
|
|
printf("encrypt %d bytes at 0x%x\n", length, offset);
|
|
|
|
TEST_ASSERT_EQUAL_HEX(ESP_OK,
|
|
|
|
spi_flash_write_encrypted(offset, data, length));
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_HEX(ESP_OK,
|
|
|
|
spi_flash_read_encrypted(offset, readback, length));
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_HEX8_ARRAY(data, readback, length);
|
|
|
|
}
|
|
|
|
|
2019-09-05 10:45:45 +00:00
|
|
|
TEST_CASE("test 16 byte encrypted writes (esp_flash)", "[flash_encryption][esp_flash_enc][test_env=UT_T1_FlashEncryption]")
|
|
|
|
{
|
|
|
|
setup_tests();
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_HEX(ESP_OK,
|
|
|
|
spi_flash_erase_sector(start / SPI_FLASH_SEC_SIZE));
|
|
|
|
|
|
|
|
uint8_t fortyeight_bytes[0x30]; // 0, 1, 2, 3, 4... 47
|
|
|
|
for(int i = 0; i < sizeof(fortyeight_bytes); i++) {
|
|
|
|
fortyeight_bytes[i] = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify unaligned start or length fails */
|
|
|
|
TEST_ASSERT_EQUAL_HEX(ESP_ERR_INVALID_ARG,
|
|
|
|
esp_flash_write_encrypted(NULL, start+1, fortyeight_bytes, 32));
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_HEX(ESP_ERR_INVALID_SIZE,
|
|
|
|
esp_flash_write_encrypted(NULL, start, fortyeight_bytes, 15));
|
|
|
|
|
|
|
|
/* ensure nothing happened to the flash yet */
|
|
|
|
verify_erased_flash(start, 0x20);
|
|
|
|
|
|
|
|
/* Write 32 byte block, this is the "normal" encrypted write */
|
|
|
|
test_encrypted_write_new_impl(start, fortyeight_bytes, 0x20);
|
|
|
|
verify_erased_flash(start + 0x20, 0x20);
|
|
|
|
|
|
|
|
/* Slip in an unaligned esp_flash_read_encrypted() test */
|
|
|
|
uint8_t buf[0x10];
|
|
|
|
esp_flash_read_encrypted(NULL, start+0x10, buf, 0x10);
|
|
|
|
TEST_ASSERT_EQUAL_HEX8_ARRAY(fortyeight_bytes+0x10, buf, 16);
|
|
|
|
|
|
|
|
/* Write 16 bytes unaligned */
|
|
|
|
test_encrypted_write_new_impl(start + 0x30, fortyeight_bytes, 0x10);
|
|
|
|
/* the 16 byte regions before and after the 16 bytes we just wrote should still be 0xFF */
|
|
|
|
verify_erased_flash(start + 0x20, 0x10);
|
|
|
|
verify_erased_flash(start + 0x40, 0x10);
|
|
|
|
|
|
|
|
/* Write 48 bytes starting at a 32-byte aligned offset */
|
|
|
|
test_encrypted_write_new_impl(start + 0x40, fortyeight_bytes, 0x30);
|
|
|
|
/* 16 bytes after this write should still be 0xFF -unencrypted- */
|
|
|
|
verify_erased_flash(start + 0x70, 0x10);
|
|
|
|
|
|
|
|
/* Write 48 bytes starting at a 16-byte aligned offset */
|
|
|
|
test_encrypted_write_new_impl(start + 0x90, fortyeight_bytes, 0x30);
|
|
|
|
/* 16 bytes after this write should still be 0xFF -unencrypted- */
|
|
|
|
verify_erased_flash(start + 0x120, 0x10);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_encrypted_write_new_impl(size_t offset, const uint8_t *data, size_t length)
|
|
|
|
{
|
|
|
|
uint8_t readback[length];
|
|
|
|
printf("encrypt %d bytes at 0x%x\n", length, offset);
|
|
|
|
TEST_ASSERT_EQUAL_HEX(ESP_OK,
|
|
|
|
esp_flash_write_encrypted(NULL, offset, data, length));
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_HEX(ESP_OK,
|
|
|
|
esp_flash_read_encrypted(NULL, offset, readback, length));
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_HEX8_ARRAY(data, readback, length);
|
|
|
|
}
|
|
|
|
|
2017-01-04 06:53:15 +00:00
|
|
|
static void verify_erased_flash(size_t offset, size_t length)
|
|
|
|
{
|
|
|
|
uint8_t readback[length];
|
|
|
|
printf("verify erased 0x%x - 0x%x\n", offset, offset + length);
|
|
|
|
TEST_ASSERT_EQUAL_HEX(ESP_OK,
|
|
|
|
spi_flash_read(offset, readback, length));
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
char message[32];
|
|
|
|
sprintf(message, "unerased flash @ 0x%08x", offset + i);
|
|
|
|
TEST_ASSERT_EQUAL_HEX_MESSAGE(0xFF, readback[i], message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 04:37:55 +00:00
|
|
|
#endif // CONFIG_SECURE_FLASH_ENC_ENABLED
|