Skip to content

Commit 8c02821

Browse files
committed
tests: crypto: Add and refactor wrapped key support in PSA crypto tests
This commit consolidates and refactors wrapped key test functionality in the PSA crypto test suite, improving maintainability and readability. Key updates include: 1. Added support for testing wrapped keys in PSA crypto tests. 2. Introduced a new Kconfig option TEST_WRAPPED_KEYS to enable wrapped key testing. 3. Updated AEAD, Cipher, Hash, Key Agreement, and Sign test cases to: - Include sl_si91x_psa_wrap.h when CONFIG_TEST_WRAPPED_KEYS is set. - Configure key lifetime using PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION for wrapped key tests. - Import wrapped keys for AES, ChaChaPoly, and ECDSA when wrapper mode is active. - Print debug messages when wrapper key mode is enabled. 4. Moved wrapper key configuration to the 917-specific implementation and cleaned up redundant SoC-level handling. 5. Replaced preprocessor checks with IS_ENABLED() to improve readability and ensure both code paths are compiled and type-checked. 6. Cleaned up redundant preprocessor blocks, fixed indentation, and resolved build and style compliance issues per Zephyr guidelines. 7. Normalized comment alignment and indentation to meet clang-format and checkpatch requirements. These updates enhance test coverage, maintain consistency, and simplify wrapped key configuration across PSA crypto test modules. Signed-off-by: Aasim Shaik <[email protected]>
1 parent 66e622c commit 8c02821

File tree

7 files changed

+150
-171
lines changed

7 files changed

+150
-171
lines changed

soc/silabs/silabs_siwx917/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,10 @@ config MBEDTLS_USER_CONFIG_FILE
2121
default "sl_mbedtls_config_zephyr.h" \
2222
if PSA_CRYPTO_DRIVER_SILABS_SIWX91X
2323

24+
if SOC_SERIES_SIWG917
25+
config TEST_WRAPPED_KEYS
26+
bool "Enable Wrapper keys - specific to SiWG917"
27+
endif
28+
2429
endif
2530
endif

tests/crypto/psa_crypto/src/aead.c

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
/*
2-
* Copyright (c) 2025 Silicon Laboratories Inc.
3-
*
4-
* SPDX-License-Identifier: Apache-2.0
5-
*/
2+
* Copyright (c) 2025 Silicon Laboratories Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
66

77
#include <zephyr/ztest.h>
88
#include <psa/crypto.h>
99

1010
const uint8_t aes_key_buf[] = {0xea, 0x4f, 0x6f, 0x3c, 0x2f, 0xed, 0x2b, 0x9d,
11-
0xd9, 0x70, 0x8c, 0x2e, 0x72, 0x1a, 0xe0, 0x0f};
11+
0xd9, 0x70, 0x8c, 0x2e, 0x72, 0x1a, 0xe0, 0x0f};
1212
const uint8_t aes_nonce_buf[] = {0xf9, 0x75, 0x80, 0x9d, 0xdb, 0x51,
13-
0x72, 0x38, 0x27, 0x45, 0x63, 0x4f};
13+
0x72, 0x38, 0x27, 0x45, 0x63, 0x4f};
1414
const uint8_t aes_ad_buf[] = {0x5c, 0x65, 0xd4, 0xf2, 0x61, 0xd2, 0xc5, 0x4f, 0xfe, 0x6a};
1515
const uint8_t aes_plaintext[] = {0x8d, 0x6c, 0x08, 0x44, 0x6c, 0xb1, 0x0d, 0x9a, 0x20, 0x75};
1616

1717
const uint8_t chachapoly_key_buf[] = {0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
18-
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
19-
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
20-
0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f};
18+
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
19+
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
20+
0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f};
2121
const uint8_t chachapoly_nonce_buf[] = {0x07, 0x00, 0x00, 0x00, 0x40, 0x41,
2222
0x42, 0x43, 0x44, 0x45, 0x46, 0x47};
2323
const uint8_t chachapoly_ad_buf[] = {0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1,
24-
0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7};
24+
0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7};
2525
const uint8_t chachapoly_plaintext[] = {
2626
0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74,
2727
0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c,
@@ -62,25 +62,23 @@ ZTEST(psa_crypto_test, test_aead_aes_ccm)
6262
psa_set_key_algorithm(&attributes, alg);
6363

6464
zassert_equal(psa_import_key(&attributes, aes_key_buf, sizeof(aes_key_buf), &key_id),
65-
PSA_SUCCESS, "Failed to import key");
65+
PSA_SUCCESS, "Failed to import key");
6666

