12
12
#include "mesh/keys.h"
13
13
#include <bs_cmd_line.h>
14
14
15
+ #include <zephyr/psa/key_ids.h>
16
+ #include <psa/crypto.h>
17
+
15
18
#define LOG_MODULE_NAME test_persistence
16
19
17
20
#include <zephyr/logging/log.h>
@@ -247,6 +250,7 @@ static const struct stack_cfg {
247
250
},
248
251
};
249
252
static const struct stack_cfg * current_stack_cfg ;
253
+ static bool persist_stuck_key ;
250
254
251
255
static void test_args_parse (int argc , char * argv [])
252
256
{
@@ -267,6 +271,13 @@ static void test_args_parse(int argc, char *argv[])
267
271
.name = "{0, 1}" ,
268
272
.option = "stack-cfg" ,
269
273
.descript = ""
274
+ },
275
+ {
276
+ .dest = & persist_stuck_key ,
277
+ .type = 'b' ,
278
+ .name = "{0, 1}" ,
279
+ .option = "persist-stuck-key" ,
280
+ .descript = "PSA ITS has a gotten stuck key"
270
281
}
271
282
};
272
283
@@ -458,10 +469,68 @@ static void provisioner_setup(void)
458
469
provisioner_ready = true;
459
470
}
460
471
472
+ static void fake_key_setup (void )
473
+ {
474
+ psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT ;
475
+ psa_status_t status ;
476
+ psa_key_id_t key_id = ZEPHYR_PSA_BT_MESH_KEY_ID_RANGE_BEGIN ;
477
+ psa_key_id_t key_id_imported ;
478
+ const uint8_t in [16 ] = {0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 ,
479
+ 0x09 , 0x0a , 0x0b , 0x0c , 0x0d , 0x0e , 0x0f , 0x10 };
480
+ uint8_t out [16 ] = {0 };
481
+ size_t out_len ;
482
+
483
+ psa_set_key_lifetime (& key_attributes , PSA_KEY_LIFETIME_PERSISTENT );
484
+ psa_set_key_id (& key_attributes , key_id );
485
+ psa_set_key_usage_flags (& key_attributes , PSA_KEY_USAGE_EXPORT );
486
+ psa_set_key_type (& key_attributes , PSA_KEY_TYPE_AES );
487
+ psa_set_key_bits (& key_attributes , 128 );
488
+
489
+ status = psa_import_key (& key_attributes , in , 16 , & key_id_imported );
490
+ psa_reset_key_attributes (& key_attributes );
491
+ if (status != PSA_SUCCESS ) {
492
+ FAIL ("Failed to import fake key" );
493
+ }
494
+
495
+ if (key_id_imported != key_id ) {
496
+ FAIL ("Imported key ID does not match expected key ID" );
497
+ }
498
+
499
+ status = psa_export_key (key_id_imported , out , sizeof (out ), & out_len );
500
+ if (status != PSA_SUCCESS ) {
501
+ FAIL ("Failed to export fake key" );
502
+ }
503
+
504
+ if (memcmp (out , in , sizeof (in )) != 0 || out_len != sizeof (in )) {
505
+ FAIL ("Exported key does not match imported key" );
506
+ }
507
+ }
508
+
509
+ static void fake_key_destruction_check (void )
510
+ {
511
+ psa_status_t status ;
512
+ psa_key_id_t key_id = ZEPHYR_PSA_BT_MESH_KEY_ID_RANGE_BEGIN ;
513
+ uint8_t out [16 ] = {0 };
514
+ size_t out_len ;
515
+
516
+ status = psa_export_key (key_id , out , sizeof (out ), & out_len );
517
+ if (status != PSA_SUCCESS ) {
518
+ FAIL ("Failed to export former fake key ID" );
519
+ }
520
+
521
+ if (memcmp (out , test_devkey , sizeof (out )) != 0 || out_len != sizeof (test_devkey )) {
522
+ FAIL ("Exported fake key does not match test device key" );
523
+ }
524
+ }
525
+
461
526
static void test_provisioning_data_save (void )
462
527
{
463
528
bt_mesh_test_cfg_set (NULL , WAIT_TIME );
464
529
530
+ if (persist_stuck_key ) {
531
+ fake_key_setup ();
532
+ }
533
+
465
534
if (device_setup_and_self_provision ()) {
466
535
FAIL ("Mesh setup failed. Settings should not be loaded." );
467
536
}
@@ -476,6 +545,10 @@ static void test_provisioning_data_load(void)
476
545
/* In this test stack should boot as provisioned */
477
546
bt_mesh_test_cfg_set (NULL , WAIT_TIME );
478
547
548
+ if (persist_stuck_key ) {
549
+ fake_key_destruction_check ();
550
+ }
551
+
479
552
if (device_setup_and_self_provision () != - EALREADY ) {
480
553
FAIL ("Device should boot up as already provisioned" );
481
554
}
0 commit comments