@@ -26,7 +26,7 @@ psa_status_t suit_aes_key_unwrap_manual(psa_key_id_t kek_key_id, const uint8_t *
2626 * nrfkms export_derived -k TEST_AES_KEY -c test --format native
2727 * hexdump -e '16/1 "0x%02x, " "\n"' kms_output/derived_key_native_test_from_TEST_AES_KEY.bin
2828 */
29- static const uint8_t test_kek_key [] = {
29+ static const uint8_t test_key_data [] = {
3030 0xf8 , 0xfa , 0x8e , 0x7b , 0xed , 0x32 , 0xd0 , 0xc7 , 0x15 , 0x1f , 0xd9 , 0xab , 0x0d ,
3131 0x8d , 0xed , 0x95 , 0x26 , 0xa8 , 0x6a , 0x15 , 0x34 , 0x16 , 0x01 , 0xcf , 0x9c , 0x6b ,
3232 0xba , 0x00 , 0x6a , 0xab , 0xaa , 0x9a ,
@@ -59,7 +59,7 @@ static const uint8_t aad[] = {
5959 * Ciphertext and NONCE (IV) taken from the encrypted_asset-... file, which is in format
6060 * |nonce (12 bytes)|ciphertext|tag (16 bytes)|
6161 *
62- */
62+ */
6363static const uint8_t wrapped_cek [] = {
6464 0x7d , 0xd6 , 0xf4 , 0xd3 , 0x52 , 0x44 , 0x5a , 0x3a , 0x67 , 0xb8 , 0xcc ,
6565 0x74 , 0x5b , 0x4b , 0x6f , 0x70 , 0x62 , 0xc3 , 0xf2 , 0x7b , 0x6b , 0x14 ,
@@ -84,35 +84,73 @@ static const uint8_t iv_aes_kw[] = {
8484 0x61 , 0xb4 , 0x70 , 0x53 , 0xa5 , 0xe2 , 0x05 , 0x68 , 0xfe , 0x77 , 0x12 , 0x89 ,
8585};
8686
87+ /**
88+ * Encryption without wrapping CEK achieved by running:
89+ *
90+ * echo "This is a sample plaintext for testing the decryption filter" > plaintext.txt
91+ * nrfkms encrypt -k TEST_AES_KEY -c test -f plaintext.txt --aad "sample aad" --format native
92+ *
93+ * Ciphertext and NONCE (IV) taken from the encrypted_data_using_TEST_AES_KEY-test.bin file,
94+ * which is in format |nonce (12 bytes)|tag (16 bytes)|ciphertext|
95+ */
96+
97+ static const uint8_t ciphertext_direct [] = {
98+ /* tag (16 bytes) */
99+ 0x4d , 0x21 , 0x30 , 0xb7 , 0xce , 0x8a , 0xd6 , 0x00 , 0xe4 , 0x04 , 0xbb , 0x32 ,
100+ 0x72 , 0x7a , 0xbb , 0x7c ,
101+ /* ciphertext */
102+ 0xf0 , 0x72 , 0xdb , 0x63 , 0x03 , 0xdd , 0x24 , 0x69 ,
103+ 0xd4 , 0xbf , 0xd7 , 0xa0 , 0xec , 0xfa , 0x66 , 0x58 , 0x95 , 0x2b , 0xc1 , 0xc2 ,
104+ 0x9d , 0x82 , 0x02 , 0x1a , 0xd7 , 0x5b , 0xc0 , 0x01 , 0xce , 0x0b , 0x79 , 0x53 ,
105+ 0xe7 , 0xdb , 0x0d , 0x35 , 0xab , 0xef , 0x81 , 0xc8 , 0x68 , 0xc5 , 0xa7 , 0x22 ,
106+ 0x90 , 0xea , 0xd0 , 0x7f , 0x36 , 0xed , 0x14 , 0xbe , 0x30 , 0xf2 , 0x81 , 0x56 ,
107+ 0x7e , 0x2e , 0x5f , 0xd8 , 0x7c ,
108+ };
109+
110+
111+ static const uint8_t iv_direct [] = {
112+ 0x60 , 0x90 , 0x6d , 0xb2 , 0xfe , 0xc3 , 0xc8 , 0x5a , 0xf0 , 0x28 , 0xb1 , 0xb6 ,
113+ };
114+
87115static uint8_t output_buffer [128 ] = {0 };
88116
89- static void * test_suite_setup (void )
117+ static void init_encryption_key (const uint8_t * data , size_t size , psa_key_id_t * key_id ,
118+ psa_key_id_t alg , uint8_t * cbor_key_id )
90119{
91- static struct suit_decrypt_filter_tests_fixture fixture = {0 };
92120 psa_status_t status ;
93121
94122 /* Configure the key attributes */
95123 psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT ;
96124
97125 psa_set_key_usage_flags (& key_attributes , PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
98126 psa_set_key_lifetime (& key_attributes , PSA_KEY_LIFETIME_VOLATILE );
99- psa_set_key_algorithm (& key_attributes , PSA_ALG_ECB_NO_PADDING );
127+ psa_set_key_algorithm (& key_attributes , alg );
100128 psa_set_key_type (& key_attributes , PSA_KEY_TYPE_AES );
101129 psa_set_key_bits (& key_attributes , 256 );
102130
103- status = psa_crypto_init ();
104- zassert_equal (status , PSA_SUCCESS , "Failed to init psa crypto" );
131+ status = psa_import_key (& key_attributes , data , size , key_id );
105132
106- status = psa_import_key (& key_attributes , test_kek_key , sizeof (test_kek_key ),
107- & fixture .key_id );
108-
109- zassert_equal (status , PSA_SUCCESS , "Failed to import KEK" );
133+ zassert_equal (status , PSA_SUCCESS , "Failed to import key" );
110134
111135 /* Encode KEK key ID as CBOR unsigned int */
112- kek_key_id_cbor [1 ] = ((fixture .key_id >> 24 ) & 0xFF );
113- kek_key_id_cbor [2 ] = ((fixture .key_id >> 16 ) & 0xFF );
114- kek_key_id_cbor [3 ] = ((fixture .key_id >> 8 ) & 0xFF );
115- kek_key_id_cbor [4 ] = ((fixture .key_id >> 0 ) & 0xFF );
136+ cbor_key_id [1 ] = ((* key_id >> 24 ) & 0xFF );
137+ cbor_key_id [2 ] = ((* key_id >> 16 ) & 0xFF );
138+ cbor_key_id [3 ] = ((* key_id >> 8 ) & 0xFF );
139+ cbor_key_id [4 ] = ((* key_id >> 0 ) & 0xFF );
140+
141+ }
142+
143+ static void * test_suite_setup (void )
144+ {
145+ static struct suit_decrypt_filter_tests_fixture fixture = {0 };
146+ psa_status_t status ;
147+
148+ status = psa_crypto_init ();
149+ zassert_equal (status , PSA_SUCCESS , "Failed to init psa crypto" );
150+
151+ /* Init the KEK key */
152+ init_encryption_key (test_key_data , sizeof (test_key_data ), & fixture .key_id ,
153+ PSA_ALG_ECB_NO_PADDING , kek_key_id_cbor );
116154
117155 return & fixture ;
118156}
@@ -127,14 +165,15 @@ static void test_suite_teardown(void *f)
127165 }
128166}
129167
130- static void test_before (void * f )
168+ static void test_before (void * f )
131169{
132170 (void ) f ;
133171 memset (output_buffer , 0 , sizeof (output_buffer ));
134172}
135173
136174
137- ZTEST_SUITE (suit_decrypt_filter_tests , NULL , test_suite_setup , test_before , NULL , test_suite_teardown );
175+ ZTEST_SUITE (suit_decrypt_filter_tests , NULL , test_suite_setup , test_before , NULL ,
176+ test_suite_teardown );
138177
139178ZTEST_F (suit_decrypt_filter_tests , test_aes_unwrap_smoke )
140179{
@@ -147,7 +186,7 @@ ZTEST_F(suit_decrypt_filter_tests, test_aes_unwrap_smoke)
147186 zassert_equal (status , PSA_SUCCESS , "Failed to unwrap CEK" );
148187}
149188
150- ZTEST_F (suit_decrypt_filter_tests , test_filter_smoke )
189+ ZTEST_F (suit_decrypt_filter_tests , test_filter_smoke_aes_kw )
151190{
152191 struct stream_sink dec_sink ;
153192 struct stream_sink ram_sink ;
@@ -189,5 +228,60 @@ ZTEST_F(suit_decrypt_filter_tests, test_filter_smoke)
189228
190229 zassert_equal (err , SUIT_PLAT_SUCCESS , "Failed to release decrypt filter" );
191230
192- zassert_equal (memcmp (output_buffer , plaintext , strlen (plaintext )), 0 , "Decrypted plaintext does not match" );
231+ zassert_equal (memcmp (output_buffer , plaintext , strlen (plaintext )), 0 ,
232+ "Decrypted plaintext does not match" );
233+ }
234+
235+ ZTEST_F (suit_decrypt_filter_tests , test_filter_smoke_direct )
236+ {
237+ struct stream_sink dec_sink ;
238+ struct stream_sink ram_sink ;
239+ uint8_t cek_key_id_cbor [] = {
240+ 0x1A , 0x00 , 0x00 , 0x00 , 0x00 ,
241+ };
242+
243+ psa_key_id_t cek_key_id ;
244+
245+ init_encryption_key (test_key_data , sizeof (test_key_data ), & cek_key_id ,
246+ PSA_ALG_GCM , cek_key_id_cbor );
247+
248+ struct suit_encryption_info enc_info = {
249+ .enc_alg_id = suit_cose_aes256_gcm ,
250+ .IV = {
251+ .value = iv_direct ,
252+ .len = sizeof (iv_direct ),
253+ },
254+ .aad = {
255+ .value = aad ,
256+ .len = strlen (aad ),
257+ },
258+ .kw_alg_id = suit_cose_direct ,
259+ .kw_key .aes = {.key_id = {.value = cek_key_id_cbor ,
260+ .len = sizeof (cek_key_id_cbor )},}
261+ };
262+
263+ suit_plat_err_t err = suit_ram_sink_get (& ram_sink , output_buffer , sizeof (output_buffer ));
264+
265+ zassert_equal (err , SUIT_PLAT_SUCCESS , "Unable to create RAM sink" );
266+
267+ err = suit_decrypt_filter_get (& dec_sink , & enc_info , & ram_sink );
268+
269+ zassert_equal (err , SUIT_PLAT_SUCCESS , "Failed to create decrypt filter" );
270+
271+ err = suit_memptr_streamer_stream (ciphertext_direct , sizeof (ciphertext_direct ), & dec_sink );
272+
273+ zassert_equal (err , SUIT_PLAT_SUCCESS , "Failed to decrypt ciphertext" );
274+
275+ err = dec_sink .flush (dec_sink .ctx );
276+
277+ zassert_equal (err , SUIT_PLAT_SUCCESS , "Failed to flush decrypt filter" );
278+
279+ err = dec_sink .release (dec_sink .ctx );
280+
281+ zassert_equal (err , SUIT_PLAT_SUCCESS , "Failed to release decrypt filter" );
282+
283+ zassert_equal (memcmp (output_buffer , plaintext , strlen (plaintext )), 0 ,
284+ "Decrypted plaintext does not match" );
285+
286+ psa_destroy_key (cek_key_id );
193287}
0 commit comments