blob: 95863756059f3843cdcb798fcc32bc07acdf25a5 [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 */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300378void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100379{
380 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +0200381 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100382 size_t actual_hash_length;
383 psa_hash_operation_t operation;
384
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100385 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300386 TEST_ASSERT( expected_hash != NULL );
387 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
388 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100389
390 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
391
392 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
393 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200394 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100395 TEST_ASSERT( psa_hash_finish( &operation,
396 actual_hash, sizeof( actual_hash ),
397 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200398 TEST_ASSERT( actual_hash_length == expected_hash->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300399 TEST_ASSERT( memcmp( expected_hash->x, actual_hash,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200400 expected_hash->len ) == 0 );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100401
402exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100403 mbedtls_psa_crypto_free( );
404}
405/* END_CASE */
406
407/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300408void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100409{
410 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100411 psa_hash_operation_t operation;
412
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100413 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300414 TEST_ASSERT( expected_hash != NULL );
415 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
416 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100417
418 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
419
420 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
421 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200422 input->x,
423 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100424 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300425 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200426 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100427
428exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100429 mbedtls_psa_crypto_free( );
430}
431/* END_CASE */
432
433/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +0200434void mac_verify( int key_type_arg,
435 data_t *key,
436 int alg_arg,
437 data_t *input,
438 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +0100439{
440 int key_slot = 1;
441 psa_key_type_t key_type = key_type_arg;
442 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100443 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -0700444 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100445
Gilles Peskine8c9def32018-02-08 10:02:12 +0100446 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100447 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100448 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300449 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300450 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
451 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100452
453 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
454
mohammad16036df908f2018-04-02 08:34:15 -0700455 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200456 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -0700457 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
458
Gilles Peskine8c9def32018-02-08 10:02:12 +0100459 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200460 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +0200461
Gilles Peskine8c9def32018-02-08 10:02:12 +0100462 TEST_ASSERT( psa_mac_start( &operation, key_slot, alg ) == PSA_SUCCESS );
463 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
464 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200465 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100466 TEST_ASSERT( psa_mac_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300467 expected_mac->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200468 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100469
470exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +0100471 psa_destroy_key( key_slot );
472 mbedtls_psa_crypto_free( );
473}
474/* END_CASE */
475
476/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +0200477void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300478 data_t *key,
479 data_t *input, data_t *expected_output,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200480 int expected_status )
481{
482 int key_slot = 1;
483 psa_status_t status;
484 psa_key_type_t key_type = key_type_arg;
485 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200486 unsigned char iv[16] = {0};
itayzafrir3e02b3b2018-06-12 17:06:52 +0300487 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200488 size_t output_buffer_size = 0;
489 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200490 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200491 psa_cipher_operation_t operation;
492
Gilles Peskine50e586b2018-06-08 14:28:46 +0200493 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200494 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200495 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300496 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
497 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
498 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200499
500 memset( iv, 0x2a, sizeof( iv ) );
501
502 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
503
504 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200505 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200506
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200507 TEST_ASSERT( psa_encrypt_setup( &operation,
508 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200509
510 TEST_ASSERT( psa_encrypt_set_iv( &operation,
511 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200512 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200513 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300514 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200515
Gilles Peskine4abf7412018-06-18 16:35:34 +0200516 TEST_ASSERT( psa_cipher_update( &operation,
517 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200518 output, output_buffer_size,
519 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200520 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200521 status = psa_cipher_finish( &operation,
522 output + function_output_length,
523 output_buffer_size,
524 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200525 total_output_length += function_output_length;
526
Gilles Peskine50e586b2018-06-08 14:28:46 +0200527 TEST_ASSERT( status == (psa_status_t) expected_status );
528 if( expected_status == PSA_SUCCESS )
529 {
530 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200531 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300532 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200533 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200534 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200535
Gilles Peskine50e586b2018-06-08 14:28:46 +0200536exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300537 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200538 psa_destroy_key( key_slot );
539 mbedtls_psa_crypto_free( );
540}
541/* END_CASE */
542
543/* BEGIN_CASE */
544void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300545 data_t *key,
546 data_t *input,
547 int first_part_size,
548 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +0200549{
550 int key_slot = 1;
551 psa_key_type_t key_type = key_type_arg;
552 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200553 unsigned char iv[16] = {0};
itayzafrir3e02b3b2018-06-12 17:06:52 +0300554 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200555 size_t output_buffer_size = 0;
556 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200557 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200558 psa_cipher_operation_t operation;
559
Gilles Peskine50e586b2018-06-08 14:28:46 +0200560 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200561 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200562 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300563 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
564 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
565 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200566
567 memset( iv, 0x2a, sizeof( iv ) );
568
569 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
570
571 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200572 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200573
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200574 TEST_ASSERT( psa_encrypt_setup( &operation,
575 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200576
577 TEST_ASSERT( psa_encrypt_set_iv( &operation,
578 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200579 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200580 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300581 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200582
Gilles Peskine4abf7412018-06-18 16:35:34 +0200583 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300584 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200585 output, output_buffer_size,
586 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200587 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200588 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300589 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200590 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200591 output, output_buffer_size,
592 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200593 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200594 TEST_ASSERT( psa_cipher_finish( &operation,
595 output + function_output_length,
596 output_buffer_size,
597 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200598 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200599 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
600
Gilles Peskine4abf7412018-06-18 16:35:34 +0200601 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300602 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200603 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200604
605exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300606 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200607 psa_destroy_key( key_slot );
608 mbedtls_psa_crypto_free( );
609}
610/* END_CASE */
611
612/* BEGIN_CASE */
613void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300614 data_t *key,
615 data_t *input,
616 int first_part_size,
617 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +0200618{
619 int key_slot = 1;
620
621 psa_key_type_t key_type = key_type_arg;
622 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200623 unsigned char iv[16] = {0};
itayzafrir3e02b3b2018-06-12 17:06:52 +0300624 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200625 size_t output_buffer_size = 0;
626 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200627 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200628 psa_cipher_operation_t operation;
629
Gilles Peskine50e586b2018-06-08 14:28:46 +0200630 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200631 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200632 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300633 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
634 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
635 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200636
637 memset( iv, 0x2a, sizeof( iv ) );
638
639 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
640
641 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200642 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200643
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200644 TEST_ASSERT( psa_decrypt_setup( &operation,
645 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200646
647 TEST_ASSERT( psa_encrypt_set_iv( &operation,
648 iv, sizeof( iv ) ) == PSA_SUCCESS );
649
Gilles Peskine4abf7412018-06-18 16:35:34 +0200650 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200651 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300652 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200653
Gilles Peskine4abf7412018-06-18 16:35:34 +0200654 TEST_ASSERT( (unsigned int) first_part_size < input->len );
655 TEST_ASSERT( psa_cipher_update( &operation,
656 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200657 output, output_buffer_size,
658 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200659 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200660 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300661 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200662 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200663 output, output_buffer_size,
664 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200665 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200666 TEST_ASSERT( psa_cipher_finish( &operation,
667 output + function_output_length,
668 output_buffer_size,
669 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200670 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200671 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
672
Gilles Peskine4abf7412018-06-18 16:35:34 +0200673 TEST_ASSERT( total_output_length == expected_output->len );
Gilles Peskine2d277862018-06-18 15:41:12 +0200674 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200675 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200676
677exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300678 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200679 psa_destroy_key( key_slot );
680 mbedtls_psa_crypto_free( );
681}
682/* END_CASE */
683
Gilles Peskine50e586b2018-06-08 14:28:46 +0200684/* BEGIN_CASE */
685void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300686 data_t *key,
687 data_t *input, data_t *expected_output,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200688 int expected_status )
689{
690 int key_slot = 1;
691 psa_status_t status;
692 psa_key_type_t key_type = key_type_arg;
693 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200694 unsigned char iv[16] = {0};
itayzafrir3e02b3b2018-06-12 17:06:52 +0300695 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200696 size_t output_buffer_size = 0;
697 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200698 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200699 psa_cipher_operation_t operation;
700
Gilles Peskine50e586b2018-06-08 14:28:46 +0200701 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200702 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200703 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300704 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
705 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
706 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200707
708 memset( iv, 0x2a, sizeof( iv ) );
709
710 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
711
712 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200713 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200714
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200715 TEST_ASSERT( psa_decrypt_setup( &operation,
716 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200717
718 TEST_ASSERT( psa_encrypt_set_iv( &operation,
719 iv, sizeof( iv ) ) == PSA_SUCCESS );
720
Gilles Peskine4abf7412018-06-18 16:35:34 +0200721 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200722 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300723 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200724
Gilles Peskine4abf7412018-06-18 16:35:34 +0200725 TEST_ASSERT( psa_cipher_update( &operation,
726 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200727 output, output_buffer_size,
728 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200729 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200730 status = psa_cipher_finish( &operation,
731 output + function_output_length,
732 output_buffer_size,
733 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200734 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200735 TEST_ASSERT( status == (psa_status_t) expected_status );
736
737 if( expected_status == PSA_SUCCESS )
738 {
739 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200740 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300741 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200742 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200743 }
744
Gilles Peskine50e586b2018-06-08 14:28:46 +0200745exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300746 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200747 psa_destroy_key( key_slot );
748 mbedtls_psa_crypto_free( );
749}
750/* END_CASE */
751
Gilles Peskine50e586b2018-06-08 14:28:46 +0200752/* BEGIN_CASE */
753void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300754 data_t *key,
755 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +0200756{
757 int key_slot = 1;
758 psa_key_type_t key_type = key_type_arg;
759 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -0700760 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +0200761 size_t iv_size = 16;
762 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300763 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200764 size_t output1_size = 0;
765 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300766 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200767 size_t output2_size = 0;
768 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +0200769 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200770 psa_cipher_operation_t operation1;
771 psa_cipher_operation_t operation2;
772
mohammad1603d7d7ba52018-03-12 18:51:53 +0200773 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200774 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300775 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
776 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200777
mohammad1603d7d7ba52018-03-12 18:51:53 +0200778 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
779
780 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200781 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200782
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200783 TEST_ASSERT( psa_encrypt_setup( &operation1,
784 key_slot, alg ) == PSA_SUCCESS );
785 TEST_ASSERT( psa_decrypt_setup( &operation2,
786 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200787
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200788 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
789 iv, iv_size,
790 &iv_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200791 output1_size = input->len + operation1.block_size;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200792 output1 = mbedtls_calloc( 1, output1_size );
793 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +0300794
Gilles Peskine4abf7412018-06-18 16:35:34 +0200795 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200796 output1, output1_size,
797 &output1_length ) == PSA_SUCCESS );
798 TEST_ASSERT( psa_cipher_finish( &operation1,
799 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200800 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200801
Gilles Peskine048b7f02018-06-08 14:20:49 +0200802 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300803
804 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
805
806 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200807 output2 = mbedtls_calloc( 1, output2_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300808 TEST_ASSERT( output2 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +0300809
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200810 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
811 iv, iv_length ) == PSA_SUCCESS );
812 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
813 output2, output2_size,
814 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +0200815 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200816 TEST_ASSERT( psa_cipher_finish( &operation2,
817 output2 + output2_length,
818 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200819 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300820
Gilles Peskine048b7f02018-06-08 14:20:49 +0200821 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200822
Moran Pekerded84402018-06-06 16:36:50 +0300823 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
824
Gilles Peskine4abf7412018-06-18 16:35:34 +0200825 TEST_ASSERT( input->len == output2_length );
826 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +0300827
828exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300829 mbedtls_free( output1 );
830 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +0300831 psa_destroy_key( key_slot );
832 mbedtls_psa_crypto_free( );
833}
834/* END_CASE */
835
836/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +0200837void cipher_verify_output_multipart( int alg_arg,
838 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300839 data_t *key,
840 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200841 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +0300842{
843 int key_slot = 1;
844 psa_key_type_t key_type = key_type_arg;
845 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +0300846 unsigned char iv[16] = {0};
847 size_t iv_size = 16;
848 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300849 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +0200850 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +0300851 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300852 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +0200853 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +0300854 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +0200855 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300856 psa_cipher_operation_t operation1;
857 psa_cipher_operation_t operation2;
858
Moran Pekerded84402018-06-06 16:36:50 +0300859 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +0300860 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300861 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
862 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200863
Moran Pekerded84402018-06-06 16:36:50 +0300864 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
865
866 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200867 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300868
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200869 TEST_ASSERT( psa_encrypt_setup( &operation1,
870 key_slot, alg ) == PSA_SUCCESS );
871 TEST_ASSERT( psa_decrypt_setup( &operation2,
872 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300873
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200874 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
875 iv, iv_size,
876 &iv_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200877 output1_buffer_size = input->len + operation1.block_size;
Gilles Peskine048b7f02018-06-08 14:20:49 +0200878 output1 = mbedtls_calloc( 1, output1_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300879 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +0300880
Gilles Peskine4abf7412018-06-18 16:35:34 +0200881 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200882
itayzafrir3e02b3b2018-06-12 17:06:52 +0300883 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200884 output1, output1_buffer_size,
885 &function_output_length ) == PSA_SUCCESS );
886 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300887
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200888 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300889 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200890 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200891 output1, output1_buffer_size,
892 &function_output_length ) == PSA_SUCCESS );
893 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300894
Gilles Peskine048b7f02018-06-08 14:20:49 +0200895 TEST_ASSERT( psa_cipher_finish( &operation1,
896 output1 + output1_length,
897 output1_buffer_size - output1_length,
898 &function_output_length ) == PSA_SUCCESS );
899 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200900
901 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
902
Gilles Peskine048b7f02018-06-08 14:20:49 +0200903 output2_buffer_size = output1_length;
904 output2 = mbedtls_calloc( 1, output2_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300905 TEST_ASSERT( output2 != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200906
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200907 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
908 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300909
910 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200911 output2, output2_buffer_size,
912 &function_output_length ) == PSA_SUCCESS );
913 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300914
Gilles Peskine048b7f02018-06-08 14:20:49 +0200915 TEST_ASSERT( psa_cipher_update( &operation2,
916 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200917 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200918 output2, output2_buffer_size,
919 &function_output_length ) == PSA_SUCCESS );
920 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300921
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200922 TEST_ASSERT( psa_cipher_finish( &operation2,
923 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200924 output2_buffer_size - output2_length,
925 &function_output_length ) == PSA_SUCCESS );
926 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200927
mohammad1603d7d7ba52018-03-12 18:51:53 +0200928 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
929
Gilles Peskine4abf7412018-06-18 16:35:34 +0200930 TEST_ASSERT( input->len == output2_length );
931 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200932
933exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300934 mbedtls_free( output1 );
935 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200936 psa_destroy_key( key_slot );
937 mbedtls_psa_crypto_free( );
938}
939/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +0200940
Gilles Peskine20035e32018-02-03 22:44:14 +0100941/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200942void aead_encrypt_decrypt( int key_type_arg,
943 data_t * key_data,
944 int alg_arg,
945 data_t * input_data,
946 data_t * nonce,
947 data_t * additional_data,
948 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +0200949{
950 int slot = 1;
951 psa_key_type_t key_type = key_type_arg;
952 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +0200953 unsigned char *output_data = NULL;
954 size_t output_size = 0;
955 size_t output_length = 0;
956 unsigned char *output_data2 = NULL;
957 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +0200958 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200959 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +0200960 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +0200961
Gilles Peskinea1cac842018-06-11 19:33:02 +0200962 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +0200963 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300964 TEST_ASSERT( nonce != NULL );
965 TEST_ASSERT( additional_data != NULL );
966 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
967 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
968 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
969 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
970
Gilles Peskine4abf7412018-06-18 16:35:34 +0200971 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +0200972 output_data = mbedtls_calloc( 1, output_size );
973 TEST_ASSERT( output_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +0200974
975 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
976
977 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200978 psa_key_policy_set_usage( &policy,
979 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
980 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +0200981 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
982
983 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200984 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +0200985
986 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200987 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +0200988 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200989 additional_data->len,
990 input_data->x, input_data->len,
991 output_data, output_size,
992 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +0200993
994 if( PSA_SUCCESS == expected_result )
995 {
996 output_data2 = mbedtls_calloc( 1, output_length );
997 TEST_ASSERT( output_data2 != NULL );
998
999 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001000 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001001 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001002 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001003 output_data, output_length,
1004 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001005 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02001006
itayzafrir3e02b3b2018-06-12 17:06:52 +03001007 TEST_ASSERT( memcmp( input_data->x, output_data2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001008 input_data->len ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001009 }
Gilles Peskine2d277862018-06-18 15:41:12 +02001010
Gilles Peskinea1cac842018-06-11 19:33:02 +02001011exit:
1012 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001013 mbedtls_free( output_data );
1014 mbedtls_free( output_data2 );
1015 mbedtls_psa_crypto_free( );
1016}
1017/* END_CASE */
1018
1019/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001020void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001021 int alg_arg, data_t * input_data,
1022 data_t * additional_data, data_t * nonce,
1023 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001024{
1025 int slot = 1;
1026 psa_key_type_t key_type = key_type_arg;
1027 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001028 unsigned char *output_data = NULL;
1029 size_t output_size = 0;
1030 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001031 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001032 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001033
Gilles Peskinea1cac842018-06-11 19:33:02 +02001034 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001035 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001036 TEST_ASSERT( additional_data != NULL );
1037 TEST_ASSERT( nonce != NULL );
1038 TEST_ASSERT( expected_result != NULL );
1039 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1040 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1041 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1042 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1043 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
1044
Gilles Peskine4abf7412018-06-18 16:35:34 +02001045 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001046 output_data = mbedtls_calloc( 1, output_size );
1047 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001048
Gilles Peskinea1cac842018-06-11 19:33:02 +02001049 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1050
1051 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001052 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001053 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1054
1055 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001056 key_data->x,
1057 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001058
1059 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001060 nonce->x, nonce->len,
1061 additional_data->x, additional_data->len,
1062 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001063 output_data, output_size,
1064 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001065
itayzafrir3e02b3b2018-06-12 17:06:52 +03001066 TEST_ASSERT( memcmp( output_data, expected_result->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001067 output_length ) == 0 );
1068
Gilles Peskinea1cac842018-06-11 19:33:02 +02001069exit:
1070 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001071 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001072 mbedtls_psa_crypto_free( );
1073}
1074/* END_CASE */
1075
1076/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001077void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001078 int alg_arg, data_t * input_data,
1079 data_t * additional_data, data_t * nonce,
1080 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001081{
1082 int slot = 1;
1083 psa_key_type_t key_type = key_type_arg;
1084 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001085 unsigned char *output_data = NULL;
1086 size_t output_size = 0;
1087 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001088 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001089 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001090 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001091
Gilles Peskinea1cac842018-06-11 19:33:02 +02001092 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001093 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001094 TEST_ASSERT( additional_data != NULL );
1095 TEST_ASSERT( nonce != NULL );
1096 TEST_ASSERT( expected_data != NULL );
1097 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1098 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1099 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1100 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1101 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
1102
Gilles Peskine4abf7412018-06-18 16:35:34 +02001103 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001104 output_data = mbedtls_calloc( 1, output_size );
1105 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001106
Gilles Peskinea1cac842018-06-11 19:33:02 +02001107 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1108
1109 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001110 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001111 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1112
1113 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001114 key_data->x,
1115 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001116
1117 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001118 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001119 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001120 additional_data->len,
1121 input_data->x, input_data->len,
1122 output_data, output_size,
1123 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001124
Gilles Peskine2d277862018-06-18 15:41:12 +02001125 if( expected_result == PSA_SUCCESS )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001126 {
itayzafrir3e02b3b2018-06-12 17:06:52 +03001127 TEST_ASSERT( memcmp( output_data, expected_data->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001128 output_length ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001129 }
1130
Gilles Peskinea1cac842018-06-11 19:33:02 +02001131exit:
1132 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001133 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001134 mbedtls_psa_crypto_free( );
1135}
1136/* END_CASE */
1137
1138/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001139void signature_size( int type_arg,
1140 int bits,
1141 int alg_arg,
1142 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001143{
1144 psa_key_type_t type = type_arg;
1145 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02001146 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01001147 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
1148exit:
1149 ;
1150}
1151/* END_CASE */
1152
1153/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001154void sign_deterministic( int key_type_arg, data_t *key_data,
1155 int alg_arg, data_t *input_data,
1156 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001157{
1158 int slot = 1;
1159 psa_key_type_t key_type = key_type_arg;
1160 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01001161 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01001162 unsigned char *signature = NULL;
1163 size_t signature_size;
1164 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02001165 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01001166
Gilles Peskine20035e32018-02-03 22:44:14 +01001167 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001168 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001169 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001170 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1171 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1172 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01001173
1174 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1175
mohammad1603a97cb8c2018-03-28 03:46:26 -07001176 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001177 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001178 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1179
Gilles Peskine20035e32018-02-03 22:44:14 +01001180 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001181 key_data->x,
1182 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01001183 TEST_ASSERT( psa_get_key_information( slot,
1184 NULL,
1185 &key_bits ) == PSA_SUCCESS );
1186
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001187 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
1188 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01001189 TEST_ASSERT( signature_size != 0 );
1190 signature = mbedtls_calloc( 1, signature_size );
1191 TEST_ASSERT( signature != NULL );
1192
1193 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001194 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01001195 NULL, 0,
1196 signature, signature_size,
1197 &signature_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001198 TEST_ASSERT( signature_length == output_data->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001199 TEST_ASSERT( memcmp( signature, output_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001200 output_data->len ) == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01001201
1202exit:
1203 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01001204 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01001205 mbedtls_psa_crypto_free( );
1206}
1207/* END_CASE */
1208
1209/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001210void sign_fail( int key_type_arg, data_t *key_data,
1211 int alg_arg, data_t *input_data,
Gilles Peskine20035e32018-02-03 22:44:14 +01001212 int signature_size, int expected_status_arg )
1213{
1214 int slot = 1;
1215 psa_key_type_t key_type = key_type_arg;
1216 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01001217 psa_status_t actual_status;
1218 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01001219 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01001220 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02001221 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01001222
Gilles Peskine20035e32018-02-03 22:44:14 +01001223 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001224 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001225 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1226 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1227
Gilles Peskine20035e32018-02-03 22:44:14 +01001228 signature = mbedtls_calloc( 1, signature_size );
1229 TEST_ASSERT( signature != NULL );
1230
1231 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1232
mohammad1603a97cb8c2018-03-28 03:46:26 -07001233 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001234 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001235 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1236
Gilles Peskine20035e32018-02-03 22:44:14 +01001237 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001238 key_data->x,
1239 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01001240
1241 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001242 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01001243 NULL, 0,
1244 signature, signature_size,
1245 &signature_length );
1246 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine93aa0332018-02-03 23:58:03 +01001247 TEST_ASSERT( signature_length == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01001248
1249exit:
1250 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01001251 mbedtls_free( signature );
1252 mbedtls_psa_crypto_free( );
1253}
1254/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03001255
1256/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001257void asymmetric_verify( int key_type_arg, data_t *key_data,
1258 int alg_arg, data_t *hash_data,
1259 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03001260{
1261 int slot = 1;
1262 psa_key_type_t key_type = key_type_arg;
1263 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001264 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03001265
itayzafrir5c753392018-05-08 11:18:38 +03001266 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03001267 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03001268 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001269 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1270 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
1271 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03001272
1273 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1274
1275 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001276 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03001277 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1278
1279 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001280 key_data->x,
1281 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03001282
1283 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001284 hash_data->x, hash_data->len,
itayzafrir5c753392018-05-08 11:18:38 +03001285 NULL, 0,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001286 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001287 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03001288exit:
1289 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03001290 mbedtls_psa_crypto_free( );
1291}
1292/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001293
1294/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001295void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
1296 int alg_arg, data_t *hash_data,
1297 data_t *signature_data,
1298 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001299{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001300 int slot = 1;
1301 psa_key_type_t key_type = key_type_arg;
1302 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001303 psa_status_t actual_status;
1304 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001305 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001306
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001307 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001308 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001309 TEST_ASSERT( signature_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( hash_data->len ) );
1312 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001313
1314 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1315
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001316 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001317 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001318 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1319
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001320 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001321 key_data->x,
1322 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001323
1324 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001325 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001326 NULL, 0,
1327 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001328 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001329
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001330 TEST_ASSERT( actual_status == expected_status );
1331
1332exit:
1333 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001334 mbedtls_psa_crypto_free( );
1335}
1336/* END_CASE */
1337
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001338/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001339void asymmetric_encrypt_decrypt( int key_type_arg, data_t *key_data,
1340 int alg_arg, data_t *input_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001341{
1342 int slot = 1;
1343 psa_key_type_t key_type = key_type_arg;
1344 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001345 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001346 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001347 size_t output_length = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001348 unsigned char *output2 = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001349 size_t output2_size = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001350 size_t output2_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02001351 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001352
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001353 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001354 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001355 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1356 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1357
Gilles Peskine4abf7412018-06-18 16:35:34 +02001358 output_size = key_data->len;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001359 output2_size = output_size;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001360 output = mbedtls_calloc( 1, output_size );
1361 TEST_ASSERT( output != NULL );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001362 output2 = mbedtls_calloc( 1, output2_size );
1363 TEST_ASSERT( output2 != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001364
1365 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1366
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001367 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001368 psa_key_policy_set_usage( &policy,
1369 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001370 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001371 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1372
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001373 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001374 key_data->x,
1375 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001376
Gilles Peskineeebd7382018-06-08 18:11:54 +02001377 /* We test encryption by checking that encrypt-then-decrypt gives back
1378 * the original plaintext because of the non-optional random
1379 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02001380 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001381 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001382 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001383 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001384 &output_length ) == PSA_SUCCESS );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001385
Gilles Peskine2d277862018-06-18 15:41:12 +02001386 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001387 output, output_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001388 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001389 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001390 &output2_length ) == PSA_SUCCESS );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001391 TEST_ASSERT( memcmp( input_data->x, output2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001392 input_data->len ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001393
1394exit:
1395 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001396 mbedtls_free( output );
1397 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001398 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001399}
1400/* END_CASE */
1401
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001402/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001403void asymmetric_encrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001404 int alg_arg, data_t *input_data,
1405 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001406{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001407 int slot = 1;
1408 psa_key_type_t key_type = key_type_arg;
1409 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001410 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001411 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001412 size_t output_length = 0;
1413 psa_status_t actual_status;
1414 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001415 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001416
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001417 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001418 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001419 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1420 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1421
Gilles Peskine4abf7412018-06-18 16:35:34 +02001422 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001423 output = mbedtls_calloc( 1, output_size );
1424 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001425
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001426 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1427
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001428 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001429 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001430 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1431
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001432 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001433 key_data->x,
1434 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001435
Gilles Peskine2d277862018-06-18 15:41:12 +02001436 actual_status = psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001437 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001438 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001439 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001440 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001441 TEST_ASSERT( actual_status == expected_status );
1442
1443exit:
1444 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001445 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001446 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001447}
1448/* END_CASE */
1449
1450/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001451void asymmetric_decrypt( int key_type_arg, data_t *key_data,
1452 int alg_arg, data_t *input_data,
1453 data_t *expected_data, int expected_size )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001454{
1455 int slot = 1;
1456 psa_key_type_t key_type = key_type_arg;
1457 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001458 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001459 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001460 size_t output_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02001461 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001462
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001463 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001464 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001465 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001466 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1467 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1468 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
1469
Gilles Peskine4abf7412018-06-18 16:35:34 +02001470 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001471 output = mbedtls_calloc( 1, output_size );
1472 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001473
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001474 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1475
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001476 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001477 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001478 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1479
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001480 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001481 key_data->x,
1482 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001483
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001484 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001485 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001486 NULL, 0,
1487 output,
1488 output_size,
1489 &output_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001490 TEST_ASSERT( (size_t) expected_size == output_length );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001491 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001492
1493exit:
1494 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001495 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001496 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001497}
1498/* END_CASE */
1499
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001500/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001501void asymmetric_decrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001502 int alg_arg, data_t *input_data,
1503 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001504{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001505 int slot = 1;
1506 psa_key_type_t key_type = key_type_arg;
1507 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001508 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001509 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001510 size_t output_length = 0;
1511 psa_status_t actual_status;
1512 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001513 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001514
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001515 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001516 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001517 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1518 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1519
Gilles Peskine4abf7412018-06-18 16:35:34 +02001520 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001521 output = mbedtls_calloc( 1, output_size );
1522 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001523
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001524 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1525
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001526 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001527 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001528 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1529
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001530 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001531 key_data->x,
1532 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001533
Gilles Peskine2d277862018-06-18 15:41:12 +02001534 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001535 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001536 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001537 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001538 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001539 TEST_ASSERT( actual_status == expected_status );
1540
1541exit:
1542 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02001543 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001544 mbedtls_psa_crypto_free( );
1545}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001546/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02001547
1548/* BEGIN_CASE */
1549void generate_random( int bytes, int retries )
1550{
1551 const unsigned char trail[] = "foobar";
1552 unsigned char *buffer1 = mbedtls_calloc( 1, bytes + sizeof( trail ) );
1553 unsigned char *buffer2 = mbedtls_calloc( 1, bytes );
1554
1555 TEST_ASSERT( buffer1 != NULL );
1556 TEST_ASSERT( buffer2 != NULL );
1557 memcpy( buffer1 + bytes, trail, sizeof( trail ) );
1558
1559 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1560
1561 TEST_ASSERT( psa_generate_random( buffer1, bytes ) == PSA_SUCCESS );
1562
1563 /* Check that no more than bytes have been overwritten */
1564 TEST_ASSERT( memcmp( buffer1 + bytes, trail, sizeof( trail ) ) == 0 );
1565
1566 if( bytes == 0 )
1567 goto exit;
1568
1569 /* We can't validate that the data is really random, but we can
1570 * validate that it doesn't repeat between calls. There's a
1571 * 1/256^bytes chance that it does repeat, of course, so allow
1572 * a few retries. */
1573 ++retries; /* The first time isn't a REtry */
1574 do
1575 {
1576 --retries;
1577 TEST_ASSERT( psa_generate_random( buffer2, bytes ) == PSA_SUCCESS );
1578 }
1579 while( memcmp( buffer1, buffer2, bytes ) == 0 && retries >= -1 );
1580 TEST_ASSERT( retries >= 0 );
1581
1582exit:
1583 mbedtls_psa_crypto_free( );
1584 mbedtls_free( buffer1 );
1585 mbedtls_free( buffer2 );
1586}
1587/* END_CASE */