Skip to content

Commit 00b1a0d

Browse files
ahasztagjukkar
authored andcommitted
tests: suit: Decrypt filter tests now using ram_sink
Earlier they used digest sink. Tests using ram_sink are a bit simpler to analyze. Signed-off-by: Artur Hadasz <[email protected]>
1 parent 4d3fc5c commit 00b1a0d

File tree

2 files changed

+71
-46
lines changed

2 files changed

+71
-46
lines changed

tests/subsys/suit/decrypt_filter/prj.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ CONFIG_SUIT_UTILS=y
1313

1414
CONFIG_SUIT_STREAM=y
1515
CONFIG_SUIT_STREAM_FILTER_DECRYPT=y
16-
CONFIG_SUIT_STREAM_SINK_DIGEST=y
16+
CONFIG_SUIT_STREAM_SINK_RAM=y
1717
CONFIG_SUIT_STREAM_SOURCE_MEMPTR=y
1818

1919
CONFIG_ZCBOR=y

tests/subsys/suit/decrypt_filter/src/main.c

Lines changed: 70 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <zephyr/ztest.h>
88
#include <psa/crypto.h>
99
#include <suit_decrypt_filter.h>
10-
#include <suit_digest_sink.h>
10+
#include <suit_ram_sink.h>
1111
#include <suit_memptr_streamer.h>
1212

1313
/* Forward declaration of the internal, temporary AES key-unwrap implementation. */
@@ -16,10 +16,20 @@ psa_status_t suit_aes_key_unwrap_manual(psa_key_id_t kek_key_id, const uint8_t *
1616
psa_algorithm_t cek_key_alg,
1717
mbedtls_svc_key_id_t *unwrapped_cek_key_id);
1818

19+
/**
20+
* The master key used by these tests can be imported into the local KMS backend by running:
21+
*
22+
* nrfkms import_keyvalue -k TEST_AES_KEY -t aes -v aHWJdIkl5hdXw4SS1nTdVYE/q7ycMOZm2mR6qx/KvKw=
23+
*
24+
* The KEK below is derived from context "test"
25+
* To acquire ut run:
26+
* nrfkms export_derived -k TEST_AES_KEY -c test --format native
27+
* hexdump -e '16/1 "0x%02x, " "\n"' kms_output/derived_key_native_test_from_TEST_AES_KEY.bin
28+
*/
1929
static const uint8_t test_kek_key[] = {
20-
0x7b, 0xf2, 0x67, 0xbe, 0x5c, 0x57, 0x35, 0x77, 0xb6, 0xe0, 0x6d,
21-
0xa3, 0x61, 0xc0, 0x88, 0x6b, 0x38, 0x91, 0x8a, 0x76, 0xf4, 0x72,
22-
0x02, 0xac, 0xf1, 0x38, 0x19, 0x5f, 0x48, 0xd3, 0x60, 0x59,
30+
0xf8, 0xfa, 0x8e, 0x7b, 0xed, 0x32, 0xd0, 0xc7, 0x15, 0x1f, 0xd9, 0xab, 0x0d,
31+
0x8d, 0xed, 0x95, 0x26, 0xa8, 0x6a, 0x15, 0x34, 0x16, 0x01, 0xcf, 0x9c, 0x6b,
32+
0xba, 0x00, 0x6a, 0xab, 0xaa, 0x9a,
2333
};
2434

