blob: 29f233b5556740a2d120d779ef95d3dc7bba439f [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 */
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200269void cipher_test_encrypt( int alg_arg, int key_type_arg,
Moran Pekerded84402018-06-06 16:36:50 +0300270 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;
Gilles Peskine048b7f02018-06-08 14:20:49 +0200285 size_t expected_output_size;
286 size_t output_buffer_size = 0;
287 size_t function_output_length = 0;
Moran Peker96cc00a2018-05-31 14:03:56 +0300288 psa_cipher_operation_t operation;
289
290
291 key = unhexify_alloc( key_hex, &key_size );
292 TEST_ASSERT( key != NULL );
293
294 input = unhexify_alloc( input_hex, &input_size );
295 TEST_ASSERT( input != NULL );
296
Gilles Peskine048b7f02018-06-08 14:20:49 +0200297 expected_output = unhexify_alloc( output_hex, &expected_output_size );
Moran Peker96cc00a2018-05-31 14:03:56 +0300298 TEST_ASSERT( expected_output != NULL );
299
300 memset( iv, 0x2a, sizeof( iv ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200301
Moran Peker96cc00a2018-05-31 14:03:56 +0300302 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
303
304 TEST_ASSERT( psa_import_key( key_slot, key_type,
305 key, key_size ) == PSA_SUCCESS );
306
307 TEST_ASSERT( psa_encrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS );
308
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200309 TEST_ASSERT( psa_encrypt_set_iv( &operation,
310 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +0200311 output_buffer_size = input_size + operation.block_size;
312 output = mbedtls_calloc( 1, output_buffer_size );
Moran Peker96cc00a2018-05-31 14:03:56 +0300313
314 TEST_ASSERT( psa_cipher_update( &operation, input, input_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200315 output, output_buffer_size,
316 &function_output_length ) == PSA_SUCCESS );
317 status = psa_cipher_finish( &operation,
318 output + function_output_length,
319 output_buffer_size,
320 &function_output_length );
Moran Pekerded84402018-06-06 16:36:50 +0300321 TEST_ASSERT( status == (psa_status_t) expected_status );
322 if( expected_status == PSA_SUCCESS )
323 {
324 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +0200325 TEST_ASSERT( memcmp( expected_output, output,
326 expected_output_size ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +0300327 }
Moran Peker96cc00a2018-05-31 14:03:56 +0300328exit:
329 mbedtls_free( key );
330 mbedtls_free( input );
331 psa_destroy_key( key_slot );
332 mbedtls_psa_crypto_free( );
333}
334/* END_CASE */
mohammad1603b152d4d2018-04-11 23:51:20 -0700335
336/* BEGIN_CASE */
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200337void cipher_test_encrypt_multipart( int alg_arg, int key_type_arg,
338 char *key_hex,
339 char *input_hex,
340 int first_part_size, char *output_hex )
Moran Peker7691fb72018-05-31 16:15:31 +0300341{
342 int key_slot = 1;
343 psa_key_type_t key_type = key_type_arg;
344 psa_algorithm_t alg = alg_arg;
345 unsigned char *key = NULL;
346 size_t key_size;
347 unsigned char iv[16] = {0};
348 unsigned char *input = NULL;
349 size_t input_size = 0;
350 unsigned char *output;
351 unsigned char *expected_output;
Gilles Peskine048b7f02018-06-08 14:20:49 +0200352 size_t expected_output_size;
353 size_t output_buffer_size = 0;
354 size_t function_output_length = 0;
Moran Peker7691fb72018-05-31 16:15:31 +0300355 psa_cipher_operation_t operation;
356
357
358 key = unhexify_alloc( key_hex, &key_size );
359 TEST_ASSERT( key != NULL );
360
361 input = unhexify_alloc( input_hex, &input_size );
362 TEST_ASSERT( input != NULL );
363
Gilles Peskine048b7f02018-06-08 14:20:49 +0200364 expected_output = unhexify_alloc( output_hex, &expected_output_size );
Moran Peker7691fb72018-05-31 16:15:31 +0300365 TEST_ASSERT( expected_output != NULL );
366
367 memset( iv, 0x2a, sizeof( iv ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200368
Moran Peker7691fb72018-05-31 16:15:31 +0300369 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
370
371 TEST_ASSERT( psa_import_key( key_slot, key_type,
372 key, key_size ) == PSA_SUCCESS );
373
374 TEST_ASSERT( psa_encrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS );
375
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200376 TEST_ASSERT( psa_encrypt_set_iv( &operation,
377 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +0200378 output_buffer_size = input_size + operation.block_size;
379 output = mbedtls_calloc( 1, output_buffer_size );
Moran Peker7691fb72018-05-31 16:15:31 +0300380
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200381 TEST_ASSERT( (unsigned int) first_part_size < input_size );
Moran Peker7691fb72018-05-31 16:15:31 +0300382 TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200383 output, output_buffer_size,
384 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200385 TEST_ASSERT( psa_cipher_update( &operation,
386 input + first_part_size,
387 input_size - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200388 output, output_buffer_size,
389 &function_output_length ) == PSA_SUCCESS );
390 TEST_ASSERT( psa_cipher_finish( &operation,
391 output + function_output_length,
392 output_buffer_size,
393 &function_output_length ) == PSA_SUCCESS );
Moran Peker7691fb72018-05-31 16:15:31 +0300394
395 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
396
Gilles Peskine048b7f02018-06-08 14:20:49 +0200397 TEST_ASSERT( input_size == expected_output_size );
398 TEST_ASSERT( memcmp( expected_output, output, expected_output_size ) == 0 );
Moran Peker7691fb72018-05-31 16:15:31 +0300399
400exit:
401 mbedtls_free( key );
402 mbedtls_free( input );
403 psa_destroy_key( key_slot );
404 mbedtls_psa_crypto_free( );
405}
406/* END_CASE */
407
408/* BEGIN_CASE */
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200409void cipher_test_decrypt_multipart( int alg_arg, int key_type_arg,
410 char *key_hex,
411 char *input_hex,
412 int first_part_size, char *output_hex )
mohammad1603b152d4d2018-04-11 23:51:20 -0700413{
414 int key_slot = 1;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200415
mohammad1603b152d4d2018-04-11 23:51:20 -0700416 psa_key_type_t key_type = key_type_arg;
417 psa_algorithm_t alg = alg_arg;
418 unsigned char *key = NULL;
419 size_t key_size;
420 unsigned char iv[16] = {0};
421 unsigned char *input = NULL;
422 size_t input_size = 0;
423 unsigned char *output;
424 unsigned char *expected_output;
Gilles Peskine048b7f02018-06-08 14:20:49 +0200425 size_t expected_output_size;
426 size_t output_buffer_size = 0;
427 size_t function_output_length = 0;
mohammad1603b152d4d2018-04-11 23:51:20 -0700428 psa_cipher_operation_t operation;
429
430
431 key = unhexify_alloc( key_hex, &key_size );
432 TEST_ASSERT( key != NULL );
433
434 input = unhexify_alloc( input_hex, &input_size );
435 TEST_ASSERT( input != NULL );
436
Gilles Peskine048b7f02018-06-08 14:20:49 +0200437 expected_output = unhexify_alloc( output_hex, &expected_output_size );
mohammad1603b152d4d2018-04-11 23:51:20 -0700438 TEST_ASSERT( expected_output != NULL );
439
440 memset( iv, 0x2a, sizeof( iv ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200441
mohammad1603b152d4d2018-04-11 23:51:20 -0700442 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
443
444 TEST_ASSERT( psa_import_key( key_slot, key_type,
445 key, key_size ) == PSA_SUCCESS );
446
447 TEST_ASSERT( psa_decrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS );
448
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200449 TEST_ASSERT( psa_encrypt_set_iv( &operation,
450 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad1603b152d4d2018-04-11 23:51:20 -0700451
Gilles Peskine048b7f02018-06-08 14:20:49 +0200452 output_buffer_size = input_size + operation.block_size;
453 output = mbedtls_calloc( 1, output_buffer_size );
mohammad1603b152d4d2018-04-11 23:51:20 -0700454
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200455 TEST_ASSERT( (unsigned int) first_part_size < input_size );
Moran Pekerded84402018-06-06 16:36:50 +0300456 TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200457 output, output_buffer_size,
458 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200459 TEST_ASSERT( psa_cipher_update( &operation,
460 input + first_part_size,
461 input_size - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200462 output, output_buffer_size,
463 &function_output_length ) == PSA_SUCCESS );
464 TEST_ASSERT( psa_cipher_finish( &operation,
465 output + function_output_length,
466 output_buffer_size,
467 &function_output_length ) == PSA_SUCCESS );
mohammad1603b152d4d2018-04-11 23:51:20 -0700468 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
469
Gilles Peskine048b7f02018-06-08 14:20:49 +0200470 TEST_ASSERT( input_size == expected_output_size );
471 TEST_ASSERT( memcmp( expected_output, output, expected_output_size ) == 0 );
mohammad1603b152d4d2018-04-11 23:51:20 -0700472
473exit:
474 mbedtls_free( key );
475 mbedtls_free( input );
476 psa_destroy_key( key_slot );
477 mbedtls_psa_crypto_free( );
478}
479/* END_CASE */
480
481
mohammad1603d7d7ba52018-03-12 18:51:53 +0200482/* BEGIN_CASE */
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200483void cipher_test_decrypt( int alg_arg, int key_type_arg,
484 char *key_hex,
485 char *input_hex, char *output_hex,
486 int expected_status )
Moran Pekerded84402018-06-06 16:36:50 +0300487{
488 int key_slot = 1;
489 psa_status_t status;
490 psa_key_type_t key_type = key_type_arg;
491 psa_algorithm_t alg = alg_arg;
492 unsigned char *key = NULL;
493 size_t key_size;
494 unsigned char iv[16] = {0};
495 unsigned char *input = NULL;
496 size_t input_size = 0;
497 unsigned char *output;
498 unsigned char *expected_output;
Gilles Peskine048b7f02018-06-08 14:20:49 +0200499 size_t expected_output_size;
500 size_t output_buffer_size = 0;
501 size_t function_output_length = 0;
Moran Pekerded84402018-06-06 16:36:50 +0300502 psa_cipher_operation_t operation;
503
504
505 key = unhexify_alloc( key_hex, &key_size );
506 TEST_ASSERT( key != NULL );
507
508 input = unhexify_alloc( input_hex, &input_size );
509 TEST_ASSERT( input != NULL );
510
Gilles Peskine048b7f02018-06-08 14:20:49 +0200511 expected_output = unhexify_alloc( output_hex, &expected_output_size );
Moran Pekerded84402018-06-06 16:36:50 +0300512 TEST_ASSERT( expected_output != NULL );
513
514 memset( iv, 0x2a, sizeof( iv ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200515
Moran Pekerded84402018-06-06 16:36:50 +0300516 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
517
518 TEST_ASSERT( psa_import_key( key_slot, key_type,
519 key, key_size ) == PSA_SUCCESS );
520
521 TEST_ASSERT( psa_decrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS );
522
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200523 TEST_ASSERT( psa_encrypt_set_iv( &operation,
524 iv, sizeof( iv ) ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300525
Gilles Peskine048b7f02018-06-08 14:20:49 +0200526 output_buffer_size = input_size + operation.block_size;
527 output = mbedtls_calloc( 1, output_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +0300528
529 TEST_ASSERT( psa_cipher_update( &operation, input, input_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200530 output, output_buffer_size,
531 &function_output_length ) == PSA_SUCCESS );
532 status = psa_cipher_finish( &operation,
533 output + function_output_length,
534 output_buffer_size,
535 &function_output_length );
Moran Pekerded84402018-06-06 16:36:50 +0300536 TEST_ASSERT( status == (psa_status_t) expected_status );
537
538 if( expected_status == PSA_SUCCESS )
539 {
540 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +0200541 TEST_ASSERT( memcmp( expected_output, output,
542 expected_output_size ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +0300543 }
544
545
546exit:
547 mbedtls_free( key );
548 mbedtls_free( input );
549 psa_destroy_key( key_slot );
550 mbedtls_psa_crypto_free( );
551}
552/* END_CASE */
553
554
555/* BEGIN_CASE */
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200556void cipher_test_verify_output( int alg_arg, int key_type_arg,
557 char *key_hex,
558 char *input_hex )
mohammad1603d7d7ba52018-03-12 18:51:53 +0200559{
560 int key_slot = 1;
561 psa_key_type_t key_type = key_type_arg;
562 psa_algorithm_t alg = alg_arg;
563 unsigned char *key = NULL;
564 size_t key_size;
mohammad1603e6b67a12018-03-12 10:38:49 -0700565 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +0200566 size_t iv_size = 16;
567 size_t iv_length = 0;
568 unsigned char *input = NULL;
569 size_t input_size = 0;
Moran Peker3205a652018-03-20 17:09:59 +0200570 unsigned char *output1;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200571 size_t output1_size = 0;
572 size_t output1_length = 0;
Moran Peker3205a652018-03-20 17:09:59 +0200573 unsigned char *output2;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200574 size_t output2_size = 0;
575 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +0200576 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200577 psa_cipher_operation_t operation1;
578 psa_cipher_operation_t operation2;
579
580 key = unhexify_alloc( key_hex, &key_size );
581 TEST_ASSERT( key != NULL );
582
583 input = unhexify_alloc( input_hex, &input_size );
584 TEST_ASSERT( input != NULL );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200585
mohammad1603d7d7ba52018-03-12 18:51:53 +0200586 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
587
588 TEST_ASSERT( psa_import_key( key_slot, key_type,
589 key, key_size ) == PSA_SUCCESS );
590
Moran Pekerf55e8042018-05-29 16:28:28 +0300591 TEST_ASSERT( psa_encrypt_setup( &operation1, key_slot, alg ) == PSA_SUCCESS );
592 TEST_ASSERT( psa_decrypt_setup( &operation2, key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200593
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200594 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
595 iv, iv_size,
596 &iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300597 output1_size = input_size + operation1.block_size;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200598 output1 = mbedtls_calloc( 1, output1_size );
599 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +0300600
mohammad1603d7d7ba52018-03-12 18:51:53 +0200601 TEST_ASSERT( psa_cipher_update( &operation1, input, input_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200602 output1, output1_size,
603 &output1_length ) == PSA_SUCCESS );
604 TEST_ASSERT( psa_cipher_finish( &operation1,
605 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200606 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200607
Gilles Peskine048b7f02018-06-08 14:20:49 +0200608 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300609
610 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
611
612 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200613 output2 = mbedtls_calloc( 1, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +0300614
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200615 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
616 iv, iv_length ) == PSA_SUCCESS );
617 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
618 output2, output2_size,
619 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +0200620 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200621 TEST_ASSERT( psa_cipher_finish( &operation2,
622 output2 + output2_length,
623 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200624 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300625
Gilles Peskine048b7f02018-06-08 14:20:49 +0200626 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200627
Moran Pekerded84402018-06-06 16:36:50 +0300628 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
629
630 TEST_ASSERT( input_size == output2_length );
631 TEST_ASSERT( memcmp( input, output2, input_size ) == 0 );
632
633exit:
634 mbedtls_free( key );
635 mbedtls_free( input );
Moran Pekerded84402018-06-06 16:36:50 +0300636 psa_destroy_key( key_slot );
637 mbedtls_psa_crypto_free( );
638}
639/* END_CASE */
640
641/* BEGIN_CASE */
642void cipher_test_verify_output_multpart( int alg_arg,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200643 int key_type_arg,
644 char *key_hex,
645 char *input_hex,
646 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +0300647{
648 int key_slot = 1;
649 psa_key_type_t key_type = key_type_arg;
650 psa_algorithm_t alg = alg_arg;
651 unsigned char *key = NULL;
652 size_t key_size;
653 unsigned char iv[16] = {0};
654 size_t iv_size = 16;
655 size_t iv_length = 0;
656 unsigned char *input = NULL;
657 size_t input_size = 0;
658 unsigned char *output1;
Gilles Peskine048b7f02018-06-08 14:20:49 +0200659 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +0300660 size_t output1_length = 0;
661 unsigned char *output2;
Gilles Peskine048b7f02018-06-08 14:20:49 +0200662 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +0300663 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +0200664 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300665 psa_cipher_operation_t operation1;
666 psa_cipher_operation_t operation2;
667
668 key = unhexify_alloc( key_hex, &key_size );
669 TEST_ASSERT( key != NULL );
670
671 input = unhexify_alloc( input_hex, &input_size );
672 TEST_ASSERT( input != NULL );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200673
Moran Pekerded84402018-06-06 16:36:50 +0300674 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
675
676 TEST_ASSERT( psa_import_key( key_slot, key_type,
677 key, key_size ) == PSA_SUCCESS );
678
679 TEST_ASSERT( psa_encrypt_setup( &operation1, key_slot, alg ) == PSA_SUCCESS );
680 TEST_ASSERT( psa_decrypt_setup( &operation2, key_slot, alg ) == PSA_SUCCESS );
681
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200682 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
683 iv, iv_size,
684 &iv_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +0200685 output1_buffer_size = input_size + operation1.block_size;
686 output1 = mbedtls_calloc( 1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +0300687
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200688 TEST_ASSERT( (unsigned int) first_part_size < input_size );
689
Moran Pekerded84402018-06-06 16:36:50 +0300690 TEST_ASSERT( psa_cipher_update( &operation1, input, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200691 output1, output1_buffer_size,
692 &function_output_length ) == PSA_SUCCESS );
693 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300694
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200695 TEST_ASSERT( psa_cipher_update( &operation1,
696 input + first_part_size,
697 input_size - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200698 output1, output1_buffer_size,
699 &function_output_length ) == PSA_SUCCESS );
700 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300701
Gilles Peskine048b7f02018-06-08 14:20:49 +0200702 TEST_ASSERT( psa_cipher_finish( &operation1,
703 output1 + output1_length,
704 output1_buffer_size - output1_length,
705 &function_output_length ) == PSA_SUCCESS );
706 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200707
708 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
709
Gilles Peskine048b7f02018-06-08 14:20:49 +0200710 output2_buffer_size = output1_length;
711 output2 = mbedtls_calloc( 1, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200712
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200713 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
714 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300715
716 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200717 output2, output2_buffer_size,
718 &function_output_length ) == PSA_SUCCESS );
719 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300720
Gilles Peskine048b7f02018-06-08 14:20:49 +0200721 TEST_ASSERT( psa_cipher_update( &operation2,
722 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200723 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200724 output2, output2_buffer_size,
725 &function_output_length ) == PSA_SUCCESS );
726 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300727
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200728 TEST_ASSERT( psa_cipher_finish( &operation2,
729 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200730 output2_buffer_size - output2_length,
731 &function_output_length ) == PSA_SUCCESS );
732 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200733
mohammad1603d7d7ba52018-03-12 18:51:53 +0200734 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
735
Moran Pekerded84402018-06-06 16:36:50 +0300736 TEST_ASSERT( input_size == output2_length );
mohammad1603e6b67a12018-03-12 10:38:49 -0700737 TEST_ASSERT( memcmp( input, output2, input_size ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200738
739exit:
740 mbedtls_free( key );
741 mbedtls_free( input );
742 psa_destroy_key( key_slot );
743 mbedtls_psa_crypto_free( );
744}
745/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +0200746
747/* BEGIN_CASE */
748void signature_size( int type_arg, int bits, int alg_arg, int expected_size_arg )
749{
750 psa_key_type_t type = type_arg;
751 psa_algorithm_t alg = alg_arg;
752 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(type, bits, alg);
753 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
754exit:
755 ;
756}
757/* END_CASE */
758
759/* BEGIN_CASE */
760void sign_deterministic( int key_type_arg, char *key_hex,
761 int alg_arg, char *input_hex, char *output_hex )
762{
763 int slot = 1;
764 psa_key_type_t key_type = key_type_arg;
765 psa_algorithm_t alg = alg_arg;
766 unsigned char *key_data = NULL;
767 size_t key_size;
768 size_t key_bits;
769 unsigned char *input_data = NULL;
770 size_t input_size;
771 unsigned char *output_data = NULL;
772 size_t output_size;
773 unsigned char *signature = NULL;
774 size_t signature_size;
775 size_t signature_length = 0xdeadbeef;
776 psa_key_policy_t policy = {0};
777
778 key_data = unhexify_alloc( key_hex, &key_size );
779 TEST_ASSERT( key_data != NULL );
780 input_data = unhexify_alloc( input_hex, &input_size );
781 TEST_ASSERT( input_data != NULL );
782 output_data = unhexify_alloc( output_hex, &output_size );
783 TEST_ASSERT( output_data != NULL );
784
785 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
786
787 psa_key_policy_init( &policy );
788
789 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg );
790
791 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
792
793 TEST_ASSERT( psa_import_key( slot, key_type,
794 key_data, key_size ) == PSA_SUCCESS );
795 TEST_ASSERT( psa_get_key_information( slot,
796 NULL,
797 &key_bits ) == PSA_SUCCESS );
798
799 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, alg, key_bits );
800 TEST_ASSERT( signature_size != 0 );
801 signature = mbedtls_calloc( 1, signature_size );
802 TEST_ASSERT( signature != NULL );
803
804 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
805 input_data, input_size,
806 NULL, 0,
807 signature, signature_size,
808 &signature_length ) == PSA_SUCCESS );
809 TEST_ASSERT( signature_length == output_size );
810 TEST_ASSERT( memcmp( signature, output_data, output_size ) == 0 );
811
812exit:
813 psa_destroy_key( slot );
814 mbedtls_free( key_data );
815 mbedtls_free( input_data );
816 mbedtls_free( output_data );
817 mbedtls_free( signature );
818 mbedtls_psa_crypto_free( );
819}
820/* END_CASE */
821
822/* BEGIN_CASE */
823void sign_fail( int key_type_arg, char *key_hex,
824 int alg_arg, char *input_hex,
825 int signature_size, int expected_status_arg )
826{
827 int slot = 1;
828 psa_key_type_t key_type = key_type_arg;
829 psa_algorithm_t alg = alg_arg;
830 unsigned char *key_data = NULL;
831 size_t key_size;
832 unsigned char *input_data = NULL;
833 size_t input_size;
834 psa_status_t actual_status;
835 psa_status_t expected_status = expected_status_arg;
836 unsigned char *signature = NULL;
837 size_t signature_length = 0xdeadbeef;
838 psa_key_policy_t policy = {0};
839
840 key_data = unhexify_alloc( key_hex, &key_size );
841 TEST_ASSERT( key_data != NULL );
842 input_data = unhexify_alloc( input_hex, &input_size );
843 TEST_ASSERT( input_data != NULL );
844 signature = mbedtls_calloc( 1, signature_size );
845 TEST_ASSERT( signature != NULL );
846
847 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
848
849 psa_key_policy_init( &policy );
850
851 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg );
852
853 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
854
855 TEST_ASSERT( psa_import_key( slot, key_type,
856 key_data, key_size ) == PSA_SUCCESS );
857
858 actual_status = psa_asymmetric_sign( slot, alg,
859 input_data, input_size,
860 NULL, 0,
861 signature, signature_size,
862 &signature_length );
863 TEST_ASSERT( actual_status == expected_status );
864 TEST_ASSERT( signature_length == 0 );
865
866exit:
867 psa_destroy_key( slot );
868 mbedtls_free( key_data );
869 mbedtls_free( input_data );
870 mbedtls_free( signature );
871 mbedtls_psa_crypto_free( );
872}
873/* END_CASE */
874
875/* BEGIN_CASE */
876void key_policy( int usage_arg, int alg_arg )
877{
878 int key_slot = 1;
879 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
880 unsigned char key[32] = {0};
881 psa_key_policy_t policy_set = {0};
882 psa_key_policy_t policy_get = {0};
883
884 memset( key, 0x2a, sizeof( key ) );
885
886 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
887
888 psa_key_policy_init(& policy_set );
889 psa_key_policy_init(& policy_get );
890
891 psa_key_policy_set_usage( &policy_set, usage_arg, alg_arg );
892
893 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == ( psa_key_usage_t )usage_arg );
894
895 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set) == ( psa_algorithm_t )alg_arg );
896
897 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
898
899 TEST_ASSERT( psa_import_key( key_slot, key_type,
900 key, sizeof( key ) ) == PSA_SUCCESS );
901
902 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
903
904 TEST_ASSERT( policy_get.usage == policy_set.usage );
905 TEST_ASSERT( policy_get.alg == policy_set.alg );
906
907exit:
908 psa_destroy_key( key_slot );
909 mbedtls_psa_crypto_free( );
910}
911/* END_CASE */
912
913/* BEGIN_CASE */
914void key_policy_fail( int usage_arg, int alg_arg, int expected_status, char *key_hex )
915{
916 int key_slot = 1;
917 unsigned char* keypair = NULL;
918 size_t key_size = 0;
919 size_t signature_length = 0;
920 psa_key_policy_t policy = {0};
921 int actual_status = PSA_SUCCESS;
922
923 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
924
925 psa_key_policy_init( &policy );
926
927 psa_key_policy_set_usage( &policy, usage_arg, alg_arg );
928
929 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
930
931 if( usage_arg & PSA_KEY_USAGE_EXPORT )
932 {
933 keypair = unhexify_alloc( key_hex, &key_size );
934 TEST_ASSERT( keypair != NULL );
935 TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR,
936 keypair, key_size ) == PSA_SUCCESS );
937 actual_status = psa_asymmetric_sign( key_slot,
938 ( psa_algorithm_t )alg_arg, NULL, 0, NULL, 0,
939 NULL, 0, &signature_length );
940 }
941
942 if( usage_arg & PSA_KEY_USAGE_SIGN )
943 {
944 keypair = unhexify_alloc( key_hex, &key_size );
945 TEST_ASSERT( keypair != NULL );
946 TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR,
947 keypair, key_size ) == PSA_SUCCESS );
948 actual_status = psa_export_key( key_slot, NULL, 0, NULL );
949 }
950
951 TEST_ASSERT( actual_status == expected_status );
952
953exit:
954 psa_destroy_key( key_slot );
955 mbedtls_free( keypair );
956 mbedtls_psa_crypto_free( );
957}
958/* END_CASE */
959
960/* BEGIN_CASE */
961void key_lifetime( int lifetime_arg )
962{
963 int key_slot = 1;
964 psa_key_type_t key_type = PSA_ALG_CBC_BASE;
965 unsigned char key[32] = {0};
966 psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg;
967 psa_key_lifetime_t lifetime_get;
968
969 memset( key, 0x2a, sizeof( key ) );
970
971 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
972
973 TEST_ASSERT( psa_set_key_lifetime( key_slot,
974 lifetime_set ) == PSA_SUCCESS );
975
976 TEST_ASSERT( psa_import_key( key_slot, key_type,
977 key, sizeof( key ) ) == PSA_SUCCESS );
978
979 TEST_ASSERT( psa_get_key_lifetime( key_slot,
980 &lifetime_get ) == PSA_SUCCESS );
981
982 TEST_ASSERT( lifetime_get == lifetime_set );
983
984exit:
985 psa_destroy_key( key_slot );
986 mbedtls_psa_crypto_free( );
987}
988/* END_CASE */
989
990
991/* BEGIN_CASE */
992void key_lifetime_set_fail( int key_slot_arg, int lifetime_arg, int expected_status_arg )
993{
994 int key_slot = 1;
995 psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg;
996 psa_status_t actual_status;
997 psa_status_t expected_status = expected_status_arg;
998
999 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1000
1001 actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set );
1002
1003 if( actual_status == PSA_SUCCESS )
1004 actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set );
1005
1006 TEST_ASSERT( expected_status == actual_status );
1007
1008exit:
1009 psa_destroy_key( key_slot );
1010 mbedtls_psa_crypto_free( );
1011}
1012/* END_CASE */