Skip to content

Commit f86a548

Browse files
committed
Passing integration tests.
1 parent af61634 commit f86a548

File tree

11 files changed

+689
-0
lines changed

11 files changed

+689
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(integration)
6+
7+
# include_directories(BEFORE
8+
# ${ZEPHYR_BASE}/tests/bluetooth/host/cs/channel_sounding/mocks_unit_tests
9+
# )
10+
11+
FILE(GLOB app_sources src/*.c)
12+
target_sources(app PRIVATE ${app_sources})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
CONFIG_ZTEST=y
2+
3+
# # # Mbed TLS
4+
CONFIG_MBEDTLS=y
5+
CONFIG_MBEDTLS_BUILTIN=y
6+
# CONFIG_MBEDTLS_ECP_MAX_BITS=512
7+
CONFIG_MBEDTLS_PSA_CRYPTO_C=y
8+
9+
CONFIG_REBOOT=y
10+
11+
CONFIG_BT=y
12+
CONFIG_BT_HCI=y
13+
14+
CONFIG_LOG=y
15+
CONFIG_LOG_DEFAULT_LEVEL=3
16+
CONFIG_ASSERT=y
17+
CONFIG_SECURE_STORAGE=y
18+
19+
CONFIG_PSA_WANT_KEY_TYPE_AES=y
20+
CONFIG_PSA_WANT_ALG_CTR=y
21+
# CONFIG_PSA_WANT_ALG_ECDH=y
22+
23+
CONFIG_ENTROPY_GENERATOR=y
24+
25+
CONFIG_TEST_RANDOM_GENERATOR=y
26+
CONFIG_TIMER_RANDOM_GENERATOR=y
27+
CONFIG_SECURE_STORAGE_ITS_IMPLEMENTATION_ZEPHYR=y
28+
CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG_ALLOW_NON_CSPRNG=y
29+
30+
CONFIG_SECURE_STORAGE_ITS_STORE_IMPLEMENTATION_SETTINGS=y
31+
CONFIG_SETTINGS_NVS=y
32+
CONFIG_NVS=y
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
* Copyright (c) 2016 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/ztest.h>
8+
9+
#include <psa/crypto.h>
10+
#include <zephyr/bluetooth/bluetooth.h>
11+
#include <zephyr/sys/reboot.h>
12+
#include <zephyr/logging/log.h>
13+
14+
#define SAMPLE_KEY_ID PSA_KEY_ID_USER_MIN
15+
#define SAMPLE_KEY_TYPE PSA_KEY_TYPE_AES
16+
#define SAMPLE_ALG PSA_ALG_CTR
17+
#define SAMPLE_KEY_BITS 256
18+
19+
LOG_MODULE_REGISTER(psa_hci_integration_tests);
20+
21+
psa_status_t generate_key_helper(psa_key_id_t* key_id, psa_algorithm_t algorithm, size_t key_bits);
22+
23+
void setup_pre_reboot(){
24+
// Use this function as a setup for the next testsuite that should simulate reboot
25+
psa_status_t ret;
26+
psa_key_id_t key_id;
27+
ret = generate_key_helper(&key_id, SAMPLE_ALG, SAMPLE_KEY_BITS);
28+
zassert_equal(ret, PSA_SUCCESS, "Key generation failed: %d", ret);
29+
30+
// Don't destroy the key, but purge it for good measure
31+
ret = psa_purge_key(SAMPLE_KEY_ID);
32+
zassert_equal(ret, PSA_SUCCESS, "Failed to purge the key: %d", ret);
33+
}
34+
35+
ZTEST_SUITE(psa_hci_integration_tests, NULL, NULL, NULL, NULL, setup_pre_reboot);
36+
37+
static uint8_t plaintext[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF};
38+
static uint8_t ciphertext[PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(SAMPLE_KEY_TYPE, SAMPLE_ALG,
39+
sizeof(plaintext))];
40+
static size_t ct_len;
41+
42+
psa_status_t generate_key_helper(psa_key_id_t* key_id, psa_algorithm_t algorithm, size_t key_bits) {
43+
psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
44+
45+
psa_set_key_lifetime(&key_attributes, PSA_KEY_LIFETIME_PERSISTENT);
46+
psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
47+
psa_set_key_id(&key_attributes, SAMPLE_KEY_ID);
48+
psa_set_key_type(&key_attributes, SAMPLE_KEY_TYPE);
49+
psa_set_key_algorithm(&key_attributes, algorithm);
50+
psa_set_key_bits(&key_attributes, key_bits);
51+
52+
// //HACK: remove when the test is ready
53+
// psa_destroy_key(SAMPLE_KEY_ID);
54+
55+
return psa_generate_key(&key_attributes, key_id);
56+
}
57+
58+
ZTEST(psa_hci_integration_tests, test_psa_persistence_encrypt_decrypt) {
59+
psa_status_t ret;
60+
psa_key_id_t key_id;
61+
62+
// Create a persistent signing key
63+
ret = generate_key_helper(&key_id, SAMPLE_ALG, SAMPLE_KEY_BITS);
64+
zassert_equal(ret, PSA_SUCCESS, "Key gen failed: %d", ret);
65+
66+
ret = psa_cipher_encrypt(key_id, SAMPLE_ALG, plaintext, sizeof(plaintext), ciphertext, sizeof(ciphertext), &ct_len);
67+
68+
LOG_INF("Encrypted %d bytes", ct_len);
69+
70+
zassert_equal(ret, PSA_SUCCESS, "Encryption failed: %d", ret);
71+
72+
// Purge key from volatile storage
73+
ret = psa_purge_key(SAMPLE_KEY_ID);
74+
zassert_equal(ret, PSA_SUCCESS, "Failed to purge the key: %d", ret);
75+
76+
/* Open persisted key */
77+
ret = psa_open_key(SAMPLE_KEY_ID, &key_id);
78+
zassert_equal(ret, PSA_SUCCESS, "Open failed: %d", ret);
79+
80+
uint8_t decrypted[sizeof(plaintext)];
81+
size_t dlen;
82+
83+
/* Verify key usability */
84+
ret = psa_cipher_decrypt(
85+
key_id, SAMPLE_ALG,
86+
ciphertext, ct_len,
87+
decrypted, sizeof(decrypted), &dlen
88+
);
89+
zassert_equal(ret, PSA_SUCCESS, "Decrypt failed: %d", ret);
90+
zassert_mem_equal(plaintext, decrypted, sizeof(plaintext),
91+
"Data corrupted");
92+
93+
/* Cleanup */
94+
psa_destroy_key(key_id);
95+
}
96+
97+
98+
ZTEST(psa_hci_integration_tests, test_psa_key_deletion_and_regeneration) {
99+
psa_status_t ret;
100+
psa_key_id_t key_id;
101+
102+
// Generate the key
103+
ret = generate_key_helper(&key_id, SAMPLE_ALG, SAMPLE_KEY_BITS);
104+
zassert_equal(ret, PSA_SUCCESS, "Key generation failed: %d", ret);
105+
106+
// Encrypt some data with the key
107+
ret = psa_cipher_encrypt(key_id, SAMPLE_ALG, plaintext, sizeof(plaintext), ciphertext, sizeof(ciphertext), &ct_len);
108+
zassert_equal(ret, PSA_SUCCESS, "Encryption failed: %d", ret);
109+
110+
// Destroy the key
111+
ret = psa_destroy_key(key_id);
112+
zassert_equal(ret, PSA_SUCCESS, "Failed to destroy key: %d", ret);
113+
114+
// Attempt to re-generate the key
115+
ret = generate_key_helper(&key_id, SAMPLE_ALG, SAMPLE_KEY_BITS);
116+
zassert_equal(ret, PSA_SUCCESS, "Key re-generation failed: %d", ret);
117+
118+
// Attempt to decrypt the ciphertext encrypted using the different key (should fail)
119+
uint8_t decrypted[sizeof(plaintext)];
120+
size_t dlen;
121+
ret = psa_cipher_decrypt(key_id, SAMPLE_ALG, ciphertext, ct_len, decrypted, sizeof(decrypted), &dlen);
122+
zassert_equal(ret, PSA_SUCCESS, "Decryption failed: %d", ret);
123+
zassert_true(memcmp(plaintext, decrypted, sizeof(plaintext)) != 0, "Decryption succeeded with a different key!");
124+
125+
// Cleanup
126+
psa_destroy_key(key_id);
127+
}
128+
129+
ZTEST(psa_hci_integration_tests, test_psa_invalid_key_usage) {
130+
psa_status_t ret;
131+
132+
// Try using an ungenerated key (should fail)
133+
psa_key_id_t bad_key_id = (psa_key_id_t)0xBAADBEEF;
134+
ret = psa_cipher_encrypt(bad_key_id, SAMPLE_ALG, plaintext, sizeof(plaintext), ciphertext, sizeof(ciphertext), &ct_len);
135+
zassert_equal(ret, PSA_ERROR_INVALID_HANDLE, "Expected failure due to invalid key handle");
136+
137+
// Try using a destroyed key
138+
psa_key_id_t key_id;
139+
ret = generate_key_helper(&key_id, SAMPLE_ALG, SAMPLE_KEY_BITS);
140+
zassert_equal(ret, PSA_SUCCESS, "Key generation failed: %d", ret);
141+
142+
// Destroy the key
143+
ret = psa_destroy_key(key_id);
144+
zassert_equal(ret, PSA_SUCCESS, "Failed to destroy key: %d", ret);
145+
146+
// Attempt to use the destroyed key (should fail)
147+
uint8_t decrypted[sizeof(plaintext)];
148+
size_t dlen;
149+
ret = psa_cipher_decrypt(key_id, SAMPLE_ALG, ciphertext, ct_len, decrypted, sizeof(decrypted), &dlen);
150+
zassert_equal(ret, PSA_ERROR_INVALID_HANDLE, "Decryption should fail with invalid key");
151+
152+
// Cleanup
153+
psa_destroy_key(key_id);
154+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2016 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/ztest.h>
8+
#include <psa/crypto.h>
9+
#include <zephyr/logging/log.h>
10+
11+
LOG_MODULE_REGISTER(psa_hci_reboot_integration_tests);
12+
13+
ZTEST_SUITE(psa_hci_reboot_integration_tests, NULL, NULL, NULL, NULL, NULL);
14+
15+
#define SAMPLE_KEY_ID PSA_KEY_ID_USER_MIN
16+
#define SAMPLE_KEY_TYPE PSA_KEY_TYPE_AES
17+
#define SAMPLE_ALG PSA_ALG_CTR
18+
#define SAMPLE_KEY_BITS 256
19+
20+
static uint8_t plaintext[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF};
21+
static uint8_t ciphertext[PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(SAMPLE_KEY_TYPE, SAMPLE_ALG,
22+
sizeof(plaintext))];
23+
static size_t ct_len;
24+
25+
ZTEST(psa_hci_reboot_integration_tests, test_psa_persistence_after_reboot) {
26+
psa_status_t ret;
27+
psa_key_id_t key_id;
28+
29+
ret = psa_open_key(SAMPLE_KEY_ID, &key_id);
30+
zassert_equal(ret, PSA_SUCCESS, "Open failed: %d", ret);
31+
32+
// Encrypt the data
33+
ret = psa_cipher_encrypt(key_id, SAMPLE_ALG, plaintext, sizeof(plaintext), ciphertext, sizeof(ciphertext), &ct_len);
34+
zassert_equal(ret, PSA_SUCCESS, "Encryption failed: %d", ret);
35+
36+
// Decrypt the ciphertext to verify persistence
37+
uint8_t decrypted[sizeof(plaintext)];
38+
size_t dlen;
39+
ret = psa_cipher_decrypt(key_id, SAMPLE_ALG, ciphertext, ct_len, decrypted, sizeof(decrypted), &dlen);
40+
zassert_equal(ret, PSA_SUCCESS, "Decryption failed after reboot: %d", ret);
41+
zassert_mem_equal(plaintext, decrypted, sizeof(plaintext), "Decryption resulted in corrupted data after reboot");
42+
43+
// Cleanup
44+
psa_destroy_key(key_id);
45+
}
46+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
tests:
2+
# section.subsection
3+
sample.testing.ztest:
4+
# build_only: true
5+
platform_allow:
6+
- native_posix
7+
- native_sim
8+
- qemu_cortex_m3
9+
integration_platforms:
10+
- native_sim
11+
- qemu_cortex_m3
12+
tags: test_framework
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(integration)
6+
7+
include_directories(BEFORE
8+
${ZEPHYR_BASE}/bluetooth/common/
9+
)
10+
11+
FILE(GLOB app_sources src/*.c)
12+
target_sources(app PRIVATE ${app_sources})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
CONFIG_ZTEST=y
2+
3+
# # # Mbed TLS
4+
CONFIG_MBEDTLS=y
5+
CONFIG_MBEDTLS_BUILTIN=y
6+
# CONFIG_MBEDTLS_ECP_MAX_BITS=512
7+
CONFIG_MBEDTLS_PSA_CRYPTO_C=y
8+
9+
CONFIG_BT_BROADCASTER=y
10+
11+
CONFIG_BT=y
12+
CONFIG_BT_SMP=y
13+
# CONFIG_BT_CONN=y
14+
CONFIG_BT_HCI=y
15+
# CONFIG_BT_HCI_HOST=y
16+
17+
CONFIG_LOG=y
18+
CONFIG_LOG_DEFAULT_LEVEL=4
19+
CONFIG_ASSERT=y
20+
CONFIG_SECURE_STORAGE=y
21+
22+
CONFIG_PSA_WANT_KEY_TYPE_AES=y
23+
CONFIG_PSA_WANT_ALG_CTR=y
24+
# CONFIG_PSA_WANT_ALG_ECDH=y
25+
26+
CONFIG_ENTROPY_GENERATOR=y
27+
28+
CONFIG_TEST_RANDOM_GENERATOR=y
29+
CONFIG_TIMER_RANDOM_GENERATOR=y
30+
CONFIG_SECURE_STORAGE_ITS_IMPLEMENTATION_ZEPHYR=y
31+
CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG_ALLOW_NON_CSPRNG=y
32+
33+
CONFIG_SECURE_STORAGE_ITS_STORE_IMPLEMENTATION_SETTINGS=y
34+
CONFIG_SETTINGS_NVS=y
35+
CONFIG_NVS=y
36+
37+
CONFIG_BT_PRIVACY=y
38+
# CONFIG_BT_RPA=y
39+
CONFIG_BT_RPA_LOG_LEVEL_DBG=y
40+
# Enable creating more than one identity:
41+
#
42+
# 2 ids for test_different_irks_yield_different_rpas
43+
# 1 id for test_null_irk_generates_random_irk
44+
# 1 id for test_rpa_is_stable_before_timeout
45+
CONFIG_BT_ID_MAX=7
46+
CONFIG_BT_CENTRAL=y
47+
CONFIG_BT_RPA_TIMEOUT=30
48+
CONFIG_BT_LOG_SNIFFER_INFO=y
49+
# Enable dynamic RPA timeout manipulation
50+
CONFIG_BT_RPA_TIMEOUT_DYNAMIC=y
51+
CONFIG_COVERAGE=y

0 commit comments

Comments
 (0)