From 843da3edd796a38053cb5e53abe5bfa5b0293b5f Mon Sep 17 00:00:00 2001 From: Grzegorz Chwierut Date: Thu, 31 Oct 2024 18:55:19 +0100 Subject: [PATCH] tests: add tests for KMU and keys provisioning Add two test applications with test scenarios automated with pytest. Keys are provisioned with the `west ncs-provision` command. The tests verify if the applications boot and if the keys are correct. Signed-off-by: Grzegorz Chwierut --- CODEOWNERS | 1 + tests/subsys/kmu/hello_for_kmu/CMakeLists.txt | 12 ++ tests/subsys/kmu/hello_for_kmu/prj.conf | 1 + .../kmu/hello_for_kmu/sb_secondary_key.conf | 6 + .../kmu/hello_for_kmu/sb_wrong_key.conf | 6 + tests/subsys/kmu/hello_for_kmu/src/main.c | 14 +++ tests/subsys/kmu/hello_for_kmu/sysbuild.conf | 7 ++ .../kmu/hello_for_kmu/sysbuild/mcuboot.conf | 13 ++ tests/subsys/kmu/hello_for_kmu/testcase.yaml | 19 +++ tests/subsys/kmu/keys/root-ed25519-1.pem | 3 + tests/subsys/kmu/keys/root-ed25519-2.pem | 3 + tests/subsys/kmu/keys/root-ed25519-w.pem | 3 + tests/subsys/kmu/pytest/common.py | 67 ++++++++++ tests/subsys/kmu/pytest/conftest.py | 12 ++ .../kmu/pytest/test_kmu_key_provision.py | 69 +++++++++++ .../kmu/pytest/test_kmu_with_mcuboot.py | 72 +++++++++++ .../verify_west_ncs_provision/CMakeLists.txt | 12 ++ .../kmu/verify_west_ncs_provision/prj.conf | 20 +++ .../kmu/verify_west_ncs_provision/src/main.c | 117 ++++++++++++++++++ .../verify_west_ncs_provision/testcase.yaml | 12 ++ 20 files changed, 469 insertions(+) create mode 100644 tests/subsys/kmu/hello_for_kmu/CMakeLists.txt create mode 100644 tests/subsys/kmu/hello_for_kmu/prj.conf create mode 100644 tests/subsys/kmu/hello_for_kmu/sb_secondary_key.conf create mode 100644 tests/subsys/kmu/hello_for_kmu/sb_wrong_key.conf create mode 100644 tests/subsys/kmu/hello_for_kmu/src/main.c create mode 100644 tests/subsys/kmu/hello_for_kmu/sysbuild.conf create mode 100644 tests/subsys/kmu/hello_for_kmu/sysbuild/mcuboot.conf create mode 100644 tests/subsys/kmu/hello_for_kmu/testcase.yaml create mode 100644 tests/subsys/kmu/keys/root-ed25519-1.pem create mode 100644 tests/subsys/kmu/keys/root-ed25519-2.pem create mode 100644 tests/subsys/kmu/keys/root-ed25519-w.pem create mode 100644 tests/subsys/kmu/pytest/common.py create mode 100644 tests/subsys/kmu/pytest/conftest.py create mode 100644 tests/subsys/kmu/pytest/test_kmu_key_provision.py create mode 100644 tests/subsys/kmu/pytest/test_kmu_with_mcuboot.py create mode 100644 tests/subsys/kmu/verify_west_ncs_provision/CMakeLists.txt create mode 100644 tests/subsys/kmu/verify_west_ncs_provision/prj.conf create mode 100644 tests/subsys/kmu/verify_west_ncs_provision/src/main.c create mode 100644 tests/subsys/kmu/verify_west_ncs_provision/testcase.yaml diff --git a/CODEOWNERS b/CODEOWNERS index 97ba3298f4c1..32b92c5f01f9 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -769,6 +769,7 @@ /tests/subsys/emds/ @balaklaka @nrfconnect/ncs-paladin /tests/subsys/event_manager_proxy/ @nrfconnect/ncs-si-muffin /tests/subsys/fw_info/ @nrfconnect/ncs-pluto +/tests/subsys/kmu/ @nrfconnect/ncs-pluto /tests/subsys/mpsl/ @nrfconnect/ncs-dragoon /tests/subsys/net/lib/aws_*/ @nrfconnect/ncs-cia /tests/subsys/net/lib/azure_iot_hub/ @nrfconnect/ncs-cia diff --git a/tests/subsys/kmu/hello_for_kmu/CMakeLists.txt b/tests/subsys/kmu/hello_for_kmu/CMakeLists.txt new file mode 100644 index 000000000000..562cfc9a6820 --- /dev/null +++ b/tests/subsys/kmu/hello_for_kmu/CMakeLists.txt @@ -0,0 +1,12 @@ +# +# Copyright (c) 2024 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(hello_for_kmu) + +target_sources(app PRIVATE src/main.c) diff --git a/tests/subsys/kmu/hello_for_kmu/prj.conf b/tests/subsys/kmu/hello_for_kmu/prj.conf new file mode 100644 index 000000000000..b2a4ba591044 --- /dev/null +++ b/tests/subsys/kmu/hello_for_kmu/prj.conf @@ -0,0 +1 @@ +# nothing here diff --git a/tests/subsys/kmu/hello_for_kmu/sb_secondary_key.conf b/tests/subsys/kmu/hello_for_kmu/sb_secondary_key.conf new file mode 100644 index 000000000000..5a870d04b205 --- /dev/null +++ b/tests/subsys/kmu/hello_for_kmu/sb_secondary_key.conf @@ -0,0 +1,6 @@ +# +# Copyright (c) 2024 Nordic Semiconductor +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# +SB_CONFIG_BOOT_SIGNATURE_KEY_FILE="\${APPLICATION_CONFIG_DIR}/../keys/root-ed25519-2.pem" diff --git a/tests/subsys/kmu/hello_for_kmu/sb_wrong_key.conf b/tests/subsys/kmu/hello_for_kmu/sb_wrong_key.conf new file mode 100644 index 000000000000..cba85c131325 --- /dev/null +++ b/tests/subsys/kmu/hello_for_kmu/sb_wrong_key.conf @@ -0,0 +1,6 @@ +# +# Copyright (c) 2024 Nordic Semiconductor +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# +SB_CONFIG_BOOT_SIGNATURE_KEY_FILE="\${APPLICATION_CONFIG_DIR}/../keys/root-ed25519-w.pem" diff --git a/tests/subsys/kmu/hello_for_kmu/src/main.c b/tests/subsys/kmu/hello_for_kmu/src/main.c new file mode 100644 index 000000000000..e8392cab6c76 --- /dev/null +++ b/tests/subsys/kmu/hello_for_kmu/src/main.c @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2024 Nordic Semiconductor ASA. + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ + +#include + +int main(void) +{ + printf("Hello World! %s\n", CONFIG_BOARD_TARGET); + + return 0; +} diff --git a/tests/subsys/kmu/hello_for_kmu/sysbuild.conf b/tests/subsys/kmu/hello_for_kmu/sysbuild.conf new file mode 100644 index 000000000000..096f1babba86 --- /dev/null +++ b/tests/subsys/kmu/hello_for_kmu/sysbuild.conf @@ -0,0 +1,7 @@ +# +# Copyright (c) 2024 Nordic Semiconductor +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# +SB_CONFIG_BOOTLOADER_MCUBOOT=y +SB_CONFIG_BOOT_SIGNATURE_TYPE_ED25519=y diff --git a/tests/subsys/kmu/hello_for_kmu/sysbuild/mcuboot.conf b/tests/subsys/kmu/hello_for_kmu/sysbuild/mcuboot.conf new file mode 100644 index 000000000000..6a283595121c --- /dev/null +++ b/tests/subsys/kmu/hello_for_kmu/sysbuild/mcuboot.conf @@ -0,0 +1,13 @@ +# +# Copyright (c) 2024 Nordic Semiconductor +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# +CONFIG_NRF_SECURITY=y +CONFIG_MBEDTLS=n +CONFIG_BOOT_ED25519_PSA=y +CONFIG_PM_PARTITION_SIZE_MCUBOOT=0x10000 +CONFIG_BOOT_SIGNATURE_USING_KMU=y + +# can be removed after merging #18487 +CONFIG_MBEDTLS_THREADING_C=n diff --git a/tests/subsys/kmu/hello_for_kmu/testcase.yaml b/tests/subsys/kmu/hello_for_kmu/testcase.yaml new file mode 100644 index 000000000000..3a4e51ab6eb0 --- /dev/null +++ b/tests/subsys/kmu/hello_for_kmu/testcase.yaml @@ -0,0 +1,19 @@ +common: + sysbuild: true + timeout: 180 + tags: pytest mcuboot kmu + platform_allow: + - nrf54l15dk/nrf54l15/cpuapp + harness: pytest + harness_config: + pytest_dut_scope: session + pytest_root: + - "../pytest/test_kmu_with_mcuboot.py" +tests: + mcuboot.kmu.west.provision.default_key: {} + mcuboot.kmu.west.provision.secondary_key: + extra_args: + - SB_EXTRA_CONF_FILE=sb_secondary_key.conf + mcuboot.kmu.west.provision.wrong_key: + extra_args: + - SB_EXTRA_CONF_FILE=sb_wrong_key.conf diff --git a/tests/subsys/kmu/keys/root-ed25519-1.pem b/tests/subsys/kmu/keys/root-ed25519-1.pem new file mode 100644 index 000000000000..f19442f23d24 --- /dev/null +++ b/tests/subsys/kmu/keys/root-ed25519-1.pem @@ -0,0 +1,3 @@ +-----BEGIN PRIVATE KEY----- +MC4CAQAwBQYDK2VwBCIEIG5zv1wuAZJttuHXngrRJfi1w536UDDKra71UXroQ5z/ +-----END PRIVATE KEY----- diff --git a/tests/subsys/kmu/keys/root-ed25519-2.pem b/tests/subsys/kmu/keys/root-ed25519-2.pem new file mode 100644 index 000000000000..bfbf77c83aaa --- /dev/null +++ b/tests/subsys/kmu/keys/root-ed25519-2.pem @@ -0,0 +1,3 @@ +-----BEGIN PRIVATE KEY----- +MC4CAQAwBQYDK2VwBCIEII9wFheJa4Lw7fAtmjp1GkonRMknzfJFEdZkTf94jyak +-----END PRIVATE KEY----- diff --git a/tests/subsys/kmu/keys/root-ed25519-w.pem b/tests/subsys/kmu/keys/root-ed25519-w.pem new file mode 100644 index 000000000000..da19c2011585 --- /dev/null +++ b/tests/subsys/kmu/keys/root-ed25519-w.pem @@ -0,0 +1,3 @@ +-----BEGIN PRIVATE KEY----- +MC4CAQAwBQYDK2VwBCIEIAGMROMZRAwsLq7pWKOsumPPKOKVfEjAydgAhaaVOi7s +-----END PRIVATE KEY----- diff --git a/tests/subsys/kmu/pytest/common.py b/tests/subsys/kmu/pytest/common.py new file mode 100644 index 000000000000..46cc4e301dd4 --- /dev/null +++ b/tests/subsys/kmu/pytest/common.py @@ -0,0 +1,67 @@ +# Copyright (c) 2024 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +from __future__ import annotations + +import logging +import shlex +import subprocess + +from pathlib import Path + +logger = logging.getLogger(__name__) + +APP_KEYS_FOR_KMU = Path(__file__).resolve().parent.parent / 'keys' + + +def run_command(command: list[str], timeout: int = 30): + logger.info(f"CMD: {shlex.join(command)}") + ret: subprocess.CompletedProcess = subprocess.run( + command, text=True, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, timeout=timeout) + if ret.returncode: + logger.error(f"Failed command: {shlex.join(command)}") + logger.error(ret.stdout) + raise subprocess.CalledProcessError(ret.returncode, command) + + +def erase_board(dev_id: str | None): + command = [ + 'nrfutil', 'device', 'erase' + ] + if dev_id: + command.extend(['--serial-number', dev_id]) + run_command(command) + + +def flash_board(build_dir: Path | str, dev_id: str | None, erase: bool = False): + logger.info("Flash the board.") + command = [ + 'west', 'flash', '--skip-rebuild', + '-d', str(build_dir) + ] + if dev_id: + command.extend(['--dev-id', dev_id]) + if erase: + command.extend(['--erase']) + run_command(command) + + +def provision_keys_for_kmu(dev_id: str | None, key1: str | Path, + key2: str | Path | None = None, + key3: str | Path | None = None): + logger.info("Provision keys using west command. Erase the board first") + erase_board(dev_id) + command = [ + 'west', 'ncs-provision', 'upload', + '--soc', 'nrf54l15', + '--key', str(key1) + ] + if key2: + command.extend(['--key', str(key2)]) + if key3: + command.extend(['--key', str(key3)]) + if dev_id: + command.extend(['--dev-id', dev_id]) + run_command(command) + logger.info("Keys provisioned successfully") diff --git a/tests/subsys/kmu/pytest/conftest.py b/tests/subsys/kmu/pytest/conftest.py new file mode 100644 index 000000000000..d996a5b856c5 --- /dev/null +++ b/tests/subsys/kmu/pytest/conftest.py @@ -0,0 +1,12 @@ +# Copyright (c) 2024 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +import pytest +import logging + +logger = logging.getLogger(__name__) + + +@pytest.fixture(scope='function', autouse=True) +def test_log(request: pytest.FixtureRequest): + logging.info("========= Test '{}' STARTED".format(request.node.nodeid)) diff --git a/tests/subsys/kmu/pytest/test_kmu_key_provision.py b/tests/subsys/kmu/pytest/test_kmu_key_provision.py new file mode 100644 index 000000000000..2b324ed7b7e5 --- /dev/null +++ b/tests/subsys/kmu/pytest/test_kmu_key_provision.py @@ -0,0 +1,69 @@ +# Copyright (c) 2024 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +from __future__ import annotations + +import logging + +from pathlib import Path +from twister_harness import DeviceAdapter +from twister_harness.helpers.utils import match_lines, find_in_config +from common import ( + provision_keys_for_kmu, + flash_board, + APP_KEYS_FOR_KMU +) + +logger = logging.getLogger(__name__) + + +def test_kmu_correct_keys_uploaded(dut: DeviceAdapter): + """ + Upload valid keys to DUT using west ncs-provission command + and verify it in application. + """ + zephyr_base = find_in_config(dut.device_config.build_dir / 'CMakeCache.txt', 'ZEPHYR_BASE:PATH') + default_key = Path(zephyr_base).parent / 'bootloader' / 'mcuboot' / 'root-ed25519.pem' + provision_keys_for_kmu(dut.device_config.id, + key1=default_key, + key2=APP_KEYS_FOR_KMU / 'root-ed25519-1.pem', + key3=APP_KEYS_FOR_KMU / 'root-ed25519-2.pem') + + logger.info("Flash the board once again and check if keys are verified") + dut.clear_buffer() + flash_board(dut.device_config.build_dir, dut.device_config.id) + + lines = dut.readlines_until( + regex='Key 2 failed|Key 2 verified|PSA crypto init failed', + print_output=True, timeout=20) + + match_lines(lines, [ + 'Default key verified', + 'Key 1 verified', + 'Key 2 verified' + ]) + + +def test_kmu_wrong_keys_uploaded(dut: DeviceAdapter): + """ + Upload two wrong keys to DUT using west ncs-provission command + and verify it in application. + """ + provision_keys_for_kmu(dut.device_config.id, + key1=APP_KEYS_FOR_KMU / 'root-ed25519-w.pem', + key2=APP_KEYS_FOR_KMU / 'root-ed25519-1.pem', + key3=APP_KEYS_FOR_KMU / 'root-ed25519-w.pem') + + logger.info("Flash the board once again and check if keys are verified") + dut.clear_buffer() + flash_board(dut.device_config.build_dir, dut.device_config.id) + + lines = dut.readlines_until( + regex='Key 2 failed|Key 2 verified|PSA crypto init failed', + print_output=True, timeout=20) + + match_lines(lines, [ + 'Default key failed', + 'Key 1 verified', + 'Key 2 failed' + ]) diff --git a/tests/subsys/kmu/pytest/test_kmu_with_mcuboot.py b/tests/subsys/kmu/pytest/test_kmu_with_mcuboot.py new file mode 100644 index 000000000000..7c5383a4105b --- /dev/null +++ b/tests/subsys/kmu/pytest/test_kmu_with_mcuboot.py @@ -0,0 +1,72 @@ +# Copyright (c) 2024 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +from __future__ import annotations + +import logging + +from pathlib import Path +from twister_harness import DeviceAdapter +from twister_harness.helpers.utils import match_lines, find_in_config +from common import ( + provision_keys_for_kmu, + flash_board, + APP_KEYS_FOR_KMU +) + +logger = logging.getLogger(__name__) + + +def test_kmu_use_key_from_config(dut: DeviceAdapter): + """ + Upload proper key using west ncs-provision command, + verify that the application boots successfully. + """ + logger.info("Provision same key that was used during building") + signature_key_file = find_in_config(Path(dut.device_config.build_dir) / 'mcuboot' / 'zephyr' / '.config', + 'CONFIG_BOOT_SIGNATURE_KEY_FILE') + provision_keys_for_kmu(dut.device_config.id, key1=signature_key_file) + + dut.clear_buffer() + flash_board(dut.device_config.build_dir, dut.device_config.id) + + lines = dut.readlines_until( + regex='Unable to find bootable image|Jumping to the first image slot', + print_output=True, timeout=20) + + match_lines(lines, ['Jumping to the first image slot']) + logger.info("Passed: Booted succesvully after provisioning the same key that was used during building") + + +def test_kmu_use_predefined_keys(dut: DeviceAdapter): + """ + Upload keys using west ncs-provision command, + verify that the application boots successfully if the keys are correct, + and does not boot if the keys are incorrect. + """ + signature_key_file = find_in_config(Path(dut.device_config.build_dir) / 'mcuboot' / 'zephyr' / '.config', + 'CONFIG_BOOT_SIGNATURE_KEY_FILE') + zephyr_base = find_in_config(dut.device_config.build_dir / 'CMakeCache.txt', 'ZEPHYR_BASE:PATH') + default_key = Path(zephyr_base).parent / 'bootloader' / 'mcuboot' / 'root-ed25519.pem' + provision_keys_for_kmu(dut.device_config.id, + key1=default_key, + key2=APP_KEYS_FOR_KMU / 'root-ed25519-1.pem', + key3=APP_KEYS_FOR_KMU / 'root-ed25519-2.pem') + + dut.clear_buffer() + flash_board(dut.device_config.build_dir, dut.device_config.id) + + lines = dut.readlines_until( + regex='Unable to find bootable image|Jumping to the first image slot', + print_output=True, timeout=20) + + if 'root-ed25519-w.pem' in signature_key_file: + match_lines(lines, [ + 'ED25519 signature verification failed', + 'Image in the primary slot is not valid', + 'Unable to find bootable image' + ]) + logger.info("Passed: Not booted when used wrong keys") + else: + match_lines(lines, ['Jumping to the first image slot']) + logger.info("Passed: Booted with correct keys") diff --git a/tests/subsys/kmu/verify_west_ncs_provision/CMakeLists.txt b/tests/subsys/kmu/verify_west_ncs_provision/CMakeLists.txt new file mode 100644 index 000000000000..c187867c23c4 --- /dev/null +++ b/tests/subsys/kmu/verify_west_ncs_provision/CMakeLists.txt @@ -0,0 +1,12 @@ +# +# Copyright (c) 2024 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(verify_west_ncs_provision) + +target_sources(app PRIVATE src/main.c) diff --git a/tests/subsys/kmu/verify_west_ncs_provision/prj.conf b/tests/subsys/kmu/verify_west_ncs_provision/prj.conf new file mode 100644 index 000000000000..131672e9b76e --- /dev/null +++ b/tests/subsys/kmu/verify_west_ncs_provision/prj.conf @@ -0,0 +1,20 @@ +# +# Copyright (c) 2024 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# +CONFIG_NRF_SECURITY=y +CONFIG_PSA_WANT_ALG_PURE_EDDSA=y +CONFIG_PSA_WANT_ALG_SHA_512=y +CONFIG_PSA_WANT_ECC_TWISTED_EDWARDS_255=y +CONFIG_PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_IMPORT=y +CONFIG_MBEDTLS=n +CONFIG_MBEDTLS_ENABLE_HEAP=y +CONFIG_MBEDTLS_HEAP_SIZE=2048 +CONFIG_PSA_WANT_ALG_GCM=y +CONFIG_PSA_WANT_KEY_TYPE_AES=y +CONFIG_PSA_WANT_AES_KEY_SIZE_256=y +CONFIG_PSA_WANT_ALG_SP800_108_COUNTER_CMAC=y +CONFIG_PSA_WANT_ALG_CMAC=y +CONFIG_PSA_WANT_ALG_ECB_NO_PADDING=y +CONFIG_MAIN_STACK_SIZE=8192 diff --git a/tests/subsys/kmu/verify_west_ncs_provision/src/main.c b/tests/subsys/kmu/verify_west_ncs_provision/src/main.c new file mode 100644 index 000000000000..464ddd6f9098 --- /dev/null +++ b/tests/subsys/kmu/verify_west_ncs_provision/src/main.c @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2024 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ + +#include +#include +#include +#include +#include + + +/* The key slots for MCUboot are identified as UROT_PUBKEY[n], where n is [0,2] + * and each MCUboot key uses two slots for ED25519; the provided IDs are + * 230-231 (gen 2) + * 228-229 (gen 1) + * 226-227 (gen 0) + * and MCUboot key ID is identified by lower key id. + * There are two 32 byte key slots used per key, even though 255 bits fit in + * 32 bytes, because there is metadata also stored with the key and allocation + * of space is only allowed by 32 byte slots. + */ +#define UROT_PUBKEY_2 230 +#define UROT_PUBKEY_1 228 +#define UROT_PUBKEY_0 226 + +#define MK_PSA_KEY_HANDLE(key) \ + PSA_KEY_HANDLE_FROM_CRACEN_KMU_SLOT(CRACEN_KMU_KEY_USAGE_SCHEME_RAW, key) + +uint8_t signature_default[] = { + 0x2e, 0x89, 0x14, 0x67, 0x34, 0x61, 0x7c, 0x9f, + 0xbb, 0xa5, 0x65, 0x55, 0xb3, 0xb0, 0xfb, 0x2a, + 0x9c, 0x6e, 0xf6, 0xbf, 0x84, 0x2d, 0x10, 0x79, + 0x99, 0x08, 0xd3, 0x9f, 0xe0, 0xbb, 0x4a, 0x8a, + 0x9c, 0x3a, 0xc7, 0xb0, 0x24, 0xcb, 0xaa, 0xdb, + 0x99, 0xf3, 0x71, 0x2d, 0xe6, 0x6d, 0xb8, 0x52, + 0xa5, 0xa9, 0xc8, 0x06, 0xa6, 0xc3, 0xa9, 0x5b, + 0x54, 0x89, 0x9f, 0xe9, 0xe7, 0xc9, 0xb1, 0x02 +}; + +uint8_t signature_1[] = { + 0xd3, 0xba, 0x03, 0x82, 0x4c, 0x86, 0x0d, 0xea, + 0xcd, 0xf2, 0x83, 0xef, 0x28, 0x1e, 0x69, 0x45, + 0x13, 0x19, 0x20, 0xb0, 0x09, 0x84, 0x14, 0x81, + 0x97, 0x9f, 0xb0, 0xa0, 0x9e, 0x98, 0x3b, 0x34, + 0xda, 0x21, 0xfa, 0x28, 0x4d, 0xdf, 0xd3, 0xee, + 0x8f, 0x26, 0x27, 0xf4, 0xcd, 0x01, 0xee, 0x48, + 0x7d, 0x4f, 0x83, 0xf3, 0x1c, 0x63, 0x08, 0x70, + 0xd0, 0xe0, 0xff, 0x13, 0x57, 0x05, 0xda, 0x0b +}; + +uint8_t signature_2[] = { + 0x30, 0x4e, 0x66, 0x48, 0x29, 0x31, 0xbb, 0xbb, + 0x2a, 0xa9, 0xd4, 0xb9, 0xc0, 0xd2, 0x2d, 0x93, + 0xec, 0x98, 0x93, 0xef, 0xf1, 0x5f, 0x99, 0xc9, + 0x66, 0x9a, 0xa3, 0x54, 0x27, 0x9d, 0x70, 0x62, + 0x74, 0xf8, 0xaa, 0x17, 0xe4, 0xfc, 0x74, 0xbb, + 0xcd, 0x1f, 0x20, 0xfb, 0x06, 0xee, 0x16, 0xb3, + 0xb4, 0x40, 0xc5, 0xcc, 0xaa, 0xfd, 0x0f, 0x26, + 0x78, 0x51, 0xa4, 0x91, 0x77, 0xc8, 0x25, 0x01 +}; + +static const uint8_t test_message[] = { + 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, + 0x72, 0x6c, 0x64, 0x20, 0x50, 0x53, 0x41, 0x0a +}; + +static int test_verify_key(psa_key_id_t key, const uint8_t *signature, + size_t signature_len, const uint8_t *message, + size_t message_len) +{ + psa_status_t status = psa_verify_message(key, PSA_ALG_PURE_EDDSA, message, message_len, + signature, signature_len); + + if (status != PSA_SUCCESS) { + printk("Signature verification failed %d\n", status); + } + return status; +} + +int main(void) +{ + psa_status_t status = PSA_ERROR_BAD_STATE; + + status = psa_crypto_init(); + if (status != PSA_SUCCESS) { + printk("PSA crypto init failed with error %d\n", status); + return 0; + } + + status = test_verify_key(MK_PSA_KEY_HANDLE(UROT_PUBKEY_0), signature_default, + sizeof(signature_default), test_message, sizeof(test_message)); + if (status == PSA_SUCCESS) { + printk("Default key verified\n"); + } else { + printk("Default key failed\n"); + } + + status = test_verify_key(MK_PSA_KEY_HANDLE(UROT_PUBKEY_1), signature_1, + sizeof(signature_1), test_message, sizeof(test_message)); + if (status == PSA_SUCCESS) { + printk("Key 1 verified\n"); + } else { + printk("Key 1 failed\n"); + } + + status = test_verify_key(MK_PSA_KEY_HANDLE(UROT_PUBKEY_2), signature_2, + sizeof(signature_2), test_message, sizeof(test_message)); + if (status == PSA_SUCCESS) { + printk("Key 2 verified\n"); + } else { + printk("Key 2 failed\n"); + } + + return 0; +} diff --git a/tests/subsys/kmu/verify_west_ncs_provision/testcase.yaml b/tests/subsys/kmu/verify_west_ncs_provision/testcase.yaml new file mode 100644 index 000000000000..a685765f3f65 --- /dev/null +++ b/tests/subsys/kmu/verify_west_ncs_provision/testcase.yaml @@ -0,0 +1,12 @@ +common: + timeout: 120 + tags: pytest kmu + platform_allow: + - nrf54l15dk/nrf54l15/cpuapp +tests: + kmu.west.provision.keys: + harness: pytest + harness_config: + pytest_dut_scope: session + pytest_root: + - "../pytest/test_kmu_key_provision.py"