Skip to content

Commit e073fb6

Browse files
author
David Saada
committed
Change Device key to work with KVStore
1 parent e9c25e2 commit e073fb6

File tree

3 files changed

+156
-126
lines changed

3 files changed

+156
-126
lines changed

features/device_key/TESTS/device_key/functionality/main.cpp

Lines changed: 76 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,21 @@
1616

1717
#include "DeviceKey.h"
1818
#include "utest/utest.h"
19+
#include "mbed_error.h"
1920
#include "unity/unity.h"
2021
#include "greentea-client/test_env.h"
21-
#include "nvstore.h"
22-
23-
#if !NVSTORE_ENABLED
24-
#error [NOT_SUPPORTED] NVSTORE needs to be enabled for this test
25-
#endif
22+
#include "KVStore.h"
23+
#include "KVMap.h"
24+
#include "kv_config.h"
25+
#include "TDBStore.h"
2626

2727
using namespace utest::v1;
2828
using namespace mbed;
2929

30+
#if !DEVICEKEY_ENABLED
31+
#error [NOT_SUPPORTED] DeviceKey needs to be enabled for this test
32+
#endif
33+
3034
#define MSG_VALUE_DUMMY "0"
3135
#define MSG_VALUE_LEN 32
3236
#define MSG_KEY_LEN 32
@@ -62,7 +66,6 @@ int inject_dummy_rot_key()
6266
void generate_derived_key_long_consistency_test()
6367
{
6468
static char key[MSG_KEY_LEN + 1] = { };
65-
static char value[MSG_VALUE_LEN + 1] = { };
6669

6770
strcpy(key, MSG_KEY_DEVICE_TEST_STEP1);
6871
generate_derived_key_consistency_16_byte_key_long_consistency_test(key);
@@ -85,16 +88,19 @@ void generate_derived_key_consistency_16_byte_key_long_consistency_test(char *ke
8588
unsigned char empty_buffer[DEVICE_KEY_16BYTE];
8689
unsigned char salt[] = "Once upon a time, I worked for the circus and I lived in Omaha.";
8790
int key_type = DEVICE_KEY_16BYTE;
88-
uint16_t actual_size = 0;
91+
size_t actual_size = 0;
8992
DeviceKey &devkey = DeviceKey::get_instance();
90-
NVStore &nvstore = NVStore::get_instance();
93+
KVMap &kv_map = KVMap::get_instance();
94+
KVStore *inner_store = kv_map.get_internal_kv_instance(NULL);
95+
TEST_ASSERT_NOT_EQUAL(NULL, inner_store);
96+
9197
size_t salt_size = sizeof(salt);
9298

9399
if (strcmp(key, MSG_KEY_DEVICE_TEST_STEP1) == 0) {
94100

95-
//Third step: Clear NVStore, create an ROT key, derive a 16 byte
96-
//key and store it in NVStore at index 15, At the end reset the device
97-
int ret = nvstore.reset();
101+
//Third step: Clear KVStore, create an ROT key, derive a 16 byte
102+
//key and store it in KVStore at index 15, At the end reset the device
103+
int ret = inner_store->reset();
98104
TEST_ASSERT_EQUAL_INT(DEVICEKEY_SUCCESS, ret);
99105

100106
ret = inject_dummy_rot_key();
@@ -107,16 +113,16 @@ void generate_derived_key_consistency_16_byte_key_long_consistency_test(char *ke
107113
bool is_empty = !memcmp(empty_buffer, output1, sizeof(output1));
108114
TEST_ASSERT_FALSE(is_empty);
109115

110-
ret = nvstore.set(15, DEVICE_KEY_16BYTE, output1);
116+
ret = inner_store->set("SetForNext", output1, DEVICE_KEY_16BYTE, 0);
111117
TEST_ASSERT_EQUAL_INT32(0, ret);
112118

113119
} else if (strcmp(key, MSG_KEY_DEVICE_TEST_STEP2) == 0) {
114120

115-
//Second step: Read from NVStore at index 15 there should be a derived key there.
121+
//Second step: Read from KVStore at index 15 there should be a derived key there.
116122
//Now try to derive a key for 100 times and check it is the same key like before the reset.
117-
//At the end clear NVStore.
118-
int ret = nvstore.get(15, DEVICE_KEY_16BYTE, output1, actual_size);
119-
TEST_ASSERT_FALSE(NVSTORE_SUCCESS != ret)
123+
//At the end clear KVStore.
124+
int ret = inner_store->get("SetForNext", output1, DEVICE_KEY_16BYTE, &actual_size);
125+
TEST_ASSERT_EQUAL_ERROR_CODE(MBED_SUCCESS, ret);
120126
TEST_ASSERT_EQUAL_INT(DEVICE_KEY_16BYTE, actual_size);
121127

122128
for (int i = 0; i < 100; i++) {
@@ -126,7 +132,7 @@ void generate_derived_key_consistency_16_byte_key_long_consistency_test(char *ke
126132
TEST_ASSERT_EQUAL_UINT8_ARRAY(output1, output2, DEVICE_KEY_16BYTE);
127133
}
128134

129-
ret = nvstore.reset();
135+
ret = inner_store->reset();
130136
TEST_ASSERT_EQUAL_INT(DEVICEKEY_SUCCESS, ret);
131137

132138
} else {
@@ -144,16 +150,18 @@ void generate_derived_key_consistency_32_byte_key_long_consistency_test(char *ke
144150
unsigned char empty_buffer[DEVICE_KEY_32BYTE];
145151
unsigned char salt[] = "The quick brown fox jumps over the lazy dog";
146152
int key_type = DEVICE_KEY_32BYTE;
147-
uint16_t actual_size = 0;
148-
DeviceKey &devkey = DeviceKey::get_instance();
149-
NVStore &nvstore = NVStore::get_instance();
153+
size_t actual_size = 0;
150154
size_t salt_size = sizeof(salt);
155+
DeviceKey &devkey = DeviceKey::get_instance();
156+
KVMap &kv_map = KVMap::get_instance();
157+
KVStore *inner_store = kv_map.get_internal_kv_instance(NULL);
158+
TEST_ASSERT_NOT_EQUAL(NULL, inner_store);
151159

152160
if (strcmp(key, MSG_KEY_DEVICE_TEST_STEP3) == 0) {
153161

154-
//Third step: Clear NVStore, create an ROT key, derive a 32 byte
155-
//key and store it in NVStore at index 15, At the end reset the device
156-
int ret = nvstore.reset();
162+
//Third step: Clear KVStore, create an ROT key, derive a 32 byte
163+
//key and store it in KVStore at index 15, At the end reset the device
164+
int ret = inner_store->reset();
157165
TEST_ASSERT_EQUAL_INT(DEVICEKEY_SUCCESS, ret);
158166

159167
ret = inject_dummy_rot_key();
@@ -166,16 +174,16 @@ void generate_derived_key_consistency_32_byte_key_long_consistency_test(char *ke
166174
bool is_empty = !memcmp(empty_buffer, output1, sizeof(output1));
167175
TEST_ASSERT_FALSE(is_empty);
168176

169-
ret = nvstore.set(15, DEVICE_KEY_32BYTE, output1);
177+
ret = inner_store->set("SetForNext", output1, DEVICE_KEY_32BYTE, 0);
170178
TEST_ASSERT_EQUAL_INT32(0, ret);
171179

172180
} else if (strcmp(key, MSG_KEY_DEVICE_TEST_STEP4) == 0) {
173181

174-
//Fourth step: Read from NVStore at index 15 there should be a derived key there.
182+
//Fourth step: Read from KVStore at index 15 there should be a derived key there.
175183
//Now try to derive a key for 100 times and check it is the same key like before the reset.
176-
//At the end clear NVStore.
177-
int ret = nvstore.get(15, DEVICE_KEY_32BYTE, output1, actual_size);
178-
TEST_ASSERT_FALSE(NVSTORE_SUCCESS != ret)
184+
//At the end clear KVStore.
185+
int ret = inner_store->get("SetForNext", output1, DEVICE_KEY_32BYTE, &actual_size);
186+
TEST_ASSERT_EQUAL_ERROR_CODE(MBED_SUCCESS, ret);
179187
TEST_ASSERT_EQUAL_INT(DEVICE_KEY_32BYTE, actual_size);
180188

181189
for (int i = 0; i < 100; i++) {
@@ -185,7 +193,7 @@ void generate_derived_key_consistency_32_byte_key_long_consistency_test(char *ke
185193
TEST_ASSERT_EQUAL_UINT8_ARRAY(output1, output2, DEVICE_KEY_32BYTE);
186194
}
187195

188-
ret = nvstore.reset();
196+
ret = inner_store->reset();
189197
TEST_ASSERT_EQUAL_INT(DEVICEKEY_SUCCESS, ret);
190198

191199
} else {
@@ -219,23 +227,23 @@ void device_inject_root_of_trust_16_byte_size_test()
219227
{
220228
DeviceKey &devkey = DeviceKey::get_instance();
221229
uint32_t rkey[DEVICE_KEY_16BYTE / sizeof(uint32_t)];
222-
uint16_t actual_size;
223230
uint32_t key[DEVICE_KEY_16BYTE / sizeof(uint32_t)];
224-
NVStore &nvstore = NVStore::get_instance();
231+
KVMap &kv_map = KVMap::get_instance();
232+
KVStore *inner_store = kv_map.get_internal_kv_instance(NULL);
233+
TEST_ASSERT_NOT_EQUAL(NULL, inner_store);
225234

226-
int ret = nvstore.reset();
235+
int ret = inner_store->reset();
227236
TEST_ASSERT_EQUAL_INT(DEVICEKEY_SUCCESS, ret);
228237

229238
memcpy(key, "1234567812345678", sizeof(key));
230239
ret = devkey.device_inject_root_of_trust(key, DEVICE_KEY_16BYTE);
231240
TEST_ASSERT_EQUAL_INT(DEVICEKEY_SUCCESS, ret);
232241

233-
//Read the key from NVStore.
242+
//Read the key from KVStore.
234243
memset(rkey, 0, sizeof(rkey));
235-
ret = nvstore.get(NVSTORE_DEVICEKEY_KEY, DEVICE_KEY_16BYTE, rkey, actual_size);
244+
ret = ((TDBStore *)inner_store)->reserved_data_get(rkey, DEVICE_KEY_16BYTE);
236245
TEST_ASSERT_EQUAL_INT(DEVICEKEY_SUCCESS, ret);
237-
TEST_ASSERT_EQUAL_INT(DEVICE_KEY_16BYTE, actual_size);
238-
TEST_ASSERT_EQUAL_INT32_ARRAY(key, rkey, actual_size / sizeof(uint32_t));
246+
TEST_ASSERT_EQUAL_INT32_ARRAY(key, rkey, DEVICE_KEY_16BYTE / sizeof(uint32_t));
239247
}
240248

241249
/*
@@ -245,23 +253,23 @@ void device_inject_root_of_trust_32_byte_size_test()
245253
{
246254
DeviceKey &devkey = DeviceKey::get_instance();
247255
uint32_t rkey[DEVICE_KEY_32BYTE / sizeof(uint32_t)];
248-
uint16_t actual_size;
249256
uint32_t key[DEVICE_KEY_32BYTE / sizeof(uint32_t)];
250-
NVStore &nvstore = NVStore::get_instance();
257+
KVMap &kv_map = KVMap::get_instance();
258+
KVStore *inner_store = kv_map.get_internal_kv_instance(NULL);
259+
TEST_ASSERT_NOT_EQUAL(NULL, inner_store);
251260

252-
int ret = nvstore.reset();
261+
int ret = inner_store->reset();
253262
TEST_ASSERT_EQUAL_INT(DEVICEKEY_SUCCESS, ret);
254263

255264
memcpy(key, "12345678123456788765432187654321", sizeof(key));
256265
ret = devkey.device_inject_root_of_trust(key, DEVICE_KEY_32BYTE);
257266
TEST_ASSERT_EQUAL_INT(DEVICEKEY_SUCCESS, ret);
258267

259-
//Read the key from NVStore.
268+
//Read the key from KVStore.
260269
memset(rkey, 0, sizeof(rkey));
261-
ret = nvstore.get(NVSTORE_DEVICEKEY_KEY, DEVICE_KEY_32BYTE, rkey, actual_size);
270+
ret = ((TDBStore *)inner_store)->reserved_data_get(rkey, DEVICE_KEY_32BYTE);
262271
TEST_ASSERT_EQUAL_INT(DEVICEKEY_SUCCESS, ret);
263-
TEST_ASSERT_EQUAL_INT(DEVICE_KEY_32BYTE, actual_size);
264-
TEST_ASSERT_EQUAL_INT32_ARRAY(key, rkey, actual_size / sizeof(uint32_t));
272+
TEST_ASSERT_EQUAL_INT32_ARRAY(key, rkey, DEVICE_KEY_32BYTE / sizeof(uint32_t));
265273
}
266274

267275
/*
@@ -271,9 +279,11 @@ void device_inject_root_of_trust_several_times_test()
271279
{
272280
DeviceKey &devkey = DeviceKey::get_instance();
273281
uint32_t key[DEVICE_KEY_32BYTE / sizeof(uint32_t)];
274-
NVStore &nvstore = NVStore::get_instance();
282+
KVMap &kv_map = KVMap::get_instance();
283+
KVStore *inner_store = kv_map.get_internal_kv_instance(NULL);
284+
TEST_ASSERT_NOT_EQUAL(NULL, inner_store);
275285

276-
int ret = nvstore.reset();
286+
int ret = inner_store->reset();
277287
TEST_ASSERT_EQUAL_INT(DEVICEKEY_SUCCESS, ret);
278288

279289
memcpy(key, "12345678123456788765432187654321", DEVICE_KEY_32BYTE);
@@ -300,9 +310,11 @@ void generate_derived_key_consistency_16_byte_key_test()
300310
unsigned char salt[] = "Once upon a time, I worked for the circus and I lived in Omaha.";
301311
int key_type = DEVICE_KEY_16BYTE;
302312
DeviceKey &devkey = DeviceKey::get_instance();
303-
NVStore &nvstore = NVStore::get_instance();
313+
KVMap &kv_map = KVMap::get_instance();
314+
KVStore *inner_store = kv_map.get_internal_kv_instance(NULL);
315+
TEST_ASSERT_NOT_EQUAL(NULL, inner_store);
304316

305-
int ret = nvstore.reset();
317+
int ret = inner_store->reset();
306318
TEST_ASSERT_EQUAL_INT(DEVICEKEY_SUCCESS, ret);
307319

308320
ret = inject_dummy_rot_key();
@@ -335,9 +347,11 @@ void generate_derived_key_consistency_32_byte_key_test()
335347
unsigned char salt[] = "The quick brown fox jumps over the lazy dog";
336348
int key_type = DEVICE_KEY_32BYTE;
337349
DeviceKey &devkey = DeviceKey::get_instance();
338-
NVStore &nvstore = NVStore::get_instance();
350+
KVMap &kv_map = KVMap::get_instance();
351+
KVStore *inner_store = kv_map.get_internal_kv_instance(NULL);
352+
TEST_ASSERT_NOT_EQUAL(NULL, inner_store);
339353

340-
int ret = nvstore.reset();
354+
int ret = inner_store->reset();
341355
TEST_ASSERT_EQUAL_INT(DEVICEKEY_SUCCESS, ret);
342356

343357
ret = inject_dummy_rot_key();
@@ -370,9 +384,11 @@ void generate_derived_key_key_type_16_test()
370384
int key_type = DEVICE_KEY_16BYTE;
371385
size_t salt_size = sizeof(salt);
372386
DeviceKey &devkey = DeviceKey::get_instance();
373-
NVStore &nvstore = NVStore::get_instance();
387+
KVMap &kv_map = KVMap::get_instance();
388+
KVStore *inner_store = kv_map.get_internal_kv_instance(NULL);
389+
TEST_ASSERT_NOT_EQUAL(NULL, inner_store);
374390

375-
int ret = nvstore.reset();
391+
int ret = inner_store->reset();
376392
TEST_ASSERT_EQUAL_INT(DEVICEKEY_SUCCESS, ret);
377393

378394
ret = inject_dummy_rot_key();
@@ -401,9 +417,11 @@ void generate_derived_key_key_type_32_test()
401417
size_t salt_size = sizeof(salt);
402418
unsigned char expectedString[] = "Some String";
403419
DeviceKey &devkey = DeviceKey::get_instance();
404-
NVStore &nvstore = NVStore::get_instance();
420+
KVMap &kv_map = KVMap::get_instance();
421+
KVStore *inner_store = kv_map.get_internal_kv_instance(NULL);
422+
TEST_ASSERT_NOT_EQUAL(NULL, inner_store);
405423

406-
int ret = nvstore.reset();
424+
int ret = inner_store->reset();
407425
TEST_ASSERT_EQUAL_INT(DEVICEKEY_SUCCESS, ret);
408426

409427
ret = inject_dummy_rot_key();
@@ -430,10 +448,11 @@ void generate_derived_key_wrong_key_type_test()
430448
unsigned char salt[] = "The quick brown fox jumps over the lazy dog";
431449
size_t salt_size = sizeof(salt);
432450
DeviceKey &devkey = DeviceKey::get_instance();
433-
NVStore &nvstore = NVStore::get_instance();
451+
KVMap &kv_map = KVMap::get_instance();
452+
KVStore *inner_store = kv_map.get_internal_kv_instance(NULL);
453+
TEST_ASSERT_NOT_EQUAL(NULL, inner_store);
434454

435-
nvstore.init();
436-
int ret = nvstore.reset();
455+
int ret = inner_store->reset();
437456
TEST_ASSERT_EQUAL_INT(DEVICEKEY_SUCCESS, ret);
438457

439458
ret = inject_dummy_rot_key();
@@ -453,7 +472,7 @@ utest::v1::status_t greentea_failure_handler(const Case *const source, const fai
453472

454473
//Currently there can be only one test that contains reset and it has to be the first test!
455474
Case cases[] = {
456-
Case("Device Key - long consistency test", generate_derived_key_long_consistency_test, greentea_failure_handler),
475+
Case("Device Key - long consistency test", generate_derived_key_long_consistency_test, greentea_failure_handler),
457476
Case("Device Key - inject value wrong size", device_inject_root_of_trust_wrong_size_test, greentea_failure_handler),
458477
Case("Device Key - inject value 16 byte size", device_inject_root_of_trust_16_byte_size_test, greentea_failure_handler),
459478
Case("Device Key - inject value 32 byte size", device_inject_root_of_trust_32_byte_size_test, greentea_failure_handler),

0 commit comments

Comments
 (0)