6767
zassert_equal(psa_aead_encrypt(key_id, alg, aes_nonce_buf, sizeof(aes_nonce_buf),
68-
aes_ad_buf, sizeof(aes_ad_buf), aes_plaintext,
69-
sizeof(aes_plaintext), cipher_tag_buf,
70-
sizeof(cipher_tag_buf), &out_len),
71-
PSA_SUCCESS, "Failed to encrypt");
68+
aes_ad_buf, sizeof(aes_ad_buf), aes_plaintext,
69+
sizeof(aes_plaintext), cipher_tag_buf,
70+
sizeof(cipher_tag_buf), &out_len),
71+
PSA_SUCCESS, "Failed to encrypt");
7272

7373
zassert_equal(out_len, sizeof(expect_cipher_tag_buf));
7474
zassert_mem_equal(cipher_tag_buf, expect_cipher_tag_buf, sizeof(expect_cipher_tag_buf));
75-
7675
zassert_equal(psa_aead_decrypt(key_id, alg, aes_nonce_buf, sizeof(aes_nonce_buf),
77-
aes_ad_buf, sizeof(aes_ad_buf), cipher_tag_buf, out_len,
78-
decrypted, sizeof(decrypted), &out_len),
79-
PSA_SUCCESS, "Failed to decrypt");
76+
aes_ad_buf, sizeof(aes_ad_buf), cipher_tag_buf, out_len,
77+
decrypted, sizeof(decrypted), &out_len),
78+
PSA_SUCCESS, "Failed to decrypt");
8079

8180
zassert_equal(out_len, sizeof(aes_plaintext));
8281
zassert_mem_equal(decrypted, aes_plaintext, sizeof(aes_plaintext));
83-
8482
zassert_equal(psa_destroy_key(key_id), PSA_SUCCESS, "Failed to destroy key");
8583
}
8684

@@ -103,21 +101,21 @@ ZTEST(psa_crypto_test, test_aead_aes_gcm)
103101
psa_set_key_algorithm(&attributes, alg);
104102

105103
zassert_equal(psa_import_key(&attributes, aes_key_buf, sizeof(aes_key_buf), &key_id),
106-
PSA_SUCCESS, "Failed to import key");
104+
PSA_SUCCESS, "Failed to import key");
107105

108106
zassert_equal(psa_aead_encrypt(key_id, alg, aes_nonce_buf, sizeof(aes_nonce_buf),
109-
aes_ad_buf, sizeof(aes_ad_buf), aes_plaintext,
110-
sizeof(aes_plaintext), cipher_tag_buf,
111-
sizeof(cipher_tag_buf), &out_len),
112-
PSA_SUCCESS, "Failed to encrypt");
107+
aes_ad_buf, sizeof(aes_ad_buf), aes_plaintext,
108+
sizeof(aes_plaintext), cipher_tag_buf,
109+
sizeof(cipher_tag_buf), &out_len),
110+
PSA_SUCCESS, "Failed to encrypt");
113111

114112
zassert_equal(out_len, sizeof(expect_cipher_tag_buf));
115113
zassert_mem_equal(cipher_tag_buf, expect_cipher_tag_buf, sizeof(expect_cipher_tag_buf));
116114

117115
zassert_equal(psa_aead_decrypt(key_id, alg, aes_nonce_buf, sizeof(aes_nonce_buf),
118-
aes_ad_buf, sizeof(aes_ad_buf), cipher_tag_buf, out_len,
119-
decrypted, sizeof(decrypted), &out_len),
120-
PSA_SUCCESS, "Failed to decrypt");
116+
aes_ad_buf, sizeof(aes_ad_buf), cipher_tag_buf, out_len,
117+
decrypted, sizeof(decrypted), &out_len),
118+
PSA_SUCCESS, "Failed to decrypt");
121119

122120
zassert_equal(out_len, sizeof(aes_plaintext));
123121
zassert_mem_equal(decrypted, aes_plaintext, sizeof(aes_plaintext));
@@ -130,7 +128,6 @@ ZTEST(psa_crypto_test, test_aead_chacha20_poly1305)
130128
uint8_t cipher_tag_buf[130]; /* Ciphertext + Tag */
131129
uint8_t decrypted[sizeof(chachapoly_plaintext)] = {0};
132130
size_t out_len;
133-
134131
psa_key_id_t key_id;
135132
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
136133
psa_algorithm_t alg = PSA_ALG_CHACHA20_POLY1305;
@@ -140,28 +137,27 @@ ZTEST(psa_crypto_test, test_aead_chacha20_poly1305)
140137
psa_set_key_algorithm(&attributes, alg);
141138

