blob: 773163ba1a103aed99aed3c3bd9b61034edfb070 [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 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020016 * \return 1 if the buffer is all-bits-zero.
17 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020018 */
Gilles Peskine3f669c32018-06-21 09:21:51 +020019static int mem_is_zero( void *buffer, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020020{
21 size_t i;
22 for( i = 0; i < size; i++ )
23 {
24 if( ( (unsigned char *) buffer )[i] != 0 )
Gilles Peskine3f669c32018-06-21 09:21:51 +020025 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020026 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020027 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028}
Gilles Peskine818ca122018-06-20 18:16:48 +020029
30static int exercise_mac_key( psa_key_slot_t key,
31 psa_key_usage_t usage,
32 psa_algorithm_t alg )
33{
34 psa_mac_operation_t operation;
35 const unsigned char input[] = "foo";
36 unsigned char mac[64] = {0};
37 size_t mac_length = sizeof( mac );
38
39 if( usage & PSA_KEY_USAGE_SIGN )
40 {
41 TEST_ASSERT( psa_mac_start( &operation, key, alg ) == PSA_SUCCESS );
42 TEST_ASSERT( psa_mac_update( &operation,
43 input, sizeof( input ) ) == PSA_SUCCESS );
44 TEST_ASSERT( psa_mac_finish( &operation,
45 mac, sizeof( input ),
46 &mac_length ) == PSA_SUCCESS );
47 }
48
49 if( usage & PSA_KEY_USAGE_VERIFY )
50 {
51 psa_status_t verify_status =
52 ( usage & PSA_KEY_USAGE_SIGN ?
53 PSA_SUCCESS :
54 PSA_ERROR_INVALID_SIGNATURE );
55 TEST_ASSERT( psa_mac_start( &operation, key, alg ) == PSA_SUCCESS );
56 TEST_ASSERT( psa_mac_update( &operation,
57 input, sizeof( input ) ) == PSA_SUCCESS );
58 TEST_ASSERT( psa_mac_verify( &operation, mac, mac_length ) == verify_status );
59 }
60
61 return( 1 );
62
63exit:
64 psa_mac_abort( &operation );
65 return( 0 );
66}
67
68static int exercise_cipher_key( psa_key_slot_t key,
69 psa_key_usage_t usage,
70 psa_algorithm_t alg )
71{
72 psa_cipher_operation_t operation;
73 unsigned char iv[16] = {0};
74 size_t iv_length = sizeof( iv );
75 const unsigned char plaintext[16] = "Hello, world...";
76 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
77 size_t ciphertext_length = sizeof( ciphertext );
78 unsigned char decrypted[sizeof( ciphertext )];
79 size_t part_length;
80
81 if( usage & PSA_KEY_USAGE_ENCRYPT )
82 {
83 TEST_ASSERT( psa_encrypt_setup( &operation, key, alg ) == PSA_SUCCESS );
84 TEST_ASSERT( psa_encrypt_generate_iv( &operation,
85 iv, sizeof( iv ),
86 &iv_length ) == PSA_SUCCESS );
87 TEST_ASSERT( psa_cipher_update( &operation,
88 plaintext, sizeof( plaintext ),
89 ciphertext, sizeof( ciphertext ),
90 &ciphertext_length ) == PSA_SUCCESS );
91 TEST_ASSERT( psa_cipher_finish( &operation,
92 ciphertext + ciphertext_length,
93 sizeof( ciphertext ) - ciphertext_length,
94 &part_length ) == PSA_SUCCESS );
95 ciphertext_length += part_length;
96 }
97
98 if( usage & PSA_KEY_USAGE_DECRYPT )
99 {
100 psa_status_t status;
101 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
102 {
103 psa_key_type_t type;
104 size_t bits;
105 TEST_ASSERT( psa_get_key_information( key, &type, &bits ) );
106 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
107 }
108 TEST_ASSERT( psa_decrypt_setup( &operation, key, alg ) == PSA_SUCCESS );
109 TEST_ASSERT( psa_encrypt_set_iv( &operation,
110 iv, iv_length ) == PSA_SUCCESS );
111 TEST_ASSERT( psa_cipher_update( &operation,
112 ciphertext, ciphertext_length,
113 decrypted, sizeof( decrypted ),
114 &part_length ) == PSA_SUCCESS );
115 status = psa_cipher_finish( &operation,
116 decrypted + part_length,
117 sizeof( decrypted ) - part_length,
118 &part_length );
119 /* For a stream cipher, all inputs are valid. For a block cipher,
120 * if the input is some aribtrary data rather than an actual
121 ciphertext, a padding error is likely. */
122 if( ( usage & PSA_KEY_USAGE_DECRYPT ) ||
123 PSA_BLOCK_CIPHER_BLOCK_SIZE( alg ) == 1 )
124 TEST_ASSERT( status == PSA_SUCCESS );
125 else
126 TEST_ASSERT( status == PSA_SUCCESS ||
127 status == PSA_ERROR_INVALID_PADDING );
128 }
129
130 return( 1 );
131
132exit:
133 psa_cipher_abort( &operation );
134 return( 0 );
135}
136
137static int exercise_aead_key( psa_key_slot_t key,
138 psa_key_usage_t usage,
139 psa_algorithm_t alg )
140{
141 unsigned char nonce[16] = {0};
142 size_t nonce_length = sizeof( nonce );
143 unsigned char plaintext[16] = "Hello, world...";
144 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
145 size_t ciphertext_length = sizeof( ciphertext );
146 size_t plaintext_length = sizeof( ciphertext );
147
148 if( usage & PSA_KEY_USAGE_ENCRYPT )
149 {
150 TEST_ASSERT( psa_aead_encrypt( key, alg,
151 nonce, nonce_length,
152 NULL, 0,
153 plaintext, sizeof( plaintext ),
154 ciphertext, sizeof( ciphertext ),
155 &ciphertext_length ) == PSA_SUCCESS );
156 }
157
158 if( usage & PSA_KEY_USAGE_DECRYPT )
159 {
160 psa_status_t verify_status =
161 ( usage & PSA_KEY_USAGE_ENCRYPT ?
162 PSA_SUCCESS :
163 PSA_ERROR_INVALID_SIGNATURE );
164 TEST_ASSERT( psa_aead_decrypt( key, alg,
165 nonce, nonce_length,
166 NULL, 0,
167 ciphertext, ciphertext_length,
168 plaintext, sizeof( plaintext ),
169 &plaintext_length ) == verify_status );
170 }
171
172 return( 1 );
173
174exit:
175 return( 0 );
176}
177
178static int exercise_signature_key( psa_key_slot_t key,
179 psa_key_usage_t usage,
180 psa_algorithm_t alg )
181{
182 unsigned char payload[16] = {0};
183 size_t payload_length = sizeof( payload );
184 unsigned char signature[256] = {0};
185 size_t signature_length = sizeof( signature );
186
187 if( usage & PSA_KEY_USAGE_SIGN )
188 {
189 TEST_ASSERT( psa_asymmetric_sign( key, alg,
190 payload, payload_length,
191 NULL, 0,
192 signature, sizeof( signature ),
193 &signature_length ) == PSA_SUCCESS );
194 }
195
196 if( usage & PSA_KEY_USAGE_VERIFY )
197 {
198 psa_status_t verify_status =
199 ( usage & PSA_KEY_USAGE_SIGN ?
200 PSA_SUCCESS :
201 PSA_ERROR_INVALID_SIGNATURE );
202 TEST_ASSERT( psa_asymmetric_verify( key, alg,
203 payload, payload_length,
204 NULL, 0,
205 signature, signature_length ) ==
206 verify_status );
207 }
208
209 return( 1 );
210
211exit:
212 return( 0 );
213}
214
215static int exercise_asymmetric_encryption_key( psa_key_slot_t key,
216 psa_key_usage_t usage,
217 psa_algorithm_t alg )
218{
219 unsigned char plaintext[256] = "Hello, world...";
220 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
221 size_t ciphertext_length = sizeof( ciphertext );
222 size_t plaintext_length = 16;
223
224 if( usage & PSA_KEY_USAGE_ENCRYPT )
225 {
226 TEST_ASSERT(
227 psa_asymmetric_encrypt( key, alg,
228 plaintext, plaintext_length,
229 NULL, 0,
230 ciphertext, sizeof( ciphertext ),
231 &ciphertext_length ) == PSA_SUCCESS );
232 }
233
234 if( usage & PSA_KEY_USAGE_DECRYPT )
235 {
236 psa_status_t status =
237 psa_asymmetric_decrypt( key, alg,
238 ciphertext, ciphertext_length,
239 NULL, 0,
240 plaintext, sizeof( plaintext ),
241 &plaintext_length );
242 TEST_ASSERT( status == PSA_SUCCESS ||
243 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
244 ( status == PSA_ERROR_INVALID_ARGUMENT ||
245 status == PSA_ERROR_INVALID_PADDING ) ) );
246 }
247
248 return( 1 );
249
250exit:
251 return( 0 );
252}
Gilles Peskinee59236f2018-01-27 23:32:46 +0100253/* END_HEADER */
254
255/* BEGIN_DEPENDENCIES
256 * depends_on:MBEDTLS_PSA_CRYPTO_C
257 * END_DEPENDENCIES
258 */
259
260/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200261void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100262{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100263 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100264 int i;
265 for( i = 0; i <= 1; i++ )
266 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100267 status = psa_crypto_init( );
268 TEST_ASSERT( status == PSA_SUCCESS );
269 status = psa_crypto_init( );
270 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100271 mbedtls_psa_crypto_free( );
272 }
273}
274/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100275
276/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300277void import( data_t *data, int type, int expected_status )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100278{
279 int slot = 1;
280 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100281
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100282 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300283 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100284 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
285
Gilles Peskine4abf7412018-06-18 16:35:34 +0200286 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100287 TEST_ASSERT( status == (psa_status_t) expected_status );
288 if( status == PSA_SUCCESS )
289 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
290
291exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100292 mbedtls_psa_crypto_free( );
293}
294/* END_CASE */
295
296/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300297void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300298 int type_arg,
299 int alg_arg,
300 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100301 int expected_bits,
302 int export_size_delta,
303 int expected_export_status,
304 int canonical_input )
305{
306 int slot = 1;
307 int slot2 = slot + 1;
308 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200309 psa_algorithm_t alg = alg_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100310 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100311 unsigned char *exported = NULL;
312 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100313 size_t export_size;
314 size_t exported_length;
315 size_t reexported_length;
316 psa_key_type_t got_type;
317 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200318 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100319
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100320 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300321 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
322 export_size = (ssize_t) data->len + export_size_delta;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100323 exported = mbedtls_calloc( 1, export_size );
324 TEST_ASSERT( exported != NULL );
325 if( ! canonical_input )
326 {
327 reexported = mbedtls_calloc( 1, export_size );
328 TEST_ASSERT( reexported != NULL );
329 }
330 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
331
mohammad1603a97cb8c2018-03-28 03:46:26 -0700332 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200333 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700334 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
335
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100336 /* Import the key */
337 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200338 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100339
340 /* Test the key information */
341 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200342 &got_type,
343 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100344 TEST_ASSERT( got_type == type );
345 TEST_ASSERT( got_bits == (size_t) expected_bits );
346
347 /* Export the key */
348 status = psa_export_key( slot,
349 exported, export_size,
350 &exported_length );
351 TEST_ASSERT( status == (psa_status_t) expected_export_status );
Gilles Peskine3f669c32018-06-21 09:21:51 +0200352 TEST_ASSERT( mem_is_zero( exported + exported_length,
353 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100354 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200355 {
356 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100357 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200358 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100359
360 if( canonical_input )
361 {
Gilles Peskine4abf7412018-06-18 16:35:34 +0200362 TEST_ASSERT( exported_length == data->len );
363 TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100364 }
365 else
366 {
mohammad1603a97cb8c2018-03-28 03:46:26 -0700367 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
368
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100369 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200370 exported,
371 export_size ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100372 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200373 reexported,
374 export_size,
375 &reexported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100376 TEST_ASSERT( reexported_length == exported_length );
377 TEST_ASSERT( memcmp( reexported, exported,
378 exported_length ) == 0 );
379 }
380
381destroy:
382 /* Destroy the key */
383 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
384 TEST_ASSERT( psa_get_key_information(
385 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
386
387exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300388 mbedtls_free( exported );
389 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100390 mbedtls_psa_crypto_free( );
391}
392/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100393
Moran Pekerf709f4a2018-06-06 17:26:04 +0300394/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300395void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200396 int type_arg,
397 int alg_arg,
398 int expected_bits,
399 int public_key_expected_length,
400 int expected_export_status )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300401{
402 int slot = 1;
403 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200404 psa_algorithm_t alg = alg_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300405 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300406 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300407 size_t export_size;
408 size_t exported_length;
409 psa_key_type_t got_type;
410 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200411 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300412
Moran Pekerf709f4a2018-06-06 17:26:04 +0300413 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300414 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
415 export_size = (ssize_t) data->len;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300416 exported = mbedtls_calloc( 1, export_size );
417 TEST_ASSERT( exported != NULL );
418
419 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
420
421 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200422 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300423 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
424
425 /* Import the key */
426 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200427 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300428
429 /* Test the key information */
430 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200431 &got_type,
432 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300433 TEST_ASSERT( got_type == type );
434 TEST_ASSERT( got_bits == (size_t) expected_bits );
435
436 /* Export the key */
437 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200438 exported, export_size,
439 &exported_length );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300440 TEST_ASSERT( status == (psa_status_t) expected_export_status );
441 if( status != PSA_SUCCESS )
442 goto destroy;
443
444 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
445
446destroy:
447 /* Destroy the key */
448 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
449 TEST_ASSERT( psa_get_key_information(
450 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
451
452exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300453 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300454 mbedtls_psa_crypto_free( );
455}
456/* END_CASE */
457
Gilles Peskine20035e32018-02-03 22:44:14 +0100458/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +0200459void key_policy( int usage_arg, int alg_arg )
460{
461 int key_slot = 1;
462 psa_algorithm_t alg = alg_arg;
463 psa_key_usage_t usage = usage_arg;
464 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
465 unsigned char key[32] = {0};
466 psa_key_policy_t policy_set;
467 psa_key_policy_t policy_get;
468
469 memset( key, 0x2a, sizeof( key ) );
470
471 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
472
473 psa_key_policy_init( &policy_set );
474 psa_key_policy_init( &policy_get );
475
476 psa_key_policy_set_usage( &policy_set, usage, alg );
477
478 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
479 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
480 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
481
482 TEST_ASSERT( psa_import_key( key_slot, key_type,
483 key, sizeof( key ) ) == PSA_SUCCESS );
484
485 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
486
487 TEST_ASSERT( policy_get.usage == policy_set.usage );
488 TEST_ASSERT( policy_get.alg == policy_set.alg );
489
490exit:
491 psa_destroy_key( key_slot );
492 mbedtls_psa_crypto_free( );
493}
494/* END_CASE */
495
496/* BEGIN_CASE */
497void key_policy_fail( int usage_arg, int alg_arg, int expected_status,
498 data_t *keypair )
499{
500 int key_slot = 1;
501 psa_algorithm_t alg = alg_arg;
502 psa_key_usage_t usage = usage_arg;
503 size_t signature_length = 0;
504 psa_key_policy_t policy;
505 int actual_status = PSA_SUCCESS;
506
507 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
508
509 psa_key_policy_init( &policy );
510 psa_key_policy_set_usage( &policy, usage, alg );
511 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
512
513 if( usage & PSA_KEY_USAGE_EXPORT )
514 {
515 TEST_ASSERT( keypair != NULL );
516 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) );
517 TEST_ASSERT( psa_import_key( key_slot,
518 PSA_KEY_TYPE_RSA_KEYPAIR,
519 keypair->x,
520 keypair->len ) == PSA_SUCCESS );
521 actual_status = psa_asymmetric_sign( key_slot, alg,
522 NULL, 0,
523 NULL, 0,
524 NULL, 0, &signature_length );
525 }
526
527 if( usage & PSA_KEY_USAGE_SIGN )
528 {
529 TEST_ASSERT( keypair != NULL );
530 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) );
531 TEST_ASSERT( psa_import_key( key_slot,
532 PSA_KEY_TYPE_RSA_KEYPAIR,
533 keypair->x,
534 keypair->len ) == PSA_SUCCESS );
535 actual_status = psa_export_key( key_slot, NULL, 0, NULL );
536 }
537
538 TEST_ASSERT( actual_status == expected_status );
539
540exit:
541 psa_destroy_key( key_slot );
542 mbedtls_psa_crypto_free( );
543}
544/* END_CASE */
545
546/* BEGIN_CASE */
547void key_lifetime( int lifetime_arg )
548{
549 int key_slot = 1;
550 psa_key_type_t key_type = PSA_ALG_CBC_BASE;
551 unsigned char key[32] = {0};
552 psa_key_lifetime_t lifetime_set = lifetime_arg;
553 psa_key_lifetime_t lifetime_get;
554
555 memset( key, 0x2a, sizeof( key ) );
556
557 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
558
559 TEST_ASSERT( psa_set_key_lifetime( key_slot,
560 lifetime_set ) == PSA_SUCCESS );
561
562 TEST_ASSERT( psa_import_key( key_slot, key_type,
563 key, sizeof( key ) ) == PSA_SUCCESS );
564
565 TEST_ASSERT( psa_get_key_lifetime( key_slot,
566 &lifetime_get ) == PSA_SUCCESS );
567
568 TEST_ASSERT( lifetime_get == lifetime_set );
569
570exit:
571 psa_destroy_key( key_slot );
572 mbedtls_psa_crypto_free( );
573}
574/* END_CASE */
575
576/* BEGIN_CASE */
577void key_lifetime_set_fail( int key_slot_arg,
578 int lifetime_arg,
579 int expected_status_arg )
580{
581 psa_key_slot_t key_slot = key_slot_arg;
582 psa_key_lifetime_t lifetime_set = lifetime_arg;
583 psa_status_t actual_status;
584 psa_status_t expected_status = expected_status_arg;
585
586 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
587
588 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
589
590 if( actual_status == PSA_SUCCESS )
591 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
592
593 TEST_ASSERT( expected_status == actual_status );
594
595exit:
596 psa_destroy_key( key_slot );
597 mbedtls_psa_crypto_free( );
598}
599/* END_CASE */
600
601/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200602void hash_setup( int alg_arg,
603 int expected_status_arg )
604{
605 psa_algorithm_t alg = alg_arg;
606 psa_hash_operation_t operation;
607 psa_status_t status;
608
609 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
610
611 status = psa_hash_start( &operation, alg );
612 psa_hash_abort( &operation );
613 TEST_ASSERT( status == (psa_status_t) expected_status_arg );
614
615exit:
616 mbedtls_psa_crypto_free( );
617}
618/* END_CASE */
619
620/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300621void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100622{
623 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +0200624 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100625 size_t actual_hash_length;
626 psa_hash_operation_t operation;
627
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100628 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300629 TEST_ASSERT( expected_hash != NULL );
630 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
631 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100632
633 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
634
635 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
636 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200637 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100638 TEST_ASSERT( psa_hash_finish( &operation,
639 actual_hash, sizeof( actual_hash ),
640 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200641 TEST_ASSERT( actual_hash_length == expected_hash->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300642 TEST_ASSERT( memcmp( expected_hash->x, actual_hash,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200643 expected_hash->len ) == 0 );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100644
645exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100646 mbedtls_psa_crypto_free( );
647}
648/* END_CASE */
649
650/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300651void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100652{
653 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100654 psa_hash_operation_t operation;
655
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100656 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300657 TEST_ASSERT( expected_hash != NULL );
658 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
659 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100660
661 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
662
663 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
664 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200665 input->x,
666 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100667 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300668 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200669 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100670
671exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100672 mbedtls_psa_crypto_free( );
673}
674/* END_CASE */
675
676/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200677void mac_setup( int key_type_arg,
678 data_t *key,
679 int alg_arg,
680 int expected_status_arg )
681{
682 int key_slot = 1;
683 psa_key_type_t key_type = key_type_arg;
684 psa_algorithm_t alg = alg_arg;
685 psa_mac_operation_t operation;
686 psa_key_policy_t policy;
687 psa_status_t status;
688
689 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
690
691 psa_key_policy_init( &policy );
692 psa_key_policy_set_usage( &policy,
693 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
694 alg );
695 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
696
697 TEST_ASSERT( psa_import_key( key_slot, key_type,
698 key->x, key->len ) == PSA_SUCCESS );
699
700 status = psa_mac_start( &operation, key_slot, alg );
701 psa_mac_abort( &operation );
702 TEST_ASSERT( status == (psa_status_t) expected_status_arg );
703
704exit:
705 psa_destroy_key( key_slot );
706 mbedtls_psa_crypto_free( );
707}
708/* END_CASE */
709
710/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +0200711void mac_verify( int key_type_arg,
712 data_t *key,
713 int alg_arg,
714 data_t *input,
715 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +0100716{
717 int key_slot = 1;
718 psa_key_type_t key_type = key_type_arg;
719 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100720 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -0700721 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100722
Gilles Peskine8c9def32018-02-08 10:02:12 +0100723 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100724 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100725 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300726 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300727 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
728 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100729
730 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
731
mohammad16036df908f2018-04-02 08:34:15 -0700732 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200733 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -0700734 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
735
Gilles Peskine8c9def32018-02-08 10:02:12 +0100736 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200737 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +0200738
Gilles Peskine8c9def32018-02-08 10:02:12 +0100739 TEST_ASSERT( psa_mac_start( &operation, key_slot, alg ) == PSA_SUCCESS );
740 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
741 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200742 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100743 TEST_ASSERT( psa_mac_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300744 expected_mac->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200745 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100746
747exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +0100748 psa_destroy_key( key_slot );
749 mbedtls_psa_crypto_free( );
750}
751/* END_CASE */
752
753/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200754void cipher_setup( int key_type_arg,
755 data_t *key,
756 int alg_arg,
757 int expected_status_arg )
758{
759 int key_slot = 1;
760 psa_key_type_t key_type = key_type_arg;
761 psa_algorithm_t alg = alg_arg;
762 psa_cipher_operation_t operation;
763 psa_key_policy_t policy;
764 psa_status_t status;
765
766 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
767
768 psa_key_policy_init( &policy );
769 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
770 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
771
772 TEST_ASSERT( psa_import_key( key_slot, key_type,
773 key->x, key->len ) == PSA_SUCCESS );
774
775 status = psa_encrypt_setup( &operation, key_slot, alg );
776 psa_cipher_abort( &operation );
777 TEST_ASSERT( status == (psa_status_t) expected_status_arg );
778
779exit:
780 psa_destroy_key( key_slot );
781 mbedtls_psa_crypto_free( );
782}
783/* END_CASE */
784
785/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +0200786void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300787 data_t *key,
788 data_t *input, data_t *expected_output,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200789 int expected_status )
790{
791 int key_slot = 1;
792 psa_status_t status;
793 psa_key_type_t key_type = key_type_arg;
794 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200795 unsigned char iv[16] = {0};
itayzafrir3e02b3b2018-06-12 17:06:52 +0300796 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200797 size_t output_buffer_size = 0;
798 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200799 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200800 psa_cipher_operation_t operation;
801
Gilles Peskine50e586b2018-06-08 14:28:46 +0200802 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200803 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200804 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300805 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
806 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
807 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200808
809 memset( iv, 0x2a, sizeof( iv ) );
810
811 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
812
813 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200814 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200815
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200816 TEST_ASSERT( psa_encrypt_setup( &operation,
817 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200818
819 TEST_ASSERT( psa_encrypt_set_iv( &operation,
820 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200821 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200822 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300823 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200824
Gilles Peskine4abf7412018-06-18 16:35:34 +0200825 TEST_ASSERT( psa_cipher_update( &operation,
826 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200827 output, output_buffer_size,
828 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200829 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200830 status = psa_cipher_finish( &operation,
831 output + function_output_length,
832 output_buffer_size,
833 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200834 total_output_length += function_output_length;
835
Gilles Peskine50e586b2018-06-08 14:28:46 +0200836 TEST_ASSERT( status == (psa_status_t) expected_status );
837 if( expected_status == PSA_SUCCESS )
838 {
839 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200840 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300841 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200842 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200843 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200844
Gilles Peskine50e586b2018-06-08 14:28:46 +0200845exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300846 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200847 psa_destroy_key( key_slot );
848 mbedtls_psa_crypto_free( );
849}
850/* END_CASE */
851
852/* BEGIN_CASE */
853void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300854 data_t *key,
855 data_t *input,
856 int first_part_size,
857 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +0200858{
859 int key_slot = 1;
860 psa_key_type_t key_type = key_type_arg;
861 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200862 unsigned char iv[16] = {0};
itayzafrir3e02b3b2018-06-12 17:06:52 +0300863 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200864 size_t output_buffer_size = 0;
865 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200866 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200867 psa_cipher_operation_t operation;
868
Gilles Peskine50e586b2018-06-08 14:28:46 +0200869 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200870 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200871 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300872 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
873 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
874 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200875
876 memset( iv, 0x2a, sizeof( iv ) );
877
878 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
879
880 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200881 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200882
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200883 TEST_ASSERT( psa_encrypt_setup( &operation,
884 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200885
886 TEST_ASSERT( psa_encrypt_set_iv( &operation,
887 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200888 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200889 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300890 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200891
Gilles Peskine4abf7412018-06-18 16:35:34 +0200892 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300893 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200894 output, output_buffer_size,
895 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200896 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200897 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300898 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200899 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200900 output, output_buffer_size,
901 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200902 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200903 TEST_ASSERT( psa_cipher_finish( &operation,
904 output + function_output_length,
905 output_buffer_size,
906 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200907 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200908 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
909
Gilles Peskine4abf7412018-06-18 16:35:34 +0200910 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300911 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200912 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200913
914exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300915 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200916 psa_destroy_key( key_slot );
917 mbedtls_psa_crypto_free( );
918}
919/* END_CASE */
920
921/* BEGIN_CASE */
922void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300923 data_t *key,
924 data_t *input,
925 int first_part_size,
926 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +0200927{
928 int key_slot = 1;
929
930 psa_key_type_t key_type = key_type_arg;
931 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200932 unsigned char iv[16] = {0};
itayzafrir3e02b3b2018-06-12 17:06:52 +0300933 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200934 size_t output_buffer_size = 0;
935 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200936 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200937 psa_cipher_operation_t operation;
938
Gilles Peskine50e586b2018-06-08 14:28:46 +0200939 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200940 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200941 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300942 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
943 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
944 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200945
946 memset( iv, 0x2a, sizeof( iv ) );
947
948 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
949
950 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200951 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200952
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200953 TEST_ASSERT( psa_decrypt_setup( &operation,
954 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200955
956 TEST_ASSERT( psa_encrypt_set_iv( &operation,
957 iv, sizeof( iv ) ) == PSA_SUCCESS );
958
Gilles Peskine4abf7412018-06-18 16:35:34 +0200959 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200960 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300961 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200962
Gilles Peskine4abf7412018-06-18 16:35:34 +0200963 TEST_ASSERT( (unsigned int) first_part_size < input->len );
964 TEST_ASSERT( psa_cipher_update( &operation,
965 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200966 output, output_buffer_size,
967 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200968 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200969 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300970 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200971 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200972 output, output_buffer_size,
973 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200974 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200975 TEST_ASSERT( psa_cipher_finish( &operation,
976 output + function_output_length,
977 output_buffer_size,
978 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200979 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200980 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
981
Gilles Peskine4abf7412018-06-18 16:35:34 +0200982 TEST_ASSERT( total_output_length == expected_output->len );
Gilles Peskine2d277862018-06-18 15:41:12 +0200983 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200984 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200985
986exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300987 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200988 psa_destroy_key( key_slot );
989 mbedtls_psa_crypto_free( );
990}
991/* END_CASE */
992
Gilles Peskine50e586b2018-06-08 14:28:46 +0200993/* BEGIN_CASE */
994void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300995 data_t *key,
996 data_t *input, data_t *expected_output,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200997 int expected_status )
998{
999 int key_slot = 1;
1000 psa_status_t status;
1001 psa_key_type_t key_type = key_type_arg;
1002 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001003 unsigned char iv[16] = {0};
itayzafrir3e02b3b2018-06-12 17:06:52 +03001004 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001005 size_t output_buffer_size = 0;
1006 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001007 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001008 psa_cipher_operation_t operation;
1009
Gilles Peskine50e586b2018-06-08 14:28:46 +02001010 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001011 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001012 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001013 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1014 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1015 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001016
1017 memset( iv, 0x2a, sizeof( iv ) );
1018
1019 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1020
1021 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001022 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001023
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001024 TEST_ASSERT( psa_decrypt_setup( &operation,
1025 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001026
1027 TEST_ASSERT( psa_encrypt_set_iv( &operation,
1028 iv, sizeof( iv ) ) == PSA_SUCCESS );
1029
Gilles Peskine4abf7412018-06-18 16:35:34 +02001030 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001031 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001032 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001033
Gilles Peskine4abf7412018-06-18 16:35:34 +02001034 TEST_ASSERT( psa_cipher_update( &operation,
1035 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001036 output, output_buffer_size,
1037 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001038 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001039 status = psa_cipher_finish( &operation,
1040 output + function_output_length,
1041 output_buffer_size,
1042 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001043 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001044 TEST_ASSERT( status == (psa_status_t) expected_status );
1045
1046 if( expected_status == PSA_SUCCESS )
1047 {
1048 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001049 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001050 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001051 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001052 }
1053
Gilles Peskine50e586b2018-06-08 14:28:46 +02001054exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001055 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001056 psa_destroy_key( key_slot );
1057 mbedtls_psa_crypto_free( );
1058}
1059/* END_CASE */
1060
Gilles Peskine50e586b2018-06-08 14:28:46 +02001061/* BEGIN_CASE */
1062void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001063 data_t *key,
1064 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02001065{
1066 int key_slot = 1;
1067 psa_key_type_t key_type = key_type_arg;
1068 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07001069 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02001070 size_t iv_size = 16;
1071 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001072 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001073 size_t output1_size = 0;
1074 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001075 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001076 size_t output2_size = 0;
1077 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001078 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001079 psa_cipher_operation_t operation1;
1080 psa_cipher_operation_t operation2;
1081
mohammad1603d7d7ba52018-03-12 18:51:53 +02001082 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001083 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001084 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1085 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001086
mohammad1603d7d7ba52018-03-12 18:51:53 +02001087 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1088
1089 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001090 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001091
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001092 TEST_ASSERT( psa_encrypt_setup( &operation1,
1093 key_slot, alg ) == PSA_SUCCESS );
1094 TEST_ASSERT( psa_decrypt_setup( &operation2,
1095 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001096
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001097 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
1098 iv, iv_size,
1099 &iv_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001100 output1_size = input->len + operation1.block_size;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001101 output1 = mbedtls_calloc( 1, output1_size );
1102 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001103
Gilles Peskine4abf7412018-06-18 16:35:34 +02001104 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001105 output1, output1_size,
1106 &output1_length ) == PSA_SUCCESS );
1107 TEST_ASSERT( psa_cipher_finish( &operation1,
1108 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001109 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001110
Gilles Peskine048b7f02018-06-08 14:20:49 +02001111 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001112
1113 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1114
1115 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001116 output2 = mbedtls_calloc( 1, output2_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001117 TEST_ASSERT( output2 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001118
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001119 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
1120 iv, iv_length ) == PSA_SUCCESS );
1121 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
1122 output2, output2_size,
1123 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001124 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001125 TEST_ASSERT( psa_cipher_finish( &operation2,
1126 output2 + output2_length,
1127 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001128 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001129
Gilles Peskine048b7f02018-06-08 14:20:49 +02001130 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001131
Moran Pekerded84402018-06-06 16:36:50 +03001132 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1133
Gilles Peskine4abf7412018-06-18 16:35:34 +02001134 TEST_ASSERT( input->len == output2_length );
1135 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +03001136
1137exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001138 mbedtls_free( output1 );
1139 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03001140 psa_destroy_key( key_slot );
1141 mbedtls_psa_crypto_free( );
1142}
1143/* END_CASE */
1144
1145/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001146void cipher_verify_output_multipart( int alg_arg,
1147 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001148 data_t *key,
1149 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001150 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03001151{
1152 int key_slot = 1;
1153 psa_key_type_t key_type = key_type_arg;
1154 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03001155 unsigned char iv[16] = {0};
1156 size_t iv_size = 16;
1157 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001158 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001159 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001160 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001161 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001162 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001163 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001164 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001165 psa_cipher_operation_t operation1;
1166 psa_cipher_operation_t operation2;
1167
Moran Pekerded84402018-06-06 16:36:50 +03001168 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001169 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001170 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1171 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001172
Moran Pekerded84402018-06-06 16:36:50 +03001173 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1174
1175 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001176 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001177
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001178 TEST_ASSERT( psa_encrypt_setup( &operation1,
1179 key_slot, alg ) == PSA_SUCCESS );
1180 TEST_ASSERT( psa_decrypt_setup( &operation2,
1181 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001182
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001183 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
1184 iv, iv_size,
1185 &iv_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001186 output1_buffer_size = input->len + operation1.block_size;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001187 output1 = mbedtls_calloc( 1, output1_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001188 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001189
Gilles Peskine4abf7412018-06-18 16:35:34 +02001190 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001191
itayzafrir3e02b3b2018-06-12 17:06:52 +03001192 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001193 output1, output1_buffer_size,
1194 &function_output_length ) == PSA_SUCCESS );
1195 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001196
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001197 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001198 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001199 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001200 output1, output1_buffer_size,
1201 &function_output_length ) == PSA_SUCCESS );
1202 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001203
Gilles Peskine048b7f02018-06-08 14:20:49 +02001204 TEST_ASSERT( psa_cipher_finish( &operation1,
1205 output1 + output1_length,
1206 output1_buffer_size - output1_length,
1207 &function_output_length ) == PSA_SUCCESS );
1208 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001209
1210 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1211
Gilles Peskine048b7f02018-06-08 14:20:49 +02001212 output2_buffer_size = output1_length;
1213 output2 = mbedtls_calloc( 1, output2_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001214 TEST_ASSERT( output2 != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001215
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001216 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
1217 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001218
1219 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001220 output2, output2_buffer_size,
1221 &function_output_length ) == PSA_SUCCESS );
1222 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001223
Gilles Peskine048b7f02018-06-08 14:20:49 +02001224 TEST_ASSERT( psa_cipher_update( &operation2,
1225 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001226 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001227 output2, output2_buffer_size,
1228 &function_output_length ) == PSA_SUCCESS );
1229 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001230
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001231 TEST_ASSERT( psa_cipher_finish( &operation2,
1232 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001233 output2_buffer_size - output2_length,
1234 &function_output_length ) == PSA_SUCCESS );
1235 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001236
mohammad1603d7d7ba52018-03-12 18:51:53 +02001237 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1238
Gilles Peskine4abf7412018-06-18 16:35:34 +02001239 TEST_ASSERT( input->len == output2_length );
1240 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001241
1242exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001243 mbedtls_free( output1 );
1244 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001245 psa_destroy_key( key_slot );
1246 mbedtls_psa_crypto_free( );
1247}
1248/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02001249
Gilles Peskine20035e32018-02-03 22:44:14 +01001250/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001251void aead_encrypt_decrypt( int key_type_arg,
1252 data_t * key_data,
1253 int alg_arg,
1254 data_t * input_data,
1255 data_t * nonce,
1256 data_t * additional_data,
1257 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001258{
1259 int slot = 1;
1260 psa_key_type_t key_type = key_type_arg;
1261 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001262 unsigned char *output_data = NULL;
1263 size_t output_size = 0;
1264 size_t output_length = 0;
1265 unsigned char *output_data2 = NULL;
1266 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001267 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001268 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001269 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001270
Gilles Peskinea1cac842018-06-11 19:33:02 +02001271 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001272 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001273 TEST_ASSERT( nonce != NULL );
1274 TEST_ASSERT( additional_data != NULL );
1275 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1276 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1277 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1278 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1279
Gilles Peskine4abf7412018-06-18 16:35:34 +02001280 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001281 output_data = mbedtls_calloc( 1, output_size );
1282 TEST_ASSERT( output_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001283
1284 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1285
1286 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001287 psa_key_policy_set_usage( &policy,
1288 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
1289 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001290 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1291
1292 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001293 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001294
1295 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001296 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001297 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001298 additional_data->len,
1299 input_data->x, input_data->len,
1300 output_data, output_size,
1301 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001302
1303 if( PSA_SUCCESS == expected_result )
1304 {
1305 output_data2 = mbedtls_calloc( 1, output_length );
1306 TEST_ASSERT( output_data2 != NULL );
1307
1308 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001309 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001310 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001311 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001312 output_data, output_length,
1313 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001314 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02001315
itayzafrir3e02b3b2018-06-12 17:06:52 +03001316 TEST_ASSERT( memcmp( input_data->x, output_data2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001317 input_data->len ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001318 }
Gilles Peskine2d277862018-06-18 15:41:12 +02001319
Gilles Peskinea1cac842018-06-11 19:33:02 +02001320exit:
1321 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001322 mbedtls_free( output_data );
1323 mbedtls_free( output_data2 );
1324 mbedtls_psa_crypto_free( );
1325}
1326/* END_CASE */
1327
1328/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001329void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001330 int alg_arg, data_t * input_data,
1331 data_t * additional_data, data_t * nonce,
1332 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001333{
1334 int slot = 1;
1335 psa_key_type_t key_type = key_type_arg;
1336 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001337 unsigned char *output_data = NULL;
1338 size_t output_size = 0;
1339 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001340 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001341 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001342
Gilles Peskinea1cac842018-06-11 19:33:02 +02001343 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001344 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001345 TEST_ASSERT( additional_data != NULL );
1346 TEST_ASSERT( nonce != NULL );
1347 TEST_ASSERT( expected_result != NULL );
1348 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1349 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1350 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1351 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1352 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
1353
Gilles Peskine4abf7412018-06-18 16:35:34 +02001354 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001355 output_data = mbedtls_calloc( 1, output_size );
1356 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001357
Gilles Peskinea1cac842018-06-11 19:33:02 +02001358 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1359
1360 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001361 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001362 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1363
1364 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001365 key_data->x,
1366 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001367
1368 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001369 nonce->x, nonce->len,
1370 additional_data->x, additional_data->len,
1371 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001372 output_data, output_size,
1373 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001374
itayzafrir3e02b3b2018-06-12 17:06:52 +03001375 TEST_ASSERT( memcmp( output_data, expected_result->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001376 output_length ) == 0 );
1377
Gilles Peskinea1cac842018-06-11 19:33:02 +02001378exit:
1379 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001380 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001381 mbedtls_psa_crypto_free( );
1382}
1383/* END_CASE */
1384
1385/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001386void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001387 int alg_arg, data_t * input_data,
1388 data_t * additional_data, data_t * nonce,
1389 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001390{
1391 int slot = 1;
1392 psa_key_type_t key_type = key_type_arg;
1393 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001394 unsigned char *output_data = NULL;
1395 size_t output_size = 0;
1396 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001397 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001398 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001399 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001400
Gilles Peskinea1cac842018-06-11 19:33:02 +02001401 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001402 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001403 TEST_ASSERT( additional_data != NULL );
1404 TEST_ASSERT( nonce != NULL );
1405 TEST_ASSERT( expected_data != NULL );
1406 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1407 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1408 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1409 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1410 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
1411
Gilles Peskine4abf7412018-06-18 16:35:34 +02001412 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001413 output_data = mbedtls_calloc( 1, output_size );
1414 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001415
Gilles Peskinea1cac842018-06-11 19:33:02 +02001416 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1417
1418 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001419 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001420 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1421
1422 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001423 key_data->x,
1424 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001425
1426 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001427 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001428 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001429 additional_data->len,
1430 input_data->x, input_data->len,
1431 output_data, output_size,
1432 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001433
Gilles Peskine2d277862018-06-18 15:41:12 +02001434 if( expected_result == PSA_SUCCESS )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001435 {
itayzafrir3e02b3b2018-06-12 17:06:52 +03001436 TEST_ASSERT( memcmp( output_data, expected_data->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001437 output_length ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001438 }
1439
Gilles Peskinea1cac842018-06-11 19:33:02 +02001440exit:
1441 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001442 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001443 mbedtls_psa_crypto_free( );
1444}
1445/* END_CASE */
1446
1447/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001448void signature_size( int type_arg,
1449 int bits,
1450 int alg_arg,
1451 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001452{
1453 psa_key_type_t type = type_arg;
1454 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02001455 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01001456 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
1457exit:
1458 ;
1459}
1460/* END_CASE */
1461
1462/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001463void sign_deterministic( int key_type_arg, data_t *key_data,
1464 int alg_arg, data_t *input_data,
1465 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001466{
1467 int slot = 1;
1468 psa_key_type_t key_type = key_type_arg;
1469 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01001470 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01001471 unsigned char *signature = NULL;
1472 size_t signature_size;
1473 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02001474 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01001475
Gilles Peskine20035e32018-02-03 22:44:14 +01001476 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001477 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001478 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001479 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1480 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1481 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01001482
1483 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1484
mohammad1603a97cb8c2018-03-28 03:46:26 -07001485 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001486 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001487 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1488
Gilles Peskine20035e32018-02-03 22:44:14 +01001489 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001490 key_data->x,
1491 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01001492 TEST_ASSERT( psa_get_key_information( slot,
1493 NULL,
1494 &key_bits ) == PSA_SUCCESS );
1495
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001496 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
1497 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01001498 TEST_ASSERT( signature_size != 0 );
1499 signature = mbedtls_calloc( 1, signature_size );
1500 TEST_ASSERT( signature != NULL );
1501
1502 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001503 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01001504 NULL, 0,
1505 signature, signature_size,
1506 &signature_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001507 TEST_ASSERT( signature_length == output_data->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001508 TEST_ASSERT( memcmp( signature, output_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001509 output_data->len ) == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01001510
1511exit:
1512 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01001513 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01001514 mbedtls_psa_crypto_free( );
1515}
1516/* END_CASE */
1517
1518/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001519void sign_fail( int key_type_arg, data_t *key_data,
1520 int alg_arg, data_t *input_data,
Gilles Peskine20035e32018-02-03 22:44:14 +01001521 int signature_size, int expected_status_arg )
1522{
1523 int slot = 1;
1524 psa_key_type_t key_type = key_type_arg;
1525 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01001526 psa_status_t actual_status;
1527 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01001528 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01001529 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02001530 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01001531
Gilles Peskine20035e32018-02-03 22:44:14 +01001532 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001533 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001534 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1535 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1536
Gilles Peskine20035e32018-02-03 22:44:14 +01001537 signature = mbedtls_calloc( 1, signature_size );
1538 TEST_ASSERT( signature != NULL );
1539
1540 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1541
mohammad1603a97cb8c2018-03-28 03:46:26 -07001542 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001543 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001544 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1545
Gilles Peskine20035e32018-02-03 22:44:14 +01001546 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001547 key_data->x,
1548 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01001549
1550 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001551 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01001552 NULL, 0,
1553 signature, signature_size,
1554 &signature_length );
1555 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine93aa0332018-02-03 23:58:03 +01001556 TEST_ASSERT( signature_length == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01001557
1558exit:
1559 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01001560 mbedtls_free( signature );
1561 mbedtls_psa_crypto_free( );
1562}
1563/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03001564
1565/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001566void asymmetric_verify( int key_type_arg, data_t *key_data,
1567 int alg_arg, data_t *hash_data,
1568 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03001569{
1570 int slot = 1;
1571 psa_key_type_t key_type = key_type_arg;
1572 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001573 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03001574
itayzafrir5c753392018-05-08 11:18:38 +03001575 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03001576 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03001577 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001578 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1579 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
1580 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03001581
1582 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1583
1584 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001585 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03001586 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1587
1588 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001589 key_data->x,
1590 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03001591
1592 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001593 hash_data->x, hash_data->len,
itayzafrir5c753392018-05-08 11:18:38 +03001594 NULL, 0,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001595 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001596 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03001597exit:
1598 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03001599 mbedtls_psa_crypto_free( );
1600}
1601/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001602
1603/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001604void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
1605 int alg_arg, data_t *hash_data,
1606 data_t *signature_data,
1607 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001608{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001609 int slot = 1;
1610 psa_key_type_t key_type = key_type_arg;
1611 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001612 psa_status_t actual_status;
1613 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001614 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001615
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001616 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001617 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001618 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001619 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1620 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
1621 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001622
1623 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1624
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001625 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001626 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001627 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1628
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001629 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001630 key_data->x,
1631 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001632
1633 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001634 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001635 NULL, 0,
1636 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001637 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001638
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001639 TEST_ASSERT( actual_status == expected_status );
1640
1641exit:
1642 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001643 mbedtls_psa_crypto_free( );
1644}
1645/* END_CASE */
1646
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001647/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001648void asymmetric_encrypt_decrypt( int key_type_arg, data_t *key_data,
1649 int alg_arg, data_t *input_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001650{
1651 int slot = 1;
1652 psa_key_type_t key_type = key_type_arg;
1653 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001654 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001655 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001656 size_t output_length = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001657 unsigned char *output2 = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001658 size_t output2_size = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001659 size_t output2_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02001660 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001661
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001662 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001663 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001664 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1665 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1666
Gilles Peskine4abf7412018-06-18 16:35:34 +02001667 output_size = key_data->len;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001668 output2_size = output_size;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001669 output = mbedtls_calloc( 1, output_size );
1670 TEST_ASSERT( output != NULL );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001671 output2 = mbedtls_calloc( 1, output2_size );
1672 TEST_ASSERT( output2 != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001673
1674 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1675
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001676 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001677 psa_key_policy_set_usage( &policy,
1678 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001679 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001680 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1681
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001682 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001683 key_data->x,
1684 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001685
Gilles Peskineeebd7382018-06-08 18:11:54 +02001686 /* We test encryption by checking that encrypt-then-decrypt gives back
1687 * the original plaintext because of the non-optional random
1688 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02001689 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001690 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001691 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001692 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001693 &output_length ) == PSA_SUCCESS );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001694
Gilles Peskine2d277862018-06-18 15:41:12 +02001695 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001696 output, output_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001697 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001698 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001699 &output2_length ) == PSA_SUCCESS );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001700 TEST_ASSERT( memcmp( input_data->x, output2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001701 input_data->len ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001702
1703exit:
1704 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001705 mbedtls_free( output );
1706 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001707 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001708}
1709/* END_CASE */
1710
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001711/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001712void asymmetric_encrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001713 int alg_arg, data_t *input_data,
1714 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001715{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001716 int slot = 1;
1717 psa_key_type_t key_type = key_type_arg;
1718 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001719 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001720 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001721 size_t output_length = 0;
1722 psa_status_t actual_status;
1723 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001724 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001725
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001726 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001727 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001728 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1729 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1730
Gilles Peskine4abf7412018-06-18 16:35:34 +02001731 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001732 output = mbedtls_calloc( 1, output_size );
1733 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001734
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001735 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1736
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001737 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001738 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001739 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1740
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001741 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001742 key_data->x,
1743 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001744
Gilles Peskine2d277862018-06-18 15:41:12 +02001745 actual_status = psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001746 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001747 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001748 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001749 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001750 TEST_ASSERT( actual_status == expected_status );
1751
1752exit:
1753 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001754 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001755 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001756}
1757/* END_CASE */
1758
1759/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001760void asymmetric_decrypt( int key_type_arg, data_t *key_data,
1761 int alg_arg, data_t *input_data,
1762 data_t *expected_data, int expected_size )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001763{
1764 int slot = 1;
1765 psa_key_type_t key_type = key_type_arg;
1766 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001767 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001768 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001769 size_t output_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02001770 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001771
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001772 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001773 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001774 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001775 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1776 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1777 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
1778
Gilles Peskine4abf7412018-06-18 16:35:34 +02001779 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001780 output = mbedtls_calloc( 1, output_size );
1781 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001782
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001783 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1784
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001785 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001786 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001787 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1788
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001789 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001790 key_data->x,
1791 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001792
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001793 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001794 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001795 NULL, 0,
1796 output,
1797 output_size,
1798 &output_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001799 TEST_ASSERT( (size_t) expected_size == output_length );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001800 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001801
1802exit:
1803 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001804 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001805 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001806}
1807/* END_CASE */
1808
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001809/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001810void asymmetric_decrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001811 int alg_arg, data_t *input_data,
1812 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001813{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001814 int slot = 1;
1815 psa_key_type_t key_type = key_type_arg;
1816 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001817 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001818 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001819 size_t output_length = 0;
1820 psa_status_t actual_status;
1821 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001822 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001823
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001824 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001825 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001826 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1827 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1828
Gilles Peskine4abf7412018-06-18 16:35:34 +02001829 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001830 output = mbedtls_calloc( 1, output_size );
1831 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001832
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001833 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1834
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001835 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001836 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001837 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1838
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001839 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001840 key_data->x,
1841 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001842
Gilles Peskine2d277862018-06-18 15:41:12 +02001843 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001844 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001845 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001846 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001847 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001848 TEST_ASSERT( actual_status == expected_status );
1849
1850exit:
1851 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02001852 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001853 mbedtls_psa_crypto_free( );
1854}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001855/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02001856
1857/* BEGIN_CASE */
1858void generate_random( int bytes, int retries )
1859{
1860 const unsigned char trail[] = "foobar";
1861 unsigned char *buffer1 = mbedtls_calloc( 1, bytes + sizeof( trail ) );
1862 unsigned char *buffer2 = mbedtls_calloc( 1, bytes );
1863
1864 TEST_ASSERT( buffer1 != NULL );
1865 TEST_ASSERT( buffer2 != NULL );
1866 memcpy( buffer1 + bytes, trail, sizeof( trail ) );
1867
1868 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1869
1870 TEST_ASSERT( psa_generate_random( buffer1, bytes ) == PSA_SUCCESS );
1871
1872 /* Check that no more than bytes have been overwritten */
1873 TEST_ASSERT( memcmp( buffer1 + bytes, trail, sizeof( trail ) ) == 0 );
1874
1875 if( bytes == 0 )
1876 goto exit;
1877
1878 /* We can't validate that the data is really random, but we can
1879 * validate that it doesn't repeat between calls. There's a
1880 * 1/256^bytes chance that it does repeat, of course, so allow
1881 * a few retries. */
1882 ++retries; /* The first time isn't a REtry */
1883 do
1884 {
1885 --retries;
1886 TEST_ASSERT( psa_generate_random( buffer2, bytes ) == PSA_SUCCESS );
1887 }
1888 while( memcmp( buffer1, buffer2, bytes ) == 0 && retries >= -1 );
1889 TEST_ASSERT( retries >= 0 );
1890
1891exit:
1892 mbedtls_psa_crypto_free( );
1893 mbedtls_free( buffer1 );
1894 mbedtls_free( buffer2 );
1895}
1896/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02001897
1898/* BEGIN_CASE */
1899void generate_key( int type_arg,
1900 int bits_arg,
1901 int usage_arg,
1902 int alg_arg,
1903 int expected_status_arg )
1904{
1905 int slot = 1;
1906 psa_key_type_t type = type_arg;
1907 psa_key_usage_t usage = usage_arg;
1908 size_t bits = bits_arg;
1909 psa_algorithm_t alg = alg_arg;
1910 psa_status_t expected_status = expected_status_arg;
1911 psa_key_type_t got_type;
1912 size_t got_bits;
1913 unsigned char exported[616] = {0}; /* enough for a 1024-bit RSA key */
1914 size_t exported_length;
1915 psa_status_t expected_export_status =
1916 usage & PSA_KEY_USAGE_EXPORT ? PSA_SUCCESS : PSA_ERROR_NOT_PERMITTED;
1917 psa_status_t expected_info_status =
1918 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
1919 psa_key_policy_t policy;
1920
1921 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1922
1923 psa_key_policy_init( &policy );
1924 psa_key_policy_set_usage( &policy, usage, alg );
1925 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1926
1927 /* Generate a key */
1928 TEST_ASSERT( psa_generate_key( slot, type, bits,
1929 NULL, 0 ) == expected_status );
1930
1931 /* Test the key information */
1932 TEST_ASSERT( psa_get_key_information( slot,
1933 &got_type,
1934 &got_bits ) == expected_info_status );
1935 if( expected_info_status != PSA_SUCCESS )
1936 goto exit;
1937 TEST_ASSERT( got_type == type );
1938 TEST_ASSERT( got_bits == bits );
1939
1940 /* Export the key */
1941 TEST_ASSERT( psa_export_key( slot,
1942 exported, sizeof( exported ),
1943 &exported_length ) == expected_export_status );
1944 if( expected_export_status == PSA_SUCCESS )
1945 {
1946 if( PSA_KEY_TYPE_IS_RAW_BYTES( type ) )
1947 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
1948#if defined(MBEDTLS_DES_C)
1949 if( type == PSA_KEY_TYPE_DES )
1950 {
1951 /* Check the parity bits. */
1952 unsigned i;
1953 for( i = 0; i < bits / 8; i++ )
1954 {
1955 unsigned bit_count = 0;
1956 unsigned m;
1957 for( m = 1; m <= 0x100; m <<= 1 )
1958 {
1959 if( exported[i] & m )
1960 ++bit_count;
1961 }
1962 TEST_ASSERT( bit_count % 2 != 0 );
1963 }
1964 }
1965#endif
1966#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
1967 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
1968 {
1969 /* Sanity check: does this look like the beginning of a PKCS#8
1970 * RSA key pair? Assumes bits is a multiple of 8. */
1971 size_t n_bytes = bits / 8 + 1;
1972 size_t n_encoded_bytes;
1973 unsigned char *n_end;
1974 TEST_ASSERT( exported_length >= 7 + ( n_bytes + 3 ) * 9 / 2 );
1975 TEST_ASSERT( exported[0] == 0x30 );
1976 TEST_ASSERT( exported[1] == 0x82 ); // assumes >=416-bit key
1977 TEST_ASSERT( exported[4] == 0x02 );
1978 TEST_ASSERT( exported[5] == 0x01 );
1979 TEST_ASSERT( exported[6] == 0x00 );
1980 TEST_ASSERT( exported[7] == 0x02 );
1981 n_encoded_bytes = exported[8];
1982 n_end = exported + 9 + n_encoded_bytes;
1983 if( n_encoded_bytes & 0x80 )
1984 {
1985 n_encoded_bytes = ( n_encoded_bytes & 0x7f ) << 7;
1986 n_encoded_bytes |= exported[9] & 0x7f;
1987 n_end += 1;
1988 }
1989 /* The encoding of n should start with a 0 byte since it should
1990 * have its high bit set. However Mbed TLS is not compliant and
1991 * generates an invalid, but widely tolerated, encoding of
1992 * positive INTEGERs with a bit size that is a multiple of 8
1993 * with no leading 0 byte. Accept this here. */
1994 TEST_ASSERT( n_bytes == n_encoded_bytes ||
1995 n_bytes == n_encoded_bytes + 1 );
1996 if( n_bytes == n_encoded_bytes )
1997 TEST_ASSERT( exported[n_encoded_bytes <= 127 ? 9 : 10] == 0x00 );
1998 /* Sanity check: e must be 3 */
1999 TEST_ASSERT( n_end[0] == 0x02 );
2000 TEST_ASSERT( n_end[1] == 0x03 );
2001 TEST_ASSERT( n_end[2] == 0x01 );
2002 TEST_ASSERT( n_end[3] == 0x00 );
2003 TEST_ASSERT( n_end[4] == 0x01 );
2004 TEST_ASSERT( n_end[5] == 0x02 );
2005 }
2006#endif /* MBEDTLS_RSA_C */
2007#if defined(MBEDTLS_ECP_C)
2008 if( PSA_KEY_TYPE_IS_ECC( type ) )
2009 {
2010 /* Sanity check: does this look like the beginning of a PKCS#8
2011 * elliptic curve key pair? */
2012 TEST_ASSERT( exported_length >= bits * 3 / 8 + 10 );
2013 TEST_ASSERT( exported[0] == 0x30 );
2014 }
2015#endif /* MBEDTLS_ECP_C */
2016 }
2017
Gilles Peskine818ca122018-06-20 18:16:48 +02002018 /* Do something with the key according to its type and permitted usage. */
2019 if( PSA_ALG_IS_MAC( alg ) )
2020 exercise_mac_key( slot, usage, alg );
2021 else if( PSA_ALG_IS_CIPHER( alg ) )
2022 exercise_cipher_key( slot, usage, alg );
2023 else if( PSA_ALG_IS_AEAD( alg ) )
2024 exercise_aead_key( slot, usage, alg );
2025 else if( PSA_ALG_IS_SIGN( alg ) )
2026 exercise_signature_key( slot, usage, alg );
2027 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
2028 exercise_asymmetric_encryption_key( slot, usage, alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02002029
2030exit:
2031 psa_destroy_key( slot );
2032 mbedtls_psa_crypto_free( );
2033}
2034/* END_CASE */