16
16
17
17
#include " DeviceKey.h"
18
18
#include " utest/utest.h"
19
+ #include " mbed_error.h"
19
20
#include " unity/unity.h"
20
21
#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"
26
26
27
27
using namespace utest ::v1;
28
28
using namespace mbed ;
29
29
30
+ #if !DEVICEKEY_ENABLED
31
+ #error [NOT_SUPPORTED] DeviceKey needs to be enabled for this test
32
+ #endif
33
+
30
34
#define MSG_VALUE_DUMMY " 0"
31
35
#define MSG_VALUE_LEN 32
32
36
#define MSG_KEY_LEN 32
@@ -62,7 +66,6 @@ int inject_dummy_rot_key()
62
66
void generate_derived_key_long_consistency_test ()
63
67
{
64
68
static char key[MSG_KEY_LEN + 1 ] = { };
65
- static char value[MSG_VALUE_LEN + 1 ] = { };
66
69
67
70
strcpy (key, MSG_KEY_DEVICE_TEST_STEP1);
68
71
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
85
88
unsigned char empty_buffer[DEVICE_KEY_16BYTE];
86
89
unsigned char salt[] = " Once upon a time, I worked for the circus and I lived in Omaha." ;
87
90
int key_type = DEVICE_KEY_16BYTE;
88
- uint16_t actual_size = 0 ;
91
+ size_t actual_size = 0 ;
89
92
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
+
91
97
size_t salt_size = sizeof (salt);
92
98
93
99
if (strcmp (key, MSG_KEY_DEVICE_TEST_STEP1) == 0 ) {
94
100
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 ();
98
104
TEST_ASSERT_EQUAL_INT (DEVICEKEY_SUCCESS, ret);
99
105
100
106
ret = inject_dummy_rot_key ();
@@ -107,16 +113,16 @@ void generate_derived_key_consistency_16_byte_key_long_consistency_test(char *ke
107
113
bool is_empty = !memcmp (empty_buffer, output1, sizeof (output1));
108
114
TEST_ASSERT_FALSE (is_empty);
109
115
110
- ret = nvstore. set (15 , DEVICE_KEY_16BYTE, output1 );
116
+ ret = inner_store-> set (" SetForNext " , output1, DEVICE_KEY_16BYTE, 0 );
111
117
TEST_ASSERT_EQUAL_INT32 (0 , ret);
112
118
113
119
} else if (strcmp (key, MSG_KEY_DEVICE_TEST_STEP2) == 0 ) {
114
120
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.
116
122
// 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);
120
126
TEST_ASSERT_EQUAL_INT (DEVICE_KEY_16BYTE, actual_size);
121
127
122
128
for (int i = 0 ; i < 100 ; i++) {
@@ -126,7 +132,7 @@ void generate_derived_key_consistency_16_byte_key_long_consistency_test(char *ke
126
132
TEST_ASSERT_EQUAL_UINT8_ARRAY (output1, output2, DEVICE_KEY_16BYTE);
127
133
}
128
134
129
- ret = nvstore. reset ();
135
+ ret = inner_store-> reset ();
130
136
TEST_ASSERT_EQUAL_INT (DEVICEKEY_SUCCESS, ret);
131
137
132
138
} else {
@@ -144,16 +150,18 @@ void generate_derived_key_consistency_32_byte_key_long_consistency_test(char *ke
144
150
unsigned char empty_buffer[DEVICE_KEY_32BYTE];
145
151
unsigned char salt[] = " The quick brown fox jumps over the lazy dog" ;
146
152
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 ;
150
154
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);
151
159
152
160
if (strcmp (key, MSG_KEY_DEVICE_TEST_STEP3) == 0 ) {
153
161
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 ();
157
165
TEST_ASSERT_EQUAL_INT (DEVICEKEY_SUCCESS, ret);
158
166
159
167
ret = inject_dummy_rot_key ();
@@ -166,16 +174,16 @@ void generate_derived_key_consistency_32_byte_key_long_consistency_test(char *ke
166
174
bool is_empty = !memcmp (empty_buffer, output1, sizeof (output1));
167
175
TEST_ASSERT_FALSE (is_empty);
168
176
169
- ret = nvstore. set (15 , DEVICE_KEY_32BYTE, output1 );
177
+ ret = inner_store-> set (" SetForNext " , output1, DEVICE_KEY_32BYTE, 0 );
170
178
TEST_ASSERT_EQUAL_INT32 (0 , ret);
171
179
172
180
} else if (strcmp (key, MSG_KEY_DEVICE_TEST_STEP4) == 0 ) {
173
181
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.
175
183
// 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);
179
187
TEST_ASSERT_EQUAL_INT (DEVICE_KEY_32BYTE, actual_size);
180
188
181
189
for (int i = 0 ; i < 100 ; i++) {
@@ -185,7 +193,7 @@ void generate_derived_key_consistency_32_byte_key_long_consistency_test(char *ke
185
193
TEST_ASSERT_EQUAL_UINT8_ARRAY (output1, output2, DEVICE_KEY_32BYTE);
186
194
}
187
195
188
- ret = nvstore. reset ();
196
+ ret = inner_store-> reset ();
189
197
TEST_ASSERT_EQUAL_INT (DEVICEKEY_SUCCESS, ret);
190
198
191
199
} else {
@@ -219,23 +227,23 @@ void device_inject_root_of_trust_16_byte_size_test()
219
227
{
220
228
DeviceKey &devkey = DeviceKey::get_instance ();
221
229
uint32_t rkey[DEVICE_KEY_16BYTE / sizeof (uint32_t )];
222
- uint16_t actual_size;
223
230
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);
225
234
226
- int ret = nvstore. reset ();
235
+ int ret = inner_store-> reset ();
227
236
TEST_ASSERT_EQUAL_INT (DEVICEKEY_SUCCESS, ret);
228
237
229
238
memcpy (key, " 1234567812345678" , sizeof (key));
230
239
ret = devkey.device_inject_root_of_trust (key, DEVICE_KEY_16BYTE);
231
240
TEST_ASSERT_EQUAL_INT (DEVICEKEY_SUCCESS, ret);
232
241
233
- // Read the key from NVStore .
242
+ // Read the key from KVStore .
234
243
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 );
236
245
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 ));
239
247
}
240
248
241
249
/*
@@ -245,23 +253,23 @@ void device_inject_root_of_trust_32_byte_size_test()
245
253
{
246
254
DeviceKey &devkey = DeviceKey::get_instance ();
247
255
uint32_t rkey[DEVICE_KEY_32BYTE / sizeof (uint32_t )];
248
- uint16_t actual_size;
249
256
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);
251
260
252
- int ret = nvstore. reset ();
261
+ int ret = inner_store-> reset ();
253
262
TEST_ASSERT_EQUAL_INT (DEVICEKEY_SUCCESS, ret);
254
263
255
264
memcpy (key, " 12345678123456788765432187654321" , sizeof (key));
256
265
ret = devkey.device_inject_root_of_trust (key, DEVICE_KEY_32BYTE);
257
266
TEST_ASSERT_EQUAL_INT (DEVICEKEY_SUCCESS, ret);
258
267
259
- // Read the key from NVStore .
268
+ // Read the key from KVStore .
260
269
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 );
262
271
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 ));
265
273
}
266
274
267
275
/*
@@ -271,9 +279,11 @@ void device_inject_root_of_trust_several_times_test()
271
279
{
272
280
DeviceKey &devkey = DeviceKey::get_instance ();
273
281
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);
275
285
276
- int ret = nvstore. reset ();
286
+ int ret = inner_store-> reset ();
277
287
TEST_ASSERT_EQUAL_INT (DEVICEKEY_SUCCESS, ret);
278
288
279
289
memcpy (key, " 12345678123456788765432187654321" , DEVICE_KEY_32BYTE);
@@ -300,9 +310,11 @@ void generate_derived_key_consistency_16_byte_key_test()
300
310
unsigned char salt[] = " Once upon a time, I worked for the circus and I lived in Omaha." ;
301
311
int key_type = DEVICE_KEY_16BYTE;
302
312
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);
304
316
305
- int ret = nvstore. reset ();
317
+ int ret = inner_store-> reset ();
306
318
TEST_ASSERT_EQUAL_INT (DEVICEKEY_SUCCESS, ret);
307
319
308
320
ret = inject_dummy_rot_key ();
@@ -335,9 +347,11 @@ void generate_derived_key_consistency_32_byte_key_test()
335
347
unsigned char salt[] = " The quick brown fox jumps over the lazy dog" ;
336
348
int key_type = DEVICE_KEY_32BYTE;
337
349
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);
339
353
340
- int ret = nvstore. reset ();
354
+ int ret = inner_store-> reset ();
341
355
TEST_ASSERT_EQUAL_INT (DEVICEKEY_SUCCESS, ret);
342
356
343
357
ret = inject_dummy_rot_key ();
@@ -370,9 +384,11 @@ void generate_derived_key_key_type_16_test()
370
384
int key_type = DEVICE_KEY_16BYTE;
371
385
size_t salt_size = sizeof (salt);
372
386
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);
374
390
375
- int ret = nvstore. reset ();
391
+ int ret = inner_store-> reset ();
376
392
TEST_ASSERT_EQUAL_INT (DEVICEKEY_SUCCESS, ret);
377
393
378
394
ret = inject_dummy_rot_key ();
@@ -401,9 +417,11 @@ void generate_derived_key_key_type_32_test()
401
417
size_t salt_size = sizeof (salt);
402
418
unsigned char expectedString[] = " Some String" ;
403
419
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);
405
423
406
- int ret = nvstore. reset ();
424
+ int ret = inner_store-> reset ();
407
425
TEST_ASSERT_EQUAL_INT (DEVICEKEY_SUCCESS, ret);
408
426
409
427
ret = inject_dummy_rot_key ();
@@ -430,10 +448,11 @@ void generate_derived_key_wrong_key_type_test()
430
448
unsigned char salt[] = " The quick brown fox jumps over the lazy dog" ;
431
449
size_t salt_size = sizeof (salt);
432
450
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);
434
454
435
- nvstore.init ();
436
- int ret = nvstore.reset ();
455
+ int ret = inner_store->reset ();
437
456
TEST_ASSERT_EQUAL_INT (DEVICEKEY_SUCCESS, ret);
438
457
439
458
ret = inject_dummy_rot_key ();
@@ -453,7 +472,7 @@ utest::v1::status_t greentea_failure_handler(const Case *const source, const fai
453
472
454
473
// Currently there can be only one test that contains reset and it has to be the first test!
455
474
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),
457
476
Case (" Device Key - inject value wrong size" , device_inject_root_of_trust_wrong_size_test, greentea_failure_handler),
458
477
Case (" Device Key - inject value 16 byte size" , device_inject_root_of_trust_16_byte_size_test, greentea_failure_handler),
459
478
Case (" Device Key - inject value 32 byte size" , device_inject_root_of_trust_32_byte_size_test, greentea_failure_handler),
0 commit comments