142139
zassert_equal(psa_import_key(&attributes, chachapoly_key_buf, sizeof(chachapoly_key_buf),
143-
&key_id),
144-
PSA_SUCCESS, "Failed to import key");
140+
&key_id),
141+
PSA_SUCCESS, "Failed to import key");
145142

146143
zassert_equal(psa_aead_encrypt(key_id, alg, chachapoly_nonce_buf,
147-
sizeof(chachapoly_nonce_buf), chachapoly_ad_buf,
148-
sizeof(chachapoly_ad_buf), chachapoly_plaintext,
149-
sizeof(chachapoly_plaintext), cipher_tag_buf,
150-
sizeof(cipher_tag_buf), &out_len),
151-
PSA_SUCCESS, "Failed to encrypt");
144+
sizeof(chachapoly_nonce_buf), chachapoly_ad_buf,
145+
sizeof(chachapoly_ad_buf), chachapoly_plaintext,
146+
sizeof(chachapoly_plaintext), cipher_tag_buf,
147+
sizeof(cipher_tag_buf), &out_len),
148+
PSA_SUCCESS, "Failed to encrypt");
152149

153150
zassert_equal(out_len, sizeof(chachapoly_expect_cipher_tag_buf));
154151
zassert_mem_equal(cipher_tag_buf, chachapoly_expect_cipher_tag_buf,
155-
sizeof(chachapoly_expect_cipher_tag_buf));
152+
sizeof(chachapoly_expect_cipher_tag_buf));
156153

157154
zassert_equal(psa_aead_decrypt(key_id, alg, chachapoly_nonce_buf,
158-
sizeof(chachapoly_nonce_buf), chachapoly_ad_buf,
159-
sizeof(chachapoly_ad_buf), cipher_tag_buf, out_len,
160-
decrypted, sizeof(decrypted), &out_len),
161-
PSA_SUCCESS, "Failed to decrypt");
155+
sizeof(chachapoly_nonce_buf), chachapoly_ad_buf,
156+
sizeof(chachapoly_ad_buf), cipher_tag_buf, out_len,
157+
decrypted, sizeof(decrypted), &out_len),
158+
PSA_SUCCESS, "Failed to decrypt");
162159

163160
zassert_equal(out_len, sizeof(chachapoly_plaintext));
164161
zassert_mem_equal(decrypted, chachapoly_plaintext, sizeof(chachapoly_plaintext));
165-
166162
zassert_equal(psa_destroy_key(key_id), PSA_SUCCESS, "Failed to destroy key");
167163
}
Lines changed: 42 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2-
* Copyright (c) 2025 Silicon Laboratories Inc.
3-
*
4-
* SPDX-License-Identifier: Apache-2.0
5-
*/
2+
* Copyright (c) 2025 Silicon Laboratories Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
66

77
#include <zephyr/ztest.h>
88
#include <psa/crypto.h>
@@ -40,44 +40,43 @@ ZTEST(psa_crypto_test, test_cipher_aes_cbc_256_multipart)
4040
psa_set_key_algorithm(&attributes, alg);
4141
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
4242
psa_set_key_bits(&attributes, 256);
43-
if (IS_ENABLED(TEST_WRAPPED_KEYS)) {
43+
#if (CONFIG_TEST_WRAPPED_KEYS)
4444
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
45-
PSA_KEY_PERSISTENCE_VOLATILE, 1));
45+
PSA_KEY_PERSISTENCE_VOLATILE, 0));
4646
zassert_equal(psa_generate_key(&attributes, &key_id), PSA_SUCCESS,
47-
"Failed to generate key");
48-
} else {
47+
"Failed to generate key");
48+
# else
4949
zassert_equal(psa_import_key(&attributes, key_256, sizeof(key_256), &key_id),
50-
PSA_SUCCESS, "Failed to import key");
51-
}
50+
PSA_SUCCESS, "Failed to import key");
51+
#endif
5252
psa_reset_key_attributes(&attributes);
53-
5453
zassert_equal(psa_cipher_encrypt_setup(&operation, key_id, alg), PSA_SUCCESS,
55-
"Failed to begin encrypt operation");
54+
"Failed to begin encrypt operation");
5655
zassert_equal(psa_cipher_generate_iv(&operation, iv, sizeof(iv), &iv_len), PSA_SUCCESS,
57-
"Failed to generate IV");
56+
"Failed to generate IV");
5857
zassert_equal(psa_cipher_update(&operation, plaintext, sizeof(plaintext), ciphertext,
59-
sizeof(ciphertext), &ciphertext_len),
60-
PSA_SUCCESS, "Failed to update encrypt operation");
58+
sizeof(ciphertext), &ciphertext_len),
59+
PSA_SUCCESS, "Failed to update encrypt operation");
6160
zassert_equal(psa_cipher_finish(&operation, ciphertext + ciphertext_len,
62-
sizeof(ciphertext) - ciphertext_len, &ciphertext_len),
63-
PSA_SUCCESS, "Failed to finish encrypt operation");
61+
sizeof(ciphertext) - ciphertext_len, &ciphertext_len),
62+
PSA_SUCCESS, "Failed to finish encrypt operation");
6463

