Skip to content

Instantly share code, notes, and snippets.

@Aietes
Last active June 17, 2020 09:40
Show Gist options
  • Save Aietes/73dc56fa37b2d8f4fa9f00ede32760db to your computer and use it in GitHub Desktop.
Save Aietes/73dc56fa37b2d8f4fa9f00ede32760db to your computer and use it in GitHub Desktop.
#include <nvs.h>
#include <nvs_flash.h>
/**
* Testing dedicated NVS partition access
*
* Used partition table
* # Name, Type, SubType, Offset, Size, Flags
* nvs, data, nvs, 0x9000, 0x3000
* void, data, nvs, 0xC000, 0x2000
* nvs_key, data, nvs_keys, 0xE000, 0x1000
* phy_init, data, phy, 0xF000, 0x1000
* factory, app, factory, 0x10000, 0x3F0000
*
* 1. Find key partition and NVS data partition using esp_partition_find* API functions.
* 2. Populate the nvs_sec_cfg_t struct using the nvs_flash_read_security_cfg or nvs_flash_generate_keys API functions.
* 3. Initialise NVS flash partition using the nvs_flash_secure_init or nvs_flash_secure_init_partition API functions.
* 4. Open a namespace using the nvs_open or nvs_open_from_part API functions.
* 5. Perform NVS read/write operations using nvs_get_* or nvs_set_*.
* 6. Deinitialise an NVS partition using nvs_flash_deinit.
*/
// 1. Find key partition and NVS data partition using esp_partition_find* API functions.
const esp_partition_t *void_nvs_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, "void");
if (void_nvs_partition)
{
// 2. Populate the nvs_sec_cfg_t struct using the nvs_flash_read_security_cfg or nvs_flash_generate_keys API functions.
nvs_sec_cfg_t cfg;
if ((nvs_flash_read_security_cfg(void_nvs_partition, &cfg) == ESP_OK) || (nvs_flash_generate_keys(void_nvs_partition, &cfg) == ESP_OK))
{
// 3. Initialise NVS flash partition using the nvs_flash_secure_init or nvs_flash_secure_init_partition API functions.
if (nvs_flash_secure_init_partition("void", &cfg) == ESP_OK)
{
nvs_handle handle = 0;
if (nvs_set_blob(handle, "key_test", crypto->hwsafe_public, sizeof(crypto->hwsafe_public)) == ESP_OK)
{
Serial.printf("main.cpp/setup:\t\t Public key written to VOID NVS partition.\n");
}
nvs_commit(handle);
byte pub_test[32];
size_t size = sizeof(pub_test);
nvs_get_blob(handle, "key_test", pub_test, &size);
Serial.printf("main.cpp/setup:\t\t Public key read from to VOID NVS partition %s.\n", crypto->bytes_to_hex(pub_test, 32).c_str());
}
else
{
Serial.printf("main.cpp/setup:\t\t ! NVS secure init partition failed.\n");
}
}
else
{
Serial.printf("main.cpp/setup:\t\t ! NVS encryption failed.\n");
}
}
/**
* Testing dedicated NVS partition access
*
* 1. Find key partition and NVS data partition using esp_partition_find* API functions.
* 2. Populate the nvs_sec_cfg_t struct using the nvs_flash_read_security_cfg or nvs_flash_generate_keys API functions.
* 3. Initialise NVS flash partition using the nvs_flash_secure_init or nvs_flash_secure_init_partition API functions.
* 4. Open a namespace using the nvs_open or nvs_open_from_part API functions.
* 5. Perform NVS read/write operations using nvs_get_* or nvs_set_*.
* 6. Deinitialise an NVS partition using nvs_flash_deinit.
*/
if (nvs_flash_init_partition("void") == ESP_OK)
{
nvs_handle handle = 0;
if (nvs_open_from_partition("void", "void", NVS_READWRITE, &handle) == ESP_OK)
{
if (nvs_set_blob(handle, "key_test", crypto->hwsafe_public, sizeof(crypto->hwsafe_public)) == ESP_OK)
{
Serial.printf("main.cpp/setup:\t\t Public key written to VOID NVS partition.\n");
}
nvs_commit(handle);
byte pub_test[32];
size_t size = sizeof(pub_test);
nvs_get_blob(handle, "key_test", pub_test, &size);
Serial.printf("main.cpp/setup:\t\t Public key read from to VOID NVS partition %s.\n", crypto->bytes_to_hex(pub_test, 32).c_str());
}
}
else
{
Serial.printf("main.cpp/setup:\t\t NVS open failed.\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment