blob: e7a4ea56c3bab52e9dee24ab9098bc8acb48c460 [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
146/* BEGIN_CASE */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100147void hash_finish( int alg_arg, char *input_hex, char *hash_hex )
148{
149 psa_algorithm_t alg = alg_arg;
150 unsigned char *input = NULL;
151 size_t input_size;
152 unsigned char expected_hash[MBEDTLS_MD_MAX_SIZE];
153 size_t expected_hash_length;
154 unsigned char actual_hash[MBEDTLS_MD_MAX_SIZE];
155 size_t actual_hash_length;
156 psa_hash_operation_t operation;
157
Gilles Peskine40f68b92018-03-07 16:43:36 +0100158 input = unhexify_alloc( input_hex, &input_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100159 TEST_ASSERT( input != NULL );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100160 expected_hash_length = unhexify( expected_hash, hash_hex );
161
162 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
163
164 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
165 TEST_ASSERT( psa_hash_update( &operation,
166 input, input_size ) == PSA_SUCCESS );
167 TEST_ASSERT( psa_hash_finish( &operation,
168 actual_hash, sizeof( actual_hash ),
169 &actual_hash_length ) == PSA_SUCCESS );
170 TEST_ASSERT( actual_hash_length == expected_hash_length );
171 TEST_ASSERT( memcmp( expected_hash, actual_hash,
172 expected_hash_length ) == 0 );
173
174exit:
175 mbedtls_free( input );
176 mbedtls_psa_crypto_free( );
177}
178/* END_CASE */
179
180/* BEGIN_CASE */
181void hash_verify( int alg_arg, char *input_hex, char *hash_hex )
182{
183 psa_algorithm_t alg = alg_arg;
184 unsigned char *input = NULL;
185 size_t input_size;
186 unsigned char expected_hash[MBEDTLS_MD_MAX_SIZE];
187 size_t expected_hash_length;
188 psa_hash_operation_t operation;
189
Gilles Peskine40f68b92018-03-07 16:43:36 +0100190 input = unhexify_alloc( input_hex, &input_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100191 TEST_ASSERT( input != NULL );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100192 expected_hash_length = unhexify( expected_hash, hash_hex );
193
194 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
195
196 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
197 TEST_ASSERT( psa_hash_update( &operation,
198 input, input_size ) == PSA_SUCCESS );
199 TEST_ASSERT( psa_hash_verify( &operation,
200 expected_hash,
201 expected_hash_length ) == PSA_SUCCESS );
202
203exit:
204 mbedtls_free( input );
205 mbedtls_psa_crypto_free( );
206}
207/* END_CASE */
208
209/* BEGIN_CASE */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100210void mac_verify( int key_type_arg, char *key_hex,
211 int alg_arg, char *iv_hex,
212 char *input_hex, char *mac_hex )
213{
214 int key_slot = 1;
215 psa_key_type_t key_type = key_type_arg;
216 psa_algorithm_t alg = alg_arg;
217 unsigned char *key = NULL;
218 size_t key_size;
219 unsigned char *iv = NULL;
220 size_t iv_size;
221 unsigned char *input = NULL;
222 size_t input_size;
223 unsigned char *expected_mac = NULL;
224 size_t expected_mac_size;
225 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -0700226 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100227
Gilles Peskine40f68b92018-03-07 16:43:36 +0100228 key = unhexify_alloc( key_hex, &key_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100229 TEST_ASSERT( key != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +0100230 if( iv_hex[0] != 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +0100231 {
Gilles Peskine40f68b92018-03-07 16:43:36 +0100232 iv = unhexify_alloc( iv_hex, &iv_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100233 TEST_ASSERT( iv != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100234 }
Gilles Peskine40f68b92018-03-07 16:43:36 +0100235 input = unhexify_alloc( input_hex, &input_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100236 TEST_ASSERT( input != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +0100237 expected_mac = unhexify_alloc( mac_hex, &expected_mac_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100238 TEST_ASSERT( expected_mac != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100239
240 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
241
mohammad16036df908f2018-04-02 08:34:15 -0700242 psa_key_policy_init( &policy );
243
244 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg_arg );
245
246 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
247
Gilles Peskine8c9def32018-02-08 10:02:12 +0100248 TEST_ASSERT( psa_import_key( key_slot, key_type,
249 key, key_size ) == PSA_SUCCESS );
250 // TODO: support IV
251 TEST_ASSERT( psa_mac_start( &operation, key_slot, alg ) == PSA_SUCCESS );
252 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
253 TEST_ASSERT( psa_mac_update( &operation,
254 input, input_size ) == PSA_SUCCESS );
255 TEST_ASSERT( psa_mac_verify( &operation,
256 expected_mac,
257 expected_mac_size ) == PSA_SUCCESS );
258
259exit:
260 mbedtls_free( key );
261 mbedtls_free( iv );
262 mbedtls_free( input );
263 mbedtls_free( expected_mac );
264 psa_destroy_key( key_slot );
265 mbedtls_psa_crypto_free( );
266}
267/* END_CASE */
268
269/* BEGIN_CASE */
Gilles Peskine0189e752018-02-03 23:57:22 +0100270void signature_size( int type_arg, int bits, int alg_arg, int expected_size_arg )
271{
272 psa_key_type_t type = type_arg;
273 psa_algorithm_t alg = alg_arg;
274 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(type, bits, alg);
275 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
276exit:
277 ;
278}
279/* END_CASE */
280
281/* BEGIN_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100282void sign_deterministic( int key_type_arg, char *key_hex,
283 int alg_arg, char *input_hex, char *output_hex )
284{
285 int slot = 1;
286 psa_key_type_t key_type = key_type_arg;
287 psa_algorithm_t alg = alg_arg;
288 unsigned char *key_data = NULL;
289 size_t key_size;
Gilles Peskine0189e752018-02-03 23:57:22 +0100290 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +0100291 unsigned char *input_data = NULL;
292 size_t input_size;
293 unsigned char *output_data = NULL;
294 size_t output_size;
Gilles Peskine0189e752018-02-03 23:57:22 +0100295 unsigned char *signature = NULL;
296 size_t signature_size;
Gilles Peskine93aa0332018-02-03 23:58:03 +0100297 size_t signature_length = 0xdeadbeef;
mohammad1603a97cb8c2018-03-28 03:46:26 -0700298 psa_key_policy_t policy = {0};
Gilles Peskine20035e32018-02-03 22:44:14 +0100299
Gilles Peskine40f68b92018-03-07 16:43:36 +0100300 key_data = unhexify_alloc( key_hex, &key_size );
Gilles Peskine20035e32018-02-03 22:44:14 +0100301 TEST_ASSERT( key_data != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +0100302 input_data = unhexify_alloc( input_hex, &input_size );
Gilles Peskine20035e32018-02-03 22:44:14 +0100303 TEST_ASSERT( input_data != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +0100304 output_data = unhexify_alloc( output_hex, &output_size );
Gilles Peskine20035e32018-02-03 22:44:14 +0100305 TEST_ASSERT( output_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +0100306
307 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
308
mohammad1603a97cb8c2018-03-28 03:46:26 -0700309 psa_key_policy_init( &policy );
310
311 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg );
312
313 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
314
Gilles Peskine20035e32018-02-03 22:44:14 +0100315 TEST_ASSERT( psa_import_key( slot, key_type,
316 key_data, key_size ) == PSA_SUCCESS );
Gilles Peskine0189e752018-02-03 23:57:22 +0100317 TEST_ASSERT( psa_get_key_information( slot,
318 NULL,
319 &key_bits ) == PSA_SUCCESS );
320
321 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, alg, key_bits );
322 TEST_ASSERT( signature_size != 0 );
323 signature = mbedtls_calloc( 1, signature_size );
324 TEST_ASSERT( signature != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +0100325
326 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
327 input_data, input_size,
328 NULL, 0,
Gilles Peskine0189e752018-02-03 23:57:22 +0100329 signature, signature_size,
Gilles Peskine20035e32018-02-03 22:44:14 +0100330 &signature_length ) == PSA_SUCCESS );
331 TEST_ASSERT( signature_length == output_size );
332 TEST_ASSERT( memcmp( signature, output_data, output_size ) == 0 );
333
334exit:
335 psa_destroy_key( slot );
336 mbedtls_free( key_data );
337 mbedtls_free( input_data );
338 mbedtls_free( output_data );
Gilles Peskine0189e752018-02-03 23:57:22 +0100339 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +0100340 mbedtls_psa_crypto_free( );
341}
342/* END_CASE */
343
344/* BEGIN_CASE */
345void sign_fail( int key_type_arg, char *key_hex,
346 int alg_arg, char *input_hex,
347 int signature_size, int expected_status_arg )
348{
349 int slot = 1;
350 psa_key_type_t key_type = key_type_arg;
351 psa_algorithm_t alg = alg_arg;
352 unsigned char *key_data = NULL;
353 size_t key_size;
354 unsigned char *input_data = NULL;
355 size_t input_size;
356 psa_status_t actual_status;
357 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +0100358 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +0100359 size_t signature_length = 0xdeadbeef;
mohammad1603a97cb8c2018-03-28 03:46:26 -0700360 psa_key_policy_t policy = {0};
Gilles Peskine20035e32018-02-03 22:44:14 +0100361
Gilles Peskine40f68b92018-03-07 16:43:36 +0100362 key_data = unhexify_alloc( key_hex, &key_size );
Gilles Peskine20035e32018-02-03 22:44:14 +0100363 TEST_ASSERT( key_data != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +0100364 input_data = unhexify_alloc( input_hex, &input_size );
Gilles Peskine20035e32018-02-03 22:44:14 +0100365 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +0100366 signature = mbedtls_calloc( 1, signature_size );
367 TEST_ASSERT( signature != NULL );
368
369 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
370
mohammad1603a97cb8c2018-03-28 03:46:26 -0700371 psa_key_policy_init( &policy );
372
373 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg );
374
375 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
376
Gilles Peskine20035e32018-02-03 22:44:14 +0100377 TEST_ASSERT( psa_import_key( slot, key_type,
378 key_data, key_size ) == PSA_SUCCESS );
379
380 actual_status = psa_asymmetric_sign( slot, alg,
381 input_data, input_size,
382 NULL, 0,
383 signature, signature_size,
384 &signature_length );
385 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine93aa0332018-02-03 23:58:03 +0100386 TEST_ASSERT( signature_length == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +0100387
388exit:
389 psa_destroy_key( slot );
390 mbedtls_free( key_data );
391 mbedtls_free( input_data );
392 mbedtls_free( signature );
393 mbedtls_psa_crypto_free( );
394}
395/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +0300396
397/* BEGIN_CASE */
398void key_policy( int usage_arg, int alg_arg )
399{
400 int key_slot = 1;
mohammad16034eed7572018-03-28 05:14:59 -0700401 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
mohammad16038cc1cee2018-03-28 01:21:33 +0300402 unsigned char key[32] = {0};
403 psa_key_policy_t policy_set = {0};
404 psa_key_policy_t policy_get = {0};
405
mohammad1603804cd712018-03-20 22:44:08 +0200406
mohammad16038cc1cee2018-03-28 01:21:33 +0300407 memset( key, 0x2a, sizeof( key ) );
408
409 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
410
411 psa_key_policy_init(& policy_set );
412 psa_key_policy_init(& policy_get );
413
414 psa_key_policy_set_usage( &policy_set, usage_arg, alg_arg );
415
416 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == ( psa_key_usage_t )usage_arg );
417
418 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set) == ( psa_algorithm_t )alg_arg );
419
420 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
421
422 TEST_ASSERT( psa_import_key( key_slot, key_type,
423 key, sizeof( key ) ) == PSA_SUCCESS );
424
425 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
426
427 TEST_ASSERT( policy_get.usage == policy_set.usage );
428 TEST_ASSERT( policy_get.alg == policy_set.alg );
429
Gilles Peskinea0655c32018-04-30 17:06:50 +0200430
431
mohammad1603804cd712018-03-20 22:44:08 +0200432
mohammad16038cc1cee2018-03-28 01:21:33 +0300433exit:
434 psa_destroy_key( key_slot );
435 mbedtls_psa_crypto_free( );
436}
437/* END_CASE */
438
mohammad16034eed7572018-03-28 05:14:59 -0700439/* BEGIN_CASE */
440void key_policy_fail( int usage_arg, int alg_arg, int expected_status, char *key_hex )
441{
442 int key_slot = 1;
mohammad16034eed7572018-03-28 05:14:59 -0700443 unsigned char* keypair = NULL;
444 size_t key_size = 0;
445 size_t signature_length = 0;
446 psa_key_policy_t policy = {0};
447 int actual_status = PSA_SUCCESS;
mohammad16034eed7572018-03-28 05:14:59 -0700448
449 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
450
451 psa_key_policy_init( &policy );
452
453 psa_key_policy_set_usage( &policy, usage_arg, alg_arg );
454
455 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
456
mohammad16036df908f2018-04-02 08:34:15 -0700457 if( usage_arg & PSA_KEY_USAGE_EXPORT )
mohammad16034eed7572018-03-28 05:14:59 -0700458 {
mohammad16036df908f2018-04-02 08:34:15 -0700459 keypair = unhexify_alloc( key_hex, &key_size );
460 TEST_ASSERT( keypair != NULL );
461 TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR,
462 keypair, key_size ) == PSA_SUCCESS );
463 actual_status = psa_asymmetric_sign( key_slot,
464 ( psa_algorithm_t )alg_arg, NULL, 0, NULL, 0,
465 NULL, 0, &signature_length );
466 }
467
468 if( usage_arg & PSA_KEY_USAGE_SIGN )
469 {
mohammad1603d926b882018-04-16 01:53:20 -0700470 keypair = unhexify_alloc( key_hex, &key_size );
471 TEST_ASSERT( keypair != NULL );
mohammad16036df908f2018-04-02 08:34:15 -0700472 TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR,
mohammad1603d926b882018-04-16 01:53:20 -0700473 keypair, key_size ) == PSA_SUCCESS );
mohammad16036df908f2018-04-02 08:34:15 -0700474 actual_status = psa_export_key( key_slot, NULL, 0, NULL );
mohammad16034eed7572018-03-28 05:14:59 -0700475 }
476
477 TEST_ASSERT( actual_status == expected_status );
478
479exit:
480 psa_destroy_key( key_slot );
mohammad1603d926b882018-04-16 01:53:20 -0700481 mbedtls_free( keypair );
mohammad16034eed7572018-03-28 05:14:59 -0700482 mbedtls_psa_crypto_free( );
483}
484/* END_CASE */
Gilles Peskinea0655c32018-04-30 17:06:50 +0200485
486/* BEGIN_CASE */
487void key_lifetime( int lifetime_arg )
488{
489 int key_slot = 1;
490 psa_key_type_t key_type = PSA_ALG_CBC_BASE;
491 unsigned char key[32] = {0};
492 psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg;
493 psa_key_lifetime_t lifetime_get;
494 memset( key, 0x2a, sizeof( key ) );
495 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
496 TEST_ASSERT( psa_set_key_lifetime( key_slot,
497 lifetime_set ) == PSA_SUCCESS );
498 TEST_ASSERT( psa_import_key( key_slot, key_type,
499 key, sizeof( key ) ) == PSA_SUCCESS );
500 TEST_ASSERT( psa_get_key_lifetime( key_slot,
501 &lifetime_get ) == PSA_SUCCESS );
502 TEST_ASSERT( lifetime_get == lifetime_set );
503exit:
504 psa_destroy_key( key_slot );
505 mbedtls_psa_crypto_free( );
506}
507/* END_CASE */
mohammad1603804cd712018-03-20 22:44:08 +0200508
509/* BEGIN_CASE */
510void key_lifetime_set_fail( int key_slot_arg, int lifetime_arg, int expected_status_arg )
511{
512 int key_slot = 1;
mohammad1603804cd712018-03-20 22:44:08 +0200513 psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg;
514 psa_status_t actual_status;
515 psa_status_t expected_status = expected_status_arg;
516
mohammad1603804cd712018-03-20 22:44:08 +0200517 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
518
mohammad1603804cd712018-03-20 22:44:08 +0200519 actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set );
520
521 if( actual_status == PSA_SUCCESS )
522 actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set );
523
524 TEST_ASSERT( expected_status == actual_status );
525
526exit:
mohammad1603804cd712018-03-20 22:44:08 +0200527 psa_destroy_key( key_slot );
528 mbedtls_psa_crypto_free( );
529}
530/* END_CASE */
Moran Pekerb4d0ddd2018-04-04 12:47:52 +0300531
532
533/* BEGIN_CASE */
534void import_export_public_key( char *hex,
535 int type_arg,
Moran Pekera964a8f2018-06-04 18:42:36 +0300536 int alg_arg,
Moran Pekerb4d0ddd2018-04-04 12:47:52 +0300537 int expected_bits,
Moran Peker338a0cf2018-05-02 12:55:55 +0300538 int public_key_expected_length,
Moran Pekerb4d0ddd2018-04-04 12:47:52 +0300539 int expected_export_status )
540{
541 int slot = 1;
542 psa_key_type_t type = type_arg;
543 psa_status_t status;
544 unsigned char *data = NULL;
545 unsigned char *exported = NULL;
546 size_t data_size;
547 size_t export_size;
548 size_t exported_length;
549 psa_key_type_t got_type;
550 size_t got_bits;
Moran Pekerb34879b2018-05-29 16:45:14 +0300551 psa_key_policy_t policy = {0};
Moran Pekerb4d0ddd2018-04-04 12:47:52 +0300552
553 data = unhexify_alloc( hex, &data_size );
554 TEST_ASSERT( data != NULL );
555 export_size = (ssize_t) data_size ;
556 exported = mbedtls_calloc( 1, export_size );
557 TEST_ASSERT( exported != NULL );
558
559 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
560
Moran Pekerb34879b2018-05-29 16:45:14 +0300561 psa_key_policy_init( &policy );
562
Gilles Peskine785fd552018-06-04 15:08:56 +0200563 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT,
Moran Pekera964a8f2018-06-04 18:42:36 +0300564 alg_arg );
Moran Pekerb34879b2018-05-29 16:45:14 +0300565
566 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
567
Moran Pekerb4d0ddd2018-04-04 12:47:52 +0300568 /* Import the key */
569 TEST_ASSERT( psa_import_key( slot, type,
570 data, data_size ) == PSA_SUCCESS );
571
572 /* Test the key information */
573 TEST_ASSERT( psa_get_key_information( slot,
574 &got_type, &got_bits ) == PSA_SUCCESS );
575 TEST_ASSERT( got_type == type );
576 TEST_ASSERT( got_bits == (size_t) expected_bits );
577
578 /* Export the key */
579 status = psa_export_public_key( slot,
580 exported, export_size,
581 &exported_length );
582 TEST_ASSERT( status == (psa_status_t) expected_export_status );
583 if( status != PSA_SUCCESS )
584 goto destroy;
585
Gilles Peskinec425e872018-06-04 15:07:13 +0200586 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
Moran Pekerb4d0ddd2018-04-04 12:47:52 +0300587
588destroy:
589 /* Destroy the key */
590 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
591 TEST_ASSERT( psa_get_key_information(
592 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
593
594exit:
595 mbedtls_free( data );
596 mbedtls_psa_crypto_free( );
597}
Moran Peker4ff99f32018-04-16 17:35:09 +0300598/* END_CASE */