6564
psa_cipher_abort(&operation);
6665

6766
zassert(memcmp(ciphertext, plaintext, sizeof(plaintext)) != 0,
6867
"Ciphertext identical to plaintext");
6968

7069
zassert_equal(psa_cipher_decrypt_setup(&operation, key_id, alg), PSA_SUCCESS,
71-
"Failed to begin decrypt operation");
70+
"Failed to begin decrypt operation");
7271
zassert_equal(psa_cipher_set_iv(&operation, iv, sizeof(iv)), PSA_SUCCESS,
73-
"Failed to set IV");
72+
"Failed to set IV");
7473
zassert_equal(psa_cipher_update(&operation, ciphertext, sizeof(ciphertext), decrypted,
7574
sizeof(decrypted), &decrypted_len),
76-
PSA_SUCCESS, "Failed to update decrypt operation");
75+
PSA_SUCCESS, "Failed to update decrypt operation");
7776
zassert_equal(decrypted_len, sizeof(decrypted), "Decrypted length mismatch");
7877
zassert_equal(psa_cipher_finish(&operation, decrypted + decrypted_len,
7978
sizeof(decrypted) - decrypted_len, &decrypted_len),
80-
PSA_SUCCESS, "Failed to finish decrypt operation");
79+
PSA_SUCCESS, "Failed to finish decrypt operation");
8180

8281
psa_cipher_abort(&operation);
8382

@@ -96,42 +95,31 @@ ZTEST(psa_crypto_test, test_cipher_aes_cbc_256_single)
9695
psa_set_key_algorithm(&attributes, alg);
9796
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
9897
psa_set_key_bits(&attributes, 256);
99-
if (IS_ENABLED(TEST_WRAPPED_KEYS)) {
98+
#if (CONFIG_TEST_WRAPPED_KEYS)
10099
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
101-
PSA_KEY_PERSISTENCE_VOLATILE, 1));
100+
PSA_KEY_PERSISTENCE_VOLATILE, 1));
102101
zassert_equal(psa_generate_key(&attributes, &key_id), PSA_SUCCESS,
103-
"Failed to generate key");
104-
} else {
102+
"Failed to generate key");
103+
# else
105104
zassert_equal(psa_import_key(&attributes, key_256, sizeof(key_256), &key_id),
106-
PSA_SUCCESS, "Failed to import key");
107-
}
105+
PSA_SUCCESS, "Failed to import key");
106+
#endif
108107
psa_reset_key_attributes(&attributes);
109108

110109
zassert_equal(psa_cipher_encrypt(key_id, alg, plaintext, sizeof(plaintext),
111-
ciphertext_buffer_256, sizeof(ciphertext_buffer_256),
112-
&ciphertext_len),
113-
PSA_SUCCESS, "Failed to perform one-shot encrypt operation");
110+
ciphertext_buffer_256, sizeof(ciphertext_buffer_256),
111+
&ciphertext_len),
112+
PSA_SUCCESS, "Failed to perform one-shot encrypt operation");
114113

115114
zassert(memcmp(ciphertext_buffer_256, plaintext, sizeof(plaintext)) != 0,
116115
"Ciphertext identical to plaintext");
117116

118117
zassert_equal(psa_cipher_decrypt(key_id, alg, ciphertext_buffer_256, ciphertext_len,
119-
decrypted, sizeof(decrypted), &decrypted_len),
120-
PSA_SUCCESS, "Failed to perform one-shot decrypt operation");
118+
decrypted, sizeof(decrypted), &decrypted_len),
119+
PSA_SUCCESS, "Failed to perform one-shot decrypt operation");
121120

