blob: 3905e01d47ef86374c68697d41580cd8a9ecbcaf [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 */
Gilles Peskinea1cac842018-06-11 19:33:02 +0200833void aead_encrypt_decrypt( int key_type_arg, char * key_hex,
834 int alg_arg, char * input_hex, char * nonce_hex,
835 char * add_data, int expected_result_arg )
836{
837 int slot = 1;
838 psa_key_type_t key_type = key_type_arg;
839 psa_algorithm_t alg = alg_arg;
840 unsigned char *key_data = NULL;
841 size_t key_size;
842 unsigned char *input_data = NULL;
843 size_t input_size;
844 unsigned char *output_data = NULL;
845 size_t output_size = 0;
846 size_t output_length = 0;
847 unsigned char *output_data2 = NULL;
848 size_t output_length2 = 0;
849 uint8_t* nonce;
850 size_t nonce_length = 16;
851 size_t tag_length = 16;
852 unsigned char *additional_data = NULL;
853 size_t additional_data_length = 0;
854 psa_status_t expected_result = (psa_status_t) expected_result_arg;
855 psa_key_policy_t policy = {0};
856
857
858 key_data = unhexify_alloc( key_hex, &key_size );
859 TEST_ASSERT( key_data != NULL );
860 input_data = unhexify_alloc( input_hex, &input_size );
861 TEST_ASSERT( input_data != NULL );
862 additional_data = unhexify_alloc( add_data, &additional_data_length );
863 TEST_ASSERT( input_data != NULL );
864 output_size = input_size + tag_length;
865 output_data = mbedtls_calloc( 1, output_size );
866 TEST_ASSERT( output_data != NULL );
867 nonce = unhexify_alloc( nonce_hex, &nonce_length );
868 TEST_ASSERT( nonce != NULL );
869
870 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
871
872 psa_key_policy_init( &policy );
873
874 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT , alg );
875
876 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
877
878 TEST_ASSERT( psa_import_key( slot, key_type,
879 key_data, key_size ) == PSA_SUCCESS );
880
881 TEST_ASSERT( psa_aead_encrypt( slot, alg,
882 nonce, nonce_length,
883 additional_data, additional_data_length,
884 input_data, input_size, output_data,
885 output_size, &output_length ) == expected_result );
886
887 if( PSA_SUCCESS == expected_result )
888 {
889 output_data2 = mbedtls_calloc( 1, output_length );
890 TEST_ASSERT( output_data2 != NULL );
891
892 TEST_ASSERT( psa_aead_decrypt( slot, alg,
893 nonce, nonce_length,
894 additional_data, additional_data_length,
895 output_data, output_length, output_data2,
896 output_length, &output_length2 ) == expected_result );
897
898
899 TEST_ASSERT( memcmp( input_data, output_data2,
900 input_size ) == 0 );
901 }
902
903
904exit:
905 psa_destroy_key( slot );
906 mbedtls_free( key_data );
907 mbedtls_free( input_data );
908 mbedtls_free( additional_data );
909 mbedtls_free( output_data );
910 mbedtls_free( output_data2 );
911 mbedtls_psa_crypto_free( );
912}
913/* END_CASE */
914
915/* BEGIN_CASE */
916void aead_encrypt( int key_type_arg, char * key_hex,
917 int alg_arg, char * input_hex,
918 char * add_data, char * nonce_hex,
919 char * expected_result_hex )
920{
921 int slot = 1;
922 psa_key_type_t key_type = key_type_arg;
923 psa_algorithm_t alg = alg_arg;
924 unsigned char *key_data = NULL;
925 size_t key_size;
926 unsigned char *input_data = NULL;
927 size_t input_size;
928 unsigned char *output_data = NULL;
929 size_t output_size = 0;
930 size_t output_length = 0;
931 unsigned char *expected_result = NULL;
932 size_t expected_result_length = 0;
933 uint8_t* nonce = NULL;
934 size_t nonce_length = 0;
935 size_t tag_length = 16;
936 unsigned char *additional_data = NULL;
937 size_t additional_data_length = 0;
938 psa_key_policy_t policy = {0};
939
940
941 key_data = unhexify_alloc( key_hex, &key_size );
942 TEST_ASSERT( key_data != NULL );
943 input_data = unhexify_alloc( input_hex, &input_size );
944 TEST_ASSERT( input_data != NULL );
945 additional_data = unhexify_alloc( add_data, &additional_data_length );
946 TEST_ASSERT( input_data != NULL );
947 output_size = input_size + tag_length;
948 output_data = mbedtls_calloc( 1, output_size );
949 TEST_ASSERT( output_data != NULL );
950 nonce = unhexify_alloc( nonce_hex, &nonce_length );
951 TEST_ASSERT( nonce != NULL );
952 expected_result = unhexify_alloc( expected_result_hex, &expected_result_length );
953 TEST_ASSERT( expected_result != NULL );
954
955 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
956
957 psa_key_policy_init( &policy );
958
959 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
960
961 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
962
963 TEST_ASSERT( psa_import_key( slot, key_type,
964 key_data, key_size ) == PSA_SUCCESS );
965
966 TEST_ASSERT( psa_aead_encrypt( slot, alg,
967 nonce, nonce_length,
968 additional_data, additional_data_length,
969 input_data, input_size, output_data,
970 output_size, &output_length ) == PSA_SUCCESS );
971
972
973 TEST_ASSERT( memcmp( output_data, expected_result,
974 output_length ) == 0 );
975
976
977exit:
978 psa_destroy_key( slot );
979 mbedtls_free( key_data );
980 mbedtls_free( input_data );
981 mbedtls_free( additional_data );
982 mbedtls_free( output_data );
983 mbedtls_free( nonce );
984 mbedtls_free( expected_result );
985 mbedtls_psa_crypto_free( );
986}
987/* END_CASE */
988
989/* BEGIN_CASE */
990void aead_decrypt( int key_type_arg, char * key_hex,
991 int alg_arg, char * input_hex,
992 char * add_data, char * nonce_hex,
993 char * expected_result_hex, int expected_result_arg )
994{
995 int slot = 1;
996 psa_key_type_t key_type = key_type_arg;
997 psa_algorithm_t alg = alg_arg;
998 unsigned char *key_data = NULL;
999 size_t key_size;
1000 unsigned char *input_data = NULL;
1001 size_t input_size;
1002 unsigned char *output_data = NULL;
1003 size_t output_size = 0;
1004 size_t output_length = 0;
1005 unsigned char *expected_data = NULL;
1006 size_t expected_result_length = 0;
1007 uint8_t* nonce = NULL;
1008 size_t nonce_length = 0;
1009 size_t tag_length = 16;
1010 unsigned char *additional_data = NULL;
1011 size_t additional_data_length = 0;
1012 psa_key_policy_t policy = {0};
1013 psa_status_t expected_result = (psa_status_t) expected_result_arg;
1014
1015
1016 key_data = unhexify_alloc( key_hex, &key_size );
1017 TEST_ASSERT( key_data != NULL );
1018 input_data = unhexify_alloc( input_hex, &input_size );
1019 TEST_ASSERT( input_data != NULL );
1020 additional_data = unhexify_alloc( add_data, &additional_data_length );
1021 TEST_ASSERT( input_data != NULL );
1022 output_size = input_size + tag_length;
1023 output_data = mbedtls_calloc( 1, output_size );
1024 TEST_ASSERT( output_data != NULL );
1025 nonce = unhexify_alloc( nonce_hex, &nonce_length );
1026 TEST_ASSERT( nonce != NULL );
1027 expected_data = unhexify_alloc( expected_result_hex, &expected_result_length );
1028 TEST_ASSERT( expected_data != NULL );
1029
1030 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1031
1032 psa_key_policy_init( &policy );
1033
1034 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
1035
1036 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1037
1038 TEST_ASSERT( psa_import_key( slot, key_type,
1039 key_data, key_size ) == PSA_SUCCESS );
1040
1041 TEST_ASSERT( psa_aead_decrypt( slot, alg,
1042 nonce, nonce_length,
1043 additional_data, additional_data_length,
1044 input_data, input_size, output_data,
1045 output_size, &output_length ) == expected_result );
1046
1047
1048 if ( expected_result == PSA_SUCCESS )
1049 {
1050 TEST_ASSERT( memcmp( output_data, expected_data,
1051 output_length ) == 0 );
1052 }
1053
1054
1055
1056exit:
1057 psa_destroy_key( slot );
1058 mbedtls_free( key_data );
1059 mbedtls_free( input_data );
1060 mbedtls_free( additional_data );
1061 mbedtls_free( output_data );
1062 mbedtls_free( nonce );
1063 mbedtls_free( expected_data );
1064 mbedtls_psa_crypto_free( );
1065}
1066/* END_CASE */
1067
1068/* BEGIN_CASE */
Gilles Peskinee59236f2018-01-27 23:32:46 +01001069void signature_size( int type_arg, int bits, int alg_arg, int expected_size_arg )
1070{
1071 psa_key_type_t type = type_arg;
1072 psa_algorithm_t alg = alg_arg;
1073 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(type, bits, alg);
1074 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
1075exit:
1076 ;
1077}
1078/* END_CASE */
1079
1080/* BEGIN_CASE */
1081void sign_deterministic( int key_type_arg, char *key_hex,
1082 int alg_arg, char *input_hex, char *output_hex )
1083{
1084 int slot = 1;
1085 psa_key_type_t key_type = key_type_arg;
1086 psa_algorithm_t alg = alg_arg;
1087 unsigned char *key_data = NULL;
1088 size_t key_size;
Gilles Peskine20035e32018-02-03 22:44:14 +01001089 size_t key_bits;
1090 unsigned char *input_data = NULL;
1091 size_t input_size;
1092 unsigned char *output_data = NULL;
1093 size_t output_size;
1094 unsigned char *signature = NULL;
1095 size_t signature_size;
1096 size_t signature_length = 0xdeadbeef;
mohammad1603a97cb8c2018-03-28 03:46:26 -07001097 psa_key_policy_t policy = {0};
Gilles Peskine20035e32018-02-03 22:44:14 +01001098
Gilles Peskine40f68b92018-03-07 16:43:36 +01001099 key_data = unhexify_alloc( key_hex, &key_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01001100 TEST_ASSERT( key_data != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +01001101 input_data = unhexify_alloc( input_hex, &input_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01001102 TEST_ASSERT( input_data != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +01001103 output_data = unhexify_alloc( output_hex, &output_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01001104 TEST_ASSERT( output_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001105
1106 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1107
mohammad1603a97cb8c2018-03-28 03:46:26 -07001108 psa_key_policy_init( &policy );
1109
1110 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg );
1111
1112 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1113
Gilles Peskine20035e32018-02-03 22:44:14 +01001114 TEST_ASSERT( psa_import_key( slot, key_type,
1115 key_data, key_size ) == PSA_SUCCESS );
1116 TEST_ASSERT( psa_get_key_information( slot,
1117 NULL,
1118 &key_bits ) == PSA_SUCCESS );
1119
1120 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, alg, key_bits );
1121 TEST_ASSERT( signature_size != 0 );
1122 signature = mbedtls_calloc( 1, signature_size );
1123 TEST_ASSERT( signature != NULL );
1124
1125 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
1126 input_data, input_size,
1127 NULL, 0,
1128 signature, signature_size,
1129 &signature_length ) == PSA_SUCCESS );
1130 TEST_ASSERT( signature_length == output_size );
1131 TEST_ASSERT( memcmp( signature, output_data, output_size ) == 0 );
1132
1133exit:
1134 psa_destroy_key( slot );
1135 mbedtls_free( key_data );
1136 mbedtls_free( input_data );
1137 mbedtls_free( output_data );
Gilles Peskine0189e752018-02-03 23:57:22 +01001138 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01001139 mbedtls_psa_crypto_free( );
1140}
1141/* END_CASE */
1142
1143/* BEGIN_CASE */
1144void sign_fail( int key_type_arg, char *key_hex,
1145 int alg_arg, char *input_hex,
1146 int signature_size, int expected_status_arg )
1147{
1148 int slot = 1;
1149 psa_key_type_t key_type = key_type_arg;
1150 psa_algorithm_t alg = alg_arg;
1151 unsigned char *key_data = NULL;
1152 size_t key_size;
1153 unsigned char *input_data = NULL;
1154 size_t input_size;
1155 psa_status_t actual_status;
1156 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01001157 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01001158 size_t signature_length = 0xdeadbeef;
mohammad1603a97cb8c2018-03-28 03:46:26 -07001159 psa_key_policy_t policy = {0};
Gilles Peskine20035e32018-02-03 22:44:14 +01001160
Gilles Peskine40f68b92018-03-07 16:43:36 +01001161 key_data = unhexify_alloc( key_hex, &key_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01001162 TEST_ASSERT( key_data != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +01001163 input_data = unhexify_alloc( input_hex, &input_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01001164 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001165 signature = mbedtls_calloc( 1, signature_size );
1166 TEST_ASSERT( signature != NULL );
1167
1168 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1169
mohammad1603a97cb8c2018-03-28 03:46:26 -07001170 psa_key_policy_init( &policy );
1171
1172 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg );
1173
1174 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1175
Gilles Peskine20035e32018-02-03 22:44:14 +01001176 TEST_ASSERT( psa_import_key( slot, key_type,
1177 key_data, key_size ) == PSA_SUCCESS );
1178
1179 actual_status = psa_asymmetric_sign( slot, alg,
1180 input_data, input_size,
1181 NULL, 0,
1182 signature, signature_size,
1183 &signature_length );
1184 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine93aa0332018-02-03 23:58:03 +01001185 TEST_ASSERT( signature_length == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01001186
1187exit:
1188 psa_destroy_key( slot );
1189 mbedtls_free( key_data );
1190 mbedtls_free( input_data );
1191 mbedtls_free( signature );
1192 mbedtls_psa_crypto_free( );
1193}
1194/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03001195
1196/* BEGIN_CASE */
1197void key_policy( int usage_arg, int alg_arg )
1198{
1199 int key_slot = 1;
mohammad16034eed7572018-03-28 05:14:59 -07001200 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
mohammad16038cc1cee2018-03-28 01:21:33 +03001201 unsigned char key[32] = {0};
1202 psa_key_policy_t policy_set = {0};
1203 psa_key_policy_t policy_get = {0};
1204
1205 memset( key, 0x2a, sizeof( key ) );
1206
1207 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1208
1209 psa_key_policy_init(& policy_set );
1210 psa_key_policy_init(& policy_get );
1211
1212 psa_key_policy_set_usage( &policy_set, usage_arg, alg_arg );
1213
1214 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == ( psa_key_usage_t )usage_arg );
1215
1216 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set) == ( psa_algorithm_t )alg_arg );
1217
1218 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
1219
1220 TEST_ASSERT( psa_import_key( key_slot, key_type,
1221 key, sizeof( key ) ) == PSA_SUCCESS );
1222
1223 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
1224
1225 TEST_ASSERT( policy_get.usage == policy_set.usage );
1226 TEST_ASSERT( policy_get.alg == policy_set.alg );
1227
1228exit:
1229 psa_destroy_key( key_slot );
1230 mbedtls_psa_crypto_free( );
1231}
1232/* END_CASE */
1233
mohammad16034eed7572018-03-28 05:14:59 -07001234/* BEGIN_CASE */
1235void key_policy_fail( int usage_arg, int alg_arg, int expected_status, char *key_hex )
1236{
1237 int key_slot = 1;
mohammad16034eed7572018-03-28 05:14:59 -07001238 unsigned char* keypair = NULL;
1239 size_t key_size = 0;
1240 size_t signature_length = 0;
1241 psa_key_policy_t policy = {0};
1242 int actual_status = PSA_SUCCESS;
mohammad16034eed7572018-03-28 05:14:59 -07001243
1244 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1245
1246 psa_key_policy_init( &policy );
1247
1248 psa_key_policy_set_usage( &policy, usage_arg, alg_arg );
1249
1250 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1251
mohammad16036df908f2018-04-02 08:34:15 -07001252 if( usage_arg & PSA_KEY_USAGE_EXPORT )
mohammad16034eed7572018-03-28 05:14:59 -07001253 {
mohammad16036df908f2018-04-02 08:34:15 -07001254 keypair = unhexify_alloc( key_hex, &key_size );
1255 TEST_ASSERT( keypair != NULL );
1256 TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR,
1257 keypair, key_size ) == PSA_SUCCESS );
1258 actual_status = psa_asymmetric_sign( key_slot,
1259 ( psa_algorithm_t )alg_arg, NULL, 0, NULL, 0,
1260 NULL, 0, &signature_length );
1261 }
1262
1263 if( usage_arg & PSA_KEY_USAGE_SIGN )
1264 {
mohammad1603d926b882018-04-16 01:53:20 -07001265 keypair = unhexify_alloc( key_hex, &key_size );
1266 TEST_ASSERT( keypair != NULL );
mohammad16036df908f2018-04-02 08:34:15 -07001267 TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR,
mohammad1603d926b882018-04-16 01:53:20 -07001268 keypair, key_size ) == PSA_SUCCESS );
mohammad16036df908f2018-04-02 08:34:15 -07001269 actual_status = psa_export_key( key_slot, NULL, 0, NULL );
mohammad16034eed7572018-03-28 05:14:59 -07001270 }
1271
1272 TEST_ASSERT( actual_status == expected_status );
1273
1274exit:
1275 psa_destroy_key( key_slot );
mohammad1603d926b882018-04-16 01:53:20 -07001276 mbedtls_free( keypair );
mohammad16034eed7572018-03-28 05:14:59 -07001277 mbedtls_psa_crypto_free( );
1278}
1279/* END_CASE */
Gilles Peskinea0655c32018-04-30 17:06:50 +02001280
1281/* BEGIN_CASE */
1282void key_lifetime( int lifetime_arg )
1283{
1284 int key_slot = 1;
1285 psa_key_type_t key_type = PSA_ALG_CBC_BASE;
1286 unsigned char key[32] = {0};
1287 psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg;
1288 psa_key_lifetime_t lifetime_get;
Gilles Peskine7268afc2018-06-06 15:19:24 +02001289
Gilles Peskinea0655c32018-04-30 17:06:50 +02001290 memset( key, 0x2a, sizeof( key ) );
Gilles Peskine7268afc2018-06-06 15:19:24 +02001291
Gilles Peskinea0655c32018-04-30 17:06:50 +02001292 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
Gilles Peskine7268afc2018-06-06 15:19:24 +02001293
Gilles Peskinea0655c32018-04-30 17:06:50 +02001294 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1295 lifetime_set ) == PSA_SUCCESS );
Gilles Peskine7268afc2018-06-06 15:19:24 +02001296
Gilles Peskinea0655c32018-04-30 17:06:50 +02001297 TEST_ASSERT( psa_import_key( key_slot, key_type,
1298 key, sizeof( key ) ) == PSA_SUCCESS );
Gilles Peskine7268afc2018-06-06 15:19:24 +02001299
Gilles Peskinea0655c32018-04-30 17:06:50 +02001300 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1301 &lifetime_get ) == PSA_SUCCESS );
Gilles Peskine7268afc2018-06-06 15:19:24 +02001302
Gilles Peskinea0655c32018-04-30 17:06:50 +02001303 TEST_ASSERT( lifetime_get == lifetime_set );
Gilles Peskine7268afc2018-06-06 15:19:24 +02001304
Gilles Peskinea0655c32018-04-30 17:06:50 +02001305exit:
1306 psa_destroy_key( key_slot );
1307 mbedtls_psa_crypto_free( );
1308}
1309/* END_CASE */
mohammad1603804cd712018-03-20 22:44:08 +02001310
Gilles Peskine7268afc2018-06-06 15:19:24 +02001311
mohammad1603804cd712018-03-20 22:44:08 +02001312/* BEGIN_CASE */
1313void key_lifetime_set_fail( int key_slot_arg, int lifetime_arg, int expected_status_arg )
1314{
1315 int key_slot = 1;
mohammad1603804cd712018-03-20 22:44:08 +02001316 psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg;
1317 psa_status_t actual_status;
1318 psa_status_t expected_status = expected_status_arg;
1319
mohammad1603804cd712018-03-20 22:44:08 +02001320 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1321
mohammad1603804cd712018-03-20 22:44:08 +02001322 actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set );
1323
1324 if( actual_status == PSA_SUCCESS )
1325 actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set );
1326
1327 TEST_ASSERT( expected_status == actual_status );
1328
1329exit:
mohammad1603804cd712018-03-20 22:44:08 +02001330 psa_destroy_key( key_slot );
1331 mbedtls_psa_crypto_free( );
1332}
1333/* END_CASE */
itayzafrir5c753392018-05-08 11:18:38 +03001334
1335/* BEGIN_CASE */
1336void asymmetric_verify( int key_type_arg, char *key_hex,
1337 int alg_arg, char *hash_hex, char *signature_hex )
1338{
1339 int slot = 1;
1340 psa_key_type_t key_type = key_type_arg;
1341 psa_algorithm_t alg = alg_arg;
1342 unsigned char *key_data = NULL;
1343 size_t key_size;
1344 unsigned char *hash_data = NULL;
1345 size_t hash_size;
1346 unsigned char *signature_data = NULL;
1347 size_t signature_size;
1348 psa_key_policy_t policy = {0};
1349
1350 key_data = unhexify_alloc( key_hex, &key_size );
1351 TEST_ASSERT( key_data != NULL );
1352 hash_data = unhexify_alloc( hash_hex, &hash_size );
1353 TEST_ASSERT( hash_data != NULL );
1354 signature_data = unhexify_alloc( signature_hex, &signature_size );
1355 TEST_ASSERT( signature_data != NULL );
1356
1357 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1358
1359 psa_key_policy_init( &policy );
1360
1361 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg_arg );
1362
1363 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1364
1365 TEST_ASSERT( psa_import_key( slot, key_type,
1366 key_data, key_size ) == PSA_SUCCESS );
1367
1368 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
1369 hash_data, hash_size,
1370 NULL, 0,
1371 signature_data, signature_size ) ==
1372 PSA_SUCCESS );
1373exit:
1374 psa_destroy_key( slot );
1375 mbedtls_free( key_data );
1376 mbedtls_free( hash_data );
1377 mbedtls_free( signature_data );
1378 mbedtls_psa_crypto_free( );
1379}
1380/* END_CASE */