blob: ee781326e2a6cc68b7b25ec87d18c1e99b36099a [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
Gilles Peskinee59236f2018-01-27 23:32:46 +01003#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +03004
5#if(UINT32_MAX > SIZE_MAX)
Gilles Peskine2d277862018-06-18 15:41:12 +02006#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX )
itayzafrir3e02b3b2018-06-12 17:06:52 +03007#else
Gilles Peskine2d277862018-06-18 15:41:12 +02008#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1
itayzafrir3e02b3b2018-06-12 17:06:52 +03009#endif
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020010
11/** Test if a buffer is not all-bits zero.
12 *
13 * \param buffer Pointer to the beginning of the buffer.
14 * \param size Size of the buffer in bytes.
15 *
16 * \return 0 if the buffer is all-bits-zero.
17 * \return A nonzero value otherwise.
18 */
19int mem_is_nonzero( void *buffer, size_t size )
20{
21 size_t i;
22 for( i = 0; i < size; i++ )
23 {
24 if( ( (unsigned char *) buffer )[i] != 0 )
25 return( i + 1 );
26 }
27 return( 0 );
28}
Gilles Peskinee59236f2018-01-27 23:32:46 +010029/* END_HEADER */
30
31/* BEGIN_DEPENDENCIES
32 * depends_on:MBEDTLS_PSA_CRYPTO_C
33 * END_DEPENDENCIES
34 */
35
36/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +020037void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +010038{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010039 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +010040 int i;
41 for( i = 0; i <= 1; i++ )
42 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010043 status = psa_crypto_init( );
44 TEST_ASSERT( status == PSA_SUCCESS );
45 status = psa_crypto_init( );
46 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +010047 mbedtls_psa_crypto_free( );
48 }
49}
50/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010051
52/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +030053void import( data_t *data, int type, int expected_status )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010054{
55 int slot = 1;
56 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010057
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010058 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +030059 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010060 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
61
Gilles Peskine4abf7412018-06-18 16:35:34 +020062 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010063 TEST_ASSERT( status == (psa_status_t) expected_status );
64 if( status == PSA_SUCCESS )
65 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
66
67exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010068 mbedtls_psa_crypto_free( );
69}
70/* END_CASE */
71
72/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +030073void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +030074 int type_arg,
75 int alg_arg,
76 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010077 int expected_bits,
78 int export_size_delta,
79 int expected_export_status,
80 int canonical_input )
81{
82 int slot = 1;
83 int slot2 = slot + 1;
84 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +020085 psa_algorithm_t alg = alg_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010086 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010087 unsigned char *exported = NULL;
88 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010089 size_t export_size;
90 size_t exported_length;
91 size_t reexported_length;
92 psa_key_type_t got_type;
93 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +020094 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010095
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010096 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +030097 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
98 export_size = (ssize_t) data->len + export_size_delta;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010099 exported = mbedtls_calloc( 1, export_size );
100 TEST_ASSERT( exported != NULL );
101 if( ! canonical_input )
102 {
103 reexported = mbedtls_calloc( 1, export_size );
104 TEST_ASSERT( reexported != NULL );
105 }
106 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
107
mohammad1603a97cb8c2018-03-28 03:46:26 -0700108 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200109 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700110 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
111
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100112 /* Import the key */
113 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200114 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100115
116 /* Test the key information */
117 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200118 &got_type,
119 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100120 TEST_ASSERT( got_type == type );
121 TEST_ASSERT( got_bits == (size_t) expected_bits );
122
123 /* Export the key */
124 status = psa_export_key( slot,
125 exported, export_size,
126 &exported_length );
127 TEST_ASSERT( status == (psa_status_t) expected_export_status );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200128 TEST_ASSERT( ! mem_is_nonzero( exported + exported_length,
129 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100130 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200131 {
132 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100133 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200134 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100135
136 if( canonical_input )
137 {
Gilles Peskine4abf7412018-06-18 16:35:34 +0200138 TEST_ASSERT( exported_length == data->len );
139 TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100140 }
141 else
142 {
mohammad1603a97cb8c2018-03-28 03:46:26 -0700143 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
144
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100145 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200146 exported,
147 export_size ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100148 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200149 reexported,
150 export_size,
151 &reexported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100152 TEST_ASSERT( reexported_length == exported_length );
153 TEST_ASSERT( memcmp( reexported, exported,
154 exported_length ) == 0 );
155 }
156
157destroy:
158 /* Destroy the key */
159 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
160 TEST_ASSERT( psa_get_key_information(
161 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
162
163exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300164 mbedtls_free( exported );
165 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100166 mbedtls_psa_crypto_free( );
167}
168/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100169
Moran Pekerf709f4a2018-06-06 17:26:04 +0300170/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300171void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200172 int type_arg,
173 int alg_arg,
174 int expected_bits,
175 int public_key_expected_length,
176 int expected_export_status )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300177{
178 int slot = 1;
179 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200180 psa_algorithm_t alg = alg_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300181 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300182 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300183 size_t export_size;
184 size_t exported_length;
185 psa_key_type_t got_type;
186 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200187 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300188
Moran Pekerf709f4a2018-06-06 17:26:04 +0300189 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300190 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
191 export_size = (ssize_t) data->len;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300192 exported = mbedtls_calloc( 1, export_size );
193 TEST_ASSERT( exported != NULL );
194
195 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
196
197 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200198 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300199 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
200
201 /* Import the key */
202 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200203 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300204
205 /* Test the key information */
206 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200207 &got_type,
208 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300209 TEST_ASSERT( got_type == type );
210 TEST_ASSERT( got_bits == (size_t) expected_bits );
211
212 /* Export the key */
213 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200214 exported, export_size,
215 &exported_length );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300216 TEST_ASSERT( status == (psa_status_t) expected_export_status );
217 if( status != PSA_SUCCESS )
218 goto destroy;
219
220 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
221
222destroy:
223 /* Destroy the key */
224 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
225 TEST_ASSERT( psa_get_key_information(
226 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
227
228exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300229 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300230 mbedtls_psa_crypto_free( );
231}
232/* END_CASE */
233
Gilles Peskine20035e32018-02-03 22:44:14 +0100234/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +0200235void key_policy( int usage_arg, int alg_arg )
236{
237 int key_slot = 1;
238 psa_algorithm_t alg = alg_arg;
239 psa_key_usage_t usage = usage_arg;
240 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
241 unsigned char key[32] = {0};
242 psa_key_policy_t policy_set;
243 psa_key_policy_t policy_get;
244
245 memset( key, 0x2a, sizeof( key ) );
246
247 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
248
249 psa_key_policy_init( &policy_set );
250 psa_key_policy_init( &policy_get );
251
252 psa_key_policy_set_usage( &policy_set, usage, alg );
253
254 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
255 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
256 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
257
258 TEST_ASSERT( psa_import_key( key_slot, key_type,
259 key, sizeof( key ) ) == PSA_SUCCESS );
260
261 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
262
263 TEST_ASSERT( policy_get.usage == policy_set.usage );
264 TEST_ASSERT( policy_get.alg == policy_set.alg );
265
266exit:
267 psa_destroy_key( key_slot );
268 mbedtls_psa_crypto_free( );
269}
270/* END_CASE */
271
272/* BEGIN_CASE */
273void key_policy_fail( int usage_arg, int alg_arg, int expected_status,
274 data_t *keypair )
275{
276 int key_slot = 1;
277 psa_algorithm_t alg = alg_arg;
278 psa_key_usage_t usage = usage_arg;
279 size_t signature_length = 0;
280 psa_key_policy_t policy;
281 int actual_status = PSA_SUCCESS;
282
283 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
284
285 psa_key_policy_init( &policy );
286 psa_key_policy_set_usage( &policy, usage, alg );
287 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
288
289 if( usage & PSA_KEY_USAGE_EXPORT )
290 {
291 TEST_ASSERT( keypair != NULL );
292 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) );
293 TEST_ASSERT( psa_import_key( key_slot,
294 PSA_KEY_TYPE_RSA_KEYPAIR,
295 keypair->x,
296 keypair->len ) == PSA_SUCCESS );
297 actual_status = psa_asymmetric_sign( key_slot, alg,
298 NULL, 0,
299 NULL, 0,
300 NULL, 0, &signature_length );
301 }
302
303 if( usage & PSA_KEY_USAGE_SIGN )
304 {
305 TEST_ASSERT( keypair != NULL );
306 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) );
307 TEST_ASSERT( psa_import_key( key_slot,
308 PSA_KEY_TYPE_RSA_KEYPAIR,
309 keypair->x,
310 keypair->len ) == PSA_SUCCESS );
311 actual_status = psa_export_key( key_slot, NULL, 0, NULL );
312 }
313
314 TEST_ASSERT( actual_status == expected_status );
315
316exit:
317 psa_destroy_key( key_slot );
318 mbedtls_psa_crypto_free( );
319}
320/* END_CASE */
321
322/* BEGIN_CASE */
323void key_lifetime( int lifetime_arg )
324{
325 int key_slot = 1;
326 psa_key_type_t key_type = PSA_ALG_CBC_BASE;
327 unsigned char key[32] = {0};
328 psa_key_lifetime_t lifetime_set = lifetime_arg;
329 psa_key_lifetime_t lifetime_get;
330
331 memset( key, 0x2a, sizeof( key ) );
332
333 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
334
335 TEST_ASSERT( psa_set_key_lifetime( key_slot,
336 lifetime_set ) == PSA_SUCCESS );
337
338 TEST_ASSERT( psa_import_key( key_slot, key_type,
339 key, sizeof( key ) ) == PSA_SUCCESS );
340
341 TEST_ASSERT( psa_get_key_lifetime( key_slot,
342 &lifetime_get ) == PSA_SUCCESS );
343
344 TEST_ASSERT( lifetime_get == lifetime_set );
345
346exit:
347 psa_destroy_key( key_slot );
348 mbedtls_psa_crypto_free( );
349}
350/* END_CASE */
351
352/* BEGIN_CASE */
353void key_lifetime_set_fail( int key_slot_arg,
354 int lifetime_arg,
355 int expected_status_arg )
356{
357 psa_key_slot_t key_slot = key_slot_arg;
358 psa_key_lifetime_t lifetime_set = lifetime_arg;
359 psa_status_t actual_status;
360 psa_status_t expected_status = expected_status_arg;
361
362 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
363
364 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
365
366 if( actual_status == PSA_SUCCESS )
367 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
368
369 TEST_ASSERT( expected_status == actual_status );
370
371exit:
372 psa_destroy_key( key_slot );
373 mbedtls_psa_crypto_free( );
374}
375/* END_CASE */
376
377/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200378void hash_setup( int alg_arg,
379 int expected_status_arg )
380{
381 psa_algorithm_t alg = alg_arg;
382 psa_hash_operation_t operation;
383 psa_status_t status;
384
385 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
386
387 status = psa_hash_start( &operation, alg );
388 psa_hash_abort( &operation );
389 TEST_ASSERT( status == (psa_status_t) expected_status_arg );
390
391exit:
392 mbedtls_psa_crypto_free( );
393}
394/* END_CASE */
395
396/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300397void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100398{
399 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +0200400 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100401 size_t actual_hash_length;
402 psa_hash_operation_t operation;
403
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100404 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300405 TEST_ASSERT( expected_hash != NULL );
406 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
407 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100408
409 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
410
411 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
412 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200413 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100414 TEST_ASSERT( psa_hash_finish( &operation,
415 actual_hash, sizeof( actual_hash ),
416 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200417 TEST_ASSERT( actual_hash_length == expected_hash->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300418 TEST_ASSERT( memcmp( expected_hash->x, actual_hash,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200419 expected_hash->len ) == 0 );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100420
421exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100422 mbedtls_psa_crypto_free( );
423}
424/* END_CASE */
425
426/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300427void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100428{
429 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100430 psa_hash_operation_t operation;
431
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100432 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300433 TEST_ASSERT( expected_hash != NULL );
434 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
435 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100436
437 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
438
439 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
440 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200441 input->x,
442 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100443 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300444 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200445 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100446
447exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100448 mbedtls_psa_crypto_free( );
449}
450/* END_CASE */
451
452/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200453void mac_setup( int key_type_arg,
454 data_t *key,
455 int alg_arg,
456 int expected_status_arg )
457{
458 int key_slot = 1;
459 psa_key_type_t key_type = key_type_arg;
460 psa_algorithm_t alg = alg_arg;
461 psa_mac_operation_t operation;
462 psa_key_policy_t policy;
463 psa_status_t status;
464
465 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
466
467 psa_key_policy_init( &policy );
468 psa_key_policy_set_usage( &policy,
469 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
470 alg );
471 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
472
473 TEST_ASSERT( psa_import_key( key_slot, key_type,
474 key->x, key->len ) == PSA_SUCCESS );
475
476 status = psa_mac_start( &operation, key_slot, alg );
477 psa_mac_abort( &operation );
478 TEST_ASSERT( status == (psa_status_t) expected_status_arg );
479
480exit:
481 psa_destroy_key( key_slot );
482 mbedtls_psa_crypto_free( );
483}
484/* END_CASE */
485
486/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +0200487void mac_verify( int key_type_arg,
488 data_t *key,
489 int alg_arg,
490 data_t *input,
491 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +0100492{
493 int key_slot = 1;
494 psa_key_type_t key_type = key_type_arg;
495 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100496 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -0700497 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100498
Gilles Peskine8c9def32018-02-08 10:02:12 +0100499 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100500 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100501 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300502 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300503 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
504 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100505
506 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
507
mohammad16036df908f2018-04-02 08:34:15 -0700508 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200509 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -0700510 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
511
Gilles Peskine8c9def32018-02-08 10:02:12 +0100512 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200513 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +0200514
Gilles Peskine8c9def32018-02-08 10:02:12 +0100515 TEST_ASSERT( psa_mac_start( &operation, key_slot, alg ) == PSA_SUCCESS );
516 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
517 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200518 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100519 TEST_ASSERT( psa_mac_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300520 expected_mac->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200521 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100522
523exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +0100524 psa_destroy_key( key_slot );
525 mbedtls_psa_crypto_free( );
526}
527/* END_CASE */
528
529/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200530void cipher_setup( int key_type_arg,
531 data_t *key,
532 int alg_arg,
533 int expected_status_arg )
534{
535 int key_slot = 1;
536 psa_key_type_t key_type = key_type_arg;
537 psa_algorithm_t alg = alg_arg;
538 psa_cipher_operation_t operation;
539 psa_key_policy_t policy;
540 psa_status_t status;
541
542 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
543
544 psa_key_policy_init( &policy );
545 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
546 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
547
548 TEST_ASSERT( psa_import_key( key_slot, key_type,
549 key->x, key->len ) == PSA_SUCCESS );
550
551 status = psa_encrypt_setup( &operation, key_slot, alg );
552 psa_cipher_abort( &operation );
553 TEST_ASSERT( status == (psa_status_t) expected_status_arg );
554
555exit:
556 psa_destroy_key( key_slot );
557 mbedtls_psa_crypto_free( );
558}
559/* END_CASE */
560
561/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +0200562void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300563 data_t *key,
564 data_t *input, data_t *expected_output,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200565 int expected_status )
566{
567 int key_slot = 1;
568 psa_status_t status;
569 psa_key_type_t key_type = key_type_arg;
570 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200571 unsigned char iv[16] = {0};
itayzafrir3e02b3b2018-06-12 17:06:52 +0300572 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200573 size_t output_buffer_size = 0;
574 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200575 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200576 psa_cipher_operation_t operation;
577
Gilles Peskine50e586b2018-06-08 14:28:46 +0200578 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200579 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200580 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300581 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
582 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
583 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200584
585 memset( iv, 0x2a, sizeof( iv ) );
586
587 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
588
589 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200590 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200591
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200592 TEST_ASSERT( psa_encrypt_setup( &operation,
593 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200594
595 TEST_ASSERT( psa_encrypt_set_iv( &operation,
596 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200597 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200598 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300599 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200600
Gilles Peskine4abf7412018-06-18 16:35:34 +0200601 TEST_ASSERT( psa_cipher_update( &operation,
602 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200603 output, output_buffer_size,
604 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200605 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200606 status = psa_cipher_finish( &operation,
607 output + function_output_length,
608 output_buffer_size,
609 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200610 total_output_length += function_output_length;
611
Gilles Peskine50e586b2018-06-08 14:28:46 +0200612 TEST_ASSERT( status == (psa_status_t) expected_status );
613 if( expected_status == PSA_SUCCESS )
614 {
615 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200616 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300617 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200618 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200619 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200620
Gilles Peskine50e586b2018-06-08 14:28:46 +0200621exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300622 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200623 psa_destroy_key( key_slot );
624 mbedtls_psa_crypto_free( );
625}
626/* END_CASE */
627
628/* BEGIN_CASE */
629void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300630 data_t *key,
631 data_t *input,
632 int first_part_size,
633 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +0200634{
635 int key_slot = 1;
636 psa_key_type_t key_type = key_type_arg;
637 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200638 unsigned char iv[16] = {0};
itayzafrir3e02b3b2018-06-12 17:06:52 +0300639 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200640 size_t output_buffer_size = 0;
641 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200642 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200643 psa_cipher_operation_t operation;
644
Gilles Peskine50e586b2018-06-08 14:28:46 +0200645 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200646 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200647 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300648 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
649 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
650 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200651
652 memset( iv, 0x2a, sizeof( iv ) );
653
654 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
655
656 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200657 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200658
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200659 TEST_ASSERT( psa_encrypt_setup( &operation,
660 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200661
662 TEST_ASSERT( psa_encrypt_set_iv( &operation,
663 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200664 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200665 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300666 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200667
Gilles Peskine4abf7412018-06-18 16:35:34 +0200668 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300669 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200670 output, output_buffer_size,
671 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200672 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200673 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300674 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200675 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200676 output, output_buffer_size,
677 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200678 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200679 TEST_ASSERT( psa_cipher_finish( &operation,
680 output + function_output_length,
681 output_buffer_size,
682 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200683 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200684 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
685
Gilles Peskine4abf7412018-06-18 16:35:34 +0200686 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300687 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200688 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200689
690exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300691 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200692 psa_destroy_key( key_slot );
693 mbedtls_psa_crypto_free( );
694}
695/* END_CASE */
696
697/* BEGIN_CASE */
698void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300699 data_t *key,
700 data_t *input,
701 int first_part_size,
702 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +0200703{
704 int key_slot = 1;
705
706 psa_key_type_t key_type = key_type_arg;
707 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200708 unsigned char iv[16] = {0};
itayzafrir3e02b3b2018-06-12 17:06:52 +0300709 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200710 size_t output_buffer_size = 0;
711 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200712 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200713 psa_cipher_operation_t operation;
714
Gilles Peskine50e586b2018-06-08 14:28:46 +0200715 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200716 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200717 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300718 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
719 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
720 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200721
722 memset( iv, 0x2a, sizeof( iv ) );
723
724 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
725
726 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200727 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200728
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200729 TEST_ASSERT( psa_decrypt_setup( &operation,
730 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200731
732 TEST_ASSERT( psa_encrypt_set_iv( &operation,
733 iv, sizeof( iv ) ) == PSA_SUCCESS );
734
Gilles Peskine4abf7412018-06-18 16:35:34 +0200735 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200736 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300737 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200738
Gilles Peskine4abf7412018-06-18 16:35:34 +0200739 TEST_ASSERT( (unsigned int) first_part_size < input->len );
740 TEST_ASSERT( psa_cipher_update( &operation,
741 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200742 output, output_buffer_size,
743 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200744 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200745 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300746 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200747 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200748 output, output_buffer_size,
749 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200750 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200751 TEST_ASSERT( psa_cipher_finish( &operation,
752 output + function_output_length,
753 output_buffer_size,
754 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200755 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200756 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
757
Gilles Peskine4abf7412018-06-18 16:35:34 +0200758 TEST_ASSERT( total_output_length == expected_output->len );
Gilles Peskine2d277862018-06-18 15:41:12 +0200759 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200760 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200761
762exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300763 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200764 psa_destroy_key( key_slot );
765 mbedtls_psa_crypto_free( );
766}
767/* END_CASE */
768
Gilles Peskine50e586b2018-06-08 14:28:46 +0200769/* BEGIN_CASE */
770void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300771 data_t *key,
772 data_t *input, data_t *expected_output,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200773 int expected_status )
774{
775 int key_slot = 1;
776 psa_status_t status;
777 psa_key_type_t key_type = key_type_arg;
778 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200779 unsigned char iv[16] = {0};
itayzafrir3e02b3b2018-06-12 17:06:52 +0300780 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200781 size_t output_buffer_size = 0;
782 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200783 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200784 psa_cipher_operation_t operation;
785
Gilles Peskine50e586b2018-06-08 14:28:46 +0200786 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200787 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200788 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300789 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
790 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
791 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200792
793 memset( iv, 0x2a, sizeof( iv ) );
794
795 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
796
797 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200798 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200799
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200800 TEST_ASSERT( psa_decrypt_setup( &operation,
801 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200802
803 TEST_ASSERT( psa_encrypt_set_iv( &operation,
804 iv, sizeof( iv ) ) == PSA_SUCCESS );
805
Gilles Peskine4abf7412018-06-18 16:35:34 +0200806 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200807 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300808 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200809
Gilles Peskine4abf7412018-06-18 16:35:34 +0200810 TEST_ASSERT( psa_cipher_update( &operation,
811 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200812 output, output_buffer_size,
813 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200814 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200815 status = psa_cipher_finish( &operation,
816 output + function_output_length,
817 output_buffer_size,
818 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200819 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200820 TEST_ASSERT( status == (psa_status_t) expected_status );
821
822 if( expected_status == PSA_SUCCESS )
823 {
824 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200825 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300826 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200827 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200828 }
829
Gilles Peskine50e586b2018-06-08 14:28:46 +0200830exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300831 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200832 psa_destroy_key( key_slot );
833 mbedtls_psa_crypto_free( );
834}
835/* END_CASE */
836
Gilles Peskine50e586b2018-06-08 14:28:46 +0200837/* BEGIN_CASE */
838void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300839 data_t *key,
840 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +0200841{
842 int key_slot = 1;
843 psa_key_type_t key_type = key_type_arg;
844 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -0700845 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +0200846 size_t iv_size = 16;
847 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300848 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200849 size_t output1_size = 0;
850 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300851 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200852 size_t output2_size = 0;
853 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +0200854 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200855 psa_cipher_operation_t operation1;
856 psa_cipher_operation_t operation2;
857
mohammad1603d7d7ba52018-03-12 18:51:53 +0200858 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200859 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300860 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
861 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200862
mohammad1603d7d7ba52018-03-12 18:51:53 +0200863 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
864
865 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200866 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200867
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200868 TEST_ASSERT( psa_encrypt_setup( &operation1,
869 key_slot, alg ) == PSA_SUCCESS );
870 TEST_ASSERT( psa_decrypt_setup( &operation2,
871 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200872
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200873 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
874 iv, iv_size,
875 &iv_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200876 output1_size = input->len + operation1.block_size;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200877 output1 = mbedtls_calloc( 1, output1_size );
878 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +0300879
Gilles Peskine4abf7412018-06-18 16:35:34 +0200880 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200881 output1, output1_size,
882 &output1_length ) == PSA_SUCCESS );
883 TEST_ASSERT( psa_cipher_finish( &operation1,
884 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200885 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200886
Gilles Peskine048b7f02018-06-08 14:20:49 +0200887 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300888
889 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
890
891 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200892 output2 = mbedtls_calloc( 1, output2_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300893 TEST_ASSERT( output2 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +0300894
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200895 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
896 iv, iv_length ) == PSA_SUCCESS );
897 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
898 output2, output2_size,
899 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +0200900 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200901 TEST_ASSERT( psa_cipher_finish( &operation2,
902 output2 + output2_length,
903 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200904 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300905
Gilles Peskine048b7f02018-06-08 14:20:49 +0200906 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200907
Moran Pekerded84402018-06-06 16:36:50 +0300908 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
909
Gilles Peskine4abf7412018-06-18 16:35:34 +0200910 TEST_ASSERT( input->len == output2_length );
911 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +0300912
913exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300914 mbedtls_free( output1 );
915 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +0300916 psa_destroy_key( key_slot );
917 mbedtls_psa_crypto_free( );
918}
919/* END_CASE */
920
921/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +0200922void cipher_verify_output_multipart( int alg_arg,
923 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300924 data_t *key,
925 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200926 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +0300927{
928 int key_slot = 1;
929 psa_key_type_t key_type = key_type_arg;
930 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +0300931 unsigned char iv[16] = {0};
932 size_t iv_size = 16;
933 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300934 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +0200935 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +0300936 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300937 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +0200938 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +0300939 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +0200940 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300941 psa_cipher_operation_t operation1;
942 psa_cipher_operation_t operation2;
943
Moran Pekerded84402018-06-06 16:36:50 +0300944 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +0300945 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300946 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
947 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200948
Moran Pekerded84402018-06-06 16:36:50 +0300949 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
950
951 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200952 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300953
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200954 TEST_ASSERT( psa_encrypt_setup( &operation1,
955 key_slot, alg ) == PSA_SUCCESS );
956 TEST_ASSERT( psa_decrypt_setup( &operation2,
957 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300958
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200959 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
960 iv, iv_size,
961 &iv_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200962 output1_buffer_size = input->len + operation1.block_size;
Gilles Peskine048b7f02018-06-08 14:20:49 +0200963 output1 = mbedtls_calloc( 1, output1_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300964 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +0300965
Gilles Peskine4abf7412018-06-18 16:35:34 +0200966 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200967
itayzafrir3e02b3b2018-06-12 17:06:52 +0300968 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200969 output1, output1_buffer_size,
970 &function_output_length ) == PSA_SUCCESS );
971 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300972
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200973 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300974 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200975 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200976 output1, output1_buffer_size,
977 &function_output_length ) == PSA_SUCCESS );
978 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300979
Gilles Peskine048b7f02018-06-08 14:20:49 +0200980 TEST_ASSERT( psa_cipher_finish( &operation1,
981 output1 + output1_length,
982 output1_buffer_size - output1_length,
983 &function_output_length ) == PSA_SUCCESS );
984 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200985
986 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
987
Gilles Peskine048b7f02018-06-08 14:20:49 +0200988 output2_buffer_size = output1_length;
989 output2 = mbedtls_calloc( 1, output2_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300990 TEST_ASSERT( output2 != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200991
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200992 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
993 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300994
995 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200996 output2, output2_buffer_size,
997 &function_output_length ) == PSA_SUCCESS );
998 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300999
Gilles Peskine048b7f02018-06-08 14:20:49 +02001000 TEST_ASSERT( psa_cipher_update( &operation2,
1001 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001002 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001003 output2, output2_buffer_size,
1004 &function_output_length ) == PSA_SUCCESS );
1005 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001006
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001007 TEST_ASSERT( psa_cipher_finish( &operation2,
1008 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001009 output2_buffer_size - output2_length,
1010 &function_output_length ) == PSA_SUCCESS );
1011 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001012
mohammad1603d7d7ba52018-03-12 18:51:53 +02001013 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1014
Gilles Peskine4abf7412018-06-18 16:35:34 +02001015 TEST_ASSERT( input->len == output2_length );
1016 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001017
1018exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001019 mbedtls_free( output1 );
1020 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001021 psa_destroy_key( key_slot );
1022 mbedtls_psa_crypto_free( );
1023}
1024/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02001025
Gilles Peskine20035e32018-02-03 22:44:14 +01001026/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001027void aead_encrypt_decrypt( int key_type_arg,
1028 data_t * key_data,
1029 int alg_arg,
1030 data_t * input_data,
1031 data_t * nonce,
1032 data_t * additional_data,
1033 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001034{
1035 int slot = 1;
1036 psa_key_type_t key_type = key_type_arg;
1037 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001038 unsigned char *output_data = NULL;
1039 size_t output_size = 0;
1040 size_t output_length = 0;
1041 unsigned char *output_data2 = NULL;
1042 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001043 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001044 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001045 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001046
Gilles Peskinea1cac842018-06-11 19:33:02 +02001047 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001048 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001049 TEST_ASSERT( nonce != NULL );
1050 TEST_ASSERT( additional_data != NULL );
1051 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1052 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1053 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1054 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1055
Gilles Peskine4abf7412018-06-18 16:35:34 +02001056 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001057 output_data = mbedtls_calloc( 1, output_size );
1058 TEST_ASSERT( output_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001059
1060 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1061
1062 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001063 psa_key_policy_set_usage( &policy,
1064 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
1065 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001066 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1067
1068 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001069 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001070
1071 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001072 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001073 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001074 additional_data->len,
1075 input_data->x, input_data->len,
1076 output_data, output_size,
1077 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001078
1079 if( PSA_SUCCESS == expected_result )
1080 {
1081 output_data2 = mbedtls_calloc( 1, output_length );
1082 TEST_ASSERT( output_data2 != NULL );
1083
1084 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001085 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001086 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001087 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001088 output_data, output_length,
1089 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001090 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02001091
itayzafrir3e02b3b2018-06-12 17:06:52 +03001092 TEST_ASSERT( memcmp( input_data->x, output_data2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001093 input_data->len ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001094 }
Gilles Peskine2d277862018-06-18 15:41:12 +02001095
Gilles Peskinea1cac842018-06-11 19:33:02 +02001096exit:
1097 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001098 mbedtls_free( output_data );
1099 mbedtls_free( output_data2 );
1100 mbedtls_psa_crypto_free( );
1101}
1102/* END_CASE */
1103
1104/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001105void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001106 int alg_arg, data_t * input_data,
1107 data_t * additional_data, data_t * nonce,
1108 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001109{
1110 int slot = 1;
1111 psa_key_type_t key_type = key_type_arg;
1112 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001113 unsigned char *output_data = NULL;
1114 size_t output_size = 0;
1115 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001116 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001117 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001118
Gilles Peskinea1cac842018-06-11 19:33:02 +02001119 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001120 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001121 TEST_ASSERT( additional_data != NULL );
1122 TEST_ASSERT( nonce != NULL );
1123 TEST_ASSERT( expected_result != NULL );
1124 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1125 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1126 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1127 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1128 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
1129
Gilles Peskine4abf7412018-06-18 16:35:34 +02001130 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001131 output_data = mbedtls_calloc( 1, output_size );
1132 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001133
Gilles Peskinea1cac842018-06-11 19:33:02 +02001134 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1135
1136 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001137 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001138 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1139
1140 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001141 key_data->x,
1142 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001143
1144 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001145 nonce->x, nonce->len,
1146 additional_data->x, additional_data->len,
1147 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001148 output_data, output_size,
1149 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001150
itayzafrir3e02b3b2018-06-12 17:06:52 +03001151 TEST_ASSERT( memcmp( output_data, expected_result->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001152 output_length ) == 0 );
1153
Gilles Peskinea1cac842018-06-11 19:33:02 +02001154exit:
1155 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001156 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001157 mbedtls_psa_crypto_free( );
1158}
1159/* END_CASE */
1160
1161/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001162void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001163 int alg_arg, data_t * input_data,
1164 data_t * additional_data, data_t * nonce,
1165 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001166{
1167 int slot = 1;
1168 psa_key_type_t key_type = key_type_arg;
1169 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001170 unsigned char *output_data = NULL;
1171 size_t output_size = 0;
1172 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001173 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001174 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001175 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001176
Gilles Peskinea1cac842018-06-11 19:33:02 +02001177 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001178 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001179 TEST_ASSERT( additional_data != NULL );
1180 TEST_ASSERT( nonce != NULL );
1181 TEST_ASSERT( expected_data != NULL );
1182 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1183 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1184 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1185 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1186 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
1187
Gilles Peskine4abf7412018-06-18 16:35:34 +02001188 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001189 output_data = mbedtls_calloc( 1, output_size );
1190 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001191
Gilles Peskinea1cac842018-06-11 19:33:02 +02001192 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1193
1194 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001195 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001196 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1197
1198 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001199 key_data->x,
1200 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001201
1202 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001203 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001204 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001205 additional_data->len,
1206 input_data->x, input_data->len,
1207 output_data, output_size,
1208 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001209
Gilles Peskine2d277862018-06-18 15:41:12 +02001210 if( expected_result == PSA_SUCCESS )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001211 {
itayzafrir3e02b3b2018-06-12 17:06:52 +03001212 TEST_ASSERT( memcmp( output_data, expected_data->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001213 output_length ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001214 }
1215
Gilles Peskinea1cac842018-06-11 19:33:02 +02001216exit:
1217 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001218 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001219 mbedtls_psa_crypto_free( );
1220}
1221/* END_CASE */
1222
1223/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001224void signature_size( int type_arg,
1225 int bits,
1226 int alg_arg,
1227 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001228{
1229 psa_key_type_t type = type_arg;
1230 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02001231 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01001232 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
1233exit:
1234 ;
1235}
1236/* END_CASE */
1237
1238/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001239void sign_deterministic( int key_type_arg, data_t *key_data,
1240 int alg_arg, data_t *input_data,
1241 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001242{
1243 int slot = 1;
1244 psa_key_type_t key_type = key_type_arg;
1245 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01001246 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01001247 unsigned char *signature = NULL;
1248 size_t signature_size;
1249 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02001250 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01001251
Gilles Peskine20035e32018-02-03 22:44:14 +01001252 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001253 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001254 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001255 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1256 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1257 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01001258
1259 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1260
mohammad1603a97cb8c2018-03-28 03:46:26 -07001261 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001262 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001263 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1264
Gilles Peskine20035e32018-02-03 22:44:14 +01001265 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001266 key_data->x,
1267 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01001268 TEST_ASSERT( psa_get_key_information( slot,
1269 NULL,
1270 &key_bits ) == PSA_SUCCESS );
1271
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001272 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
1273 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01001274 TEST_ASSERT( signature_size != 0 );
1275 signature = mbedtls_calloc( 1, signature_size );
1276 TEST_ASSERT( signature != NULL );
1277
1278 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001279 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01001280 NULL, 0,
1281 signature, signature_size,
1282 &signature_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001283 TEST_ASSERT( signature_length == output_data->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001284 TEST_ASSERT( memcmp( signature, output_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001285 output_data->len ) == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01001286
1287exit:
1288 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01001289 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01001290 mbedtls_psa_crypto_free( );
1291}
1292/* END_CASE */
1293
1294/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001295void sign_fail( int key_type_arg, data_t *key_data,
1296 int alg_arg, data_t *input_data,
Gilles Peskine20035e32018-02-03 22:44:14 +01001297 int signature_size, int expected_status_arg )
1298{
1299 int slot = 1;
1300 psa_key_type_t key_type = key_type_arg;
1301 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01001302 psa_status_t actual_status;
1303 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01001304 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01001305 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02001306 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01001307
Gilles Peskine20035e32018-02-03 22:44:14 +01001308 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001309 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001310 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1311 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1312
Gilles Peskine20035e32018-02-03 22:44:14 +01001313 signature = mbedtls_calloc( 1, signature_size );
1314 TEST_ASSERT( signature != NULL );
1315
1316 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1317
mohammad1603a97cb8c2018-03-28 03:46:26 -07001318 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001319 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001320 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1321
Gilles Peskine20035e32018-02-03 22:44:14 +01001322 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001323 key_data->x,
1324 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01001325
1326 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001327 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01001328 NULL, 0,
1329 signature, signature_size,
1330 &signature_length );
1331 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine93aa0332018-02-03 23:58:03 +01001332 TEST_ASSERT( signature_length == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01001333
1334exit:
1335 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01001336 mbedtls_free( signature );
1337 mbedtls_psa_crypto_free( );
1338}
1339/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03001340
1341/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001342void asymmetric_verify( int key_type_arg, data_t *key_data,
1343 int alg_arg, data_t *hash_data,
1344 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03001345{
1346 int slot = 1;
1347 psa_key_type_t key_type = key_type_arg;
1348 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001349 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03001350
itayzafrir5c753392018-05-08 11:18:38 +03001351 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03001352 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03001353 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001354 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1355 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
1356 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03001357
1358 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1359
1360 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001361 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03001362 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1363
1364 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001365 key_data->x,
1366 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03001367
1368 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001369 hash_data->x, hash_data->len,
itayzafrir5c753392018-05-08 11:18:38 +03001370 NULL, 0,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001371 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001372 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03001373exit:
1374 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03001375 mbedtls_psa_crypto_free( );
1376}
1377/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001378
1379/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001380void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
1381 int alg_arg, data_t *hash_data,
1382 data_t *signature_data,
1383 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001384{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001385 int slot = 1;
1386 psa_key_type_t key_type = key_type_arg;
1387 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001388 psa_status_t actual_status;
1389 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001390 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001391
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001392 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001393 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001394 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001395 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1396 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
1397 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001398
1399 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1400
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001401 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001402 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001403 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1404
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001405 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001406 key_data->x,
1407 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001408
1409 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001410 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001411 NULL, 0,
1412 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001413 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001414
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001415 TEST_ASSERT( actual_status == expected_status );
1416
1417exit:
1418 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001419 mbedtls_psa_crypto_free( );
1420}
1421/* END_CASE */
1422
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001423/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001424void asymmetric_encrypt_decrypt( int key_type_arg, data_t *key_data,
1425 int alg_arg, data_t *input_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001426{
1427 int slot = 1;
1428 psa_key_type_t key_type = key_type_arg;
1429 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001430 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001431 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001432 size_t output_length = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001433 unsigned char *output2 = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001434 size_t output2_size = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001435 size_t output2_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02001436 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001437
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001438 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001439 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001440 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1441 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1442
Gilles Peskine4abf7412018-06-18 16:35:34 +02001443 output_size = key_data->len;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001444 output2_size = output_size;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001445 output = mbedtls_calloc( 1, output_size );
1446 TEST_ASSERT( output != NULL );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001447 output2 = mbedtls_calloc( 1, output2_size );
1448 TEST_ASSERT( output2 != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001449
1450 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1451
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001452 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001453 psa_key_policy_set_usage( &policy,
1454 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001455 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001456 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1457
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001458 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001459 key_data->x,
1460 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001461
Gilles Peskineeebd7382018-06-08 18:11:54 +02001462 /* We test encryption by checking that encrypt-then-decrypt gives back
1463 * the original plaintext because of the non-optional random
1464 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02001465 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001466 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001467 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001468 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001469 &output_length ) == PSA_SUCCESS );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001470
Gilles Peskine2d277862018-06-18 15:41:12 +02001471 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001472 output, output_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001473 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001474 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001475 &output2_length ) == PSA_SUCCESS );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001476 TEST_ASSERT( memcmp( input_data->x, output2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001477 input_data->len ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001478
1479exit:
1480 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001481 mbedtls_free( output );
1482 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001483 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001484}
1485/* END_CASE */
1486
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001487/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001488void asymmetric_encrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001489 int alg_arg, data_t *input_data,
1490 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001491{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001492 int slot = 1;
1493 psa_key_type_t key_type = key_type_arg;
1494 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001495 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001496 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001497 size_t output_length = 0;
1498 psa_status_t actual_status;
1499 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001500 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001501
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001502 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001503 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001504 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1505 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1506
Gilles Peskine4abf7412018-06-18 16:35:34 +02001507 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001508 output = mbedtls_calloc( 1, output_size );
1509 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001510
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001511 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1512
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001513 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001514 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001515 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1516
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001517 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001518 key_data->x,
1519 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001520
Gilles Peskine2d277862018-06-18 15:41:12 +02001521 actual_status = psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001522 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001523 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001524 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001525 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001526 TEST_ASSERT( actual_status == expected_status );
1527
1528exit:
1529 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001530 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001531 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001532}
1533/* END_CASE */
1534
1535/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001536void asymmetric_decrypt( int key_type_arg, data_t *key_data,
1537 int alg_arg, data_t *input_data,
1538 data_t *expected_data, int expected_size )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001539{
1540 int slot = 1;
1541 psa_key_type_t key_type = key_type_arg;
1542 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001543 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001544 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001545 size_t output_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02001546 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001547
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001548 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001549 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001550 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001551 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1552 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1553 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
1554
Gilles Peskine4abf7412018-06-18 16:35:34 +02001555 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001556 output = mbedtls_calloc( 1, output_size );
1557 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001558
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001559 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1560
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001561 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001562 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001563 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1564
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001565 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001566 key_data->x,
1567 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001568
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001569 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001570 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001571 NULL, 0,
1572 output,
1573 output_size,
1574 &output_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001575 TEST_ASSERT( (size_t) expected_size == output_length );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001576 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001577
1578exit:
1579 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001580 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001581 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001582}
1583/* END_CASE */
1584
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001585/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001586void asymmetric_decrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001587 int alg_arg, data_t *input_data,
1588 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001589{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001590 int slot = 1;
1591 psa_key_type_t key_type = key_type_arg;
1592 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001593 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001594 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001595 size_t output_length = 0;
1596 psa_status_t actual_status;
1597 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001598 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001599
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001600 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001601 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001602 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1603 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1604
Gilles Peskine4abf7412018-06-18 16:35:34 +02001605 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001606 output = mbedtls_calloc( 1, output_size );
1607 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001608
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001609 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1610
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001611 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001612 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001613 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1614
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001615 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001616 key_data->x,
1617 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001618
Gilles Peskine2d277862018-06-18 15:41:12 +02001619 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001620 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001621 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001622 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001623 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001624 TEST_ASSERT( actual_status == expected_status );
1625
1626exit:
1627 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02001628 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001629 mbedtls_psa_crypto_free( );
1630}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001631/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02001632
1633/* BEGIN_CASE */
1634void generate_random( int bytes, int retries )
1635{
1636 const unsigned char trail[] = "foobar";
1637 unsigned char *buffer1 = mbedtls_calloc( 1, bytes + sizeof( trail ) );
1638 unsigned char *buffer2 = mbedtls_calloc( 1, bytes );
1639
1640 TEST_ASSERT( buffer1 != NULL );
1641 TEST_ASSERT( buffer2 != NULL );
1642 memcpy( buffer1 + bytes, trail, sizeof( trail ) );
1643
1644 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1645
1646 TEST_ASSERT( psa_generate_random( buffer1, bytes ) == PSA_SUCCESS );
1647
1648 /* Check that no more than bytes have been overwritten */
1649 TEST_ASSERT( memcmp( buffer1 + bytes, trail, sizeof( trail ) ) == 0 );
1650
1651 if( bytes == 0 )
1652 goto exit;
1653
1654 /* We can't validate that the data is really random, but we can
1655 * validate that it doesn't repeat between calls. There's a
1656 * 1/256^bytes chance that it does repeat, of course, so allow
1657 * a few retries. */
1658 ++retries; /* The first time isn't a REtry */
1659 do
1660 {
1661 --retries;
1662 TEST_ASSERT( psa_generate_random( buffer2, bytes ) == PSA_SUCCESS );
1663 }
1664 while( memcmp( buffer1, buffer2, bytes ) == 0 && retries >= -1 );
1665 TEST_ASSERT( retries >= 0 );
1666
1667exit:
1668 mbedtls_psa_crypto_free( );
1669 mbedtls_free( buffer1 );
1670 mbedtls_free( buffer2 );
1671}
1672/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02001673
1674/* BEGIN_CASE */
1675void generate_key( int type_arg,
1676 int bits_arg,
1677 int usage_arg,
1678 int alg_arg,
1679 int expected_status_arg )
1680{
1681 int slot = 1;
1682 psa_key_type_t type = type_arg;
1683 psa_key_usage_t usage = usage_arg;
1684 size_t bits = bits_arg;
1685 psa_algorithm_t alg = alg_arg;
1686 psa_status_t expected_status = expected_status_arg;
1687 psa_key_type_t got_type;
1688 size_t got_bits;
1689 unsigned char exported[616] = {0}; /* enough for a 1024-bit RSA key */
1690 size_t exported_length;
1691 psa_status_t expected_export_status =
1692 usage & PSA_KEY_USAGE_EXPORT ? PSA_SUCCESS : PSA_ERROR_NOT_PERMITTED;
1693 psa_status_t expected_info_status =
1694 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
1695 psa_key_policy_t policy;
1696
1697 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1698
1699 psa_key_policy_init( &policy );
1700 psa_key_policy_set_usage( &policy, usage, alg );
1701 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1702
1703 /* Generate a key */
1704 TEST_ASSERT( psa_generate_key( slot, type, bits,
1705 NULL, 0 ) == expected_status );
1706
1707 /* Test the key information */
1708 TEST_ASSERT( psa_get_key_information( slot,
1709 &got_type,
1710 &got_bits ) == expected_info_status );
1711 if( expected_info_status != PSA_SUCCESS )
1712 goto exit;
1713 TEST_ASSERT( got_type == type );
1714 TEST_ASSERT( got_bits == bits );
1715
1716 /* Export the key */
1717 TEST_ASSERT( psa_export_key( slot,
1718 exported, sizeof( exported ),
1719 &exported_length ) == expected_export_status );
1720 if( expected_export_status == PSA_SUCCESS )
1721 {
1722 if( PSA_KEY_TYPE_IS_RAW_BYTES( type ) )
1723 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
1724#if defined(MBEDTLS_DES_C)
1725 if( type == PSA_KEY_TYPE_DES )
1726 {
1727 /* Check the parity bits. */
1728 unsigned i;
1729 for( i = 0; i < bits / 8; i++ )
1730 {
1731 unsigned bit_count = 0;
1732 unsigned m;
1733 for( m = 1; m <= 0x100; m <<= 1 )
1734 {
1735 if( exported[i] & m )
1736 ++bit_count;
1737 }
1738 TEST_ASSERT( bit_count % 2 != 0 );
1739 }
1740 }
1741#endif
1742#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
1743 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
1744 {
1745 /* Sanity check: does this look like the beginning of a PKCS#8
1746 * RSA key pair? Assumes bits is a multiple of 8. */
1747 size_t n_bytes = bits / 8 + 1;
1748 size_t n_encoded_bytes;
1749 unsigned char *n_end;
1750 TEST_ASSERT( exported_length >= 7 + ( n_bytes + 3 ) * 9 / 2 );
1751 TEST_ASSERT( exported[0] == 0x30 );
1752 TEST_ASSERT( exported[1] == 0x82 ); // assumes >=416-bit key
1753 TEST_ASSERT( exported[4] == 0x02 );
1754 TEST_ASSERT( exported[5] == 0x01 );
1755 TEST_ASSERT( exported[6] == 0x00 );
1756 TEST_ASSERT( exported[7] == 0x02 );
1757 n_encoded_bytes = exported[8];
1758 n_end = exported + 9 + n_encoded_bytes;
1759 if( n_encoded_bytes & 0x80 )
1760 {
1761 n_encoded_bytes = ( n_encoded_bytes & 0x7f ) << 7;
1762 n_encoded_bytes |= exported[9] & 0x7f;
1763 n_end += 1;
1764 }
1765 /* The encoding of n should start with a 0 byte since it should
1766 * have its high bit set. However Mbed TLS is not compliant and
1767 * generates an invalid, but widely tolerated, encoding of
1768 * positive INTEGERs with a bit size that is a multiple of 8
1769 * with no leading 0 byte. Accept this here. */
1770 TEST_ASSERT( n_bytes == n_encoded_bytes ||
1771 n_bytes == n_encoded_bytes + 1 );
1772 if( n_bytes == n_encoded_bytes )
1773 TEST_ASSERT( exported[n_encoded_bytes <= 127 ? 9 : 10] == 0x00 );
1774 /* Sanity check: e must be 3 */
1775 TEST_ASSERT( n_end[0] == 0x02 );
1776 TEST_ASSERT( n_end[1] == 0x03 );
1777 TEST_ASSERT( n_end[2] == 0x01 );
1778 TEST_ASSERT( n_end[3] == 0x00 );
1779 TEST_ASSERT( n_end[4] == 0x01 );
1780 TEST_ASSERT( n_end[5] == 0x02 );
1781 }
1782#endif /* MBEDTLS_RSA_C */
1783#if defined(MBEDTLS_ECP_C)
1784 if( PSA_KEY_TYPE_IS_ECC( type ) )
1785 {
1786 /* Sanity check: does this look like the beginning of a PKCS#8
1787 * elliptic curve key pair? */
1788 TEST_ASSERT( exported_length >= bits * 3 / 8 + 10 );
1789 TEST_ASSERT( exported[0] == 0x30 );
1790 }
1791#endif /* MBEDTLS_ECP_C */
1792 }
1793
1794 /* We should do something with the key according to its permitted usage.
1795 * This would require figuring out what the key type allows or
1796 * specifying it somehow in the test data. */
1797
1798exit:
1799 psa_destroy_key( slot );
1800 mbedtls_psa_crypto_free( );
1801}
1802/* END_CASE */