blob: 22a5de69ddfcd79fc45b6ac27bd4a957cc3a704e [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
2#include "psa/crypto.h"
Gilles Peskine8c9def32018-02-08 10:02:12 +01003
4#include "mbedtls/md.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +01005/* END_HEADER */
6
7/* BEGIN_DEPENDENCIES
8 * depends_on:MBEDTLS_PSA_CRYPTO_C
9 * END_DEPENDENCIES
10 */
11
12/* BEGIN_CASE */
13void init_deinit()
14{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010015 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +010016 int i;
17 for( i = 0; i <= 1; i++ )
18 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010019 status = psa_crypto_init( );
20 TEST_ASSERT( status == PSA_SUCCESS );
21 status = psa_crypto_init( );
22 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +010023 mbedtls_psa_crypto_free( );
24 }
25}
26/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027
28/* BEGIN_CASE */
29void import( char *hex, int type, int expected_status )
30{
31 int slot = 1;
32 psa_status_t status;
33 unsigned char *data = NULL;
34 size_t data_size;
35
Gilles Peskine40f68b92018-03-07 16:43:36 +010036 data = unhexify_alloc( hex, &data_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010037 TEST_ASSERT( data != NULL );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010038 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
39
40 status = psa_import_key( slot, type, data, data_size );
41 TEST_ASSERT( status == (psa_status_t) expected_status );
42 if( status == PSA_SUCCESS )
43 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
44
45exit:
46 mbedtls_free( data );
47 mbedtls_psa_crypto_free( );
48}
49/* END_CASE */
50
51/* BEGIN_CASE */
Moran Pekera964a8f2018-06-04 18:42:36 +030052void import_export( char *hex,
53 int type_arg,
54 int alg_arg,
55 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010056 int expected_bits,
57 int export_size_delta,
58 int expected_export_status,
59 int canonical_input )
60{
61 int slot = 1;
62 int slot2 = slot + 1;
63 psa_key_type_t type = type_arg;
64 psa_status_t status;
65 unsigned char *data = NULL;
66 unsigned char *exported = NULL;
67 unsigned char *reexported = NULL;
68 size_t data_size;
69 size_t export_size;
70 size_t exported_length;
71 size_t reexported_length;
72 psa_key_type_t got_type;
73 size_t got_bits;
mohammad1603a97cb8c2018-03-28 03:46:26 -070074 psa_key_policy_t policy = {0};
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075
Gilles Peskine40f68b92018-03-07 16:43:36 +010076 data = unhexify_alloc( hex, &data_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010077 TEST_ASSERT( data != NULL );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010078 export_size = (ssize_t) data_size + export_size_delta;
79 exported = mbedtls_calloc( 1, export_size );
80 TEST_ASSERT( exported != NULL );
81 if( ! canonical_input )
82 {
83 reexported = mbedtls_calloc( 1, export_size );
84 TEST_ASSERT( reexported != NULL );
85 }
86 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
87
mohammad1603a97cb8c2018-03-28 03:46:26 -070088 psa_key_policy_init( &policy );
89
Moran Pekera964a8f2018-06-04 18:42:36 +030090 psa_key_policy_set_usage( &policy, usage_arg, alg_arg );
mohammad1603a97cb8c2018-03-28 03:46:26 -070091
92 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094 /* Import the key */
95 TEST_ASSERT( psa_import_key( slot, type,
96 data, data_size ) == PSA_SUCCESS );
97
98 /* Test the key information */
99 TEST_ASSERT( psa_get_key_information( slot,
100 &got_type, &got_bits ) ==
101 PSA_SUCCESS );
102 TEST_ASSERT( got_type == type );
103 TEST_ASSERT( got_bits == (size_t) expected_bits );
104
105 /* Export the key */
106 status = psa_export_key( slot,
107 exported, export_size,
108 &exported_length );
109 TEST_ASSERT( status == (psa_status_t) expected_export_status );
110 if( status != PSA_SUCCESS )
111 goto destroy;
112
113 if( canonical_input )
114 {
115 TEST_ASSERT( exported_length == data_size );
116 TEST_ASSERT( memcmp( exported, data, data_size ) == 0 );
117 }
118 else
119 {
mohammad1603a97cb8c2018-03-28 03:46:26 -0700120 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
121
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100122 TEST_ASSERT( psa_import_key( slot2, type,
123 exported, export_size ) ==
124 PSA_SUCCESS );
125 TEST_ASSERT( psa_export_key( slot2,
126 reexported, export_size,
127 &reexported_length ) ==
128 PSA_SUCCESS );
129 TEST_ASSERT( reexported_length == exported_length );
130 TEST_ASSERT( memcmp( reexported, exported,
131 exported_length ) == 0 );
132 }
133
134destroy:
135 /* Destroy the key */
136 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
137 TEST_ASSERT( psa_get_key_information(
138 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
139
140exit:
141 mbedtls_free( data );
142 mbedtls_psa_crypto_free( );
143}
144/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100145
Moran Pekerf709f4a2018-06-06 17:26:04 +0300146
147/* BEGIN_CASE */
148void import_export_public_key( char *hex,
149 int type_arg,
150 int alg_arg,
151 int expected_bits,
152 int public_key_expected_length,
153 int expected_export_status )
154{
155 int slot = 1;
156 psa_key_type_t type = type_arg;
157 psa_status_t status;
158 unsigned char *data = NULL;
159 unsigned char *exported = NULL;
160 size_t data_size;
161 size_t export_size;
162 size_t exported_length;
163 psa_key_type_t got_type;
164 size_t got_bits;
165 psa_key_policy_t policy = {0};
166
167 data = unhexify_alloc( hex, &data_size );
168 TEST_ASSERT( data != NULL );
169 export_size = (ssize_t) data_size ;
170 exported = mbedtls_calloc( 1, export_size );
171 TEST_ASSERT( exported != NULL );
172
173 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
174
175 psa_key_policy_init( &policy );
176
177 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT,
178 alg_arg );
179
180 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
181
182 /* Import the key */
183 TEST_ASSERT( psa_import_key( slot, type,
184 data, data_size ) == PSA_SUCCESS );
185
186 /* Test the key information */
187 TEST_ASSERT( psa_get_key_information( slot,
188 &got_type, &got_bits ) == PSA_SUCCESS );
189 TEST_ASSERT( got_type == type );
190 TEST_ASSERT( got_bits == (size_t) expected_bits );
191
192 /* Export the key */
193 status = psa_export_public_key( slot,
194 exported, export_size,
195 &exported_length );
196 TEST_ASSERT( status == (psa_status_t) expected_export_status );
197 if( status != PSA_SUCCESS )
198 goto destroy;
199
200 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
201
202destroy:
203 /* Destroy the key */
204 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
205 TEST_ASSERT( psa_get_key_information(
206 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
207
208exit:
209 mbedtls_free( data );
210 mbedtls_psa_crypto_free( );
211}
212/* END_CASE */
213
Gilles Peskine20035e32018-02-03 22:44:14 +0100214/* BEGIN_CASE */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100215void hash_finish( int alg_arg, char *input_hex, char *hash_hex )
216{
217 psa_algorithm_t alg = alg_arg;
218 unsigned char *input = NULL;
219 size_t input_size;
220 unsigned char expected_hash[MBEDTLS_MD_MAX_SIZE];
221 size_t expected_hash_length;
222 unsigned char actual_hash[MBEDTLS_MD_MAX_SIZE];
223 size_t actual_hash_length;
224 psa_hash_operation_t operation;
225
Gilles Peskine40f68b92018-03-07 16:43:36 +0100226 input = unhexify_alloc( input_hex, &input_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100227 TEST_ASSERT( input != NULL );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100228 expected_hash_length = unhexify( expected_hash, hash_hex );
229
230 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
231
232 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
233 TEST_ASSERT( psa_hash_update( &operation,
234 input, input_size ) == PSA_SUCCESS );
235 TEST_ASSERT( psa_hash_finish( &operation,
236 actual_hash, sizeof( actual_hash ),
237 &actual_hash_length ) == PSA_SUCCESS );
238 TEST_ASSERT( actual_hash_length == expected_hash_length );
239 TEST_ASSERT( memcmp( expected_hash, actual_hash,
240 expected_hash_length ) == 0 );
241
242exit:
243 mbedtls_free( input );
244 mbedtls_psa_crypto_free( );
245}
246/* END_CASE */
247
248/* BEGIN_CASE */
249void hash_verify( int alg_arg, char *input_hex, char *hash_hex )
250{
251 psa_algorithm_t alg = alg_arg;
252 unsigned char *input = NULL;
253 size_t input_size;
254 unsigned char expected_hash[MBEDTLS_MD_MAX_SIZE];
255 size_t expected_hash_length;
256 psa_hash_operation_t operation;
257
Gilles Peskine40f68b92018-03-07 16:43:36 +0100258 input = unhexify_alloc( input_hex, &input_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100259 TEST_ASSERT( input != NULL );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100260 expected_hash_length = unhexify( expected_hash, hash_hex );
261
262 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
263
264 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
265 TEST_ASSERT( psa_hash_update( &operation,
266 input, input_size ) == PSA_SUCCESS );
267 TEST_ASSERT( psa_hash_verify( &operation,
268 expected_hash,
269 expected_hash_length ) == PSA_SUCCESS );
270
271exit:
272 mbedtls_free( input );
273 mbedtls_psa_crypto_free( );
274}
275/* END_CASE */
276
277/* BEGIN_CASE */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100278void mac_verify( int key_type_arg, char *key_hex,
279 int alg_arg, char *iv_hex,
280 char *input_hex, char *mac_hex )
281{
282 int key_slot = 1;
283 psa_key_type_t key_type = key_type_arg;
284 psa_algorithm_t alg = alg_arg;
285 unsigned char *key = NULL;
286 size_t key_size;
287 unsigned char *iv = NULL;
288 size_t iv_size;
289 unsigned char *input = NULL;
290 size_t input_size;
291 unsigned char *expected_mac = NULL;
292 size_t expected_mac_size;
293 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -0700294 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100295
Gilles Peskine40f68b92018-03-07 16:43:36 +0100296 key = unhexify_alloc( key_hex, &key_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100297 TEST_ASSERT( key != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +0100298 if( iv_hex[0] != 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +0100299 {
Gilles Peskine40f68b92018-03-07 16:43:36 +0100300 iv = unhexify_alloc( iv_hex, &iv_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100301 TEST_ASSERT( iv != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100302 }
Gilles Peskine40f68b92018-03-07 16:43:36 +0100303 input = unhexify_alloc( input_hex, &input_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100304 TEST_ASSERT( input != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +0100305 expected_mac = unhexify_alloc( mac_hex, &expected_mac_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100306 TEST_ASSERT( expected_mac != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100307
308 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
309
mohammad16036df908f2018-04-02 08:34:15 -0700310 psa_key_policy_init( &policy );
311
312 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg_arg );
313
314 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
315
Gilles Peskine8c9def32018-02-08 10:02:12 +0100316 TEST_ASSERT( psa_import_key( key_slot, key_type,
317 key, key_size ) == PSA_SUCCESS );
318 // TODO: support IV
319 TEST_ASSERT( psa_mac_start( &operation, key_slot, alg ) == PSA_SUCCESS );
320 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
321 TEST_ASSERT( psa_mac_update( &operation,
322 input, input_size ) == PSA_SUCCESS );
323 TEST_ASSERT( psa_mac_verify( &operation,
324 expected_mac,
325 expected_mac_size ) == PSA_SUCCESS );
326
327exit:
328 mbedtls_free( key );
329 mbedtls_free( iv );
330 mbedtls_free( input );
331 mbedtls_free( expected_mac );
332 psa_destroy_key( key_slot );
333 mbedtls_psa_crypto_free( );
334}
335/* END_CASE */
336
337/* BEGIN_CASE */
Gilles Peskine0189e752018-02-03 23:57:22 +0100338void signature_size( int type_arg, int bits, int alg_arg, int expected_size_arg )
339{
340 psa_key_type_t type = type_arg;
341 psa_algorithm_t alg = alg_arg;
342 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(type, bits, alg);
343 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
344exit:
345 ;
346}
347/* END_CASE */
348
349/* BEGIN_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100350void sign_deterministic( int key_type_arg, char *key_hex,
351 int alg_arg, char *input_hex, char *output_hex )
352{
353 int slot = 1;
354 psa_key_type_t key_type = key_type_arg;
355 psa_algorithm_t alg = alg_arg;
356 unsigned char *key_data = NULL;
357 size_t key_size;
Gilles Peskine0189e752018-02-03 23:57:22 +0100358 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +0100359 unsigned char *input_data = NULL;
360 size_t input_size;
361 unsigned char *output_data = NULL;
362 size_t output_size;
Gilles Peskine0189e752018-02-03 23:57:22 +0100363 unsigned char *signature = NULL;
364 size_t signature_size;
Gilles Peskine93aa0332018-02-03 23:58:03 +0100365 size_t signature_length = 0xdeadbeef;
mohammad1603a97cb8c2018-03-28 03:46:26 -0700366 psa_key_policy_t policy = {0};
Gilles Peskine20035e32018-02-03 22:44:14 +0100367
Gilles Peskine40f68b92018-03-07 16:43:36 +0100368 key_data = unhexify_alloc( key_hex, &key_size );
Gilles Peskine20035e32018-02-03 22:44:14 +0100369 TEST_ASSERT( key_data != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +0100370 input_data = unhexify_alloc( input_hex, &input_size );
Gilles Peskine20035e32018-02-03 22:44:14 +0100371 TEST_ASSERT( input_data != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +0100372 output_data = unhexify_alloc( output_hex, &output_size );
Gilles Peskine20035e32018-02-03 22:44:14 +0100373 TEST_ASSERT( output_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +0100374
375 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
376
mohammad1603a97cb8c2018-03-28 03:46:26 -0700377 psa_key_policy_init( &policy );
378
379 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg );
380
381 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
382
Gilles Peskine20035e32018-02-03 22:44:14 +0100383 TEST_ASSERT( psa_import_key( slot, key_type,
384 key_data, key_size ) == PSA_SUCCESS );
Gilles Peskine0189e752018-02-03 23:57:22 +0100385 TEST_ASSERT( psa_get_key_information( slot,
386 NULL,
387 &key_bits ) == PSA_SUCCESS );
388
389 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, alg, key_bits );
390 TEST_ASSERT( signature_size != 0 );
391 signature = mbedtls_calloc( 1, signature_size );
392 TEST_ASSERT( signature != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +0100393
394 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
395 input_data, input_size,
396 NULL, 0,
Gilles Peskine0189e752018-02-03 23:57:22 +0100397 signature, signature_size,
Gilles Peskine20035e32018-02-03 22:44:14 +0100398 &signature_length ) == PSA_SUCCESS );
399 TEST_ASSERT( signature_length == output_size );
400 TEST_ASSERT( memcmp( signature, output_data, output_size ) == 0 );
401
402exit:
403 psa_destroy_key( slot );
404 mbedtls_free( key_data );
405 mbedtls_free( input_data );
406 mbedtls_free( output_data );
Gilles Peskine0189e752018-02-03 23:57:22 +0100407 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +0100408 mbedtls_psa_crypto_free( );
409}
410/* END_CASE */
411
412/* BEGIN_CASE */
413void sign_fail( int key_type_arg, char *key_hex,
414 int alg_arg, char *input_hex,
415 int signature_size, int expected_status_arg )
416{
417 int slot = 1;
418 psa_key_type_t key_type = key_type_arg;
419 psa_algorithm_t alg = alg_arg;
420 unsigned char *key_data = NULL;
421 size_t key_size;
422 unsigned char *input_data = NULL;
423 size_t input_size;
424 psa_status_t actual_status;
425 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +0100426 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +0100427 size_t signature_length = 0xdeadbeef;
mohammad1603a97cb8c2018-03-28 03:46:26 -0700428 psa_key_policy_t policy = {0};
Gilles Peskine20035e32018-02-03 22:44:14 +0100429
Gilles Peskine40f68b92018-03-07 16:43:36 +0100430 key_data = unhexify_alloc( key_hex, &key_size );
Gilles Peskine20035e32018-02-03 22:44:14 +0100431 TEST_ASSERT( key_data != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +0100432 input_data = unhexify_alloc( input_hex, &input_size );
Gilles Peskine20035e32018-02-03 22:44:14 +0100433 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +0100434 signature = mbedtls_calloc( 1, signature_size );
435 TEST_ASSERT( signature != NULL );
436
437 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
438
mohammad1603a97cb8c2018-03-28 03:46:26 -0700439 psa_key_policy_init( &policy );
440
441 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg );
442
443 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
444
Gilles Peskine20035e32018-02-03 22:44:14 +0100445 TEST_ASSERT( psa_import_key( slot, key_type,
446 key_data, key_size ) == PSA_SUCCESS );
447
448 actual_status = psa_asymmetric_sign( slot, alg,
449 input_data, input_size,
450 NULL, 0,
451 signature, signature_size,
452 &signature_length );
453 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine93aa0332018-02-03 23:58:03 +0100454 TEST_ASSERT( signature_length == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +0100455
456exit:
457 psa_destroy_key( slot );
458 mbedtls_free( key_data );
459 mbedtls_free( input_data );
460 mbedtls_free( signature );
461 mbedtls_psa_crypto_free( );
462}
463/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +0300464
465/* BEGIN_CASE */
466void key_policy( int usage_arg, int alg_arg )
467{
468 int key_slot = 1;
mohammad16034eed7572018-03-28 05:14:59 -0700469 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
mohammad16038cc1cee2018-03-28 01:21:33 +0300470 unsigned char key[32] = {0};
471 psa_key_policy_t policy_set = {0};
472 psa_key_policy_t policy_get = {0};
473
mohammad1603804cd712018-03-20 22:44:08 +0200474
mohammad16038cc1cee2018-03-28 01:21:33 +0300475 memset( key, 0x2a, sizeof( key ) );
476
477 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
478
479 psa_key_policy_init(& policy_set );
480 psa_key_policy_init(& policy_get );
481
482 psa_key_policy_set_usage( &policy_set, usage_arg, alg_arg );
483
484 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == ( psa_key_usage_t )usage_arg );
485
486 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set) == ( psa_algorithm_t )alg_arg );
487
488 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
489
490 TEST_ASSERT( psa_import_key( key_slot, key_type,
491 key, sizeof( key ) ) == PSA_SUCCESS );
492
493 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
494
495 TEST_ASSERT( policy_get.usage == policy_set.usage );
496 TEST_ASSERT( policy_get.alg == policy_set.alg );
497
Gilles Peskinea0655c32018-04-30 17:06:50 +0200498
499
mohammad1603804cd712018-03-20 22:44:08 +0200500
mohammad16038cc1cee2018-03-28 01:21:33 +0300501exit:
502 psa_destroy_key( key_slot );
503 mbedtls_psa_crypto_free( );
504}
505/* END_CASE */
506
mohammad16034eed7572018-03-28 05:14:59 -0700507/* BEGIN_CASE */
508void key_policy_fail( int usage_arg, int alg_arg, int expected_status, char *key_hex )
509{
510 int key_slot = 1;
mohammad16034eed7572018-03-28 05:14:59 -0700511 unsigned char* keypair = NULL;
512 size_t key_size = 0;
513 size_t signature_length = 0;
514 psa_key_policy_t policy = {0};
515 int actual_status = PSA_SUCCESS;
mohammad16034eed7572018-03-28 05:14:59 -0700516
517 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
518
519 psa_key_policy_init( &policy );
520
521 psa_key_policy_set_usage( &policy, usage_arg, alg_arg );
522
523 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
524
mohammad16036df908f2018-04-02 08:34:15 -0700525 if( usage_arg & PSA_KEY_USAGE_EXPORT )
mohammad16034eed7572018-03-28 05:14:59 -0700526 {
mohammad16036df908f2018-04-02 08:34:15 -0700527 keypair = unhexify_alloc( key_hex, &key_size );
528 TEST_ASSERT( keypair != NULL );
529 TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR,
530 keypair, key_size ) == PSA_SUCCESS );
531 actual_status = psa_asymmetric_sign( key_slot,
532 ( psa_algorithm_t )alg_arg, NULL, 0, NULL, 0,
533 NULL, 0, &signature_length );
534 }
535
536 if( usage_arg & PSA_KEY_USAGE_SIGN )
537 {
mohammad1603d926b882018-04-16 01:53:20 -0700538 keypair = unhexify_alloc( key_hex, &key_size );
539 TEST_ASSERT( keypair != NULL );
mohammad16036df908f2018-04-02 08:34:15 -0700540 TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR,
mohammad1603d926b882018-04-16 01:53:20 -0700541 keypair, key_size ) == PSA_SUCCESS );
mohammad16036df908f2018-04-02 08:34:15 -0700542 actual_status = psa_export_key( key_slot, NULL, 0, NULL );
mohammad16034eed7572018-03-28 05:14:59 -0700543 }
544
545 TEST_ASSERT( actual_status == expected_status );
546
547exit:
548 psa_destroy_key( key_slot );
mohammad1603d926b882018-04-16 01:53:20 -0700549 mbedtls_free( keypair );
mohammad16034eed7572018-03-28 05:14:59 -0700550 mbedtls_psa_crypto_free( );
551}
552/* END_CASE */
Gilles Peskinea0655c32018-04-30 17:06:50 +0200553
554/* BEGIN_CASE */
555void key_lifetime( int lifetime_arg )
556{
557 int key_slot = 1;
558 psa_key_type_t key_type = PSA_ALG_CBC_BASE;
559 unsigned char key[32] = {0};
560 psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg;
561 psa_key_lifetime_t lifetime_get;
562 memset( key, 0x2a, sizeof( key ) );
563 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
564 TEST_ASSERT( psa_set_key_lifetime( key_slot,
565 lifetime_set ) == PSA_SUCCESS );
566 TEST_ASSERT( psa_import_key( key_slot, key_type,
567 key, sizeof( key ) ) == PSA_SUCCESS );
568 TEST_ASSERT( psa_get_key_lifetime( key_slot,
569 &lifetime_get ) == PSA_SUCCESS );
570 TEST_ASSERT( lifetime_get == lifetime_set );
571exit:
572 psa_destroy_key( key_slot );
573 mbedtls_psa_crypto_free( );
574}
575/* END_CASE */
mohammad1603804cd712018-03-20 22:44:08 +0200576
577/* BEGIN_CASE */
578void key_lifetime_set_fail( int key_slot_arg, int lifetime_arg, int expected_status_arg )
579{
580 int key_slot = 1;
mohammad1603804cd712018-03-20 22:44:08 +0200581 psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg;
582 psa_status_t actual_status;
583 psa_status_t expected_status = expected_status_arg;
584
mohammad1603804cd712018-03-20 22:44:08 +0200585 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
586
mohammad1603804cd712018-03-20 22:44:08 +0200587 actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set );
588
589 if( actual_status == PSA_SUCCESS )
590 actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set );
591
592 TEST_ASSERT( expected_status == actual_status );
593
594exit:
mohammad1603804cd712018-03-20 22:44:08 +0200595 psa_destroy_key( key_slot );
596 mbedtls_psa_crypto_free( );
597}
598/* END_CASE */