blob: c67725d70d4368195b4bf15233facaa5622180f5 [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
Jaeden Amerof24c7f82018-06-27 17:20:43 +010011/** An invalid export length that will never be set by psa_export_key(). */
12static const size_t INVALID_EXPORT_LENGTH = ~0U;
13
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020014/** Test if a buffer is not all-bits zero.
15 *
16 * \param buffer Pointer to the beginning of the buffer.
17 * \param size Size of the buffer in bytes.
18 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020019 * \return 1 if the buffer is all-bits-zero.
20 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020021 */
Gilles Peskine3f669c32018-06-21 09:21:51 +020022static int mem_is_zero( void *buffer, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020023{
24 size_t i;
25 for( i = 0; i < size; i++ )
26 {
27 if( ( (unsigned char *) buffer )[i] != 0 )
Gilles Peskine3f669c32018-06-21 09:21:51 +020028 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020029 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020030 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020031}
Gilles Peskine818ca122018-06-20 18:16:48 +020032
Gilles Peskine48c0ea12018-06-21 14:15:31 +020033static int key_type_is_raw_bytes( psa_key_type_t type )
34{
35 psa_key_type_t category = type & PSA_KEY_TYPE_CATEGORY_MASK;
36 return( category == PSA_KEY_TYPE_RAW_DATA ||
37 category == PSA_KEY_TYPE_CATEGORY_SYMMETRIC );
38}
39
Gilles Peskine818ca122018-06-20 18:16:48 +020040static int exercise_mac_key( psa_key_slot_t key,
41 psa_key_usage_t usage,
42 psa_algorithm_t alg )
43{
44 psa_mac_operation_t operation;
45 const unsigned char input[] = "foo";
46 unsigned char mac[64] = {0};
47 size_t mac_length = sizeof( mac );
48
49 if( usage & PSA_KEY_USAGE_SIGN )
50 {
51 TEST_ASSERT( psa_mac_start( &operation, key, alg ) == PSA_SUCCESS );
52 TEST_ASSERT( psa_mac_update( &operation,
53 input, sizeof( input ) ) == PSA_SUCCESS );
54 TEST_ASSERT( psa_mac_finish( &operation,
55 mac, sizeof( input ),
56 &mac_length ) == PSA_SUCCESS );
57 }
58
59 if( usage & PSA_KEY_USAGE_VERIFY )
60 {
61 psa_status_t verify_status =
62 ( usage & PSA_KEY_USAGE_SIGN ?
63 PSA_SUCCESS :
64 PSA_ERROR_INVALID_SIGNATURE );
65 TEST_ASSERT( psa_mac_start( &operation, key, alg ) == PSA_SUCCESS );
66 TEST_ASSERT( psa_mac_update( &operation,
67 input, sizeof( input ) ) == PSA_SUCCESS );
68 TEST_ASSERT( psa_mac_verify( &operation, mac, mac_length ) == verify_status );
69 }
70
71 return( 1 );
72
73exit:
74 psa_mac_abort( &operation );
75 return( 0 );
76}
77
78static int exercise_cipher_key( psa_key_slot_t key,
79 psa_key_usage_t usage,
80 psa_algorithm_t alg )
81{
82 psa_cipher_operation_t operation;
83 unsigned char iv[16] = {0};
84 size_t iv_length = sizeof( iv );
85 const unsigned char plaintext[16] = "Hello, world...";
86 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
87 size_t ciphertext_length = sizeof( ciphertext );
88 unsigned char decrypted[sizeof( ciphertext )];
89 size_t part_length;
90
91 if( usage & PSA_KEY_USAGE_ENCRYPT )
92 {
93 TEST_ASSERT( psa_encrypt_setup( &operation, key, alg ) == PSA_SUCCESS );
94 TEST_ASSERT( psa_encrypt_generate_iv( &operation,
95 iv, sizeof( iv ),
96 &iv_length ) == PSA_SUCCESS );
97 TEST_ASSERT( psa_cipher_update( &operation,
98 plaintext, sizeof( plaintext ),
99 ciphertext, sizeof( ciphertext ),
100 &ciphertext_length ) == PSA_SUCCESS );
101 TEST_ASSERT( psa_cipher_finish( &operation,
102 ciphertext + ciphertext_length,
103 sizeof( ciphertext ) - ciphertext_length,
104 &part_length ) == PSA_SUCCESS );
105 ciphertext_length += part_length;
106 }
107
108 if( usage & PSA_KEY_USAGE_DECRYPT )
109 {
110 psa_status_t status;
111 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
112 {
113 psa_key_type_t type;
114 size_t bits;
115 TEST_ASSERT( psa_get_key_information( key, &type, &bits ) );
116 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
117 }
118 TEST_ASSERT( psa_decrypt_setup( &operation, key, alg ) == PSA_SUCCESS );
119 TEST_ASSERT( psa_encrypt_set_iv( &operation,
120 iv, iv_length ) == PSA_SUCCESS );
121 TEST_ASSERT( psa_cipher_update( &operation,
122 ciphertext, ciphertext_length,
123 decrypted, sizeof( decrypted ),
124 &part_length ) == PSA_SUCCESS );
125 status = psa_cipher_finish( &operation,
126 decrypted + part_length,
127 sizeof( decrypted ) - part_length,
128 &part_length );
129 /* For a stream cipher, all inputs are valid. For a block cipher,
130 * if the input is some aribtrary data rather than an actual
131 ciphertext, a padding error is likely. */
132 if( ( usage & PSA_KEY_USAGE_DECRYPT ) ||
133 PSA_BLOCK_CIPHER_BLOCK_SIZE( alg ) == 1 )
134 TEST_ASSERT( status == PSA_SUCCESS );
135 else
136 TEST_ASSERT( status == PSA_SUCCESS ||
137 status == PSA_ERROR_INVALID_PADDING );
138 }
139
140 return( 1 );
141
142exit:
143 psa_cipher_abort( &operation );
144 return( 0 );
145}
146
147static int exercise_aead_key( psa_key_slot_t key,
148 psa_key_usage_t usage,
149 psa_algorithm_t alg )
150{
151 unsigned char nonce[16] = {0};
152 size_t nonce_length = sizeof( nonce );
153 unsigned char plaintext[16] = "Hello, world...";
154 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
155 size_t ciphertext_length = sizeof( ciphertext );
156 size_t plaintext_length = sizeof( ciphertext );
157
158 if( usage & PSA_KEY_USAGE_ENCRYPT )
159 {
160 TEST_ASSERT( psa_aead_encrypt( key, alg,
161 nonce, nonce_length,
162 NULL, 0,
163 plaintext, sizeof( plaintext ),
164 ciphertext, sizeof( ciphertext ),
165 &ciphertext_length ) == PSA_SUCCESS );
166 }
167
168 if( usage & PSA_KEY_USAGE_DECRYPT )
169 {
170 psa_status_t verify_status =
171 ( usage & PSA_KEY_USAGE_ENCRYPT ?
172 PSA_SUCCESS :
173 PSA_ERROR_INVALID_SIGNATURE );
174 TEST_ASSERT( psa_aead_decrypt( key, alg,
175 nonce, nonce_length,
176 NULL, 0,
177 ciphertext, ciphertext_length,
178 plaintext, sizeof( plaintext ),
179 &plaintext_length ) == verify_status );
180 }
181
182 return( 1 );
183
184exit:
185 return( 0 );
186}
187
188static int exercise_signature_key( psa_key_slot_t key,
189 psa_key_usage_t usage,
190 psa_algorithm_t alg )
191{
192 unsigned char payload[16] = {0};
193 size_t payload_length = sizeof( payload );
194 unsigned char signature[256] = {0};
195 size_t signature_length = sizeof( signature );
196
197 if( usage & PSA_KEY_USAGE_SIGN )
198 {
199 TEST_ASSERT( psa_asymmetric_sign( key, alg,
200 payload, payload_length,
201 NULL, 0,
202 signature, sizeof( signature ),
203 &signature_length ) == PSA_SUCCESS );
204 }
205
206 if( usage & PSA_KEY_USAGE_VERIFY )
207 {
208 psa_status_t verify_status =
209 ( usage & PSA_KEY_USAGE_SIGN ?
210 PSA_SUCCESS :
211 PSA_ERROR_INVALID_SIGNATURE );
212 TEST_ASSERT( psa_asymmetric_verify( key, alg,
213 payload, payload_length,
214 NULL, 0,
215 signature, signature_length ) ==
216 verify_status );
217 }
218
219 return( 1 );
220
221exit:
222 return( 0 );
223}
224
225static int exercise_asymmetric_encryption_key( psa_key_slot_t key,
226 psa_key_usage_t usage,
227 psa_algorithm_t alg )
228{
229 unsigned char plaintext[256] = "Hello, world...";
230 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
231 size_t ciphertext_length = sizeof( ciphertext );
232 size_t plaintext_length = 16;
233
234 if( usage & PSA_KEY_USAGE_ENCRYPT )
235 {
236 TEST_ASSERT(
237 psa_asymmetric_encrypt( key, alg,
238 plaintext, plaintext_length,
239 NULL, 0,
240 ciphertext, sizeof( ciphertext ),
241 &ciphertext_length ) == PSA_SUCCESS );
242 }
243
244 if( usage & PSA_KEY_USAGE_DECRYPT )
245 {
246 psa_status_t status =
247 psa_asymmetric_decrypt( key, alg,
248 ciphertext, ciphertext_length,
249 NULL, 0,
250 plaintext, sizeof( plaintext ),
251 &plaintext_length );
252 TEST_ASSERT( status == PSA_SUCCESS ||
253 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
254 ( status == PSA_ERROR_INVALID_ARGUMENT ||
255 status == PSA_ERROR_INVALID_PADDING ) ) );
256 }
257
258 return( 1 );
259
260exit:
261 return( 0 );
262}
Gilles Peskinee59236f2018-01-27 23:32:46 +0100263/* END_HEADER */
264
265/* BEGIN_DEPENDENCIES
266 * depends_on:MBEDTLS_PSA_CRYPTO_C
267 * END_DEPENDENCIES
268 */
269
270/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200271void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100272{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100273 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100274 int i;
275 for( i = 0; i <= 1; i++ )
276 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100277 status = psa_crypto_init( );
278 TEST_ASSERT( status == PSA_SUCCESS );
279 status = psa_crypto_init( );
280 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100281 mbedtls_psa_crypto_free( );
282 }
283}
284/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100285
286/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200287void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100288{
289 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200290 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100291 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100292
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100293 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300294 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100295 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
296
Gilles Peskine4abf7412018-06-18 16:35:34 +0200297 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200298 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100299 if( status == PSA_SUCCESS )
300 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
301
302exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100303 mbedtls_psa_crypto_free( );
304}
305/* END_CASE */
306
307/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300308void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300309 int type_arg,
310 int alg_arg,
311 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100312 int expected_bits,
313 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200314 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100315 int canonical_input )
316{
317 int slot = 1;
318 int slot2 = slot + 1;
319 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200320 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200321 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100322 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100323 unsigned char *exported = NULL;
324 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100325 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100326 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100327 size_t reexported_length;
328 psa_key_type_t got_type;
329 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200330 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100331
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100332 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300333 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
334 export_size = (ssize_t) data->len + export_size_delta;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100335 exported = mbedtls_calloc( 1, export_size );
336 TEST_ASSERT( exported != NULL );
337 if( ! canonical_input )
338 {
339 reexported = mbedtls_calloc( 1, export_size );
340 TEST_ASSERT( reexported != NULL );
341 }
342 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
343
mohammad1603a97cb8c2018-03-28 03:46:26 -0700344 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200345 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700346 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
347
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100348 /* Import the key */
349 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200350 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100351
352 /* Test the key information */
353 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200354 &got_type,
355 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100356 TEST_ASSERT( got_type == type );
357 TEST_ASSERT( got_bits == (size_t) expected_bits );
358
359 /* Export the key */
360 status = psa_export_key( slot,
361 exported, export_size,
362 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200363 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100364
365 /* The exported length must be set by psa_export_key() to a value between 0
366 * and export_size. On errors, the exported length must be 0. */
367 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
368 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
369 TEST_ASSERT( exported_length <= export_size );
370
Gilles Peskine3f669c32018-06-21 09:21:51 +0200371 TEST_ASSERT( mem_is_zero( exported + exported_length,
372 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100373 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200374 {
375 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100376 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200377 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100378
379 if( canonical_input )
380 {
Gilles Peskine4abf7412018-06-18 16:35:34 +0200381 TEST_ASSERT( exported_length == data->len );
382 TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100383 }
384 else
385 {
mohammad1603a97cb8c2018-03-28 03:46:26 -0700386 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
387
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100388 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200389 exported,
390 export_size ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100391 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200392 reexported,
393 export_size,
394 &reexported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100395 TEST_ASSERT( reexported_length == exported_length );
396 TEST_ASSERT( memcmp( reexported, exported,
397 exported_length ) == 0 );
398 }
399
400destroy:
401 /* Destroy the key */
402 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
403 TEST_ASSERT( psa_get_key_information(
404 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
405
406exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300407 mbedtls_free( exported );
408 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100409 mbedtls_psa_crypto_free( );
410}
411/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100412
Moran Pekerf709f4a2018-06-06 17:26:04 +0300413/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300414void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200415 int type_arg,
416 int alg_arg,
417 int expected_bits,
418 int public_key_expected_length,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200419 int expected_export_status_arg )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300420{
421 int slot = 1;
422 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200423 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200424 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300425 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300426 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300427 size_t export_size;
428 size_t exported_length;
429 psa_key_type_t got_type;
430 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200431 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300432
Moran Pekerf709f4a2018-06-06 17:26:04 +0300433 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300434 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
435 export_size = (ssize_t) data->len;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300436 exported = mbedtls_calloc( 1, export_size );
437 TEST_ASSERT( exported != NULL );
438
439 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
440
441 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200442 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300443 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
444
445 /* Import the key */
446 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200447 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300448
449 /* Test the key information */
450 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200451 &got_type,
452 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300453 TEST_ASSERT( got_type == type );
454 TEST_ASSERT( got_bits == (size_t) expected_bits );
455
456 /* Export the key */
457 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200458 exported, export_size,
459 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200460 TEST_ASSERT( status == expected_export_status );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300461 if( status != PSA_SUCCESS )
462 goto destroy;
463
464 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
465
466destroy:
467 /* Destroy the key */
468 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
469 TEST_ASSERT( psa_get_key_information(
470 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
471
472exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300473 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300474 mbedtls_psa_crypto_free( );
475}
476/* END_CASE */
477
Gilles Peskine20035e32018-02-03 22:44:14 +0100478/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +0200479void key_policy( int usage_arg, int alg_arg )
480{
481 int key_slot = 1;
482 psa_algorithm_t alg = alg_arg;
483 psa_key_usage_t usage = usage_arg;
484 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
485 unsigned char key[32] = {0};
486 psa_key_policy_t policy_set;
487 psa_key_policy_t policy_get;
488
489 memset( key, 0x2a, sizeof( key ) );
490
491 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
492
493 psa_key_policy_init( &policy_set );
494 psa_key_policy_init( &policy_get );
495
496 psa_key_policy_set_usage( &policy_set, usage, alg );
497
498 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
499 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
500 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
501
502 TEST_ASSERT( psa_import_key( key_slot, key_type,
503 key, sizeof( key ) ) == PSA_SUCCESS );
504
505 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
506
507 TEST_ASSERT( policy_get.usage == policy_set.usage );
508 TEST_ASSERT( policy_get.alg == policy_set.alg );
509
510exit:
511 psa_destroy_key( key_slot );
512 mbedtls_psa_crypto_free( );
513}
514/* END_CASE */
515
516/* BEGIN_CASE */
517void key_policy_fail( int usage_arg, int alg_arg, int expected_status,
518 data_t *keypair )
519{
520 int key_slot = 1;
521 psa_algorithm_t alg = alg_arg;
522 psa_key_usage_t usage = usage_arg;
523 size_t signature_length = 0;
524 psa_key_policy_t policy;
525 int actual_status = PSA_SUCCESS;
526
527 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
528
529 psa_key_policy_init( &policy );
530 psa_key_policy_set_usage( &policy, usage, alg );
531 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
532
533 if( usage & PSA_KEY_USAGE_EXPORT )
534 {
535 TEST_ASSERT( keypair != NULL );
536 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) );
537 TEST_ASSERT( psa_import_key( key_slot,
538 PSA_KEY_TYPE_RSA_KEYPAIR,
539 keypair->x,
540 keypair->len ) == PSA_SUCCESS );
541 actual_status = psa_asymmetric_sign( key_slot, alg,
542 NULL, 0,
543 NULL, 0,
544 NULL, 0, &signature_length );
545 }
546
547 if( usage & PSA_KEY_USAGE_SIGN )
548 {
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100549 size_t data_length;
Gilles Peskined5b33222018-06-18 22:20:03 +0200550 TEST_ASSERT( keypair != NULL );
551 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) );
552 TEST_ASSERT( psa_import_key( key_slot,
553 PSA_KEY_TYPE_RSA_KEYPAIR,
554 keypair->x,
555 keypair->len ) == PSA_SUCCESS );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100556 actual_status = psa_export_key( key_slot, NULL, 0, &data_length );
Gilles Peskined5b33222018-06-18 22:20:03 +0200557 }
558
559 TEST_ASSERT( actual_status == expected_status );
560
561exit:
562 psa_destroy_key( key_slot );
563 mbedtls_psa_crypto_free( );
564}
565/* END_CASE */
566
567/* BEGIN_CASE */
568void key_lifetime( int lifetime_arg )
569{
570 int key_slot = 1;
571 psa_key_type_t key_type = PSA_ALG_CBC_BASE;
572 unsigned char key[32] = {0};
573 psa_key_lifetime_t lifetime_set = lifetime_arg;
574 psa_key_lifetime_t lifetime_get;
575
576 memset( key, 0x2a, sizeof( key ) );
577
578 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
579
580 TEST_ASSERT( psa_set_key_lifetime( key_slot,
581 lifetime_set ) == PSA_SUCCESS );
582
583 TEST_ASSERT( psa_import_key( key_slot, key_type,
584 key, sizeof( key ) ) == PSA_SUCCESS );
585
586 TEST_ASSERT( psa_get_key_lifetime( key_slot,
587 &lifetime_get ) == PSA_SUCCESS );
588
589 TEST_ASSERT( lifetime_get == lifetime_set );
590
591exit:
592 psa_destroy_key( key_slot );
593 mbedtls_psa_crypto_free( );
594}
595/* END_CASE */
596
597/* BEGIN_CASE */
598void key_lifetime_set_fail( int key_slot_arg,
599 int lifetime_arg,
600 int expected_status_arg )
601{
602 psa_key_slot_t key_slot = key_slot_arg;
603 psa_key_lifetime_t lifetime_set = lifetime_arg;
604 psa_status_t actual_status;
605 psa_status_t expected_status = expected_status_arg;
606
607 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
608
609 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
610
611 if( actual_status == PSA_SUCCESS )
612 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
613
614 TEST_ASSERT( expected_status == actual_status );
615
616exit:
617 psa_destroy_key( key_slot );
618 mbedtls_psa_crypto_free( );
619}
620/* END_CASE */
621
622/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200623void hash_setup( int alg_arg,
624 int expected_status_arg )
625{
626 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200627 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200628 psa_hash_operation_t operation;
629 psa_status_t status;
630
631 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
632
633 status = psa_hash_start( &operation, alg );
634 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200635 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200636
637exit:
638 mbedtls_psa_crypto_free( );
639}
640/* END_CASE */
641
642/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300643void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100644{
645 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +0200646 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100647 size_t actual_hash_length;
648 psa_hash_operation_t operation;
649
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100650 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300651 TEST_ASSERT( expected_hash != NULL );
652 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
653 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100654
655 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
656
657 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
658 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200659 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100660 TEST_ASSERT( psa_hash_finish( &operation,
661 actual_hash, sizeof( actual_hash ),
662 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200663 TEST_ASSERT( actual_hash_length == expected_hash->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300664 TEST_ASSERT( memcmp( expected_hash->x, actual_hash,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200665 expected_hash->len ) == 0 );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100666
667exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100668 mbedtls_psa_crypto_free( );
669}
670/* END_CASE */
671
672/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300673void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100674{
675 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100676 psa_hash_operation_t operation;
677
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100678 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300679 TEST_ASSERT( expected_hash != NULL );
680 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
681 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100682
683 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
684
685 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
686 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200687 input->x,
688 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100689 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300690 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200691 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100692
693exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100694 mbedtls_psa_crypto_free( );
695}
696/* END_CASE */
697
698/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200699void mac_setup( int key_type_arg,
700 data_t *key,
701 int alg_arg,
702 int expected_status_arg )
703{
704 int key_slot = 1;
705 psa_key_type_t key_type = key_type_arg;
706 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200707 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200708 psa_mac_operation_t operation;
709 psa_key_policy_t policy;
710 psa_status_t status;
711
712 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
713
714 psa_key_policy_init( &policy );
715 psa_key_policy_set_usage( &policy,
716 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
717 alg );
718 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
719
720 TEST_ASSERT( psa_import_key( key_slot, key_type,
721 key->x, key->len ) == PSA_SUCCESS );
722
723 status = psa_mac_start( &operation, key_slot, alg );
724 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200725 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200726
727exit:
728 psa_destroy_key( key_slot );
729 mbedtls_psa_crypto_free( );
730}
731/* END_CASE */
732
733/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +0200734void mac_verify( int key_type_arg,
735 data_t *key,
736 int alg_arg,
737 data_t *input,
738 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +0100739{
740 int key_slot = 1;
741 psa_key_type_t key_type = key_type_arg;
742 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100743 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -0700744 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100745
Gilles Peskine8c9def32018-02-08 10:02:12 +0100746 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100747 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100748 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300749 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300750 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
751 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100752
753 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
754
mohammad16036df908f2018-04-02 08:34:15 -0700755 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200756 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -0700757 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
758
Gilles Peskine8c9def32018-02-08 10:02:12 +0100759 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200760 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +0200761
Gilles Peskine8c9def32018-02-08 10:02:12 +0100762 TEST_ASSERT( psa_mac_start( &operation, key_slot, alg ) == PSA_SUCCESS );
763 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
764 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200765 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100766 TEST_ASSERT( psa_mac_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300767 expected_mac->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200768 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100769
770exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +0100771 psa_destroy_key( key_slot );
772 mbedtls_psa_crypto_free( );
773}
774/* END_CASE */
775
776/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200777void cipher_setup( int key_type_arg,
778 data_t *key,
779 int alg_arg,
780 int expected_status_arg )
781{
782 int key_slot = 1;
783 psa_key_type_t key_type = key_type_arg;
784 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200785 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200786 psa_cipher_operation_t operation;
787 psa_key_policy_t policy;
788 psa_status_t status;
789
790 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
791
792 psa_key_policy_init( &policy );
793 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
794 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
795
796 TEST_ASSERT( psa_import_key( key_slot, key_type,
797 key->x, key->len ) == PSA_SUCCESS );
798
799 status = psa_encrypt_setup( &operation, key_slot, alg );
800 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200801 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200802
803exit:
804 psa_destroy_key( key_slot );
805 mbedtls_psa_crypto_free( );
806}
807/* END_CASE */
808
809/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +0200810void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300811 data_t *key,
812 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200813 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +0200814{
815 int key_slot = 1;
816 psa_status_t status;
817 psa_key_type_t key_type = key_type_arg;
818 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200819 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200820 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200821 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300822 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200823 size_t output_buffer_size = 0;
824 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200825 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200826 psa_cipher_operation_t operation;
827
Gilles Peskine50e586b2018-06-08 14:28:46 +0200828 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200829 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200830 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300831 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
832 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
833 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200834
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200835 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
836 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200837
838 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
839
840 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200841 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200842
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200843 TEST_ASSERT( psa_encrypt_setup( &operation,
844 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200845
846 TEST_ASSERT( psa_encrypt_set_iv( &operation,
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200847 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200848 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200849 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300850 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200851
Gilles Peskine4abf7412018-06-18 16:35:34 +0200852 TEST_ASSERT( psa_cipher_update( &operation,
853 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200854 output, output_buffer_size,
855 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200856 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200857 status = psa_cipher_finish( &operation,
858 output + function_output_length,
859 output_buffer_size,
860 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200861 total_output_length += function_output_length;
862
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200863 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200864 if( expected_status == PSA_SUCCESS )
865 {
866 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200867 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300868 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200869 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200870 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200871
Gilles Peskine50e586b2018-06-08 14:28:46 +0200872exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300873 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200874 psa_destroy_key( key_slot );
875 mbedtls_psa_crypto_free( );
876}
877/* END_CASE */
878
879/* BEGIN_CASE */
880void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300881 data_t *key,
882 data_t *input,
883 int first_part_size,
884 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +0200885{
886 int key_slot = 1;
887 psa_key_type_t key_type = key_type_arg;
888 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200889 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200890 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300891 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200892 size_t output_buffer_size = 0;
893 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200894 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200895 psa_cipher_operation_t operation;
896
Gilles Peskine50e586b2018-06-08 14:28:46 +0200897 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200898 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200899 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300900 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
901 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
902 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200903
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200904 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
905 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200906
907 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
908
909 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200910 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200911
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200912 TEST_ASSERT( psa_encrypt_setup( &operation,
913 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200914
915 TEST_ASSERT( psa_encrypt_set_iv( &operation,
916 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200917 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200918 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300919 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200920
Gilles Peskine4abf7412018-06-18 16:35:34 +0200921 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300922 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200923 output, output_buffer_size,
924 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200925 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200926 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300927 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200928 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200929 output, output_buffer_size,
930 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200931 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200932 TEST_ASSERT( psa_cipher_finish( &operation,
933 output + function_output_length,
934 output_buffer_size,
935 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200936 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200937 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
938
Gilles Peskine4abf7412018-06-18 16:35:34 +0200939 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300940 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200941 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200942
943exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300944 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200945 psa_destroy_key( key_slot );
946 mbedtls_psa_crypto_free( );
947}
948/* END_CASE */
949
950/* BEGIN_CASE */
951void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300952 data_t *key,
953 data_t *input,
954 int first_part_size,
955 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +0200956{
957 int key_slot = 1;
958
959 psa_key_type_t key_type = key_type_arg;
960 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200961 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200962 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300963 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200964 size_t output_buffer_size = 0;
965 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200966 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200967 psa_cipher_operation_t operation;
968
Gilles Peskine50e586b2018-06-08 14:28:46 +0200969 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200970 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200971 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300972 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
973 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
974 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200975
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200976 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
977 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200978
979 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
980
981 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200982 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200983
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200984 TEST_ASSERT( psa_decrypt_setup( &operation,
985 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200986
987 TEST_ASSERT( psa_encrypt_set_iv( &operation,
988 iv, sizeof( iv ) ) == PSA_SUCCESS );
989
Gilles Peskine4abf7412018-06-18 16:35:34 +0200990 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200991 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300992 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200993
Gilles Peskine4abf7412018-06-18 16:35:34 +0200994 TEST_ASSERT( (unsigned int) first_part_size < input->len );
995 TEST_ASSERT( psa_cipher_update( &operation,
996 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200997 output, output_buffer_size,
998 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200999 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001000 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001001 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001002 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001003 output, output_buffer_size,
1004 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001005 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001006 TEST_ASSERT( psa_cipher_finish( &operation,
1007 output + function_output_length,
1008 output_buffer_size,
1009 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001010 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001011 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1012
Gilles Peskine4abf7412018-06-18 16:35:34 +02001013 TEST_ASSERT( total_output_length == expected_output->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001014 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001015 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001016
1017exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001018 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001019 psa_destroy_key( key_slot );
1020 mbedtls_psa_crypto_free( );
1021}
1022/* END_CASE */
1023
Gilles Peskine50e586b2018-06-08 14:28:46 +02001024/* BEGIN_CASE */
1025void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001026 data_t *key,
1027 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001028 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001029{
1030 int key_slot = 1;
1031 psa_status_t status;
1032 psa_key_type_t key_type = key_type_arg;
1033 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001034 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001035 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001036 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001037 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001038 size_t output_buffer_size = 0;
1039 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001040 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001041 psa_cipher_operation_t operation;
1042
Gilles Peskine50e586b2018-06-08 14:28:46 +02001043 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001044 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001045 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001046 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1047 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1048 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001049
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001050 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1051 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001052
1053 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1054
1055 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001056 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001057
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001058 TEST_ASSERT( psa_decrypt_setup( &operation,
1059 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001060
1061 TEST_ASSERT( psa_encrypt_set_iv( &operation,
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001062 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001063
Gilles Peskine4abf7412018-06-18 16:35:34 +02001064 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001065 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001066 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001067
Gilles Peskine4abf7412018-06-18 16:35:34 +02001068 TEST_ASSERT( psa_cipher_update( &operation,
1069 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001070 output, output_buffer_size,
1071 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001072 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001073 status = psa_cipher_finish( &operation,
1074 output + function_output_length,
1075 output_buffer_size,
1076 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001077 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001078 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001079
1080 if( expected_status == PSA_SUCCESS )
1081 {
1082 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001083 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001084 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001085 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001086 }
1087
Gilles Peskine50e586b2018-06-08 14:28:46 +02001088exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001089 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001090 psa_destroy_key( key_slot );
1091 mbedtls_psa_crypto_free( );
1092}
1093/* END_CASE */
1094
Gilles Peskine50e586b2018-06-08 14:28:46 +02001095/* BEGIN_CASE */
1096void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001097 data_t *key,
1098 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02001099{
1100 int key_slot = 1;
1101 psa_key_type_t key_type = key_type_arg;
1102 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07001103 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02001104 size_t iv_size = 16;
1105 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001106 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001107 size_t output1_size = 0;
1108 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001109 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001110 size_t output2_size = 0;
1111 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001112 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001113 psa_cipher_operation_t operation1;
1114 psa_cipher_operation_t operation2;
1115
mohammad1603d7d7ba52018-03-12 18:51:53 +02001116 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001117 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001118 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1119 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001120
mohammad1603d7d7ba52018-03-12 18:51:53 +02001121 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1122
1123 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001124 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001125
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001126 TEST_ASSERT( psa_encrypt_setup( &operation1,
1127 key_slot, alg ) == PSA_SUCCESS );
1128 TEST_ASSERT( psa_decrypt_setup( &operation2,
1129 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001130
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001131 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
1132 iv, iv_size,
1133 &iv_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001134 output1_size = input->len + operation1.block_size;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001135 output1 = mbedtls_calloc( 1, output1_size );
1136 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001137
Gilles Peskine4abf7412018-06-18 16:35:34 +02001138 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001139 output1, output1_size,
1140 &output1_length ) == PSA_SUCCESS );
1141 TEST_ASSERT( psa_cipher_finish( &operation1,
1142 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001143 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001144
Gilles Peskine048b7f02018-06-08 14:20:49 +02001145 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001146
1147 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1148
1149 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001150 output2 = mbedtls_calloc( 1, output2_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001151 TEST_ASSERT( output2 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001152
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001153 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
1154 iv, iv_length ) == PSA_SUCCESS );
1155 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
1156 output2, output2_size,
1157 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001158 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001159 TEST_ASSERT( psa_cipher_finish( &operation2,
1160 output2 + output2_length,
1161 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001162 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001163
Gilles Peskine048b7f02018-06-08 14:20:49 +02001164 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001165
Moran Pekerded84402018-06-06 16:36:50 +03001166 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1167
Gilles Peskine4abf7412018-06-18 16:35:34 +02001168 TEST_ASSERT( input->len == output2_length );
1169 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +03001170
1171exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001172 mbedtls_free( output1 );
1173 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03001174 psa_destroy_key( key_slot );
1175 mbedtls_psa_crypto_free( );
1176}
1177/* END_CASE */
1178
1179/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001180void cipher_verify_output_multipart( int alg_arg,
1181 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001182 data_t *key,
1183 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001184 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03001185{
1186 int key_slot = 1;
1187 psa_key_type_t key_type = key_type_arg;
1188 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03001189 unsigned char iv[16] = {0};
1190 size_t iv_size = 16;
1191 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001192 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001193 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001194 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001195 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001196 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001197 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001198 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001199 psa_cipher_operation_t operation1;
1200 psa_cipher_operation_t operation2;
1201
Moran Pekerded84402018-06-06 16:36:50 +03001202 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001203 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001204 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1205 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001206
Moran Pekerded84402018-06-06 16:36:50 +03001207 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1208
1209 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001210 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001211
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001212 TEST_ASSERT( psa_encrypt_setup( &operation1,
1213 key_slot, alg ) == PSA_SUCCESS );
1214 TEST_ASSERT( psa_decrypt_setup( &operation2,
1215 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001216
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001217 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
1218 iv, iv_size,
1219 &iv_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001220 output1_buffer_size = input->len + operation1.block_size;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001221 output1 = mbedtls_calloc( 1, output1_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001222 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001223
Gilles Peskine4abf7412018-06-18 16:35:34 +02001224 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001225
itayzafrir3e02b3b2018-06-12 17:06:52 +03001226 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001227 output1, output1_buffer_size,
1228 &function_output_length ) == PSA_SUCCESS );
1229 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001230
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001231 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001232 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001233 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001234 output1, output1_buffer_size,
1235 &function_output_length ) == PSA_SUCCESS );
1236 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001237
Gilles Peskine048b7f02018-06-08 14:20:49 +02001238 TEST_ASSERT( psa_cipher_finish( &operation1,
1239 output1 + output1_length,
1240 output1_buffer_size - output1_length,
1241 &function_output_length ) == PSA_SUCCESS );
1242 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001243
1244 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1245
Gilles Peskine048b7f02018-06-08 14:20:49 +02001246 output2_buffer_size = output1_length;
1247 output2 = mbedtls_calloc( 1, output2_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001248 TEST_ASSERT( output2 != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001249
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001250 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
1251 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001252
1253 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001254 output2, output2_buffer_size,
1255 &function_output_length ) == PSA_SUCCESS );
1256 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001257
Gilles Peskine048b7f02018-06-08 14:20:49 +02001258 TEST_ASSERT( psa_cipher_update( &operation2,
1259 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001260 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001261 output2, output2_buffer_size,
1262 &function_output_length ) == PSA_SUCCESS );
1263 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001264
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001265 TEST_ASSERT( psa_cipher_finish( &operation2,
1266 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001267 output2_buffer_size - output2_length,
1268 &function_output_length ) == PSA_SUCCESS );
1269 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001270
mohammad1603d7d7ba52018-03-12 18:51:53 +02001271 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1272
Gilles Peskine4abf7412018-06-18 16:35:34 +02001273 TEST_ASSERT( input->len == output2_length );
1274 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001275
1276exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001277 mbedtls_free( output1 );
1278 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001279 psa_destroy_key( key_slot );
1280 mbedtls_psa_crypto_free( );
1281}
1282/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02001283
Gilles Peskine20035e32018-02-03 22:44:14 +01001284/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001285void aead_encrypt_decrypt( int key_type_arg,
1286 data_t * key_data,
1287 int alg_arg,
1288 data_t * input_data,
1289 data_t * nonce,
1290 data_t * additional_data,
1291 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001292{
1293 int slot = 1;
1294 psa_key_type_t key_type = key_type_arg;
1295 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001296 unsigned char *output_data = NULL;
1297 size_t output_size = 0;
1298 size_t output_length = 0;
1299 unsigned char *output_data2 = NULL;
1300 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001301 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001302 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001303 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001304
Gilles Peskinea1cac842018-06-11 19:33:02 +02001305 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001306 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001307 TEST_ASSERT( nonce != NULL );
1308 TEST_ASSERT( additional_data != NULL );
1309 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1310 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1311 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1312 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1313
Gilles Peskine4abf7412018-06-18 16:35:34 +02001314 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001315 output_data = mbedtls_calloc( 1, output_size );
1316 TEST_ASSERT( output_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001317
1318 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1319
1320 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001321 psa_key_policy_set_usage( &policy,
1322 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
1323 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001324 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1325
1326 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001327 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001328
1329 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001330 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001331 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001332 additional_data->len,
1333 input_data->x, input_data->len,
1334 output_data, output_size,
1335 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001336
1337 if( PSA_SUCCESS == expected_result )
1338 {
1339 output_data2 = mbedtls_calloc( 1, output_length );
1340 TEST_ASSERT( output_data2 != NULL );
1341
1342 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001343 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001344 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001345 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001346 output_data, output_length,
1347 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001348 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02001349
itayzafrir3e02b3b2018-06-12 17:06:52 +03001350 TEST_ASSERT( memcmp( input_data->x, output_data2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001351 input_data->len ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001352 }
Gilles Peskine2d277862018-06-18 15:41:12 +02001353
Gilles Peskinea1cac842018-06-11 19:33:02 +02001354exit:
1355 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001356 mbedtls_free( output_data );
1357 mbedtls_free( output_data2 );
1358 mbedtls_psa_crypto_free( );
1359}
1360/* END_CASE */
1361
1362/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001363void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001364 int alg_arg, data_t * input_data,
1365 data_t * additional_data, data_t * nonce,
1366 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001367{
1368 int slot = 1;
1369 psa_key_type_t key_type = key_type_arg;
1370 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001371 unsigned char *output_data = NULL;
1372 size_t output_size = 0;
1373 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001374 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001375 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001376
Gilles Peskinea1cac842018-06-11 19:33:02 +02001377 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001378 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001379 TEST_ASSERT( additional_data != NULL );
1380 TEST_ASSERT( nonce != NULL );
1381 TEST_ASSERT( expected_result != NULL );
1382 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1383 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1384 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1385 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1386 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
1387
Gilles Peskine4abf7412018-06-18 16:35:34 +02001388 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001389 output_data = mbedtls_calloc( 1, output_size );
1390 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001391
Gilles Peskinea1cac842018-06-11 19:33:02 +02001392 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1393
1394 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001395 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001396 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1397
1398 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001399 key_data->x,
1400 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001401
1402 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001403 nonce->x, nonce->len,
1404 additional_data->x, additional_data->len,
1405 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001406 output_data, output_size,
1407 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001408
itayzafrir3e02b3b2018-06-12 17:06:52 +03001409 TEST_ASSERT( memcmp( output_data, expected_result->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001410 output_length ) == 0 );
1411
Gilles Peskinea1cac842018-06-11 19:33:02 +02001412exit:
1413 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001414 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001415 mbedtls_psa_crypto_free( );
1416}
1417/* END_CASE */
1418
1419/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001420void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001421 int alg_arg, data_t * input_data,
1422 data_t * additional_data, data_t * nonce,
1423 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001424{
1425 int slot = 1;
1426 psa_key_type_t key_type = key_type_arg;
1427 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001428 unsigned char *output_data = NULL;
1429 size_t output_size = 0;
1430 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001431 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001432 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001433 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001434
Gilles Peskinea1cac842018-06-11 19:33:02 +02001435 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001436 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001437 TEST_ASSERT( additional_data != NULL );
1438 TEST_ASSERT( nonce != NULL );
1439 TEST_ASSERT( expected_data != NULL );
1440 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1441 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1442 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1443 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1444 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
1445
Gilles Peskine4abf7412018-06-18 16:35:34 +02001446 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001447 output_data = mbedtls_calloc( 1, output_size );
1448 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001449
Gilles Peskinea1cac842018-06-11 19:33:02 +02001450 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1451
1452 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001453 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001454 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1455
1456 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001457 key_data->x,
1458 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001459
1460 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001461 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001462 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001463 additional_data->len,
1464 input_data->x, input_data->len,
1465 output_data, output_size,
1466 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001467
Gilles Peskine2d277862018-06-18 15:41:12 +02001468 if( expected_result == PSA_SUCCESS )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001469 {
itayzafrir3e02b3b2018-06-12 17:06:52 +03001470 TEST_ASSERT( memcmp( output_data, expected_data->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001471 output_length ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001472 }
1473
Gilles Peskinea1cac842018-06-11 19:33:02 +02001474exit:
1475 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001476 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001477 mbedtls_psa_crypto_free( );
1478}
1479/* END_CASE */
1480
1481/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001482void signature_size( int type_arg,
1483 int bits,
1484 int alg_arg,
1485 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001486{
1487 psa_key_type_t type = type_arg;
1488 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02001489 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01001490 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
1491exit:
1492 ;
1493}
1494/* END_CASE */
1495
1496/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001497void sign_deterministic( int key_type_arg, data_t *key_data,
1498 int alg_arg, data_t *input_data,
1499 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001500{
1501 int slot = 1;
1502 psa_key_type_t key_type = key_type_arg;
1503 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01001504 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01001505 unsigned char *signature = NULL;
1506 size_t signature_size;
1507 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02001508 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01001509
Gilles Peskine20035e32018-02-03 22:44:14 +01001510 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001511 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001512 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001513 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1514 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1515 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01001516
1517 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1518
mohammad1603a97cb8c2018-03-28 03:46:26 -07001519 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001520 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001521 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1522
Gilles Peskine20035e32018-02-03 22:44:14 +01001523 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001524 key_data->x,
1525 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01001526 TEST_ASSERT( psa_get_key_information( slot,
1527 NULL,
1528 &key_bits ) == PSA_SUCCESS );
1529
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001530 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
1531 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01001532 TEST_ASSERT( signature_size != 0 );
1533 signature = mbedtls_calloc( 1, signature_size );
1534 TEST_ASSERT( signature != NULL );
1535
1536 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001537 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01001538 NULL, 0,
1539 signature, signature_size,
1540 &signature_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001541 TEST_ASSERT( signature_length == output_data->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001542 TEST_ASSERT( memcmp( signature, output_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001543 output_data->len ) == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01001544
1545exit:
1546 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01001547 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01001548 mbedtls_psa_crypto_free( );
1549}
1550/* END_CASE */
1551
1552/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001553void sign_fail( int key_type_arg, data_t *key_data,
1554 int alg_arg, data_t *input_data,
Gilles Peskine20035e32018-02-03 22:44:14 +01001555 int signature_size, int expected_status_arg )
1556{
1557 int slot = 1;
1558 psa_key_type_t key_type = key_type_arg;
1559 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01001560 psa_status_t actual_status;
1561 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01001562 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01001563 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02001564 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01001565
Gilles Peskine20035e32018-02-03 22:44:14 +01001566 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001567 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001568 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1569 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1570
Gilles Peskine20035e32018-02-03 22:44:14 +01001571 signature = mbedtls_calloc( 1, signature_size );
1572 TEST_ASSERT( signature != NULL );
1573
1574 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1575
mohammad1603a97cb8c2018-03-28 03:46:26 -07001576 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001577 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001578 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1579
Gilles Peskine20035e32018-02-03 22:44:14 +01001580 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001581 key_data->x,
1582 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01001583
1584 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001585 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01001586 NULL, 0,
1587 signature, signature_size,
1588 &signature_length );
1589 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine93aa0332018-02-03 23:58:03 +01001590 TEST_ASSERT( signature_length == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01001591
1592exit:
1593 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01001594 mbedtls_free( signature );
1595 mbedtls_psa_crypto_free( );
1596}
1597/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03001598
1599/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001600void asymmetric_verify( int key_type_arg, data_t *key_data,
1601 int alg_arg, data_t *hash_data,
1602 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03001603{
1604 int slot = 1;
1605 psa_key_type_t key_type = key_type_arg;
1606 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001607 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03001608
itayzafrir5c753392018-05-08 11:18:38 +03001609 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03001610 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03001611 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001612 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1613 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
1614 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03001615
1616 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1617
1618 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001619 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03001620 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1621
1622 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001623 key_data->x,
1624 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03001625
1626 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001627 hash_data->x, hash_data->len,
itayzafrir5c753392018-05-08 11:18:38 +03001628 NULL, 0,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001629 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001630 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03001631exit:
1632 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03001633 mbedtls_psa_crypto_free( );
1634}
1635/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001636
1637/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001638void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
1639 int alg_arg, data_t *hash_data,
1640 data_t *signature_data,
1641 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001642{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001643 int slot = 1;
1644 psa_key_type_t key_type = key_type_arg;
1645 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001646 psa_status_t actual_status;
1647 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001648 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001649
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001650 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001651 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001652 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001653 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1654 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
1655 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001656
1657 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1658
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001659 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001660 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001661 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1662
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001663 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001664 key_data->x,
1665 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001666
1667 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001668 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001669 NULL, 0,
1670 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001671 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001672
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001673 TEST_ASSERT( actual_status == expected_status );
1674
1675exit:
1676 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001677 mbedtls_psa_crypto_free( );
1678}
1679/* END_CASE */
1680
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001681/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001682void asymmetric_encrypt_decrypt( int key_type_arg, data_t *key_data,
1683 int alg_arg, data_t *input_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001684{
1685 int slot = 1;
1686 psa_key_type_t key_type = key_type_arg;
1687 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001688 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001689 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001690 size_t output_length = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001691 unsigned char *output2 = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001692 size_t output2_size = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001693 size_t output2_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02001694 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001695
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001696 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001697 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001698 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1699 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1700
Gilles Peskine4abf7412018-06-18 16:35:34 +02001701 output_size = key_data->len;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001702 output2_size = output_size;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001703 output = mbedtls_calloc( 1, output_size );
1704 TEST_ASSERT( output != NULL );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001705 output2 = mbedtls_calloc( 1, output2_size );
1706 TEST_ASSERT( output2 != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001707
1708 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1709
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001710 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001711 psa_key_policy_set_usage( &policy,
1712 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001713 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001714 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1715
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001716 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001717 key_data->x,
1718 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001719
Gilles Peskineeebd7382018-06-08 18:11:54 +02001720 /* We test encryption by checking that encrypt-then-decrypt gives back
1721 * the original plaintext because of the non-optional random
1722 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02001723 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001724 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001725 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001726 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001727 &output_length ) == PSA_SUCCESS );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001728
Gilles Peskine2d277862018-06-18 15:41:12 +02001729 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001730 output, output_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001731 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001732 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001733 &output2_length ) == PSA_SUCCESS );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001734 TEST_ASSERT( memcmp( input_data->x, output2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001735 input_data->len ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001736
1737exit:
1738 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001739 mbedtls_free( output );
1740 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001741 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001742}
1743/* END_CASE */
1744
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001745/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001746void asymmetric_encrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001747 int alg_arg, data_t *input_data,
1748 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001749{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001750 int slot = 1;
1751 psa_key_type_t key_type = key_type_arg;
1752 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001753 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001754 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001755 size_t output_length = 0;
1756 psa_status_t actual_status;
1757 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001758 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001759
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001760 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001761 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001762 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1763 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1764
Gilles Peskine4abf7412018-06-18 16:35:34 +02001765 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001766 output = mbedtls_calloc( 1, output_size );
1767 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001768
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001769 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1770
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001771 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001772 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001773 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1774
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001775 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001776 key_data->x,
1777 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001778
Gilles Peskine2d277862018-06-18 15:41:12 +02001779 actual_status = psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001780 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001781 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001782 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001783 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001784 TEST_ASSERT( actual_status == expected_status );
1785
1786exit:
1787 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001788 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001789 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001790}
1791/* END_CASE */
1792
1793/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001794void asymmetric_decrypt( int key_type_arg, data_t *key_data,
1795 int alg_arg, data_t *input_data,
1796 data_t *expected_data, int expected_size )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001797{
1798 int slot = 1;
1799 psa_key_type_t key_type = key_type_arg;
1800 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001801 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001802 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001803 size_t output_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02001804 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001805
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001806 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001807 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001808 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001809 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1810 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1811 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
1812
Gilles Peskine4abf7412018-06-18 16:35:34 +02001813 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001814 output = mbedtls_calloc( 1, output_size );
1815 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001816
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001817 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1818
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001819 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001820 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001821 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1822
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001823 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001824 key_data->x,
1825 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001826
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001827 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001828 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001829 NULL, 0,
1830 output,
1831 output_size,
1832 &output_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001833 TEST_ASSERT( (size_t) expected_size == output_length );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001834 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001835
1836exit:
1837 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001838 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001839 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001840}
1841/* END_CASE */
1842
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001843/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001844void asymmetric_decrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001845 int alg_arg, data_t *input_data,
1846 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001847{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001848 int slot = 1;
1849 psa_key_type_t key_type = key_type_arg;
1850 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001851 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001852 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001853 size_t output_length = 0;
1854 psa_status_t actual_status;
1855 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001856 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001857
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001858 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001859 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001860 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1861 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1862
Gilles Peskine4abf7412018-06-18 16:35:34 +02001863 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001864 output = mbedtls_calloc( 1, output_size );
1865 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001866
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001867 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1868
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001869 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001870 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001871 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1872
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001873 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001874 key_data->x,
1875 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001876
Gilles Peskine2d277862018-06-18 15:41:12 +02001877 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001878 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001879 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001880 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001881 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001882 TEST_ASSERT( actual_status == expected_status );
1883
1884exit:
1885 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02001886 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001887 mbedtls_psa_crypto_free( );
1888}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001889/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02001890
1891/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02001892void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02001893{
Gilles Peskinea50d7392018-06-21 10:22:13 +02001894 size_t bytes = bytes_arg;
1895 const unsigned char trail[] = "don't overwrite me";
1896 unsigned char *output = mbedtls_calloc( 1, bytes + sizeof( trail ) );
1897 unsigned char *changed = mbedtls_calloc( 1, bytes );
1898 size_t i;
1899 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02001900
Gilles Peskinea50d7392018-06-21 10:22:13 +02001901 TEST_ASSERT( output != NULL );
1902 TEST_ASSERT( changed != NULL );
1903 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02001904
1905 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1906
Gilles Peskinea50d7392018-06-21 10:22:13 +02001907 /* Run several times, to ensure that every output byte will be
1908 * nonzero at least once with overwhelming probability
1909 * (2^(-8*number_of_runs)). */
1910 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02001911 {
Gilles Peskinea50d7392018-06-21 10:22:13 +02001912 memset( output, 0, bytes );
1913 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
1914
1915 /* Check that no more than bytes have been overwritten */
1916 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
1917
1918 for( i = 0; i < bytes; i++ )
1919 {
1920 if( output[i] != 0 )
1921 ++changed[i];
1922 }
Gilles Peskine05d69892018-06-19 22:00:52 +02001923 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02001924
1925 /* Check that every byte was changed to nonzero at least once. This
1926 * validates that psa_generate_random is overwriting every byte of
1927 * the output buffer. */
1928 for( i = 0; i < bytes; i++ )
1929 {
1930 TEST_ASSERT( changed[i] != 0 );
1931 }
Gilles Peskine05d69892018-06-19 22:00:52 +02001932
1933exit:
1934 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02001935 mbedtls_free( output );
1936 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02001937}
1938/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02001939
1940/* BEGIN_CASE */
1941void generate_key( int type_arg,
1942 int bits_arg,
1943 int usage_arg,
1944 int alg_arg,
1945 int expected_status_arg )
1946{
1947 int slot = 1;
1948 psa_key_type_t type = type_arg;
1949 psa_key_usage_t usage = usage_arg;
1950 size_t bits = bits_arg;
1951 psa_algorithm_t alg = alg_arg;
1952 psa_status_t expected_status = expected_status_arg;
1953 psa_key_type_t got_type;
1954 size_t got_bits;
1955 unsigned char exported[616] = {0}; /* enough for a 1024-bit RSA key */
1956 size_t exported_length;
1957 psa_status_t expected_export_status =
1958 usage & PSA_KEY_USAGE_EXPORT ? PSA_SUCCESS : PSA_ERROR_NOT_PERMITTED;
1959 psa_status_t expected_info_status =
1960 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
1961 psa_key_policy_t policy;
1962
1963 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1964
1965 psa_key_policy_init( &policy );
1966 psa_key_policy_set_usage( &policy, usage, alg );
1967 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1968
1969 /* Generate a key */
1970 TEST_ASSERT( psa_generate_key( slot, type, bits,
1971 NULL, 0 ) == expected_status );
1972
1973 /* Test the key information */
1974 TEST_ASSERT( psa_get_key_information( slot,
1975 &got_type,
1976 &got_bits ) == expected_info_status );
1977 if( expected_info_status != PSA_SUCCESS )
1978 goto exit;
1979 TEST_ASSERT( got_type == type );
1980 TEST_ASSERT( got_bits == bits );
1981
1982 /* Export the key */
1983 TEST_ASSERT( psa_export_key( slot,
1984 exported, sizeof( exported ),
1985 &exported_length ) == expected_export_status );
1986 if( expected_export_status == PSA_SUCCESS )
1987 {
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001988 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02001989 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
1990#if defined(MBEDTLS_DES_C)
1991 if( type == PSA_KEY_TYPE_DES )
1992 {
1993 /* Check the parity bits. */
1994 unsigned i;
1995 for( i = 0; i < bits / 8; i++ )
1996 {
1997 unsigned bit_count = 0;
1998 unsigned m;
1999 for( m = 1; m <= 0x100; m <<= 1 )
2000 {
2001 if( exported[i] & m )
2002 ++bit_count;
2003 }
2004 TEST_ASSERT( bit_count % 2 != 0 );
2005 }
2006 }
2007#endif
2008#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
2009 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
2010 {
2011 /* Sanity check: does this look like the beginning of a PKCS#8
2012 * RSA key pair? Assumes bits is a multiple of 8. */
2013 size_t n_bytes = bits / 8 + 1;
2014 size_t n_encoded_bytes;
2015 unsigned char *n_end;
2016 TEST_ASSERT( exported_length >= 7 + ( n_bytes + 3 ) * 9 / 2 );
2017 TEST_ASSERT( exported[0] == 0x30 );
2018 TEST_ASSERT( exported[1] == 0x82 ); // assumes >=416-bit key
2019 TEST_ASSERT( exported[4] == 0x02 );
2020 TEST_ASSERT( exported[5] == 0x01 );
2021 TEST_ASSERT( exported[6] == 0x00 );
2022 TEST_ASSERT( exported[7] == 0x02 );
2023 n_encoded_bytes = exported[8];
2024 n_end = exported + 9 + n_encoded_bytes;
2025 if( n_encoded_bytes & 0x80 )
2026 {
2027 n_encoded_bytes = ( n_encoded_bytes & 0x7f ) << 7;
2028 n_encoded_bytes |= exported[9] & 0x7f;
2029 n_end += 1;
2030 }
2031 /* The encoding of n should start with a 0 byte since it should
2032 * have its high bit set. However Mbed TLS is not compliant and
2033 * generates an invalid, but widely tolerated, encoding of
2034 * positive INTEGERs with a bit size that is a multiple of 8
2035 * with no leading 0 byte. Accept this here. */
2036 TEST_ASSERT( n_bytes == n_encoded_bytes ||
2037 n_bytes == n_encoded_bytes + 1 );
2038 if( n_bytes == n_encoded_bytes )
2039 TEST_ASSERT( exported[n_encoded_bytes <= 127 ? 9 : 10] == 0x00 );
2040 /* Sanity check: e must be 3 */
2041 TEST_ASSERT( n_end[0] == 0x02 );
2042 TEST_ASSERT( n_end[1] == 0x03 );
2043 TEST_ASSERT( n_end[2] == 0x01 );
2044 TEST_ASSERT( n_end[3] == 0x00 );
2045 TEST_ASSERT( n_end[4] == 0x01 );
2046 TEST_ASSERT( n_end[5] == 0x02 );
2047 }
2048#endif /* MBEDTLS_RSA_C */
2049#if defined(MBEDTLS_ECP_C)
2050 if( PSA_KEY_TYPE_IS_ECC( type ) )
2051 {
2052 /* Sanity check: does this look like the beginning of a PKCS#8
2053 * elliptic curve key pair? */
2054 TEST_ASSERT( exported_length >= bits * 3 / 8 + 10 );
2055 TEST_ASSERT( exported[0] == 0x30 );
2056 }
2057#endif /* MBEDTLS_ECP_C */
2058 }
2059
Gilles Peskine818ca122018-06-20 18:16:48 +02002060 /* Do something with the key according to its type and permitted usage. */
2061 if( PSA_ALG_IS_MAC( alg ) )
2062 exercise_mac_key( slot, usage, alg );
2063 else if( PSA_ALG_IS_CIPHER( alg ) )
2064 exercise_cipher_key( slot, usage, alg );
2065 else if( PSA_ALG_IS_AEAD( alg ) )
2066 exercise_aead_key( slot, usage, alg );
2067 else if( PSA_ALG_IS_SIGN( alg ) )
2068 exercise_signature_key( slot, usage, alg );
2069 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
2070 exercise_asymmetric_encryption_key( slot, usage, alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02002071
2072exit:
2073 psa_destroy_key( slot );
2074 mbedtls_psa_crypto_free( );
2075}
2076/* END_CASE */