2535
static uint8_t kek_key_id_cbor[] = {
@@ -30,34 +40,52 @@ struct suit_decrypt_filter_tests_fixture {
3040
psa_key_id_t key_id;
3141
};
3242

33-
const static uint8_t ciphertext[] = {
34-
0x27, 0x3c, 0xa8, 0x1f, 0x01, 0x6b, 0x76, 0xbe, 0x22, 0xab, 0x9f, 0x76,
35-
0xab, 0x33, 0x2a, 0x24, 0x08, 0x3b, 0x98, 0xd4, 0x8f, 0x26, 0xa3, 0xdc,
36-
0x4f, 0x35, 0xfd, 0x0b, 0x38, 0xb2, 0xd2, 0xd2, 0xc9, 0x4c, 0xde, 0xc9,
37-
0x9c, 0xca, 0x5f, 0x56, 0xda, 0x01, 0xe3, 0x66, 0x50, 0xaf, 0x9c,
43+
static const uint8_t plaintext[] = {
44+
"This is a sample plaintext for testing the decryption filter",
3845
};
3946

40-
static const uint8_t valid_digest[] = {
41-
0xfa, 0xf2, 0xf9, 0xb2, 0x44, 0xdc, 0xc7, 0xaa, 0x53, 0x1e, 0x06,
42-
0x49, 0x86, 0x6f, 0xfb, 0x3f, 0xf0, 0x8f, 0x86, 0xad, 0xf4, 0xda,
43-
0x5e, 0x2c, 0x15, 0xc9, 0xf5, 0xac, 0x06, 0x6b, 0x9f, 0xde,
47+
static const uint8_t aad[] = {
48+
"sample aad"
4449
};
4550

46-
static const psa_algorithm_t valid_algorithm = PSA_ALG_SHA_256;
51+
/**
52+
* Encryption and using wrapped CEK achieved by running:
53+
*
54+
* echo "This is a sample plaintext for testing the decryption filter" > plaintext.txt
55+
* nrfkms wrap -k TEST_AES_KEY -c test -f plaintext.txt --format native -t aes --aad "sample aad"
56+
*
57+
* Wrapped CEK stored in the resulting wrapped_aek-aes-... file
58+
*
59+
* Ciphertext and NONCE (IV) taken from the encrypted_asset-... file, which is in format
60+
* |nonce (12 bytes)|ciphertext|tag (16 bytes)|
61+
*
62+
*/
63+
static const uint8_t wrapped_cek[] = {
64+
0x7d, 0xd6, 0xf4, 0xd3, 0x52, 0x44, 0x5a, 0x3a, 0x67, 0xb8, 0xcc,
65+
0x74, 0x5b, 0x4b, 0x6f, 0x70, 0x62, 0xc3, 0xf2, 0x7b, 0x6b, 0x14,
66+
0xf1, 0x06, 0x57, 0xa3, 0x68, 0x32, 0x44, 0xc3, 0x85, 0x77, 0x86,
67+
0xe7, 0xda, 0x15, 0xbf, 0xf8, 0x9e, 0x63
68+
};
4769

48-
static const uint8_t suit_aad_aes256_gcm[] = {
49-
0x83, /* array (3 elements) */
50-
0x67, /* context: text (7 characters) */
51-
'E', 'n', 'c', 'r', 'y', 'p', 't', 0x43, /* protected: bstr encoded map (3 elements) */
52-
0xA1, /* map (1 element) */
53-
0x01, 0x03, /* alg_id: A256GCM */
54-
0x40 /* external_aad: h'' */
70+
static const uint8_t ciphertext_aes_kw[] = {
71+
/* tag (16 bytes) */
72+
0xdc, 0xe6, 0x95, 0xac, 0x0f, 0x61, 0x87, 0x17, 0x51, 0x48, 0xb4, 0xa1,
73+
0x8e, 0x09, 0x89, 0xb4,
74+
/* ciphertext */
75+
0x8b, 0xfb, 0xd9, 0xe4, 0xcf, 0xde, 0xf8, 0xcf, 0xe5, 0x69, 0x9d, 0x6d,
76+
0x92, 0x8a, 0x04, 0xf8, 0x26, 0x22, 0xd5, 0xd8, 0xe8, 0x77, 0x18, 0x5a,
77+
0x01, 0x13, 0xba, 0xd5, 0x23, 0x72, 0xae, 0x80, 0x44, 0xed, 0xea, 0xdf,
78+
0x74, 0x79, 0x8a, 0x83, 0x52, 0x72, 0x2f, 0x43, 0x06, 0xe9, 0xd4, 0xbb,
79+
0x54, 0x8a, 0x0d, 0xea, 0x7f, 0xe6, 0x48, 0xf0, 0xfd, 0x0e, 0xbb, 0xaa,
80+
0xa3,
5581
};
5682

57-
static const uint8_t iv_aes256_gcm[] = {
58-
0x61, 0x61, 0x75, 0x1C, 0x47, 0x79, 0x33, 0x2F, 0xFC, 0xBE, 0x0A, 0xA9,
83+
static const uint8_t iv_aes_kw[] = {
84+
0x61, 0xb4, 0x70, 0x53, 0xa5, 0xe2, 0x05, 0x68, 0xfe, 0x77, 0x12, 0x89,
5985
};
6086

87+
static uint8_t output_buffer[128] = {0};
88+
6189
static void *test_suite_setup(void)
6290
{
6391
static struct suit_decrypt_filter_tests_fixture fixture = {0};
@@ -99,15 +127,18 @@ static void test_suite_teardown(void *f)
99127
}
100128
}
101129

102-
ZTEST_SUITE(suit_decrypt_filter_tests, NULL, test_suite_setup, NULL, NULL, test_suite_teardown);
130+
static void test_before(void* f)
131+
{
132+
(void) f;
133+
memset(output_buffer, 0, sizeof(output_buffer));
134+
}
135+
136+
137+
ZTEST_SUITE(suit_decrypt_filter_tests, NULL, test_suite_setup, test_before, NULL, test_suite_teardown);
103138

104139
ZTEST_F(suit_decrypt_filter_tests, test_aes_unwrap_smoke)
105140
{
106141
mbedtls_svc_key_id_t unwrapped_cek_key_id;
107-
const uint8_t wrapped_cek[] = {0xb2, 0x43, 0x88, 0x9a, 0x6a, 0x4a, 0x91, 0xc4, 0xf0, 0xb0,
108-
0x9b, 0xe8, 0xc5, 0xbc, 0x54, 0x60, 0xb9, 0x38, 0x99, 0xa0,
109-
0x1a, 0xdd, 0xa7, 0xd3, 0x87, 0x9f, 0xc7, 0x0a, 0xd8, 0xbf,
110-
0x53, 0x28, 0xfa, 0x64, 0xea, 0x44, 0xaf, 0xb5, 0xbb, 0x92};
111142

112143
psa_status_t status =
113144
suit_aes_key_unwrap_manual(fixture->key_id, wrapped_cek, 256, PSA_KEY_TYPE_AES,
@@ -119,20 +150,16 @@ ZTEST_F(suit_decrypt_filter_tests, test_aes_unwrap_smoke)
119150
ZTEST_F(suit_decrypt_filter_tests, test_filter_smoke)
120151
{
121152
struct stream_sink dec_sink;
122-
struct stream_sink digest_sink;
123-
const uint8_t wrapped_cek[] = {0x50, 0x0A, 0xC9, 0x37, 0x2F, 0xA0, 0x34, 0x14, 0x8D, 0xB3,
124-
0xE6, 0x59, 0x50, 0xED, 0x37, 0xE4, 0x76, 0xBE, 0x30, 0x18,
125-
0x58, 0x81, 0xEA, 0xFA, 0xE5, 0x8A, 0xD1, 0x44, 0x1E, 0xD1,
126-
0xAB, 0x3C, 0x6E, 0xBD, 0x31, 0xDD, 0x33, 0x61, 0x13, 0x49};
153+
struct stream_sink ram_sink;
127154
struct suit_encryption_info enc_info = {
128155
.enc_alg_id = suit_cose_aes256_gcm,
129156
.IV = {
130-
.value = iv_aes256_gcm,
131-
.len = sizeof(iv_aes256_gcm),
157+
.value = iv_aes_kw,
158+
.len = sizeof(iv_aes_kw),
132159
},
133160
.aad = {
134-
.value = suit_aad_aes256_gcm,
135-
.len = sizeof(suit_aad_aes256_gcm),
161+
.value = aad,
162+
.len = strlen(aad),
136163
},
137164
.kw_alg_id = suit_cose_aes256_kw,
138165
.kw_key.aes = {.key_id = {.value = kek_key_id_cbor, .len = sizeof(kek_key_id_cbor)},
@@ -142,27 +169,25 @@ ZTEST_F(suit_decrypt_filter_tests, test_filter_smoke)
142169
}},
143170
};
144171

145-
suit_plat_err_t err = suit_digest_sink_get(&digest_sink, valid_algorithm, valid_digest);
172+
suit_plat_err_t err = suit_ram_sink_get(&ram_sink, output_buffer, sizeof(output_buffer));
146173

147-
zassert_equal(err, SUIT_PLAT_SUCCESS, "Unable to create digest sink");
174+
zassert_equal(err, SUIT_PLAT_SUCCESS, "Unable to create RAM sink");
148175

149-
err = suit_decrypt_filter_get(&dec_sink, &enc_info, &digest_sink);
176+
err = suit_decrypt_filter_get(&dec_sink, &enc_info, &ram_sink);
150177

151178
zassert_equal(err, SUIT_PLAT_SUCCESS, "Failed to create decrypt filter");
152179

153-
err = suit_memptr_streamer_stream(ciphertext, sizeof(ciphertext), &dec_sink);
180+
err = suit_memptr_streamer_stream(ciphertext_aes_kw, sizeof(ciphertext_aes_kw), &dec_sink);
154181

155182
zassert_equal(err, SUIT_PLAT_SUCCESS, "Failed to decrypt ciphertext");
156183

157184
err = dec_sink.flush(dec_sink.ctx);
158185

159186
zassert_equal(err, SUIT_PLAT_SUCCESS, "Failed to flush decrypt filter");
160187

161-
err = suit_digest_sink_digest_match(digest_sink.ctx);
162-
163-
zassert_equal(err, SUIT_PLAT_SUCCESS, "Decrypted content digest mismatch");
164-
165188
err = dec_sink.release(dec_sink.ctx);
166189

167190
zassert_equal(err, SUIT_PLAT_SUCCESS, "Failed to release decrypt filter");
191+
192+
zassert_equal(memcmp(output_buffer, plaintext, strlen(plaintext)), 0, "Decrypted plaintext does not match");
168193
}

0 commit comments

Comments
 (0)