Skip to content

Commit 1192c41

Browse files
committed
tests: crypto: Add wrapped key support in PSA crypto tests
1. Added wrapped key buffers for AES, ChaChaPoly, and ECDSA keys in AEAD and Sign test cases. 2. Updated AEAD, Cipher, Key Agreement, and Sign tests to: - Import wrapped keys when CONFIG_TEST_WRAPPED_KEYS is enabled. - Configure key lifetime using PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION. - Print a debug message when wrapper key mode is active. - Use normal key import when wrapper keys are disabled. 3. Removed conditional inclusion of sl_si91x_psa_wrap.h and cleaned up redundant preprocessor blocks for better readability. Signed-off-by: Aasim Shaik <[email protected]>
1 parent 6eac604 commit 1192c41

File tree

4 files changed

+41
-96
lines changed

4 files changed

+41
-96
lines changed

tests/crypto/psa_crypto/src/aead.c

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
#include <zephyr/ztest.h>
88
#include <psa/crypto.h>
99

10-
#if defined CONFIG_TEST_WRAPPED_KEYS
11-
#include "sl_si91x_psa_wrap.h"
12-
#endif
13-
1410
const uint8_t aes_key_buf[] = {0xea, 0x4f, 0x6f, 0x3c, 0x2f, 0xed, 0x2b, 0x9d,
1511
0xd9, 0x70, 0x8c, 0x2e, 0x72, 0x1a, 0xe0, 0x0f};
12+
const uint8_t wrapped_aes_key_buf[] = {0x52, 0xb3, 0x41, 0x71, 0xcf, 0xd6,
13+
0xa2, 0x00, 0xe6, 0xe4, 0x75, 0x57, 0xa6, 0xe5, 0x0a, 0x0f};
1614
const uint8_t aes_nonce_buf[] = {0xf9, 0x75, 0x80, 0x9d, 0xdb, 0x51,
1715
0x72, 0x38, 0x27, 0x45, 0x63, 0x4f};
1816
const uint8_t aes_ad_buf[] = {0x5c, 0x65, 0xd4, 0xf2, 0x61, 0xd2, 0xc5, 0x4f, 0xfe, 0x6a};
@@ -22,6 +20,10 @@ const uint8_t chachapoly_key_buf[] = {0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86,
2220
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
2321
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
2422
0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f};
23+
const uint8_t wrapped_chachapoly_key_buf[] = {0x19, 0x67, 0x03, 0x6a, 0x20, 0xe6, 0x32,
24+
0xed, 0x21, 0x2a, 0x4a, 0xdc, 0x2c, 0x5f, 0x19,
25+
0x22, 0x77, 0x56, 0x61, 0x92, 0x2c, 0xf3, 0xdc,
26+
0xe8, 0x8e, 0x81, 0x45, 0x4d, 0xd6, 0xc7, 0x86, 0x0b};
2527
const uint8_t chachapoly_nonce_buf[] = {0x07, 0x00, 0x00, 0x00, 0x40, 0x41,
2628
0x42, 0x43, 0x44, 0x45, 0x46, 0x47};
2729
const uint8_t chachapoly_ad_buf[] = {0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1,
@@ -49,7 +51,6 @@ const uint8_t chachapoly_expect_cipher_tag_buf[] = {
4951

5052
ZTEST(psa_crypto_test, test_aead_aes_ccm)
5153
{
52-
5354
const uint8_t expect_cipher_tag_buf[] = {
5455
0xe2, 0x2f, 0x37, 0x3b, 0xeb, 0xf6, 0x4a, 0x3e, 0x9b, 0x87, 0x75, 0x2b, 0xf9,
5556
0xdb, 0x34, 0xdc, 0x4d, 0x43, 0x3f, 0x00, 0xf5, 0x5c, 0x3f, 0x53, 0x0c, 0x89,
@@ -67,21 +68,21 @@ ZTEST(psa_crypto_test, test_aead_aes_ccm)
6768
psa_set_key_algorithm(&attributes, alg);
6869

6970
#if defined(CONFIG_TEST_WRAPPED_KEYS) && CONFIG_TEST_WRAPPED_KEYS
70-
printf("Test Wrapper keys enabled\n");
71-
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
72-
PSA_KEY_PERSISTENCE_VOLATILE, PSA_KEY_VOLATILE_PERSISTENT_WRAP_IMPORT));
73-
#endif
71+
printf("Test Wrapper keys enabled\n");
72+
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
73+
PSA_KEY_PERSISTENCE_VOLATILE, 1));
74+
zassert_equal(psa_import_key(&attributes, wrapped_aes_key_buf, sizeof(wrapped_aes_key_buf), &key_id),
75+
PSA_SUCCESS, "Failed to import key");
76+
#else
7477
zassert_equal(psa_import_key(&attributes, aes_key_buf, sizeof(aes_key_buf), &key_id),
7578
PSA_SUCCESS, "Failed to import key");
76-
79+
#endif
7780
zassert_equal(psa_aead_encrypt(key_id, alg, aes_nonce_buf, sizeof(aes_nonce_buf),
7881
aes_ad_buf, sizeof(aes_ad_buf), aes_plaintext,
7982
sizeof(aes_plaintext), cipher_tag_buf,
8083
sizeof(cipher_tag_buf), &out_len),
8184
PSA_SUCCESS, "Failed to perform encrypt");
82-
8385
zassert_equal(out_len, sizeof(expect_cipher_tag_buf));
84-
8586
zassert_mem_equal(cipher_tag_buf, expect_cipher_tag_buf, sizeof(expect_cipher_tag_buf));
8687

