blob: 03ce5b33bf817e5b1964a9329d4e309f23a175a6 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
Gilles Peskinee59236f2018-01-27 23:32:46 +01003#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +03004
5#if(UINT32_MAX > SIZE_MAX)
Gilles Peskine2d277862018-06-18 15:41:12 +02006#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX )
itayzafrir3e02b3b2018-06-12 17:06:52 +03007#else
Gilles Peskine2d277862018-06-18 15:41:12 +02008#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1
itayzafrir3e02b3b2018-06-12 17:06:52 +03009#endif
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020010
Jaeden Amerof24c7f82018-06-27 17:20:43 +010011/** An invalid export length that will never be set by psa_export_key(). */
12static const size_t INVALID_EXPORT_LENGTH = ~0U;
13
Gilles Peskined35a1cc2018-06-26 21:26:10 +020014/** Test if a buffer is all-bits zero.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020015 *
16 * \param buffer Pointer to the beginning of the buffer.
17 * \param size Size of the buffer in bytes.
18 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020019 * \return 1 if the buffer is all-bits-zero.
20 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020021 */
Gilles Peskine3f669c32018-06-21 09:21:51 +020022static int mem_is_zero( void *buffer, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020023{
24 size_t i;
25 for( i = 0; i < size; i++ )
26 {
27 if( ( (unsigned char *) buffer )[i] != 0 )
Gilles Peskine3f669c32018-06-21 09:21:51 +020028 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020029 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020030 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020031}
Gilles Peskine818ca122018-06-20 18:16:48 +020032
Gilles Peskine48c0ea12018-06-21 14:15:31 +020033static int key_type_is_raw_bytes( psa_key_type_t type )
34{
35 psa_key_type_t category = type & PSA_KEY_TYPE_CATEGORY_MASK;
36 return( category == PSA_KEY_TYPE_RAW_DATA ||
37 category == PSA_KEY_TYPE_CATEGORY_SYMMETRIC );
38}
39
Gilles Peskine818ca122018-06-20 18:16:48 +020040static int exercise_mac_key( psa_key_slot_t key,
41 psa_key_usage_t usage,
42 psa_algorithm_t alg )
43{
44 psa_mac_operation_t operation;
45 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +020046 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +020047 size_t mac_length = sizeof( mac );
48
49 if( usage & PSA_KEY_USAGE_SIGN )
50 {
51 TEST_ASSERT( psa_mac_start( &operation, key, alg ) == PSA_SUCCESS );
52 TEST_ASSERT( psa_mac_update( &operation,
53 input, sizeof( input ) ) == PSA_SUCCESS );
54 TEST_ASSERT( psa_mac_finish( &operation,
55 mac, sizeof( input ),
56 &mac_length ) == PSA_SUCCESS );
57 }
58
59 if( usage & PSA_KEY_USAGE_VERIFY )
60 {
61 psa_status_t verify_status =
62 ( usage & PSA_KEY_USAGE_SIGN ?
63 PSA_SUCCESS :
64 PSA_ERROR_INVALID_SIGNATURE );
65 TEST_ASSERT( psa_mac_start( &operation, key, alg ) == PSA_SUCCESS );
66 TEST_ASSERT( psa_mac_update( &operation,
67 input, sizeof( input ) ) == PSA_SUCCESS );
68 TEST_ASSERT( psa_mac_verify( &operation, mac, mac_length ) == verify_status );
69 }
70
71 return( 1 );
72
73exit:
74 psa_mac_abort( &operation );
75 return( 0 );
76}
77
78static int exercise_cipher_key( psa_key_slot_t key,
79 psa_key_usage_t usage,
80 psa_algorithm_t alg )
81{
82 psa_cipher_operation_t operation;
83 unsigned char iv[16] = {0};
84 size_t iv_length = sizeof( iv );
85 const unsigned char plaintext[16] = "Hello, world...";
86 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
87 size_t ciphertext_length = sizeof( ciphertext );
88 unsigned char decrypted[sizeof( ciphertext )];
89 size_t part_length;
90
91 if( usage & PSA_KEY_USAGE_ENCRYPT )
92 {
93 TEST_ASSERT( psa_encrypt_setup( &operation, key, alg ) == PSA_SUCCESS );
94 TEST_ASSERT( psa_encrypt_generate_iv( &operation,
95 iv, sizeof( iv ),
96 &iv_length ) == PSA_SUCCESS );
97 TEST_ASSERT( psa_cipher_update( &operation,
98 plaintext, sizeof( plaintext ),
99 ciphertext, sizeof( ciphertext ),
100 &ciphertext_length ) == PSA_SUCCESS );
101 TEST_ASSERT( psa_cipher_finish( &operation,
102 ciphertext + ciphertext_length,
103 sizeof( ciphertext ) - ciphertext_length,
104 &part_length ) == PSA_SUCCESS );
105 ciphertext_length += part_length;
106 }
107
108 if( usage & PSA_KEY_USAGE_DECRYPT )
109 {
110 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700111 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200112 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
113 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200114 size_t bits;
115 TEST_ASSERT( psa_get_key_information( key, &type, &bits ) );
116 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
117 }
118 TEST_ASSERT( psa_decrypt_setup( &operation, key, alg ) == PSA_SUCCESS );
119 TEST_ASSERT( psa_encrypt_set_iv( &operation,
120 iv, iv_length ) == PSA_SUCCESS );
121 TEST_ASSERT( psa_cipher_update( &operation,
122 ciphertext, ciphertext_length,
123 decrypted, sizeof( decrypted ),
124 &part_length ) == PSA_SUCCESS );
125 status = psa_cipher_finish( &operation,
126 decrypted + part_length,
127 sizeof( decrypted ) - part_length,
128 &part_length );
129 /* For a stream cipher, all inputs are valid. For a block cipher,
130 * if the input is some aribtrary data rather than an actual
131 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700132 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700133 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine818ca122018-06-20 18:16:48 +0200134 TEST_ASSERT( status == PSA_SUCCESS );
135 else
136 TEST_ASSERT( status == PSA_SUCCESS ||
137 status == PSA_ERROR_INVALID_PADDING );
138 }
139
140 return( 1 );
141
142exit:
143 psa_cipher_abort( &operation );
144 return( 0 );
145}
146
147static int exercise_aead_key( psa_key_slot_t key,
148 psa_key_usage_t usage,
149 psa_algorithm_t alg )
150{
151 unsigned char nonce[16] = {0};
152 size_t nonce_length = sizeof( nonce );
153 unsigned char plaintext[16] = "Hello, world...";
154 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
155 size_t ciphertext_length = sizeof( ciphertext );
156 size_t plaintext_length = sizeof( ciphertext );
157
158 if( usage & PSA_KEY_USAGE_ENCRYPT )
159 {
160 TEST_ASSERT( psa_aead_encrypt( key, alg,
161 nonce, nonce_length,
162 NULL, 0,
163 plaintext, sizeof( plaintext ),
164 ciphertext, sizeof( ciphertext ),
165 &ciphertext_length ) == PSA_SUCCESS );
166 }
167
168 if( usage & PSA_KEY_USAGE_DECRYPT )
169 {
170 psa_status_t verify_status =
171 ( usage & PSA_KEY_USAGE_ENCRYPT ?
172 PSA_SUCCESS :
173 PSA_ERROR_INVALID_SIGNATURE );
174 TEST_ASSERT( psa_aead_decrypt( key, alg,
175 nonce, nonce_length,
176 NULL, 0,
177 ciphertext, ciphertext_length,
178 plaintext, sizeof( plaintext ),
179 &plaintext_length ) == verify_status );
180 }
181
182 return( 1 );
183
184exit:
185 return( 0 );
186}
187
188static int exercise_signature_key( psa_key_slot_t key,
189 psa_key_usage_t usage,
190 psa_algorithm_t alg )
191{
Gilles Peskineca45c352018-06-26 16:13:09 +0200192 unsigned char payload[16] = {1};
Gilles Peskine818ca122018-06-20 18:16:48 +0200193 size_t payload_length = sizeof( payload );
Gilles Peskine69c12672018-06-28 00:07:19 +0200194 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200195 size_t signature_length = sizeof( signature );
196
197 if( usage & PSA_KEY_USAGE_SIGN )
198 {
199 TEST_ASSERT( psa_asymmetric_sign( key, alg,
200 payload, payload_length,
201 NULL, 0,
202 signature, sizeof( signature ),
203 &signature_length ) == PSA_SUCCESS );
204 }
205
206 if( usage & PSA_KEY_USAGE_VERIFY )
207 {
208 psa_status_t verify_status =
209 ( usage & PSA_KEY_USAGE_SIGN ?
210 PSA_SUCCESS :
211 PSA_ERROR_INVALID_SIGNATURE );
212 TEST_ASSERT( psa_asymmetric_verify( key, alg,
213 payload, payload_length,
214 NULL, 0,
215 signature, signature_length ) ==
216 verify_status );
217 }
218
219 return( 1 );
220
221exit:
222 return( 0 );
223}
224
225static int exercise_asymmetric_encryption_key( psa_key_slot_t key,
226 psa_key_usage_t usage,
227 psa_algorithm_t alg )
228{
229 unsigned char plaintext[256] = "Hello, world...";
230 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
231 size_t ciphertext_length = sizeof( ciphertext );
232 size_t plaintext_length = 16;
233
234 if( usage & PSA_KEY_USAGE_ENCRYPT )
235 {
236 TEST_ASSERT(
237 psa_asymmetric_encrypt( key, alg,
238 plaintext, plaintext_length,
239 NULL, 0,
240 ciphertext, sizeof( ciphertext ),
241 &ciphertext_length ) == PSA_SUCCESS );
242 }
243
244 if( usage & PSA_KEY_USAGE_DECRYPT )
245 {
246 psa_status_t status =
247 psa_asymmetric_decrypt( key, alg,
248 ciphertext, ciphertext_length,
249 NULL, 0,
250 plaintext, sizeof( plaintext ),
251 &plaintext_length );
252 TEST_ASSERT( status == PSA_SUCCESS ||
253 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
254 ( status == PSA_ERROR_INVALID_ARGUMENT ||
255 status == PSA_ERROR_INVALID_PADDING ) ) );
256 }
257
258 return( 1 );
259
260exit:
261 return( 0 );
262}
Gilles Peskinee59236f2018-01-27 23:32:46 +0100263/* END_HEADER */
264
265/* BEGIN_DEPENDENCIES
266 * depends_on:MBEDTLS_PSA_CRYPTO_C
267 * END_DEPENDENCIES
268 */
269
270/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200271void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100272{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100273 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100274 int i;
275 for( i = 0; i <= 1; i++ )
276 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100277 status = psa_crypto_init( );
278 TEST_ASSERT( status == PSA_SUCCESS );
279 status = psa_crypto_init( );
280 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100281 mbedtls_psa_crypto_free( );
282 }
283}
284/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100285
286/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200287void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100288{
289 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200290 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100291 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100292
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100293 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300294 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100295 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
296
Gilles Peskine4abf7412018-06-18 16:35:34 +0200297 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200298 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100299 if( status == PSA_SUCCESS )
300 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
301
302exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100303 mbedtls_psa_crypto_free( );
304}
305/* END_CASE */
306
307/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300308void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300309 int type_arg,
310 int alg_arg,
311 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100312 int expected_bits,
313 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200314 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100315 int canonical_input )
316{
317 int slot = 1;
318 int slot2 = slot + 1;
319 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200320 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200321 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100322 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100323 unsigned char *exported = NULL;
324 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100325 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100326 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100327 size_t reexported_length;
328 psa_key_type_t got_type;
329 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200330 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100331
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100332 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300333 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
334 export_size = (ssize_t) data->len + export_size_delta;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100335 exported = mbedtls_calloc( 1, export_size );
336 TEST_ASSERT( exported != NULL );
337 if( ! canonical_input )
338 {
339 reexported = mbedtls_calloc( 1, export_size );
340 TEST_ASSERT( reexported != NULL );
341 }
342 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
343
mohammad1603a97cb8c2018-03-28 03:46:26 -0700344 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200345 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700346 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
347
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100348 /* Import the key */
349 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200350 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100351
352 /* Test the key information */
353 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200354 &got_type,
355 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100356 TEST_ASSERT( got_type == type );
357 TEST_ASSERT( got_bits == (size_t) expected_bits );
358
359 /* Export the key */
360 status = psa_export_key( slot,
361 exported, export_size,
362 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200363 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100364
365 /* The exported length must be set by psa_export_key() to a value between 0
366 * and export_size. On errors, the exported length must be 0. */
367 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
368 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
369 TEST_ASSERT( exported_length <= export_size );
370
Gilles Peskine3f669c32018-06-21 09:21:51 +0200371 TEST_ASSERT( mem_is_zero( exported + exported_length,
372 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100373 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200374 {
375 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100376 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200377 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100378
379 if( canonical_input )
380 {
Gilles Peskine4abf7412018-06-18 16:35:34 +0200381 TEST_ASSERT( exported_length == data->len );
382 TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100383 }
384 else
385 {
mohammad1603a97cb8c2018-03-28 03:46:26 -0700386 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
387
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100388 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200389 exported,
390 export_size ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100391 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200392 reexported,
393 export_size,
394 &reexported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100395 TEST_ASSERT( reexported_length == exported_length );
396 TEST_ASSERT( memcmp( reexported, exported,
397 exported_length ) == 0 );
398 }
399
400destroy:
401 /* Destroy the key */
402 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
403 TEST_ASSERT( psa_get_key_information(
404 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
405
406exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300407 mbedtls_free( exported );
408 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100409 mbedtls_psa_crypto_free( );
410}
411/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100412
Moran Pekerf709f4a2018-06-06 17:26:04 +0300413/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300414void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200415 int type_arg,
416 int alg_arg,
417 int expected_bits,
418 int public_key_expected_length,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200419 int expected_export_status_arg )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300420{
421 int slot = 1;
422 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200423 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200424 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300425 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300426 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300427 size_t export_size;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100428 size_t exported_length = INVALID_EXPORT_LENGTH;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300429 psa_key_type_t got_type;
430 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200431 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300432
Moran Pekerf709f4a2018-06-06 17:26:04 +0300433 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300434 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
435 export_size = (ssize_t) data->len;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300436 exported = mbedtls_calloc( 1, export_size );
437 TEST_ASSERT( exported != NULL );
438
439 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
440
441 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200442 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300443 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
444
445 /* Import the key */
446 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200447 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300448
449 /* Test the key information */
450 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200451 &got_type,
452 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300453 TEST_ASSERT( got_type == type );
454 TEST_ASSERT( got_bits == (size_t) expected_bits );
455
456 /* Export the key */
457 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200458 exported, export_size,
459 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200460 TEST_ASSERT( status == expected_export_status );
Jaeden Amero2a671e92018-06-27 17:47:40 +0100461 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
462 TEST_ASSERT( mem_is_zero( exported + exported_length,
463 export_size - exported_length ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300464 if( status != PSA_SUCCESS )
465 goto destroy;
466
Moran Pekerf709f4a2018-06-06 17:26:04 +0300467destroy:
468 /* Destroy the key */
469 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
470 TEST_ASSERT( psa_get_key_information(
471 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
472
473exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300474 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300475 mbedtls_psa_crypto_free( );
476}
477/* END_CASE */
478
Gilles Peskine20035e32018-02-03 22:44:14 +0100479/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200480void import_and_exercise_key( data_t *data,
481 int type_arg,
482 int bits_arg,
483 int alg_arg )
484{
485 int slot = 1;
486 psa_key_type_t type = type_arg;
487 size_t bits = bits_arg;
488 psa_algorithm_t alg = alg_arg;
489 psa_key_usage_t usage =
490 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
491 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
492 PSA_KEY_USAGE_VERIFY :
493 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
494 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
495 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
496 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
497 PSA_KEY_USAGE_ENCRYPT :
498 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
499 0 );
500 psa_key_policy_t policy;
501 psa_key_type_t got_type;
502 size_t got_bits;
503 psa_status_t status;
504
505 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
506
507 psa_key_policy_init( &policy );
508 psa_key_policy_set_usage( &policy, usage, alg );
509 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
510
511 /* Import the key */
512 status = psa_import_key( slot, type, data->x, data->len );
513 TEST_ASSERT( status == PSA_SUCCESS );
514
515 /* Test the key information */
516 TEST_ASSERT( psa_get_key_information( slot,
517 &got_type,
518 &got_bits ) == PSA_SUCCESS );
519 TEST_ASSERT( got_type == type );
520 TEST_ASSERT( got_bits == bits );
521
522 /* Do something with the key according to its type and permitted usage. */
523 if( PSA_ALG_IS_MAC( alg ) )
524 exercise_mac_key( slot, usage, alg );
525 else if( PSA_ALG_IS_CIPHER( alg ) )
526 exercise_cipher_key( slot, usage, alg );
527 else if( PSA_ALG_IS_AEAD( alg ) )
528 exercise_aead_key( slot, usage, alg );
529 else if( PSA_ALG_IS_SIGN( alg ) )
530 exercise_signature_key( slot, usage, alg );
531 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
532 exercise_asymmetric_encryption_key( slot, usage, alg );
533
534exit:
535 psa_destroy_key( slot );
536 mbedtls_psa_crypto_free( );
537}
538/* END_CASE */
539
540/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +0200541void key_policy( int usage_arg, int alg_arg )
542{
543 int key_slot = 1;
544 psa_algorithm_t alg = alg_arg;
545 psa_key_usage_t usage = usage_arg;
546 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
547 unsigned char key[32] = {0};
548 psa_key_policy_t policy_set;
549 psa_key_policy_t policy_get;
550
551 memset( key, 0x2a, sizeof( key ) );
552
553 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
554
555 psa_key_policy_init( &policy_set );
556 psa_key_policy_init( &policy_get );
557
558 psa_key_policy_set_usage( &policy_set, usage, alg );
559
560 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
561 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
562 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
563
564 TEST_ASSERT( psa_import_key( key_slot, key_type,
565 key, sizeof( key ) ) == PSA_SUCCESS );
566
567 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
568
569 TEST_ASSERT( policy_get.usage == policy_set.usage );
570 TEST_ASSERT( policy_get.alg == policy_set.alg );
571
572exit:
573 psa_destroy_key( key_slot );
574 mbedtls_psa_crypto_free( );
575}
576/* END_CASE */
577
578/* BEGIN_CASE */
579void key_policy_fail( int usage_arg, int alg_arg, int expected_status,
580 data_t *keypair )
581{
582 int key_slot = 1;
583 psa_algorithm_t alg = alg_arg;
584 psa_key_usage_t usage = usage_arg;
585 size_t signature_length = 0;
586 psa_key_policy_t policy;
587 int actual_status = PSA_SUCCESS;
588
589 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
590
591 psa_key_policy_init( &policy );
592 psa_key_policy_set_usage( &policy, usage, alg );
593 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
594
595 if( usage & PSA_KEY_USAGE_EXPORT )
596 {
597 TEST_ASSERT( keypair != NULL );
598 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) );
599 TEST_ASSERT( psa_import_key( key_slot,
600 PSA_KEY_TYPE_RSA_KEYPAIR,
601 keypair->x,
602 keypair->len ) == PSA_SUCCESS );
603 actual_status = psa_asymmetric_sign( key_slot, alg,
604 NULL, 0,
605 NULL, 0,
606 NULL, 0, &signature_length );
607 }
608
609 if( usage & PSA_KEY_USAGE_SIGN )
610 {
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100611 size_t data_length;
Gilles Peskined5b33222018-06-18 22:20:03 +0200612 TEST_ASSERT( keypair != NULL );
613 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) );
614 TEST_ASSERT( psa_import_key( key_slot,
615 PSA_KEY_TYPE_RSA_KEYPAIR,
616 keypair->x,
617 keypair->len ) == PSA_SUCCESS );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100618 actual_status = psa_export_key( key_slot, NULL, 0, &data_length );
Gilles Peskined5b33222018-06-18 22:20:03 +0200619 }
620
621 TEST_ASSERT( actual_status == expected_status );
622
623exit:
624 psa_destroy_key( key_slot );
625 mbedtls_psa_crypto_free( );
626}
627/* END_CASE */
628
629/* BEGIN_CASE */
630void key_lifetime( int lifetime_arg )
631{
632 int key_slot = 1;
633 psa_key_type_t key_type = PSA_ALG_CBC_BASE;
634 unsigned char key[32] = {0};
635 psa_key_lifetime_t lifetime_set = lifetime_arg;
636 psa_key_lifetime_t lifetime_get;
637
638 memset( key, 0x2a, sizeof( key ) );
639
640 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
641
642 TEST_ASSERT( psa_set_key_lifetime( key_slot,
643 lifetime_set ) == PSA_SUCCESS );
644
645 TEST_ASSERT( psa_import_key( key_slot, key_type,
646 key, sizeof( key ) ) == PSA_SUCCESS );
647
648 TEST_ASSERT( psa_get_key_lifetime( key_slot,
649 &lifetime_get ) == PSA_SUCCESS );
650
651 TEST_ASSERT( lifetime_get == lifetime_set );
652
653exit:
654 psa_destroy_key( key_slot );
655 mbedtls_psa_crypto_free( );
656}
657/* END_CASE */
658
659/* BEGIN_CASE */
660void key_lifetime_set_fail( int key_slot_arg,
661 int lifetime_arg,
662 int expected_status_arg )
663{
664 psa_key_slot_t key_slot = key_slot_arg;
665 psa_key_lifetime_t lifetime_set = lifetime_arg;
666 psa_status_t actual_status;
667 psa_status_t expected_status = expected_status_arg;
668
669 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
670
671 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
672
673 if( actual_status == PSA_SUCCESS )
674 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
675
676 TEST_ASSERT( expected_status == actual_status );
677
678exit:
679 psa_destroy_key( key_slot );
680 mbedtls_psa_crypto_free( );
681}
682/* END_CASE */
683
684/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200685void hash_setup( int alg_arg,
686 int expected_status_arg )
687{
688 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200689 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200690 psa_hash_operation_t operation;
691 psa_status_t status;
692
693 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
694
695 status = psa_hash_start( &operation, alg );
696 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200697 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200698
699exit:
700 mbedtls_psa_crypto_free( );
701}
702/* END_CASE */
703
704/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300705void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100706{
707 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +0200708 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100709 size_t actual_hash_length;
710 psa_hash_operation_t operation;
711
Gilles Peskine69c12672018-06-28 00:07:19 +0200712 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
713 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
714
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100715 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300716 TEST_ASSERT( expected_hash != NULL );
717 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
718 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100719
720 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
721
722 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
723 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200724 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100725 TEST_ASSERT( psa_hash_finish( &operation,
726 actual_hash, sizeof( actual_hash ),
727 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200728 TEST_ASSERT( actual_hash_length == expected_hash->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300729 TEST_ASSERT( memcmp( expected_hash->x, actual_hash,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200730 expected_hash->len ) == 0 );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100731
732exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100733 mbedtls_psa_crypto_free( );
734}
735/* END_CASE */
736
737/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300738void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100739{
740 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100741 psa_hash_operation_t operation;
742
Gilles Peskine69c12672018-06-28 00:07:19 +0200743 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
744 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
745
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100746 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300747 TEST_ASSERT( expected_hash != NULL );
748 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
749 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100750
751 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
752
753 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
754 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200755 input->x,
756 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100757 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300758 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200759 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100760
761exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100762 mbedtls_psa_crypto_free( );
763}
764/* END_CASE */
765
766/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200767void mac_setup( int key_type_arg,
768 data_t *key,
769 int alg_arg,
770 int expected_status_arg )
771{
772 int key_slot = 1;
773 psa_key_type_t key_type = key_type_arg;
774 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200775 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200776 psa_mac_operation_t operation;
777 psa_key_policy_t policy;
778 psa_status_t status;
779
780 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
781
782 psa_key_policy_init( &policy );
783 psa_key_policy_set_usage( &policy,
784 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
785 alg );
786 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
787
788 TEST_ASSERT( psa_import_key( key_slot, key_type,
789 key->x, key->len ) == PSA_SUCCESS );
790
791 status = psa_mac_start( &operation, key_slot, alg );
792 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200793 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200794
795exit:
796 psa_destroy_key( key_slot );
797 mbedtls_psa_crypto_free( );
798}
799/* END_CASE */
800
801/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +0200802void mac_verify( int key_type_arg,
803 data_t *key,
804 int alg_arg,
805 data_t *input,
806 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +0100807{
808 int key_slot = 1;
809 psa_key_type_t key_type = key_type_arg;
810 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100811 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -0700812 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100813
Gilles Peskine69c12672018-06-28 00:07:19 +0200814 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
815
Gilles Peskine8c9def32018-02-08 10:02:12 +0100816 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100817 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100818 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300819 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300820 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
821 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100822
823 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
824
mohammad16036df908f2018-04-02 08:34:15 -0700825 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200826 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -0700827 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
828
Gilles Peskine8c9def32018-02-08 10:02:12 +0100829 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200830 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +0200831
Gilles Peskine8c9def32018-02-08 10:02:12 +0100832 TEST_ASSERT( psa_mac_start( &operation, key_slot, alg ) == PSA_SUCCESS );
833 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
834 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200835 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100836 TEST_ASSERT( psa_mac_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300837 expected_mac->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200838 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100839
840exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +0100841 psa_destroy_key( key_slot );
842 mbedtls_psa_crypto_free( );
843}
844/* END_CASE */
845
846/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200847void cipher_setup( int key_type_arg,
848 data_t *key,
849 int alg_arg,
850 int expected_status_arg )
851{
852 int key_slot = 1;
853 psa_key_type_t key_type = key_type_arg;
854 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200855 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200856 psa_cipher_operation_t operation;
857 psa_key_policy_t policy;
858 psa_status_t status;
859
860 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
861
862 psa_key_policy_init( &policy );
863 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
864 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
865
866 TEST_ASSERT( psa_import_key( key_slot, key_type,
867 key->x, key->len ) == PSA_SUCCESS );
868
869 status = psa_encrypt_setup( &operation, key_slot, alg );
870 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200871 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200872
873exit:
874 psa_destroy_key( key_slot );
875 mbedtls_psa_crypto_free( );
876}
877/* END_CASE */
878
879/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +0200880void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300881 data_t *key,
882 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200883 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +0200884{
885 int key_slot = 1;
886 psa_status_t status;
887 psa_key_type_t key_type = key_type_arg;
888 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200889 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200890 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200891 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300892 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200893 size_t output_buffer_size = 0;
894 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200895 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200896 psa_cipher_operation_t operation;
897
Gilles Peskine50e586b2018-06-08 14:28:46 +0200898 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200899 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200900 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300901 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
902 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
903 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200904
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200905 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
906 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200907
908 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
909
910 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200911 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200912
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200913 TEST_ASSERT( psa_encrypt_setup( &operation,
914 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200915
916 TEST_ASSERT( psa_encrypt_set_iv( &operation,
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200917 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200918 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200919 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300920 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200921
Gilles Peskine4abf7412018-06-18 16:35:34 +0200922 TEST_ASSERT( psa_cipher_update( &operation,
923 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200924 output, output_buffer_size,
925 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200926 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200927 status = psa_cipher_finish( &operation,
928 output + function_output_length,
929 output_buffer_size,
930 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200931 total_output_length += function_output_length;
932
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200933 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200934 if( expected_status == PSA_SUCCESS )
935 {
936 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200937 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300938 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200939 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200940 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200941
Gilles Peskine50e586b2018-06-08 14:28:46 +0200942exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300943 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200944 psa_destroy_key( key_slot );
945 mbedtls_psa_crypto_free( );
946}
947/* END_CASE */
948
949/* BEGIN_CASE */
950void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300951 data_t *key,
952 data_t *input,
953 int first_part_size,
954 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +0200955{
956 int key_slot = 1;
957 psa_key_type_t key_type = key_type_arg;
958 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200959 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200960 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300961 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200962 size_t output_buffer_size = 0;
963 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200964 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200965 psa_cipher_operation_t operation;
966
Gilles Peskine50e586b2018-06-08 14:28:46 +0200967 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200968 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200969 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300970 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
971 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
972 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200973
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200974 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
975 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200976
977 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
978
979 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200980 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200981
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200982 TEST_ASSERT( psa_encrypt_setup( &operation,
983 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200984
985 TEST_ASSERT( psa_encrypt_set_iv( &operation,
986 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200987 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200988 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300989 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200990
Gilles Peskine4abf7412018-06-18 16:35:34 +0200991 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300992 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200993 output, output_buffer_size,
994 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200995 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200996 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300997 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200998 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200999 output, output_buffer_size,
1000 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001001 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001002 TEST_ASSERT( psa_cipher_finish( &operation,
1003 output + function_output_length,
1004 output_buffer_size,
1005 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001006 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001007 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1008
Gilles Peskine4abf7412018-06-18 16:35:34 +02001009 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001010 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001011 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001012
1013exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001014 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001015 psa_destroy_key( key_slot );
1016 mbedtls_psa_crypto_free( );
1017}
1018/* END_CASE */
1019
1020/* BEGIN_CASE */
1021void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001022 data_t *key,
1023 data_t *input,
1024 int first_part_size,
1025 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001026{
1027 int key_slot = 1;
1028
1029 psa_key_type_t key_type = key_type_arg;
1030 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001031 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001032 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001033 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001034 size_t output_buffer_size = 0;
1035 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001036 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001037 psa_cipher_operation_t operation;
1038
Gilles Peskine50e586b2018-06-08 14:28:46 +02001039 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001040 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001041 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001042 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1043 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1044 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001045
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001046 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1047 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001048
1049 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1050
1051 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001052 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001053
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001054 TEST_ASSERT( psa_decrypt_setup( &operation,
1055 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001056
1057 TEST_ASSERT( psa_encrypt_set_iv( &operation,
1058 iv, sizeof( iv ) ) == PSA_SUCCESS );
1059
Gilles Peskine4abf7412018-06-18 16:35:34 +02001060 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001061 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001062 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001063
Gilles Peskine4abf7412018-06-18 16:35:34 +02001064 TEST_ASSERT( (unsigned int) first_part_size < input->len );
1065 TEST_ASSERT( psa_cipher_update( &operation,
1066 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001067 output, output_buffer_size,
1068 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001069 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001070 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001071 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001072 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001073 output, output_buffer_size,
1074 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001075 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001076 TEST_ASSERT( psa_cipher_finish( &operation,
1077 output + function_output_length,
1078 output_buffer_size,
1079 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001080 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001081 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1082
Gilles Peskine4abf7412018-06-18 16:35:34 +02001083 TEST_ASSERT( total_output_length == expected_output->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001084 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001085 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001086
1087exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001088 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001089 psa_destroy_key( key_slot );
1090 mbedtls_psa_crypto_free( );
1091}
1092/* END_CASE */
1093
Gilles Peskine50e586b2018-06-08 14:28:46 +02001094/* BEGIN_CASE */
1095void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001096 data_t *key,
1097 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001098 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001099{
1100 int key_slot = 1;
1101 psa_status_t status;
1102 psa_key_type_t key_type = key_type_arg;
1103 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001104 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001105 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001106 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001107 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001108 size_t output_buffer_size = 0;
1109 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001110 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001111 psa_cipher_operation_t operation;
1112
Gilles Peskine50e586b2018-06-08 14:28:46 +02001113 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001114 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001115 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001116 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1117 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1118 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001119
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001120 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1121 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001122
1123 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1124
1125 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001126 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001127
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001128 TEST_ASSERT( psa_decrypt_setup( &operation,
1129 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001130
1131 TEST_ASSERT( psa_encrypt_set_iv( &operation,
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001132 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001133
Gilles Peskine4abf7412018-06-18 16:35:34 +02001134 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001135 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001136 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001137
Gilles Peskine4abf7412018-06-18 16:35:34 +02001138 TEST_ASSERT( psa_cipher_update( &operation,
1139 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001140 output, output_buffer_size,
1141 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001142 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001143 status = psa_cipher_finish( &operation,
1144 output + function_output_length,
1145 output_buffer_size,
1146 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001147 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001148 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001149
1150 if( expected_status == PSA_SUCCESS )
1151 {
1152 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001153 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001154 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001155 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001156 }
1157
Gilles Peskine50e586b2018-06-08 14:28:46 +02001158exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001159 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001160 psa_destroy_key( key_slot );
1161 mbedtls_psa_crypto_free( );
1162}
1163/* END_CASE */
1164
Gilles Peskine50e586b2018-06-08 14:28:46 +02001165/* BEGIN_CASE */
1166void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001167 data_t *key,
1168 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02001169{
1170 int key_slot = 1;
1171 psa_key_type_t key_type = key_type_arg;
1172 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07001173 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02001174 size_t iv_size = 16;
1175 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001176 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001177 size_t output1_size = 0;
1178 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001179 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001180 size_t output2_size = 0;
1181 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001182 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001183 psa_cipher_operation_t operation1;
1184 psa_cipher_operation_t operation2;
1185
mohammad1603d7d7ba52018-03-12 18:51:53 +02001186 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001187 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001188 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1189 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001190
mohammad1603d7d7ba52018-03-12 18:51:53 +02001191 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1192
1193 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001194 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001195
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001196 TEST_ASSERT( psa_encrypt_setup( &operation1,
1197 key_slot, alg ) == PSA_SUCCESS );
1198 TEST_ASSERT( psa_decrypt_setup( &operation2,
1199 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001200
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001201 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
1202 iv, iv_size,
1203 &iv_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001204 output1_size = input->len + operation1.block_size;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001205 output1 = mbedtls_calloc( 1, output1_size );
1206 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001207
Gilles Peskine4abf7412018-06-18 16:35:34 +02001208 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001209 output1, output1_size,
1210 &output1_length ) == PSA_SUCCESS );
1211 TEST_ASSERT( psa_cipher_finish( &operation1,
1212 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001213 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001214
Gilles Peskine048b7f02018-06-08 14:20:49 +02001215 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001216
1217 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1218
1219 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001220 output2 = mbedtls_calloc( 1, output2_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001221 TEST_ASSERT( output2 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001222
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001223 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
1224 iv, iv_length ) == PSA_SUCCESS );
1225 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
1226 output2, output2_size,
1227 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001228 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001229 TEST_ASSERT( psa_cipher_finish( &operation2,
1230 output2 + output2_length,
1231 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001232 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001233
Gilles Peskine048b7f02018-06-08 14:20:49 +02001234 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001235
Moran Pekerded84402018-06-06 16:36:50 +03001236 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1237
Gilles Peskine4abf7412018-06-18 16:35:34 +02001238 TEST_ASSERT( input->len == output2_length );
1239 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +03001240
1241exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001242 mbedtls_free( output1 );
1243 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03001244 psa_destroy_key( key_slot );
1245 mbedtls_psa_crypto_free( );
1246}
1247/* END_CASE */
1248
1249/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001250void cipher_verify_output_multipart( int alg_arg,
1251 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001252 data_t *key,
1253 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001254 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03001255{
1256 int key_slot = 1;
1257 psa_key_type_t key_type = key_type_arg;
1258 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03001259 unsigned char iv[16] = {0};
1260 size_t iv_size = 16;
1261 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001262 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001263 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001264 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001265 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001266 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001267 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001268 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001269 psa_cipher_operation_t operation1;
1270 psa_cipher_operation_t operation2;
1271
Moran Pekerded84402018-06-06 16:36:50 +03001272 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001273 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001274 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1275 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001276
Moran Pekerded84402018-06-06 16:36:50 +03001277 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1278
1279 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001280 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001281
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001282 TEST_ASSERT( psa_encrypt_setup( &operation1,
1283 key_slot, alg ) == PSA_SUCCESS );
1284 TEST_ASSERT( psa_decrypt_setup( &operation2,
1285 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001286
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001287 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
1288 iv, iv_size,
1289 &iv_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001290 output1_buffer_size = input->len + operation1.block_size;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001291 output1 = mbedtls_calloc( 1, output1_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001292 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001293
Gilles Peskine4abf7412018-06-18 16:35:34 +02001294 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001295
itayzafrir3e02b3b2018-06-12 17:06:52 +03001296 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001297 output1, output1_buffer_size,
1298 &function_output_length ) == PSA_SUCCESS );
1299 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001300
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001301 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001302 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001303 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001304 output1, output1_buffer_size,
1305 &function_output_length ) == PSA_SUCCESS );
1306 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001307
Gilles Peskine048b7f02018-06-08 14:20:49 +02001308 TEST_ASSERT( psa_cipher_finish( &operation1,
1309 output1 + output1_length,
1310 output1_buffer_size - output1_length,
1311 &function_output_length ) == PSA_SUCCESS );
1312 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001313
1314 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1315
Gilles Peskine048b7f02018-06-08 14:20:49 +02001316 output2_buffer_size = output1_length;
1317 output2 = mbedtls_calloc( 1, output2_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001318 TEST_ASSERT( output2 != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001319
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001320 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
1321 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001322
1323 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001324 output2, output2_buffer_size,
1325 &function_output_length ) == PSA_SUCCESS );
1326 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001327
Gilles Peskine048b7f02018-06-08 14:20:49 +02001328 TEST_ASSERT( psa_cipher_update( &operation2,
1329 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001330 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001331 output2, output2_buffer_size,
1332 &function_output_length ) == PSA_SUCCESS );
1333 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001334
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001335 TEST_ASSERT( psa_cipher_finish( &operation2,
1336 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001337 output2_buffer_size - output2_length,
1338 &function_output_length ) == PSA_SUCCESS );
1339 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001340
mohammad1603d7d7ba52018-03-12 18:51:53 +02001341 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1342
Gilles Peskine4abf7412018-06-18 16:35:34 +02001343 TEST_ASSERT( input->len == output2_length );
1344 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001345
1346exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001347 mbedtls_free( output1 );
1348 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001349 psa_destroy_key( key_slot );
1350 mbedtls_psa_crypto_free( );
1351}
1352/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02001353
Gilles Peskine20035e32018-02-03 22:44:14 +01001354/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001355void aead_encrypt_decrypt( int key_type_arg,
1356 data_t * key_data,
1357 int alg_arg,
1358 data_t * input_data,
1359 data_t * nonce,
1360 data_t * additional_data,
1361 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001362{
1363 int slot = 1;
1364 psa_key_type_t key_type = key_type_arg;
1365 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001366 unsigned char *output_data = NULL;
1367 size_t output_size = 0;
1368 size_t output_length = 0;
1369 unsigned char *output_data2 = NULL;
1370 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001371 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001372 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001373 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001374
Gilles Peskinea1cac842018-06-11 19:33:02 +02001375 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001376 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001377 TEST_ASSERT( nonce != NULL );
1378 TEST_ASSERT( additional_data != NULL );
1379 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1380 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1381 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1382 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1383
Gilles Peskine4abf7412018-06-18 16:35:34 +02001384 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001385 output_data = mbedtls_calloc( 1, output_size );
1386 TEST_ASSERT( output_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001387
1388 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1389
1390 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001391 psa_key_policy_set_usage( &policy,
1392 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
1393 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001394 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1395
1396 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001397 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001398
1399 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001400 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001401 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001402 additional_data->len,
1403 input_data->x, input_data->len,
1404 output_data, output_size,
1405 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001406
1407 if( PSA_SUCCESS == expected_result )
1408 {
1409 output_data2 = mbedtls_calloc( 1, output_length );
1410 TEST_ASSERT( output_data2 != NULL );
1411
1412 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001413 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001414 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001415 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001416 output_data, output_length,
1417 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001418 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02001419
itayzafrir3e02b3b2018-06-12 17:06:52 +03001420 TEST_ASSERT( memcmp( input_data->x, output_data2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001421 input_data->len ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001422 }
Gilles Peskine2d277862018-06-18 15:41:12 +02001423
Gilles Peskinea1cac842018-06-11 19:33:02 +02001424exit:
1425 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001426 mbedtls_free( output_data );
1427 mbedtls_free( output_data2 );
1428 mbedtls_psa_crypto_free( );
1429}
1430/* END_CASE */
1431
1432/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001433void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001434 int alg_arg, data_t * input_data,
1435 data_t * additional_data, data_t * nonce,
1436 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001437{
1438 int slot = 1;
1439 psa_key_type_t key_type = key_type_arg;
1440 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001441 unsigned char *output_data = NULL;
1442 size_t output_size = 0;
1443 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001444 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001445 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001446
Gilles Peskinea1cac842018-06-11 19:33:02 +02001447 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001448 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001449 TEST_ASSERT( additional_data != NULL );
1450 TEST_ASSERT( nonce != NULL );
1451 TEST_ASSERT( expected_result != NULL );
1452 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1453 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1454 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1455 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1456 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
1457
Gilles Peskine4abf7412018-06-18 16:35:34 +02001458 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001459 output_data = mbedtls_calloc( 1, output_size );
1460 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001461
Gilles Peskinea1cac842018-06-11 19:33:02 +02001462 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1463
1464 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001465 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001466 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1467
1468 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001469 key_data->x,
1470 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001471
1472 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001473 nonce->x, nonce->len,
1474 additional_data->x, additional_data->len,
1475 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001476 output_data, output_size,
1477 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001478
itayzafrir3e02b3b2018-06-12 17:06:52 +03001479 TEST_ASSERT( memcmp( output_data, expected_result->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001480 output_length ) == 0 );
1481
Gilles Peskinea1cac842018-06-11 19:33:02 +02001482exit:
1483 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001484 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001485 mbedtls_psa_crypto_free( );
1486}
1487/* END_CASE */
1488
1489/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001490void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001491 int alg_arg, data_t * input_data,
1492 data_t * additional_data, data_t * nonce,
1493 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001494{
1495 int slot = 1;
1496 psa_key_type_t key_type = key_type_arg;
1497 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001498 unsigned char *output_data = NULL;
1499 size_t output_size = 0;
1500 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001501 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001502 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001503 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001504
Gilles Peskinea1cac842018-06-11 19:33:02 +02001505 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001506 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001507 TEST_ASSERT( additional_data != NULL );
1508 TEST_ASSERT( nonce != NULL );
1509 TEST_ASSERT( expected_data != NULL );
1510 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1511 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1512 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1513 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1514 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
1515
Gilles Peskine4abf7412018-06-18 16:35:34 +02001516 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001517 output_data = mbedtls_calloc( 1, output_size );
1518 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001519
Gilles Peskinea1cac842018-06-11 19:33:02 +02001520 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1521
1522 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001523 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001524 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1525
1526 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001527 key_data->x,
1528 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001529
1530 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001531 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001532 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001533 additional_data->len,
1534 input_data->x, input_data->len,
1535 output_data, output_size,
1536 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001537
Gilles Peskine2d277862018-06-18 15:41:12 +02001538 if( expected_result == PSA_SUCCESS )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001539 {
itayzafrir3e02b3b2018-06-12 17:06:52 +03001540 TEST_ASSERT( memcmp( output_data, expected_data->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001541 output_length ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001542 }
1543
Gilles Peskinea1cac842018-06-11 19:33:02 +02001544exit:
1545 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001546 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001547 mbedtls_psa_crypto_free( );
1548}
1549/* END_CASE */
1550
1551/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001552void signature_size( int type_arg,
1553 int bits,
1554 int alg_arg,
1555 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001556{
1557 psa_key_type_t type = type_arg;
1558 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02001559 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01001560 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
1561exit:
1562 ;
1563}
1564/* END_CASE */
1565
1566/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001567void sign_deterministic( int key_type_arg, data_t *key_data,
1568 int alg_arg, data_t *input_data,
1569 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001570{
1571 int slot = 1;
1572 psa_key_type_t key_type = key_type_arg;
1573 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01001574 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01001575 unsigned char *signature = NULL;
1576 size_t signature_size;
1577 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02001578 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01001579
Gilles Peskine20035e32018-02-03 22:44:14 +01001580 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001581 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001582 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001583 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1584 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1585 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01001586
1587 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1588
mohammad1603a97cb8c2018-03-28 03:46:26 -07001589 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001590 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001591 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1592
Gilles Peskine20035e32018-02-03 22:44:14 +01001593 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001594 key_data->x,
1595 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01001596 TEST_ASSERT( psa_get_key_information( slot,
1597 NULL,
1598 &key_bits ) == PSA_SUCCESS );
1599
Gilles Peskine860ce9d2018-06-28 12:23:00 +02001600 /* Allocate a buffer which has the size advertized by the
1601 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001602 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
1603 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01001604 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02001605 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine20035e32018-02-03 22:44:14 +01001606 signature = mbedtls_calloc( 1, signature_size );
1607 TEST_ASSERT( signature != NULL );
1608
Gilles Peskine860ce9d2018-06-28 12:23:00 +02001609 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01001610 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001611 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01001612 NULL, 0,
1613 signature, signature_size,
1614 &signature_length ) == PSA_SUCCESS );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02001615 /* Verify that the signature is correct. */
Gilles Peskine4abf7412018-06-18 16:35:34 +02001616 TEST_ASSERT( signature_length == output_data->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001617 TEST_ASSERT( memcmp( signature, output_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001618 output_data->len ) == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01001619
1620exit:
1621 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01001622 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01001623 mbedtls_psa_crypto_free( );
1624}
1625/* END_CASE */
1626
1627/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001628void sign_fail( int key_type_arg, data_t *key_data,
1629 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02001630 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001631{
1632 int slot = 1;
1633 psa_key_type_t key_type = key_type_arg;
1634 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02001635 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01001636 psa_status_t actual_status;
1637 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01001638 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01001639 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02001640 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01001641
Gilles Peskine20035e32018-02-03 22:44:14 +01001642 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001643 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001644 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1645 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1646
Gilles Peskine20035e32018-02-03 22:44:14 +01001647 signature = mbedtls_calloc( 1, signature_size );
1648 TEST_ASSERT( signature != NULL );
1649
1650 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1651
mohammad1603a97cb8c2018-03-28 03:46:26 -07001652 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001653 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001654 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1655
Gilles Peskine20035e32018-02-03 22:44:14 +01001656 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001657 key_data->x,
1658 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01001659
1660 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001661 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01001662 NULL, 0,
1663 signature, signature_size,
1664 &signature_length );
1665 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02001666 /* The value of *signature_length is unspecified on error, but
1667 * whatever it is, it should be less than signature_size, so that
1668 * if the caller tries to read *signature_length bytes without
1669 * checking the error code then they don't overflow a buffer. */
1670 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01001671
1672exit:
1673 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01001674 mbedtls_free( signature );
1675 mbedtls_psa_crypto_free( );
1676}
1677/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03001678
1679/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001680void asymmetric_verify( int key_type_arg, data_t *key_data,
1681 int alg_arg, data_t *hash_data,
1682 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03001683{
1684 int slot = 1;
1685 psa_key_type_t key_type = key_type_arg;
1686 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001687 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03001688
Gilles Peskine69c12672018-06-28 00:07:19 +02001689 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
1690
itayzafrir5c753392018-05-08 11:18:38 +03001691 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03001692 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03001693 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001694 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1695 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
1696 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03001697
1698 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1699
1700 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001701 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03001702 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1703
1704 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001705 key_data->x,
1706 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03001707
1708 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001709 hash_data->x, hash_data->len,
itayzafrir5c753392018-05-08 11:18:38 +03001710 NULL, 0,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001711 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001712 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03001713exit:
1714 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03001715 mbedtls_psa_crypto_free( );
1716}
1717/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001718
1719/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001720void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
1721 int alg_arg, data_t *hash_data,
1722 data_t *signature_data,
1723 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001724{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001725 int slot = 1;
1726 psa_key_type_t key_type = key_type_arg;
1727 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001728 psa_status_t actual_status;
1729 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001730 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001731
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001732 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001733 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001734 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001735 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1736 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
1737 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001738
1739 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1740
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001741 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001742 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001743 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1744
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001745 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001746 key_data->x,
1747 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001748
1749 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001750 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001751 NULL, 0,
1752 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001753 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001754
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001755 TEST_ASSERT( actual_status == expected_status );
1756
1757exit:
1758 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001759 mbedtls_psa_crypto_free( );
1760}
1761/* END_CASE */
1762
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001763/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001764void asymmetric_encrypt_decrypt( int key_type_arg, data_t *key_data,
1765 int alg_arg, data_t *input_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001766{
1767 int slot = 1;
1768 psa_key_type_t key_type = key_type_arg;
1769 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001770 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001771 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001772 size_t output_length = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001773 unsigned char *output2 = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001774 size_t output2_size = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001775 size_t output2_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02001776 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001777
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001778 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001779 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001780 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1781 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1782
Gilles Peskine4abf7412018-06-18 16:35:34 +02001783 output_size = key_data->len;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001784 output2_size = output_size;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001785 output = mbedtls_calloc( 1, output_size );
1786 TEST_ASSERT( output != NULL );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001787 output2 = mbedtls_calloc( 1, output2_size );
1788 TEST_ASSERT( output2 != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001789
1790 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1791
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001792 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001793 psa_key_policy_set_usage( &policy,
1794 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001795 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001796 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1797
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001798 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001799 key_data->x,
1800 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001801
Gilles Peskineeebd7382018-06-08 18:11:54 +02001802 /* We test encryption by checking that encrypt-then-decrypt gives back
1803 * the original plaintext because of the non-optional random
1804 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02001805 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001806 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001807 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001808 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001809 &output_length ) == PSA_SUCCESS );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001810
Gilles Peskine2d277862018-06-18 15:41:12 +02001811 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001812 output, output_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001813 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001814 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001815 &output2_length ) == PSA_SUCCESS );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001816 TEST_ASSERT( memcmp( input_data->x, output2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001817 input_data->len ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001818
1819exit:
1820 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001821 mbedtls_free( output );
1822 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001823 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001824}
1825/* END_CASE */
1826
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001827/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001828void asymmetric_encrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001829 int alg_arg, data_t *input_data,
1830 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001831{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001832 int slot = 1;
1833 psa_key_type_t key_type = key_type_arg;
1834 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001835 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001836 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001837 size_t output_length = 0;
1838 psa_status_t actual_status;
1839 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001840 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001841
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001842 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001843 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001844 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1845 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1846
Gilles Peskine4abf7412018-06-18 16:35:34 +02001847 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001848 output = mbedtls_calloc( 1, output_size );
1849 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001850
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001851 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1852
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001853 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001854 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001855 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1856
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001857 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001858 key_data->x,
1859 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001860
Gilles Peskine2d277862018-06-18 15:41:12 +02001861 actual_status = psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001862 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001863 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001864 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001865 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001866 TEST_ASSERT( actual_status == expected_status );
1867
1868exit:
1869 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001870 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001871 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001872}
1873/* END_CASE */
1874
1875/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001876void asymmetric_decrypt( int key_type_arg, data_t *key_data,
1877 int alg_arg, data_t *input_data,
1878 data_t *expected_data, int expected_size )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001879{
1880 int slot = 1;
1881 psa_key_type_t key_type = key_type_arg;
1882 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001883 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001884 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001885 size_t output_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02001886 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001887
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001888 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001889 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001890 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001891 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1892 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1893 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
1894
Gilles Peskine4abf7412018-06-18 16:35:34 +02001895 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001896 output = mbedtls_calloc( 1, output_size );
1897 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001898
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001899 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1900
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001901 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001902 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001903 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1904
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001905 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001906 key_data->x,
1907 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001908
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001909 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001910 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001911 NULL, 0,
1912 output,
1913 output_size,
1914 &output_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001915 TEST_ASSERT( (size_t) expected_size == output_length );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001916 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001917
1918exit:
1919 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001920 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001921 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001922}
1923/* END_CASE */
1924
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001925/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001926void asymmetric_decrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001927 int alg_arg, data_t *input_data,
1928 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001929{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001930 int slot = 1;
1931 psa_key_type_t key_type = key_type_arg;
1932 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001933 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001934 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001935 size_t output_length = 0;
1936 psa_status_t actual_status;
1937 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001938 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001939
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001940 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001941 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001942 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1943 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1944
Gilles Peskine4abf7412018-06-18 16:35:34 +02001945 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001946 output = mbedtls_calloc( 1, output_size );
1947 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001948
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001949 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1950
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001951 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001952 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001953 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1954
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001955 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001956 key_data->x,
1957 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001958
Gilles Peskine2d277862018-06-18 15:41:12 +02001959 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001960 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001961 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001962 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001963 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001964 TEST_ASSERT( actual_status == expected_status );
1965
1966exit:
1967 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02001968 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001969 mbedtls_psa_crypto_free( );
1970}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001971/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02001972
1973/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02001974void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02001975{
Gilles Peskinea50d7392018-06-21 10:22:13 +02001976 size_t bytes = bytes_arg;
1977 const unsigned char trail[] = "don't overwrite me";
1978 unsigned char *output = mbedtls_calloc( 1, bytes + sizeof( trail ) );
1979 unsigned char *changed = mbedtls_calloc( 1, bytes );
1980 size_t i;
1981 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02001982
Gilles Peskinea50d7392018-06-21 10:22:13 +02001983 TEST_ASSERT( output != NULL );
1984 TEST_ASSERT( changed != NULL );
1985 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02001986
1987 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1988
Gilles Peskinea50d7392018-06-21 10:22:13 +02001989 /* Run several times, to ensure that every output byte will be
1990 * nonzero at least once with overwhelming probability
1991 * (2^(-8*number_of_runs)). */
1992 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02001993 {
Gilles Peskinea50d7392018-06-21 10:22:13 +02001994 memset( output, 0, bytes );
1995 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
1996
1997 /* Check that no more than bytes have been overwritten */
1998 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
1999
2000 for( i = 0; i < bytes; i++ )
2001 {
2002 if( output[i] != 0 )
2003 ++changed[i];
2004 }
Gilles Peskine05d69892018-06-19 22:00:52 +02002005 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02002006
2007 /* Check that every byte was changed to nonzero at least once. This
2008 * validates that psa_generate_random is overwriting every byte of
2009 * the output buffer. */
2010 for( i = 0; i < bytes; i++ )
2011 {
2012 TEST_ASSERT( changed[i] != 0 );
2013 }
Gilles Peskine05d69892018-06-19 22:00:52 +02002014
2015exit:
2016 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02002017 mbedtls_free( output );
2018 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02002019}
2020/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02002021
2022/* BEGIN_CASE */
2023void generate_key( int type_arg,
2024 int bits_arg,
2025 int usage_arg,
2026 int alg_arg,
2027 int expected_status_arg )
2028{
2029 int slot = 1;
2030 psa_key_type_t type = type_arg;
2031 psa_key_usage_t usage = usage_arg;
2032 size_t bits = bits_arg;
2033 psa_algorithm_t alg = alg_arg;
2034 psa_status_t expected_status = expected_status_arg;
2035 psa_key_type_t got_type;
2036 size_t got_bits;
2037 unsigned char exported[616] = {0}; /* enough for a 1024-bit RSA key */
2038 size_t exported_length;
2039 psa_status_t expected_export_status =
2040 usage & PSA_KEY_USAGE_EXPORT ? PSA_SUCCESS : PSA_ERROR_NOT_PERMITTED;
2041 psa_status_t expected_info_status =
2042 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
2043 psa_key_policy_t policy;
2044
2045 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2046
2047 psa_key_policy_init( &policy );
2048 psa_key_policy_set_usage( &policy, usage, alg );
2049 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2050
2051 /* Generate a key */
2052 TEST_ASSERT( psa_generate_key( slot, type, bits,
2053 NULL, 0 ) == expected_status );
2054
2055 /* Test the key information */
2056 TEST_ASSERT( psa_get_key_information( slot,
2057 &got_type,
2058 &got_bits ) == expected_info_status );
2059 if( expected_info_status != PSA_SUCCESS )
2060 goto exit;
2061 TEST_ASSERT( got_type == type );
2062 TEST_ASSERT( got_bits == bits );
2063
2064 /* Export the key */
2065 TEST_ASSERT( psa_export_key( slot,
2066 exported, sizeof( exported ),
2067 &exported_length ) == expected_export_status );
2068 if( expected_export_status == PSA_SUCCESS )
2069 {
Gilles Peskine48c0ea12018-06-21 14:15:31 +02002070 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02002071 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
2072#if defined(MBEDTLS_DES_C)
2073 if( type == PSA_KEY_TYPE_DES )
2074 {
2075 /* Check the parity bits. */
2076 unsigned i;
2077 for( i = 0; i < bits / 8; i++ )
2078 {
2079 unsigned bit_count = 0;
2080 unsigned m;
2081 for( m = 1; m <= 0x100; m <<= 1 )
2082 {
2083 if( exported[i] & m )
2084 ++bit_count;
2085 }
2086 TEST_ASSERT( bit_count % 2 != 0 );
2087 }
2088 }
2089#endif
2090#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
2091 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
2092 {
2093 /* Sanity check: does this look like the beginning of a PKCS#8
2094 * RSA key pair? Assumes bits is a multiple of 8. */
2095 size_t n_bytes = bits / 8 + 1;
2096 size_t n_encoded_bytes;
2097 unsigned char *n_end;
2098 TEST_ASSERT( exported_length >= 7 + ( n_bytes + 3 ) * 9 / 2 );
2099 TEST_ASSERT( exported[0] == 0x30 );
2100 TEST_ASSERT( exported[1] == 0x82 ); // assumes >=416-bit key
2101 TEST_ASSERT( exported[4] == 0x02 );
2102 TEST_ASSERT( exported[5] == 0x01 );
2103 TEST_ASSERT( exported[6] == 0x00 );
2104 TEST_ASSERT( exported[7] == 0x02 );
2105 n_encoded_bytes = exported[8];
2106 n_end = exported + 9 + n_encoded_bytes;
2107 if( n_encoded_bytes & 0x80 )
2108 {
2109 n_encoded_bytes = ( n_encoded_bytes & 0x7f ) << 7;
2110 n_encoded_bytes |= exported[9] & 0x7f;
2111 n_end += 1;
2112 }
2113 /* The encoding of n should start with a 0 byte since it should
2114 * have its high bit set. However Mbed TLS is not compliant and
2115 * generates an invalid, but widely tolerated, encoding of
2116 * positive INTEGERs with a bit size that is a multiple of 8
2117 * with no leading 0 byte. Accept this here. */
2118 TEST_ASSERT( n_bytes == n_encoded_bytes ||
2119 n_bytes == n_encoded_bytes + 1 );
2120 if( n_bytes == n_encoded_bytes )
2121 TEST_ASSERT( exported[n_encoded_bytes <= 127 ? 9 : 10] == 0x00 );
2122 /* Sanity check: e must be 3 */
2123 TEST_ASSERT( n_end[0] == 0x02 );
2124 TEST_ASSERT( n_end[1] == 0x03 );
2125 TEST_ASSERT( n_end[2] == 0x01 );
2126 TEST_ASSERT( n_end[3] == 0x00 );
2127 TEST_ASSERT( n_end[4] == 0x01 );
2128 TEST_ASSERT( n_end[5] == 0x02 );
2129 }
2130#endif /* MBEDTLS_RSA_C */
2131#if defined(MBEDTLS_ECP_C)
2132 if( PSA_KEY_TYPE_IS_ECC( type ) )
2133 {
2134 /* Sanity check: does this look like the beginning of a PKCS#8
2135 * elliptic curve key pair? */
2136 TEST_ASSERT( exported_length >= bits * 3 / 8 + 10 );
2137 TEST_ASSERT( exported[0] == 0x30 );
2138 }
2139#endif /* MBEDTLS_ECP_C */
2140 }
2141
Gilles Peskine818ca122018-06-20 18:16:48 +02002142 /* Do something with the key according to its type and permitted usage. */
2143 if( PSA_ALG_IS_MAC( alg ) )
2144 exercise_mac_key( slot, usage, alg );
2145 else if( PSA_ALG_IS_CIPHER( alg ) )
2146 exercise_cipher_key( slot, usage, alg );
2147 else if( PSA_ALG_IS_AEAD( alg ) )
2148 exercise_aead_key( slot, usage, alg );
2149 else if( PSA_ALG_IS_SIGN( alg ) )
2150 exercise_signature_key( slot, usage, alg );
2151 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
2152 exercise_asymmetric_encryption_key( slot, usage, alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02002153
2154exit:
2155 psa_destroy_key( slot );
2156 mbedtls_psa_crypto_free( );
2157}
2158/* END_CASE */