blob: 2d279fc384bfbda1523288e3825e408e05eef4ae [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
11/** Test if a buffer is not all-bits zero.
12 *
13 * \param buffer Pointer to the beginning of the buffer.
14 * \param size Size of the buffer in bytes.
15 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020016 * \return 1 if the buffer is all-bits-zero.
17 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020018 */
Gilles Peskine3f669c32018-06-21 09:21:51 +020019static int mem_is_zero( void *buffer, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020020{
21 size_t i;
22 for( i = 0; i < size; i++ )
23 {
24 if( ( (unsigned char *) buffer )[i] != 0 )
Gilles Peskine3f669c32018-06-21 09:21:51 +020025 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020026 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020027 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028}
Gilles Peskine818ca122018-06-20 18:16:48 +020029
Gilles Peskine48c0ea12018-06-21 14:15:31 +020030static int key_type_is_raw_bytes( psa_key_type_t type )
31{
32 psa_key_type_t category = type & PSA_KEY_TYPE_CATEGORY_MASK;
33 return( category == PSA_KEY_TYPE_RAW_DATA ||
34 category == PSA_KEY_TYPE_CATEGORY_SYMMETRIC );
35}
36
Gilles Peskine818ca122018-06-20 18:16:48 +020037static int exercise_mac_key( psa_key_slot_t key,
38 psa_key_usage_t usage,
39 psa_algorithm_t alg )
40{
41 psa_mac_operation_t operation;
42 const unsigned char input[] = "foo";
43 unsigned char mac[64] = {0};
44 size_t mac_length = sizeof( mac );
45
46 if( usage & PSA_KEY_USAGE_SIGN )
47 {
48 TEST_ASSERT( psa_mac_start( &operation, key, alg ) == PSA_SUCCESS );
49 TEST_ASSERT( psa_mac_update( &operation,
50 input, sizeof( input ) ) == PSA_SUCCESS );
51 TEST_ASSERT( psa_mac_finish( &operation,
52 mac, sizeof( input ),
53 &mac_length ) == PSA_SUCCESS );
54 }
55
56 if( usage & PSA_KEY_USAGE_VERIFY )
57 {
58 psa_status_t verify_status =
59 ( usage & PSA_KEY_USAGE_SIGN ?
60 PSA_SUCCESS :
61 PSA_ERROR_INVALID_SIGNATURE );
62 TEST_ASSERT( psa_mac_start( &operation, key, alg ) == PSA_SUCCESS );
63 TEST_ASSERT( psa_mac_update( &operation,
64 input, sizeof( input ) ) == PSA_SUCCESS );
65 TEST_ASSERT( psa_mac_verify( &operation, mac, mac_length ) == verify_status );
66 }
67
68 return( 1 );
69
70exit:
71 psa_mac_abort( &operation );
72 return( 0 );
73}
74
75static int exercise_cipher_key( psa_key_slot_t key,
76 psa_key_usage_t usage,
77 psa_algorithm_t alg )
78{
79 psa_cipher_operation_t operation;
80 unsigned char iv[16] = {0};
81 size_t iv_length = sizeof( iv );
82 const unsigned char plaintext[16] = "Hello, world...";
83 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
84 size_t ciphertext_length = sizeof( ciphertext );
85 unsigned char decrypted[sizeof( ciphertext )];
86 size_t part_length;
87
88 if( usage & PSA_KEY_USAGE_ENCRYPT )
89 {
90 TEST_ASSERT( psa_encrypt_setup( &operation, key, alg ) == PSA_SUCCESS );
91 TEST_ASSERT( psa_encrypt_generate_iv( &operation,
92 iv, sizeof( iv ),
93 &iv_length ) == PSA_SUCCESS );
94 TEST_ASSERT( psa_cipher_update( &operation,
95 plaintext, sizeof( plaintext ),
96 ciphertext, sizeof( ciphertext ),
97 &ciphertext_length ) == PSA_SUCCESS );
98 TEST_ASSERT( psa_cipher_finish( &operation,
99 ciphertext + ciphertext_length,
100 sizeof( ciphertext ) - ciphertext_length,
101 &part_length ) == PSA_SUCCESS );
102 ciphertext_length += part_length;
103 }
104
105 if( usage & PSA_KEY_USAGE_DECRYPT )
106 {
107 psa_status_t status;
108 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
109 {
110 psa_key_type_t type;
111 size_t bits;
112 TEST_ASSERT( psa_get_key_information( key, &type, &bits ) );
113 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
114 }
115 TEST_ASSERT( psa_decrypt_setup( &operation, key, alg ) == PSA_SUCCESS );
116 TEST_ASSERT( psa_encrypt_set_iv( &operation,
117 iv, iv_length ) == PSA_SUCCESS );
118 TEST_ASSERT( psa_cipher_update( &operation,
119 ciphertext, ciphertext_length,
120 decrypted, sizeof( decrypted ),
121 &part_length ) == PSA_SUCCESS );
122 status = psa_cipher_finish( &operation,
123 decrypted + part_length,
124 sizeof( decrypted ) - part_length,
125 &part_length );
126 /* For a stream cipher, all inputs are valid. For a block cipher,
127 * if the input is some aribtrary data rather than an actual
128 ciphertext, a padding error is likely. */
129 if( ( usage & PSA_KEY_USAGE_DECRYPT ) ||
130 PSA_BLOCK_CIPHER_BLOCK_SIZE( alg ) == 1 )
131 TEST_ASSERT( status == PSA_SUCCESS );
132 else
133 TEST_ASSERT( status == PSA_SUCCESS ||
134 status == PSA_ERROR_INVALID_PADDING );
135 }
136
137 return( 1 );
138
139exit:
140 psa_cipher_abort( &operation );
141 return( 0 );
142}
143
144static int exercise_aead_key( psa_key_slot_t key,
145 psa_key_usage_t usage,
146 psa_algorithm_t alg )
147{
148 unsigned char nonce[16] = {0};
149 size_t nonce_length = sizeof( nonce );
150 unsigned char plaintext[16] = "Hello, world...";
151 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
152 size_t ciphertext_length = sizeof( ciphertext );
153 size_t plaintext_length = sizeof( ciphertext );
154
155 if( usage & PSA_KEY_USAGE_ENCRYPT )
156 {
157 TEST_ASSERT( psa_aead_encrypt( key, alg,
158 nonce, nonce_length,
159 NULL, 0,
160 plaintext, sizeof( plaintext ),
161 ciphertext, sizeof( ciphertext ),
162 &ciphertext_length ) == PSA_SUCCESS );
163 }
164
165 if( usage & PSA_KEY_USAGE_DECRYPT )
166 {
167 psa_status_t verify_status =
168 ( usage & PSA_KEY_USAGE_ENCRYPT ?
169 PSA_SUCCESS :
170 PSA_ERROR_INVALID_SIGNATURE );
171 TEST_ASSERT( psa_aead_decrypt( key, alg,
172 nonce, nonce_length,
173 NULL, 0,
174 ciphertext, ciphertext_length,
175 plaintext, sizeof( plaintext ),
176 &plaintext_length ) == verify_status );
177 }
178
179 return( 1 );
180
181exit:
182 return( 0 );
183}
184
185static int exercise_signature_key( psa_key_slot_t key,
186 psa_key_usage_t usage,
187 psa_algorithm_t alg )
188{
189 unsigned char payload[16] = {0};
190 size_t payload_length = sizeof( payload );
191 unsigned char signature[256] = {0};
192 size_t signature_length = sizeof( signature );
193
194 if( usage & PSA_KEY_USAGE_SIGN )
195 {
196 TEST_ASSERT( psa_asymmetric_sign( key, alg,
197 payload, payload_length,
198 NULL, 0,
199 signature, sizeof( signature ),
200 &signature_length ) == PSA_SUCCESS );
201 }
202
203 if( usage & PSA_KEY_USAGE_VERIFY )
204 {
205 psa_status_t verify_status =
206 ( usage & PSA_KEY_USAGE_SIGN ?
207 PSA_SUCCESS :
208 PSA_ERROR_INVALID_SIGNATURE );
209 TEST_ASSERT( psa_asymmetric_verify( key, alg,
210 payload, payload_length,
211 NULL, 0,
212 signature, signature_length ) ==
213 verify_status );
214 }
215
216 return( 1 );
217
218exit:
219 return( 0 );
220}
221
222static int exercise_asymmetric_encryption_key( psa_key_slot_t key,
223 psa_key_usage_t usage,
224 psa_algorithm_t alg )
225{
226 unsigned char plaintext[256] = "Hello, world...";
227 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
228 size_t ciphertext_length = sizeof( ciphertext );
229 size_t plaintext_length = 16;
230
231 if( usage & PSA_KEY_USAGE_ENCRYPT )
232 {
233 TEST_ASSERT(
234 psa_asymmetric_encrypt( key, alg,
235 plaintext, plaintext_length,
236 NULL, 0,
237 ciphertext, sizeof( ciphertext ),
238 &ciphertext_length ) == PSA_SUCCESS );
239 }
240
241 if( usage & PSA_KEY_USAGE_DECRYPT )
242 {
243 psa_status_t status =
244 psa_asymmetric_decrypt( key, alg,
245 ciphertext, ciphertext_length,
246 NULL, 0,
247 plaintext, sizeof( plaintext ),
248 &plaintext_length );
249 TEST_ASSERT( status == PSA_SUCCESS ||
250 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
251 ( status == PSA_ERROR_INVALID_ARGUMENT ||
252 status == PSA_ERROR_INVALID_PADDING ) ) );
253 }
254
255 return( 1 );
256
257exit:
258 return( 0 );
259}
Gilles Peskinee59236f2018-01-27 23:32:46 +0100260/* END_HEADER */
261
262/* BEGIN_DEPENDENCIES
263 * depends_on:MBEDTLS_PSA_CRYPTO_C
264 * END_DEPENDENCIES
265 */
266
267/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200268void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100269{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100270 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100271 int i;
272 for( i = 0; i <= 1; i++ )
273 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100274 status = psa_crypto_init( );
275 TEST_ASSERT( status == PSA_SUCCESS );
276 status = psa_crypto_init( );
277 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100278 mbedtls_psa_crypto_free( );
279 }
280}
281/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100282
283/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200284void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100285{
286 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200287 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100288 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100289
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100290 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300291 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100292 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
293
Gilles Peskine4abf7412018-06-18 16:35:34 +0200294 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200295 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100296 if( status == PSA_SUCCESS )
297 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
298
299exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100300 mbedtls_psa_crypto_free( );
301}
302/* END_CASE */
303
304/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300305void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300306 int type_arg,
307 int alg_arg,
308 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100309 int expected_bits,
310 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200311 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100312 int canonical_input )
313{
314 int slot = 1;
315 int slot2 = slot + 1;
316 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200317 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200318 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100319 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100320 unsigned char *exported = NULL;
321 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100322 size_t export_size;
323 size_t exported_length;
324 size_t reexported_length;
325 psa_key_type_t got_type;
326 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200327 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100328
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100329 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300330 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
331 export_size = (ssize_t) data->len + export_size_delta;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100332 exported = mbedtls_calloc( 1, export_size );
333 TEST_ASSERT( exported != NULL );
334 if( ! canonical_input )
335 {
336 reexported = mbedtls_calloc( 1, export_size );
337 TEST_ASSERT( reexported != NULL );
338 }
339 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
340
mohammad1603a97cb8c2018-03-28 03:46:26 -0700341 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200342 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700343 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
344
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100345 /* Import the key */
346 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200347 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100348
349 /* Test the key information */
350 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200351 &got_type,
352 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100353 TEST_ASSERT( got_type == type );
354 TEST_ASSERT( got_bits == (size_t) expected_bits );
355
356 /* Export the key */
357 status = psa_export_key( slot,
358 exported, export_size,
359 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200360 TEST_ASSERT( status == expected_export_status );
Gilles Peskine3f669c32018-06-21 09:21:51 +0200361 TEST_ASSERT( mem_is_zero( exported + exported_length,
362 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100363 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200364 {
365 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100366 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200367 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100368
369 if( canonical_input )
370 {
Gilles Peskine4abf7412018-06-18 16:35:34 +0200371 TEST_ASSERT( exported_length == data->len );
372 TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100373 }
374 else
375 {
mohammad1603a97cb8c2018-03-28 03:46:26 -0700376 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
377
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100378 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200379 exported,
380 export_size ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100381 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200382 reexported,
383 export_size,
384 &reexported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100385 TEST_ASSERT( reexported_length == exported_length );
386 TEST_ASSERT( memcmp( reexported, exported,
387 exported_length ) == 0 );
388 }
389
390destroy:
391 /* Destroy the key */
392 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
393 TEST_ASSERT( psa_get_key_information(
394 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
395
396exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300397 mbedtls_free( exported );
398 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100399 mbedtls_psa_crypto_free( );
400}
401/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100402
Moran Pekerf709f4a2018-06-06 17:26:04 +0300403/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300404void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200405 int type_arg,
406 int alg_arg,
407 int expected_bits,
408 int public_key_expected_length,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200409 int expected_export_status_arg )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300410{
411 int slot = 1;
412 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200413 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200414 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300415 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300416 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300417 size_t export_size;
418 size_t exported_length;
419 psa_key_type_t got_type;
420 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200421 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300422
Moran Pekerf709f4a2018-06-06 17:26:04 +0300423 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300424 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
425 export_size = (ssize_t) data->len;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300426 exported = mbedtls_calloc( 1, export_size );
427 TEST_ASSERT( exported != NULL );
428
429 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
430
431 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200432 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300433 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
434
435 /* Import the key */
436 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200437 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300438
439 /* Test the key information */
440 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200441 &got_type,
442 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300443 TEST_ASSERT( got_type == type );
444 TEST_ASSERT( got_bits == (size_t) expected_bits );
445
446 /* Export the key */
447 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200448 exported, export_size,
449 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200450 TEST_ASSERT( status == expected_export_status );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300451 if( status != PSA_SUCCESS )
452 goto destroy;
453
454 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
455
456destroy:
457 /* Destroy the key */
458 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
459 TEST_ASSERT( psa_get_key_information(
460 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
461
462exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300463 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300464 mbedtls_psa_crypto_free( );
465}
466/* END_CASE */
467
Gilles Peskine20035e32018-02-03 22:44:14 +0100468/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +0200469void key_policy( int usage_arg, int alg_arg )
470{
471 int key_slot = 1;
472 psa_algorithm_t alg = alg_arg;
473 psa_key_usage_t usage = usage_arg;
474 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
475 unsigned char key[32] = {0};
476 psa_key_policy_t policy_set;
477 psa_key_policy_t policy_get;
478
479 memset( key, 0x2a, sizeof( key ) );
480
481 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
482
483 psa_key_policy_init( &policy_set );
484 psa_key_policy_init( &policy_get );
485
486 psa_key_policy_set_usage( &policy_set, usage, alg );
487
488 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
489 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
490 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
491
492 TEST_ASSERT( psa_import_key( key_slot, key_type,
493 key, sizeof( key ) ) == PSA_SUCCESS );
494
495 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
496
497 TEST_ASSERT( policy_get.usage == policy_set.usage );
498 TEST_ASSERT( policy_get.alg == policy_set.alg );
499
500exit:
501 psa_destroy_key( key_slot );
502 mbedtls_psa_crypto_free( );
503}
504/* END_CASE */
505
506/* BEGIN_CASE */
507void key_policy_fail( int usage_arg, int alg_arg, int expected_status,
508 data_t *keypair )
509{
510 int key_slot = 1;
511 psa_algorithm_t alg = alg_arg;
512 psa_key_usage_t usage = usage_arg;
513 size_t signature_length = 0;
514 psa_key_policy_t policy;
515 int actual_status = PSA_SUCCESS;
516
517 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
518
519 psa_key_policy_init( &policy );
520 psa_key_policy_set_usage( &policy, usage, alg );
521 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
522
523 if( usage & PSA_KEY_USAGE_EXPORT )
524 {
525 TEST_ASSERT( keypair != NULL );
526 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) );
527 TEST_ASSERT( psa_import_key( key_slot,
528 PSA_KEY_TYPE_RSA_KEYPAIR,
529 keypair->x,
530 keypair->len ) == PSA_SUCCESS );
531 actual_status = psa_asymmetric_sign( key_slot, alg,
532 NULL, 0,
533 NULL, 0,
534 NULL, 0, &signature_length );
535 }
536
537 if( usage & PSA_KEY_USAGE_SIGN )
538 {
539 TEST_ASSERT( keypair != NULL );
540 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) );
541 TEST_ASSERT( psa_import_key( key_slot,
542 PSA_KEY_TYPE_RSA_KEYPAIR,
543 keypair->x,
544 keypair->len ) == PSA_SUCCESS );
545 actual_status = psa_export_key( key_slot, NULL, 0, NULL );
546 }
547
548 TEST_ASSERT( actual_status == expected_status );
549
550exit:
551 psa_destroy_key( key_slot );
552 mbedtls_psa_crypto_free( );
553}
554/* END_CASE */
555
556/* BEGIN_CASE */
557void key_lifetime( int lifetime_arg )
558{
559 int key_slot = 1;
560 psa_key_type_t key_type = PSA_ALG_CBC_BASE;
561 unsigned char key[32] = {0};
562 psa_key_lifetime_t lifetime_set = lifetime_arg;
563 psa_key_lifetime_t lifetime_get;
564
565 memset( key, 0x2a, sizeof( key ) );
566
567 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
568
569 TEST_ASSERT( psa_set_key_lifetime( key_slot,
570 lifetime_set ) == PSA_SUCCESS );
571
572 TEST_ASSERT( psa_import_key( key_slot, key_type,
573 key, sizeof( key ) ) == PSA_SUCCESS );
574
575 TEST_ASSERT( psa_get_key_lifetime( key_slot,
576 &lifetime_get ) == PSA_SUCCESS );
577
578 TEST_ASSERT( lifetime_get == lifetime_set );
579
580exit:
581 psa_destroy_key( key_slot );
582 mbedtls_psa_crypto_free( );
583}
584/* END_CASE */
585
586/* BEGIN_CASE */
587void key_lifetime_set_fail( int key_slot_arg,
588 int lifetime_arg,
589 int expected_status_arg )
590{
591 psa_key_slot_t key_slot = key_slot_arg;
592 psa_key_lifetime_t lifetime_set = lifetime_arg;
593 psa_status_t actual_status;
594 psa_status_t expected_status = expected_status_arg;
595
596 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
597
598 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
599
600 if( actual_status == PSA_SUCCESS )
601 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
602
603 TEST_ASSERT( expected_status == actual_status );
604
605exit:
606 psa_destroy_key( key_slot );
607 mbedtls_psa_crypto_free( );
608}
609/* END_CASE */
610
611/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200612void hash_setup( int alg_arg,
613 int expected_status_arg )
614{
615 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200616 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200617 psa_hash_operation_t operation;
618 psa_status_t status;
619
620 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
621
622 status = psa_hash_start( &operation, alg );
623 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200624 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200625
626exit:
627 mbedtls_psa_crypto_free( );
628}
629/* END_CASE */
630
631/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300632void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100633{
634 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +0200635 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100636 size_t actual_hash_length;
637 psa_hash_operation_t operation;
638
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100639 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300640 TEST_ASSERT( expected_hash != NULL );
641 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
642 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100643
644 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
645
646 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
647 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200648 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100649 TEST_ASSERT( psa_hash_finish( &operation,
650 actual_hash, sizeof( actual_hash ),
651 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200652 TEST_ASSERT( actual_hash_length == expected_hash->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300653 TEST_ASSERT( memcmp( expected_hash->x, actual_hash,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200654 expected_hash->len ) == 0 );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100655
656exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100657 mbedtls_psa_crypto_free( );
658}
659/* END_CASE */
660
661/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300662void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100663{
664 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100665 psa_hash_operation_t operation;
666
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100667 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300668 TEST_ASSERT( expected_hash != NULL );
669 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
670 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100671
672 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
673
674 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
675 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200676 input->x,
677 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100678 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300679 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200680 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100681
682exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100683 mbedtls_psa_crypto_free( );
684}
685/* END_CASE */
686
687/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200688void mac_setup( int key_type_arg,
689 data_t *key,
690 int alg_arg,
691 int expected_status_arg )
692{
693 int key_slot = 1;
694 psa_key_type_t key_type = key_type_arg;
695 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200696 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200697 psa_mac_operation_t operation;
698 psa_key_policy_t policy;
699 psa_status_t status;
700
701 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
702
703 psa_key_policy_init( &policy );
704 psa_key_policy_set_usage( &policy,
705 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
706 alg );
707 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
708
709 TEST_ASSERT( psa_import_key( key_slot, key_type,
710 key->x, key->len ) == PSA_SUCCESS );
711
712 status = psa_mac_start( &operation, key_slot, alg );
713 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200714 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200715
716exit:
717 psa_destroy_key( key_slot );
718 mbedtls_psa_crypto_free( );
719}
720/* END_CASE */
721
722/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +0200723void mac_verify( int key_type_arg,
724 data_t *key,
725 int alg_arg,
726 data_t *input,
727 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +0100728{
729 int key_slot = 1;
730 psa_key_type_t key_type = key_type_arg;
731 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100732 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -0700733 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100734
Gilles Peskine8c9def32018-02-08 10:02:12 +0100735 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100736 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100737 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300738 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300739 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
740 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100741
742 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
743
mohammad16036df908f2018-04-02 08:34:15 -0700744 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200745 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -0700746 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
747
Gilles Peskine8c9def32018-02-08 10:02:12 +0100748 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200749 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +0200750
Gilles Peskine8c9def32018-02-08 10:02:12 +0100751 TEST_ASSERT( psa_mac_start( &operation, key_slot, alg ) == PSA_SUCCESS );
752 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
753 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200754 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100755 TEST_ASSERT( psa_mac_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300756 expected_mac->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200757 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100758
759exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +0100760 psa_destroy_key( key_slot );
761 mbedtls_psa_crypto_free( );
762}
763/* END_CASE */
764
765/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200766void cipher_setup( int key_type_arg,
767 data_t *key,
768 int alg_arg,
769 int expected_status_arg )
770{
771 int key_slot = 1;
772 psa_key_type_t key_type = key_type_arg;
773 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200774 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200775 psa_cipher_operation_t operation;
776 psa_key_policy_t policy;
777 psa_status_t status;
778
779 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
780
781 psa_key_policy_init( &policy );
782 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
783 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
784
785 TEST_ASSERT( psa_import_key( key_slot, key_type,
786 key->x, key->len ) == PSA_SUCCESS );
787
788 status = psa_encrypt_setup( &operation, key_slot, alg );
789 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200790 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200791
792exit:
793 psa_destroy_key( key_slot );
794 mbedtls_psa_crypto_free( );
795}
796/* END_CASE */
797
798/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +0200799void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300800 data_t *key,
801 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200802 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +0200803{
804 int key_slot = 1;
805 psa_status_t status;
806 psa_key_type_t key_type = key_type_arg;
807 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200808 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200809 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200810 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300811 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200812 size_t output_buffer_size = 0;
813 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200814 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200815 psa_cipher_operation_t operation;
816
Gilles Peskine50e586b2018-06-08 14:28:46 +0200817 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200818 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200819 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300820 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
821 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
822 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200823
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200824 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
825 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200826
827 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
828
829 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200830 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200831
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200832 TEST_ASSERT( psa_encrypt_setup( &operation,
833 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200834
835 TEST_ASSERT( psa_encrypt_set_iv( &operation,
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200836 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200837 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200838 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300839 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200840
Gilles Peskine4abf7412018-06-18 16:35:34 +0200841 TEST_ASSERT( psa_cipher_update( &operation,
842 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200843 output, output_buffer_size,
844 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200845 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200846 status = psa_cipher_finish( &operation,
847 output + function_output_length,
848 output_buffer_size,
849 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200850 total_output_length += function_output_length;
851
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200852 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200853 if( expected_status == PSA_SUCCESS )
854 {
855 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200856 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300857 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200858 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200859 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200860
Gilles Peskine50e586b2018-06-08 14:28:46 +0200861exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300862 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200863 psa_destroy_key( key_slot );
864 mbedtls_psa_crypto_free( );
865}
866/* END_CASE */
867
868/* BEGIN_CASE */
869void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300870 data_t *key,
871 data_t *input,
872 int first_part_size,
873 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +0200874{
875 int key_slot = 1;
876 psa_key_type_t key_type = key_type_arg;
877 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200878 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200879 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300880 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200881 size_t output_buffer_size = 0;
882 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200883 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200884 psa_cipher_operation_t operation;
885
Gilles Peskine50e586b2018-06-08 14:28:46 +0200886 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200887 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200888 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300889 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
890 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
891 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200892
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200893 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
894 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200895
896 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
897
898 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200899 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200900
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200901 TEST_ASSERT( psa_encrypt_setup( &operation,
902 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200903
904 TEST_ASSERT( psa_encrypt_set_iv( &operation,
905 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200906 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200907 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300908 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200909
Gilles Peskine4abf7412018-06-18 16:35:34 +0200910 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300911 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200912 output, output_buffer_size,
913 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200914 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200915 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300916 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200917 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200918 output, output_buffer_size,
919 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200920 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200921 TEST_ASSERT( psa_cipher_finish( &operation,
922 output + function_output_length,
923 output_buffer_size,
924 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200925 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200926 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
927
Gilles Peskine4abf7412018-06-18 16:35:34 +0200928 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300929 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200930 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200931
932exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300933 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200934 psa_destroy_key( key_slot );
935 mbedtls_psa_crypto_free( );
936}
937/* END_CASE */
938
939/* BEGIN_CASE */
940void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300941 data_t *key,
942 data_t *input,
943 int first_part_size,
944 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +0200945{
946 int key_slot = 1;
947
948 psa_key_type_t key_type = key_type_arg;
949 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200950 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200951 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300952 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200953 size_t output_buffer_size = 0;
954 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200955 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200956 psa_cipher_operation_t operation;
957
Gilles Peskine50e586b2018-06-08 14:28:46 +0200958 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200959 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200960 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300961 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
962 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
963 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200964
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200965 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
966 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200967
968 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
969
970 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200971 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200972
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200973 TEST_ASSERT( psa_decrypt_setup( &operation,
974 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200975
976 TEST_ASSERT( psa_encrypt_set_iv( &operation,
977 iv, sizeof( iv ) ) == PSA_SUCCESS );
978
Gilles Peskine4abf7412018-06-18 16:35:34 +0200979 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200980 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300981 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200982
Gilles Peskine4abf7412018-06-18 16:35:34 +0200983 TEST_ASSERT( (unsigned int) first_part_size < input->len );
984 TEST_ASSERT( psa_cipher_update( &operation,
985 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200986 output, output_buffer_size,
987 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200988 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200989 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300990 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200991 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200992 output, output_buffer_size,
993 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200994 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200995 TEST_ASSERT( psa_cipher_finish( &operation,
996 output + function_output_length,
997 output_buffer_size,
998 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200999 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001000 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1001
Gilles Peskine4abf7412018-06-18 16:35:34 +02001002 TEST_ASSERT( total_output_length == expected_output->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001003 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001004 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001005
1006exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001007 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001008 psa_destroy_key( key_slot );
1009 mbedtls_psa_crypto_free( );
1010}
1011/* END_CASE */
1012
Gilles Peskine50e586b2018-06-08 14:28:46 +02001013/* BEGIN_CASE */
1014void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001015 data_t *key,
1016 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001017 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001018{
1019 int key_slot = 1;
1020 psa_status_t status;
1021 psa_key_type_t key_type = key_type_arg;
1022 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001023 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001024 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001025 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001026 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001027 size_t output_buffer_size = 0;
1028 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001029 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001030 psa_cipher_operation_t operation;
1031
Gilles Peskine50e586b2018-06-08 14:28:46 +02001032 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001033 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001034 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001035 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1036 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1037 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001038
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001039 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1040 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001041
1042 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1043
1044 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001045 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001046
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001047 TEST_ASSERT( psa_decrypt_setup( &operation,
1048 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001049
1050 TEST_ASSERT( psa_encrypt_set_iv( &operation,
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001051 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001052
Gilles Peskine4abf7412018-06-18 16:35:34 +02001053 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001054 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001055 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001056
Gilles Peskine4abf7412018-06-18 16:35:34 +02001057 TEST_ASSERT( psa_cipher_update( &operation,
1058 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001059 output, output_buffer_size,
1060 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001061 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001062 status = psa_cipher_finish( &operation,
1063 output + function_output_length,
1064 output_buffer_size,
1065 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001066 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001067 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001068
1069 if( expected_status == PSA_SUCCESS )
1070 {
1071 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001072 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001073 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001074 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001075 }
1076
Gilles Peskine50e586b2018-06-08 14:28:46 +02001077exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001078 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001079 psa_destroy_key( key_slot );
1080 mbedtls_psa_crypto_free( );
1081}
1082/* END_CASE */
1083
Gilles Peskine50e586b2018-06-08 14:28:46 +02001084/* BEGIN_CASE */
1085void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001086 data_t *key,
1087 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02001088{
1089 int key_slot = 1;
1090 psa_key_type_t key_type = key_type_arg;
1091 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07001092 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02001093 size_t iv_size = 16;
1094 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001095 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001096 size_t output1_size = 0;
1097 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001098 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001099 size_t output2_size = 0;
1100 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001101 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001102 psa_cipher_operation_t operation1;
1103 psa_cipher_operation_t operation2;
1104
mohammad1603d7d7ba52018-03-12 18:51:53 +02001105 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001106 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001107 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1108 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001109
mohammad1603d7d7ba52018-03-12 18:51:53 +02001110 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1111
1112 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001113 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001114
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001115 TEST_ASSERT( psa_encrypt_setup( &operation1,
1116 key_slot, alg ) == PSA_SUCCESS );
1117 TEST_ASSERT( psa_decrypt_setup( &operation2,
1118 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001119
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001120 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
1121 iv, iv_size,
1122 &iv_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001123 output1_size = input->len + operation1.block_size;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001124 output1 = mbedtls_calloc( 1, output1_size );
1125 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001126
Gilles Peskine4abf7412018-06-18 16:35:34 +02001127 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001128 output1, output1_size,
1129 &output1_length ) == PSA_SUCCESS );
1130 TEST_ASSERT( psa_cipher_finish( &operation1,
1131 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001132 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001133
Gilles Peskine048b7f02018-06-08 14:20:49 +02001134 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001135
1136 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1137
1138 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001139 output2 = mbedtls_calloc( 1, output2_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001140 TEST_ASSERT( output2 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001141
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001142 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
1143 iv, iv_length ) == PSA_SUCCESS );
1144 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
1145 output2, output2_size,
1146 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001147 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001148 TEST_ASSERT( psa_cipher_finish( &operation2,
1149 output2 + output2_length,
1150 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001151 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001152
Gilles Peskine048b7f02018-06-08 14:20:49 +02001153 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001154
Moran Pekerded84402018-06-06 16:36:50 +03001155 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1156
Gilles Peskine4abf7412018-06-18 16:35:34 +02001157 TEST_ASSERT( input->len == output2_length );
1158 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +03001159
1160exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001161 mbedtls_free( output1 );
1162 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03001163 psa_destroy_key( key_slot );
1164 mbedtls_psa_crypto_free( );
1165}
1166/* END_CASE */
1167
1168/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001169void cipher_verify_output_multipart( int alg_arg,
1170 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001171 data_t *key,
1172 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001173 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03001174{
1175 int key_slot = 1;
1176 psa_key_type_t key_type = key_type_arg;
1177 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03001178 unsigned char iv[16] = {0};
1179 size_t iv_size = 16;
1180 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001181 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001182 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001183 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001184 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001185 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001186 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001187 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001188 psa_cipher_operation_t operation1;
1189 psa_cipher_operation_t operation2;
1190
Moran Pekerded84402018-06-06 16:36:50 +03001191 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001192 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001193 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1194 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001195
Moran Pekerded84402018-06-06 16:36:50 +03001196 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1197
1198 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001199 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001200
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001201 TEST_ASSERT( psa_encrypt_setup( &operation1,
1202 key_slot, alg ) == PSA_SUCCESS );
1203 TEST_ASSERT( psa_decrypt_setup( &operation2,
1204 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001205
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001206 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
1207 iv, iv_size,
1208 &iv_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001209 output1_buffer_size = input->len + operation1.block_size;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001210 output1 = mbedtls_calloc( 1, output1_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001211 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001212
Gilles Peskine4abf7412018-06-18 16:35:34 +02001213 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001214
itayzafrir3e02b3b2018-06-12 17:06:52 +03001215 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001216 output1, output1_buffer_size,
1217 &function_output_length ) == PSA_SUCCESS );
1218 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001219
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001220 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001221 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001222 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001223 output1, output1_buffer_size,
1224 &function_output_length ) == PSA_SUCCESS );
1225 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001226
Gilles Peskine048b7f02018-06-08 14:20:49 +02001227 TEST_ASSERT( psa_cipher_finish( &operation1,
1228 output1 + output1_length,
1229 output1_buffer_size - output1_length,
1230 &function_output_length ) == PSA_SUCCESS );
1231 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001232
1233 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1234
Gilles Peskine048b7f02018-06-08 14:20:49 +02001235 output2_buffer_size = output1_length;
1236 output2 = mbedtls_calloc( 1, output2_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001237 TEST_ASSERT( output2 != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001238
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001239 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
1240 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001241
1242 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001243 output2, output2_buffer_size,
1244 &function_output_length ) == PSA_SUCCESS );
1245 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001246
Gilles Peskine048b7f02018-06-08 14:20:49 +02001247 TEST_ASSERT( psa_cipher_update( &operation2,
1248 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001249 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001250 output2, output2_buffer_size,
1251 &function_output_length ) == PSA_SUCCESS );
1252 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001253
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001254 TEST_ASSERT( psa_cipher_finish( &operation2,
1255 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001256 output2_buffer_size - output2_length,
1257 &function_output_length ) == PSA_SUCCESS );
1258 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001259
mohammad1603d7d7ba52018-03-12 18:51:53 +02001260 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1261
Gilles Peskine4abf7412018-06-18 16:35:34 +02001262 TEST_ASSERT( input->len == output2_length );
1263 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001264
1265exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001266 mbedtls_free( output1 );
1267 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001268 psa_destroy_key( key_slot );
1269 mbedtls_psa_crypto_free( );
1270}
1271/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02001272
Gilles Peskine20035e32018-02-03 22:44:14 +01001273/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001274void aead_encrypt_decrypt( int key_type_arg,
1275 data_t * key_data,
1276 int alg_arg,
1277 data_t * input_data,
1278 data_t * nonce,
1279 data_t * additional_data,
1280 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001281{
1282 int slot = 1;
1283 psa_key_type_t key_type = key_type_arg;
1284 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001285 unsigned char *output_data = NULL;
1286 size_t output_size = 0;
1287 size_t output_length = 0;
1288 unsigned char *output_data2 = NULL;
1289 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001290 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001291 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001292 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001293
Gilles Peskinea1cac842018-06-11 19:33:02 +02001294 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001295 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001296 TEST_ASSERT( nonce != NULL );
1297 TEST_ASSERT( additional_data != NULL );
1298 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1299 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1300 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1301 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1302
Gilles Peskine4abf7412018-06-18 16:35:34 +02001303 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001304 output_data = mbedtls_calloc( 1, output_size );
1305 TEST_ASSERT( output_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001306
1307 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1308
1309 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001310 psa_key_policy_set_usage( &policy,
1311 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
1312 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001313 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1314
1315 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001316 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001317
1318 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001319 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001320 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001321 additional_data->len,
1322 input_data->x, input_data->len,
1323 output_data, output_size,
1324 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001325
1326 if( PSA_SUCCESS == expected_result )
1327 {
1328 output_data2 = mbedtls_calloc( 1, output_length );
1329 TEST_ASSERT( output_data2 != NULL );
1330
1331 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001332 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001333 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001334 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001335 output_data, output_length,
1336 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001337 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02001338
itayzafrir3e02b3b2018-06-12 17:06:52 +03001339 TEST_ASSERT( memcmp( input_data->x, output_data2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001340 input_data->len ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001341 }
Gilles Peskine2d277862018-06-18 15:41:12 +02001342
Gilles Peskinea1cac842018-06-11 19:33:02 +02001343exit:
1344 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001345 mbedtls_free( output_data );
1346 mbedtls_free( output_data2 );
1347 mbedtls_psa_crypto_free( );
1348}
1349/* END_CASE */
1350
1351/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001352void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001353 int alg_arg, data_t * input_data,
1354 data_t * additional_data, data_t * nonce,
1355 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001356{
1357 int slot = 1;
1358 psa_key_type_t key_type = key_type_arg;
1359 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001360 unsigned char *output_data = NULL;
1361 size_t output_size = 0;
1362 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001363 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001364 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001365
Gilles Peskinea1cac842018-06-11 19:33:02 +02001366 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001367 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001368 TEST_ASSERT( additional_data != NULL );
1369 TEST_ASSERT( nonce != NULL );
1370 TEST_ASSERT( expected_result != NULL );
1371 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1372 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1373 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1374 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1375 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
1376
Gilles Peskine4abf7412018-06-18 16:35:34 +02001377 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001378 output_data = mbedtls_calloc( 1, output_size );
1379 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001380
Gilles Peskinea1cac842018-06-11 19:33:02 +02001381 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1382
1383 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001384 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001385 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1386
1387 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001388 key_data->x,
1389 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001390
1391 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001392 nonce->x, nonce->len,
1393 additional_data->x, additional_data->len,
1394 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001395 output_data, output_size,
1396 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001397
itayzafrir3e02b3b2018-06-12 17:06:52 +03001398 TEST_ASSERT( memcmp( output_data, expected_result->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001399 output_length ) == 0 );
1400
Gilles Peskinea1cac842018-06-11 19:33:02 +02001401exit:
1402 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001403 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001404 mbedtls_psa_crypto_free( );
1405}
1406/* END_CASE */
1407
1408/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001409void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001410 int alg_arg, data_t * input_data,
1411 data_t * additional_data, data_t * nonce,
1412 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001413{
1414 int slot = 1;
1415 psa_key_type_t key_type = key_type_arg;
1416 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001417 unsigned char *output_data = NULL;
1418 size_t output_size = 0;
1419 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001420 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001421 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001422 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001423
Gilles Peskinea1cac842018-06-11 19:33:02 +02001424 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001425 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001426 TEST_ASSERT( additional_data != NULL );
1427 TEST_ASSERT( nonce != NULL );
1428 TEST_ASSERT( expected_data != NULL );
1429 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1430 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1431 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1432 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1433 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
1434
Gilles Peskine4abf7412018-06-18 16:35:34 +02001435 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001436 output_data = mbedtls_calloc( 1, output_size );
1437 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001438
Gilles Peskinea1cac842018-06-11 19:33:02 +02001439 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1440
1441 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001442 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001443 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1444
1445 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001446 key_data->x,
1447 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001448
1449 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001450 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001451 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001452 additional_data->len,
1453 input_data->x, input_data->len,
1454 output_data, output_size,
1455 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001456
Gilles Peskine2d277862018-06-18 15:41:12 +02001457 if( expected_result == PSA_SUCCESS )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001458 {
itayzafrir3e02b3b2018-06-12 17:06:52 +03001459 TEST_ASSERT( memcmp( output_data, expected_data->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001460 output_length ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001461 }
1462
Gilles Peskinea1cac842018-06-11 19:33:02 +02001463exit:
1464 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001465 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001466 mbedtls_psa_crypto_free( );
1467}
1468/* END_CASE */
1469
1470/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001471void signature_size( int type_arg,
1472 int bits,
1473 int alg_arg,
1474 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001475{
1476 psa_key_type_t type = type_arg;
1477 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02001478 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01001479 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
1480exit:
1481 ;
1482}
1483/* END_CASE */
1484
1485/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001486void sign_deterministic( int key_type_arg, data_t *key_data,
1487 int alg_arg, data_t *input_data,
1488 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001489{
1490 int slot = 1;
1491 psa_key_type_t key_type = key_type_arg;
1492 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01001493 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01001494 unsigned char *signature = NULL;
1495 size_t signature_size;
1496 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02001497 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01001498
Gilles Peskine20035e32018-02-03 22:44:14 +01001499 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001500 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001501 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001502 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1503 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1504 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01001505
1506 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1507
mohammad1603a97cb8c2018-03-28 03:46:26 -07001508 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001509 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001510 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1511
Gilles Peskine20035e32018-02-03 22:44:14 +01001512 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001513 key_data->x,
1514 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01001515 TEST_ASSERT( psa_get_key_information( slot,
1516 NULL,
1517 &key_bits ) == PSA_SUCCESS );
1518
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001519 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
1520 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01001521 TEST_ASSERT( signature_size != 0 );
1522 signature = mbedtls_calloc( 1, signature_size );
1523 TEST_ASSERT( signature != NULL );
1524
1525 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001526 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01001527 NULL, 0,
1528 signature, signature_size,
1529 &signature_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001530 TEST_ASSERT( signature_length == output_data->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001531 TEST_ASSERT( memcmp( signature, output_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001532 output_data->len ) == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01001533
1534exit:
1535 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01001536 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01001537 mbedtls_psa_crypto_free( );
1538}
1539/* END_CASE */
1540
1541/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001542void sign_fail( int key_type_arg, data_t *key_data,
1543 int alg_arg, data_t *input_data,
Gilles Peskine20035e32018-02-03 22:44:14 +01001544 int signature_size, int expected_status_arg )
1545{
1546 int slot = 1;
1547 psa_key_type_t key_type = key_type_arg;
1548 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01001549 psa_status_t actual_status;
1550 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01001551 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01001552 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02001553 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01001554
Gilles Peskine20035e32018-02-03 22:44:14 +01001555 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001556 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001557 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1558 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1559
Gilles Peskine20035e32018-02-03 22:44:14 +01001560 signature = mbedtls_calloc( 1, signature_size );
1561 TEST_ASSERT( signature != NULL );
1562
1563 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1564
mohammad1603a97cb8c2018-03-28 03:46:26 -07001565 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001566 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001567 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1568
Gilles Peskine20035e32018-02-03 22:44:14 +01001569 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001570 key_data->x,
1571 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01001572
1573 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001574 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01001575 NULL, 0,
1576 signature, signature_size,
1577 &signature_length );
1578 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine93aa0332018-02-03 23:58:03 +01001579 TEST_ASSERT( signature_length == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01001580
1581exit:
1582 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01001583 mbedtls_free( signature );
1584 mbedtls_psa_crypto_free( );
1585}
1586/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03001587
1588/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001589void asymmetric_verify( int key_type_arg, data_t *key_data,
1590 int alg_arg, data_t *hash_data,
1591 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03001592{
1593 int slot = 1;
1594 psa_key_type_t key_type = key_type_arg;
1595 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001596 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03001597
itayzafrir5c753392018-05-08 11:18:38 +03001598 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03001599 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03001600 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001601 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1602 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
1603 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03001604
1605 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1606
1607 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001608 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03001609 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1610
1611 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001612 key_data->x,
1613 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03001614
1615 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001616 hash_data->x, hash_data->len,
itayzafrir5c753392018-05-08 11:18:38 +03001617 NULL, 0,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001618 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001619 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03001620exit:
1621 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03001622 mbedtls_psa_crypto_free( );
1623}
1624/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001625
1626/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001627void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
1628 int alg_arg, data_t *hash_data,
1629 data_t *signature_data,
1630 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001631{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001632 int slot = 1;
1633 psa_key_type_t key_type = key_type_arg;
1634 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001635 psa_status_t actual_status;
1636 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001637 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001638
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001639 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001640 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001641 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001642 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1643 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
1644 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001645
1646 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1647
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001648 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001649 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001650 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1651
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001652 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001653 key_data->x,
1654 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001655
1656 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001657 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001658 NULL, 0,
1659 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001660 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001661
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001662 TEST_ASSERT( actual_status == expected_status );
1663
1664exit:
1665 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001666 mbedtls_psa_crypto_free( );
1667}
1668/* END_CASE */
1669
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001670/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001671void asymmetric_encrypt_decrypt( int key_type_arg, data_t *key_data,
1672 int alg_arg, data_t *input_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001673{
1674 int slot = 1;
1675 psa_key_type_t key_type = key_type_arg;
1676 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001677 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001678 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001679 size_t output_length = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001680 unsigned char *output2 = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001681 size_t output2_size = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001682 size_t output2_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02001683 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001684
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001685 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001686 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001687 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1688 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1689
Gilles Peskine4abf7412018-06-18 16:35:34 +02001690 output_size = key_data->len;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001691 output2_size = output_size;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001692 output = mbedtls_calloc( 1, output_size );
1693 TEST_ASSERT( output != NULL );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001694 output2 = mbedtls_calloc( 1, output2_size );
1695 TEST_ASSERT( output2 != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001696
1697 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1698
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001699 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001700 psa_key_policy_set_usage( &policy,
1701 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001702 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001703 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1704
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001705 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001706 key_data->x,
1707 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001708
Gilles Peskineeebd7382018-06-08 18:11:54 +02001709 /* We test encryption by checking that encrypt-then-decrypt gives back
1710 * the original plaintext because of the non-optional random
1711 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02001712 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001713 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001714 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001715 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001716 &output_length ) == PSA_SUCCESS );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001717
Gilles Peskine2d277862018-06-18 15:41:12 +02001718 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001719 output, output_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001720 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001721 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001722 &output2_length ) == PSA_SUCCESS );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001723 TEST_ASSERT( memcmp( input_data->x, output2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001724 input_data->len ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001725
1726exit:
1727 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001728 mbedtls_free( output );
1729 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001730 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001731}
1732/* END_CASE */
1733
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001734/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001735void asymmetric_encrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001736 int alg_arg, data_t *input_data,
1737 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001738{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001739 int slot = 1;
1740 psa_key_type_t key_type = key_type_arg;
1741 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001742 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001743 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001744 size_t output_length = 0;
1745 psa_status_t actual_status;
1746 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001747 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001748
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001749 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001750 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001751 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1752 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1753
Gilles Peskine4abf7412018-06-18 16:35:34 +02001754 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001755 output = mbedtls_calloc( 1, output_size );
1756 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001757
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001758 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1759
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001760 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001761 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001762 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1763
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001764 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001765 key_data->x,
1766 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001767
Gilles Peskine2d277862018-06-18 15:41:12 +02001768 actual_status = psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001769 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001770 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001771 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001772 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001773 TEST_ASSERT( actual_status == expected_status );
1774
1775exit:
1776 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001777 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001778 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001779}
1780/* END_CASE */
1781
1782/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001783void asymmetric_decrypt( int key_type_arg, data_t *key_data,
1784 int alg_arg, data_t *input_data,
1785 data_t *expected_data, int expected_size )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001786{
1787 int slot = 1;
1788 psa_key_type_t key_type = key_type_arg;
1789 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001790 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001791 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001792 size_t output_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02001793 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001794
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001795 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001796 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001797 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001798 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1799 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1800 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
1801
Gilles Peskine4abf7412018-06-18 16:35:34 +02001802 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001803 output = mbedtls_calloc( 1, output_size );
1804 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001805
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001806 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1807
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001808 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001809 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001810 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1811
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001812 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001813 key_data->x,
1814 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001815
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001816 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001817 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001818 NULL, 0,
1819 output,
1820 output_size,
1821 &output_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001822 TEST_ASSERT( (size_t) expected_size == output_length );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001823 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001824
1825exit:
1826 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001827 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001828 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001829}
1830/* END_CASE */
1831
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001832/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001833void asymmetric_decrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001834 int alg_arg, data_t *input_data,
1835 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001836{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001837 int slot = 1;
1838 psa_key_type_t key_type = key_type_arg;
1839 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001840 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001841 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001842 size_t output_length = 0;
1843 psa_status_t actual_status;
1844 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001845 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001846
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001847 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001848 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001849 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1850 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1851
Gilles Peskine4abf7412018-06-18 16:35:34 +02001852 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001853 output = mbedtls_calloc( 1, output_size );
1854 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001855
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001856 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1857
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001858 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001859 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001860 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1861
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001862 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001863 key_data->x,
1864 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001865
Gilles Peskine2d277862018-06-18 15:41:12 +02001866 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001867 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001868 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001869 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001870 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001871 TEST_ASSERT( actual_status == expected_status );
1872
1873exit:
1874 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02001875 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001876 mbedtls_psa_crypto_free( );
1877}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001878/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02001879
1880/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02001881void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02001882{
Gilles Peskinea50d7392018-06-21 10:22:13 +02001883 size_t bytes = bytes_arg;
1884 const unsigned char trail[] = "don't overwrite me";
1885 unsigned char *output = mbedtls_calloc( 1, bytes + sizeof( trail ) );
1886 unsigned char *changed = mbedtls_calloc( 1, bytes );
1887 size_t i;
1888 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02001889
Gilles Peskinea50d7392018-06-21 10:22:13 +02001890 TEST_ASSERT( output != NULL );
1891 TEST_ASSERT( changed != NULL );
1892 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02001893
1894 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1895
Gilles Peskinea50d7392018-06-21 10:22:13 +02001896 /* Run several times, to ensure that every output byte will be
1897 * nonzero at least once with overwhelming probability
1898 * (2^(-8*number_of_runs)). */
1899 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02001900 {
Gilles Peskinea50d7392018-06-21 10:22:13 +02001901 memset( output, 0, bytes );
1902 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
1903
1904 /* Check that no more than bytes have been overwritten */
1905 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
1906
1907 for( i = 0; i < bytes; i++ )
1908 {
1909 if( output[i] != 0 )
1910 ++changed[i];
1911 }
Gilles Peskine05d69892018-06-19 22:00:52 +02001912 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02001913
1914 /* Check that every byte was changed to nonzero at least once. This
1915 * validates that psa_generate_random is overwriting every byte of
1916 * the output buffer. */
1917 for( i = 0; i < bytes; i++ )
1918 {
1919 TEST_ASSERT( changed[i] != 0 );
1920 }
Gilles Peskine05d69892018-06-19 22:00:52 +02001921
1922exit:
1923 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02001924 mbedtls_free( output );
1925 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02001926}
1927/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02001928
1929/* BEGIN_CASE */
1930void generate_key( int type_arg,
1931 int bits_arg,
1932 int usage_arg,
1933 int alg_arg,
1934 int expected_status_arg )
1935{
1936 int slot = 1;
1937 psa_key_type_t type = type_arg;
1938 psa_key_usage_t usage = usage_arg;
1939 size_t bits = bits_arg;
1940 psa_algorithm_t alg = alg_arg;
1941 psa_status_t expected_status = expected_status_arg;
1942 psa_key_type_t got_type;
1943 size_t got_bits;
1944 unsigned char exported[616] = {0}; /* enough for a 1024-bit RSA key */
1945 size_t exported_length;
1946 psa_status_t expected_export_status =
1947 usage & PSA_KEY_USAGE_EXPORT ? PSA_SUCCESS : PSA_ERROR_NOT_PERMITTED;
1948 psa_status_t expected_info_status =
1949 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
1950 psa_key_policy_t policy;
1951
1952 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1953
1954 psa_key_policy_init( &policy );
1955 psa_key_policy_set_usage( &policy, usage, alg );
1956 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1957
1958 /* Generate a key */
1959 TEST_ASSERT( psa_generate_key( slot, type, bits,
1960 NULL, 0 ) == expected_status );
1961
1962 /* Test the key information */
1963 TEST_ASSERT( psa_get_key_information( slot,
1964 &got_type,
1965 &got_bits ) == expected_info_status );
1966 if( expected_info_status != PSA_SUCCESS )
1967 goto exit;
1968 TEST_ASSERT( got_type == type );
1969 TEST_ASSERT( got_bits == bits );
1970
1971 /* Export the key */
1972 TEST_ASSERT( psa_export_key( slot,
1973 exported, sizeof( exported ),
1974 &exported_length ) == expected_export_status );
1975 if( expected_export_status == PSA_SUCCESS )
1976 {
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001977 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02001978 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
1979#if defined(MBEDTLS_DES_C)
1980 if( type == PSA_KEY_TYPE_DES )
1981 {
1982 /* Check the parity bits. */
1983 unsigned i;
1984 for( i = 0; i < bits / 8; i++ )
1985 {
1986 unsigned bit_count = 0;
1987 unsigned m;
1988 for( m = 1; m <= 0x100; m <<= 1 )
1989 {
1990 if( exported[i] & m )
1991 ++bit_count;
1992 }
1993 TEST_ASSERT( bit_count % 2 != 0 );
1994 }
1995 }
1996#endif
1997#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
1998 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
1999 {
2000 /* Sanity check: does this look like the beginning of a PKCS#8
2001 * RSA key pair? Assumes bits is a multiple of 8. */
2002 size_t n_bytes = bits / 8 + 1;
2003 size_t n_encoded_bytes;
2004 unsigned char *n_end;
2005 TEST_ASSERT( exported_length >= 7 + ( n_bytes + 3 ) * 9 / 2 );
2006 TEST_ASSERT( exported[0] == 0x30 );
2007 TEST_ASSERT( exported[1] == 0x82 ); // assumes >=416-bit key
2008 TEST_ASSERT( exported[4] == 0x02 );
2009 TEST_ASSERT( exported[5] == 0x01 );
2010 TEST_ASSERT( exported[6] == 0x00 );
2011 TEST_ASSERT( exported[7] == 0x02 );
2012 n_encoded_bytes = exported[8];
2013 n_end = exported + 9 + n_encoded_bytes;
2014 if( n_encoded_bytes & 0x80 )
2015 {
2016 n_encoded_bytes = ( n_encoded_bytes & 0x7f ) << 7;
2017 n_encoded_bytes |= exported[9] & 0x7f;
2018 n_end += 1;
2019 }
2020 /* The encoding of n should start with a 0 byte since it should
2021 * have its high bit set. However Mbed TLS is not compliant and
2022 * generates an invalid, but widely tolerated, encoding of
2023 * positive INTEGERs with a bit size that is a multiple of 8
2024 * with no leading 0 byte. Accept this here. */
2025 TEST_ASSERT( n_bytes == n_encoded_bytes ||
2026 n_bytes == n_encoded_bytes + 1 );
2027 if( n_bytes == n_encoded_bytes )
2028 TEST_ASSERT( exported[n_encoded_bytes <= 127 ? 9 : 10] == 0x00 );
2029 /* Sanity check: e must be 3 */
2030 TEST_ASSERT( n_end[0] == 0x02 );
2031 TEST_ASSERT( n_end[1] == 0x03 );
2032 TEST_ASSERT( n_end[2] == 0x01 );
2033 TEST_ASSERT( n_end[3] == 0x00 );
2034 TEST_ASSERT( n_end[4] == 0x01 );
2035 TEST_ASSERT( n_end[5] == 0x02 );
2036 }
2037#endif /* MBEDTLS_RSA_C */
2038#if defined(MBEDTLS_ECP_C)
2039 if( PSA_KEY_TYPE_IS_ECC( type ) )
2040 {
2041 /* Sanity check: does this look like the beginning of a PKCS#8
2042 * elliptic curve key pair? */
2043 TEST_ASSERT( exported_length >= bits * 3 / 8 + 10 );
2044 TEST_ASSERT( exported[0] == 0x30 );
2045 }
2046#endif /* MBEDTLS_ECP_C */
2047 }
2048
Gilles Peskine818ca122018-06-20 18:16:48 +02002049 /* Do something with the key according to its type and permitted usage. */
2050 if( PSA_ALG_IS_MAC( alg ) )
2051 exercise_mac_key( slot, usage, alg );
2052 else if( PSA_ALG_IS_CIPHER( alg ) )
2053 exercise_cipher_key( slot, usage, alg );
2054 else if( PSA_ALG_IS_AEAD( alg ) )
2055 exercise_aead_key( slot, usage, alg );
2056 else if( PSA_ALG_IS_SIGN( alg ) )
2057 exercise_signature_key( slot, usage, alg );
2058 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
2059 exercise_asymmetric_encryption_key( slot, usage, alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02002060
2061exit:
2062 psa_destroy_key( slot );
2063 mbedtls_psa_crypto_free( );
2064}
2065/* END_CASE */