blob: 4f1d3d60e4ca7f49928ccd8fdce3b2e5c9781c3c [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
Moran Peker7f878502018-06-06 17:09:40 +0300267
Gilles Peskine8c9def32018-02-08 10:02:12 +0100268/* BEGIN_CASE */
Moran Pekerded84402018-06-06 16:36:50 +0300269void cipher_test_encrypt( int alg_arg, int key_type_arg,
270 char *key_hex,
271 char *input_hex, char *output_hex,
272 int expected_status )
Moran Peker96cc00a2018-05-31 14:03:56 +0300273{
274 int key_slot = 1;
Moran Pekerded84402018-06-06 16:36:50 +0300275 psa_status_t status;
Moran Peker96cc00a2018-05-31 14:03:56 +0300276 psa_key_type_t key_type = key_type_arg;
277 psa_algorithm_t alg = alg_arg;
278 unsigned char *key = NULL;
279 size_t key_size;
280 unsigned char iv[16] = {0};
281 unsigned char *input = NULL;
282 size_t input_size = 0;
283 unsigned char *output;
284 unsigned char *expected_output;
Moran Pekerded84402018-06-06 16:36:50 +0300285 size_t output_size, output_size_1 = 0;
Moran Peker96cc00a2018-05-31 14:03:56 +0300286 size_t output_length = 0;
287 psa_cipher_operation_t operation;
288
289
290 key = unhexify_alloc( key_hex, &key_size );
291 TEST_ASSERT( key != NULL );
292
293 input = unhexify_alloc( input_hex, &input_size );
294 TEST_ASSERT( input != NULL );
295
296 expected_output = unhexify_alloc( output_hex, &output_size );
297 TEST_ASSERT( expected_output != NULL );
298
299 memset( iv, 0x2a, sizeof( iv ) );
Moran Pekerded84402018-06-06 16:36:50 +0300300
Moran Peker96cc00a2018-05-31 14:03:56 +0300301 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
302
303 TEST_ASSERT( psa_import_key( key_slot, key_type,
304 key, key_size ) == PSA_SUCCESS );
305
306 TEST_ASSERT( psa_encrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS );
307
308 TEST_ASSERT( psa_encrypt_set_iv( &operation, iv,
309 sizeof( iv ) ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300310 output_size_1 = input_size + operation.block_size;
311 output = mbedtls_calloc(1, output_size_1);
Moran Peker96cc00a2018-05-31 14:03:56 +0300312
313 TEST_ASSERT( psa_cipher_update( &operation, input, input_size,
Moran Pekerded84402018-06-06 16:36:50 +0300314 output, output_size_1,
Moran Peker96cc00a2018-05-31 14:03:56 +0300315 &output_length) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300316 status = psa_cipher_finish( &operation, output + output_length,
317 output_size_1, &output_length);
318 TEST_ASSERT( status == (psa_status_t) expected_status );
319 if( expected_status == PSA_SUCCESS )
320 {
321 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Moran Peker96cc00a2018-05-31 14:03:56 +0300322
Moran Pekerded84402018-06-06 16:36:50 +0300323 TEST_ASSERT( input_size == output_size );
324 TEST_ASSERT( memcmp( expected_output, output, output_size ) == 0 );
325 }
Moran Peker96cc00a2018-05-31 14:03:56 +0300326exit:
327 mbedtls_free( key );
328 mbedtls_free( input );
329 psa_destroy_key( key_slot );
330 mbedtls_psa_crypto_free( );
331}
332/* END_CASE */
mohammad1603b152d4d2018-04-11 23:51:20 -0700333
334/* BEGIN_CASE */
Moran Pekerded84402018-06-06 16:36:50 +0300335void cipher_test_encrypt_multipart( int alg_arg, int key_type_arg,
Moran Peker7691fb72018-05-31 16:15:31 +0300336 char *key_hex,
337 char *input_hex,
338 int first_part_size, char *output_hex )
339{
340 int key_slot = 1;
341 psa_key_type_t key_type = key_type_arg;
342 psa_algorithm_t alg = alg_arg;
343 unsigned char *key = NULL;
344 size_t key_size;
345 unsigned char iv[16] = {0};
346 unsigned char *input = NULL;
347 size_t input_size = 0;
348 unsigned char *output;
349 unsigned char *expected_output;
Moran Pekerded84402018-06-06 16:36:50 +0300350 size_t output_size, output_size_1 = 0;
Moran Peker7691fb72018-05-31 16:15:31 +0300351 size_t output_length = 0;
352 psa_cipher_operation_t operation;
353
354
355 key = unhexify_alloc( key_hex, &key_size );
356 TEST_ASSERT( key != NULL );
357
358 input = unhexify_alloc( input_hex, &input_size );
359 TEST_ASSERT( input != NULL );
360
361 expected_output = unhexify_alloc( output_hex, &output_size );
362 TEST_ASSERT( expected_output != NULL );
363
364 memset( iv, 0x2a, sizeof( iv ) );
Moran Pekerded84402018-06-06 16:36:50 +0300365
Moran Peker7691fb72018-05-31 16:15:31 +0300366 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
367
368 TEST_ASSERT( psa_import_key( key_slot, key_type,
369 key, key_size ) == PSA_SUCCESS );
370
371 TEST_ASSERT( psa_encrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS );
372
373 TEST_ASSERT( psa_encrypt_set_iv( &operation, iv,
Moran Pekerded84402018-06-06 16:36:50 +0300374 sizeof( iv ) ) == PSA_SUCCESS );
375 output_size_1 = input_size + operation.block_size;
376 output = mbedtls_calloc(1, output_size_1);
Moran Peker7691fb72018-05-31 16:15:31 +0300377
378 TEST_ASSERT( (unsigned int)first_part_size < input_size );
379 TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size,
Moran Pekerded84402018-06-06 16:36:50 +0300380 output, output_size_1,
Moran Peker7691fb72018-05-31 16:15:31 +0300381 &output_length) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300382 TEST_ASSERT( psa_cipher_update( &operation, input + first_part_size, input_size - first_part_size,
383 output, output_size_1,
Moran Peker7691fb72018-05-31 16:15:31 +0300384 &output_length) == PSA_SUCCESS );
385 TEST_ASSERT( psa_cipher_finish( &operation, output + output_length,
Moran Pekerded84402018-06-06 16:36:50 +0300386 output_size_1, &output_length) == PSA_SUCCESS );
Moran Peker7691fb72018-05-31 16:15:31 +0300387
388 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
389
390 TEST_ASSERT( input_size == output_size );
391 TEST_ASSERT( memcmp( expected_output, output, output_size ) == 0 );
392
393exit:
394 mbedtls_free( key );
395 mbedtls_free( input );
396 psa_destroy_key( key_slot );
397 mbedtls_psa_crypto_free( );
398}
399/* END_CASE */
400
401/* BEGIN_CASE */
Moran Pekerded84402018-06-06 16:36:50 +0300402void cipher_test_decrypt_multipart( int alg_arg, int key_type_arg,
mohammad1603b152d4d2018-04-11 23:51:20 -0700403 char *key_hex,
Moran Pekerded84402018-06-06 16:36:50 +0300404 char *input_hex,
405 int first_part_size, char *output_hex)
mohammad1603b152d4d2018-04-11 23:51:20 -0700406{
407 int key_slot = 1;
Moran Pekerded84402018-06-06 16:36:50 +0300408
mohammad1603b152d4d2018-04-11 23:51:20 -0700409 psa_key_type_t key_type = key_type_arg;
410 psa_algorithm_t alg = alg_arg;
411 unsigned char *key = NULL;
412 size_t key_size;
413 unsigned char iv[16] = {0};
414 unsigned char *input = NULL;
415 size_t input_size = 0;
416 unsigned char *output;
417 unsigned char *expected_output;
Moran Pekerded84402018-06-06 16:36:50 +0300418 size_t output_size, output_size_1 = 0;
mohammad1603b152d4d2018-04-11 23:51:20 -0700419 size_t output_length = 0;
420 psa_cipher_operation_t operation;
421
422
423 key = unhexify_alloc( key_hex, &key_size );
424 TEST_ASSERT( key != NULL );
425
426 input = unhexify_alloc( input_hex, &input_size );
427 TEST_ASSERT( input != NULL );
428
429 expected_output = unhexify_alloc( output_hex, &output_size );
430 TEST_ASSERT( expected_output != NULL );
431
432 memset( iv, 0x2a, sizeof( iv ) );
Moran Pekerded84402018-06-06 16:36:50 +0300433
mohammad1603b152d4d2018-04-11 23:51:20 -0700434 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
435
436 TEST_ASSERT( psa_import_key( key_slot, key_type,
437 key, key_size ) == PSA_SUCCESS );
438
439 TEST_ASSERT( psa_decrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS );
440
441 TEST_ASSERT( psa_encrypt_set_iv( &operation, iv,
442 sizeof( iv ) ) == PSA_SUCCESS );
443
Moran Pekerded84402018-06-06 16:36:50 +0300444 output_size_1 = input_size + operation.block_size;
445 output = mbedtls_calloc(1, output_size_1);
mohammad1603b152d4d2018-04-11 23:51:20 -0700446
Moran Pekerded84402018-06-06 16:36:50 +0300447 TEST_ASSERT( (unsigned int)first_part_size < input_size );
448 TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size,
449 output, output_size_1,
450 &output_length) == PSA_SUCCESS );
451 TEST_ASSERT( psa_cipher_update( &operation, input + first_part_size, input_size - first_part_size,
452 output, output_size_1,
mohammad1603b152d4d2018-04-11 23:51:20 -0700453 &output_length) == PSA_SUCCESS );
Moran Pekerf55e8042018-05-29 16:28:28 +0300454 TEST_ASSERT( psa_cipher_finish( &operation, output + output_length,
Moran Pekerded84402018-06-06 16:36:50 +0300455 output_size_1, &output_length) == PSA_SUCCESS );
mohammad1603b152d4d2018-04-11 23:51:20 -0700456 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
457
458 TEST_ASSERT( input_size == output_size );
459 TEST_ASSERT( memcmp( expected_output, output, output_size ) == 0 );
460
461exit:
462 mbedtls_free( key );
463 mbedtls_free( input );
464 psa_destroy_key( key_slot );
465 mbedtls_psa_crypto_free( );
466}
467/* END_CASE */
468
469
mohammad1603d7d7ba52018-03-12 18:51:53 +0200470/* BEGIN_CASE */
Moran Pekerded84402018-06-06 16:36:50 +0300471void cipher_test_decrypt( int alg_arg, int key_type_arg,
472 char *key_hex,
473 char *input_hex, char *output_hex,
474 int expected_status )
475{
476 int key_slot = 1;
477 psa_status_t status;
478 psa_key_type_t key_type = key_type_arg;
479 psa_algorithm_t alg = alg_arg;
480 unsigned char *key = NULL;
481 size_t key_size;
482 unsigned char iv[16] = {0};
483 unsigned char *input = NULL;
484 size_t input_size = 0;
485 unsigned char *output;
486 unsigned char *expected_output;
487 size_t output_size, output_size_1 = 0;
488 size_t output_length = 0;
489 psa_cipher_operation_t operation;
490
491
492 key = unhexify_alloc( key_hex, &key_size );
493 TEST_ASSERT( key != NULL );
494
495 input = unhexify_alloc( input_hex, &input_size );
496 TEST_ASSERT( input != NULL );
497
498 expected_output = unhexify_alloc( output_hex, &output_size );
499 TEST_ASSERT( expected_output != NULL );
500
501 memset( iv, 0x2a, sizeof( iv ) );
502
503 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
504
505 TEST_ASSERT( psa_import_key( key_slot, key_type,
506 key, key_size ) == PSA_SUCCESS );
507
508 TEST_ASSERT( psa_decrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS );
509
510 TEST_ASSERT( psa_encrypt_set_iv( &operation, iv,
511 sizeof( iv ) ) == PSA_SUCCESS );
512
513 output_size_1 = input_size + operation.block_size;
514 output = mbedtls_calloc(1, output_size);
515
516 TEST_ASSERT( psa_cipher_update( &operation, input, input_size,
517 output, output_size_1,
518 &output_length) == PSA_SUCCESS );
519 status = psa_cipher_finish( &operation, output + output_length,
520 output_size_1, &output_length);
521 TEST_ASSERT( status == (psa_status_t) expected_status );
522
523 if( expected_status == PSA_SUCCESS )
524 {
525 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
526
527 TEST_ASSERT( input_size == output_size );
528 TEST_ASSERT( memcmp( expected_output, output, output_size ) == 0 );
529 }
530
531
532exit:
533 mbedtls_free( key );
534 mbedtls_free( input );
535 psa_destroy_key( key_slot );
536 mbedtls_psa_crypto_free( );
537}
538/* END_CASE */
539
540
541/* BEGIN_CASE */
542void cipher_test_verify_output( int alg_arg, int key_type_arg,
mohammad1603d7d7ba52018-03-12 18:51:53 +0200543 char *key_hex,
544 char *input_hex )
545{
546 int key_slot = 1;
547 psa_key_type_t key_type = key_type_arg;
548 psa_algorithm_t alg = alg_arg;
549 unsigned char *key = NULL;
550 size_t key_size;
mohammad1603e6b67a12018-03-12 10:38:49 -0700551 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +0200552 size_t iv_size = 16;
553 size_t iv_length = 0;
554 unsigned char *input = NULL;
555 size_t input_size = 0;
Moran Peker3205a652018-03-20 17:09:59 +0200556 unsigned char *output1;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200557 size_t output1_size = 0;
558 size_t output1_length = 0;
Moran Peker3205a652018-03-20 17:09:59 +0200559 unsigned char *output2;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200560 size_t output2_size = 0;
561 size_t output2_length = 0;
562 size_t tmp_output_length = 0;
563 psa_cipher_operation_t operation1;
564 psa_cipher_operation_t operation2;
565
566 key = unhexify_alloc( key_hex, &key_size );
567 TEST_ASSERT( key != NULL );
568
569 input = unhexify_alloc( input_hex, &input_size );
570 TEST_ASSERT( input != NULL );
Moran Pekerded84402018-06-06 16:36:50 +0300571
mohammad1603d7d7ba52018-03-12 18:51:53 +0200572 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
573
574 TEST_ASSERT( psa_import_key( key_slot, key_type,
575 key, key_size ) == PSA_SUCCESS );
576
Moran Pekerf55e8042018-05-29 16:28:28 +0300577 TEST_ASSERT( psa_encrypt_setup( &operation1, key_slot, alg ) == PSA_SUCCESS );
578 TEST_ASSERT( psa_decrypt_setup( &operation2, key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200579
mohammad16038481e742018-03-18 13:57:31 +0200580 TEST_ASSERT( psa_encrypt_generate_iv( &operation1, iv,
mohammad1603d7d7ba52018-03-12 18:51:53 +0200581 iv_size, &iv_length) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300582 output1_size = input_size + operation1.block_size;
583 output1 = mbedtls_calloc(1, output1_size);
584 TEST_ASSERT( output1 != NULL);
585
mohammad1603d7d7ba52018-03-12 18:51:53 +0200586 TEST_ASSERT( psa_cipher_update( &operation1, input, input_size,
Moran Pekerded84402018-06-06 16:36:50 +0300587 output1, output1_size,
mohammad1603d7d7ba52018-03-12 18:51:53 +0200588 &output1_length) == PSA_SUCCESS );
Moran Pekerf55e8042018-05-29 16:28:28 +0300589 TEST_ASSERT( psa_cipher_finish( &operation1, output1 + output1_length,
Moran Pekerded84402018-06-06 16:36:50 +0300590 output1_size, &tmp_output_length) == PSA_SUCCESS );
591
592 output1_length += tmp_output_length;
593
594 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
595
596 output2_size = output1_length;
597 output2 = mbedtls_calloc(1, output2_size);
598
599 TEST_ASSERT( psa_encrypt_set_iv( &operation2, iv,
600 iv_length) == PSA_SUCCESS );
601 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
602 output2, output2_size, &output2_length) == PSA_SUCCESS );
603 tmp_output_length = 0;
604 TEST_ASSERT( psa_cipher_finish( &operation2, output2 + output2_length,
605 output2_size, &tmp_output_length) == PSA_SUCCESS );
606
607 output2_length += tmp_output_length;
608
609 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
610
611 TEST_ASSERT( input_size == output2_length );
612 TEST_ASSERT( memcmp( input, output2, input_size ) == 0 );
613
614exit:
615 mbedtls_free( key );
616 mbedtls_free( input );
Moran Pekerded84402018-06-06 16:36:50 +0300617 psa_destroy_key( key_slot );
618 mbedtls_psa_crypto_free( );
619}
620/* END_CASE */
621
622/* BEGIN_CASE */
623void cipher_test_verify_output_multpart( int alg_arg,
624 int key_type_arg,
625 char *key_hex,
626 char *input_hex,
627 int first_part_size )
628{
629 int key_slot = 1;
630 psa_key_type_t key_type = key_type_arg;
631 psa_algorithm_t alg = alg_arg;
632 unsigned char *key = NULL;
633 size_t key_size;
634 unsigned char iv[16] = {0};
635 size_t iv_size = 16;
636 size_t iv_length = 0;
637 unsigned char *input = NULL;
638 size_t input_size = 0;
639 unsigned char *output1;
640 size_t output1_size = 0;
641 size_t output1_length = 0;
642 unsigned char *output2;
643 size_t output2_size = 0;
644 size_t output2_length = 0;
645 size_t tmp_output_length , temp = 0;
646 psa_cipher_operation_t operation1;
647 psa_cipher_operation_t operation2;
648
649 key = unhexify_alloc( key_hex, &key_size );
650 TEST_ASSERT( key != NULL );
651
652 input = unhexify_alloc( input_hex, &input_size );
653 TEST_ASSERT( input != NULL );
654
655 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
656
657 TEST_ASSERT( psa_import_key( key_slot, key_type,
658 key, key_size ) == PSA_SUCCESS );
659
660 TEST_ASSERT( psa_encrypt_setup( &operation1, key_slot, alg ) == PSA_SUCCESS );
661 TEST_ASSERT( psa_decrypt_setup( &operation2, key_slot, alg ) == PSA_SUCCESS );
662
663 TEST_ASSERT( psa_encrypt_generate_iv( &operation1, iv,
664 iv_size, &iv_length) == PSA_SUCCESS );
665 output1_size = input_size + operation1.block_size;
666 output1 = mbedtls_calloc(1, output1_size);
667
668 TEST_ASSERT( (unsigned int)first_part_size < input_size );
669
670 TEST_ASSERT( psa_cipher_update( &operation1, input, first_part_size,
671 output1, output1_size,
672 &output1_length) == PSA_SUCCESS );
673 temp = output1_length ;
674
675 TEST_ASSERT( psa_cipher_update( &operation1, input + first_part_size, input_size - first_part_size,
676 output1, output1_size,
677 &output1_length) == PSA_SUCCESS );
678 output1_length += temp;
679
680 TEST_ASSERT( psa_cipher_finish( &operation1, output1 + output1_length,
681 output1_size - output1_length, &tmp_output_length) == PSA_SUCCESS );
Gilles Peskine691dfb32018-06-06 15:18:02 +0200682
mohammad1603d7d7ba52018-03-12 18:51:53 +0200683 output1_length += tmp_output_length;
684
685 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
686
Moran Pekerf55e8042018-05-29 16:28:28 +0300687 output2_size = output1_length;
Moran Pekerded84402018-06-06 16:36:50 +0300688 output2 = mbedtls_calloc(1, output2_size);
mohammad1603d7d7ba52018-03-12 18:51:53 +0200689
Moran Pekerf55e8042018-05-29 16:28:28 +0300690 TEST_ASSERT( psa_encrypt_set_iv( &operation2, iv,
Moran Pekerded84402018-06-06 16:36:50 +0300691 iv_length) == PSA_SUCCESS );
692
693 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
694 output2, output2_size,
695 &output2_length) == PSA_SUCCESS );
696
697 temp = output2_length ;
698
699 TEST_ASSERT( psa_cipher_update( &operation2, output1 + first_part_size,
700 output1_length - first_part_size,
701 output2, output2_size,
702 &output2_length) == PSA_SUCCESS );
703
704 output2_length += temp;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200705 tmp_output_length = 0;
Moran Pekerf55e8042018-05-29 16:28:28 +0300706 TEST_ASSERT( psa_cipher_finish( &operation2, output2 + output2_length,
Moran Pekerded84402018-06-06 16:36:50 +0300707 output2_size - output2_length, &tmp_output_length) == PSA_SUCCESS );
Moran Pekerf55e8042018-05-29 16:28:28 +0300708
709 output2_length += tmp_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300710
mohammad1603d7d7ba52018-03-12 18:51:53 +0200711 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
712
Moran Pekerded84402018-06-06 16:36:50 +0300713 TEST_ASSERT( input_size == output2_length );
mohammad1603e6b67a12018-03-12 10:38:49 -0700714 TEST_ASSERT( memcmp( input, output2, input_size ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200715
716exit:
717 mbedtls_free( key );
718 mbedtls_free( input );
719 psa_destroy_key( key_slot );
720 mbedtls_psa_crypto_free( );
721}
722/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +0200723
724/* BEGIN_CASE */
725void signature_size( int type_arg, int bits, int alg_arg, int expected_size_arg )
726{
727 psa_key_type_t type = type_arg;
728 psa_algorithm_t alg = alg_arg;
729 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(type, bits, alg);
730 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
731exit:
732 ;
733}
734/* END_CASE */
735
736/* BEGIN_CASE */
737void sign_deterministic( int key_type_arg, char *key_hex,
738 int alg_arg, char *input_hex, char *output_hex )
739{
740 int slot = 1;
741 psa_key_type_t key_type = key_type_arg;
742 psa_algorithm_t alg = alg_arg;
743 unsigned char *key_data = NULL;
744 size_t key_size;
745 size_t key_bits;
746 unsigned char *input_data = NULL;
747 size_t input_size;
748 unsigned char *output_data = NULL;
749 size_t output_size;
750 unsigned char *signature = NULL;
751 size_t signature_size;
752 size_t signature_length = 0xdeadbeef;
753 psa_key_policy_t policy = {0};
754
755 key_data = unhexify_alloc( key_hex, &key_size );
756 TEST_ASSERT( key_data != NULL );
757 input_data = unhexify_alloc( input_hex, &input_size );
758 TEST_ASSERT( input_data != NULL );
759 output_data = unhexify_alloc( output_hex, &output_size );
760 TEST_ASSERT( output_data != NULL );
761
762 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
763
764 psa_key_policy_init( &policy );
765
766 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg );
767
768 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
769
770 TEST_ASSERT( psa_import_key( slot, key_type,
771 key_data, key_size ) == PSA_SUCCESS );
772 TEST_ASSERT( psa_get_key_information( slot,
773 NULL,
774 &key_bits ) == PSA_SUCCESS );
775
776 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, alg, key_bits );
777 TEST_ASSERT( signature_size != 0 );
778 signature = mbedtls_calloc( 1, signature_size );
779 TEST_ASSERT( signature != NULL );
780
781 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
782 input_data, input_size,
783 NULL, 0,
784 signature, signature_size,
785 &signature_length ) == PSA_SUCCESS );
786 TEST_ASSERT( signature_length == output_size );
787 TEST_ASSERT( memcmp( signature, output_data, output_size ) == 0 );
788
789exit:
790 psa_destroy_key( slot );
791 mbedtls_free( key_data );
792 mbedtls_free( input_data );
793 mbedtls_free( output_data );
794 mbedtls_free( signature );
795 mbedtls_psa_crypto_free( );
796}
797/* END_CASE */
798
799/* BEGIN_CASE */
800void sign_fail( int key_type_arg, char *key_hex,
801 int alg_arg, char *input_hex,
802 int signature_size, int expected_status_arg )
803{
804 int slot = 1;
805 psa_key_type_t key_type = key_type_arg;
806 psa_algorithm_t alg = alg_arg;
807 unsigned char *key_data = NULL;
808 size_t key_size;
809 unsigned char *input_data = NULL;
810 size_t input_size;
811 psa_status_t actual_status;
812 psa_status_t expected_status = expected_status_arg;
813 unsigned char *signature = NULL;
814 size_t signature_length = 0xdeadbeef;
815 psa_key_policy_t policy = {0};
816
817 key_data = unhexify_alloc( key_hex, &key_size );
818 TEST_ASSERT( key_data != NULL );
819 input_data = unhexify_alloc( input_hex, &input_size );
820 TEST_ASSERT( input_data != NULL );
821 signature = mbedtls_calloc( 1, signature_size );
822 TEST_ASSERT( signature != NULL );
823
824 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
825
826 psa_key_policy_init( &policy );
827
828 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg );
829
830 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
831
832 TEST_ASSERT( psa_import_key( slot, key_type,
833 key_data, key_size ) == PSA_SUCCESS );
834
835 actual_status = psa_asymmetric_sign( slot, alg,
836 input_data, input_size,
837 NULL, 0,
838 signature, signature_size,
839 &signature_length );
840 TEST_ASSERT( actual_status == expected_status );
841 TEST_ASSERT( signature_length == 0 );
842
843exit:
844 psa_destroy_key( slot );
845 mbedtls_free( key_data );
846 mbedtls_free( input_data );
847 mbedtls_free( signature );
848 mbedtls_psa_crypto_free( );
849}
850/* END_CASE */
851
852/* BEGIN_CASE */
853void key_policy( int usage_arg, int alg_arg )
854{
855 int key_slot = 1;
856 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
857 unsigned char key[32] = {0};
858 psa_key_policy_t policy_set = {0};
859 psa_key_policy_t policy_get = {0};
860
861 memset( key, 0x2a, sizeof( key ) );
862
863 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
864
865 psa_key_policy_init(& policy_set );
866 psa_key_policy_init(& policy_get );
867
868 psa_key_policy_set_usage( &policy_set, usage_arg, alg_arg );
869
870 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == ( psa_key_usage_t )usage_arg );
871
872 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set) == ( psa_algorithm_t )alg_arg );
873
874 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
875
876 TEST_ASSERT( psa_import_key( key_slot, key_type,
877 key, sizeof( key ) ) == PSA_SUCCESS );
878
879 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
880
881 TEST_ASSERT( policy_get.usage == policy_set.usage );
882 TEST_ASSERT( policy_get.alg == policy_set.alg );
883
884exit:
885 psa_destroy_key( key_slot );
886 mbedtls_psa_crypto_free( );
887}
888/* END_CASE */
889
890/* BEGIN_CASE */
891void key_policy_fail( int usage_arg, int alg_arg, int expected_status, char *key_hex )
892{
893 int key_slot = 1;
894 unsigned char* keypair = NULL;
895 size_t key_size = 0;
896 size_t signature_length = 0;
897 psa_key_policy_t policy = {0};
898 int actual_status = PSA_SUCCESS;
899
900 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
901
902 psa_key_policy_init( &policy );
903
904 psa_key_policy_set_usage( &policy, usage_arg, alg_arg );
905
906 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
907
908 if( usage_arg & PSA_KEY_USAGE_EXPORT )
909 {
910 keypair = unhexify_alloc( key_hex, &key_size );
911 TEST_ASSERT( keypair != NULL );
912 TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR,
913 keypair, key_size ) == PSA_SUCCESS );
914 actual_status = psa_asymmetric_sign( key_slot,
915 ( psa_algorithm_t )alg_arg, NULL, 0, NULL, 0,
916 NULL, 0, &signature_length );
917 }
918
919 if( usage_arg & PSA_KEY_USAGE_SIGN )
920 {
921 keypair = unhexify_alloc( key_hex, &key_size );
922 TEST_ASSERT( keypair != NULL );
923 TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR,
924 keypair, key_size ) == PSA_SUCCESS );
925 actual_status = psa_export_key( key_slot, NULL, 0, NULL );
926 }
927
928 TEST_ASSERT( actual_status == expected_status );
929
930exit:
931 psa_destroy_key( key_slot );
932 mbedtls_free( keypair );
933 mbedtls_psa_crypto_free( );
934}
935/* END_CASE */
936
937/* BEGIN_CASE */
938void key_lifetime( int lifetime_arg )
939{
940 int key_slot = 1;
941 psa_key_type_t key_type = PSA_ALG_CBC_BASE;
942 unsigned char key[32] = {0};
943 psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg;
944 psa_key_lifetime_t lifetime_get;
945
946 memset( key, 0x2a, sizeof( key ) );
947
948 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
949
950 TEST_ASSERT( psa_set_key_lifetime( key_slot,
951 lifetime_set ) == PSA_SUCCESS );
952
953 TEST_ASSERT( psa_import_key( key_slot, key_type,
954 key, sizeof( key ) ) == PSA_SUCCESS );
955
956 TEST_ASSERT( psa_get_key_lifetime( key_slot,
957 &lifetime_get ) == PSA_SUCCESS );
958
959 TEST_ASSERT( lifetime_get == lifetime_set );
960
961exit:
962 psa_destroy_key( key_slot );
963 mbedtls_psa_crypto_free( );
964}
965/* END_CASE */
966
967
968/* BEGIN_CASE */
969void key_lifetime_set_fail( int key_slot_arg, int lifetime_arg, int expected_status_arg )
970{
971 int key_slot = 1;
972 psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg;
973 psa_status_t actual_status;
974 psa_status_t expected_status = expected_status_arg;
975
976 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
977
978 actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set );
979
980 if( actual_status == PSA_SUCCESS )
981 actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set );
982
983 TEST_ASSERT( expected_status == actual_status );
984
985exit:
986 psa_destroy_key( key_slot );
987 mbedtls_psa_crypto_free( );
988}
989/* END_CASE */