@@ -95,6 +95,57 @@ void test_crypto_hash_verify(void)
95
95
mbedtls_psa_crypto_free ();
96
96
}
97
97
98
+ void test_crypto_symmetric_cipher_encrypt_decrypt (void )
99
+ {
100
+ psa_key_slot_t slot = 1 ;
101
+ psa_key_type_t key_type = PSA_KEY_TYPE_AES;
102
+ psa_algorithm_t alg = PSA_ALG_CBC_NO_PADDING;
103
+ psa_cipher_operation_t operation;
104
+ psa_key_policy_t policy;
105
+ size_t output_len;
106
+ static const unsigned char key[] = {
107
+ 0x2b , 0x7e , 0x15 , 0x16 , 0x28 , 0xae , 0xd2 , 0xa6 ,
108
+ 0xab , 0xf7 , 0x15 , 0x88 , 0x09 , 0xcf , 0x4f , 0x3c
109
+ };
110
+ static const unsigned char input[] = {
111
+ 0xb0 , 0x28 , 0x9f , 0x04 , 0xdc , 0x7f , 0xe2 , 0x25 ,
112
+ 0xa2 , 0xce , 0xe9 , 0xd3 , 0xb9 , 0xbc , 0xc7 , 0x2f
113
+ };
114
+ static const unsigned char expected_encryption[] = {
115
+ 0x28 , 0x8d , 0x76 , 0xc0 , 0xa7 , 0x09 , 0x50 , 0x3f ,
116
+ 0x87 , 0x96 , 0x1e , 0x96 , 0x05 , 0xcb , 0xb9 , 0x6d
117
+ };
118
+ unsigned char encrypted[sizeof (input)], decrypted[sizeof (input)], iv[16 ];
119
+
120
+ memset (iv, 0x2a , sizeof (iv));
121
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_crypto_init ());
122
+
123
+ psa_key_policy_init (&policy);
124
+ psa_key_policy_set_usage (&policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg);
125
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_set_key_policy (slot, &policy));
126
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_import_key (slot, key_type, key, sizeof (key)));
127
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_cipher_encrypt_setup (&operation, slot, alg));
128
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_cipher_set_iv (&operation, iv, sizeof (iv)));
129
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_cipher_update (&operation, input, sizeof (input),
130
+ encrypted, sizeof (encrypted), &output_len));
131
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_cipher_finish (&operation, encrypted + output_len,
132
+ sizeof (encrypted) - output_len, &output_len));
133
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_cipher_abort (&operation));
134
+ TEST_ASSERT_EQUAL_HEX8_ARRAY (expected_encryption, encrypted, sizeof (expected_encryption));
135
+
136
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_cipher_decrypt_setup (&operation, slot, alg));
137
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_cipher_set_iv (&operation, iv, sizeof (iv)));
138
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_cipher_update (&operation, encrypted, sizeof (encrypted),
139
+ decrypted, sizeof (decrypted), &output_len));
140
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_cipher_finish (&operation, decrypted + output_len,
141
+ sizeof (decrypted) - output_len, &output_len));
142
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_cipher_abort (&operation));
143
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_destroy_key (slot));
144
+ TEST_ASSERT_EQUAL_HEX8_ARRAY (input, decrypted, sizeof (input));
145
+
146
+ mbedtls_psa_crypto_free ();
147
+ }
148
+
98
149
utest::v1::status_t case_failure_handler (const Case *const source, const failure_t reason)
99
150
{
100
151
mbedtls_psa_crypto_free ();
@@ -112,6 +163,7 @@ Case cases[] = {
112
163
Case (" mbed-crypto random" , test_crypto_random, case_failure_handler),
113
164
Case (" mbed-crypto asymmetric encrypt/decrypt" , test_crypto_asymmetric_encrypt_decrypt, case_failure_handler),
114
165
Case (" mbed-crypto hash verify" , test_crypto_hash_verify, case_failure_handler),
166
+ Case (" mbed-crypto symmetric cipher encrypt/decrypt" , test_crypto_symmetric_cipher_encrypt_decrypt, case_failure_handler),
115
167
};
116
168
117
169
Specification specification (test_setup, cases);
0 commit comments