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+
1014#include "test_vectors.h"
1115
1216uint8_t key_256 [32 ] = {
@@ -40,24 +44,29 @@ ZTEST(psa_crypto_test, test_cipher_aes_cbc_256_multipart)
4044 psa_set_key_algorithm (& attributes , alg );
4145 psa_set_key_type (& attributes , PSA_KEY_TYPE_AES );
4246 psa_set_key_bits (& attributes , 256 );
43- if (IS_ENABLED (TEST_WRAPPED_KEYS )) {
47+
48+ #if defined(CONFIG_TEST_WRAPPED_KEYS ) && CONFIG_TEST_WRAPPED_KEYS
49+ printf ("Test Wrapper keys enabled\n" );
4450 psa_set_key_lifetime (& attributes , PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION (
45- PSA_KEY_PERSISTENCE_VOLATILE , 1 ));
51+ PSA_KEY_PERSISTENCE_VOLATILE , 0 ));
4652 zassert_equal (psa_generate_key (& attributes , & key_id ), PSA_SUCCESS ,
4753 "Failed to generate key" );
48- } else {
54+ # else
4955 zassert_equal (psa_import_key (& attributes , key_256 , sizeof (key_256 ), & key_id ),
5056 PSA_SUCCESS , "Failed to import key" );
51- }
57+ #endif
5258 psa_reset_key_attributes (& attributes );
5359
5460 zassert_equal (psa_cipher_encrypt_setup (& operation , key_id , alg ), PSA_SUCCESS ,
5561 "Failed to begin encrypt operation" );
62+
5663 zassert_equal (psa_cipher_generate_iv (& operation , iv , sizeof (iv ), & iv_len ), PSA_SUCCESS ,
5764 "Failed to generate IV" );
65+
5866 zassert_equal (psa_cipher_update (& operation , plaintext , sizeof (plaintext ), ciphertext ,
5967 sizeof (ciphertext ), & ciphertext_len ),
6068 PSA_SUCCESS , "Failed to update encrypt operation" );
69+
6170 zassert_equal (psa_cipher_finish (& operation , ciphertext + ciphertext_len ,
6271 sizeof (ciphertext ) - ciphertext_len , & ciphertext_len ),
6372 PSA_SUCCESS , "Failed to finish encrypt operation" );
@@ -69,12 +78,16 @@ ZTEST(psa_crypto_test, test_cipher_aes_cbc_256_multipart)
6978
7079 zassert_equal (psa_cipher_decrypt_setup (& operation , key_id , alg ), PSA_SUCCESS ,
7180 "Failed to begin decrypt operation" );
81+
7282 zassert_equal (psa_cipher_set_iv (& operation , iv , sizeof (iv )), PSA_SUCCESS ,
7383 "Failed to set IV" );
84+
7485 zassert_equal (psa_cipher_update (& operation , ciphertext , sizeof (ciphertext ), decrypted ,
7586 sizeof (decrypted ), & decrypted_len ),
7687 PSA_SUCCESS , "Failed to update decrypt operation" );
88+
7789 zassert_equal (decrypted_len , sizeof (decrypted ), "Decrypted length mismatch" );
90+
7891 zassert_equal (psa_cipher_finish (& operation , decrypted + decrypted_len ,
7992 sizeof (decrypted ) - decrypted_len , & decrypted_len ),
8093 PSA_SUCCESS , "Failed to finish decrypt operation" );
@@ -96,15 +109,17 @@ ZTEST(psa_crypto_test, test_cipher_aes_cbc_256_single)
96109 psa_set_key_algorithm (& attributes , alg );
97110 psa_set_key_type (& attributes , PSA_KEY_TYPE_AES );
98111 psa_set_key_bits (& attributes , 256 );
99- if (IS_ENABLED (TEST_WRAPPED_KEYS )) {
112+
113+ #if defined(CONFIG_TEST_WRAPPED_KEYS ) && CONFIG_TEST_WRAPPED_KEYS
114+ printf ("Test Wrapper keys enabled\n" );
100115 psa_set_key_lifetime (& attributes , PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION (
101- PSA_KEY_PERSISTENCE_VOLATILE , 1 ));
116+ PSA_KEY_PERSISTENCE_VOLATILE , PSA_KEY_VOLATILE_PERSISTENT_WRAP_IMPORT ));
102117 zassert_equal (psa_generate_key (& attributes , & key_id ), PSA_SUCCESS ,
103118 "Failed to generate key" );
104- } else {
119+ # else
105120 zassert_equal (psa_import_key (& attributes , key_256 , sizeof (key_256 ), & key_id ),
106121 PSA_SUCCESS , "Failed to import key" );
107- }
122+ #endif
108123 psa_reset_key_attributes (& attributes );
109124
110125 zassert_equal (psa_cipher_encrypt (key_id , alg , plaintext , sizeof (plaintext ),
@@ -120,17 +135,8 @@ ZTEST(psa_crypto_test, test_cipher_aes_cbc_256_single)
120135 PSA_SUCCESS , "Failed to perform one-shot decrypt operation" );
121136
122137 zassert_equal (decrypted_len , sizeof (decrypted ), "Decrypted length mismatch" );
123- 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" );
130138
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" );
139+ zassert_mem_equal (decrypted , plaintext , sizeof (plaintext ));
134140
135141 psa_destroy_key (key_id );
136142}
@@ -145,16 +151,18 @@ ZTEST(psa_crypto_test, test_cipher_aes_ecb_128_single)
145151 psa_set_key_algorithm (& attributes , alg );
146152 psa_set_key_type (& attributes , PSA_KEY_TYPE_AES );
147153 psa_set_key_bits (& attributes , 128 );
148- if (IS_ENABLED (TEST_WRAPPED_KEYS )) {
149- psa_set_key_lifetime (& attributes , PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION (
150- PSA_KEY_PERSISTENCE_VOLATILE , 1 ));
151154
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 ));
152159 zassert_equal (psa_generate_key (& attributes , & key_id ), PSA_SUCCESS ,
153160 "Failed to generate key" );
154- } else {
161+ # else
155162 zassert_equal (psa_import_key (& attributes , key_128 , sizeof (key_128 ), & key_id ),
156163 PSA_SUCCESS , "Failed to import key" );
157- }
164+ #endif
165+
158166 psa_reset_key_attributes (& attributes );
159167
160168 zassert_equal (psa_cipher_encrypt (key_id , alg , plaintext , sizeof (plaintext ), ciphertext ,
@@ -167,18 +175,10 @@ ZTEST(psa_crypto_test, test_cipher_aes_ecb_128_single)
167175 zassert_equal (psa_cipher_decrypt (key_id , alg , ciphertext , ciphertext_len , decrypted ,
168176 sizeof (decrypted ), & decrypted_len ),
169177 PSA_SUCCESS , "Failed to perform one-shot decrypt operation" );
170- zassert_equal (decrypted_len , sizeof (decrypted ), "Decrypted length mismatch" );
171- zassert_mem_equal (decrypted , plaintext , sizeof (plaintext ));
172-
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" );
178178
179179 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" );
180+
181+ zassert_mem_equal ( decrypted , plaintext , sizeof ( plaintext ) );
182182
183183 psa_destroy_key (key_id );
184184}
@@ -194,6 +194,12 @@ ZTEST(psa_crypto_test, test_cipher_chacha20_single)
194194 psa_set_key_usage_flags (& attributes , PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
195195 psa_set_key_algorithm (& attributes , alg );
196196
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+
197203 zassert_equal (psa_import_key (& attributes , key_256 , sizeof (key_256 ), & key_id ), PSA_SUCCESS ,
198204 "Failed to import key" );
199205
@@ -209,5 +215,8 @@ ZTEST(psa_crypto_test, test_cipher_chacha20_single)
209215 sizeof (decrypted ), & out_len ),
210216 PSA_SUCCESS , "Failed to decrypt" );
211217
218+ zassert (memcmp (ciphertext_buffer_256 , plaintext , sizeof (plaintext )) != 0 ,
219+ "Ciphertext identical to plaintext" );
220+
212221 zassert_mem_equal (decrypted , plaintext , sizeof (plaintext ));
213222}
0 commit comments