blob: 39e1041456e17201ae473af1e6770f8eac396a47 [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
Gilles Peskine61b91d42018-06-08 16:09:36 +020087 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT,
mohammad1603a97cb8c2018-03-28 03:46:26 -070088 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
267/* BEGIN_CASE */
Gilles Peskine0189e752018-02-03 23:57:22 +0100268void signature_size( int type_arg, int bits, int alg_arg, int expected_size_arg )
269{
270 psa_key_type_t type = type_arg;
271 psa_algorithm_t alg = alg_arg;
272 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(type, bits, alg);
273 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
274exit:
275 ;
276}
277/* END_CASE */
278
279/* BEGIN_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100280void sign_deterministic( int key_type_arg, char *key_hex,
281 int alg_arg, char *input_hex, char *output_hex )
282{
283 int slot = 1;
284 psa_key_type_t key_type = key_type_arg;
285 psa_algorithm_t alg = alg_arg;
286 unsigned char *key_data = NULL;
287 size_t key_size;
Gilles Peskine0189e752018-02-03 23:57:22 +0100288 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +0100289 unsigned char *input_data = NULL;
290 size_t input_size;
291 unsigned char *output_data = NULL;
292 size_t output_size;
Gilles Peskine0189e752018-02-03 23:57:22 +0100293 unsigned char *signature = NULL;
294 size_t signature_size;
Gilles Peskine93aa0332018-02-03 23:58:03 +0100295 size_t signature_length = 0xdeadbeef;
mohammad1603a97cb8c2018-03-28 03:46:26 -0700296 psa_key_policy_t policy = {0};
Gilles Peskine20035e32018-02-03 22:44:14 +0100297
Gilles Peskine40f68b92018-03-07 16:43:36 +0100298 key_data = unhexify_alloc( key_hex, &key_size );
Gilles Peskine20035e32018-02-03 22:44:14 +0100299 TEST_ASSERT( key_data != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +0100300 input_data = unhexify_alloc( input_hex, &input_size );
Gilles Peskine20035e32018-02-03 22:44:14 +0100301 TEST_ASSERT( input_data != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +0100302 output_data = unhexify_alloc( output_hex, &output_size );
Gilles Peskine20035e32018-02-03 22:44:14 +0100303 TEST_ASSERT( output_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +0100304
305 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
306
mohammad1603a97cb8c2018-03-28 03:46:26 -0700307 psa_key_policy_init( &policy );
308
309 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg );
310
311 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
312
Gilles Peskine20035e32018-02-03 22:44:14 +0100313 TEST_ASSERT( psa_import_key( slot, key_type,
314 key_data, key_size ) == PSA_SUCCESS );
Gilles Peskine0189e752018-02-03 23:57:22 +0100315 TEST_ASSERT( psa_get_key_information( slot,
316 NULL,
317 &key_bits ) == PSA_SUCCESS );
318
319 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, alg, key_bits );
320 TEST_ASSERT( signature_size != 0 );
321 signature = mbedtls_calloc( 1, signature_size );
322 TEST_ASSERT( signature != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +0100323
324 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
325 input_data, input_size,
326 NULL, 0,
Gilles Peskine0189e752018-02-03 23:57:22 +0100327 signature, signature_size,
Gilles Peskine20035e32018-02-03 22:44:14 +0100328 &signature_length ) == PSA_SUCCESS );
329 TEST_ASSERT( signature_length == output_size );
330 TEST_ASSERT( memcmp( signature, output_data, output_size ) == 0 );
331
332exit:
333 psa_destroy_key( slot );
334 mbedtls_free( key_data );
335 mbedtls_free( input_data );
336 mbedtls_free( output_data );
Gilles Peskine0189e752018-02-03 23:57:22 +0100337 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +0100338 mbedtls_psa_crypto_free( );
339}
340/* END_CASE */
341
342/* BEGIN_CASE */
343void sign_fail( int key_type_arg, char *key_hex,
344 int alg_arg, char *input_hex,
345 int signature_size, int expected_status_arg )
346{
347 int slot = 1;
348 psa_key_type_t key_type = key_type_arg;
349 psa_algorithm_t alg = alg_arg;
350 unsigned char *key_data = NULL;
351 size_t key_size;
352 unsigned char *input_data = NULL;
353 size_t input_size;
354 psa_status_t actual_status;
355 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +0100356 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +0100357 size_t signature_length = 0xdeadbeef;
mohammad1603a97cb8c2018-03-28 03:46:26 -0700358 psa_key_policy_t policy = {0};
Gilles Peskine20035e32018-02-03 22:44:14 +0100359
Gilles Peskine40f68b92018-03-07 16:43:36 +0100360 key_data = unhexify_alloc( key_hex, &key_size );
Gilles Peskine20035e32018-02-03 22:44:14 +0100361 TEST_ASSERT( key_data != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +0100362 input_data = unhexify_alloc( input_hex, &input_size );
Gilles Peskine20035e32018-02-03 22:44:14 +0100363 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +0100364 signature = mbedtls_calloc( 1, signature_size );
365 TEST_ASSERT( signature != NULL );
366
367 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
368
mohammad1603a97cb8c2018-03-28 03:46:26 -0700369 psa_key_policy_init( &policy );
370
371 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg );
372
373 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
374
Gilles Peskine20035e32018-02-03 22:44:14 +0100375 TEST_ASSERT( psa_import_key( slot, key_type,
376 key_data, key_size ) == PSA_SUCCESS );
377
378 actual_status = psa_asymmetric_sign( slot, alg,
379 input_data, input_size,
380 NULL, 0,
381 signature, signature_size,
382 &signature_length );
383 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine93aa0332018-02-03 23:58:03 +0100384 TEST_ASSERT( signature_length == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +0100385
386exit:
387 psa_destroy_key( slot );
388 mbedtls_free( key_data );
389 mbedtls_free( input_data );
390 mbedtls_free( signature );
391 mbedtls_psa_crypto_free( );
392}
393/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +0300394
395/* BEGIN_CASE */
396void key_policy( int usage_arg, int alg_arg )
397{
398 int key_slot = 1;
mohammad16034eed7572018-03-28 05:14:59 -0700399 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
mohammad16038cc1cee2018-03-28 01:21:33 +0300400 unsigned char key[32] = {0};
401 psa_key_policy_t policy_set = {0};
402 psa_key_policy_t policy_get = {0};
mohammad1603804cd712018-03-20 22:44:08 +0200403
mohammad16038cc1cee2018-03-28 01:21:33 +0300404 memset( key, 0x2a, sizeof( key ) );
Gilles Peskine5b051bc2018-05-31 13:25:48 +0200405
mohammad16038cc1cee2018-03-28 01:21:33 +0300406 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
407
408 psa_key_policy_init(& policy_set );
409 psa_key_policy_init(& policy_get );
410
411 psa_key_policy_set_usage( &policy_set, usage_arg, alg_arg );
412
413 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == ( psa_key_usage_t )usage_arg );
414
415 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set) == ( psa_algorithm_t )alg_arg );
416
417 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
418
419 TEST_ASSERT( psa_import_key( key_slot, key_type,
420 key, sizeof( key ) ) == PSA_SUCCESS );
421
422 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
423
424 TEST_ASSERT( policy_get.usage == policy_set.usage );
425 TEST_ASSERT( policy_get.alg == policy_set.alg );
426
427exit:
428 psa_destroy_key( key_slot );
429 mbedtls_psa_crypto_free( );
430}
431/* END_CASE */
432
mohammad16034eed7572018-03-28 05:14:59 -0700433/* BEGIN_CASE */
434void key_policy_fail( int usage_arg, int alg_arg, int expected_status, char *key_hex )
435{
436 int key_slot = 1;
mohammad16034eed7572018-03-28 05:14:59 -0700437 unsigned char* keypair = NULL;
438 size_t key_size = 0;
439 size_t signature_length = 0;
440 psa_key_policy_t policy = {0};
441 int actual_status = PSA_SUCCESS;
Gilles Peskine5b051bc2018-05-31 13:25:48 +0200442
mohammad16034eed7572018-03-28 05:14:59 -0700443 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
444
445 psa_key_policy_init( &policy );
446
447 psa_key_policy_set_usage( &policy, usage_arg, alg_arg );
448
449 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
450
mohammad16036df908f2018-04-02 08:34:15 -0700451 if( usage_arg & PSA_KEY_USAGE_EXPORT )
mohammad16034eed7572018-03-28 05:14:59 -0700452 {
mohammad16036df908f2018-04-02 08:34:15 -0700453 keypair = unhexify_alloc( key_hex, &key_size );
454 TEST_ASSERT( keypair != NULL );
455 TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR,
456 keypair, key_size ) == PSA_SUCCESS );
Gilles Peskine5b051bc2018-05-31 13:25:48 +0200457 actual_status = psa_asymmetric_sign( key_slot,
458 ( psa_algorithm_t )alg_arg, NULL, 0, NULL, 0,
mohammad16036df908f2018-04-02 08:34:15 -0700459 NULL, 0, &signature_length );
460 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +0200461
mohammad16036df908f2018-04-02 08:34:15 -0700462 if( usage_arg & PSA_KEY_USAGE_SIGN )
463 {
mohammad1603d926b882018-04-16 01:53:20 -0700464 keypair = unhexify_alloc( key_hex, &key_size );
465 TEST_ASSERT( keypair != NULL );
mohammad16036df908f2018-04-02 08:34:15 -0700466 TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR,
mohammad1603d926b882018-04-16 01:53:20 -0700467 keypair, key_size ) == PSA_SUCCESS );
mohammad16036df908f2018-04-02 08:34:15 -0700468 actual_status = psa_export_key( key_slot, NULL, 0, NULL );
mohammad16034eed7572018-03-28 05:14:59 -0700469 }
470
471 TEST_ASSERT( actual_status == expected_status );
472
473exit:
474 psa_destroy_key( key_slot );
mohammad1603d926b882018-04-16 01:53:20 -0700475 mbedtls_free( keypair );
mohammad16034eed7572018-03-28 05:14:59 -0700476 mbedtls_psa_crypto_free( );
477}
478/* END_CASE */
Gilles Peskinea0655c32018-04-30 17:06:50 +0200479
480/* BEGIN_CASE */
481void key_lifetime( int lifetime_arg )
482{
483 int key_slot = 1;
484 psa_key_type_t key_type = PSA_ALG_CBC_BASE;
485 unsigned char key[32] = {0};
486 psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg;
487 psa_key_lifetime_t lifetime_get;
Gilles Peskine5b051bc2018-05-31 13:25:48 +0200488
Gilles Peskinea0655c32018-04-30 17:06:50 +0200489 memset( key, 0x2a, sizeof( key ) );
Gilles Peskine5b051bc2018-05-31 13:25:48 +0200490
Gilles Peskinea0655c32018-04-30 17:06:50 +0200491 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
Gilles Peskine5b051bc2018-05-31 13:25:48 +0200492
493 TEST_ASSERT( psa_set_key_lifetime( key_slot,
Gilles Peskinea0655c32018-04-30 17:06:50 +0200494 lifetime_set ) == PSA_SUCCESS );
Gilles Peskine5b051bc2018-05-31 13:25:48 +0200495
Gilles Peskinea0655c32018-04-30 17:06:50 +0200496 TEST_ASSERT( psa_import_key( key_slot, key_type,
497 key, sizeof( key ) ) == PSA_SUCCESS );
Gilles Peskine5b051bc2018-05-31 13:25:48 +0200498
499 TEST_ASSERT( psa_get_key_lifetime( key_slot,
Gilles Peskinea0655c32018-04-30 17:06:50 +0200500 &lifetime_get ) == PSA_SUCCESS );
Gilles Peskine5b051bc2018-05-31 13:25:48 +0200501
502 TEST_ASSERT( lifetime_get == lifetime_set );
503
Gilles Peskinea0655c32018-04-30 17:06:50 +0200504exit:
505 psa_destroy_key( key_slot );
506 mbedtls_psa_crypto_free( );
507}
508/* END_CASE */
mohammad1603804cd712018-03-20 22:44:08 +0200509
Gilles Peskine5b051bc2018-05-31 13:25:48 +0200510
mohammad1603804cd712018-03-20 22:44:08 +0200511/* BEGIN_CASE */
512void key_lifetime_set_fail( int key_slot_arg, int lifetime_arg, int expected_status_arg )
513{
514 int key_slot = 1;
mohammad1603804cd712018-03-20 22:44:08 +0200515 psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg;
516 psa_status_t actual_status;
517 psa_status_t expected_status = expected_status_arg;
518
mohammad1603804cd712018-03-20 22:44:08 +0200519 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
520
mohammad1603804cd712018-03-20 22:44:08 +0200521 actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set );
522
523 if( actual_status == PSA_SUCCESS )
524 actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set );
Gilles Peskine5b051bc2018-05-31 13:25:48 +0200525
mohammad1603804cd712018-03-20 22:44:08 +0200526 TEST_ASSERT( expected_status == actual_status );
527
528exit:
mohammad1603804cd712018-03-20 22:44:08 +0200529 psa_destroy_key( key_slot );
530 mbedtls_psa_crypto_free( );
531}
532/* END_CASE */
itayzafrir5c753392018-05-08 11:18:38 +0300533
534/* BEGIN_CASE */
535void asymmetric_verify( int key_type_arg, char *key_hex,
536 int alg_arg, char *hash_hex, char *signature_hex )
537{
538 int slot = 1;
539 psa_key_type_t key_type = key_type_arg;
540 psa_algorithm_t alg = alg_arg;
541 unsigned char *key_data = NULL;
542 size_t key_size;
543 unsigned char *hash_data = NULL;
544 size_t hash_size;
545 unsigned char *signature_data = NULL;
546 size_t signature_size;
547 psa_key_policy_t policy = {0};
548
549 key_data = unhexify_alloc( key_hex, &key_size );
550 TEST_ASSERT( key_data != NULL );
551 hash_data = unhexify_alloc( hash_hex, &hash_size );
552 TEST_ASSERT( hash_data != NULL );
553 signature_data = unhexify_alloc( signature_hex, &signature_size );
554 TEST_ASSERT( signature_data != NULL );
555
556 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
557
558 psa_key_policy_init( &policy );
559
560 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg_arg );
561
562 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
563
564 TEST_ASSERT( psa_import_key( slot, key_type,
565 key_data, key_size ) == PSA_SUCCESS );
566
567 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
568 hash_data, hash_size,
569 NULL, 0,
570 signature_data, signature_size ) ==
571 PSA_SUCCESS );
572exit:
573 psa_destroy_key( slot );
574 mbedtls_free( key_data );
575 mbedtls_free( hash_data );
576 mbedtls_free( signature_data );
577 mbedtls_psa_crypto_free( );
578}
579/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300580
581/* BEGIN_CASE */
582void asymmetric_verify_fail( int key_type_arg, char *key_hex,
583 int alg_arg, char *hash_hex, char *signature_hex,
584 int expected_status_arg )
585{
586
587 int slot = 1;
588 psa_key_type_t key_type = key_type_arg;
589 psa_algorithm_t alg = alg_arg;
590 unsigned char *key_data = NULL;
591 size_t key_size;
592 unsigned char *hash_data = NULL;
593 size_t hash_size;
594 unsigned char *signature_data = NULL;
595 size_t signature_size;
596 psa_status_t actual_status;
597 psa_status_t expected_status = expected_status_arg;
Nir Sonnenscheind7082602018-06-04 16:45:27 +0300598 psa_key_policy_t policy = {0};
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300599
600 key_data = unhexify_alloc( key_hex, &key_size );
601 TEST_ASSERT( key_data != NULL );
602 hash_data = unhexify_alloc( hash_hex, &hash_size );
603 TEST_ASSERT( hash_data != NULL );
604 signature_data = unhexify_alloc( signature_hex, &signature_size );
605 TEST_ASSERT( signature_data != NULL );
606
607 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
608
Nir Sonnenscheind7082602018-06-04 16:45:27 +0300609 psa_key_policy_init( &policy );
610
611 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg_arg );
612
613 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
614
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300615 TEST_ASSERT( psa_import_key( slot, key_type,
616 key_data, key_size ) == PSA_SUCCESS );
617
618 actual_status = psa_asymmetric_verify( slot, alg,
619 hash_data, hash_size,
620 NULL, 0,
621 signature_data, signature_size );
622
623
624 TEST_ASSERT( actual_status == expected_status );
625
626exit:
627 psa_destroy_key( slot );
628 mbedtls_free( key_data );
629 mbedtls_free( hash_data );
630 mbedtls_free( signature_data );
631 mbedtls_psa_crypto_free( );
632}
633/* END_CASE */
634
635
636/* BEGIN_CASE */
Gilles Peskineeebd7382018-06-08 18:11:54 +0200637void asymmetric_encrypt_decrypt( int key_type_arg, char *key_hex,
638 int alg_arg, char *input_hex )
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300639{
640 int slot = 1;
641 psa_key_type_t key_type = key_type_arg;
642 psa_algorithm_t alg = alg_arg;
643 unsigned char *key_data = NULL;
644 size_t key_size;
645 unsigned char *input_data = NULL;
646 size_t input_size;
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300647 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +0300648 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300649 size_t output_length = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +0300650 unsigned char *output2 = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +0300651 size_t output2_size = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +0300652 size_t output2_length = 0;
Nir Sonnenscheind7082602018-06-04 16:45:27 +0300653 psa_key_policy_t policy = {0};
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300654
655 key_data = unhexify_alloc( key_hex, &key_size );
656 TEST_ASSERT( key_data != NULL );
Nir Sonnenscheind70bc482018-06-04 16:31:13 +0300657 output_size = key_size;
658 output2_size = key_size;
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300659 input_data = unhexify_alloc( input_hex, &input_size );
660 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300661 output = mbedtls_calloc( 1, output_size );
662 TEST_ASSERT( output != NULL );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +0300663 output2 = mbedtls_calloc( 1, output2_size );
664 TEST_ASSERT( output2 != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300665
666 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
667
Nir Sonnenscheind7082602018-06-04 16:45:27 +0300668 psa_key_policy_init( &policy );
669 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg_arg );
670 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
671
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300672 TEST_ASSERT( psa_import_key( slot, key_type,
673 key_data, key_size ) == PSA_SUCCESS );
674
Gilles Peskineeebd7382018-06-08 18:11:54 +0200675 /* We test encryption by checking that encrypt-then-decrypt gives back
676 * the original plaintext because of the non-optional random
677 * part of encryption process which prevents using fixed vectors. */
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +0300678 TEST_ASSERT( psa_asymmetric_encrypt(slot, alg,
679 input_data,
680 input_size,
681 NULL, 0,
682 output,
683 output_size,
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300684 &output_length) == PSA_SUCCESS );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +0300685
686 TEST_ASSERT( psa_asymmetric_decrypt(slot, alg,
687 output,
688 output_length,
689 NULL, 0,
690 output2,
691 output2_size,
Gilles Peskine5b051bc2018-05-31 13:25:48 +0200692 &output2_length) == PSA_SUCCESS );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +0300693 TEST_ASSERT( memcmp( input_data, output2, input_size ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300694
695exit:
696 psa_destroy_key( slot );
697 mbedtls_free( key_data );
698 mbedtls_free( input_data );
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300699 mbedtls_free( output);
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +0300700 mbedtls_free( output2);
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300701 mbedtls_psa_crypto_free( );
702
703}
704/* END_CASE */
705
706
707/* BEGIN_CASE */
708void asymmetric_encrypt_fail( int key_type_arg, char *key_hex,
709 int alg_arg, char *input_hex,
710 int expected_status_arg )
711{
712
713
714 int slot = 1;
715 psa_key_type_t key_type = key_type_arg;
716 psa_algorithm_t alg = alg_arg;
717 unsigned char *key_data = NULL;
718 size_t key_size;
719 unsigned char *input_data = NULL;
720 size_t input_size;
721 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +0300722 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300723 size_t output_length = 0;
724 psa_status_t actual_status;
725 psa_status_t expected_status = expected_status_arg;
Nir Sonnenscheind7082602018-06-04 16:45:27 +0300726 psa_key_policy_t policy = {0};
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300727
728 key_data = unhexify_alloc( key_hex, &key_size );
729 TEST_ASSERT( key_data != NULL );
Nir Sonnenscheind70bc482018-06-04 16:31:13 +0300730 output_size = key_size;
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300731 input_data = unhexify_alloc( input_hex, &input_size );
732 TEST_ASSERT( input_data != NULL );
733 output = mbedtls_calloc( 1, output_size );
734 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +0200735
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300736 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
737
Nir Sonnenscheind7082602018-06-04 16:45:27 +0300738 psa_key_policy_init( &policy );
739 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg_arg );
740 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
741
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300742 TEST_ASSERT( psa_import_key( slot, key_type,
743 key_data, key_size ) == PSA_SUCCESS );
744
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +0300745 actual_status = psa_asymmetric_encrypt(slot, alg,
746 input_data,
747 input_size,
748 NULL, 0,
749 output,
750 output_size,
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300751 &output_length);
752 TEST_ASSERT( actual_status == expected_status );
753
754exit:
755 psa_destroy_key( slot );
756 mbedtls_free( key_data );
757 mbedtls_free( input_data );
758 mbedtls_free( output);
759 mbedtls_psa_crypto_free( );
760
761}
762/* END_CASE */
763
764/* BEGIN_CASE */
765void asymmetric_decrypt( int key_type_arg, char *key_hex,
766 int alg_arg, char *input_hex,
767 char *expected_hex, int expected_size )
768{
769 int slot = 1;
770 psa_key_type_t key_type = key_type_arg;
771 psa_algorithm_t alg = alg_arg;
772 unsigned char *key_data = NULL;
773 size_t key_size;
774 unsigned char *input_data = NULL;
775 size_t input_size;
776 unsigned char *expected_data = NULL;
777 size_t expected_data_size;
778 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +0300779 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300780 size_t output_length = 0;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +0300781 psa_key_policy_t policy = {0};
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300782
783 key_data = unhexify_alloc( key_hex, &key_size );
784 TEST_ASSERT( key_data != NULL );
Nir Sonnenscheind70bc482018-06-04 16:31:13 +0300785 output_size = key_size;
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300786 input_data = unhexify_alloc( input_hex, &input_size );
787 TEST_ASSERT( input_data != NULL );
788 expected_data = unhexify_alloc( expected_hex, &expected_data_size );
789 TEST_ASSERT( expected_data != NULL );
790 output = mbedtls_calloc( 1, output_size );
791 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +0200792
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300793 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
794
Nir Sonnenscheind7082602018-06-04 16:45:27 +0300795 psa_key_policy_init( &policy );
796 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg_arg );
797 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
798
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300799 TEST_ASSERT( psa_import_key( slot, key_type,
800 key_data, key_size ) == PSA_SUCCESS );
801
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +0300802 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
803 input_data,
804 input_size,
805 NULL, 0,
806 output,
807 output_size,
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300808 &output_length) == PSA_SUCCESS );
809 TEST_ASSERT( ((size_t)expected_size) == output_length );
Nir Sonnenscheind70bc482018-06-04 16:31:13 +0300810 TEST_ASSERT( memcmp( expected_data, output, (output_length) ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300811
812exit:
813 psa_destroy_key( slot );
814 mbedtls_free( key_data );
815 mbedtls_free( input_data );
816 mbedtls_free( expected_data );
817 mbedtls_free( output);
818 mbedtls_psa_crypto_free( );
819
820
821}
822/* END_CASE */
823
824
825/* BEGIN_CASE */
826void asymmetric_decrypt_fail( int key_type_arg, char *key_hex,
827 int alg_arg, char *input_hex,
828 int expected_status_arg )
829{
830
831 int slot = 1;
832 psa_key_type_t key_type = key_type_arg;
833 psa_algorithm_t alg = alg_arg;
834 unsigned char *key_data = NULL;
835 size_t key_size;
836 unsigned char *input_data = NULL;
837 size_t input_size;
838 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +0300839 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300840 size_t output_length = 0;
841 psa_status_t actual_status;
842 psa_status_t expected_status = expected_status_arg;
Nir Sonnenscheind7082602018-06-04 16:45:27 +0300843 psa_key_policy_t policy = {0};
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300844
845 key_data = unhexify_alloc( key_hex, &key_size );
846 TEST_ASSERT( key_data != NULL );
Nir Sonnenscheind70bc482018-06-04 16:31:13 +0300847 output_size = key_size;
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300848 input_data = unhexify_alloc( input_hex, &input_size );
849 TEST_ASSERT( input_data != NULL );
850 output = mbedtls_calloc( 1, output_size );
851 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +0200852
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300853 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
854
Nir Sonnenscheind7082602018-06-04 16:45:27 +0300855 psa_key_policy_init( &policy );
856 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg_arg );
857 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
858
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300859 TEST_ASSERT( psa_import_key( slot, key_type,
860 key_data, key_size ) == PSA_SUCCESS );
861
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +0300862 actual_status = psa_asymmetric_decrypt(slot, alg,
863 input_data,
864 input_size,
865 NULL, 0,
866 output,
867 output_size,
Nir Sonnenschein39e59142018-05-02 23:16:26 +0300868 &output_length);
869 TEST_ASSERT( actual_status == expected_status );
870
871exit:
872 psa_destroy_key( slot );
873 mbedtls_free( key_data );
874 mbedtls_free( input_data );
875 mbedtls_free( output);
876 mbedtls_psa_crypto_free( );
877}
Gilles Peskine5b051bc2018-05-31 13:25:48 +0200878/* END_CASE */