Skip to content

Commit 34c3ccf

Browse files
alxelaxcfriedt
authored andcommitted
tests: bluetooth: mesh: extend provisioning test with gotten stuck key
Commit extends existing persistent provisioning bsim mesh test that checks that mesh removes gotten stuck persisted key if key is reused. Correct key is imported normally after that. Signed-off-by: Aleksandr Khromykh <[email protected]>
1 parent c02ec1f commit 34c3ccf

File tree

3 files changed

+115
-5
lines changed

3 files changed

+115
-5
lines changed

tests/bsim/bluetooth/mesh/src/test_persistence.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
#include "mesh/keys.h"
1313
#include <bs_cmd_line.h>
1414

15+
#include <zephyr/psa/key_ids.h>
16+
#include <psa/crypto.h>
17+
1518
#define LOG_MODULE_NAME test_persistence
1619

1720
#include <zephyr/logging/log.h>
@@ -247,6 +250,7 @@ static const struct stack_cfg {
247250
},
248251
};
249252
static const struct stack_cfg *current_stack_cfg;
253+
static bool persist_stuck_key;
250254

251255
static void test_args_parse(int argc, char *argv[])
252256
{
@@ -267,6 +271,13 @@ static void test_args_parse(int argc, char *argv[])
267271
.name = "{0, 1}",
268272
.option = "stack-cfg",
269273
.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"
270281
}
271282
};
272283

@@ -458,10 +469,68 @@ static void provisioner_setup(void)
458469
provisioner_ready = true;
459470
}
460471

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+
461526
static void test_provisioning_data_save(void)
462527
{
463528
bt_mesh_test_cfg_set(NULL, WAIT_TIME);
464529

530+
if (persist_stuck_key) {
531+
fake_key_setup();
532+
}
533+
465534
if (device_setup_and_self_provision()) {
466535
FAIL("Mesh setup failed. Settings should not be loaded.");
467536
}
@@ -476,6 +545,10 @@ static void test_provisioning_data_load(void)
476545
/* In this test stack should boot as provisioned */
477546
bt_mesh_test_cfg_set(NULL, WAIT_TIME);
478547

548+
if (persist_stuck_key) {
549+
fake_key_destruction_check();
550+
}
551+
479552
if (device_setup_and_self_provision() != -EALREADY) {
480553
FAIL("Device should boot up as already provisioned");
481554
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2025 Nordic Semiconductor
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
source $(dirname "${BASH_SOURCE[0]}")/../../_mesh_test.sh
6+
7+
# Test checks that mesh provisioning data is saved and loaded correctly
8+
# if a key with ID from mesh PSA key ID range exists in the PSA ITS,
9+
# it is destroyed and the correct key is used instead.
10+
#
11+
# Test must be added in pair and in sequence.
12+
# First test: saves data; second test: verifies it.
13+
#
14+
# Test procedure:
15+
# 1. Fake key is created in the PSA ITS to emulate gotten stuck key.
16+
# 2. Device Under Test (DUT) initializes the Mesh stack,
17+
# and starts provisioning and configuration itself.
18+
# 3. Test scenario emulates hardware reset by running the second test
19+
# with stored data from the first test.
20+
# 4. DUT checks that the stuck key was substituted by the real key.
21+
# 5. DUT starts mesh with loading stored data and checks that if they were restored correctly.
22+
23+
overlay=overlay_pst_conf
24+
RunTestFlash mesh_pst_prov_data_check_stuck_key persistence_provisioning_data_save -flash_erase \
25+
-- -argstest persist-stuck-key=1
26+
27+
overlay=overlay_pst_conf
28+
RunTestFlash mesh_pst_prov_data_check_stuck_key persistence_provisioning_data_load -flash_rm \
29+
-- -argstest persist-stuck-key=1

tests/bsim/bluetooth/mesh/tests_scripts/persistence/provisioning.sh

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,22 @@
44

55
source $(dirname "${BASH_SOURCE[0]}")/../../_mesh_test.sh
66

7-
# Note:
7+
# Test checks that mesh provisioning data is saved and loaded correctly.
8+
#
89
# Tests must be added in pairs and in sequence.
910
# First test: saves data; second test: verifies it.
11+
#
12+
# Test procedure:
13+
# 1. Device Under Test (DUT) initializes the Mesh stack,
14+
# and starts provisioning and configuration itself.
15+
# 2. Test scenario emulates hardware reset by running the second test
16+
# with stored data from the first test.
17+
# 3. DUT starts mesh with loading stored data and checks that if they were restored correctly.
1018

11-
# SKIP=(persistence_provisioning_data_save)
1219
overlay=overlay_pst_conf
13-
RunTestFlash mesh_pst_prov_data_check persistence_provisioning_data_save -flash_erase
20+
RunTestFlash mesh_pst_prov_data_check persistence_provisioning_data_save -flash_erase \
21+
-- -argstest persist-stuck-key=0
1422

15-
# SKIP=(persistence_provisioning_data_load)
1623
overlay=overlay_pst_conf
17-
RunTestFlash mesh_pst_prov_data_check persistence_provisioning_data_load -flash_rm
24+
RunTestFlash mesh_pst_prov_data_check persistence_provisioning_data_load -flash_rm \
25+
-- -argstest persist-stuck-key=0

0 commit comments

Comments
 (0)