blob: 8d16c6fbf28006e7f5ce5caee6f40db3824d3f04 [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 */
52void import_export( char *hex, int type_arg,
53 int expected_bits,
54 int export_size_delta,
55 int expected_export_status,
56 int canonical_input )
57{
58 int slot = 1;
59 int slot2 = slot + 1;
60 psa_key_type_t type = type_arg;
61 psa_status_t status;
62 unsigned char *data = NULL;
63 unsigned char *exported = NULL;
64 unsigned char *reexported = NULL;
65 size_t data_size;
66 size_t export_size;
67 size_t exported_length;
68 size_t reexported_length;
69 psa_key_type_t got_type;
70 size_t got_bits;
mohammad1603a97cb8c2018-03-28 03:46:26 -070071 psa_key_policy_t policy = {0};
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010072
Gilles Peskine40f68b92018-03-07 16:43:36 +010073 data = unhexify_alloc( hex, &data_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010074 TEST_ASSERT( data != NULL );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075 export_size = (ssize_t) data_size + export_size_delta;
76 exported = mbedtls_calloc( 1, export_size );
77 TEST_ASSERT( exported != NULL );
78 if( ! canonical_input )
79 {
80 reexported = mbedtls_calloc( 1, export_size );
81 TEST_ASSERT( reexported != NULL );
82 }
83 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
84
mohammad1603a97cb8c2018-03-28 03:46:26 -070085 psa_key_policy_init( &policy );
86
87 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT,
88 PSA_ALG_VENDOR_FLAG );
89
90 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
91
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010092 /* Import the key */
93 TEST_ASSERT( psa_import_key( slot, type,
94 data, data_size ) == PSA_SUCCESS );
95
96 /* Test the key information */
97 TEST_ASSERT( psa_get_key_information( slot,
98 &got_type, &got_bits ) ==
99 PSA_SUCCESS );
100 TEST_ASSERT( got_type == type );
101 TEST_ASSERT( got_bits == (size_t) expected_bits );
102
103 /* Export the key */
104 status = psa_export_key( slot,
105 exported, export_size,
106 &exported_length );
107 TEST_ASSERT( status == (psa_status_t) expected_export_status );
108 if( status != PSA_SUCCESS )
109 goto destroy;
110
111 if( canonical_input )
112 {
113 TEST_ASSERT( exported_length == data_size );
114 TEST_ASSERT( memcmp( exported, data, data_size ) == 0 );
115 }
116 else
117 {
mohammad1603a97cb8c2018-03-28 03:46:26 -0700118 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
119
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100120 TEST_ASSERT( psa_import_key( slot2, type,
121 exported, export_size ) ==
122 PSA_SUCCESS );
123 TEST_ASSERT( psa_export_key( slot2,
124 reexported, export_size,
125 &reexported_length ) ==
126 PSA_SUCCESS );
127 TEST_ASSERT( reexported_length == exported_length );
128 TEST_ASSERT( memcmp( reexported, exported,
129 exported_length ) == 0 );
130 }
131
132destroy:
133 /* Destroy the key */
134 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
135 TEST_ASSERT( psa_get_key_information(
136 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
137
138exit:
139 mbedtls_free( data );
140 mbedtls_psa_crypto_free( );
141}
142/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100143
144/* BEGIN_CASE */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100145void hash_finish( int alg_arg, char *input_hex, char *hash_hex )
146{
147 psa_algorithm_t alg = alg_arg;
148 unsigned char *input = NULL;
149 size_t input_size;
150 unsigned char expected_hash[MBEDTLS_MD_MAX_SIZE];
151 size_t expected_hash_length;
152 unsigned char actual_hash[MBEDTLS_MD_MAX_SIZE];
153 size_t actual_hash_length;
154 psa_hash_operation_t operation;
155
Gilles Peskine40f68b92018-03-07 16:43:36 +0100156 input = unhexify_alloc( input_hex, &input_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100157 TEST_ASSERT( input != NULL );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100158 expected_hash_length = unhexify( expected_hash, hash_hex );
159
160 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
161
162 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
163 TEST_ASSERT( psa_hash_update( &operation,
164 input, input_size ) == PSA_SUCCESS );
165 TEST_ASSERT( psa_hash_finish( &operation,
166 actual_hash, sizeof( actual_hash ),
167 &actual_hash_length ) == PSA_SUCCESS );
168 TEST_ASSERT( actual_hash_length == expected_hash_length );
169 TEST_ASSERT( memcmp( expected_hash, actual_hash,
170 expected_hash_length ) == 0 );
171
172exit:
173 mbedtls_free( input );
174 mbedtls_psa_crypto_free( );
175}
176/* END_CASE */
177
178/* BEGIN_CASE */
179void hash_verify( int alg_arg, char *input_hex, char *hash_hex )
180{
181 psa_algorithm_t alg = alg_arg;
182 unsigned char *input = NULL;
183 size_t input_size;
184 unsigned char expected_hash[MBEDTLS_MD_MAX_SIZE];
185 size_t expected_hash_length;
186 psa_hash_operation_t operation;
187
Gilles Peskine40f68b92018-03-07 16:43:36 +0100188 input = unhexify_alloc( input_hex, &input_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100189 TEST_ASSERT( input != NULL );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100190 expected_hash_length = unhexify( expected_hash, hash_hex );
191
192 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
193
194 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
195 TEST_ASSERT( psa_hash_update( &operation,
196 input, input_size ) == PSA_SUCCESS );
197 TEST_ASSERT( psa_hash_verify( &operation,
198 expected_hash,
199 expected_hash_length ) == PSA_SUCCESS );
200
201exit:
202 mbedtls_free( input );
203 mbedtls_psa_crypto_free( );
204}
205/* END_CASE */
206
207/* BEGIN_CASE */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100208void mac_verify( int key_type_arg, char *key_hex,
209 int alg_arg, char *iv_hex,
210 char *input_hex, char *mac_hex )
211{
212 int key_slot = 1;
213 psa_key_type_t key_type = key_type_arg;
214 psa_algorithm_t alg = alg_arg;
215 unsigned char *key = NULL;
216 size_t key_size;
217 unsigned char *iv = NULL;
218 size_t iv_size;
219 unsigned char *input = NULL;
220 size_t input_size;
221 unsigned char *expected_mac = NULL;
222 size_t expected_mac_size;
223 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -0700224 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100225
Gilles Peskine40f68b92018-03-07 16:43:36 +0100226 key = unhexify_alloc( key_hex, &key_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100227 TEST_ASSERT( key != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +0100228 if( iv_hex[0] != 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +0100229 {
Gilles Peskine40f68b92018-03-07 16:43:36 +0100230 iv = unhexify_alloc( iv_hex, &iv_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100231 TEST_ASSERT( iv != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100232 }
Gilles Peskine40f68b92018-03-07 16:43:36 +0100233 input = unhexify_alloc( input_hex, &input_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100234 TEST_ASSERT( input != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +0100235 expected_mac = unhexify_alloc( mac_hex, &expected_mac_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100236 TEST_ASSERT( expected_mac != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100237
238 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
239
mohammad16036df908f2018-04-02 08:34:15 -0700240 psa_key_policy_init( &policy );
241
242 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg_arg );
243
244 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
245
Gilles Peskine8c9def32018-02-08 10:02:12 +0100246 TEST_ASSERT( psa_import_key( key_slot, key_type,
247 key, key_size ) == PSA_SUCCESS );
248 // TODO: support IV
249 TEST_ASSERT( psa_mac_start( &operation, key_slot, alg ) == PSA_SUCCESS );
250 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
251 TEST_ASSERT( psa_mac_update( &operation,
252 input, input_size ) == PSA_SUCCESS );
253 TEST_ASSERT( psa_mac_verify( &operation,
254 expected_mac,
255 expected_mac_size ) == PSA_SUCCESS );
256
257exit:
258 mbedtls_free( key );
259 mbedtls_free( iv );
260 mbedtls_free( input );
261 mbedtls_free( expected_mac );
262 psa_destroy_key( key_slot );
263 mbedtls_psa_crypto_free( );
264}
265/* END_CASE */
266
267/* BEGIN_CASE */
Moran Pekerded84402018-06-06 16:36:50 +0300268void cipher_test_encrypt( int alg_arg, int key_type_arg,
269 char *key_hex,
270 char *input_hex, char *output_hex,
271 int expected_status )
Moran Peker96cc00a2018-05-31 14:03:56 +0300272{
273 int key_slot = 1;
Moran Pekerded84402018-06-06 16:36:50 +0300274 psa_status_t status;
Moran Peker96cc00a2018-05-31 14:03:56 +0300275 psa_key_type_t key_type = key_type_arg;
276 psa_algorithm_t alg = alg_arg;
277 unsigned char *key = NULL;
278 size_t key_size;
279 unsigned char iv[16] = {0};
280 unsigned char *input = NULL;
281 size_t input_size = 0;
282 unsigned char *output;
283 unsigned char *expected_output;
Moran Pekerded84402018-06-06 16:36:50 +0300284 size_t output_size, output_size_1 = 0;
Moran Peker96cc00a2018-05-31 14:03:56 +0300285 size_t output_length = 0;
286 psa_cipher_operation_t operation;
287
288
289 key = unhexify_alloc( key_hex, &key_size );
290 TEST_ASSERT( key != NULL );
291
292 input = unhexify_alloc( input_hex, &input_size );
293 TEST_ASSERT( input != NULL );
294
295 expected_output = unhexify_alloc( output_hex, &output_size );
296 TEST_ASSERT( expected_output != NULL );
297
298 memset( iv, 0x2a, sizeof( iv ) );
Moran Pekerded84402018-06-06 16:36:50 +0300299
Moran Peker96cc00a2018-05-31 14:03:56 +0300300 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
301
302 TEST_ASSERT( psa_import_key( key_slot, key_type,
303 key, key_size ) == PSA_SUCCESS );
304
305 TEST_ASSERT( psa_encrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS );
306
307 TEST_ASSERT( psa_encrypt_set_iv( &operation, iv,
308 sizeof( iv ) ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300309 output_size_1 = input_size + operation.block_size;
310 output = mbedtls_calloc(1, output_size_1);
Moran Peker96cc00a2018-05-31 14:03:56 +0300311
312 TEST_ASSERT( psa_cipher_update( &operation, input, input_size,
Moran Pekerded84402018-06-06 16:36:50 +0300313 output, output_size_1,
Moran Peker96cc00a2018-05-31 14:03:56 +0300314 &output_length) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300315 status = psa_cipher_finish( &operation, output + output_length,
316 output_size_1, &output_length);
317 TEST_ASSERT( status == (psa_status_t) expected_status );
318 if( expected_status == PSA_SUCCESS )
319 {
320 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Moran Peker96cc00a2018-05-31 14:03:56 +0300321
Moran Pekerded84402018-06-06 16:36:50 +0300322 TEST_ASSERT( input_size == output_size );
323 TEST_ASSERT( memcmp( expected_output, output, output_size ) == 0 );
324 }
Moran Peker96cc00a2018-05-31 14:03:56 +0300325exit:
326 mbedtls_free( key );
327 mbedtls_free( input );
328 psa_destroy_key( key_slot );
329 mbedtls_psa_crypto_free( );
330}
331/* END_CASE */
mohammad1603b152d4d2018-04-11 23:51:20 -0700332
333/* BEGIN_CASE */
Moran Pekerded84402018-06-06 16:36:50 +0300334void cipher_test_encrypt_multipart( int alg_arg, int key_type_arg,
Moran Peker7691fb72018-05-31 16:15:31 +0300335 char *key_hex,
336 char *input_hex,
337 int first_part_size, char *output_hex )
338{
339 int key_slot = 1;
340 psa_key_type_t key_type = key_type_arg;
341 psa_algorithm_t alg = alg_arg;
342 unsigned char *key = NULL;
343 size_t key_size;
344 unsigned char iv[16] = {0};
345 unsigned char *input = NULL;
346 size_t input_size = 0;
347 unsigned char *output;
348 unsigned char *expected_output;
Moran Pekerded84402018-06-06 16:36:50 +0300349 size_t output_size, output_size_1 = 0;
Moran Peker7691fb72018-05-31 16:15:31 +0300350 size_t output_length = 0;
351 psa_cipher_operation_t operation;
352
353
354 key = unhexify_alloc( key_hex, &key_size );
355 TEST_ASSERT( key != NULL );
356
357 input = unhexify_alloc( input_hex, &input_size );
358 TEST_ASSERT( input != NULL );
359
360 expected_output = unhexify_alloc( output_hex, &output_size );
361 TEST_ASSERT( expected_output != NULL );
362
363 memset( iv, 0x2a, sizeof( iv ) );
Moran Pekerded84402018-06-06 16:36:50 +0300364
Moran Peker7691fb72018-05-31 16:15:31 +0300365 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
366
367 TEST_ASSERT( psa_import_key( key_slot, key_type,
368 key, key_size ) == PSA_SUCCESS );
369
370 TEST_ASSERT( psa_encrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS );
371
372 TEST_ASSERT( psa_encrypt_set_iv( &operation, iv,
Moran Pekerded84402018-06-06 16:36:50 +0300373 sizeof( iv ) ) == PSA_SUCCESS );
374 output_size_1 = input_size + operation.block_size;
375 output = mbedtls_calloc(1, output_size_1);
Moran Peker7691fb72018-05-31 16:15:31 +0300376
377 TEST_ASSERT( (unsigned int)first_part_size < input_size );
378 TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size,
Moran Pekerded84402018-06-06 16:36:50 +0300379 output, output_size_1,
Moran Peker7691fb72018-05-31 16:15:31 +0300380 &output_length) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300381 TEST_ASSERT( psa_cipher_update( &operation, input + first_part_size, input_size - first_part_size,
382 output, output_size_1,
Moran Peker7691fb72018-05-31 16:15:31 +0300383 &output_length) == PSA_SUCCESS );
384 TEST_ASSERT( psa_cipher_finish( &operation, output + output_length,
Moran Pekerded84402018-06-06 16:36:50 +0300385 output_size_1, &output_length) == PSA_SUCCESS );
Moran Peker7691fb72018-05-31 16:15:31 +0300386
387 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
388
389 TEST_ASSERT( input_size == output_size );
390 TEST_ASSERT( memcmp( expected_output, output, output_size ) == 0 );
391
392exit:
393 mbedtls_free( key );
394 mbedtls_free( input );
395 psa_destroy_key( key_slot );
396 mbedtls_psa_crypto_free( );
397}
398/* END_CASE */
399
400/* BEGIN_CASE */
Moran Pekerded84402018-06-06 16:36:50 +0300401void cipher_test_decrypt_multipart( int alg_arg, int key_type_arg,
mohammad1603b152d4d2018-04-11 23:51:20 -0700402 char *key_hex,
Moran Pekerded84402018-06-06 16:36:50 +0300403 char *input_hex,
404 int first_part_size, char *output_hex)
mohammad1603b152d4d2018-04-11 23:51:20 -0700405{
406 int key_slot = 1;
Moran Pekerded84402018-06-06 16:36:50 +0300407
mohammad1603b152d4d2018-04-11 23:51:20 -0700408 psa_key_type_t key_type = key_type_arg;
409 psa_algorithm_t alg = alg_arg;
410 unsigned char *key = NULL;
411 size_t key_size;
412 unsigned char iv[16] = {0};
413 unsigned char *input = NULL;
414 size_t input_size = 0;
415 unsigned char *output;
416 unsigned char *expected_output;
Moran Pekerded84402018-06-06 16:36:50 +0300417 size_t output_size, output_size_1 = 0;
mohammad1603b152d4d2018-04-11 23:51:20 -0700418 size_t output_length = 0;
419 psa_cipher_operation_t operation;
420
421
422 key = unhexify_alloc( key_hex, &key_size );
423 TEST_ASSERT( key != NULL );
424
425 input = unhexify_alloc( input_hex, &input_size );
426 TEST_ASSERT( input != NULL );
427
428 expected_output = unhexify_alloc( output_hex, &output_size );
429 TEST_ASSERT( expected_output != NULL );
430
431 memset( iv, 0x2a, sizeof( iv ) );
Moran Pekerded84402018-06-06 16:36:50 +0300432
mohammad1603b152d4d2018-04-11 23:51:20 -0700433 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
434
435 TEST_ASSERT( psa_import_key( key_slot, key_type,
436 key, key_size ) == PSA_SUCCESS );
437
438 TEST_ASSERT( psa_decrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS );
439
440 TEST_ASSERT( psa_encrypt_set_iv( &operation, iv,
441 sizeof( iv ) ) == PSA_SUCCESS );
442
Moran Pekerded84402018-06-06 16:36:50 +0300443 output_size_1 = input_size + operation.block_size;
444 output = mbedtls_calloc(1, output_size_1);
mohammad1603b152d4d2018-04-11 23:51:20 -0700445
Moran Pekerded84402018-06-06 16:36:50 +0300446 TEST_ASSERT( (unsigned int)first_part_size < input_size );
447 TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size,
448 output, output_size_1,
449 &output_length) == PSA_SUCCESS );
450 TEST_ASSERT( psa_cipher_update( &operation, input + first_part_size, input_size - first_part_size,
451 output, output_size_1,
mohammad1603b152d4d2018-04-11 23:51:20 -0700452 &output_length) == PSA_SUCCESS );
Moran Pekerf55e8042018-05-29 16:28:28 +0300453 TEST_ASSERT( psa_cipher_finish( &operation, output + output_length,
Moran Pekerded84402018-06-06 16:36:50 +0300454 output_size_1, &output_length) == PSA_SUCCESS );
mohammad1603b152d4d2018-04-11 23:51:20 -0700455 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
456
457 TEST_ASSERT( input_size == output_size );
458 TEST_ASSERT( memcmp( expected_output, output, output_size ) == 0 );
459
460exit:
461 mbedtls_free( key );
462 mbedtls_free( input );
463 psa_destroy_key( key_slot );
464 mbedtls_psa_crypto_free( );
465}
466/* END_CASE */
467
468
mohammad1603d7d7ba52018-03-12 18:51:53 +0200469/* BEGIN_CASE */
Moran Pekerded84402018-06-06 16:36:50 +0300470void cipher_test_decrypt( int alg_arg, int key_type_arg,
471 char *key_hex,
472 char *input_hex, char *output_hex,
473 int expected_status )
474{
475 int key_slot = 1;
476 psa_status_t status;
477 psa_key_type_t key_type = key_type_arg;
478 psa_algorithm_t alg = alg_arg;
479 unsigned char *key = NULL;
480 size_t key_size;
481 unsigned char iv[16] = {0};
482 unsigned char *input = NULL;
483 size_t input_size = 0;
484 unsigned char *output;
485 unsigned char *expected_output;
486 size_t output_size, output_size_1 = 0;
487 size_t output_length = 0;
488 psa_cipher_operation_t operation;
489
490
491 key = unhexify_alloc( key_hex, &key_size );
492 TEST_ASSERT( key != NULL );
493
494 input = unhexify_alloc( input_hex, &input_size );
495 TEST_ASSERT( input != NULL );
496
497 expected_output = unhexify_alloc( output_hex, &output_size );
498 TEST_ASSERT( expected_output != NULL );
499
500 memset( iv, 0x2a, sizeof( iv ) );
501
502 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
503
504 TEST_ASSERT( psa_import_key( key_slot, key_type,
505 key, key_size ) == PSA_SUCCESS );
506
507 TEST_ASSERT( psa_decrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS );
508
509 TEST_ASSERT( psa_encrypt_set_iv( &operation, iv,
510 sizeof( iv ) ) == PSA_SUCCESS );
511
512 output_size_1 = input_size + operation.block_size;
513 output = mbedtls_calloc(1, output_size);
514
515 TEST_ASSERT( psa_cipher_update( &operation, input, input_size,
516 output, output_size_1,
517 &output_length) == PSA_SUCCESS );
518 status = psa_cipher_finish( &operation, output + output_length,
519 output_size_1, &output_length);
520 TEST_ASSERT( status == (psa_status_t) expected_status );
521
522 if( expected_status == PSA_SUCCESS )
523 {
524 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
525
526 TEST_ASSERT( input_size == output_size );
527 TEST_ASSERT( memcmp( expected_output, output, output_size ) == 0 );
528 }
529
530
531exit:
532 mbedtls_free( key );
533 mbedtls_free( input );
534 psa_destroy_key( key_slot );
535 mbedtls_psa_crypto_free( );
536}
537/* END_CASE */
538
539
540/* BEGIN_CASE */
541void cipher_test_verify_output( int alg_arg, int key_type_arg,
mohammad1603d7d7ba52018-03-12 18:51:53 +0200542 char *key_hex,
543 char *input_hex )
544{
545 int key_slot = 1;
546 psa_key_type_t key_type = key_type_arg;
547 psa_algorithm_t alg = alg_arg;
548 unsigned char *key = NULL;
549 size_t key_size;
mohammad1603e6b67a12018-03-12 10:38:49 -0700550 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +0200551 size_t iv_size = 16;
552 size_t iv_length = 0;
553 unsigned char *input = NULL;
554 size_t input_size = 0;
Moran Peker3205a652018-03-20 17:09:59 +0200555 unsigned char *output1;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200556 size_t output1_size = 0;
557 size_t output1_length = 0;
Moran Peker3205a652018-03-20 17:09:59 +0200558 unsigned char *output2;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200559 size_t output2_size = 0;
560 size_t output2_length = 0;
561 size_t tmp_output_length = 0;
562 psa_cipher_operation_t operation1;
563 psa_cipher_operation_t operation2;
564
565 key = unhexify_alloc( key_hex, &key_size );
566 TEST_ASSERT( key != NULL );
567
568 input = unhexify_alloc( input_hex, &input_size );
569 TEST_ASSERT( input != NULL );
Moran Pekerded84402018-06-06 16:36:50 +0300570
mohammad1603d7d7ba52018-03-12 18:51:53 +0200571 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
572
573 TEST_ASSERT( psa_import_key( key_slot, key_type,
574 key, key_size ) == PSA_SUCCESS );
575
Moran Pekerf55e8042018-05-29 16:28:28 +0300576 TEST_ASSERT( psa_encrypt_setup( &operation1, key_slot, alg ) == PSA_SUCCESS );
577 TEST_ASSERT( psa_decrypt_setup( &operation2, key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200578
mohammad16038481e742018-03-18 13:57:31 +0200579 TEST_ASSERT( psa_encrypt_generate_iv( &operation1, iv,
mohammad1603d7d7ba52018-03-12 18:51:53 +0200580 iv_size, &iv_length) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300581 output1_size = input_size + operation1.block_size;
582 output1 = mbedtls_calloc(1, output1_size);
583 TEST_ASSERT( output1 != NULL);
584
mohammad1603d7d7ba52018-03-12 18:51:53 +0200585 TEST_ASSERT( psa_cipher_update( &operation1, input, input_size,
Moran Pekerded84402018-06-06 16:36:50 +0300586 output1, output1_size,
mohammad1603d7d7ba52018-03-12 18:51:53 +0200587 &output1_length) == PSA_SUCCESS );
Moran Pekerf55e8042018-05-29 16:28:28 +0300588 TEST_ASSERT( psa_cipher_finish( &operation1, output1 + output1_length,
Moran Pekerded84402018-06-06 16:36:50 +0300589 output1_size, &tmp_output_length) == PSA_SUCCESS );
590
591 output1_length += tmp_output_length;
592
593 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
594
595 output2_size = output1_length;
596 output2 = mbedtls_calloc(1, output2_size);
597
598 TEST_ASSERT( psa_encrypt_set_iv( &operation2, iv,
599 iv_length) == PSA_SUCCESS );
600 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
601 output2, output2_size, &output2_length) == PSA_SUCCESS );
602 tmp_output_length = 0;
603 TEST_ASSERT( psa_cipher_finish( &operation2, output2 + output2_length,
604 output2_size, &tmp_output_length) == PSA_SUCCESS );
605
606 output2_length += tmp_output_length;
607
608 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
609
610 TEST_ASSERT( input_size == output2_length );
611 TEST_ASSERT( memcmp( input, output2, input_size ) == 0 );
612
613exit:
614 mbedtls_free( key );
615 mbedtls_free( input );
616 mbedtls_free( output1 );
617 mbedtls_free( output2 );
618
619 psa_destroy_key( key_slot );
620 mbedtls_psa_crypto_free( );
621}
622/* END_CASE */
623
624/* BEGIN_CASE */
625void cipher_test_verify_output_multpart( int alg_arg,
626 int key_type_arg,
627 char *key_hex,
628 char *input_hex,
629 int first_part_size )
630{
631 int key_slot = 1;
632 psa_key_type_t key_type = key_type_arg;
633 psa_algorithm_t alg = alg_arg;
634 unsigned char *key = NULL;
635 size_t key_size;
636 unsigned char iv[16] = {0};
637 size_t iv_size = 16;
638 size_t iv_length = 0;
639 unsigned char *input = NULL;
640 size_t input_size = 0;
641 unsigned char *output1;
642 size_t output1_size = 0;
643 size_t output1_length = 0;
644 unsigned char *output2;
645 size_t output2_size = 0;
646 size_t output2_length = 0;
647 size_t tmp_output_length , temp = 0;
648 psa_cipher_operation_t operation1;
649 psa_cipher_operation_t operation2;
650
651 key = unhexify_alloc( key_hex, &key_size );
652 TEST_ASSERT( key != NULL );
653
654 input = unhexify_alloc( input_hex, &input_size );
655 TEST_ASSERT( input != NULL );
656
657 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
658
659 TEST_ASSERT( psa_import_key( key_slot, key_type,
660 key, key_size ) == PSA_SUCCESS );
661
662 TEST_ASSERT( psa_encrypt_setup( &operation1, key_slot, alg ) == PSA_SUCCESS );
663 TEST_ASSERT( psa_decrypt_setup( &operation2, key_slot, alg ) == PSA_SUCCESS );
664
665 TEST_ASSERT( psa_encrypt_generate_iv( &operation1, iv,
666 iv_size, &iv_length) == PSA_SUCCESS );
667 output1_size = input_size + operation1.block_size;
668 output1 = mbedtls_calloc(1, output1_size);
669
670 TEST_ASSERT( (unsigned int)first_part_size < input_size );
671
672 TEST_ASSERT( psa_cipher_update( &operation1, input, first_part_size,
673 output1, output1_size,
674 &output1_length) == PSA_SUCCESS );
675 temp = output1_length ;
676
677 TEST_ASSERT( psa_cipher_update( &operation1, input + first_part_size, input_size - first_part_size,
678 output1, output1_size,
679 &output1_length) == PSA_SUCCESS );
680 output1_length += temp;
681
682 TEST_ASSERT( psa_cipher_finish( &operation1, output1 + output1_length,
683 output1_size - output1_length, &tmp_output_length) == PSA_SUCCESS );
Gilles Peskine691dfb32018-06-06 15:18:02 +0200684
mohammad1603d7d7ba52018-03-12 18:51:53 +0200685 output1_length += tmp_output_length;
686
687 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
688
Moran Pekerf55e8042018-05-29 16:28:28 +0300689 output2_size = output1_length;
Moran Pekerded84402018-06-06 16:36:50 +0300690 output2 = mbedtls_calloc(1, output2_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +0200691
Moran Pekerf55e8042018-05-29 16:28:28 +0300692 TEST_ASSERT( psa_encrypt_set_iv( &operation2, iv,
Moran Pekerded84402018-06-06 16:36:50 +0300693 iv_length) == PSA_SUCCESS );
694
695 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
696 output2, output2_size,
697 &output2_length) == PSA_SUCCESS );
698
699 temp = output2_length ;
700
701 TEST_ASSERT( psa_cipher_update( &operation2, output1 + first_part_size,
702 output1_length - first_part_size,
703 output2, output2_size,
704 &output2_length) == PSA_SUCCESS );
705
706 output2_length += temp;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200707 tmp_output_length = 0;
Moran Pekerf55e8042018-05-29 16:28:28 +0300708 TEST_ASSERT( psa_cipher_finish( &operation2, output2 + output2_length,
Moran Pekerded84402018-06-06 16:36:50 +0300709 output2_size - output2_length, &tmp_output_length) == PSA_SUCCESS );
Moran Pekerf55e8042018-05-29 16:28:28 +0300710
711 output2_length += tmp_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300712
mohammad1603d7d7ba52018-03-12 18:51:53 +0200713 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
714
Moran Pekerded84402018-06-06 16:36:50 +0300715 TEST_ASSERT( input_size == output2_length );
mohammad1603e6b67a12018-03-12 10:38:49 -0700716 TEST_ASSERT( memcmp( input, output2, input_size ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200717
718exit:
719 mbedtls_free( key );
720 mbedtls_free( input );
Moran Pekerded84402018-06-06 16:36:50 +0300721 mbedtls_free( output1 );
722 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200723 psa_destroy_key( key_slot );
724 mbedtls_psa_crypto_free( );
725}
726/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +0200727
Moran Pekerded84402018-06-06 16:36:50 +0300728
Gilles Peskine7268afc2018-06-06 15:19:24 +0200729/* BEGIN_CASE */
730void signature_size( int type_arg, int bits, int alg_arg, int expected_size_arg )
731{
732 psa_key_type_t type = type_arg;
733 psa_algorithm_t alg = alg_arg;
734 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(type, bits, alg);
735 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
736exit:
737 ;
738}
739/* END_CASE */
740
741/* BEGIN_CASE */
742void sign_deterministic( int key_type_arg, char *key_hex,
743 int alg_arg, char *input_hex, char *output_hex )
744{
745 int slot = 1;
746 psa_key_type_t key_type = key_type_arg;
747 psa_algorithm_t alg = alg_arg;
748 unsigned char *key_data = NULL;
749 size_t key_size;
750 size_t key_bits;
751 unsigned char *input_data = NULL;
752 size_t input_size;
753 unsigned char *output_data = NULL;
754 size_t output_size;
755 unsigned char *signature = NULL;
756 size_t signature_size;
757 size_t signature_length = 0xdeadbeef;
758 psa_key_policy_t policy = {0};
759
760 key_data = unhexify_alloc( key_hex, &key_size );
761 TEST_ASSERT( key_data != NULL );
762 input_data = unhexify_alloc( input_hex, &input_size );
763 TEST_ASSERT( input_data != NULL );
764 output_data = unhexify_alloc( output_hex, &output_size );
765 TEST_ASSERT( output_data != NULL );
766
767 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
768
769 psa_key_policy_init( &policy );
770
771 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg );
772
773 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
774
775 TEST_ASSERT( psa_import_key( slot, key_type,
776 key_data, key_size ) == PSA_SUCCESS );
777 TEST_ASSERT( psa_get_key_information( slot,
778 NULL,
779 &key_bits ) == PSA_SUCCESS );
780
781 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, alg, key_bits );
782 TEST_ASSERT( signature_size != 0 );
783 signature = mbedtls_calloc( 1, signature_size );
784 TEST_ASSERT( signature != NULL );
785
786 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
787 input_data, input_size,
788 NULL, 0,
789 signature, signature_size,
790 &signature_length ) == PSA_SUCCESS );
791 TEST_ASSERT( signature_length == output_size );
792 TEST_ASSERT( memcmp( signature, output_data, output_size ) == 0 );
793
794exit:
795 psa_destroy_key( slot );
796 mbedtls_free( key_data );
797 mbedtls_free( input_data );
798 mbedtls_free( output_data );
799 mbedtls_free( signature );
800 mbedtls_psa_crypto_free( );
801}
802/* END_CASE */
803
804/* BEGIN_CASE */
805void sign_fail( int key_type_arg, char *key_hex,
806 int alg_arg, char *input_hex,
807 int signature_size, int expected_status_arg )
808{
809 int slot = 1;
810 psa_key_type_t key_type = key_type_arg;
811 psa_algorithm_t alg = alg_arg;
812 unsigned char *key_data = NULL;
813 size_t key_size;
814 unsigned char *input_data = NULL;
815 size_t input_size;
816 psa_status_t actual_status;
817 psa_status_t expected_status = expected_status_arg;
818 unsigned char *signature = NULL;
819 size_t signature_length = 0xdeadbeef;
820 psa_key_policy_t policy = {0};
821
822 key_data = unhexify_alloc( key_hex, &key_size );
823 TEST_ASSERT( key_data != NULL );
824 input_data = unhexify_alloc( input_hex, &input_size );
825 TEST_ASSERT( input_data != NULL );
826 signature = mbedtls_calloc( 1, signature_size );
827 TEST_ASSERT( signature != NULL );
828
829 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
830
831 psa_key_policy_init( &policy );
832
833 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg );
834
835 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
836
837 TEST_ASSERT( psa_import_key( slot, key_type,
838 key_data, key_size ) == PSA_SUCCESS );
839
840 actual_status = psa_asymmetric_sign( slot, alg,
841 input_data, input_size,
842 NULL, 0,
843 signature, signature_size,
844 &signature_length );
845 TEST_ASSERT( actual_status == expected_status );
846 TEST_ASSERT( signature_length == 0 );
847
848exit:
849 psa_destroy_key( slot );
850 mbedtls_free( key_data );
851 mbedtls_free( input_data );
852 mbedtls_free( signature );
853 mbedtls_psa_crypto_free( );
854}
855/* END_CASE */
856
857/* BEGIN_CASE */
858void key_policy( int usage_arg, int alg_arg )
859{
860 int key_slot = 1;
861 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
862 unsigned char key[32] = {0};
863 psa_key_policy_t policy_set = {0};
864 psa_key_policy_t policy_get = {0};
865
866 memset( key, 0x2a, sizeof( key ) );
867
868 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
869
870 psa_key_policy_init(& policy_set );
871 psa_key_policy_init(& policy_get );
872
873 psa_key_policy_set_usage( &policy_set, usage_arg, alg_arg );
874
875 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == ( psa_key_usage_t )usage_arg );
876
877 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set) == ( psa_algorithm_t )alg_arg );
878
879 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
880
881 TEST_ASSERT( psa_import_key( key_slot, key_type,
882 key, sizeof( key ) ) == PSA_SUCCESS );
883
884 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
885
886 TEST_ASSERT( policy_get.usage == policy_set.usage );
887 TEST_ASSERT( policy_get.alg == policy_set.alg );
888
889exit:
890 psa_destroy_key( key_slot );
891 mbedtls_psa_crypto_free( );
892}
893/* END_CASE */
894
895/* BEGIN_CASE */
896void key_policy_fail( int usage_arg, int alg_arg, int expected_status, char *key_hex )
897{
898 int key_slot = 1;
899 unsigned char* keypair = NULL;
900 size_t key_size = 0;
901 size_t signature_length = 0;
902 psa_key_policy_t policy = {0};
903 int actual_status = PSA_SUCCESS;
904
905 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
906
907 psa_key_policy_init( &policy );
908
909 psa_key_policy_set_usage( &policy, usage_arg, alg_arg );
910
911 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
912
913 if( usage_arg & PSA_KEY_USAGE_EXPORT )
914 {
915 keypair = unhexify_alloc( key_hex, &key_size );
916 TEST_ASSERT( keypair != NULL );
917 TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR,
918 keypair, key_size ) == PSA_SUCCESS );
919 actual_status = psa_asymmetric_sign( key_slot,
920 ( psa_algorithm_t )alg_arg, NULL, 0, NULL, 0,
921 NULL, 0, &signature_length );
922 }
923
924 if( usage_arg & PSA_KEY_USAGE_SIGN )
925 {
926 keypair = unhexify_alloc( key_hex, &key_size );
927 TEST_ASSERT( keypair != NULL );
928 TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR,
929 keypair, key_size ) == PSA_SUCCESS );
930 actual_status = psa_export_key( key_slot, NULL, 0, NULL );
931 }
932
933 TEST_ASSERT( actual_status == expected_status );
934
935exit:
936 psa_destroy_key( key_slot );
937 mbedtls_free( keypair );
938 mbedtls_psa_crypto_free( );
939}
940/* END_CASE */
941
942/* BEGIN_CASE */
943void key_lifetime( int lifetime_arg )
944{
945 int key_slot = 1;
946 psa_key_type_t key_type = PSA_ALG_CBC_BASE;
947 unsigned char key[32] = {0};
948 psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg;
949 psa_key_lifetime_t lifetime_get;
950
951 memset( key, 0x2a, sizeof( key ) );
952
953 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
954
955 TEST_ASSERT( psa_set_key_lifetime( key_slot,
956 lifetime_set ) == PSA_SUCCESS );
957
958 TEST_ASSERT( psa_import_key( key_slot, key_type,
959 key, sizeof( key ) ) == PSA_SUCCESS );
960
961 TEST_ASSERT( psa_get_key_lifetime( key_slot,
962 &lifetime_get ) == PSA_SUCCESS );
963
964 TEST_ASSERT( lifetime_get == lifetime_set );
965
966exit:
967 psa_destroy_key( key_slot );
968 mbedtls_psa_crypto_free( );
969}
970/* END_CASE */
971
972
973/* BEGIN_CASE */
974void key_lifetime_set_fail( int key_slot_arg, int lifetime_arg, int expected_status_arg )
975{
976 int key_slot = 1;
977 psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg;
978 psa_status_t actual_status;
979 psa_status_t expected_status = expected_status_arg;
980
981 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
982
983 actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set );
984
985 if( actual_status == PSA_SUCCESS )
986 actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set );
987
988 TEST_ASSERT( expected_status == actual_status );
989
990exit:
991 psa_destroy_key( key_slot );
992 mbedtls_psa_crypto_free( );
993}
994/* END_CASE */