8788
zassert_equal(psa_aead_decrypt(key_id, alg, aes_nonce_buf, sizeof(aes_nonce_buf),
@@ -90,16 +91,12 @@ ZTEST(psa_crypto_test, test_aead_aes_ccm)
9091
PSA_SUCCESS, "Failed to decrypt");
9192

9293
zassert_equal(out_len, sizeof(aes_plaintext));
93-
9494
zassert_mem_equal(decrypted, aes_plaintext, sizeof(aes_plaintext));
95-
9695
zassert_equal(psa_destroy_key(key_id), PSA_SUCCESS, "Failed to destroy key");
97-
9896
}
9997

10098
ZTEST(psa_crypto_test, test_aead_aes_gcm)
10199
{
102-
103100
const uint8_t expect_cipher_tag_buf[] = {
104101
0x0f, 0x51, 0xf7, 0xa8, 0x3c, 0x5b, 0x5a, 0xa7, 0x96, 0xb9, 0x70, 0x25, 0x9c,
105102
0xdd, 0xfe, 0x8f, 0x9a, 0x15, 0xa5, 0xc5, 0xeb, 0x48, 0x5a, 0xf5, 0x78, 0xfb,
@@ -119,17 +116,19 @@ ZTEST(psa_crypto_test, test_aead_aes_gcm)
119116
#if defined(CONFIG_TEST_WRAPPED_KEYS) && CONFIG_TEST_WRAPPED_KEYS
120117
printf("Test Wrapper keys enabled\n");
121118
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
122-
PSA_KEY_PERSISTENCE_VOLATILE, PSA_KEY_VOLATILE_PERSISTENT_WRAP_IMPORT));
123-
#endif
119+
PSA_KEY_PERSISTENCE_VOLATILE, 1));
120+
zassert_equal(psa_import_key(&attributes, wrapped_aes_key_buf, sizeof(wrapped_aes_key_buf), &key_id),
121+
PSA_SUCCESS, "Failed to import key");
122+
#else
124123
zassert_equal(psa_import_key(&attributes, aes_key_buf, sizeof(aes_key_buf), &key_id),
125124
PSA_SUCCESS, "Failed to import key");
126-
125+
#endif
127126
zassert_equal(psa_aead_encrypt(key_id, alg, aes_nonce_buf, sizeof(aes_nonce_buf),
128127
aes_ad_buf, sizeof(aes_ad_buf), aes_plaintext,
129128
sizeof(aes_plaintext), cipher_tag_buf,
130129
sizeof(cipher_tag_buf), &out_len),
131130
PSA_SUCCESS, "Failed to perform encrypt");
132-
131+
133132
zassert_equal(out_len, sizeof(expect_cipher_tag_buf));
134133

135134
zassert_mem_equal(cipher_tag_buf, expect_cipher_tag_buf, sizeof(expect_cipher_tag_buf));
@@ -140,16 +139,12 @@ ZTEST(psa_crypto_test, test_aead_aes_gcm)
140139
PSA_SUCCESS, "Failed to decrypt");
141140

