blob: a308cbd18fa46b3a4d2b5adb8476fb9dfcc96bba [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
2#include "psa/crypto.h"
Gilles Peskine8c9def32018-02-08 10:02:12 +01003
4#include "mbedtls/md.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +01005/* END_HEADER */
6
7/* BEGIN_DEPENDENCIES
8 * depends_on:MBEDTLS_PSA_CRYPTO_C
9 * END_DEPENDENCIES
10 */
11
12/* BEGIN_CASE */
13void init_deinit()
14{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010015 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +010016 int i;
17 for( i = 0; i <= 1; i++ )
18 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010019 status = psa_crypto_init( );
20 TEST_ASSERT( status == PSA_SUCCESS );
21 status = psa_crypto_init( );
22 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +010023 mbedtls_psa_crypto_free( );
24 }
25}
26/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027
28/* BEGIN_CASE */
29void import( char *hex, int type, int expected_status )
30{
31 int slot = 1;
32 psa_status_t status;
33 unsigned char *data = NULL;
34 size_t data_size;
35
Gilles Peskine40f68b92018-03-07 16:43:36 +010036 data = unhexify_alloc( hex, &data_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010037 TEST_ASSERT( data != NULL );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010038 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
39
40 status = psa_import_key( slot, type, data, data_size );
41 TEST_ASSERT( status == (psa_status_t) expected_status );
42 if( status == PSA_SUCCESS )
43 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
44
45exit:
46 mbedtls_free( data );
47 mbedtls_psa_crypto_free( );
48}
49/* END_CASE */
50
51/* BEGIN_CASE */
Moran Pekera964a8f2018-06-04 18:42:36 +030052void import_export( char *hex,
53 int type_arg,
54 int alg_arg,
55 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010056 int expected_bits,
57 int export_size_delta,
58 int expected_export_status,
59 int canonical_input )
60{
61 int slot = 1;
62 int slot2 = slot + 1;
63 psa_key_type_t type = type_arg;
64 psa_status_t status;
65 unsigned char *data = NULL;
66 unsigned char *exported = NULL;
67 unsigned char *reexported = NULL;
68 size_t data_size;
69 size_t export_size;
70 size_t exported_length;
71 size_t reexported_length;
72 psa_key_type_t got_type;
73 size_t got_bits;
mohammad1603a97cb8c2018-03-28 03:46:26 -070074 psa_key_policy_t policy = {0};
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075
Gilles Peskine40f68b92018-03-07 16:43:36 +010076 data = unhexify_alloc( hex, &data_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010077 TEST_ASSERT( data != NULL );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010078 export_size = (ssize_t) data_size + export_size_delta;
79 exported = mbedtls_calloc( 1, export_size );
80 TEST_ASSERT( exported != NULL );
81 if( ! canonical_input )
82 {
83 reexported = mbedtls_calloc( 1, export_size );
84 TEST_ASSERT( reexported != NULL );
85 }
86 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
87
mohammad1603a97cb8c2018-03-28 03:46:26 -070088 psa_key_policy_init( &policy );
89
Moran Pekera964a8f2018-06-04 18:42:36 +030090 psa_key_policy_set_usage( &policy, usage_arg, alg_arg );
mohammad1603a97cb8c2018-03-28 03:46:26 -070091
92 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094 /* Import the key */
95 TEST_ASSERT( psa_import_key( slot, type,
96 data, data_size ) == PSA_SUCCESS );
97
98 /* Test the key information */
99 TEST_ASSERT( psa_get_key_information( slot,
100 &got_type, &got_bits ) ==
101 PSA_SUCCESS );
102 TEST_ASSERT( got_type == type );
103 TEST_ASSERT( got_bits == (size_t) expected_bits );
104
105 /* Export the key */
106 status = psa_export_key( slot,
107 exported, export_size,
108 &exported_length );
109 TEST_ASSERT( status == (psa_status_t) expected_export_status );
110 if( status != PSA_SUCCESS )
111 goto destroy;
112
113 if( canonical_input )
114 {
115 TEST_ASSERT( exported_length == data_size );
116 TEST_ASSERT( memcmp( exported, data, data_size ) == 0 );
117 }
118 else
119 {
mohammad1603a97cb8c2018-03-28 03:46:26 -0700120 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
121
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100122 TEST_ASSERT( psa_import_key( slot2, type,
123 exported, export_size ) ==
124 PSA_SUCCESS );
125 TEST_ASSERT( psa_export_key( slot2,
126 reexported, export_size,
127 &reexported_length ) ==
128 PSA_SUCCESS );
129 TEST_ASSERT( reexported_length == exported_length );
130 TEST_ASSERT( memcmp( reexported, exported,
131 exported_length ) == 0 );
132 }
133
134destroy:
135 /* Destroy the key */
136 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
137 TEST_ASSERT( psa_get_key_information(
138 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
139
140exit:
141 mbedtls_free( data );
142 mbedtls_psa_crypto_free( );
143}
144/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100145
Moran Pekerf709f4a2018-06-06 17:26:04 +0300146
147/* BEGIN_CASE */
148void import_export_public_key( char *hex,
149 int type_arg,
150 int alg_arg,
151 int expected_bits,
152 int public_key_expected_length,
153 int expected_export_status )
154{
155 int slot = 1;
156 psa_key_type_t type = type_arg;
157 psa_status_t status;
158 unsigned char *data = NULL;
159 unsigned char *exported = NULL;
160 size_t data_size;
161 size_t export_size;
162 size_t exported_length;
163 psa_key_type_t got_type;
164 size_t got_bits;
165 psa_key_policy_t policy = {0};
166
167 data = unhexify_alloc( hex, &data_size );
168 TEST_ASSERT( data != NULL );
169 export_size = (ssize_t) data_size ;
170 exported = mbedtls_calloc( 1, export_size );
171 TEST_ASSERT( exported != NULL );
172
173 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
174
175 psa_key_policy_init( &policy );
176
177 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT,
178 alg_arg );
179
180 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
181
182 /* Import the key */
183 TEST_ASSERT( psa_import_key( slot, type,
184 data, data_size ) == PSA_SUCCESS );
185
186 /* Test the key information */
187 TEST_ASSERT( psa_get_key_information( slot,
188 &got_type, &got_bits ) == PSA_SUCCESS );
189 TEST_ASSERT( got_type == type );
190 TEST_ASSERT( got_bits == (size_t) expected_bits );
191
192 /* Export the key */
193 status = psa_export_public_key( slot,
194 exported, export_size,
195 &exported_length );
196 TEST_ASSERT( status == (psa_status_t) expected_export_status );
197 if( status != PSA_SUCCESS )
198 goto destroy;
199
200 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
201
202destroy:
203 /* Destroy the key */
204 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
205 TEST_ASSERT( psa_get_key_information(
206 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
207
208exit:
209 mbedtls_free( data );
210 mbedtls_psa_crypto_free( );
211}
212/* END_CASE */
213
Gilles Peskine20035e32018-02-03 22:44:14 +0100214/* BEGIN_CASE */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100215void hash_finish( int alg_arg, char *input_hex, char *hash_hex )
216{
217 psa_algorithm_t alg = alg_arg;
218 unsigned char *input = NULL;
219 size_t input_size;
220 unsigned char expected_hash[MBEDTLS_MD_MAX_SIZE];
221 size_t expected_hash_length;
222 unsigned char actual_hash[MBEDTLS_MD_MAX_SIZE];
223 size_t actual_hash_length;
224 psa_hash_operation_t operation;
225
Gilles Peskine40f68b92018-03-07 16:43:36 +0100226 input = unhexify_alloc( input_hex, &input_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100227 TEST_ASSERT( input != NULL );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100228 expected_hash_length = unhexify( expected_hash, hash_hex );
229
230 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
231
232 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
233 TEST_ASSERT( psa_hash_update( &operation,
234 input, input_size ) == PSA_SUCCESS );
235 TEST_ASSERT( psa_hash_finish( &operation,
236 actual_hash, sizeof( actual_hash ),
237 &actual_hash_length ) == PSA_SUCCESS );
238 TEST_ASSERT( actual_hash_length == expected_hash_length );
239 TEST_ASSERT( memcmp( expected_hash, actual_hash,
240 expected_hash_length ) == 0 );
241
242exit:
243 mbedtls_free( input );
244 mbedtls_psa_crypto_free( );
245}
246/* END_CASE */
247
248/* BEGIN_CASE */
249void hash_verify( int alg_arg, char *input_hex, char *hash_hex )
250{
251 psa_algorithm_t alg = alg_arg;
252 unsigned char *input = NULL;
253 size_t input_size;
254 unsigned char expected_hash[MBEDTLS_MD_MAX_SIZE];
255 size_t expected_hash_length;
256 psa_hash_operation_t operation;
257
Gilles Peskine40f68b92018-03-07 16:43:36 +0100258 input = unhexify_alloc( input_hex, &input_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100259 TEST_ASSERT( input != NULL );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100260 expected_hash_length = unhexify( expected_hash, hash_hex );
261
262 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
263
264 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
265 TEST_ASSERT( psa_hash_update( &operation,
266 input, input_size ) == PSA_SUCCESS );
267 TEST_ASSERT( psa_hash_verify( &operation,
268 expected_hash,
269 expected_hash_length ) == PSA_SUCCESS );
270
271exit:
272 mbedtls_free( input );
273 mbedtls_psa_crypto_free( );
274}
275/* END_CASE */
276
277/* BEGIN_CASE */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100278void mac_verify( int key_type_arg, char *key_hex,
279 int alg_arg, char *iv_hex,
280 char *input_hex, char *mac_hex )
281{
282 int key_slot = 1;
283 psa_key_type_t key_type = key_type_arg;
284 psa_algorithm_t alg = alg_arg;
285 unsigned char *key = NULL;
286 size_t key_size;
287 unsigned char *iv = NULL;
288 size_t iv_size;
289 unsigned char *input = NULL;
290 size_t input_size;
291 unsigned char *expected_mac = NULL;
292 size_t expected_mac_size;
293 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -0700294 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100295
Gilles Peskine40f68b92018-03-07 16:43:36 +0100296 key = unhexify_alloc( key_hex, &key_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100297 TEST_ASSERT( key != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +0100298 if( iv_hex[0] != 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +0100299 {
Gilles Peskine40f68b92018-03-07 16:43:36 +0100300 iv = unhexify_alloc( iv_hex, &iv_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100301 TEST_ASSERT( iv != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100302 }
Gilles Peskine40f68b92018-03-07 16:43:36 +0100303 input = unhexify_alloc( input_hex, &input_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100304 TEST_ASSERT( input != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +0100305 expected_mac = unhexify_alloc( mac_hex, &expected_mac_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100306 TEST_ASSERT( expected_mac != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100307
308 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
309
mohammad16036df908f2018-04-02 08:34:15 -0700310 psa_key_policy_init( &policy );
311
312 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg_arg );
313
314 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
315
Gilles Peskine8c9def32018-02-08 10:02:12 +0100316 TEST_ASSERT( psa_import_key( key_slot, key_type,
317 key, key_size ) == PSA_SUCCESS );
318 // TODO: support IV
319 TEST_ASSERT( psa_mac_start( &operation, key_slot, alg ) == PSA_SUCCESS );
320 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
321 TEST_ASSERT( psa_mac_update( &operation,
322 input, input_size ) == PSA_SUCCESS );
323 TEST_ASSERT( psa_mac_verify( &operation,
324 expected_mac,
325 expected_mac_size ) == PSA_SUCCESS );
326
327exit:
328 mbedtls_free( key );
329 mbedtls_free( iv );
330 mbedtls_free( input );
331 mbedtls_free( expected_mac );
332 psa_destroy_key( key_slot );
333 mbedtls_psa_crypto_free( );
334}
335/* END_CASE */
336
Moran Peker7f878502018-06-06 17:09:40 +0300337
Gilles Peskine8c9def32018-02-08 10:02:12 +0100338/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +0200339void cipher_encrypt( int alg_arg, int key_type_arg,
340 char *key_hex,
341 char *input_hex, char *output_hex,
342 int expected_status )
343{
344 int key_slot = 1;
345 psa_status_t status;
346 psa_key_type_t key_type = key_type_arg;
347 psa_algorithm_t alg = alg_arg;
348 unsigned char *key = NULL;
349 size_t key_size;
350 unsigned char iv[16] = {0};
351 unsigned char *input = NULL;
352 size_t input_size = 0;
353 unsigned char *output;
354 unsigned char *expected_output;
355 size_t expected_output_size;
356 size_t output_buffer_size = 0;
357 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200358 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200359 psa_cipher_operation_t operation;
360
361
362 key = unhexify_alloc( key_hex, &key_size );
363 TEST_ASSERT( key != NULL );
364
365 input = unhexify_alloc( input_hex, &input_size );
366 TEST_ASSERT( input != NULL );
367
368 expected_output = unhexify_alloc( output_hex, &expected_output_size );
369 TEST_ASSERT( expected_output != NULL );
370
371 memset( iv, 0x2a, sizeof( iv ) );
372
373 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
374
375 TEST_ASSERT( psa_import_key( key_slot, key_type,
376 key, key_size ) == PSA_SUCCESS );
377
378 TEST_ASSERT( psa_encrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS );
379
380 TEST_ASSERT( psa_encrypt_set_iv( &operation,
381 iv, sizeof( iv ) ) == PSA_SUCCESS );
382 output_buffer_size = input_size + operation.block_size;
383 output = mbedtls_calloc( 1, output_buffer_size );
384
385 TEST_ASSERT( psa_cipher_update( &operation, input, input_size,
386 output, output_buffer_size,
387 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200388 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200389 status = psa_cipher_finish( &operation,
390 output + function_output_length,
391 output_buffer_size,
392 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200393 total_output_length += function_output_length;
394
Gilles Peskine50e586b2018-06-08 14:28:46 +0200395 TEST_ASSERT( status == (psa_status_t) expected_status );
396 if( expected_status == PSA_SUCCESS )
397 {
398 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200399 TEST_ASSERT( total_output_length == expected_output_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200400 TEST_ASSERT( memcmp( expected_output, output,
401 expected_output_size ) == 0 );
402 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200403
Gilles Peskine50e586b2018-06-08 14:28:46 +0200404exit:
405 mbedtls_free( key );
406 mbedtls_free( input );
407 psa_destroy_key( key_slot );
408 mbedtls_psa_crypto_free( );
409}
410/* END_CASE */
411
412/* BEGIN_CASE */
413void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
414 char *key_hex,
415 char *input_hex,
416 int first_part_size, char *output_hex )
417{
418 int key_slot = 1;
419 psa_key_type_t key_type = key_type_arg;
420 psa_algorithm_t alg = alg_arg;
421 unsigned char *key = NULL;
422 size_t key_size;
423 unsigned char iv[16] = {0};
424 unsigned char *input = NULL;
425 size_t input_size = 0;
426 unsigned char *output;
427 unsigned char *expected_output;
428 size_t expected_output_size;
429 size_t output_buffer_size = 0;
430 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200431 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200432 psa_cipher_operation_t operation;
433
Gilles Peskine50e586b2018-06-08 14:28:46 +0200434 key = unhexify_alloc( key_hex, &key_size );
435 TEST_ASSERT( key != NULL );
436
437 input = unhexify_alloc( input_hex, &input_size );
438 TEST_ASSERT( input != NULL );
439
440 expected_output = unhexify_alloc( output_hex, &expected_output_size );
441 TEST_ASSERT( expected_output != NULL );
442
443 memset( iv, 0x2a, sizeof( iv ) );
444
445 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
446
447 TEST_ASSERT( psa_import_key( key_slot, key_type,
448 key, key_size ) == PSA_SUCCESS );
449
450 TEST_ASSERT( psa_encrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS );
451
452 TEST_ASSERT( psa_encrypt_set_iv( &operation,
453 iv, sizeof( iv ) ) == PSA_SUCCESS );
454 output_buffer_size = input_size + operation.block_size;
455 output = mbedtls_calloc( 1, output_buffer_size );
456
457 TEST_ASSERT( (unsigned int) first_part_size < input_size );
458 TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size,
459 output, output_buffer_size,
460 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200461 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200462 TEST_ASSERT( psa_cipher_update( &operation,
463 input + first_part_size,
464 input_size - first_part_size,
465 output, output_buffer_size,
466 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200467 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200468 TEST_ASSERT( psa_cipher_finish( &operation,
469 output + function_output_length,
470 output_buffer_size,
471 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200472 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200473 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
474
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200475 TEST_ASSERT( total_output_length == expected_output_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200476 TEST_ASSERT( memcmp( expected_output, output, expected_output_size ) == 0 );
477
478exit:
479 mbedtls_free( key );
480 mbedtls_free( input );
481 psa_destroy_key( key_slot );
482 mbedtls_psa_crypto_free( );
483}
484/* END_CASE */
485
486/* BEGIN_CASE */
487void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
488 char *key_hex,
489 char *input_hex,
490 int first_part_size, char *output_hex )
491{
492 int key_slot = 1;
493
494 psa_key_type_t key_type = key_type_arg;
495 psa_algorithm_t alg = alg_arg;
496 unsigned char *key = NULL;
497 size_t key_size;
498 unsigned char iv[16] = {0};
499 unsigned char *input = NULL;
500 size_t input_size = 0;
501 unsigned char *output;
502 unsigned char *expected_output;
503 size_t expected_output_size;
504 size_t output_buffer_size = 0;
505 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200506 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200507 psa_cipher_operation_t operation;
508
Gilles Peskine50e586b2018-06-08 14:28:46 +0200509 key = unhexify_alloc( key_hex, &key_size );
510 TEST_ASSERT( key != NULL );
511
512 input = unhexify_alloc( input_hex, &input_size );
513 TEST_ASSERT( input != NULL );
514
515 expected_output = unhexify_alloc( output_hex, &expected_output_size );
516 TEST_ASSERT( expected_output != NULL );
517
518 memset( iv, 0x2a, sizeof( iv ) );
519
520 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
521
522 TEST_ASSERT( psa_import_key( key_slot, key_type,
523 key, key_size ) == PSA_SUCCESS );
524
525 TEST_ASSERT( psa_decrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS );
526
527 TEST_ASSERT( psa_encrypt_set_iv( &operation,
528 iv, sizeof( iv ) ) == PSA_SUCCESS );
529
530 output_buffer_size = input_size + operation.block_size;
531 output = mbedtls_calloc( 1, output_buffer_size );
532
533 TEST_ASSERT( (unsigned int) first_part_size < input_size );
534 TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size,
535 output, output_buffer_size,
536 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200537 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200538 TEST_ASSERT( psa_cipher_update( &operation,
539 input + first_part_size,
540 input_size - first_part_size,
541 output, output_buffer_size,
542 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200543 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200544 TEST_ASSERT( psa_cipher_finish( &operation,
545 output + function_output_length,
546 output_buffer_size,
547 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200548 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200549 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
550
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200551 TEST_ASSERT( total_output_length == expected_output_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200552 TEST_ASSERT( memcmp( expected_output, output, expected_output_size ) == 0 );
553
554exit:
555 mbedtls_free( key );
556 mbedtls_free( input );
557 psa_destroy_key( key_slot );
558 mbedtls_psa_crypto_free( );
559}
560/* END_CASE */
561
562
563/* BEGIN_CASE */
564void cipher_decrypt( int alg_arg, int key_type_arg,
565 char *key_hex,
566 char *input_hex, char *output_hex,
567 int expected_status )
568{
569 int key_slot = 1;
570 psa_status_t status;
571 psa_key_type_t key_type = key_type_arg;
572 psa_algorithm_t alg = alg_arg;
573 unsigned char *key = NULL;
574 size_t key_size;
575 unsigned char iv[16] = {0};
576 unsigned char *input = NULL;
577 size_t input_size = 0;
578 unsigned char *output;
579 unsigned char *expected_output;
580 size_t expected_output_size;
581 size_t output_buffer_size = 0;
582 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200583 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200584 psa_cipher_operation_t operation;
585
586
587 key = unhexify_alloc( key_hex, &key_size );
588 TEST_ASSERT( key != NULL );
589
590 input = unhexify_alloc( input_hex, &input_size );
591 TEST_ASSERT( input != NULL );
592
593 expected_output = unhexify_alloc( output_hex, &expected_output_size );
594 TEST_ASSERT( expected_output != NULL );
595
596 memset( iv, 0x2a, sizeof( iv ) );
597
598 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
599
600 TEST_ASSERT( psa_import_key( key_slot, key_type,
601 key, key_size ) == PSA_SUCCESS );
602
603 TEST_ASSERT( psa_decrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS );
604
605 TEST_ASSERT( psa_encrypt_set_iv( &operation,
606 iv, sizeof( iv ) ) == PSA_SUCCESS );
607
608 output_buffer_size = input_size + operation.block_size;
609 output = mbedtls_calloc( 1, output_buffer_size );
610
611 TEST_ASSERT( psa_cipher_update( &operation, input, input_size,
612 output, output_buffer_size,
613 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200614 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200615 status = psa_cipher_finish( &operation,
616 output + function_output_length,
617 output_buffer_size,
618 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200619 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200620 TEST_ASSERT( status == (psa_status_t) expected_status );
621
622 if( expected_status == PSA_SUCCESS )
623 {
624 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200625 TEST_ASSERT( total_output_length == expected_output_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200626 TEST_ASSERT( memcmp( expected_output, output,
627 expected_output_size ) == 0 );
628 }
629
630
631exit:
632 mbedtls_free( key );
633 mbedtls_free( input );
634 psa_destroy_key( key_slot );
635 mbedtls_psa_crypto_free( );
636}
637/* END_CASE */
638
639
640/* BEGIN_CASE */
641void cipher_verify_output( int alg_arg, int key_type_arg,
Moran Pekerded84402018-06-06 16:36:50 +0300642 char *key_hex,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200643 char *input_hex )
mohammad1603d7d7ba52018-03-12 18:51:53 +0200644{
645 int key_slot = 1;
646 psa_key_type_t key_type = key_type_arg;
647 psa_algorithm_t alg = alg_arg;
648 unsigned char *key = NULL;
649 size_t key_size;
mohammad1603e6b67a12018-03-12 10:38:49 -0700650 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +0200651 size_t iv_size = 16;
652 size_t iv_length = 0;
653 unsigned char *input = NULL;
654 size_t input_size = 0;
Moran Peker3205a652018-03-20 17:09:59 +0200655 unsigned char *output1;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200656 size_t output1_size = 0;
657 size_t output1_length = 0;
Moran Peker3205a652018-03-20 17:09:59 +0200658 unsigned char *output2;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200659 size_t output2_size = 0;
660 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +0200661 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200662 psa_cipher_operation_t operation1;
663 psa_cipher_operation_t operation2;
664
665 key = unhexify_alloc( key_hex, &key_size );
666 TEST_ASSERT( key != NULL );
667
668 input = unhexify_alloc( input_hex, &input_size );
669 TEST_ASSERT( input != NULL );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200670
mohammad1603d7d7ba52018-03-12 18:51:53 +0200671 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
672
673 TEST_ASSERT( psa_import_key( key_slot, key_type,
674 key, key_size ) == PSA_SUCCESS );
675
Moran Pekerf55e8042018-05-29 16:28:28 +0300676 TEST_ASSERT( psa_encrypt_setup( &operation1, key_slot, alg ) == PSA_SUCCESS );
677 TEST_ASSERT( psa_decrypt_setup( &operation2, key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200678
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200679 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
680 iv, iv_size,
681 &iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300682 output1_size = input_size + operation1.block_size;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200683 output1 = mbedtls_calloc( 1, output1_size );
684 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +0300685
mohammad1603d7d7ba52018-03-12 18:51:53 +0200686 TEST_ASSERT( psa_cipher_update( &operation1, input, input_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200687 output1, output1_size,
688 &output1_length ) == PSA_SUCCESS );
689 TEST_ASSERT( psa_cipher_finish( &operation1,
690 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200691 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200692
Gilles Peskine048b7f02018-06-08 14:20:49 +0200693 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300694
695 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
696
697 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200698 output2 = mbedtls_calloc( 1, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +0300699
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200700 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
701 iv, iv_length ) == PSA_SUCCESS );
702 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
703 output2, output2_size,
704 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +0200705 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200706 TEST_ASSERT( psa_cipher_finish( &operation2,
707 output2 + output2_length,
708 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200709 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300710
Gilles Peskine048b7f02018-06-08 14:20:49 +0200711 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200712
Moran Pekerded84402018-06-06 16:36:50 +0300713 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
714
715 TEST_ASSERT( input_size == output2_length );
716 TEST_ASSERT( memcmp( input, output2, input_size ) == 0 );
717
718exit:
719 mbedtls_free( key );
720 mbedtls_free( input );
Moran Pekerded84402018-06-06 16:36:50 +0300721 psa_destroy_key( key_slot );
722 mbedtls_psa_crypto_free( );
723}
724/* END_CASE */
725
726/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +0200727void cipher_verify_output_multipart( int alg_arg,
728 int key_type_arg,
729 char *key_hex,
730 char *input_hex,
731 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +0300732{
733 int key_slot = 1;
734 psa_key_type_t key_type = key_type_arg;
735 psa_algorithm_t alg = alg_arg;
736 unsigned char *key = NULL;
737 size_t key_size;
738 unsigned char iv[16] = {0};
739 size_t iv_size = 16;
740 size_t iv_length = 0;
741 unsigned char *input = NULL;
742 size_t input_size = 0;
743 unsigned char *output1;
Gilles Peskine048b7f02018-06-08 14:20:49 +0200744 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +0300745 size_t output1_length = 0;
746 unsigned char *output2;
Gilles Peskine048b7f02018-06-08 14:20:49 +0200747 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +0300748 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +0200749 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300750 psa_cipher_operation_t operation1;
751 psa_cipher_operation_t operation2;
752
753 key = unhexify_alloc( key_hex, &key_size );
754 TEST_ASSERT( key != NULL );
755
756 input = unhexify_alloc( input_hex, &input_size );
757 TEST_ASSERT( input != NULL );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200758
Moran Pekerded84402018-06-06 16:36:50 +0300759 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
760
761 TEST_ASSERT( psa_import_key( key_slot, key_type,
762 key, key_size ) == PSA_SUCCESS );
763
764 TEST_ASSERT( psa_encrypt_setup( &operation1, key_slot, alg ) == PSA_SUCCESS );
765 TEST_ASSERT( psa_decrypt_setup( &operation2, key_slot, alg ) == PSA_SUCCESS );
766
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200767 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
768 iv, iv_size,
769 &iv_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +0200770 output1_buffer_size = input_size + operation1.block_size;
771 output1 = mbedtls_calloc( 1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +0300772
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200773 TEST_ASSERT( (unsigned int) first_part_size < input_size );
774
Moran Pekerded84402018-06-06 16:36:50 +0300775 TEST_ASSERT( psa_cipher_update( &operation1, input, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200776 output1, output1_buffer_size,
777 &function_output_length ) == PSA_SUCCESS );
778 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300779
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200780 TEST_ASSERT( psa_cipher_update( &operation1,
781 input + first_part_size,
782 input_size - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200783 output1, output1_buffer_size,
784 &function_output_length ) == PSA_SUCCESS );
785 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300786
Gilles Peskine048b7f02018-06-08 14:20:49 +0200787 TEST_ASSERT( psa_cipher_finish( &operation1,
788 output1 + output1_length,
789 output1_buffer_size - output1_length,
790 &function_output_length ) == PSA_SUCCESS );
791 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200792
793 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
794
Gilles Peskine048b7f02018-06-08 14:20:49 +0200795 output2_buffer_size = output1_length;
796 output2 = mbedtls_calloc( 1, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200797
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200798 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
799 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300800
801 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200802 output2, output2_buffer_size,
803 &function_output_length ) == PSA_SUCCESS );
804 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300805
Gilles Peskine048b7f02018-06-08 14:20:49 +0200806 TEST_ASSERT( psa_cipher_update( &operation2,
807 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200808 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200809 output2, output2_buffer_size,
810 &function_output_length ) == PSA_SUCCESS );
811 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300812
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200813 TEST_ASSERT( psa_cipher_finish( &operation2,
814 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200815 output2_buffer_size - output2_length,
816 &function_output_length ) == PSA_SUCCESS );
817 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200818
mohammad1603d7d7ba52018-03-12 18:51:53 +0200819 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
820
Moran Pekerded84402018-06-06 16:36:50 +0300821 TEST_ASSERT( input_size == output2_length );
mohammad1603e6b67a12018-03-12 10:38:49 -0700822 TEST_ASSERT( memcmp( input, output2, input_size ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200823
824exit:
825 mbedtls_free( key );
826 mbedtls_free( input );
827 psa_destroy_key( key_slot );
828 mbedtls_psa_crypto_free( );
829}
830/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +0200831
Gilles Peskine20035e32018-02-03 22:44:14 +0100832/* BEGIN_CASE */
833void signature_size( int type_arg, int bits, int alg_arg, int expected_size_arg )
834{
Gilles Peskine0189e752018-02-03 23:57:22 +0100835 psa_key_type_t type = type_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +0100836 psa_algorithm_t alg = alg_arg;
837 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(type, bits, alg);
838 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
839exit:
840 ;
841}
842/* END_CASE */
843
844/* BEGIN_CASE */
845void sign_deterministic( int key_type_arg, char *key_hex,
846 int alg_arg, char *input_hex, char *output_hex )
847{
848 int slot = 1;
849 psa_key_type_t key_type = key_type_arg;
850 psa_algorithm_t alg = alg_arg;
851 unsigned char *key_data = NULL;
852 size_t key_size;
853 size_t key_bits;
854 unsigned char *input_data = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +0100855 size_t input_size;
Gilles Peskine20035e32018-02-03 22:44:14 +0100856 unsigned char *output_data = NULL;
857 size_t output_size;
858 unsigned char *signature = NULL;
859 size_t signature_size;
860 size_t signature_length = 0xdeadbeef;
mohammad1603a97cb8c2018-03-28 03:46:26 -0700861 psa_key_policy_t policy = {0};
Gilles Peskine20035e32018-02-03 22:44:14 +0100862
Gilles Peskine40f68b92018-03-07 16:43:36 +0100863 key_data = unhexify_alloc( key_hex, &key_size );
Gilles Peskine20035e32018-02-03 22:44:14 +0100864 TEST_ASSERT( key_data != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +0100865 input_data = unhexify_alloc( input_hex, &input_size );
Gilles Peskine20035e32018-02-03 22:44:14 +0100866 TEST_ASSERT( input_data != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +0100867 output_data = unhexify_alloc( output_hex, &output_size );
Gilles Peskine20035e32018-02-03 22:44:14 +0100868 TEST_ASSERT( output_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +0100869
870 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
871
mohammad1603a97cb8c2018-03-28 03:46:26 -0700872 psa_key_policy_init( &policy );
873
874 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg );
875
876 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
877
Gilles Peskine20035e32018-02-03 22:44:14 +0100878 TEST_ASSERT( psa_import_key( slot, key_type,
879 key_data, key_size ) == PSA_SUCCESS );
880 TEST_ASSERT( psa_get_key_information( slot,
881 NULL,
882 &key_bits ) == PSA_SUCCESS );
883
884 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, alg, key_bits );
885 TEST_ASSERT( signature_size != 0 );
886 signature = mbedtls_calloc( 1, signature_size );
887 TEST_ASSERT( signature != NULL );
888
889 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
890 input_data, input_size,
891 NULL, 0,
892 signature, signature_size,
893 &signature_length ) == PSA_SUCCESS );
894 TEST_ASSERT( signature_length == output_size );
895 TEST_ASSERT( memcmp( signature, output_data, output_size ) == 0 );
896
897exit:
898 psa_destroy_key( slot );
899 mbedtls_free( key_data );
900 mbedtls_free( input_data );
901 mbedtls_free( output_data );
902 mbedtls_free( signature );
903 mbedtls_psa_crypto_free( );
904}
905/* END_CASE */
906
907/* BEGIN_CASE */
908void sign_fail( int key_type_arg, char *key_hex,
909 int alg_arg, char *input_hex,
910 int signature_size, int expected_status_arg )
911{
912 int slot = 1;
913 psa_key_type_t key_type = key_type_arg;
914 psa_algorithm_t alg = alg_arg;
915 unsigned char *key_data = NULL;
916 size_t key_size;
917 unsigned char *input_data = NULL;
918 size_t input_size;
919 psa_status_t actual_status;
920 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +0100921 unsigned char *signature = NULL;
Gilles Peskine20035e32018-02-03 22:44:14 +0100922 size_t signature_length = 0xdeadbeef;
mohammad1603a97cb8c2018-03-28 03:46:26 -0700923 psa_key_policy_t policy = {0};
Gilles Peskine20035e32018-02-03 22:44:14 +0100924
Gilles Peskine40f68b92018-03-07 16:43:36 +0100925 key_data = unhexify_alloc( key_hex, &key_size );
Gilles Peskine20035e32018-02-03 22:44:14 +0100926 TEST_ASSERT( key_data != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +0100927 input_data = unhexify_alloc( input_hex, &input_size );
Gilles Peskine20035e32018-02-03 22:44:14 +0100928 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +0100929 signature = mbedtls_calloc( 1, signature_size );
930 TEST_ASSERT( signature != NULL );
931
932 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
933
mohammad1603a97cb8c2018-03-28 03:46:26 -0700934 psa_key_policy_init( &policy );
935
936 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg );
937
938 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
939
Gilles Peskine20035e32018-02-03 22:44:14 +0100940 TEST_ASSERT( psa_import_key( slot, key_type,
941 key_data, key_size ) == PSA_SUCCESS );
942
943 actual_status = psa_asymmetric_sign( slot, alg,
944 input_data, input_size,
945 NULL, 0,
946 signature, signature_size,
947 &signature_length );
948 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine93aa0332018-02-03 23:58:03 +0100949 TEST_ASSERT( signature_length == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +0100950
951exit:
952 psa_destroy_key( slot );
953 mbedtls_free( key_data );
954 mbedtls_free( input_data );
955 mbedtls_free( signature );
956 mbedtls_psa_crypto_free( );
957}
958/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +0300959
960/* BEGIN_CASE */
961void key_policy( int usage_arg, int alg_arg )
962{
963 int key_slot = 1;
mohammad16034eed7572018-03-28 05:14:59 -0700964 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
mohammad16038cc1cee2018-03-28 01:21:33 +0300965 unsigned char key[32] = {0};
966 psa_key_policy_t policy_set = {0};
967 psa_key_policy_t policy_get = {0};
968
969 memset( key, 0x2a, sizeof( key ) );
970
971 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
972
973 psa_key_policy_init(& policy_set );
974 psa_key_policy_init(& policy_get );
975
976 psa_key_policy_set_usage( &policy_set, usage_arg, alg_arg );
977
978 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == ( psa_key_usage_t )usage_arg );
979
980 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set) == ( psa_algorithm_t )alg_arg );
981
982 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
983
984 TEST_ASSERT( psa_import_key( key_slot, key_type,
985 key, sizeof( key ) ) == PSA_SUCCESS );
986
987 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
988
989 TEST_ASSERT( policy_get.usage == policy_set.usage );
990 TEST_ASSERT( policy_get.alg == policy_set.alg );
991
992exit:
993 psa_destroy_key( key_slot );
994 mbedtls_psa_crypto_free( );
995}
996/* END_CASE */
997
mohammad16034eed7572018-03-28 05:14:59 -0700998/* BEGIN_CASE */
999void key_policy_fail( int usage_arg, int alg_arg, int expected_status, char *key_hex )
1000{
1001 int key_slot = 1;
mohammad16034eed7572018-03-28 05:14:59 -07001002 unsigned char* keypair = NULL;
1003 size_t key_size = 0;
1004 size_t signature_length = 0;
1005 psa_key_policy_t policy = {0};
1006 int actual_status = PSA_SUCCESS;
mohammad16034eed7572018-03-28 05:14:59 -07001007
1008 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1009
1010 psa_key_policy_init( &policy );
1011
1012 psa_key_policy_set_usage( &policy, usage_arg, alg_arg );
1013
1014 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1015
mohammad16036df908f2018-04-02 08:34:15 -07001016 if( usage_arg & PSA_KEY_USAGE_EXPORT )
mohammad16034eed7572018-03-28 05:14:59 -07001017 {
mohammad16036df908f2018-04-02 08:34:15 -07001018 keypair = unhexify_alloc( key_hex, &key_size );
1019 TEST_ASSERT( keypair != NULL );
1020 TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR,
1021 keypair, key_size ) == PSA_SUCCESS );
1022 actual_status = psa_asymmetric_sign( key_slot,
1023 ( psa_algorithm_t )alg_arg, NULL, 0, NULL, 0,
1024 NULL, 0, &signature_length );
1025 }
1026
1027 if( usage_arg & PSA_KEY_USAGE_SIGN )
1028 {
mohammad1603d926b882018-04-16 01:53:20 -07001029 keypair = unhexify_alloc( key_hex, &key_size );
1030 TEST_ASSERT( keypair != NULL );
mohammad16036df908f2018-04-02 08:34:15 -07001031 TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR,
mohammad1603d926b882018-04-16 01:53:20 -07001032 keypair, key_size ) == PSA_SUCCESS );
mohammad16036df908f2018-04-02 08:34:15 -07001033 actual_status = psa_export_key( key_slot, NULL, 0, NULL );
mohammad16034eed7572018-03-28 05:14:59 -07001034 }
1035
1036 TEST_ASSERT( actual_status == expected_status );
1037
1038exit:
1039 psa_destroy_key( key_slot );
mohammad1603d926b882018-04-16 01:53:20 -07001040 mbedtls_free( keypair );
mohammad16034eed7572018-03-28 05:14:59 -07001041 mbedtls_psa_crypto_free( );
1042}
1043/* END_CASE */
Gilles Peskinea0655c32018-04-30 17:06:50 +02001044
1045/* BEGIN_CASE */
1046void key_lifetime( int lifetime_arg )
1047{
1048 int key_slot = 1;
1049 psa_key_type_t key_type = PSA_ALG_CBC_BASE;
1050 unsigned char key[32] = {0};
1051 psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg;
1052 psa_key_lifetime_t lifetime_get;
Gilles Peskine7268afc2018-06-06 15:19:24 +02001053
Gilles Peskinea0655c32018-04-30 17:06:50 +02001054 memset( key, 0x2a, sizeof( key ) );
Gilles Peskine7268afc2018-06-06 15:19:24 +02001055
Gilles Peskinea0655c32018-04-30 17:06:50 +02001056 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
Gilles Peskine7268afc2018-06-06 15:19:24 +02001057
Gilles Peskinea0655c32018-04-30 17:06:50 +02001058 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1059 lifetime_set ) == PSA_SUCCESS );
Gilles Peskine7268afc2018-06-06 15:19:24 +02001060
Gilles Peskinea0655c32018-04-30 17:06:50 +02001061 TEST_ASSERT( psa_import_key( key_slot, key_type,
1062 key, sizeof( key ) ) == PSA_SUCCESS );
Gilles Peskine7268afc2018-06-06 15:19:24 +02001063
Gilles Peskinea0655c32018-04-30 17:06:50 +02001064 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1065 &lifetime_get ) == PSA_SUCCESS );
Gilles Peskine7268afc2018-06-06 15:19:24 +02001066
Gilles Peskinea0655c32018-04-30 17:06:50 +02001067 TEST_ASSERT( lifetime_get == lifetime_set );
Gilles Peskine7268afc2018-06-06 15:19:24 +02001068
Gilles Peskinea0655c32018-04-30 17:06:50 +02001069exit:
1070 psa_destroy_key( key_slot );
1071 mbedtls_psa_crypto_free( );
1072}
1073/* END_CASE */
mohammad1603804cd712018-03-20 22:44:08 +02001074
Gilles Peskine7268afc2018-06-06 15:19:24 +02001075
mohammad1603804cd712018-03-20 22:44:08 +02001076/* BEGIN_CASE */
1077void key_lifetime_set_fail( int key_slot_arg, int lifetime_arg, int expected_status_arg )
1078{
1079 int key_slot = 1;
mohammad1603804cd712018-03-20 22:44:08 +02001080 psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg;
1081 psa_status_t actual_status;
1082 psa_status_t expected_status = expected_status_arg;
1083
mohammad1603804cd712018-03-20 22:44:08 +02001084 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1085
mohammad1603804cd712018-03-20 22:44:08 +02001086 actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set );
1087
1088 if( actual_status == PSA_SUCCESS )
1089 actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set );
1090
1091 TEST_ASSERT( expected_status == actual_status );
1092
1093exit:
mohammad1603804cd712018-03-20 22:44:08 +02001094 psa_destroy_key( key_slot );
1095 mbedtls_psa_crypto_free( );
1096}
1097/* END_CASE */
itayzafrir5c753392018-05-08 11:18:38 +03001098
1099/* BEGIN_CASE */
1100void asymmetric_verify( int key_type_arg, char *key_hex,
1101 int alg_arg, char *hash_hex, char *signature_hex )
1102{
1103 int slot = 1;
1104 psa_key_type_t key_type = key_type_arg;
1105 psa_algorithm_t alg = alg_arg;
1106 unsigned char *key_data = NULL;
1107 size_t key_size;
1108 unsigned char *hash_data = NULL;
1109 size_t hash_size;
1110 unsigned char *signature_data = NULL;
1111 size_t signature_size;
1112 psa_key_policy_t policy = {0};
1113
1114 key_data = unhexify_alloc( key_hex, &key_size );
1115 TEST_ASSERT( key_data != NULL );
1116 hash_data = unhexify_alloc( hash_hex, &hash_size );
1117 TEST_ASSERT( hash_data != NULL );
1118 signature_data = unhexify_alloc( signature_hex, &signature_size );
1119 TEST_ASSERT( signature_data != NULL );
1120
1121 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1122
1123 psa_key_policy_init( &policy );
1124
1125 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg_arg );
1126
1127 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1128
1129 TEST_ASSERT( psa_import_key( slot, key_type,
1130 key_data, key_size ) == PSA_SUCCESS );
1131
1132 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
1133 hash_data, hash_size,
1134 NULL, 0,
1135 signature_data, signature_size ) ==
1136 PSA_SUCCESS );
1137exit:
1138 psa_destroy_key( slot );
1139 mbedtls_free( key_data );
1140 mbedtls_free( hash_data );
1141 mbedtls_free( signature_data );
1142 mbedtls_psa_crypto_free( );
1143}
1144/* END_CASE */
mohammad160391126932018-04-30 11:10:16 +03001145
1146/* BEGIN_CASE */
mohammad1603bdd892a2018-05-09 02:26:51 -07001147void aead_encrypt_decrypt( int key_type_arg, char * key_hex,
mohammad160331585642018-06-07 11:45:03 +03001148 int alg_arg, char * input_hex, char * nonce_hex,
mohammad16034b268502018-06-03 19:01:25 +03001149 char * add_data, int expected_result_arg )
mohammad160391126932018-04-30 11:10:16 +03001150{
1151 int slot = 1;
1152 psa_key_type_t key_type = key_type_arg;
1153 psa_algorithm_t alg = alg_arg;
1154 unsigned char *key_data = NULL;
1155 size_t key_size;
1156 unsigned char *input_data = NULL;
1157 size_t input_size;
1158 unsigned char *output_data = NULL;
mohammad1603d9734722018-05-09 04:59:26 -07001159 size_t output_size = 0;
mohammad1603e7979452018-06-01 04:41:03 -07001160 size_t output_length = 0;
mohammad160391126932018-04-30 11:10:16 +03001161 unsigned char *output_data2 = NULL;
mohammad1603e7979452018-06-01 04:41:03 -07001162 size_t output_length2 = 0;
mohammad160331585642018-06-07 11:45:03 +03001163 uint8_t* nonce;
mohammad160391126932018-04-30 11:10:16 +03001164 size_t nonce_length = 16;
1165 size_t tag_length = 16;
mohammad1603bdd892a2018-05-09 02:26:51 -07001166 unsigned char *additional_data = NULL;
1167 size_t additional_data_length = 0;
mohammad16039b071322018-05-31 03:18:45 -07001168 psa_status_t expected_result = (psa_status_t) expected_result_arg;
mohammad1603f14394b2018-06-04 14:33:19 +03001169 psa_key_policy_t policy = {0};
mohammad160391126932018-04-30 11:10:16 +03001170
1171
1172 key_data = unhexify_alloc( key_hex, &key_size );
1173 TEST_ASSERT( key_data != NULL );
1174 input_data = unhexify_alloc( input_hex, &input_size );
1175 TEST_ASSERT( input_data != NULL );
mohammad1603bdd892a2018-05-09 02:26:51 -07001176 additional_data = unhexify_alloc( add_data, &additional_data_length );
1177 TEST_ASSERT( input_data != NULL );
mohammad1603d9734722018-05-09 04:59:26 -07001178 output_size = input_size + tag_length;
1179 output_data = mbedtls_calloc( 1, output_size );
mohammad160391126932018-04-30 11:10:16 +03001180 TEST_ASSERT( output_data != NULL );
mohammad160331585642018-06-07 11:45:03 +03001181 nonce = unhexify_alloc( nonce_hex, &nonce_length );
1182 TEST_ASSERT( nonce != NULL );
mohammad160391126932018-04-30 11:10:16 +03001183
1184 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1185
mohammad1603f14394b2018-06-04 14:33:19 +03001186 psa_key_policy_init( &policy );
1187
1188 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT , alg );
1189
1190 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1191
mohammad160391126932018-04-30 11:10:16 +03001192 TEST_ASSERT( psa_import_key( slot, key_type,
1193 key_data, key_size ) == PSA_SUCCESS );
1194
1195 TEST_ASSERT( psa_aead_encrypt( slot, alg,
1196 nonce, nonce_length,
1197 additional_data, additional_data_length,
1198 input_data, input_size, output_data,
mohammad16039b071322018-05-31 03:18:45 -07001199 output_size, &output_length ) == expected_result );
mohammad160391126932018-04-30 11:10:16 +03001200
mohammad16039b071322018-05-31 03:18:45 -07001201 if( PSA_SUCCESS == expected_result )
mohammad1603f07db2e2018-05-09 05:41:08 -07001202 {
mohammad1603e7979452018-06-01 04:41:03 -07001203 output_data2 = mbedtls_calloc( 1, output_length );
1204 TEST_ASSERT( output_data2 != NULL );
1205
1206 TEST_ASSERT( psa_aead_decrypt( slot, alg,
1207 nonce, nonce_length,
1208 additional_data, additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02001209 output_data, output_length, output_data2,
mohammad1603e7979452018-06-01 04:41:03 -07001210 output_length, &output_length2 ) == expected_result );
1211
1212
mohammad1603f07db2e2018-05-09 05:41:08 -07001213 TEST_ASSERT( memcmp( input_data, output_data2,
mohammad1603e7979452018-06-01 04:41:03 -07001214 input_size ) == 0 );
mohammad1603f07db2e2018-05-09 05:41:08 -07001215 }
mohammad160391126932018-04-30 11:10:16 +03001216
1217
1218exit:
1219 psa_destroy_key( slot );
1220 mbedtls_free( key_data );
1221 mbedtls_free( input_data );
mohammad1603f07db2e2018-05-09 05:41:08 -07001222 mbedtls_free( additional_data );
1223 mbedtls_free( output_data );
mohammad16039b071322018-05-31 03:18:45 -07001224 mbedtls_free( output_data2 );
mohammad160391126932018-04-30 11:10:16 +03001225 mbedtls_psa_crypto_free( );
1226}
1227/* END_CASE */
mohammad1603f2525eb2018-06-03 19:13:34 +03001228
1229/* BEGIN_CASE */
1230void aead_encrypt( int key_type_arg, char * key_hex,
1231 int alg_arg, char * input_hex,
1232 char * add_data, char * nonce_hex,
1233 char * expected_result_hex )
1234{
1235 int slot = 1;
1236 psa_key_type_t key_type = key_type_arg;
1237 psa_algorithm_t alg = alg_arg;
1238 unsigned char *key_data = NULL;
1239 size_t key_size;
1240 unsigned char *input_data = NULL;
1241 size_t input_size;
1242 unsigned char *output_data = NULL;
1243 size_t output_size = 0;
1244 size_t output_length = 0;
1245 unsigned char *expected_result = NULL;
1246 size_t expected_result_length = 0;
1247 uint8_t* nonce = NULL;
1248 size_t nonce_length = 0;
1249 size_t tag_length = 16;
1250 unsigned char *additional_data = NULL;
1251 size_t additional_data_length = 0;
mohammad1603f14394b2018-06-04 14:33:19 +03001252 psa_key_policy_t policy = {0};
mohammad1603f2525eb2018-06-03 19:13:34 +03001253
1254
1255 key_data = unhexify_alloc( key_hex, &key_size );
1256 TEST_ASSERT( key_data != NULL );
1257 input_data = unhexify_alloc( input_hex, &input_size );
1258 TEST_ASSERT( input_data != NULL );
1259 additional_data = unhexify_alloc( add_data, &additional_data_length );
1260 TEST_ASSERT( input_data != NULL );
1261 output_size = input_size + tag_length;
1262 output_data = mbedtls_calloc( 1, output_size );
1263 TEST_ASSERT( output_data != NULL );
1264 nonce = unhexify_alloc( nonce_hex, &nonce_length );
1265 TEST_ASSERT( nonce != NULL );
1266 expected_result = unhexify_alloc( expected_result_hex, &expected_result_length );
1267 TEST_ASSERT( expected_result != NULL );
1268
1269 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1270
mohammad1603f14394b2018-06-04 14:33:19 +03001271 psa_key_policy_init( &policy );
1272
1273 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
1274
1275 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1276
mohammad1603f2525eb2018-06-03 19:13:34 +03001277 TEST_ASSERT( psa_import_key( slot, key_type,
1278 key_data, key_size ) == PSA_SUCCESS );
1279
1280 TEST_ASSERT( psa_aead_encrypt( slot, alg,
1281 nonce, nonce_length,
1282 additional_data, additional_data_length,
1283 input_data, input_size, output_data,
1284 output_size, &output_length ) == PSA_SUCCESS );
1285
1286
1287 TEST_ASSERT( memcmp( output_data, expected_result,
1288 output_length ) == 0 );
1289
1290
1291exit:
1292 psa_destroy_key( slot );
1293 mbedtls_free( key_data );
1294 mbedtls_free( input_data );
1295 mbedtls_free( additional_data );
1296 mbedtls_free( output_data );
1297 mbedtls_free( nonce );
1298 mbedtls_free( expected_result );
1299 mbedtls_psa_crypto_free( );
1300}
1301/* END_CASE */
mohammad1603371a6e42018-06-04 15:11:08 +03001302
1303/* BEGIN_CASE */
1304void aead_decrypt( int key_type_arg, char * key_hex,
1305 int alg_arg, char * input_hex,
1306 char * add_data, char * nonce_hex,
mohammad1603f7f72da2018-06-04 16:32:11 +03001307 char * expected_result_hex, int expected_result_arg )
mohammad1603371a6e42018-06-04 15:11:08 +03001308{
1309 int slot = 1;
1310 psa_key_type_t key_type = key_type_arg;
1311 psa_algorithm_t alg = alg_arg;
1312 unsigned char *key_data = NULL;
1313 size_t key_size;
1314 unsigned char *input_data = NULL;
1315 size_t input_size;
1316 unsigned char *output_data = NULL;
1317 size_t output_size = 0;
1318 size_t output_length = 0;
mohammad1603f7f72da2018-06-04 16:32:11 +03001319 unsigned char *expected_data = NULL;
mohammad1603371a6e42018-06-04 15:11:08 +03001320 size_t expected_result_length = 0;
1321 uint8_t* nonce = NULL;
1322 size_t nonce_length = 0;
1323 size_t tag_length = 16;
1324 unsigned char *additional_data = NULL;
1325 size_t additional_data_length = 0;
1326 psa_key_policy_t policy = {0};
mohammad1603f7f72da2018-06-04 16:32:11 +03001327 psa_status_t expected_result = (psa_status_t) expected_result_arg;
mohammad1603371a6e42018-06-04 15:11:08 +03001328
1329
1330 key_data = unhexify_alloc( key_hex, &key_size );
1331 TEST_ASSERT( key_data != NULL );
1332 input_data = unhexify_alloc( input_hex, &input_size );
1333 TEST_ASSERT( input_data != NULL );
1334 additional_data = unhexify_alloc( add_data, &additional_data_length );
1335 TEST_ASSERT( input_data != NULL );
1336 output_size = input_size + tag_length;
1337 output_data = mbedtls_calloc( 1, output_size );
1338 TEST_ASSERT( output_data != NULL );
1339 nonce = unhexify_alloc( nonce_hex, &nonce_length );
1340 TEST_ASSERT( nonce != NULL );
mohammad1603f7f72da2018-06-04 16:32:11 +03001341 expected_data = unhexify_alloc( expected_result_hex, &expected_result_length );
1342 TEST_ASSERT( expected_data != NULL );
mohammad1603371a6e42018-06-04 15:11:08 +03001343
1344 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1345
1346 psa_key_policy_init( &policy );
1347
1348 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
1349
1350 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1351
1352 TEST_ASSERT( psa_import_key( slot, key_type,
1353 key_data, key_size ) == PSA_SUCCESS );
1354
1355 TEST_ASSERT( psa_aead_decrypt( slot, alg,
1356 nonce, nonce_length,
1357 additional_data, additional_data_length,
1358 input_data, input_size, output_data,
mohammad1603f7f72da2018-06-04 16:32:11 +03001359 output_size, &output_length ) == expected_result );
mohammad1603371a6e42018-06-04 15:11:08 +03001360
1361
mohammad1603f7f72da2018-06-04 16:32:11 +03001362 if ( expected_result == PSA_SUCCESS )
1363 {
1364 TEST_ASSERT( memcmp( output_data, expected_data,
1365 output_length ) == 0 );
1366 }
1367
mohammad1603371a6e42018-06-04 15:11:08 +03001368
1369
1370exit:
1371 psa_destroy_key( slot );
1372 mbedtls_free( key_data );
1373 mbedtls_free( input_data );
1374 mbedtls_free( additional_data );
1375 mbedtls_free( output_data );
1376 mbedtls_free( nonce );
mohammad1603f7f72da2018-06-04 16:32:11 +03001377 mbedtls_free( expected_data );
mohammad1603371a6e42018-06-04 15:11:08 +03001378 mbedtls_psa_crypto_free( );
1379}
1380/* END_CASE */