2022-05-11 06:49:55 -04:00
/*
* SPDX - FileCopyrightText : 2015 - 2022 Espressif Systems ( Shanghai ) CO LTD
*
* SPDX - License - Identifier : Apache - 2.0
*/
2016-08-17 11:08:22 -04:00
# include "catch.hpp"
# include "nvs.hpp"
2016-11-15 05:24:56 -05:00
# include "nvs_test_api.h"
2019-06-21 08:31:22 -04:00
# include "sdkconfig.h"
2016-08-17 11:08:22 -04:00
# include "spi_flash_emulation.h"
2020-04-26 20:51:31 -04:00
# include "nvs_partition_manager.hpp"
# include "nvs_partition.hpp"
# include "mbedtls/aes.h"
2023-04-16 02:11:11 -04:00
# include "mbedtls/md.h"
2016-08-17 11:08:22 -04:00
# include <sstream>
# include <iostream>
2018-04-02 06:44:59 -04:00
# include <fstream>
2018-11-28 00:26:06 -05:00
# include <dirent.h>
2018-04-18 20:18:05 -04:00
# include <unistd.h>
# include <sys/wait.h>
2018-11-28 00:26:06 -05:00
# include <string.h>
2018-11-05 03:03:04 -05:00
# include <string>
2020-04-26 20:51:31 -04:00
# include "test_fixtures.hpp"
2017-05-12 00:18:08 -04:00
# define TEST_ESP_ERR(rc, res) CHECK((rc) == (res))
# define TEST_ESP_OK(rc) CHECK((rc) == ESP_OK)
2016-08-17 11:08:22 -04:00
stringstream s_perf ;
2023-04-03 16:25:02 -04:00
bool memeq ( void * a , size_t a_len , void * b , size_t b_len )
{
if ( a_len ! = b_len ) {
return false ;
}
return memcmp ( a , b , a_len ) = = 0 ;
}
2017-05-31 00:59:24 -04:00
2023-06-20 09:23:12 -04:00
static void check_nvs_part_gen_args ( SpiFlashEmulator * spi_flash_emulator ,
char const * part_name ,
int size ,
char const * filename ,
bool is_encr ,
nvs_sec_cfg_t * xts_cfg )
{
nvs_handle_t handle ;
2022-06-30 11:53:12 -04:00
2023-06-20 09:23:12 -04:00
esp_partition_t esp_part ;
esp_part . encrypted = false ; // we're not testing generic flash encryption here, only the legacy NVS encryption
esp_part . address = 0 ;
esp_part . size = size * SPI_FLASH_SEC_SIZE ;
strncpy ( esp_part . label , part_name , PART_NAME_MAX_SIZE ) ;
unique_ptr < nvs : : Partition > part ;
2022-06-30 11:53:12 -04:00
2023-06-20 09:23:12 -04:00
if ( is_encr ) {
nvs : : NVSEncryptedPartition * enc_part = new ( std : : nothrow ) nvs : : NVSEncryptedPartition ( & esp_part ) ;
REQUIRE ( enc_part ! = nullptr ) ;
TEST_ESP_OK ( enc_part - > init ( xts_cfg ) ) ;
part . reset ( enc_part ) ;
} else {
part . reset ( new PartitionEmulation ( spi_flash_emulator , 0 , size , part_name ) ) ;
}
2018-10-29 03:49:20 -04:00
2023-06-20 09:23:12 -04:00
TEST_ESP_OK ( nvs : : NVSPartitionManager : : get_instance ( ) - > init_custom ( part . get ( ) , 0 , size ) ) ;
2018-10-29 03:49:20 -04:00
2023-06-20 09:23:12 -04:00
TEST_ESP_OK ( nvs_open_from_partition ( part_name , " dummyNamespace " , NVS_READONLY , & handle ) ) ;
uint8_t u8v ;
TEST_ESP_OK ( nvs_get_u8 ( handle , " dummyU8Key " , & u8v ) ) ;
CHECK ( u8v = = 127 ) ;
int8_t i8v ;
TEST_ESP_OK ( nvs_get_i8 ( handle , " dummyI8Key " , & i8v ) ) ;
CHECK ( i8v = = - 128 ) ;
uint16_t u16v ;
TEST_ESP_OK ( nvs_get_u16 ( handle , " dummyU16Key " , & u16v ) ) ;
CHECK ( u16v = = 32768 ) ;
uint32_t u32v ;
TEST_ESP_OK ( nvs_get_u32 ( handle , " dummyU32Key " , & u32v ) ) ;
CHECK ( u32v = = 4294967295 ) ;
int32_t i32v ;
TEST_ESP_OK ( nvs_get_i32 ( handle , " dummyI32Key " , & i32v ) ) ;
CHECK ( i32v = = - 2147483648 ) ;
2018-10-29 03:49:20 -04:00
2023-06-20 09:23:12 -04:00
char string_buf [ 256 ] ;
const char test_str [ ] = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. \n "
" Fusce quis risus justo. \n "
" Suspendisse egestas in nisi sit amet auctor. \n "
" Pellentesque rhoncus dictum sodales. \n "
" In justo erat, viverra at interdum eget, interdum vel dui. " ;
size_t str_len = sizeof ( test_str ) ;
TEST_ESP_OK ( nvs_get_str ( handle , " dummyStringKey " , string_buf , & str_len ) ) ;
CHECK ( strncmp ( string_buf , test_str , str_len ) = = 0 ) ;
2018-10-29 03:49:20 -04:00
2023-06-20 09:23:12 -04:00
char buf [ 64 ] = { 0 } ;
uint8_t hexdata [ ] = { 0x01 , 0x02 , 0x03 , 0xab , 0xcd , 0xef } ;
size_t buflen = 64 ;
int j ;
TEST_ESP_OK ( nvs_get_blob ( handle , " dummyHex2BinKey " , buf , & buflen ) ) ;
CHECK ( memeq ( buf , buflen , hexdata , sizeof ( hexdata ) ) ) ;
2018-10-29 03:49:20 -04:00
2023-06-20 09:23:12 -04:00
uint8_t base64data [ ] = { ' 1 ' , ' 2 ' , ' 3 ' , ' a ' , ' b ' , ' c ' } ;
TEST_ESP_OK ( nvs_get_blob ( handle , " dummyBase64Key " , buf , & buflen ) ) ;
CHECK ( memeq ( buf , buflen , base64data , sizeof ( base64data ) ) ) ;
2018-10-29 03:49:20 -04:00
2023-06-20 09:23:12 -04:00
buflen = 64 ;
uint8_t hexfiledata [ ] = { 0x01 , 0x23 , 0x45 , 0x67 , 0x89 , 0xab , 0xcd , 0xef } ;
TEST_ESP_OK ( nvs_get_blob ( handle , " hexFileKey " , buf , & buflen ) ) ;
CHECK ( memeq ( buf , buflen , hexfiledata , sizeof ( hexfiledata ) ) ) ;
2018-10-29 03:49:20 -04:00
2023-06-20 09:23:12 -04:00
buflen = 64 ;
const char strfiledata [ 64 ] = " abcdefghijklmnopqrstuvwxyz " ;
TEST_ESP_OK ( nvs_get_str ( handle , " stringFileKey " , buf , & buflen ) ) ;
CHECK ( strcmp ( buf , strfiledata ) = = 0 ) ;
2018-10-29 03:49:20 -04:00
2023-06-20 09:23:12 -04:00
char bin_data [ 5200 ] ;
size_t bin_len = sizeof ( bin_data ) ;
char binfiledata [ 5200 ] ;
ifstream file ;
file . open ( filename ) ;
file . read ( binfiledata , 5200 ) ;
size_t binfile_len = file . gcount ( ) ;
TEST_ESP_OK ( nvs_get_blob ( handle , " binFileKey " , bin_data , & bin_len ) ) ;
CHECK ( memeq ( bin_data , bin_len , binfiledata , binfile_len ) ) ;
2018-10-29 03:49:20 -04:00
2023-06-20 09:23:12 -04:00
file . close ( ) ;
2018-10-29 03:49:20 -04:00
2023-06-20 09:23:12 -04:00
nvs_close ( handle ) ;
2018-10-29 03:49:20 -04:00
2023-06-20 09:23:12 -04:00
TEST_ESP_OK ( nvs_flash_deinit_partition ( part_name ) ) ;
2018-10-29 03:49:20 -04:00
}
2023-06-20 09:23:12 -04:00
static void check_nvs_part_gen_args_mfg ( SpiFlashEmulator * spi_flash_emulator ,
char const * part_name ,
int size ,
char const * filename ,
bool is_encr ,
nvs_sec_cfg_t * xts_cfg )
2018-10-29 03:49:20 -04:00
{
2023-06-20 09:23:12 -04:00
nvs_handle_t handle ;
2018-10-29 03:49:20 -04:00
2023-06-20 09:23:12 -04:00
esp_partition_t esp_part ;
esp_part . encrypted = false ; // we're not testing generic flash encryption here, only the legacy NVS encryption
esp_part . address = 0 ;
esp_part . size = size * SPI_FLASH_SEC_SIZE ;
strncpy ( esp_part . label , part_name , PART_NAME_MAX_SIZE ) ;
unique_ptr < nvs : : Partition > part ;
2018-10-29 03:49:20 -04:00
2023-06-20 09:23:12 -04:00
if ( is_encr ) {
nvs : : NVSEncryptedPartition * enc_part = new ( std : : nothrow ) nvs : : NVSEncryptedPartition ( & esp_part ) ;
REQUIRE ( enc_part ! = nullptr ) ;
TEST_ESP_OK ( enc_part - > init ( xts_cfg ) ) ;
part . reset ( enc_part ) ;
} else {
part . reset ( new PartitionEmulation ( spi_flash_emulator , 0 , size , part_name ) ) ;
}
2018-10-29 03:49:20 -04:00
2023-06-20 09:23:12 -04:00
TEST_ESP_OK ( nvs : : NVSPartitionManager : : get_instance ( ) - > init_custom ( part . get ( ) , 0 , size ) ) ;
2018-10-29 03:49:20 -04:00
2023-06-20 09:23:12 -04:00
TEST_ESP_OK ( nvs_open_from_partition ( part_name , " dummyNamespace " , NVS_READONLY , & handle ) ) ;
uint8_t u8v ;
TEST_ESP_OK ( nvs_get_u8 ( handle , " dummyU8Key " , & u8v ) ) ;
CHECK ( u8v = = 127 ) ;
int8_t i8v ;
TEST_ESP_OK ( nvs_get_i8 ( handle , " dummyI8Key " , & i8v ) ) ;
CHECK ( i8v = = - 128 ) ;
uint16_t u16v ;
TEST_ESP_OK ( nvs_get_u16 ( handle , " dummyU16Key " , & u16v ) ) ;
CHECK ( u16v = = 32768 ) ;
uint32_t u32v ;
TEST_ESP_OK ( nvs_get_u32 ( handle , " dummyU32Key " , & u32v ) ) ;
CHECK ( u32v = = 4294967295 ) ;
int32_t i32v ;
TEST_ESP_OK ( nvs_get_i32 ( handle , " dummyI32Key " , & i32v ) ) ;
CHECK ( i32v = = - 2147483648 ) ;
2018-10-29 03:49:20 -04:00
2023-06-20 09:23:12 -04:00
char buf [ 64 ] = { 0 } ;
size_t buflen = 64 ;
TEST_ESP_OK ( nvs_get_str ( handle , " dummyStringKey " , buf , & buflen ) ) ;
CHECK ( strncmp ( buf , " 0A:0B:0C:0D:0E:0F " , buflen ) = = 0 ) ;
2018-10-29 03:49:20 -04:00
2023-06-20 09:23:12 -04:00
uint8_t hexdata [ ] = { 0x01 , 0x02 , 0x03 , 0xab , 0xcd , 0xef } ;
buflen = 64 ;
int j ;
TEST_ESP_OK ( nvs_get_blob ( handle , " dummyHex2BinKey " , buf , & buflen ) ) ;
CHECK ( memeq ( buf , buflen , hexdata , sizeof ( hexdata ) ) ) ;
2018-10-29 03:49:20 -04:00
2023-06-20 09:23:12 -04:00
uint8_t base64data [ ] = { ' 1 ' , ' 2 ' , ' 3 ' , ' a ' , ' b ' , ' c ' } ;
TEST_ESP_OK ( nvs_get_blob ( handle , " dummyBase64Key " , buf , & buflen ) ) ;
CHECK ( memeq ( buf , buflen , base64data , sizeof ( base64data ) ) ) ;
2018-10-29 03:49:20 -04:00
2023-06-20 09:23:12 -04:00
buflen = 64 ;
uint8_t hexfiledata [ ] = { 0x01 , 0x23 , 0x45 , 0x67 , 0x89 , 0xab , 0xcd , 0xef } ;
TEST_ESP_OK ( nvs_get_blob ( handle , " hexFileKey " , buf , & buflen ) ) ;
CHECK ( memeq ( buf , buflen , hexfiledata , sizeof ( hexfiledata ) ) ) ;
2018-10-29 03:49:20 -04:00
2023-06-20 09:23:12 -04:00
buflen = 64 ;
const char strfiledata [ 64 ] = " abcdefghijklmnopqrstuvwxyz " ;
TEST_ESP_OK ( nvs_get_str ( handle , " stringFileKey " , buf , & buflen ) ) ;
CHECK ( strcmp ( buf , strfiledata ) = = 0 ) ;
2018-10-29 03:49:20 -04:00
2023-06-20 09:23:12 -04:00
char bin_data [ 5200 ] ;
size_t bin_len = sizeof ( bin_data ) ;
char binfiledata [ 5200 ] ;
ifstream file ;
file . open ( filename ) ;
file . read ( binfiledata , 5200 ) ;
size_t binfile_len = file . gcount ( ) ;
TEST_ESP_OK ( nvs_get_blob ( handle , " binFileKey " , bin_data , & bin_len ) ) ;
CHECK ( memeq ( bin_data , bin_len , binfiledata , binfile_len ) ) ;
2018-10-29 03:49:20 -04:00
2023-06-20 09:23:12 -04:00
file . close ( ) ;
2018-10-29 03:49:20 -04:00
2023-06-20 09:23:12 -04:00
nvs_close ( handle ) ;
2018-10-29 03:49:20 -04:00
2023-06-20 09:23:12 -04:00
TEST_ESP_OK ( nvs_flash_deinit_partition ( part_name ) ) ;
2018-10-29 03:49:20 -04:00
}
2018-07-02 07:10:43 -04:00
# if CONFIG_NVS_ENCRYPTION
TEST_CASE ( " check underlying xts code for 32-byte size sector encryption " , " [nvs] " )
{
auto toHex = [ ] ( char ch ) {
if ( ch > = ' 0 ' & & ch < = ' 9 ' )
return ch - ' 0 ' ;
else if ( ch > = ' a ' & & ch < = ' f ' )
return ch - ' a ' + 10 ;
else if ( ch > = ' A ' & & ch < = ' F ' )
return ch - ' A ' + 10 ;
else
return 0 ;
} ;
auto toHexByte = [ toHex ] ( char * c ) {
return 16 * toHex ( c [ 0 ] ) + toHex ( c [ 1 ] ) ;
} ;
auto toHexStream = [ toHexByte ] ( char * src , uint8_t * dest ) {
uint32_t cnt = 0 ;
char * p = src ;
while ( * p ! = ' \0 ' & & * ( p + 1 ) ! = ' \0 ' )
{
dest [ cnt + + ] = toHexByte ( p ) ; p + = 2 ;
}
} ;
uint8_t eky_hex [ 2 * NVS_KEY_SIZE ] ;
2022-10-18 09:06:10 -04:00
uint8_t ptxt_hex [ nvs : : Page : : ENTRY_SIZE ] , ctxt_hex [ nvs : : Page : : ENTRY_SIZE ] , ba_hex [ 16 ] ;
2018-07-02 07:10:43 -04:00
mbedtls_aes_xts_context ectx [ 1 ] ;
mbedtls_aes_xts_context dctx [ 1 ] ;
char eky [ ] [ 2 * NVS_KEY_SIZE + 1 ] = {
" 0000000000000000000000000000000000000000000000000000000000000000 " ,
" 1111111111111111111111111111111111111111111111111111111111111111 "
} ;
char tky [ ] [ 2 * NVS_KEY_SIZE + 1 ] = {
" 0000000000000000000000000000000000000000000000000000000000000000 " ,
" 2222222222222222222222222222222222222222222222222222222222222222 "
} ;
char blk_addr [ ] [ 2 * 16 + 1 ] = {
" 00000000000000000000000000000000 " ,
" 33333333330000000000000000000000 "
} ;
2022-10-18 09:06:10 -04:00
char ptxt [ ] [ 2 * nvs : : Page : : ENTRY_SIZE + 1 ] = {
2018-07-02 07:10:43 -04:00
" 0000000000000000000000000000000000000000000000000000000000000000 " ,
" 4444444444444444444444444444444444444444444444444444444444444444 "
} ;
2022-10-18 09:06:10 -04:00
char ctxt [ ] [ 2 * nvs : : Page : : ENTRY_SIZE + 1 ] = {
2018-07-02 07:10:43 -04:00
" d456b4fc2e620bba6ffbed27b956c9543454dd49ebd8d8ee6f94b65cbe158f73 " ,
" e622334f184bbce129a25b2ac76b3d92abf98e22df5bdd15af471f3db8946a85 "
} ;
mbedtls_aes_xts_init ( ectx ) ;
mbedtls_aes_xts_init ( dctx ) ;
for ( uint8_t cnt = 0 ; cnt < sizeof ( eky ) / sizeof ( eky [ 0 ] ) ; cnt + + ) {
toHexStream ( eky [ cnt ] , eky_hex ) ;
toHexStream ( tky [ cnt ] , & eky_hex [ NVS_KEY_SIZE ] ) ;
toHexStream ( ptxt [ cnt ] , ptxt_hex ) ;
toHexStream ( ctxt [ cnt ] , ctxt_hex ) ;
toHexStream ( blk_addr [ cnt ] , ba_hex ) ;
CHECK ( ! mbedtls_aes_xts_setkey_enc ( ectx , eky_hex , 2 * NVS_KEY_SIZE * 8 ) ) ;
CHECK ( ! mbedtls_aes_xts_setkey_enc ( dctx , eky_hex , 2 * NVS_KEY_SIZE * 8 ) ) ;
2022-10-18 09:06:10 -04:00
CHECK ( ! mbedtls_aes_crypt_xts ( ectx , MBEDTLS_AES_ENCRYPT , nvs : : Page : : ENTRY_SIZE , ba_hex , ptxt_hex , ptxt_hex ) ) ;
2018-07-02 07:10:43 -04:00
2022-10-18 09:06:10 -04:00
CHECK ( ! memcmp ( ptxt_hex , ctxt_hex , nvs : : Page : : ENTRY_SIZE ) ) ;
2018-07-02 07:10:43 -04:00
}
}
TEST_CASE ( " test nvs apis with encryption enabled " , " [nvs] " )
{
2019-04-01 10:13:55 -04:00
nvs_handle_t handle_1 ;
2018-07-02 07:10:43 -04:00
const uint32_t NVS_FLASH_SECTOR = 6 ;
const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3 ;
nvs_sec_cfg_t xts_cfg ;
for ( int count = 0 ; count < NVS_KEY_SIZE ; count + + ) {
xts_cfg . eky [ count ] = 0x11 ;
xts_cfg . tky [ count ] = 0x22 ;
}
2020-04-26 20:51:31 -04:00
EncryptedPartitionFixture fixture ( & xts_cfg , NVS_FLASH_SECTOR , NVS_FLASH_SECTOR_COUNT_MIN ) ;
fixture . emu . randomize ( 100 ) ;
fixture . emu . setBounds ( NVS_FLASH_SECTOR , NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN ) ;
2018-07-02 07:10:43 -04:00
for ( uint16_t i = NVS_FLASH_SECTOR ; i < NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN ; + + i ) {
2020-04-26 20:51:31 -04:00
fixture . emu . erase ( i ) ;
2018-07-02 07:10:43 -04:00
}
2022-10-18 09:06:10 -04:00
TEST_ESP_OK ( nvs : : NVSPartitionManager : : get_instance ( ) - >
2020-04-26 20:51:31 -04:00
init_custom ( & fixture . part , NVS_FLASH_SECTOR , NVS_FLASH_SECTOR_COUNT_MIN ) ) ;
2018-07-02 07:10:43 -04:00
TEST_ESP_ERR ( nvs_open ( " namespace1 " , NVS_READONLY , & handle_1 ) , ESP_ERR_NVS_NOT_FOUND ) ;
TEST_ESP_OK ( nvs_open ( " namespace1 " , NVS_READWRITE , & handle_1 ) ) ;
TEST_ESP_OK ( nvs_set_i32 ( handle_1 , " foo " , 0x12345678 ) ) ;
TEST_ESP_OK ( nvs_set_i32 ( handle_1 , " foo " , 0x23456789 ) ) ;
2019-04-01 10:13:55 -04:00
nvs_handle_t handle_2 ;
2018-07-02 07:10:43 -04:00
TEST_ESP_OK ( nvs_open ( " namespace2 " , NVS_READWRITE , & handle_2 ) ) ;
TEST_ESP_OK ( nvs_set_i32 ( handle_2 , " foo " , 0x3456789a ) ) ;
const char * str = " value 0123456789abcdef0123456789abcdef " ;
TEST_ESP_OK ( nvs_set_str ( handle_2 , " key " , str ) ) ;
int32_t v1 ;
TEST_ESP_OK ( nvs_get_i32 ( handle_1 , " foo " , & v1 ) ) ;
CHECK ( 0x23456789 = = v1 ) ;
int32_t v2 ;
TEST_ESP_OK ( nvs_get_i32 ( handle_2 , " foo " , & v2 ) ) ;
CHECK ( 0x3456789a = = v2 ) ;
char buf [ strlen ( str ) + 1 ] ;
size_t buf_len = sizeof ( buf ) ;
size_t buf_len_needed ;
TEST_ESP_OK ( nvs_get_str ( handle_2 , " key " , NULL , & buf_len_needed ) ) ;
CHECK ( buf_len_needed = = buf_len ) ;
size_t buf_len_short = buf_len - 1 ;
TEST_ESP_ERR ( ESP_ERR_NVS_INVALID_LENGTH , nvs_get_str ( handle_2 , " key " , buf , & buf_len_short ) ) ;
CHECK ( buf_len_short = = buf_len ) ;
size_t buf_len_long = buf_len + 1 ;
TEST_ESP_OK ( nvs_get_str ( handle_2 , " key " , buf , & buf_len_long ) ) ;
CHECK ( buf_len_long = = buf_len ) ;
TEST_ESP_OK ( nvs_get_str ( handle_2 , " key " , buf , & buf_len ) ) ;
CHECK ( 0 = = strcmp ( buf , str ) ) ;
nvs_close ( handle_1 ) ;
nvs_close ( handle_2 ) ;
TEST_ESP_OK ( nvs_flash_deinit ( ) ) ;
}
TEST_CASE ( " test nvs apis for nvs partition generator utility with encryption enabled " , " [nvs_part_gen] " )
{
2018-11-28 00:26:06 -05:00
int status ;
2018-07-02 07:10:43 -04:00
int childpid = fork ( ) ;
if ( childpid = = 0 ) {
2018-11-28 00:26:06 -05:00
exit ( execlp ( " cp " , " cp " ,
" -rf " ,
" ../nvs_partition_generator/testdata " ,
2022-10-18 09:06:10 -04:00
" . " , NULL ) ) ;
2018-07-02 07:10:43 -04:00
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
2020-11-10 18:35:24 -05:00
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
2018-11-28 00:26:06 -05:00
childpid = fork ( ) ;
if ( childpid = = 0 ) {
exit ( execlp ( " python " , " python " ,
2022-10-18 09:06:10 -04:00
" ../nvs_partition_generator/nvs_partition_gen.py " ,
" encrypt " ,
" ../nvs_partition_generator/sample_multipage_blob.csv " ,
" partition_encrypted.bin " ,
" 0x4000 " ,
" --inputkey " ,
" ../nvs_partition_generator/testdata/sample_encryption_keys.bin " ,
" --outdir " ,
" ../nvs_partition_generator " , NULL ) ) ;
2018-11-28 00:26:06 -05:00
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
2020-11-10 18:35:24 -05:00
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
2018-11-28 00:26:06 -05:00
}
2018-07-02 07:10:43 -04:00
}
SpiFlashEmulator emu ( " ../nvs_partition_generator/partition_encrypted.bin " ) ;
2018-11-28 00:26:06 -05:00
2018-10-17 07:38:32 -04:00
nvs_sec_cfg_t cfg ;
2022-10-18 09:06:10 -04:00
for ( int count = 0 ; count < NVS_KEY_SIZE ; count + + ) {
2018-10-17 07:38:32 -04:00
cfg . eky [ count ] = 0x11 ;
cfg . tky [ count ] = 0x22 ;
}
2018-07-02 07:10:43 -04:00
2020-04-26 20:51:31 -04:00
check_nvs_part_gen_args ( & emu , NVS_DEFAULT_PART_NAME , 4 , " ../nvs_partition_generator/testdata/sample_multipage_blob.bin " , true , & cfg ) ;
2018-11-28 00:26:06 -05:00
childpid = fork ( ) ;
if ( childpid = = 0 ) {
exit ( execlp ( " rm " , " rm " ,
" -rf " ,
2022-10-18 09:06:10 -04:00
" testdata " , NULL ) ) ;
2018-11-28 00:26:06 -05:00
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
2020-11-10 18:35:24 -05:00
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
2018-11-28 00:26:06 -05:00
}
2018-10-17 07:38:32 -04:00
}
2020-07-20 04:19:12 -04:00
TEST_CASE ( " test decrypt functionality for encrypted data " , " [nvs_part_gen] " )
{
//retrieving the temporary test data
int status = system ( " cp -rf ../nvs_partition_generator/testdata . " ) ;
CHECK ( status = = 0 ) ;
//encoding data from sample_multipage_blob.csv
status = system ( " python ../nvs_partition_generator/nvs_partition_gen.py generate ../nvs_partition_generator/sample_multipage_blob.csv partition_encoded.bin 0x5000 --outdir ../nvs_partition_generator " ) ;
CHECK ( status = = 0 ) ;
//encrypting data from sample_multipage_blob.csv
status = system ( " python ../nvs_partition_generator/nvs_partition_gen.py encrypt ../nvs_partition_generator/sample_multipage_blob.csv partition_encrypted.bin 0x5000 --inputkey ../nvs_partition_generator/testdata/sample_encryption_keys.bin --outdir ../nvs_partition_generator " ) ;
CHECK ( status = = 0 ) ;
2023-04-16 02:11:11 -04:00
//encrypting data from sample_multipage_blob.csv (hmac-based scheme)
status = system ( " python ../nvs_partition_generator/nvs_partition_gen.py encrypt ../nvs_partition_generator/sample_multipage_blob.csv partition_encrypted_hmac.bin 0x5000 --keygen --key_protect_hmac --kp_hmac_inputkey ../nvs_partition_generator/testdata/sample_hmac_key.bin --outdir ../nvs_partition_generator " ) ;
CHECK ( status = = 0 ) ;
2020-07-20 04:19:12 -04:00
//decrypting data from partition_encrypted.bin
status = system ( " python ../nvs_partition_generator/nvs_partition_gen.py decrypt ../nvs_partition_generator/partition_encrypted.bin ../nvs_partition_generator/testdata/sample_encryption_keys.bin ../nvs_partition_generator/partition_decrypted.bin " ) ;
CHECK ( status = = 0 ) ;
status = system ( " diff ../nvs_partition_generator/partition_decrypted.bin ../nvs_partition_generator/partition_encoded.bin " ) ;
CHECK ( status = = 0 ) ;
2023-04-16 02:11:11 -04:00
//decrypting data from partition_encrypted_hmac.bin
status = system ( " python ../nvs_partition_generator/nvs_partition_gen.py decrypt ../nvs_partition_generator/partition_encrypted_hmac.bin ../nvs_partition_generator/testdata/sample_encryption_keys_hmac.bin ../nvs_partition_generator/partition_decrypted_hmac.bin " ) ;
CHECK ( status = = 0 ) ;
status = system ( " diff ../nvs_partition_generator/partition_decrypted_hmac.bin ../nvs_partition_generator/partition_encoded.bin " ) ;
CHECK ( status = = 0 ) ;
2020-07-20 04:19:12 -04:00
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
//cleaning up the temporary test data
status = system ( " rm -rf testdata " ) ;
CHECK ( status = = 0 ) ;
}
2018-10-17 07:38:32 -04:00
TEST_CASE ( " test nvs apis for nvs partition generator utility with encryption enabled using keygen " , " [nvs_part_gen] " )
{
int childpid = fork ( ) ;
2018-11-21 03:10:24 -05:00
int status ;
2018-10-17 07:38:32 -04:00
2018-11-28 00:26:06 -05:00
if ( childpid = = 0 ) {
exit ( execlp ( " cp " , " cp " ,
" -rf " ,
" ../nvs_partition_generator/testdata " ,
2022-10-18 09:06:10 -04:00
" . " , NULL ) ) ;
2018-10-17 07:38:32 -04:00
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
2020-11-10 18:35:24 -05:00
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
2018-11-28 00:26:06 -05:00
childpid = fork ( ) ;
if ( childpid = = 0 ) {
exit ( execlp ( " rm " , " rm " ,
" -rf " ,
2022-10-18 09:06:10 -04:00
" ../nvs_partition_generator/keys " , NULL ) ) ;
2018-11-28 00:26:06 -05:00
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
2020-11-10 18:35:24 -05:00
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
2018-11-28 00:26:06 -05:00
childpid = fork ( ) ;
if ( childpid = = 0 ) {
exit ( execlp ( " python " , " python " ,
" ../nvs_partition_generator/nvs_partition_gen.py " ,
2019-05-07 06:06:02 -04:00
" encrypt " ,
2018-11-28 00:26:06 -05:00
" ../nvs_partition_generator/sample_multipage_blob.csv " ,
2019-05-07 06:06:02 -04:00
" partition_encrypted_using_keygen.bin " ,
2018-11-28 00:26:06 -05:00
" 0x4000 " ,
" --keygen " ,
2019-05-07 06:06:02 -04:00
" --outdir " ,
2022-10-18 09:06:10 -04:00
" ../nvs_partition_generator " , NULL ) ) ;
2018-11-28 00:26:06 -05:00
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
2020-11-10 18:35:24 -05:00
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
2018-11-28 00:26:06 -05:00
}
}
2018-10-17 07:38:32 -04:00
}
2018-11-28 00:26:06 -05:00
DIR * dir ;
struct dirent * file ;
char * filename ;
char * files ;
char * file_ext ;
2019-05-07 06:06:02 -04:00
dir = opendir ( " ../nvs_partition_generator/keys " ) ;
2022-10-18 09:06:10 -04:00
while ( ( file = readdir ( dir ) ) ! = NULL ) {
2018-11-28 00:26:06 -05:00
filename = file - > d_name ;
files = strrchr ( filename , ' . ' ) ;
2022-10-18 09:06:10 -04:00
if ( files ! = NULL ) {
file_ext = files + 1 ;
if ( strncmp ( file_ext , " bin " , 3 ) = = 0 ) {
2018-11-28 00:26:06 -05:00
break ;
}
}
}
2019-05-07 06:06:02 -04:00
std : : string encr_file = std : : string ( " ../nvs_partition_generator/keys/ " ) + std : : string ( filename ) ;
2018-10-17 07:38:32 -04:00
SpiFlashEmulator emu ( " ../nvs_partition_generator/partition_encrypted_using_keygen.bin " ) ;
2018-11-28 00:26:06 -05:00
2018-10-17 07:38:32 -04:00
char buffer [ 64 ] ;
FILE * fp ;
2022-10-18 09:06:10 -04:00
fp = fopen ( encr_file . c_str ( ) , " rb " ) ;
fread ( buffer , sizeof ( buffer ) , 1 , fp ) ;
2018-10-17 07:38:32 -04:00
fclose ( fp ) ;
nvs_sec_cfg_t cfg ;
2018-07-02 07:10:43 -04:00
2022-10-18 09:06:10 -04:00
for ( int count = 0 ; count < NVS_KEY_SIZE ; count + + ) {
2018-10-17 07:38:32 -04:00
cfg . eky [ count ] = buffer [ count ] & 255 ;
2022-10-18 09:06:10 -04:00
cfg . tky [ count ] = buffer [ count + 32 ] & 255 ;
2018-07-02 07:10:43 -04:00
}
2020-04-26 20:51:31 -04:00
check_nvs_part_gen_args ( & emu , NVS_DEFAULT_PART_NAME , 4 , " ../nvs_partition_generator/testdata/sample_multipage_blob.bin " , true , & cfg ) ;
2018-07-02 07:10:43 -04:00
2018-10-17 07:38:32 -04:00
}
2018-07-02 07:10:43 -04:00
2019-05-07 06:06:02 -04:00
TEST_CASE ( " test nvs apis for nvs partition generator utility with encryption enabled using inputkey " , " [nvs_part_gen] " )
2018-10-17 07:38:32 -04:00
{
int childpid = fork ( ) ;
2018-11-21 03:10:24 -05:00
int status ;
2018-11-28 00:26:06 -05:00
DIR * dir ;
struct dirent * file ;
char * filename ;
char * files ;
char * file_ext ;
2019-05-07 06:06:02 -04:00
dir = opendir ( " ../nvs_partition_generator/keys " ) ;
2022-10-18 09:06:10 -04:00
while ( ( file = readdir ( dir ) ) ! = NULL ) {
2018-11-28 00:26:06 -05:00
filename = file - > d_name ;
files = strrchr ( filename , ' . ' ) ;
2022-10-18 09:06:10 -04:00
if ( files ! = NULL ) {
file_ext = files + 1 ;
if ( strncmp ( file_ext , " bin " , 3 ) = = 0 ) {
2018-11-28 00:26:06 -05:00
break ;
}
}
}
2019-05-07 06:06:02 -04:00
std : : string encr_file = std : : string ( " ../nvs_partition_generator/keys/ " ) + std : : string ( filename ) ;
2018-11-28 00:26:06 -05:00
2022-10-18 09:06:10 -04:00
if ( childpid = = 0 ) {
2018-10-17 07:38:32 -04:00
exit ( execlp ( " python " , " python " ,
2022-10-18 09:06:10 -04:00
" ../nvs_partition_generator/nvs_partition_gen.py " ,
" encrypt " ,
" ../nvs_partition_generator/sample_multipage_blob.csv " ,
" partition_encrypted_using_keyfile.bin " ,
" 0x4000 " ,
" --inputkey " ,
encr_file . c_str ( ) ,
" --outdir " ,
" ../nvs_partition_generator " , NULL ) ) ;
2018-07-02 07:10:43 -04:00
2018-10-17 07:38:32 -04:00
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
2020-11-10 18:35:24 -05:00
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
2018-10-17 07:38:32 -04:00
}
2018-07-02 07:10:43 -04:00
2018-10-17 07:38:32 -04:00
SpiFlashEmulator emu ( " ../nvs_partition_generator/partition_encrypted_using_keyfile.bin " ) ;
2018-07-02 07:10:43 -04:00
2018-10-17 07:38:32 -04:00
char buffer [ 64 ] ;
FILE * fp ;
2018-07-02 07:10:43 -04:00
2022-10-18 09:06:10 -04:00
fp = fopen ( encr_file . c_str ( ) , " rb " ) ;
fread ( buffer , sizeof ( buffer ) , 1 , fp ) ;
2018-07-02 07:10:43 -04:00
2018-10-17 07:38:32 -04:00
fclose ( fp ) ;
2018-07-02 07:10:43 -04:00
2018-10-17 07:38:32 -04:00
nvs_sec_cfg_t cfg ;
2022-10-18 09:06:10 -04:00
for ( int count = 0 ; count < NVS_KEY_SIZE ; count + + ) {
2018-10-17 07:38:32 -04:00
cfg . eky [ count ] = buffer [ count ] & 255 ;
2022-10-18 09:06:10 -04:00
cfg . tky [ count ] = buffer [ count + 32 ] & 255 ;
2018-10-17 07:38:32 -04:00
}
2020-04-26 20:51:31 -04:00
check_nvs_part_gen_args ( & emu , NVS_DEFAULT_PART_NAME , 4 , " ../nvs_partition_generator/testdata/sample_multipage_blob.bin " , true , & cfg ) ;
2018-07-02 07:10:43 -04:00
2018-11-21 03:10:24 -05:00
childpid = fork ( ) ;
if ( childpid = = 0 ) {
exit ( execlp ( " rm " , " rm " ,
2018-11-28 00:26:06 -05:00
" -rf " ,
2022-10-18 09:06:10 -04:00
" ../nvs_partition_generator/keys " , NULL ) ) ;
2018-11-21 03:10:24 -05:00
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
2020-11-10 18:35:24 -05:00
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
2018-11-21 03:10:24 -05:00
2018-11-28 00:26:06 -05:00
childpid = fork ( ) ;
if ( childpid = = 0 ) {
exit ( execlp ( " rm " , " rm " ,
" -rf " ,
2022-10-18 09:06:10 -04:00
" testdata " , NULL ) ) ;
2018-11-28 00:26:06 -05:00
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
2020-11-10 18:35:24 -05:00
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
2018-11-28 00:26:06 -05:00
}
2018-11-21 03:10:24 -05:00
}
2018-10-29 03:49:20 -04:00
}
2023-04-16 02:11:11 -04:00
static void compute_nvs_keys_with_hmac ( nvs_sec_cfg_t * cfg , void * hmac_key )
{
unsigned char key_bytes [ 32 ] = { 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 ,
0x09 , 0x0A , 0x0B , 0x0C , 0x0D , 0x0E , 0x0F , 0x10 ,
0x11 , 0x12 , 0x13 , 0x14 , 0x15 , 0x16 , 0x17 , 0x18 ,
0x19 , 0x1A , 0x1B , 0x1C , 0x1D , 0x1E , 0x1F , 0x20 } ;
if ( hmac_key ! = NULL ) {
memcpy ( key_bytes , hmac_key , 32 ) ;
}
unsigned char ekey_seed [ 32 ] , tkey_seed [ 32 ] ;
for ( unsigned int i = 0 ; i < sizeof ( ekey_seed ) ; i + = 4 ) {
ekey_seed [ i ] = 0x5A ;
ekey_seed [ i + 1 ] = 0x5A ;
ekey_seed [ i + 2 ] = 0xBE ;
ekey_seed [ i + 3 ] = 0xAE ;
}
for ( unsigned int i = 0 ; i < sizeof ( tkey_seed ) ; i + = 4 ) {
tkey_seed [ i ] = 0xA5 ;
tkey_seed [ i + 1 ] = 0xA5 ;
tkey_seed [ i + 2 ] = 0xDE ;
tkey_seed [ i + 3 ] = 0xCE ;
}
const mbedtls_md_type_t alg = MBEDTLS_MD_SHA256 ;
mbedtls_md_context_t ctx ;
mbedtls_md_init ( & ctx ) ;
const mbedtls_md_info_t * info = mbedtls_md_info_from_type ( alg ) ;
mbedtls_md_setup ( & ctx , info , 1 ) ;
mbedtls_md_hmac_starts ( & ctx , key_bytes , sizeof ( key_bytes ) ) ;
mbedtls_md_hmac_update ( & ctx , ekey_seed , sizeof ( ekey_seed ) ) ;
mbedtls_md_hmac_finish ( & ctx , cfg - > eky ) ;
mbedtls_md_hmac_reset ( & ctx ) ;
mbedtls_md_hmac_update ( & ctx , tkey_seed , sizeof ( tkey_seed ) ) ;
mbedtls_md_hmac_finish ( & ctx , cfg - > tky ) ;
assert ( memcmp ( cfg - > eky , cfg - > tky , NVS_KEY_SIZE ) ) ;
mbedtls_md_free ( & ctx ) ;
}
TEST_CASE ( " test nvs apis for nvs partition generator utility with encryption enabled using keygen (user-provided HMAC-key) " , " [nvs_part_gen] " )
{
int childpid = fork ( ) ;
int status ;
if ( childpid = = 0 ) {
exit ( execlp ( " cp " , " cp " ,
" -rf " ,
" ../nvs_partition_generator/testdata " ,
" . " , NULL ) ) ;
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
childpid = fork ( ) ;
if ( childpid = = 0 ) {
exit ( execlp ( " rm " , " rm " ,
" -rf " ,
" ../nvs_partition_generator/keys " , NULL ) ) ;
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
childpid = fork ( ) ;
if ( childpid = = 0 ) {
exit ( execlp ( " python " , " python " ,
" ../nvs_partition_generator/nvs_partition_gen.py " ,
" encrypt " ,
" ../nvs_partition_generator/sample_multipage_blob.csv " ,
" partition_encrypted_using_keygen_hmac.bin " ,
" 0x4000 " ,
" --keygen " ,
" --key_protect_hmac " ,
" --kp_hmac_inputkey " ,
" ../nvs_partition_generator/testdata/sample_hmac_key.bin " ,
" --outdir " ,
" ../nvs_partition_generator " , NULL ) ) ;
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
}
}
}
SpiFlashEmulator emu ( " ../nvs_partition_generator/partition_encrypted_using_keygen_hmac.bin " ) ;
nvs_sec_cfg_t cfg ;
compute_nvs_keys_with_hmac ( & cfg , NULL ) ;
check_nvs_part_gen_args ( & emu , NVS_DEFAULT_PART_NAME , 4 , " ../nvs_partition_generator/testdata/sample_multipage_blob.bin " , true , & cfg ) ;
}
TEST_CASE ( " test nvs apis for nvs partition generator utility with encryption enabled using keygen (dynamically generated HMAC-key) " , " [nvs_part_gen] " )
{
int childpid = fork ( ) ;
int status ;
if ( childpid = = 0 ) {
exit ( execlp ( " cp " , " cp " ,
" -rf " ,
" ../nvs_partition_generator/testdata " ,
" . " , NULL ) ) ;
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
childpid = fork ( ) ;
if ( childpid = = 0 ) {
exit ( execlp ( " rm " , " rm " ,
" -rf " ,
" ../nvs_partition_generator/keys " , NULL ) ) ;
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
childpid = fork ( ) ;
if ( childpid = = 0 ) {
exit ( execlp ( " python " , " python " ,
" ../nvs_partition_generator/nvs_partition_gen.py " ,
" encrypt " ,
" ../nvs_partition_generator/sample_multipage_blob.csv " ,
" partition_encrypted_using_keygen_hmac.bin " ,
" 0x4000 " ,
" --keygen " ,
" --key_protect_hmac " ,
" --kp_hmac_keygen " ,
" --outdir " ,
" ../nvs_partition_generator " , NULL ) ) ;
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
}
}
}
DIR * dir ;
struct dirent * file ;
char * filename ;
char * files ;
char * file_ext ;
char * hmac_key_file ;
dir = opendir ( " ../nvs_partition_generator/keys " ) ;
while ( ( file = readdir ( dir ) ) ! = NULL ) {
filename = file - > d_name ;
file_ext = NULL ;
files = strrchr ( filename , ' . ' ) ;
if ( files ! = NULL ) {
file_ext = files + 1 ;
if ( strncmp ( file_ext , " bin " , 3 ) ! = 0 ) {
continue ;
}
}
if ( strstr ( filename , " hmac " ) ! = NULL ) {
hmac_key_file = filename ;
}
}
std : : string hmac_key_path = std : : string ( " ../nvs_partition_generator/keys/ " ) + std : : string ( hmac_key_file ) ;
SpiFlashEmulator emu ( " ../nvs_partition_generator/partition_encrypted_using_keygen_hmac.bin " ) ;
char hmac_key_buf [ 32 ] ;
FILE * fp ;
fp = fopen ( hmac_key_path . c_str ( ) , " rb " ) ;
fread ( hmac_key_buf , sizeof ( hmac_key_buf ) , 1 , fp ) ;
fclose ( fp ) ;
nvs_sec_cfg_t cfg ;
compute_nvs_keys_with_hmac ( & cfg , hmac_key_buf ) ;
check_nvs_part_gen_args ( & emu , NVS_DEFAULT_PART_NAME , 4 , " ../nvs_partition_generator/testdata/sample_multipage_blob.bin " , true , & cfg ) ;
}
2019-06-20 09:17:59 -04:00
TEST_CASE ( " check and read data from partition generated via manufacturing utility with encryption enabled using sample inputkey " , " [mfg_gen] " )
2018-10-29 03:49:20 -04:00
{
int childpid = fork ( ) ;
int status ;
if ( childpid = = 0 ) {
exit ( execlp ( " bash " , " bash " ,
" -c " ,
" rm -rf ../../../tools/mass_mfg/host_test | \
cp - rf . . / . . / . . / tools / mass_mfg / testdata mfg_testdata | \
cp - rf . . / nvs_partition_generator / testdata . | \
2022-10-18 09:06:10 -04:00
mkdir - p . . / . . / . . / tools / mass_mfg / host_test " , NULL));
2018-10-29 03:49:20 -04:00
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
2020-11-10 18:35:24 -05:00
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
2018-10-29 03:49:20 -04:00
childpid = fork ( ) ;
if ( childpid = = 0 ) {
exit ( execlp ( " python " , " python " ,
" ../../../tools/mass_mfg/mfg_gen.py " ,
2019-06-20 09:17:59 -04:00
" generate " ,
2018-10-29 03:49:20 -04:00
" ../../../tools/mass_mfg/samples/sample_config.csv " ,
" ../../../tools/mass_mfg/samples/sample_values_multipage_blob.csv " ,
" Test " ,
" 0x4000 " ,
" --outdir " ,
" ../../../tools/mass_mfg/host_test " ,
" --version " ,
2019-06-20 09:17:59 -04:00
" 2 " ,
" --inputkey " ,
2022-10-18 09:06:10 -04:00
" mfg_testdata/sample_encryption_keys.bin " , NULL ) ) ;
2018-10-29 03:49:20 -04:00
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
2020-11-10 18:35:24 -05:00
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
2018-10-29 03:49:20 -04:00
childpid = fork ( ) ;
if ( childpid = = 0 ) {
exit ( execlp ( " python " , " python " ,
" ../nvs_partition_generator/nvs_partition_gen.py " ,
2019-06-20 09:17:59 -04:00
" encrypt " ,
2018-10-29 03:49:20 -04:00
" ../../../tools/mass_mfg/host_test/csv/Test-1.csv " ,
" ../nvs_partition_generator/Test-1-partition-encrypted.bin " ,
" 0x4000 " ,
" --version " ,
2019-06-20 09:17:59 -04:00
" 2 " ,
" --inputkey " ,
2022-10-18 09:06:10 -04:00
" testdata/sample_encryption_keys.bin " , NULL ) ) ;
2018-10-29 03:49:20 -04:00
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
2020-11-10 18:35:24 -05:00
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
2018-10-29 03:49:20 -04:00
}
}
}
SpiFlashEmulator emu1 ( " ../../../tools/mass_mfg/host_test/bin/Test-1.bin " ) ;
nvs_sec_cfg_t cfg ;
2022-10-18 09:06:10 -04:00
for ( int count = 0 ; count < NVS_KEY_SIZE ; count + + ) {
2018-10-29 03:49:20 -04:00
cfg . eky [ count ] = 0x11 ;
cfg . tky [ count ] = 0x22 ;
}
2022-06-02 09:29:35 -04:00
check_nvs_part_gen_args_mfg ( & emu1 , NVS_DEFAULT_PART_NAME , 4 , " mfg_testdata/sample_multipage_blob.bin " , true , & cfg ) ;
2018-10-29 03:49:20 -04:00
SpiFlashEmulator emu2 ( " ../nvs_partition_generator/Test-1-partition-encrypted.bin " ) ;
2022-06-02 09:29:35 -04:00
check_nvs_part_gen_args_mfg ( & emu2 , NVS_DEFAULT_PART_NAME , 4 , " testdata/sample_multipage_blob.bin " , true , & cfg ) ;
2018-10-29 03:49:20 -04:00
childpid = fork ( ) ;
if ( childpid = = 0 ) {
exit ( execlp ( " bash " , " bash " ,
" -c " ,
" rm -rf ../../../tools/mass_mfg/host_test | \
rm - rf mfg_testdata | \
2022-10-18 09:06:10 -04:00
rm - rf testdata " , NULL));
2018-10-29 03:49:20 -04:00
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
2020-11-10 18:35:24 -05:00
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
2018-10-29 03:49:20 -04:00
}
}
TEST_CASE ( " check and read data from partition generated via manufacturing utility with encryption enabled using new generated key " , " [mfg_gen] " )
{
int childpid = fork ( ) ;
int status ;
if ( childpid = = 0 ) {
exit ( execlp ( " bash " , " bash " ,
" -c " ,
" rm -rf ../../../tools/mass_mfg/host_test | \
cp - rf . . / . . / . . / tools / mass_mfg / testdata mfg_testdata | \
cp - rf . . / nvs_partition_generator / testdata . | \
2022-10-18 09:06:10 -04:00
mkdir - p . . / . . / . . / tools / mass_mfg / host_test " , NULL));
2018-10-29 03:49:20 -04:00
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
2020-11-10 18:35:24 -05:00
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
2018-10-29 03:49:20 -04:00
childpid = fork ( ) ;
if ( childpid = = 0 ) {
exit ( execlp ( " python " , " python " ,
" ../../../tools/mass_mfg/mfg_gen.py " ,
2019-06-20 09:17:59 -04:00
" generate-key " ,
2018-10-29 03:49:20 -04:00
" --outdir " ,
" ../../../tools/mass_mfg/host_test " ,
" --keyfile " ,
2022-10-18 09:06:10 -04:00
" encr_keys_host_test.bin " , NULL ) ) ;
2018-10-29 03:49:20 -04:00
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
2020-11-10 18:35:24 -05:00
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
2018-10-29 03:49:20 -04:00
childpid = fork ( ) ;
if ( childpid = = 0 ) {
exit ( execlp ( " python " , " python " ,
" ../../../tools/mass_mfg/mfg_gen.py " ,
2019-06-20 09:17:59 -04:00
" generate " ,
2018-10-29 03:49:20 -04:00
" ../../../tools/mass_mfg/samples/sample_config.csv " ,
" ../../../tools/mass_mfg/samples/sample_values_multipage_blob.csv " ,
" Test " ,
" 0x4000 " ,
" --outdir " ,
" ../../../tools/mass_mfg/host_test " ,
" --version " ,
2019-06-20 09:17:59 -04:00
" 2 " ,
" --inputkey " ,
2022-10-18 09:06:10 -04:00
" ../../../tools/mass_mfg/host_test/keys/encr_keys_host_test.bin " , NULL ) ) ;
2018-10-29 03:49:20 -04:00
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
2020-11-10 18:35:24 -05:00
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
2018-10-29 03:49:20 -04:00
childpid = fork ( ) ;
if ( childpid = = 0 ) {
exit ( execlp ( " python " , " python " ,
" ../nvs_partition_generator/nvs_partition_gen.py " ,
2019-06-20 09:17:59 -04:00
" encrypt " ,
2018-10-29 03:49:20 -04:00
" ../../../tools/mass_mfg/host_test/csv/Test-1.csv " ,
" ../nvs_partition_generator/Test-1-partition-encrypted.bin " ,
" 0x4000 " ,
" --version " ,
2019-06-20 09:17:59 -04:00
" 2 " ,
" --inputkey " ,
2022-10-18 09:06:10 -04:00
" ../../../tools/mass_mfg/host_test/keys/encr_keys_host_test.bin " , NULL ) ) ;
2018-10-29 03:49:20 -04:00
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
2020-11-10 18:35:24 -05:00
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
2018-10-29 03:49:20 -04:00
}
}
}
}
SpiFlashEmulator emu1 ( " ../../../tools/mass_mfg/host_test/bin/Test-1.bin " ) ;
char buffer [ 64 ] ;
FILE * fp ;
2022-10-18 09:06:10 -04:00
fp = fopen ( " ../../../tools/mass_mfg/host_test/keys/encr_keys_host_test.bin " , " rb " ) ;
fread ( buffer , sizeof ( buffer ) , 1 , fp ) ;
2018-10-29 03:49:20 -04:00
fclose ( fp ) ;
nvs_sec_cfg_t cfg ;
2022-10-18 09:06:10 -04:00
for ( int count = 0 ; count < NVS_KEY_SIZE ; count + + ) {
2018-10-29 03:49:20 -04:00
cfg . eky [ count ] = buffer [ count ] & 255 ;
2022-10-18 09:06:10 -04:00
cfg . tky [ count ] = buffer [ count + 32 ] & 255 ;
2018-10-29 03:49:20 -04:00
}
2022-06-02 09:29:35 -04:00
check_nvs_part_gen_args_mfg ( & emu1 , NVS_DEFAULT_PART_NAME , 4 , " mfg_testdata/sample_multipage_blob.bin " , true , & cfg ) ;
2018-10-29 03:49:20 -04:00
SpiFlashEmulator emu2 ( " ../nvs_partition_generator/Test-1-partition-encrypted.bin " ) ;
2022-06-02 09:29:35 -04:00
check_nvs_part_gen_args_mfg ( & emu2 , NVS_DEFAULT_PART_NAME , 4 , " testdata/sample_multipage_blob.bin " , true , & cfg ) ;
2018-10-29 03:49:20 -04:00
childpid = fork ( ) ;
if ( childpid = = 0 ) {
exit ( execlp ( " bash " , " bash " ,
" -c " ,
" rm -rf keys | \
rm - rf mfg_testdata | \
rm - rf testdata | \
2022-10-18 09:06:10 -04:00
rm - rf . . / . . / . . / tools / mass_mfg / host_test " , NULL));
2018-10-29 03:49:20 -04:00
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
2020-11-10 18:35:24 -05:00
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
2018-10-29 03:49:20 -04:00
}
2018-07-02 07:10:43 -04:00
}
2023-04-16 02:11:11 -04:00
TEST_CASE ( " check and read data from partition generated via manufacturing utility with encryption enabled using new generated key (user-provided HMAC-key) " , " [mfg_gen] " )
{
int childpid = fork ( ) ;
int status ;
if ( childpid = = 0 ) {
exit ( execlp ( " bash " , " bash " ,
" -c " ,
" rm -rf ../../../tools/mass_mfg/host_test | \
cp - rf . . / . . / . . / tools / mass_mfg / testdata mfg_testdata | \
cp - rf . . / nvs_partition_generator / testdata . | \
mkdir - p . . / . . / . . / tools / mass_mfg / host_test " , NULL));
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
childpid = fork ( ) ;
if ( childpid = = 0 ) {
exit ( execlp ( " python " , " python " ,
" ../../../tools/mass_mfg/mfg_gen.py " ,
" generate " ,
" ../../../tools/mass_mfg/samples/sample_config.csv " ,
" ../../../tools/mass_mfg/samples/sample_values_multipage_blob.csv " ,
" Test " ,
" 0x4000 " ,
" --version " ,
" 2 " ,
" --keygen " ,
" --key_protect_hmac " ,
" --kp_hmac_inputkey " ,
" mfg_testdata/sample_hmac_key.bin " ,
" --outdir " ,
" ../../../tools/mass_mfg/host_test " , NULL ) ) ;
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
childpid = fork ( ) ;
if ( childpid = = 0 ) {
exit ( execlp ( " python " , " python " ,
" ../nvs_partition_generator/nvs_partition_gen.py " ,
" encrypt " ,
" ../../../tools/mass_mfg/host_test/csv/Test-1.csv " ,
" ../nvs_partition_generator/Test-1-partition-encrypted-hmac.bin " ,
" 0x4000 " ,
" --version " ,
" 2 " ,
" --keygen " ,
" --key_protect_hmac " ,
" --kp_hmac_inputkey " ,
" mfg_testdata/sample_hmac_key.bin " , NULL ) ) ;
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
}
}
}
SpiFlashEmulator emu1 ( " ../../../tools/mass_mfg/host_test/bin/Test-1.bin " ) ;
nvs_sec_cfg_t cfg ;
compute_nvs_keys_with_hmac ( & cfg , NULL ) ;
check_nvs_part_gen_args_mfg ( & emu1 , NVS_DEFAULT_PART_NAME , 4 , " mfg_testdata/sample_multipage_blob.bin " , true , & cfg ) ;
SpiFlashEmulator emu2 ( " ../nvs_partition_generator/Test-1-partition-encrypted-hmac.bin " ) ;
check_nvs_part_gen_args_mfg ( & emu2 , NVS_DEFAULT_PART_NAME , 4 , " testdata/sample_multipage_blob.bin " , true , & cfg ) ;
childpid = fork ( ) ;
if ( childpid = = 0 ) {
exit ( execlp ( " bash " , " bash " ,
" -c " ,
" rm -rf ../../../tools/mass_mfg/host_test | \
rm - rf mfg_testdata | \
rm - rf testdata " , NULL));
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
}
}
TEST_CASE ( " check and read data from partition generated via manufacturing utility with encryption enabled using new generated key (dynamically generated HMAC-key) " , " [mfg_gen] " )
{
int childpid = fork ( ) ;
int status ;
if ( childpid = = 0 ) {
exit ( execlp ( " bash " , " bash " ,
" -c " ,
" rm -rf ../../../tools/mass_mfg/host_test | \
cp - rf . . / . . / . . / tools / mass_mfg / testdata mfg_testdata | \
cp - rf . . / nvs_partition_generator / testdata . | \
mkdir - p . . / . . / . . / tools / mass_mfg / host_test " , NULL));
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
childpid = fork ( ) ;
if ( childpid = = 0 ) {
exit ( execlp ( " python " , " python " ,
" ../../../tools/mass_mfg/mfg_gen.py " ,
" generate-key " ,
" --outdir " ,
" ../../../tools/mass_mfg/host_test " ,
" --key_protect_hmac " ,
" --kp_hmac_keygen " ,
" --kp_hmac_keyfile " ,
" hmac_key_host_test.bin " ,
" --keyfile " ,
" encr_keys_host_test.bin " , NULL ) ) ;
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
childpid = fork ( ) ;
if ( childpid = = 0 ) {
exit ( execlp ( " python " , " python " ,
" ../../../tools/mass_mfg/mfg_gen.py " ,
" generate " ,
" ../../../tools/mass_mfg/samples/sample_config.csv " ,
" ../../../tools/mass_mfg/samples/sample_values_multipage_blob.csv " ,
" Test " ,
" 0x4000 " ,
" --outdir " ,
" ../../../tools/mass_mfg/host_test " ,
" --version " ,
" 2 " ,
" --inputkey " ,
" ../../../tools/mass_mfg/host_test/keys/encr_keys_host_test.bin " , NULL ) ) ;
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
childpid = fork ( ) ;
if ( childpid = = 0 ) {
exit ( execlp ( " python " , " python " ,
" ../nvs_partition_generator/nvs_partition_gen.py " ,
" encrypt " ,
" ../../../tools/mass_mfg/host_test/csv/Test-1.csv " ,
" ../nvs_partition_generator/Test-1-partition-encrypted-hmac.bin " ,
" 0x4000 " ,
" --version " ,
" 2 " ,
" --inputkey " ,
" ../../../tools/mass_mfg/host_test/keys/encr_keys_host_test.bin " , NULL ) ) ;
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
}
}
}
}
SpiFlashEmulator emu1 ( " ../../../tools/mass_mfg/host_test/bin/Test-1.bin " ) ;
char hmac_key_buf [ 32 ] ;
FILE * fp ;
fp = fopen ( " ../../../tools/mass_mfg/host_test/keys/hmac_key_host_test.bin " , " rb " ) ;
fread ( hmac_key_buf , sizeof ( hmac_key_buf ) , 1 , fp ) ;
fclose ( fp ) ;
nvs_sec_cfg_t cfg ;
compute_nvs_keys_with_hmac ( & cfg , hmac_key_buf ) ;
check_nvs_part_gen_args_mfg ( & emu1 , NVS_DEFAULT_PART_NAME , 4 , " mfg_testdata/sample_multipage_blob.bin " , true , & cfg ) ;
SpiFlashEmulator emu2 ( " ../nvs_partition_generator/Test-1-partition-encrypted-hmac.bin " ) ;
check_nvs_part_gen_args_mfg ( & emu2 , NVS_DEFAULT_PART_NAME , 4 , " testdata/sample_multipage_blob.bin " , true , & cfg ) ;
childpid = fork ( ) ;
if ( childpid = = 0 ) {
exit ( execlp ( " bash " , " bash " ,
" -c " ,
" rm -rf keys | \
rm - rf mfg_testdata | \
rm - rf testdata | \
rm - rf . . / . . / . . / tools / mass_mfg / host_test " , NULL));
} else {
CHECK ( childpid > 0 ) ;
waitpid ( childpid , & status , 0 ) ;
CHECK ( WEXITSTATUS ( status ) = = 0 ) ;
}
}
2018-07-02 07:10:43 -04:00
# endif