142141
zassert_equal(out_len, sizeof(aes_plaintext));
143-
144142
zassert_mem_equal(decrypted, aes_plaintext, sizeof(aes_plaintext));
145-
146143
zassert_equal(psa_destroy_key(key_id), PSA_SUCCESS, "Failed to destroy key");
147-
148144
}
149145

150146
ZTEST(psa_crypto_test, test_aead_chacha20_poly1305)
151147
{
152-
153148
uint8_t cipher_tag_buf[130]; /* Ciphertext + Tag */
154149
uint8_t decrypted[sizeof(chachapoly_plaintext)] = {0};
155150
size_t out_len;
@@ -164,34 +159,30 @@ ZTEST(psa_crypto_test, test_aead_chacha20_poly1305)
164159

165160
#if defined(CONFIG_TEST_WRAPPED_KEYS) && CONFIG_TEST_WRAPPED_KEYS
166161
printf("Test Wrapper keys enabled\n");
167-
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
168-
PSA_KEY_PERSISTENCE_VOLATILE, PSA_KEY_VOLATILE_PERSISTENT_WRAP_IMPORT));
169-
#endif
162+
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
163+
PSA_KEY_PERSISTENCE_VOLATILE, 1));
164+
zassert_equal(psa_import_key(&attributes, wrapped_chachapoly_key_buf,
165+
sizeof(wrapped_chachapoly_key_buf), &key_id),
166+
PSA_SUCCESS, "Failed to import key");
167+
#else
170168
zassert_equal(psa_import_key(&attributes, chachapoly_key_buf, sizeof(chachapoly_key_buf), &key_id),
171169
PSA_SUCCESS, "Failed to import key");
172-
170+
#endif
173171
zassert_equal(psa_aead_encrypt(key_id, alg, chachapoly_nonce_buf,
174172
sizeof(chachapoly_nonce_buf), chachapoly_ad_buf,
175173
sizeof(chachapoly_ad_buf), chachapoly_plaintext,
176174
sizeof(chachapoly_plaintext), cipher_tag_buf,
177175
sizeof(cipher_tag_buf), &out_len),
178176
PSA_SUCCESS, "Failed to perform encrypt");
179-
180177
zassert_equal(out_len, sizeof(chachapoly_expect_cipher_tag_buf));
181-
182178
zassert_mem_equal(cipher_tag_buf, chachapoly_expect_cipher_tag_buf,
183179
sizeof(chachapoly_expect_cipher_tag_buf));
184-
185180
zassert_equal(psa_aead_decrypt(key_id, alg, chachapoly_nonce_buf,
186181
sizeof(chachapoly_nonce_buf), chachapoly_ad_buf,
187182
sizeof(chachapoly_ad_buf), cipher_tag_buf, out_len,
188183
decrypted, sizeof(decrypted), &out_len),
189184
PSA_SUCCESS, "Failed to decrypt");
190-
191185
zassert_equal(out_len, sizeof(chachapoly_plaintext));
192-
193186
zassert_mem_equal(decrypted, chachapoly_plaintext, sizeof(chachapoly_plaintext));
194-
195187
zassert_equal(psa_destroy_key(key_id), PSA_SUCCESS, "Failed to destroy key");
196-
197188
}

tests/crypto/psa_crypto/src/cipher.c

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
#include <zephyr/ztest.h>
88
#include <psa/crypto.h>
99

10-
#if defined CONFIG_TEST_WRAPPED_KEYS
11-
#include "sl_si91x_psa_wrap.h"
12-
#endif
13-
1410
#include "test_vectors.h"
1511

