blob: 906100328928de7b2310fc018dbc0736a964b684 [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;
Moran Peker9e3aa622018-06-07 12:08:47 +0300285 size_t output_size, max_output_size = 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 ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200300
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
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200308 TEST_ASSERT( psa_encrypt_set_iv( &operation,
309 iv, sizeof( iv ) ) == PSA_SUCCESS );
Moran Peker9e3aa622018-06-07 12:08:47 +0300310 max_output_size = input_size + operation.block_size;
311 output = mbedtls_calloc( 1, max_output_size );
Moran Peker96cc00a2018-05-31 14:03:56 +0300312
313 TEST_ASSERT( psa_cipher_update( &operation, input, input_size,
Moran Peker9e3aa622018-06-07 12:08:47 +0300314 output, max_output_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200315 &output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300316 status = psa_cipher_finish( &operation, output + output_length,
Moran Peker9e3aa622018-06-07 12:08:47 +0300317 max_output_size, &output_length );
Moran Pekerded84402018-06-06 16:36:50 +0300318 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 */
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200335void cipher_test_encrypt_multipart( int alg_arg, int key_type_arg,
336 char *key_hex,
337 char *input_hex,
338 int first_part_size, char *output_hex )
Moran Peker7691fb72018-05-31 16:15:31 +0300339{
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 Peker9e3aa622018-06-07 12:08:47 +0300350 size_t output_size, max_output_size = 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 ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200365
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
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200373 TEST_ASSERT( psa_encrypt_set_iv( &operation,
374 iv, sizeof( iv ) ) == PSA_SUCCESS );
Moran Peker9e3aa622018-06-07 12:08:47 +0300375 max_output_size = input_size + operation.block_size;
376 output = mbedtls_calloc( 1, max_output_size );
Moran Peker7691fb72018-05-31 16:15:31 +0300377
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200378 TEST_ASSERT( (unsigned int) first_part_size < input_size );
Moran Peker7691fb72018-05-31 16:15:31 +0300379 TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size,
Moran Peker9e3aa622018-06-07 12:08:47 +0300380 output, max_output_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200381 &output_length ) == PSA_SUCCESS );
382 TEST_ASSERT( psa_cipher_update( &operation,
383 input + first_part_size,
384 input_size - first_part_size,
Moran Peker9e3aa622018-06-07 12:08:47 +0300385 output, max_output_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200386 &output_length ) == PSA_SUCCESS );
Moran Peker7691fb72018-05-31 16:15:31 +0300387 TEST_ASSERT( psa_cipher_finish( &operation, output + output_length,
Moran Peker9e3aa622018-06-07 12:08:47 +0300388 max_output_size, &output_length ) == PSA_SUCCESS );
Moran Peker7691fb72018-05-31 16:15:31 +0300389
390 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
391
392 TEST_ASSERT( input_size == output_size );
393 TEST_ASSERT( memcmp( expected_output, output, output_size ) == 0 );
394
395exit:
396 mbedtls_free( key );
397 mbedtls_free( input );
398 psa_destroy_key( key_slot );
399 mbedtls_psa_crypto_free( );
400}
401/* END_CASE */
402
403/* BEGIN_CASE */
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200404void cipher_test_decrypt_multipart( int alg_arg, int key_type_arg,
405 char *key_hex,
406 char *input_hex,
407 int first_part_size, char *output_hex )
mohammad1603b152d4d2018-04-11 23:51:20 -0700408{
409 int key_slot = 1;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200410
mohammad1603b152d4d2018-04-11 23:51:20 -0700411 psa_key_type_t key_type = key_type_arg;
412 psa_algorithm_t alg = alg_arg;
413 unsigned char *key = NULL;
414 size_t key_size;
415 unsigned char iv[16] = {0};
416 unsigned char *input = NULL;
417 size_t input_size = 0;
418 unsigned char *output;
419 unsigned char *expected_output;
Moran Peker9e3aa622018-06-07 12:08:47 +0300420 size_t output_size, max_output_size = 0;
mohammad1603b152d4d2018-04-11 23:51:20 -0700421 size_t output_length = 0;
422 psa_cipher_operation_t operation;
423
424
425 key = unhexify_alloc( key_hex, &key_size );
426 TEST_ASSERT( key != NULL );
427
428 input = unhexify_alloc( input_hex, &input_size );
429 TEST_ASSERT( input != NULL );
430
431 expected_output = unhexify_alloc( output_hex, &output_size );
432 TEST_ASSERT( expected_output != NULL );
433
434 memset( iv, 0x2a, sizeof( iv ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200435
mohammad1603b152d4d2018-04-11 23:51:20 -0700436 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
437
438 TEST_ASSERT( psa_import_key( key_slot, key_type,
439 key, key_size ) == PSA_SUCCESS );
440
441 TEST_ASSERT( psa_decrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS );
442
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200443 TEST_ASSERT( psa_encrypt_set_iv( &operation,
444 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad1603b152d4d2018-04-11 23:51:20 -0700445
Moran Peker9e3aa622018-06-07 12:08:47 +0300446 output_simax_output_sizeze_1 = input_size + operation.block_size;
447 output = mbedtls_calloc( 1, max_output_size );
mohammad1603b152d4d2018-04-11 23:51:20 -0700448
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200449 TEST_ASSERT( (unsigned int) first_part_size < input_size );
Moran Pekerded84402018-06-06 16:36:50 +0300450 TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size,
Moran Peker9e3aa622018-06-07 12:08:47 +0300451 output, max_output_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200452 &output_length ) == PSA_SUCCESS );
453 TEST_ASSERT( psa_cipher_update( &operation,
454 input + first_part_size,
455 input_size - first_part_size,
Moran Peker9e3aa622018-06-07 12:08:47 +0300456 output, max_output_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200457 &output_length ) == PSA_SUCCESS );
Moran Pekerf55e8042018-05-29 16:28:28 +0300458 TEST_ASSERT( psa_cipher_finish( &operation, output + output_length,
Moran Peker9e3aa622018-06-07 12:08:47 +0300459 max_output_size, &output_length ) == PSA_SUCCESS );
mohammad1603b152d4d2018-04-11 23:51:20 -0700460 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
461
462 TEST_ASSERT( input_size == output_size );
463 TEST_ASSERT( memcmp( expected_output, output, output_size ) == 0 );
464
465exit:
466 mbedtls_free( key );
467 mbedtls_free( input );
468 psa_destroy_key( key_slot );
469 mbedtls_psa_crypto_free( );
470}
471/* END_CASE */
472
473
mohammad1603d7d7ba52018-03-12 18:51:53 +0200474/* BEGIN_CASE */
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200475void cipher_test_decrypt( int alg_arg, int key_type_arg,
476 char *key_hex,
477 char *input_hex, char *output_hex,
478 int expected_status )
Moran Pekerded84402018-06-06 16:36:50 +0300479{
480 int key_slot = 1;
481 psa_status_t status;
482 psa_key_type_t key_type = key_type_arg;
483 psa_algorithm_t alg = alg_arg;
484 unsigned char *key = NULL;
485 size_t key_size;
486 unsigned char iv[16] = {0};
487 unsigned char *input = NULL;
488 size_t input_size = 0;
489 unsigned char *output;
490 unsigned char *expected_output;
Moran Peker9e3aa622018-06-07 12:08:47 +0300491 size_t output_size, max_output_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +0300492 size_t output_length = 0;
493 psa_cipher_operation_t operation;
494
495
496 key = unhexify_alloc( key_hex, &key_size );
497 TEST_ASSERT( key != NULL );
498
499 input = unhexify_alloc( input_hex, &input_size );
500 TEST_ASSERT( input != NULL );
501
502 expected_output = unhexify_alloc( output_hex, &output_size );
503 TEST_ASSERT( expected_output != NULL );
504
505 memset( iv, 0x2a, sizeof( iv ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200506
Moran Pekerded84402018-06-06 16:36:50 +0300507 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
508
509 TEST_ASSERT( psa_import_key( key_slot, key_type,
510 key, key_size ) == PSA_SUCCESS );
511
512 TEST_ASSERT( psa_decrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS );
513
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200514 TEST_ASSERT( psa_encrypt_set_iv( &operation,
515 iv, sizeof( iv ) ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300516
Moran Peker9e3aa622018-06-07 12:08:47 +0300517 max_output_size = input_size + operation.block_size;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200518 output = mbedtls_calloc( 1, output_size );
Moran Pekerded84402018-06-06 16:36:50 +0300519
520 TEST_ASSERT( psa_cipher_update( &operation, input, input_size,
Moran Peker9e3aa622018-06-07 12:08:47 +0300521 output, max_output_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200522 &output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300523 status = psa_cipher_finish( &operation, output + output_length,
Moran Peker9e3aa622018-06-07 12:08:47 +0300524 max_output_size, &output_length );
Moran Pekerded84402018-06-06 16:36:50 +0300525 TEST_ASSERT( status == (psa_status_t) expected_status );
526
527 if( expected_status == PSA_SUCCESS )
528 {
529 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
530
531 TEST_ASSERT( input_size == output_size );
532 TEST_ASSERT( memcmp( expected_output, output, output_size ) == 0 );
533 }
534
535
536exit:
537 mbedtls_free( key );
538 mbedtls_free( input );
539 psa_destroy_key( key_slot );
540 mbedtls_psa_crypto_free( );
541}
542/* END_CASE */
543
544
545/* BEGIN_CASE */
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200546void cipher_test_verify_output( int alg_arg, int key_type_arg,
547 char *key_hex,
548 char *input_hex )
mohammad1603d7d7ba52018-03-12 18:51:53 +0200549{
550 int key_slot = 1;
551 psa_key_type_t key_type = key_type_arg;
552 psa_algorithm_t alg = alg_arg;
553 unsigned char *key = NULL;
554 size_t key_size;
mohammad1603e6b67a12018-03-12 10:38:49 -0700555 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +0200556 size_t iv_size = 16;
557 size_t iv_length = 0;
558 unsigned char *input = NULL;
559 size_t input_size = 0;
Moran Peker3205a652018-03-20 17:09:59 +0200560 unsigned char *output1;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200561 size_t output1_size = 0;
562 size_t output1_length = 0;
Moran Peker3205a652018-03-20 17:09:59 +0200563 unsigned char *output2;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200564 size_t output2_size = 0;
565 size_t output2_length = 0;
566 size_t tmp_output_length = 0;
567 psa_cipher_operation_t operation1;
568 psa_cipher_operation_t operation2;
569
570 key = unhexify_alloc( key_hex, &key_size );
571 TEST_ASSERT( key != NULL );
572
573 input = unhexify_alloc( input_hex, &input_size );
574 TEST_ASSERT( input != NULL );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200575
mohammad1603d7d7ba52018-03-12 18:51:53 +0200576 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
577
578 TEST_ASSERT( psa_import_key( key_slot, key_type,
579 key, key_size ) == PSA_SUCCESS );
580
Moran Pekerf55e8042018-05-29 16:28:28 +0300581 TEST_ASSERT( psa_encrypt_setup( &operation1, key_slot, alg ) == PSA_SUCCESS );
582 TEST_ASSERT( psa_decrypt_setup( &operation2, key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200583
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200584 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
585 iv, iv_size,
586 &iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300587 output1_size = input_size + operation1.block_size;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200588 output1 = mbedtls_calloc( 1, output1_size );
589 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +0300590
mohammad1603d7d7ba52018-03-12 18:51:53 +0200591 TEST_ASSERT( psa_cipher_update( &operation1, input, input_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200592 output1, output1_size,
593 &output1_length ) == PSA_SUCCESS );
594 TEST_ASSERT( psa_cipher_finish( &operation1,
595 output1 + output1_length, output1_size,
596 &tmp_output_length ) == PSA_SUCCESS );
597
Moran Pekerded84402018-06-06 16:36:50 +0300598 output1_length += tmp_output_length;
599
600 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
601
602 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200603 output2 = mbedtls_calloc( 1, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +0300604
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200605 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
606 iv, iv_length ) == PSA_SUCCESS );
607 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
608 output2, output2_size,
609 &output2_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300610 tmp_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200611 TEST_ASSERT( psa_cipher_finish( &operation2,
612 output2 + output2_length,
613 output2_size,
614 &tmp_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300615
616 output2_length += tmp_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200617
Moran Pekerded84402018-06-06 16:36:50 +0300618 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
619
620 TEST_ASSERT( input_size == output2_length );
621 TEST_ASSERT( memcmp( input, output2, input_size ) == 0 );
622
623exit:
624 mbedtls_free( key );
625 mbedtls_free( input );
Moran Pekerded84402018-06-06 16:36:50 +0300626 psa_destroy_key( key_slot );
627 mbedtls_psa_crypto_free( );
628}
629/* END_CASE */
630
631/* BEGIN_CASE */
632void cipher_test_verify_output_multpart( int alg_arg,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200633 int key_type_arg,
634 char *key_hex,
635 char *input_hex,
636 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +0300637{
638 int key_slot = 1;
639 psa_key_type_t key_type = key_type_arg;
640 psa_algorithm_t alg = alg_arg;
641 unsigned char *key = NULL;
642 size_t key_size;
643 unsigned char iv[16] = {0};
644 size_t iv_size = 16;
645 size_t iv_length = 0;
646 unsigned char *input = NULL;
647 size_t input_size = 0;
648 unsigned char *output1;
649 size_t output1_size = 0;
650 size_t output1_length = 0;
651 unsigned char *output2;
652 size_t output2_size = 0;
653 size_t output2_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200654 size_t tmp_output_length, temp = 0;
Moran Pekerded84402018-06-06 16:36:50 +0300655 psa_cipher_operation_t operation1;
656 psa_cipher_operation_t operation2;
657
658 key = unhexify_alloc( key_hex, &key_size );
659 TEST_ASSERT( key != NULL );
660
661 input = unhexify_alloc( input_hex, &input_size );
662 TEST_ASSERT( input != NULL );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200663
Moran Pekerded84402018-06-06 16:36:50 +0300664 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
665
666 TEST_ASSERT( psa_import_key( key_slot, key_type,
667 key, key_size ) == PSA_SUCCESS );
668
669 TEST_ASSERT( psa_encrypt_setup( &operation1, key_slot, alg ) == PSA_SUCCESS );
670 TEST_ASSERT( psa_decrypt_setup( &operation2, key_slot, alg ) == PSA_SUCCESS );
671
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200672 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
673 iv, iv_size,
674 &iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300675 output1_size = input_size + operation1.block_size;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200676 output1 = mbedtls_calloc( 1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +0300677
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200678 TEST_ASSERT( (unsigned int) first_part_size < input_size );
679
Moran Pekerded84402018-06-06 16:36:50 +0300680 TEST_ASSERT( psa_cipher_update( &operation1, input, first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200681 output1, output1_size,
682 &output1_length ) == PSA_SUCCESS );
683 temp = output1_length;
Moran Pekerded84402018-06-06 16:36:50 +0300684
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200685 TEST_ASSERT( psa_cipher_update( &operation1,
686 input + first_part_size,
687 input_size - first_part_size,
688 output1, output1_size,
689 &output1_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300690 output1_length += temp;
691
692 TEST_ASSERT( psa_cipher_finish( &operation1, output1 + output1_length,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200693 output1_size - output1_length,
694 &tmp_output_length ) == PSA_SUCCESS );
Gilles Peskine691dfb32018-06-06 15:18:02 +0200695
mohammad1603d7d7ba52018-03-12 18:51:53 +0200696 output1_length += tmp_output_length;
697
698 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
699
Moran Pekerf55e8042018-05-29 16:28:28 +0300700 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200701 output2 = mbedtls_calloc( 1, output2_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200702
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200703 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
704 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300705
706 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200707 output2, output2_size,
708 &output2_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300709
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200710 temp = output2_length;
Moran Pekerded84402018-06-06 16:36:50 +0300711
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200712 TEST_ASSERT( psa_cipher_update( &operation2, output1 + first_part_size,
713 output1_length - first_part_size,
714 output2, output2_size,
715 &output2_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300716
717 output2_length += temp;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200718 tmp_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200719 TEST_ASSERT( psa_cipher_finish( &operation2,
720 output2 + output2_length,
721 output2_size - output2_length,
722 &tmp_output_length ) == PSA_SUCCESS );
Moran Pekerf55e8042018-05-29 16:28:28 +0300723
724 output2_length += tmp_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200725
mohammad1603d7d7ba52018-03-12 18:51:53 +0200726 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
727
Moran Pekerded84402018-06-06 16:36:50 +0300728 TEST_ASSERT( input_size == output2_length );
mohammad1603e6b67a12018-03-12 10:38:49 -0700729 TEST_ASSERT( memcmp( input, output2, input_size ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200730
731exit:
732 mbedtls_free( key );
733 mbedtls_free( input );
734 psa_destroy_key( key_slot );
735 mbedtls_psa_crypto_free( );
736}
737/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +0200738
739/* BEGIN_CASE */
740void signature_size( int type_arg, int bits, int alg_arg, int expected_size_arg )
741{
742 psa_key_type_t type = type_arg;
743 psa_algorithm_t alg = alg_arg;
744 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(type, bits, alg);
745 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
746exit:
747 ;
748}
749/* END_CASE */
750
751/* BEGIN_CASE */
752void sign_deterministic( int key_type_arg, char *key_hex,
753 int alg_arg, char *input_hex, char *output_hex )
754{
755 int slot = 1;
756 psa_key_type_t key_type = key_type_arg;
757 psa_algorithm_t alg = alg_arg;
758 unsigned char *key_data = NULL;
759 size_t key_size;
760 size_t key_bits;
761 unsigned char *input_data = NULL;
762 size_t input_size;
763 unsigned char *output_data = NULL;
764 size_t output_size;
765 unsigned char *signature = NULL;
766 size_t signature_size;
767 size_t signature_length = 0xdeadbeef;
768 psa_key_policy_t policy = {0};
769
770 key_data = unhexify_alloc( key_hex, &key_size );
771 TEST_ASSERT( key_data != NULL );
772 input_data = unhexify_alloc( input_hex, &input_size );
773 TEST_ASSERT( input_data != NULL );
774 output_data = unhexify_alloc( output_hex, &output_size );
775 TEST_ASSERT( output_data != NULL );
776
777 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
778
779 psa_key_policy_init( &policy );
780
781 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg );
782
783 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
784
785 TEST_ASSERT( psa_import_key( slot, key_type,
786 key_data, key_size ) == PSA_SUCCESS );
787 TEST_ASSERT( psa_get_key_information( slot,
788 NULL,
789 &key_bits ) == PSA_SUCCESS );
790
791 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, alg, key_bits );
792 TEST_ASSERT( signature_size != 0 );
793 signature = mbedtls_calloc( 1, signature_size );
794 TEST_ASSERT( signature != NULL );
795
796 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
797 input_data, input_size,
798 NULL, 0,
799 signature, signature_size,
800 &signature_length ) == PSA_SUCCESS );
801 TEST_ASSERT( signature_length == output_size );
802 TEST_ASSERT( memcmp( signature, output_data, output_size ) == 0 );
803
804exit:
805 psa_destroy_key( slot );
806 mbedtls_free( key_data );
807 mbedtls_free( input_data );
808 mbedtls_free( output_data );
809 mbedtls_free( signature );
810 mbedtls_psa_crypto_free( );
811}
812/* END_CASE */
813
814/* BEGIN_CASE */
815void sign_fail( int key_type_arg, char *key_hex,
816 int alg_arg, char *input_hex,
817 int signature_size, int expected_status_arg )
818{
819 int slot = 1;
820 psa_key_type_t key_type = key_type_arg;
821 psa_algorithm_t alg = alg_arg;
822 unsigned char *key_data = NULL;
823 size_t key_size;
824 unsigned char *input_data = NULL;
825 size_t input_size;
826 psa_status_t actual_status;
827 psa_status_t expected_status = expected_status_arg;
828 unsigned char *signature = NULL;
829 size_t signature_length = 0xdeadbeef;
830 psa_key_policy_t policy = {0};
831
832 key_data = unhexify_alloc( key_hex, &key_size );
833 TEST_ASSERT( key_data != NULL );
834 input_data = unhexify_alloc( input_hex, &input_size );
835 TEST_ASSERT( input_data != NULL );
836 signature = mbedtls_calloc( 1, signature_size );
837 TEST_ASSERT( signature != NULL );
838
839 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
840
841 psa_key_policy_init( &policy );
842
843 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg );
844
845 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
846
847 TEST_ASSERT( psa_import_key( slot, key_type,
848 key_data, key_size ) == PSA_SUCCESS );
849
850 actual_status = psa_asymmetric_sign( slot, alg,
851 input_data, input_size,
852 NULL, 0,
853 signature, signature_size,
854 &signature_length );
855 TEST_ASSERT( actual_status == expected_status );
856 TEST_ASSERT( signature_length == 0 );
857
858exit:
859 psa_destroy_key( slot );
860 mbedtls_free( key_data );
861 mbedtls_free( input_data );
862 mbedtls_free( signature );
863 mbedtls_psa_crypto_free( );
864}
865/* END_CASE */
866
867/* BEGIN_CASE */
868void key_policy( int usage_arg, int alg_arg )
869{
870 int key_slot = 1;
871 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
872 unsigned char key[32] = {0};
873 psa_key_policy_t policy_set = {0};
874 psa_key_policy_t policy_get = {0};
875
876 memset( key, 0x2a, sizeof( key ) );
877
878 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
879
880 psa_key_policy_init(& policy_set );
881 psa_key_policy_init(& policy_get );
882
883 psa_key_policy_set_usage( &policy_set, usage_arg, alg_arg );
884
885 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == ( psa_key_usage_t )usage_arg );
886
887 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set) == ( psa_algorithm_t )alg_arg );
888
889 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
890
891 TEST_ASSERT( psa_import_key( key_slot, key_type,
892 key, sizeof( key ) ) == PSA_SUCCESS );
893
894 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
895
896 TEST_ASSERT( policy_get.usage == policy_set.usage );
897 TEST_ASSERT( policy_get.alg == policy_set.alg );
898
899exit:
900 psa_destroy_key( key_slot );
901 mbedtls_psa_crypto_free( );
902}
903/* END_CASE */
904
905/* BEGIN_CASE */
906void key_policy_fail( int usage_arg, int alg_arg, int expected_status, char *key_hex )
907{
908 int key_slot = 1;
909 unsigned char* keypair = NULL;
910 size_t key_size = 0;
911 size_t signature_length = 0;
912 psa_key_policy_t policy = {0};
913 int actual_status = PSA_SUCCESS;
914
915 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
916
917 psa_key_policy_init( &policy );
918
919 psa_key_policy_set_usage( &policy, usage_arg, alg_arg );
920
921 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
922
923 if( usage_arg & PSA_KEY_USAGE_EXPORT )
924 {
925 keypair = unhexify_alloc( key_hex, &key_size );
926 TEST_ASSERT( keypair != NULL );
927 TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR,
928 keypair, key_size ) == PSA_SUCCESS );
929 actual_status = psa_asymmetric_sign( key_slot,
930 ( psa_algorithm_t )alg_arg, NULL, 0, NULL, 0,
931 NULL, 0, &signature_length );
932 }
933
934 if( usage_arg & PSA_KEY_USAGE_SIGN )
935 {
936 keypair = unhexify_alloc( key_hex, &key_size );
937 TEST_ASSERT( keypair != NULL );
938 TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR,
939 keypair, key_size ) == PSA_SUCCESS );
940 actual_status = psa_export_key( key_slot, NULL, 0, NULL );
941 }
942
943 TEST_ASSERT( actual_status == expected_status );
944
945exit:
946 psa_destroy_key( key_slot );
947 mbedtls_free( keypair );
948 mbedtls_psa_crypto_free( );
949}
950/* END_CASE */
951
952/* BEGIN_CASE */
953void key_lifetime( int lifetime_arg )
954{
955 int key_slot = 1;
956 psa_key_type_t key_type = PSA_ALG_CBC_BASE;
957 unsigned char key[32] = {0};
958 psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg;
959 psa_key_lifetime_t lifetime_get;
960
961 memset( key, 0x2a, sizeof( key ) );
962
963 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
964
965 TEST_ASSERT( psa_set_key_lifetime( key_slot,
966 lifetime_set ) == PSA_SUCCESS );
967
968 TEST_ASSERT( psa_import_key( key_slot, key_type,
969 key, sizeof( key ) ) == PSA_SUCCESS );
970
971 TEST_ASSERT( psa_get_key_lifetime( key_slot,
972 &lifetime_get ) == PSA_SUCCESS );
973
974 TEST_ASSERT( lifetime_get == lifetime_set );
975
976exit:
977 psa_destroy_key( key_slot );
978 mbedtls_psa_crypto_free( );
979}
980/* END_CASE */
981
982
983/* BEGIN_CASE */
984void key_lifetime_set_fail( int key_slot_arg, int lifetime_arg, int expected_status_arg )
985{
986 int key_slot = 1;
987 psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg;
988 psa_status_t actual_status;
989 psa_status_t expected_status = expected_status_arg;
990
991 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
992
993 actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set );
994
995 if( actual_status == PSA_SUCCESS )
996 actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set );
997
998 TEST_ASSERT( expected_status == actual_status );
999
1000exit:
1001 psa_destroy_key( key_slot );
1002 mbedtls_psa_crypto_free( );
1003}
1004/* END_CASE */