blob: 9dbf0340d16f047910fcdd292c53322718540354 [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 Peskinee59236f2018-01-27 23:32:46 +010010/* END_HEADER */
11
12/* BEGIN_DEPENDENCIES
13 * depends_on:MBEDTLS_PSA_CRYPTO_C
14 * END_DEPENDENCIES
15 */
16
17/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +020018void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +010019{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010020 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +010021 int i;
22 for( i = 0; i <= 1; i++ )
23 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010024 status = psa_crypto_init( );
25 TEST_ASSERT( status == PSA_SUCCESS );
26 status = psa_crypto_init( );
27 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +010028 mbedtls_psa_crypto_free( );
29 }
30}
31/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010032
33/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +030034void import( data_t *data, int type, int expected_status )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010035{
36 int slot = 1;
37 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010038
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010039 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +030040 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010041 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
42
Gilles Peskine4abf7412018-06-18 16:35:34 +020043 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044 TEST_ASSERT( status == (psa_status_t) expected_status );
45 if( status == PSA_SUCCESS )
46 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
47
48exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010049 mbedtls_psa_crypto_free( );
50}
51/* END_CASE */
52
53/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +030054void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +030055 int type_arg,
56 int alg_arg,
57 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010058 int expected_bits,
59 int export_size_delta,
60 int expected_export_status,
61 int canonical_input )
62{
63 int slot = 1;
64 int slot2 = slot + 1;
65 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +020066 psa_algorithm_t alg = alg_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010067 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010068 unsigned char *exported = NULL;
69 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010070 size_t export_size;
71 size_t exported_length;
72 size_t reexported_length;
73 psa_key_type_t got_type;
74 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +020075 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010076
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010077 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +030078 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
79 export_size = (ssize_t) data->len + export_size_delta;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010080 exported = mbedtls_calloc( 1, export_size );
81 TEST_ASSERT( exported != NULL );
82 if( ! canonical_input )
83 {
84 reexported = mbedtls_calloc( 1, export_size );
85 TEST_ASSERT( reexported != NULL );
86 }
87 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
88
mohammad1603a97cb8c2018-03-28 03:46:26 -070089 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +020090 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -070091 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
92
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010093 /* Import the key */
94 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +020095 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010096
97 /* Test the key information */
98 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +020099 &got_type,
100 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100101 TEST_ASSERT( got_type == type );
102 TEST_ASSERT( got_bits == (size_t) expected_bits );
103
104 /* Export the key */
105 status = psa_export_key( slot,
106 exported, export_size,
107 &exported_length );
108 TEST_ASSERT( status == (psa_status_t) expected_export_status );
109 if( status != PSA_SUCCESS )
110 goto destroy;
111
112 if( canonical_input )
113 {
Gilles Peskine4abf7412018-06-18 16:35:34 +0200114 TEST_ASSERT( exported_length == data->len );
115 TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100116 }
117 else
118 {
mohammad1603a97cb8c2018-03-28 03:46:26 -0700119 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
120
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100121 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200122 exported,
123 export_size ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100124 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200125 reexported,
126 export_size,
127 &reexported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100128 TEST_ASSERT( reexported_length == exported_length );
129 TEST_ASSERT( memcmp( reexported, exported,
130 exported_length ) == 0 );
131 }
132
133destroy:
134 /* Destroy the key */
135 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
136 TEST_ASSERT( psa_get_key_information(
137 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
138
139exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300140 mbedtls_free( exported );
141 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100142 mbedtls_psa_crypto_free( );
143}
144/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100145
Moran Pekerf709f4a2018-06-06 17:26:04 +0300146/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300147void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200148 int type_arg,
149 int alg_arg,
150 int expected_bits,
151 int public_key_expected_length,
152 int expected_export_status )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300153{
154 int slot = 1;
155 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200156 psa_algorithm_t alg = alg_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300157 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300158 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300159 size_t export_size;
160 size_t exported_length;
161 psa_key_type_t got_type;
162 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200163 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300164
Moran Pekerf709f4a2018-06-06 17:26:04 +0300165 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300166 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
167 export_size = (ssize_t) data->len;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300168 exported = mbedtls_calloc( 1, export_size );
169 TEST_ASSERT( exported != NULL );
170
171 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
172
173 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200174 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300175 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
176
177 /* Import the key */
178 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200179 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300180
181 /* Test the key information */
182 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200183 &got_type,
184 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300185 TEST_ASSERT( got_type == type );
186 TEST_ASSERT( got_bits == (size_t) expected_bits );
187
188 /* Export the key */
189 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200190 exported, export_size,
191 &exported_length );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300192 TEST_ASSERT( status == (psa_status_t) expected_export_status );
193 if( status != PSA_SUCCESS )
194 goto destroy;
195
196 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
197
198destroy:
199 /* Destroy the key */
200 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
201 TEST_ASSERT( psa_get_key_information(
202 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
203
204exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300205 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300206 mbedtls_psa_crypto_free( );
207}
208/* END_CASE */
209
Gilles Peskine20035e32018-02-03 22:44:14 +0100210/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +0200211void key_policy( int usage_arg, int alg_arg )
212{
213 int key_slot = 1;
214 psa_algorithm_t alg = alg_arg;
215 psa_key_usage_t usage = usage_arg;
216 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
217 unsigned char key[32] = {0};
218 psa_key_policy_t policy_set;
219 psa_key_policy_t policy_get;
220
221 memset( key, 0x2a, sizeof( key ) );
222
223 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
224
225 psa_key_policy_init( &policy_set );
226 psa_key_policy_init( &policy_get );
227
228 psa_key_policy_set_usage( &policy_set, usage, alg );
229
230 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
231 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
232 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
233
234 TEST_ASSERT( psa_import_key( key_slot, key_type,
235 key, sizeof( key ) ) == PSA_SUCCESS );
236
237 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
238
239 TEST_ASSERT( policy_get.usage == policy_set.usage );
240 TEST_ASSERT( policy_get.alg == policy_set.alg );
241
242exit:
243 psa_destroy_key( key_slot );
244 mbedtls_psa_crypto_free( );
245}
246/* END_CASE */
247
248/* BEGIN_CASE */
249void key_policy_fail( int usage_arg, int alg_arg, int expected_status,
250 data_t *keypair )
251{
252 int key_slot = 1;
253 psa_algorithm_t alg = alg_arg;
254 psa_key_usage_t usage = usage_arg;
255 size_t signature_length = 0;
256 psa_key_policy_t policy;
257 int actual_status = PSA_SUCCESS;
258
259 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
260
261 psa_key_policy_init( &policy );
262 psa_key_policy_set_usage( &policy, usage, alg );
263 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
264
265 if( usage & PSA_KEY_USAGE_EXPORT )
266 {
267 TEST_ASSERT( keypair != NULL );
268 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) );
269 TEST_ASSERT( psa_import_key( key_slot,
270 PSA_KEY_TYPE_RSA_KEYPAIR,
271 keypair->x,
272 keypair->len ) == PSA_SUCCESS );
273 actual_status = psa_asymmetric_sign( key_slot, alg,
274 NULL, 0,
275 NULL, 0,
276 NULL, 0, &signature_length );
277 }
278
279 if( usage & PSA_KEY_USAGE_SIGN )
280 {
281 TEST_ASSERT( keypair != NULL );
282 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) );
283 TEST_ASSERT( psa_import_key( key_slot,
284 PSA_KEY_TYPE_RSA_KEYPAIR,
285 keypair->x,
286 keypair->len ) == PSA_SUCCESS );
287 actual_status = psa_export_key( key_slot, NULL, 0, NULL );
288 }
289
290 TEST_ASSERT( actual_status == expected_status );
291
292exit:
293 psa_destroy_key( key_slot );
294 mbedtls_psa_crypto_free( );
295}
296/* END_CASE */
297
298/* BEGIN_CASE */
299void key_lifetime( int lifetime_arg )
300{
301 int key_slot = 1;
302 psa_key_type_t key_type = PSA_ALG_CBC_BASE;
303 unsigned char key[32] = {0};
304 psa_key_lifetime_t lifetime_set = lifetime_arg;
305 psa_key_lifetime_t lifetime_get;
306
307 memset( key, 0x2a, sizeof( key ) );
308
309 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
310
311 TEST_ASSERT( psa_set_key_lifetime( key_slot,
312 lifetime_set ) == PSA_SUCCESS );
313
314 TEST_ASSERT( psa_import_key( key_slot, key_type,
315 key, sizeof( key ) ) == PSA_SUCCESS );
316
317 TEST_ASSERT( psa_get_key_lifetime( key_slot,
318 &lifetime_get ) == PSA_SUCCESS );
319
320 TEST_ASSERT( lifetime_get == lifetime_set );
321
322exit:
323 psa_destroy_key( key_slot );
324 mbedtls_psa_crypto_free( );
325}
326/* END_CASE */
327
328/* BEGIN_CASE */
329void key_lifetime_set_fail( int key_slot_arg,
330 int lifetime_arg,
331 int expected_status_arg )
332{
333 psa_key_slot_t key_slot = key_slot_arg;
334 psa_key_lifetime_t lifetime_set = lifetime_arg;
335 psa_status_t actual_status;
336 psa_status_t expected_status = expected_status_arg;
337
338 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
339
340 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
341
342 if( actual_status == PSA_SUCCESS )
343 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
344
345 TEST_ASSERT( expected_status == actual_status );
346
347exit:
348 psa_destroy_key( key_slot );
349 mbedtls_psa_crypto_free( );
350}
351/* END_CASE */
352
353/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300354void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100355{
356 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +0200357 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100358 size_t actual_hash_length;
359 psa_hash_operation_t operation;
360
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100361 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300362 TEST_ASSERT( expected_hash != NULL );
363 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
364 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100365
366 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
367
368 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
369 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200370 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100371 TEST_ASSERT( psa_hash_finish( &operation,
372 actual_hash, sizeof( actual_hash ),
373 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200374 TEST_ASSERT( actual_hash_length == expected_hash->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300375 TEST_ASSERT( memcmp( expected_hash->x, actual_hash,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200376 expected_hash->len ) == 0 );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100377
378exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100379 mbedtls_psa_crypto_free( );
380}
381/* END_CASE */
382
383/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300384void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100385{
386 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100387 psa_hash_operation_t operation;
388
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100389 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300390 TEST_ASSERT( expected_hash != NULL );
391 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
392 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100393
394 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
395
396 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
397 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200398 input->x,
399 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100400 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300401 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200402 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100403
404exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100405 mbedtls_psa_crypto_free( );
406}
407/* END_CASE */
408
409/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +0200410void mac_verify( int key_type_arg,
411 data_t *key,
412 int alg_arg,
413 data_t *input,
414 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +0100415{
416 int key_slot = 1;
417 psa_key_type_t key_type = key_type_arg;
418 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100419 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -0700420 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100421
Gilles Peskine8c9def32018-02-08 10:02:12 +0100422 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100423 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100424 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300425 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300426 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
427 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100428
429 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
430
mohammad16036df908f2018-04-02 08:34:15 -0700431 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200432 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -0700433 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
434
Gilles Peskine8c9def32018-02-08 10:02:12 +0100435 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200436 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +0200437
Gilles Peskine8c9def32018-02-08 10:02:12 +0100438 TEST_ASSERT( psa_mac_start( &operation, key_slot, alg ) == PSA_SUCCESS );
439 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
440 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200441 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100442 TEST_ASSERT( psa_mac_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300443 expected_mac->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200444 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100445
446exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +0100447 psa_destroy_key( key_slot );
448 mbedtls_psa_crypto_free( );
449}
450/* END_CASE */
451
452/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +0200453void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300454 data_t *key,
455 data_t *input, data_t *expected_output,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200456 int expected_status )
457{
458 int key_slot = 1;
459 psa_status_t status;
460 psa_key_type_t key_type = key_type_arg;
461 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200462 unsigned char iv[16] = {0};
itayzafrir3e02b3b2018-06-12 17:06:52 +0300463 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200464 size_t output_buffer_size = 0;
465 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200466 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200467 psa_cipher_operation_t operation;
468
Gilles Peskine50e586b2018-06-08 14:28:46 +0200469 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200470 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200471 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300472 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
473 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
474 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200475
476 memset( iv, 0x2a, sizeof( iv ) );
477
478 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
479
480 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200481 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200482
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200483 TEST_ASSERT( psa_encrypt_setup( &operation,
484 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200485
486 TEST_ASSERT( psa_encrypt_set_iv( &operation,
487 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200488 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200489 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300490 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200491
Gilles Peskine4abf7412018-06-18 16:35:34 +0200492 TEST_ASSERT( psa_cipher_update( &operation,
493 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200494 output, output_buffer_size,
495 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200496 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200497 status = psa_cipher_finish( &operation,
498 output + function_output_length,
499 output_buffer_size,
500 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200501 total_output_length += function_output_length;
502
Gilles Peskine50e586b2018-06-08 14:28:46 +0200503 TEST_ASSERT( status == (psa_status_t) expected_status );
504 if( expected_status == PSA_SUCCESS )
505 {
506 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200507 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300508 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200509 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200510 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200511
Gilles Peskine50e586b2018-06-08 14:28:46 +0200512exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300513 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200514 psa_destroy_key( key_slot );
515 mbedtls_psa_crypto_free( );
516}
517/* END_CASE */
518
519/* BEGIN_CASE */
520void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300521 data_t *key,
522 data_t *input,
523 int first_part_size,
524 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +0200525{
526 int key_slot = 1;
527 psa_key_type_t key_type = key_type_arg;
528 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200529 unsigned char iv[16] = {0};
itayzafrir3e02b3b2018-06-12 17:06:52 +0300530 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200531 size_t output_buffer_size = 0;
532 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200533 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200534 psa_cipher_operation_t operation;
535
Gilles Peskine50e586b2018-06-08 14:28:46 +0200536 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200537 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200538 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300539 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
540 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
541 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200542
543 memset( iv, 0x2a, sizeof( iv ) );
544
545 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
546
547 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200548 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200549
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200550 TEST_ASSERT( psa_encrypt_setup( &operation,
551 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200552
553 TEST_ASSERT( psa_encrypt_set_iv( &operation,
554 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200555 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200556 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300557 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200558
Gilles Peskine4abf7412018-06-18 16:35:34 +0200559 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300560 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200561 output, output_buffer_size,
562 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200563 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200564 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300565 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200566 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200567 output, output_buffer_size,
568 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200569 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200570 TEST_ASSERT( psa_cipher_finish( &operation,
571 output + function_output_length,
572 output_buffer_size,
573 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200574 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200575 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
576
Gilles Peskine4abf7412018-06-18 16:35:34 +0200577 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300578 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200579 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200580
581exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300582 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200583 psa_destroy_key( key_slot );
584 mbedtls_psa_crypto_free( );
585}
586/* END_CASE */
587
588/* BEGIN_CASE */
589void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300590 data_t *key,
591 data_t *input,
592 int first_part_size,
593 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +0200594{
595 int key_slot = 1;
596
597 psa_key_type_t key_type = key_type_arg;
598 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200599 unsigned char iv[16] = {0};
itayzafrir3e02b3b2018-06-12 17:06:52 +0300600 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200601 size_t output_buffer_size = 0;
602 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200603 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200604 psa_cipher_operation_t operation;
605
Gilles Peskine50e586b2018-06-08 14:28:46 +0200606 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200607 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200608 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300609 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
610 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
611 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200612
613 memset( iv, 0x2a, sizeof( iv ) );
614
615 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
616
617 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200618 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200619
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200620 TEST_ASSERT( psa_decrypt_setup( &operation,
621 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200622
623 TEST_ASSERT( psa_encrypt_set_iv( &operation,
624 iv, sizeof( iv ) ) == PSA_SUCCESS );
625
Gilles Peskine4abf7412018-06-18 16:35:34 +0200626 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200627 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300628 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200629
Gilles Peskine4abf7412018-06-18 16:35:34 +0200630 TEST_ASSERT( (unsigned int) first_part_size < input->len );
631 TEST_ASSERT( psa_cipher_update( &operation,
632 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200633 output, output_buffer_size,
634 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200635 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200636 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300637 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200638 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200639 output, output_buffer_size,
640 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200641 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200642 TEST_ASSERT( psa_cipher_finish( &operation,
643 output + function_output_length,
644 output_buffer_size,
645 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200646 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200647 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
648
Gilles Peskine4abf7412018-06-18 16:35:34 +0200649 TEST_ASSERT( total_output_length == expected_output->len );
Gilles Peskine2d277862018-06-18 15:41:12 +0200650 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200651 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200652
653exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300654 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200655 psa_destroy_key( key_slot );
656 mbedtls_psa_crypto_free( );
657}
658/* END_CASE */
659
Gilles Peskine50e586b2018-06-08 14:28:46 +0200660/* BEGIN_CASE */
661void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300662 data_t *key,
663 data_t *input, data_t *expected_output,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200664 int expected_status )
665{
666 int key_slot = 1;
667 psa_status_t status;
668 psa_key_type_t key_type = key_type_arg;
669 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200670 unsigned char iv[16] = {0};
itayzafrir3e02b3b2018-06-12 17:06:52 +0300671 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200672 size_t output_buffer_size = 0;
673 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200674 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200675 psa_cipher_operation_t operation;
676
Gilles Peskine50e586b2018-06-08 14:28:46 +0200677 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200678 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200679 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300680 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
681 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
682 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200683
684 memset( iv, 0x2a, sizeof( iv ) );
685
686 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
687
688 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200689 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200690
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200691 TEST_ASSERT( psa_decrypt_setup( &operation,
692 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200693
694 TEST_ASSERT( psa_encrypt_set_iv( &operation,
695 iv, sizeof( iv ) ) == PSA_SUCCESS );
696
Gilles Peskine4abf7412018-06-18 16:35:34 +0200697 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200698 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300699 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200700
Gilles Peskine4abf7412018-06-18 16:35:34 +0200701 TEST_ASSERT( psa_cipher_update( &operation,
702 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200703 output, output_buffer_size,
704 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200705 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200706 status = psa_cipher_finish( &operation,
707 output + function_output_length,
708 output_buffer_size,
709 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200710 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200711 TEST_ASSERT( status == (psa_status_t) expected_status );
712
713 if( expected_status == PSA_SUCCESS )
714 {
715 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200716 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300717 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200718 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200719 }
720
Gilles Peskine50e586b2018-06-08 14:28:46 +0200721exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300722 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200723 psa_destroy_key( key_slot );
724 mbedtls_psa_crypto_free( );
725}
726/* END_CASE */
727
Gilles Peskine50e586b2018-06-08 14:28:46 +0200728/* BEGIN_CASE */
729void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300730 data_t *key,
731 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +0200732{
733 int key_slot = 1;
734 psa_key_type_t key_type = key_type_arg;
735 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -0700736 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +0200737 size_t iv_size = 16;
738 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300739 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200740 size_t output1_size = 0;
741 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300742 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200743 size_t output2_size = 0;
744 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +0200745 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200746 psa_cipher_operation_t operation1;
747 psa_cipher_operation_t operation2;
748
mohammad1603d7d7ba52018-03-12 18:51:53 +0200749 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200750 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300751 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
752 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200753
mohammad1603d7d7ba52018-03-12 18:51:53 +0200754 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
755
756 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200757 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200758
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200759 TEST_ASSERT( psa_encrypt_setup( &operation1,
760 key_slot, alg ) == PSA_SUCCESS );
761 TEST_ASSERT( psa_decrypt_setup( &operation2,
762 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200763
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200764 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
765 iv, iv_size,
766 &iv_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200767 output1_size = input->len + operation1.block_size;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200768 output1 = mbedtls_calloc( 1, output1_size );
769 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +0300770
Gilles Peskine4abf7412018-06-18 16:35:34 +0200771 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200772 output1, output1_size,
773 &output1_length ) == PSA_SUCCESS );
774 TEST_ASSERT( psa_cipher_finish( &operation1,
775 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200776 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200777
Gilles Peskine048b7f02018-06-08 14:20:49 +0200778 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300779
780 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
781
782 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200783 output2 = mbedtls_calloc( 1, output2_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300784 TEST_ASSERT( output2 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +0300785
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200786 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
787 iv, iv_length ) == PSA_SUCCESS );
788 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
789 output2, output2_size,
790 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +0200791 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200792 TEST_ASSERT( psa_cipher_finish( &operation2,
793 output2 + output2_length,
794 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200795 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300796
Gilles Peskine048b7f02018-06-08 14:20:49 +0200797 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200798
Moran Pekerded84402018-06-06 16:36:50 +0300799 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
800
Gilles Peskine4abf7412018-06-18 16:35:34 +0200801 TEST_ASSERT( input->len == output2_length );
802 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +0300803
804exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300805 mbedtls_free( output1 );
806 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +0300807 psa_destroy_key( key_slot );
808 mbedtls_psa_crypto_free( );
809}
810/* END_CASE */
811
812/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +0200813void cipher_verify_output_multipart( int alg_arg,
814 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300815 data_t *key,
816 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200817 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +0300818{
819 int key_slot = 1;
820 psa_key_type_t key_type = key_type_arg;
821 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +0300822 unsigned char iv[16] = {0};
823 size_t iv_size = 16;
824 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300825 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +0200826 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +0300827 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300828 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +0200829 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +0300830 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +0200831 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300832 psa_cipher_operation_t operation1;
833 psa_cipher_operation_t operation2;
834
Moran Pekerded84402018-06-06 16:36:50 +0300835 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +0300836 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300837 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
838 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200839
Moran Pekerded84402018-06-06 16:36:50 +0300840 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
841
842 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200843 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300844
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200845 TEST_ASSERT( psa_encrypt_setup( &operation1,
846 key_slot, alg ) == PSA_SUCCESS );
847 TEST_ASSERT( psa_decrypt_setup( &operation2,
848 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300849
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200850 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
851 iv, iv_size,
852 &iv_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200853 output1_buffer_size = input->len + operation1.block_size;
Gilles Peskine048b7f02018-06-08 14:20:49 +0200854 output1 = mbedtls_calloc( 1, output1_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300855 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +0300856
Gilles Peskine4abf7412018-06-18 16:35:34 +0200857 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200858
itayzafrir3e02b3b2018-06-12 17:06:52 +0300859 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200860 output1, output1_buffer_size,
861 &function_output_length ) == PSA_SUCCESS );
862 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300863
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200864 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300865 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200866 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200867 output1, output1_buffer_size,
868 &function_output_length ) == PSA_SUCCESS );
869 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300870
Gilles Peskine048b7f02018-06-08 14:20:49 +0200871 TEST_ASSERT( psa_cipher_finish( &operation1,
872 output1 + output1_length,
873 output1_buffer_size - output1_length,
874 &function_output_length ) == PSA_SUCCESS );
875 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +0200876
877 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
878
Gilles Peskine048b7f02018-06-08 14:20:49 +0200879 output2_buffer_size = output1_length;
880 output2 = mbedtls_calloc( 1, output2_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300881 TEST_ASSERT( output2 != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200882
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200883 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
884 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +0300885
886 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200887 output2, output2_buffer_size,
888 &function_output_length ) == PSA_SUCCESS );
889 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300890
Gilles Peskine048b7f02018-06-08 14:20:49 +0200891 TEST_ASSERT( psa_cipher_update( &operation2,
892 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200893 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200894 output2, output2_buffer_size,
895 &function_output_length ) == PSA_SUCCESS );
896 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +0300897
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200898 TEST_ASSERT( psa_cipher_finish( &operation2,
899 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +0200900 output2_buffer_size - output2_length,
901 &function_output_length ) == PSA_SUCCESS );
902 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +0200903
mohammad1603d7d7ba52018-03-12 18:51:53 +0200904 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
905
Gilles Peskine4abf7412018-06-18 16:35:34 +0200906 TEST_ASSERT( input->len == output2_length );
907 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200908
909exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300910 mbedtls_free( output1 );
911 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +0200912 psa_destroy_key( key_slot );
913 mbedtls_psa_crypto_free( );
914}
915/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +0200916
Gilles Peskine20035e32018-02-03 22:44:14 +0100917/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200918void aead_encrypt_decrypt( int key_type_arg,
919 data_t * key_data,
920 int alg_arg,
921 data_t * input_data,
922 data_t * nonce,
923 data_t * additional_data,
924 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +0200925{
926 int slot = 1;
927 psa_key_type_t key_type = key_type_arg;
928 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +0200929 unsigned char *output_data = NULL;
930 size_t output_size = 0;
931 size_t output_length = 0;
932 unsigned char *output_data2 = NULL;
933 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +0200934 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200935 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +0200936 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +0200937
Gilles Peskinea1cac842018-06-11 19:33:02 +0200938 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +0200939 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300940 TEST_ASSERT( nonce != NULL );
941 TEST_ASSERT( additional_data != NULL );
942 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
943 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
944 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
945 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
946
Gilles Peskine4abf7412018-06-18 16:35:34 +0200947 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +0200948 output_data = mbedtls_calloc( 1, output_size );
949 TEST_ASSERT( output_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +0200950
951 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
952
953 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200954 psa_key_policy_set_usage( &policy,
955 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
956 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +0200957 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
958
959 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200960 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +0200961
962 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200963 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +0200964 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200965 additional_data->len,
966 input_data->x, input_data->len,
967 output_data, output_size,
968 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +0200969
970 if( PSA_SUCCESS == expected_result )
971 {
972 output_data2 = mbedtls_calloc( 1, output_length );
973 TEST_ASSERT( output_data2 != NULL );
974
975 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200976 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +0200977 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200978 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200979 output_data, output_length,
980 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200981 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +0200982
itayzafrir3e02b3b2018-06-12 17:06:52 +0300983 TEST_ASSERT( memcmp( input_data->x, output_data2,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200984 input_data->len ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +0200985 }
Gilles Peskine2d277862018-06-18 15:41:12 +0200986
Gilles Peskinea1cac842018-06-11 19:33:02 +0200987exit:
988 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +0200989 mbedtls_free( output_data );
990 mbedtls_free( output_data2 );
991 mbedtls_psa_crypto_free( );
992}
993/* END_CASE */
994
995/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300996void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200997 int alg_arg, data_t * input_data,
998 data_t * additional_data, data_t * nonce,
999 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001000{
1001 int slot = 1;
1002 psa_key_type_t key_type = key_type_arg;
1003 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001004 unsigned char *output_data = NULL;
1005 size_t output_size = 0;
1006 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001007 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001008 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001009
Gilles Peskinea1cac842018-06-11 19:33:02 +02001010 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001011 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001012 TEST_ASSERT( additional_data != NULL );
1013 TEST_ASSERT( nonce != NULL );
1014 TEST_ASSERT( expected_result != NULL );
1015 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1016 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1017 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1018 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1019 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
1020
Gilles Peskine4abf7412018-06-18 16:35:34 +02001021 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001022 output_data = mbedtls_calloc( 1, output_size );
1023 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001024
Gilles Peskinea1cac842018-06-11 19:33:02 +02001025 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1026
1027 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001028 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001029 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1030
1031 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001032 key_data->x,
1033 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001034
1035 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001036 nonce->x, nonce->len,
1037 additional_data->x, additional_data->len,
1038 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001039 output_data, output_size,
1040 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001041
itayzafrir3e02b3b2018-06-12 17:06:52 +03001042 TEST_ASSERT( memcmp( output_data, expected_result->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001043 output_length ) == 0 );
1044
Gilles Peskinea1cac842018-06-11 19:33:02 +02001045exit:
1046 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001047 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001048 mbedtls_psa_crypto_free( );
1049}
1050/* END_CASE */
1051
1052/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001053void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001054 int alg_arg, data_t * input_data,
1055 data_t * additional_data, data_t * nonce,
1056 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001057{
1058 int slot = 1;
1059 psa_key_type_t key_type = key_type_arg;
1060 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001061 unsigned char *output_data = NULL;
1062 size_t output_size = 0;
1063 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001064 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001065 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001066 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001067
Gilles Peskinea1cac842018-06-11 19:33:02 +02001068 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001069 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001070 TEST_ASSERT( additional_data != NULL );
1071 TEST_ASSERT( nonce != NULL );
1072 TEST_ASSERT( expected_data != NULL );
1073 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1074 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1075 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1076 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1077 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
1078
Gilles Peskine4abf7412018-06-18 16:35:34 +02001079 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001080 output_data = mbedtls_calloc( 1, output_size );
1081 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001082
Gilles Peskinea1cac842018-06-11 19:33:02 +02001083 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1084
1085 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001086 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001087 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1088
1089 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001090 key_data->x,
1091 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001092
1093 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001094 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001095 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001096 additional_data->len,
1097 input_data->x, input_data->len,
1098 output_data, output_size,
1099 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001100
Gilles Peskine2d277862018-06-18 15:41:12 +02001101 if( expected_result == PSA_SUCCESS )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001102 {
itayzafrir3e02b3b2018-06-12 17:06:52 +03001103 TEST_ASSERT( memcmp( output_data, expected_data->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001104 output_length ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001105 }
1106
Gilles Peskinea1cac842018-06-11 19:33:02 +02001107exit:
1108 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001109 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001110 mbedtls_psa_crypto_free( );
1111}
1112/* END_CASE */
1113
1114/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001115void signature_size( int type_arg,
1116 int bits,
1117 int alg_arg,
1118 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001119{
1120 psa_key_type_t type = type_arg;
1121 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02001122 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01001123 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
1124exit:
1125 ;
1126}
1127/* END_CASE */
1128
1129/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001130void sign_deterministic( int key_type_arg, data_t *key_data,
1131 int alg_arg, data_t *input_data,
1132 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001133{
1134 int slot = 1;
1135 psa_key_type_t key_type = key_type_arg;
1136 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01001137 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01001138 unsigned char *signature = NULL;
1139 size_t signature_size;
1140 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02001141 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01001142
Gilles Peskine20035e32018-02-03 22:44:14 +01001143 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001144 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001145 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001146 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1147 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1148 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01001149
1150 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1151
mohammad1603a97cb8c2018-03-28 03:46:26 -07001152 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001153 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001154 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1155
Gilles Peskine20035e32018-02-03 22:44:14 +01001156 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001157 key_data->x,
1158 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01001159 TEST_ASSERT( psa_get_key_information( slot,
1160 NULL,
1161 &key_bits ) == PSA_SUCCESS );
1162
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001163 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
1164 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01001165 TEST_ASSERT( signature_size != 0 );
1166 signature = mbedtls_calloc( 1, signature_size );
1167 TEST_ASSERT( signature != NULL );
1168
1169 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001170 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01001171 NULL, 0,
1172 signature, signature_size,
1173 &signature_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001174 TEST_ASSERT( signature_length == output_data->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001175 TEST_ASSERT( memcmp( signature, output_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001176 output_data->len ) == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01001177
1178exit:
1179 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01001180 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01001181 mbedtls_psa_crypto_free( );
1182}
1183/* END_CASE */
1184
1185/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001186void sign_fail( int key_type_arg, data_t *key_data,
1187 int alg_arg, data_t *input_data,
Gilles Peskine20035e32018-02-03 22:44:14 +01001188 int signature_size, int expected_status_arg )
1189{
1190 int slot = 1;
1191 psa_key_type_t key_type = key_type_arg;
1192 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01001193 psa_status_t actual_status;
1194 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01001195 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01001196 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02001197 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01001198
Gilles Peskine20035e32018-02-03 22:44:14 +01001199 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001200 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001201 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1202 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1203
Gilles Peskine20035e32018-02-03 22:44:14 +01001204 signature = mbedtls_calloc( 1, signature_size );
1205 TEST_ASSERT( signature != NULL );
1206
1207 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1208
mohammad1603a97cb8c2018-03-28 03:46:26 -07001209 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001210 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001211 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1212
Gilles Peskine20035e32018-02-03 22:44:14 +01001213 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001214 key_data->x,
1215 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01001216
1217 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001218 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01001219 NULL, 0,
1220 signature, signature_size,
1221 &signature_length );
1222 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine93aa0332018-02-03 23:58:03 +01001223 TEST_ASSERT( signature_length == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01001224
1225exit:
1226 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01001227 mbedtls_free( signature );
1228 mbedtls_psa_crypto_free( );
1229}
1230/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03001231
1232/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001233void asymmetric_verify( int key_type_arg, data_t *key_data,
1234 int alg_arg, data_t *hash_data,
1235 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03001236{
1237 int slot = 1;
1238 psa_key_type_t key_type = key_type_arg;
1239 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001240 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03001241
itayzafrir5c753392018-05-08 11:18:38 +03001242 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03001243 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03001244 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001245 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1246 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
1247 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03001248
1249 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1250
1251 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001252 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03001253 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1254
1255 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001256 key_data->x,
1257 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03001258
1259 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001260 hash_data->x, hash_data->len,
itayzafrir5c753392018-05-08 11:18:38 +03001261 NULL, 0,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001262 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001263 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03001264exit:
1265 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03001266 mbedtls_psa_crypto_free( );
1267}
1268/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001269
1270/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001271void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
1272 int alg_arg, data_t *hash_data,
1273 data_t *signature_data,
1274 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001275{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001276 int slot = 1;
1277 psa_key_type_t key_type = key_type_arg;
1278 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001279 psa_status_t actual_status;
1280 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001281 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001282
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001283 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001284 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001285 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001286 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1287 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
1288 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001289
1290 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1291
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001292 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001293 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001294 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1295
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001296 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001297 key_data->x,
1298 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001299
1300 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001301 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001302 NULL, 0,
1303 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001304 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001305
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001306 TEST_ASSERT( actual_status == expected_status );
1307
1308exit:
1309 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001310 mbedtls_psa_crypto_free( );
1311}
1312/* END_CASE */
1313
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001314/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001315void asymmetric_encrypt_decrypt( int key_type_arg, data_t *key_data,
1316 int alg_arg, data_t *input_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001317{
1318 int slot = 1;
1319 psa_key_type_t key_type = key_type_arg;
1320 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001321 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001322 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001323 size_t output_length = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001324 unsigned char *output2 = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001325 size_t output2_size = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001326 size_t output2_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02001327 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001328
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001329 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001330 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001331 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1332 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1333
Gilles Peskine4abf7412018-06-18 16:35:34 +02001334 output_size = key_data->len;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001335 output2_size = output_size;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001336 output = mbedtls_calloc( 1, output_size );
1337 TEST_ASSERT( output != NULL );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001338 output2 = mbedtls_calloc( 1, output2_size );
1339 TEST_ASSERT( output2 != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001340
1341 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1342
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001343 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001344 psa_key_policy_set_usage( &policy,
1345 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001346 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001347 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1348
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001349 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001350 key_data->x,
1351 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001352
Gilles Peskineeebd7382018-06-08 18:11:54 +02001353 /* We test encryption by checking that encrypt-then-decrypt gives back
1354 * the original plaintext because of the non-optional random
1355 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02001356 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001357 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001358 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001359 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001360 &output_length ) == PSA_SUCCESS );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001361
Gilles Peskine2d277862018-06-18 15:41:12 +02001362 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001363 output, output_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001364 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001365 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001366 &output2_length ) == PSA_SUCCESS );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001367 TEST_ASSERT( memcmp( input_data->x, output2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001368 input_data->len ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001369
1370exit:
1371 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001372 mbedtls_free( output );
1373 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001374 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001375}
1376/* END_CASE */
1377
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001378/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001379void asymmetric_encrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001380 int alg_arg, data_t *input_data,
1381 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001382{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001383 int slot = 1;
1384 psa_key_type_t key_type = key_type_arg;
1385 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001386 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001387 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001388 size_t output_length = 0;
1389 psa_status_t actual_status;
1390 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001391 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001392
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001393 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001394 TEST_ASSERT( input_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( input_data->len ) );
1397
Gilles Peskine4abf7412018-06-18 16:35:34 +02001398 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001399 output = mbedtls_calloc( 1, output_size );
1400 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001401
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001402 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1403
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001404 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001405 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001406 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1407
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001408 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001409 key_data->x,
1410 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001411
Gilles Peskine2d277862018-06-18 15:41:12 +02001412 actual_status = psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001413 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001414 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001415 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001416 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001417 TEST_ASSERT( actual_status == expected_status );
1418
1419exit:
1420 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001421 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001422 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001423}
1424/* END_CASE */
1425
1426/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001427void asymmetric_decrypt( int key_type_arg, data_t *key_data,
1428 int alg_arg, data_t *input_data,
1429 data_t *expected_data, int expected_size )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001430{
1431 int slot = 1;
1432 psa_key_type_t key_type = key_type_arg;
1433 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001434 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001435 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001436 size_t output_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02001437 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001438
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001439 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001440 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001441 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001442 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1443 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1444 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
1445
Gilles Peskine4abf7412018-06-18 16:35:34 +02001446 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001447 output = mbedtls_calloc( 1, output_size );
1448 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001449
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001450 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1451
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001452 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001453 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001454 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1455
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001456 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001457 key_data->x,
1458 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001459
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001460 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001461 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001462 NULL, 0,
1463 output,
1464 output_size,
1465 &output_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001466 TEST_ASSERT( (size_t) expected_size == output_length );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001467 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001468
1469exit:
1470 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001471 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001472 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001473}
1474/* END_CASE */
1475
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001476/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001477void asymmetric_decrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001478 int alg_arg, data_t *input_data,
1479 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001480{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001481 int slot = 1;
1482 psa_key_type_t key_type = key_type_arg;
1483 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001484 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001485 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001486 size_t output_length = 0;
1487 psa_status_t actual_status;
1488 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001489 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001490
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001491 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001492 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001493 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1494 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1495
Gilles Peskine4abf7412018-06-18 16:35:34 +02001496 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001497 output = mbedtls_calloc( 1, output_size );
1498 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001499
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001500 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1501
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001502 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001503 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001504 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1505
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001506 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001507 key_data->x,
1508 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001509
Gilles Peskine2d277862018-06-18 15:41:12 +02001510 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001511 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001512 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001513 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001514 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001515 TEST_ASSERT( actual_status == expected_status );
1516
1517exit:
1518 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02001519 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001520 mbedtls_psa_crypto_free( );
1521}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001522/* END_CASE */