1612
uint8_t key_256[32] = {
@@ -45,16 +41,8 @@ ZTEST(psa_crypto_test, test_cipher_aes_cbc_256_multipart)
4541
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
4642
psa_set_key_bits(&attributes, 256);
4743

48-
#if defined(CONFIG_TEST_WRAPPED_KEYS) && CONFIG_TEST_WRAPPED_KEYS
49-
printf("Test Wrapper keys enabled\n");
50-
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
51-
PSA_KEY_PERSISTENCE_VOLATILE, 0));
52-
zassert_equal(psa_generate_key(&attributes, &key_id), PSA_SUCCESS,
53-
"Failed to generate key");
54-
#else
55-
zassert_equal(psa_import_key(&attributes, key_256, sizeof(key_256), &key_id),
44+
zassert_equal(psa_import_key(&attributes, key_256, sizeof(key_256), &key_id),
5645
PSA_SUCCESS, "Failed to import key");
57-
#endif
5846
psa_reset_key_attributes(&attributes);
5947

6048
zassert_equal(psa_cipher_encrypt_setup(&operation, key_id, alg), PSA_SUCCESS,
@@ -110,16 +98,9 @@ ZTEST(psa_crypto_test, test_cipher_aes_cbc_256_single)
11098
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
11199
psa_set_key_bits(&attributes, 256);
112100

113-
#if defined(CONFIG_TEST_WRAPPED_KEYS) && CONFIG_TEST_WRAPPED_KEYS
114-
printf("Test Wrapper keys enabled\n");
115-
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
116-
PSA_KEY_PERSISTENCE_VOLATILE, PSA_KEY_VOLATILE_PERSISTENT_WRAP_IMPORT));
117-
zassert_equal(psa_generate_key(&attributes, &key_id), PSA_SUCCESS,
118-
"Failed to generate key");
119-
#else
120-
zassert_equal(psa_import_key(&attributes, key_256, sizeof(key_256), &key_id),
101+
zassert_equal(psa_import_key(&attributes, key_256, sizeof(key_256), &key_id),
121102
PSA_SUCCESS, "Failed to import key");
122-
#endif
103+
123104
psa_reset_key_attributes(&attributes);
124105

125106
zassert_equal(psa_cipher_encrypt(key_id, alg, plaintext, sizeof(plaintext),
@@ -152,16 +133,8 @@ ZTEST(psa_crypto_test, test_cipher_aes_ecb_128_single)
152133
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
153134
psa_set_key_bits(&attributes, 128);
154135

155-
#if defined(CONFIG_TEST_WRAPPED_KEYS) && CONFIG_TEST_WRAPPED_KEYS
156-
printf("Test Wrapper keys enabled\n");
157-
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
158-
PSA_KEY_PERSISTENCE_VOLATILE, PSA_KEY_VOLATILE_PERSISTENT_WRAP_IMPORT));
159-
zassert_equal(psa_generate_key(&attributes, &key_id), PSA_SUCCESS,
160-
"Failed to generate key");
161-
#else
162-
zassert_equal(psa_import_key(&attributes, key_128, sizeof(key_128), &key_id),
136+
zassert_equal(psa_import_key(&attributes, key_128, sizeof(key_128), &key_id),
163137
PSA_SUCCESS, "Failed to import key");
164-
#endif
165138

166139
psa_reset_key_attributes(&attributes);
167140

@@ -194,12 +167,6 @@ ZTEST(psa_crypto_test, test_cipher_chacha20_single)
194167
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
195168
psa_set_key_algorithm(&attributes, alg);
196169

197-
#if defined(CONFIG_TEST_WRAPPED_KEYS) && CONFIG_TEST_WRAPPED_KEYS
198-
printf("Test Wrapper keys enabled\n");
199-
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
200-
PSA_KEY_PERSISTENCE_VOLATILE, 0));
201-
#endif
202-
203170
zassert_equal(psa_import_key(&attributes, key_256, sizeof(key_256), &key_id), PSA_SUCCESS,
204171
"Failed to import key");
205172

tests/crypto/psa_crypto/src/key_agreement.c

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
#include <zephyr/ztest.h>
88
#include <psa/crypto.h>
99