122121
zassert_equal(decrypted_len, sizeof(decrypted), "Decrypted length mismatch");
123122
zassert_mem_equal(decrypted, plaintext, sizeof(plaintext));
124-
125-
ciphertext_buffer_256[0] += 1;
126-
zassert_equal(psa_cipher_decrypt(key_id, alg, ciphertext_buffer_256, ciphertext_len,
127-
decrypted, sizeof(decrypted), &decrypted_len),
128-
PSA_SUCCESS,
129-
"Failed to perform one-shot decrypt operation with modified ciphertext");
130-
131-
zassert_equal(decrypted_len, sizeof(decrypted), "Decrypted length mismatch");
132-
zassert(memcmp(decrypted, plaintext, sizeof(plaintext)) != 0,
133-
"Decrypted modified data identical to original plaintext");
134-
135123
psa_destroy_key(key_id);
136124
}
137125

@@ -145,41 +133,31 @@ ZTEST(psa_crypto_test, test_cipher_aes_ecb_128_single)
145133
psa_set_key_algorithm(&attributes, alg);
146134
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
147135
psa_set_key_bits(&attributes, 128);
148-
if (IS_ENABLED(TEST_WRAPPED_KEYS)) {
136+
#if (CONFIG_TEST_WRAPPED_KEYS)
149137
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
150-
PSA_KEY_PERSISTENCE_VOLATILE, 1));
138+
PSA_KEY_PERSISTENCE_VOLATILE, 1));
151139

152140
zassert_equal(psa_generate_key(&attributes, &key_id), PSA_SUCCESS,
153-
"Failed to generate key");
154-
} else {
141+
"Failed to generate key");
142+
# else
155143
zassert_equal(psa_import_key(&attributes, key_128, sizeof(key_128), &key_id),
156-
PSA_SUCCESS, "Failed to import key");
157-
}
144+
PSA_SUCCESS, "Failed to import key");
145+
#endif
158146
psa_reset_key_attributes(&attributes);
159147

160148
zassert_equal(psa_cipher_encrypt(key_id, alg, plaintext, sizeof(plaintext), ciphertext,
161149
sizeof(ciphertext), &ciphertext_len),
162-
PSA_SUCCESS, "Failed to perform one-shot encrypt operation");
150+
PSA_SUCCESS, "Failed to perform one-shot encrypt operation");
163151

164152
zassert(memcmp(ciphertext, plaintext, sizeof(plaintext)) != 0,
165153
"Ciphertext identical to plaintext");
166154

167155
zassert_equal(psa_cipher_decrypt(key_id, alg, ciphertext, ciphertext_len, decrypted,
168156
sizeof(decrypted), &decrypted_len),
169-
PSA_SUCCESS, "Failed to perform one-shot decrypt operation");
157+
PSA_SUCCESS, "Failed to perform one-shot decrypt operation");
170158
zassert_equal(decrypted_len, sizeof(decrypted), "Decrypted length mismatch");
171159
zassert_mem_equal(decrypted, plaintext, sizeof(plaintext));
172160

173-
ciphertext[0] += 1;
174-
zassert_equal(psa_cipher_decrypt(key_id, alg, ciphertext, ciphertext_len, decrypted,
175-
sizeof(decrypted), &decrypted_len),
176-
PSA_SUCCESS,
177-
"Failed to perform one-shot decrypt operation with modified ciphertext");
178-
179-
zassert_equal(decrypted_len, sizeof(decrypted), "Decrypted length mismatch");
180-
zassert(memcmp(decrypted, plaintext, sizeof(plaintext)) != 0,
181-
"Decrypted modified data identical to original plaintext");
182-
183161
psa_destroy_key(key_id);
184162
}
185163

@@ -195,19 +173,19 @@ ZTEST(psa_crypto_test, test_cipher_chacha20_single)
195173
psa_set_key_algorithm(&attributes, alg);
196174

197175
zassert_equal(psa_import_key(&attributes, key_256, sizeof(key_256), &key_id), PSA_SUCCESS,
198-
"Failed to import key");
176+
"Failed to import key");
199177

200178
zassert_equal(psa_cipher_encrypt(key_id, alg, plaintext, sizeof(plaintext),
201179
ciphertext_buffer_256, sizeof(ciphertext_buffer_256),
202180
&out_len),
203-
PSA_SUCCESS, "Failed to encrypt");
181+
PSA_SUCCESS, "Failed to encrypt");
204182

205183
zassert(memcmp(ciphertext_buffer_256, plaintext, sizeof(plaintext)) != 0,
206184
"Ciphertext identical to plaintext");
207185

208186
zassert_equal(psa_cipher_decrypt(key_id, alg, ciphertext_buffer_256, out_len, decrypted,
209187
sizeof(decrypted), &out_len),
210-
PSA_SUCCESS, "Failed to decrypt");
188+
PSA_SUCCESS, "Failed to decrypt");
211189

212190
zassert_mem_equal(decrypted, plaintext, sizeof(plaintext));
213191
}

0 commit comments

Comments
 (0)