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-
1410const 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 };
1614const uint8_t aes_nonce_buf [] = {0xf9 , 0x75 , 0x80 , 0x9d , 0xdb , 0x51 ,
1715 0x72 , 0x38 , 0x27 , 0x45 , 0x63 , 0x4f };
1816const 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 };
2527const uint8_t chachapoly_nonce_buf [] = {0x07 , 0x00 , 0x00 , 0x00 , 0x40 , 0x41 ,
2628 0x42 , 0x43 , 0x44 , 0x45 , 0x46 , 0x47 };
2729const uint8_t chachapoly_ad_buf [] = {0x50 , 0x51 , 0x52 , 0x53 , 0xc0 , 0xc1 ,
@@ -49,7 +51,6 @@ const uint8_t chachapoly_expect_cipher_tag_buf[] = {
4951
5052ZTEST (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
10098ZTEST (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
150146ZTEST (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}
0 commit comments