10-
#if defined CONFIG_TEST_WRAPPED_KEYS
11-
#include "sl_si91x_psa_wrap.h"
12-
#endif
13-
1410
static const uint8_t client_private_key[] = {
1511
0xB0, 0x76, 0x51, 0xEA, 0x20, 0xF0, 0x28, 0xA8, 0x16, 0xEE, 0x01,
1612
0xB0, 0xD1, 0x06, 0x2A, 0x7C, 0x81, 0x58, 0xE8, 0x84, 0xE9, 0xBC,
@@ -49,11 +45,6 @@ ZTEST(psa_crypto_test, test_key_agreement_ecdh_25519)
4945
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
5046
psa_set_key_algorithm(&attributes, PSA_ALG_ECDH);
5147

52-
#if defined(CONFIG_TEST_WRAPPED_KEYS) && CONFIG_TEST_WRAPPED_KEYS
53-
printf("Test Wrapper keys enabled\n");
54-
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
55-
PSA_KEY_PERSISTENCE_VOLATILE, 0));
56-
#endif
5748
zassert_equal(psa_import_key(&attributes, client_private_key, sizeof(client_private_key),
5849
&key_id),
5950
PSA_SUCCESS, "Failed to import client key");
@@ -71,10 +62,6 @@ ZTEST(psa_crypto_test, test_key_agreement_ecdh_25519)
7162
psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_MONTGOMERY));
7263
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
7364
psa_set_key_algorithm(&attributes, PSA_ALG_ECDH);
74-
if (IS_ENABLED(TEST_WRAPPED_KEYS)) {
75-
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
76-
PSA_KEY_PERSISTENCE_VOLATILE, 0));
77-
}
7865
zassert_equal(psa_import_key(&attributes, server_private_key, sizeof(server_private_key),
7966
&key_id),
8067
PSA_SUCCESS, "Failed to import server key");

tests/crypto/psa_crypto/src/sign.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66

77
#include <zephyr/ztest.h>
88
#include <psa/crypto.h>
9-
10-
#if defined CONFIG_TEST_WRAPPED_KEYS
11-
#include "sl_si91x_psa_wrap.h"
12-
#endif
13-
149
#include "test_vectors.h"
1510

1611
uint8_t pubkey[65];
@@ -20,6 +15,10 @@ size_t signature_len;
2015
static const unsigned char private_key[] = { 0x95, 0xCD, 0x3A, 0x36, 0x25, 0xD6, 0xF6, 0x06, 0xBD, 0xC8, 0x64,
2116
0x77, 0x8D, 0x4A, 0xA6, 0x50, 0xC2, 0xD7, 0x9A, 0x05, 0x94, 0xDD,
2217
0x10, 0xCF, 0x4C, 0x47, 0x4B, 0x83, 0xD2, 0x87, 0x0D, 0x1A };
18+
static const unsigned char wrapped_private_key[] = { 0xd1, 0xd6, 0x56, 0x96, 0x9a, 0x78, 0x46, 0x36, 0xdd,
19+
0xa8, 0xbc, 0x0c, 0xe2, 0xe5, 0xbb, 0xee, 0x05, 0x33, 0x82,
20+
0x0f, 0x7d, 0xc7, 0x12, 0xf4, 0xd9, 0x34, 0x25, 0x00, 0x72,
21+
0x53, 0x95, 0x7d,};
2322
#define MESSAGE_SIZE (sizeof(plaintext) / 2)
2423

2524
ZTEST(psa_crypto_test, test_sign_ecdsa_secp256r1)
@@ -50,14 +49,15 @@ ZTEST(psa_crypto_test, test_sign_ecdsa_secp256r1)
5049
psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_ANY_HASH));
5150

5251
#if defined(CONFIG_TEST_WRAPPED_KEYS) && CONFIG_TEST_WRAPPED_KEYS
53-
printf("Test Wrapper keys enabled\n");
54-
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
55-
PSA_KEY_PERSISTENCE_VOLATILE, PSA_KEY_VOLATILE_PERSISTENT_WRAP_IMPORT));
56-
#endif
57-
52+
printf("Test Wrapper keys enabled\n");
53+
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
54+
PSA_KEY_PERSISTENCE_VOLATILE, 1));
55+
zassert_equal(psa_import_key(&attributes,wrapped_private_key, sizeof(wrapped_private_key),
56+
&key_id), PSA_SUCCESS,"Failed to import private key");
57+
#else
5858
zassert_equal(psa_import_key(&attributes,private_key, sizeof(private_key), &key_id), PSA_SUCCESS,
5959
"Failed to import private key");
60-
60+
#endif
6161
zassert_equal(psa_sign_message(key_id, PSA_ALG_ECDSA(PSA_ALG_SHA_256), plaintext,
6262
MESSAGE_SIZE, signature, sizeof(signature), &signature_len),
6363
PSA_SUCCESS, "Failed to hash-and-sign message");

0 commit comments

Comments
 (0)