blob: 51e541902c8ca0a52ca85a9acd334584f3ee2cd1 [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;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700111 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200112 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
113 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200114 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 ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700133 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine818ca122018-06-20 18:16:48 +0200134 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;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100428 size_t exported_length = INVALID_EXPORT_LENGTH;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300429 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 );
Jaeden Amero2a671e92018-06-27 17:47:40 +0100461 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
462 TEST_ASSERT( mem_is_zero( exported + exported_length,
463 export_size - exported_length ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300464 if( status != PSA_SUCCESS )
465 goto destroy;
466
Moran Pekerf709f4a2018-06-06 17:26:04 +0300467destroy:
468 /* Destroy the key */
469 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
470 TEST_ASSERT( psa_get_key_information(
471 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
472
473exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300474 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300475 mbedtls_psa_crypto_free( );
476}
477/* END_CASE */
478
Gilles Peskine20035e32018-02-03 22:44:14 +0100479/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +0200480void key_policy( int usage_arg, int alg_arg )
481{
482 int key_slot = 1;
483 psa_algorithm_t alg = alg_arg;
484 psa_key_usage_t usage = usage_arg;
485 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
486 unsigned char key[32] = {0};
487 psa_key_policy_t policy_set;
488 psa_key_policy_t policy_get;
489
490 memset( key, 0x2a, sizeof( key ) );
491
492 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
493
494 psa_key_policy_init( &policy_set );
495 psa_key_policy_init( &policy_get );
496
497 psa_key_policy_set_usage( &policy_set, usage, alg );
498
499 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
500 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
501 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
502
503 TEST_ASSERT( psa_import_key( key_slot, key_type,
504 key, sizeof( key ) ) == PSA_SUCCESS );
505
506 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
507
508 TEST_ASSERT( policy_get.usage == policy_set.usage );
509 TEST_ASSERT( policy_get.alg == policy_set.alg );
510
511exit:
512 psa_destroy_key( key_slot );
513 mbedtls_psa_crypto_free( );
514}
515/* END_CASE */
516
517/* BEGIN_CASE */
518void key_policy_fail( int usage_arg, int alg_arg, int expected_status,
519 data_t *keypair )
520{
521 int key_slot = 1;
522 psa_algorithm_t alg = alg_arg;
523 psa_key_usage_t usage = usage_arg;
524 size_t signature_length = 0;
525 psa_key_policy_t policy;
526 int actual_status = PSA_SUCCESS;
527
528 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
529
530 psa_key_policy_init( &policy );
531 psa_key_policy_set_usage( &policy, usage, alg );
532 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
533
534 if( usage & PSA_KEY_USAGE_EXPORT )
535 {
536 TEST_ASSERT( keypair != NULL );
537 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) );
538 TEST_ASSERT( psa_import_key( key_slot,
539 PSA_KEY_TYPE_RSA_KEYPAIR,
540 keypair->x,
541 keypair->len ) == PSA_SUCCESS );
542 actual_status = psa_asymmetric_sign( key_slot, alg,
543 NULL, 0,
544 NULL, 0,
545 NULL, 0, &signature_length );
546 }
547
548 if( usage & PSA_KEY_USAGE_SIGN )
549 {
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100550 size_t data_length;
Gilles Peskined5b33222018-06-18 22:20:03 +0200551 TEST_ASSERT( keypair != NULL );
552 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) );
553 TEST_ASSERT( psa_import_key( key_slot,
554 PSA_KEY_TYPE_RSA_KEYPAIR,
555 keypair->x,
556 keypair->len ) == PSA_SUCCESS );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100557 actual_status = psa_export_key( key_slot, NULL, 0, &data_length );
Gilles Peskined5b33222018-06-18 22:20:03 +0200558 }
559
560 TEST_ASSERT( actual_status == expected_status );
561
562exit:
563 psa_destroy_key( key_slot );
564 mbedtls_psa_crypto_free( );
565}
566/* END_CASE */
567
568/* BEGIN_CASE */
569void key_lifetime( int lifetime_arg )
570{
571 int key_slot = 1;
572 psa_key_type_t key_type = PSA_ALG_CBC_BASE;
573 unsigned char key[32] = {0};
574 psa_key_lifetime_t lifetime_set = lifetime_arg;
575 psa_key_lifetime_t lifetime_get;
576
577 memset( key, 0x2a, sizeof( key ) );
578
579 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
580
581 TEST_ASSERT( psa_set_key_lifetime( key_slot,
582 lifetime_set ) == PSA_SUCCESS );
583
584 TEST_ASSERT( psa_import_key( key_slot, key_type,
585 key, sizeof( key ) ) == PSA_SUCCESS );
586
587 TEST_ASSERT( psa_get_key_lifetime( key_slot,
588 &lifetime_get ) == PSA_SUCCESS );
589
590 TEST_ASSERT( lifetime_get == lifetime_set );
591
592exit:
593 psa_destroy_key( key_slot );
594 mbedtls_psa_crypto_free( );
595}
596/* END_CASE */
597
598/* BEGIN_CASE */
599void key_lifetime_set_fail( int key_slot_arg,
600 int lifetime_arg,
601 int expected_status_arg )
602{
603 psa_key_slot_t key_slot = key_slot_arg;
604 psa_key_lifetime_t lifetime_set = lifetime_arg;
605 psa_status_t actual_status;
606 psa_status_t expected_status = expected_status_arg;
607
608 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
609
610 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
611
612 if( actual_status == PSA_SUCCESS )
613 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
614
615 TEST_ASSERT( expected_status == actual_status );
616
617exit:
618 psa_destroy_key( key_slot );
619 mbedtls_psa_crypto_free( );
620}
621/* END_CASE */
622
623/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200624void hash_setup( int alg_arg,
625 int expected_status_arg )
626{
627 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200628 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200629 psa_hash_operation_t operation;
630 psa_status_t status;
631
632 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
633
634 status = psa_hash_start( &operation, alg );
635 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200636 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200637
638exit:
639 mbedtls_psa_crypto_free( );
640}
641/* END_CASE */
642
643/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300644void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100645{
646 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +0200647 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100648 size_t actual_hash_length;
649 psa_hash_operation_t operation;
650
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100651 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300652 TEST_ASSERT( expected_hash != NULL );
653 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
654 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100655
656 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
657
658 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
659 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200660 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100661 TEST_ASSERT( psa_hash_finish( &operation,
662 actual_hash, sizeof( actual_hash ),
663 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200664 TEST_ASSERT( actual_hash_length == expected_hash->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300665 TEST_ASSERT( memcmp( expected_hash->x, actual_hash,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200666 expected_hash->len ) == 0 );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100667
668exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100669 mbedtls_psa_crypto_free( );
670}
671/* END_CASE */
672
673/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300674void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100675{
676 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100677 psa_hash_operation_t operation;
678
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100679 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300680 TEST_ASSERT( expected_hash != NULL );
681 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
682 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100683
684 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
685
686 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
687 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200688 input->x,
689 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100690 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300691 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200692 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100693
694exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100695 mbedtls_psa_crypto_free( );
696}
697/* END_CASE */
698
699/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200700void mac_setup( int key_type_arg,
701 data_t *key,
702 int alg_arg,
703 int expected_status_arg )
704{
705 int key_slot = 1;
706 psa_key_type_t key_type = key_type_arg;
707 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200708 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200709 psa_mac_operation_t operation;
710 psa_key_policy_t policy;
711 psa_status_t status;
712
713 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
714
715 psa_key_policy_init( &policy );
716 psa_key_policy_set_usage( &policy,
717 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
718 alg );
719 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
720
721 TEST_ASSERT( psa_import_key( key_slot, key_type,
722 key->x, key->len ) == PSA_SUCCESS );
723
724 status = psa_mac_start( &operation, key_slot, alg );
725 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200726 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200727
728exit:
729 psa_destroy_key( key_slot );
730 mbedtls_psa_crypto_free( );
731}
732/* END_CASE */
733
734/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +0200735void mac_verify( int key_type_arg,
736 data_t *key,
737 int alg_arg,
738 data_t *input,
739 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +0100740{
741 int key_slot = 1;
742 psa_key_type_t key_type = key_type_arg;
743 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100744 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -0700745 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100746
Gilles Peskine8c9def32018-02-08 10:02:12 +0100747 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100748 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100749 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300750 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300751 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
752 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100753
754 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
755
mohammad16036df908f2018-04-02 08:34:15 -0700756 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200757 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -0700758 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
759
Gilles Peskine8c9def32018-02-08 10:02:12 +0100760 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200761 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +0200762
Gilles Peskine8c9def32018-02-08 10:02:12 +0100763 TEST_ASSERT( psa_mac_start( &operation, key_slot, alg ) == PSA_SUCCESS );
764 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
765 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200766 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100767 TEST_ASSERT( psa_mac_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300768 expected_mac->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200769 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100770
771exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +0100772 psa_destroy_key( key_slot );
773 mbedtls_psa_crypto_free( );
774}
775/* END_CASE */
776
777/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200778void cipher_setup( int key_type_arg,
779 data_t *key,
780 int alg_arg,
781 int expected_status_arg )
782{
783 int key_slot = 1;
784 psa_key_type_t key_type = key_type_arg;
785 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200786 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200787 psa_cipher_operation_t operation;
788 psa_key_policy_t policy;
789 psa_status_t status;
790
791 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
792
793 psa_key_policy_init( &policy );
794 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
795 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
796
797 TEST_ASSERT( psa_import_key( key_slot, key_type,
798 key->x, key->len ) == PSA_SUCCESS );
799
800 status = psa_encrypt_setup( &operation, key_slot, alg );
801 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200802 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200803
804exit:
805 psa_destroy_key( key_slot );
806 mbedtls_psa_crypto_free( );
807}
808/* END_CASE */
809
810/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +0200811void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300812 data_t *key,
813 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200814 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +0200815{
816 int key_slot = 1;
817 psa_status_t status;
818 psa_key_type_t key_type = key_type_arg;
819 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200820 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200821 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200822 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300823 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200824 size_t output_buffer_size = 0;
825 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200826 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200827 psa_cipher_operation_t operation;
828
Gilles Peskine50e586b2018-06-08 14:28:46 +0200829 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200830 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200831 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300832 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
833 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
834 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200835
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200836 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
837 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200838
839 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
840
841 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200842 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200843
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200844 TEST_ASSERT( psa_encrypt_setup( &operation,
845 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200846
847 TEST_ASSERT( psa_encrypt_set_iv( &operation,
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200848 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200849 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200850 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300851 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200852
Gilles Peskine4abf7412018-06-18 16:35:34 +0200853 TEST_ASSERT( psa_cipher_update( &operation,
854 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200855 output, output_buffer_size,
856 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200857 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200858 status = psa_cipher_finish( &operation,
859 output + function_output_length,
860 output_buffer_size,
861 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200862 total_output_length += function_output_length;
863
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200864 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200865 if( expected_status == PSA_SUCCESS )
866 {
867 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200868 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300869 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200870 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200871 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200872
Gilles Peskine50e586b2018-06-08 14:28:46 +0200873exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300874 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200875 psa_destroy_key( key_slot );
876 mbedtls_psa_crypto_free( );
877}
878/* END_CASE */
879
880/* BEGIN_CASE */
881void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300882 data_t *key,
883 data_t *input,
884 int first_part_size,
885 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +0200886{
887 int key_slot = 1;
888 psa_key_type_t key_type = key_type_arg;
889 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200890 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200891 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300892 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200893 size_t output_buffer_size = 0;
894 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200895 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200896 psa_cipher_operation_t operation;
897
Gilles Peskine50e586b2018-06-08 14:28:46 +0200898 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200899 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200900 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300901 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
902 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
903 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200904
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200905 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
906 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200907
908 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
909
910 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200911 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200912
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200913 TEST_ASSERT( psa_encrypt_setup( &operation,
914 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200915
916 TEST_ASSERT( psa_encrypt_set_iv( &operation,
917 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200918 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200919 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300920 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200921
Gilles Peskine4abf7412018-06-18 16:35:34 +0200922 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300923 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200924 output, output_buffer_size,
925 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200926 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200927 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300928 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200929 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200930 output, output_buffer_size,
931 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200932 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200933 TEST_ASSERT( psa_cipher_finish( &operation,
934 output + function_output_length,
935 output_buffer_size,
936 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200937 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200938 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
939
Gilles Peskine4abf7412018-06-18 16:35:34 +0200940 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300941 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200942 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200943
944exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300945 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200946 psa_destroy_key( key_slot );
947 mbedtls_psa_crypto_free( );
948}
949/* END_CASE */
950
951/* BEGIN_CASE */
952void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300953 data_t *key,
954 data_t *input,
955 int first_part_size,
956 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +0200957{
958 int key_slot = 1;
959
960 psa_key_type_t key_type = key_type_arg;
961 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200962 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200963 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300964 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200965 size_t output_buffer_size = 0;
966 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200967 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200968 psa_cipher_operation_t operation;
969
Gilles Peskine50e586b2018-06-08 14:28:46 +0200970 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200971 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200972 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300973 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
974 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
975 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200976
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200977 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
978 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200979
980 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
981
982 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200983 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200984
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200985 TEST_ASSERT( psa_decrypt_setup( &operation,
986 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200987
988 TEST_ASSERT( psa_encrypt_set_iv( &operation,
989 iv, sizeof( iv ) ) == PSA_SUCCESS );
990
Gilles Peskine4abf7412018-06-18 16:35:34 +0200991 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200992 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300993 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200994
Gilles Peskine4abf7412018-06-18 16:35:34 +0200995 TEST_ASSERT( (unsigned int) first_part_size < input->len );
996 TEST_ASSERT( psa_cipher_update( &operation,
997 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200998 output, output_buffer_size,
999 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001000 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001001 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001002 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001003 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001004 output, output_buffer_size,
1005 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001006 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001007 TEST_ASSERT( psa_cipher_finish( &operation,
1008 output + function_output_length,
1009 output_buffer_size,
1010 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001011 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001012 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1013
Gilles Peskine4abf7412018-06-18 16:35:34 +02001014 TEST_ASSERT( total_output_length == expected_output->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001015 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001016 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001017
1018exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001019 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001020 psa_destroy_key( key_slot );
1021 mbedtls_psa_crypto_free( );
1022}
1023/* END_CASE */
1024
Gilles Peskine50e586b2018-06-08 14:28:46 +02001025/* BEGIN_CASE */
1026void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001027 data_t *key,
1028 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001029 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001030{
1031 int key_slot = 1;
1032 psa_status_t status;
1033 psa_key_type_t key_type = key_type_arg;
1034 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001035 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001036 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001037 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001038 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001039 size_t output_buffer_size = 0;
1040 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001041 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001042 psa_cipher_operation_t operation;
1043
Gilles Peskine50e586b2018-06-08 14:28:46 +02001044 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001045 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001046 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001047 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1048 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1049 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001050
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001051 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1052 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001053
1054 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1055
1056 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001057 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001058
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001059 TEST_ASSERT( psa_decrypt_setup( &operation,
1060 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001061
1062 TEST_ASSERT( psa_encrypt_set_iv( &operation,
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001063 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001064
Gilles Peskine4abf7412018-06-18 16:35:34 +02001065 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001066 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001067 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001068
Gilles Peskine4abf7412018-06-18 16:35:34 +02001069 TEST_ASSERT( psa_cipher_update( &operation,
1070 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001071 output, output_buffer_size,
1072 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001073 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001074 status = psa_cipher_finish( &operation,
1075 output + function_output_length,
1076 output_buffer_size,
1077 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001078 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001079 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001080
1081 if( expected_status == PSA_SUCCESS )
1082 {
1083 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001084 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001085 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001086 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001087 }
1088
Gilles Peskine50e586b2018-06-08 14:28:46 +02001089exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001090 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001091 psa_destroy_key( key_slot );
1092 mbedtls_psa_crypto_free( );
1093}
1094/* END_CASE */
1095
Gilles Peskine50e586b2018-06-08 14:28:46 +02001096/* BEGIN_CASE */
1097void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001098 data_t *key,
1099 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02001100{
1101 int key_slot = 1;
1102 psa_key_type_t key_type = key_type_arg;
1103 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07001104 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02001105 size_t iv_size = 16;
1106 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001107 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001108 size_t output1_size = 0;
1109 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001110 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001111 size_t output2_size = 0;
1112 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001113 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001114 psa_cipher_operation_t operation1;
1115 psa_cipher_operation_t operation2;
1116
mohammad1603d7d7ba52018-03-12 18:51:53 +02001117 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001118 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001119 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1120 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001121
mohammad1603d7d7ba52018-03-12 18:51:53 +02001122 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1123
1124 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001125 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001126
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001127 TEST_ASSERT( psa_encrypt_setup( &operation1,
1128 key_slot, alg ) == PSA_SUCCESS );
1129 TEST_ASSERT( psa_decrypt_setup( &operation2,
1130 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001131
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001132 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
1133 iv, iv_size,
1134 &iv_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001135 output1_size = input->len + operation1.block_size;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001136 output1 = mbedtls_calloc( 1, output1_size );
1137 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001138
Gilles Peskine4abf7412018-06-18 16:35:34 +02001139 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001140 output1, output1_size,
1141 &output1_length ) == PSA_SUCCESS );
1142 TEST_ASSERT( psa_cipher_finish( &operation1,
1143 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001144 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001145
Gilles Peskine048b7f02018-06-08 14:20:49 +02001146 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001147
1148 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1149
1150 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001151 output2 = mbedtls_calloc( 1, output2_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001152 TEST_ASSERT( output2 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001153
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001154 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
1155 iv, iv_length ) == PSA_SUCCESS );
1156 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
1157 output2, output2_size,
1158 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001159 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001160 TEST_ASSERT( psa_cipher_finish( &operation2,
1161 output2 + output2_length,
1162 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001163 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001164
Gilles Peskine048b7f02018-06-08 14:20:49 +02001165 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001166
Moran Pekerded84402018-06-06 16:36:50 +03001167 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1168
Gilles Peskine4abf7412018-06-18 16:35:34 +02001169 TEST_ASSERT( input->len == output2_length );
1170 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +03001171
1172exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001173 mbedtls_free( output1 );
1174 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03001175 psa_destroy_key( key_slot );
1176 mbedtls_psa_crypto_free( );
1177}
1178/* END_CASE */
1179
1180/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001181void cipher_verify_output_multipart( int alg_arg,
1182 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001183 data_t *key,
1184 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001185 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03001186{
1187 int key_slot = 1;
1188 psa_key_type_t key_type = key_type_arg;
1189 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03001190 unsigned char iv[16] = {0};
1191 size_t iv_size = 16;
1192 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001193 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001194 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001195 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001196 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001197 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001198 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001199 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001200 psa_cipher_operation_t operation1;
1201 psa_cipher_operation_t operation2;
1202
Moran Pekerded84402018-06-06 16:36:50 +03001203 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001204 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001205 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1206 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001207
Moran Pekerded84402018-06-06 16:36:50 +03001208 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1209
1210 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001211 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001212
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001213 TEST_ASSERT( psa_encrypt_setup( &operation1,
1214 key_slot, alg ) == PSA_SUCCESS );
1215 TEST_ASSERT( psa_decrypt_setup( &operation2,
1216 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001217
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001218 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
1219 iv, iv_size,
1220 &iv_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001221 output1_buffer_size = input->len + operation1.block_size;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001222 output1 = mbedtls_calloc( 1, output1_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001223 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001224
Gilles Peskine4abf7412018-06-18 16:35:34 +02001225 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001226
itayzafrir3e02b3b2018-06-12 17:06:52 +03001227 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001228 output1, output1_buffer_size,
1229 &function_output_length ) == PSA_SUCCESS );
1230 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001231
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001232 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001233 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001234 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001235 output1, output1_buffer_size,
1236 &function_output_length ) == PSA_SUCCESS );
1237 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001238
Gilles Peskine048b7f02018-06-08 14:20:49 +02001239 TEST_ASSERT( psa_cipher_finish( &operation1,
1240 output1 + output1_length,
1241 output1_buffer_size - output1_length,
1242 &function_output_length ) == PSA_SUCCESS );
1243 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001244
1245 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1246
Gilles Peskine048b7f02018-06-08 14:20:49 +02001247 output2_buffer_size = output1_length;
1248 output2 = mbedtls_calloc( 1, output2_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001249 TEST_ASSERT( output2 != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001250
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001251 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
1252 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001253
1254 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001255 output2, output2_buffer_size,
1256 &function_output_length ) == PSA_SUCCESS );
1257 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001258
Gilles Peskine048b7f02018-06-08 14:20:49 +02001259 TEST_ASSERT( psa_cipher_update( &operation2,
1260 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001261 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001262 output2, output2_buffer_size,
1263 &function_output_length ) == PSA_SUCCESS );
1264 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001265
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001266 TEST_ASSERT( psa_cipher_finish( &operation2,
1267 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001268 output2_buffer_size - output2_length,
1269 &function_output_length ) == PSA_SUCCESS );
1270 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001271
mohammad1603d7d7ba52018-03-12 18:51:53 +02001272 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1273
Gilles Peskine4abf7412018-06-18 16:35:34 +02001274 TEST_ASSERT( input->len == output2_length );
1275 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001276
1277exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001278 mbedtls_free( output1 );
1279 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001280 psa_destroy_key( key_slot );
1281 mbedtls_psa_crypto_free( );
1282}
1283/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02001284
Gilles Peskine20035e32018-02-03 22:44:14 +01001285/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001286void aead_encrypt_decrypt( int key_type_arg,
1287 data_t * key_data,
1288 int alg_arg,
1289 data_t * input_data,
1290 data_t * nonce,
1291 data_t * additional_data,
1292 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001293{
1294 int slot = 1;
1295 psa_key_type_t key_type = key_type_arg;
1296 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001297 unsigned char *output_data = NULL;
1298 size_t output_size = 0;
1299 size_t output_length = 0;
1300 unsigned char *output_data2 = NULL;
1301 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001302 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001303 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001304 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001305
Gilles Peskinea1cac842018-06-11 19:33:02 +02001306 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001307 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001308 TEST_ASSERT( nonce != NULL );
1309 TEST_ASSERT( additional_data != NULL );
1310 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1311 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1312 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1313 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1314
Gilles Peskine4abf7412018-06-18 16:35:34 +02001315 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001316 output_data = mbedtls_calloc( 1, output_size );
1317 TEST_ASSERT( output_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001318
1319 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1320
1321 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001322 psa_key_policy_set_usage( &policy,
1323 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
1324 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001325 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1326
1327 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001328 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001329
1330 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001331 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001332 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001333 additional_data->len,
1334 input_data->x, input_data->len,
1335 output_data, output_size,
1336 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001337
1338 if( PSA_SUCCESS == expected_result )
1339 {
1340 output_data2 = mbedtls_calloc( 1, output_length );
1341 TEST_ASSERT( output_data2 != NULL );
1342
1343 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001344 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001345 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001346 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001347 output_data, output_length,
1348 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001349 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02001350
itayzafrir3e02b3b2018-06-12 17:06:52 +03001351 TEST_ASSERT( memcmp( input_data->x, output_data2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001352 input_data->len ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001353 }
Gilles Peskine2d277862018-06-18 15:41:12 +02001354
Gilles Peskinea1cac842018-06-11 19:33:02 +02001355exit:
1356 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001357 mbedtls_free( output_data );
1358 mbedtls_free( output_data2 );
1359 mbedtls_psa_crypto_free( );
1360}
1361/* END_CASE */
1362
1363/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001364void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001365 int alg_arg, data_t * input_data,
1366 data_t * additional_data, data_t * nonce,
1367 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001368{
1369 int slot = 1;
1370 psa_key_type_t key_type = key_type_arg;
1371 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001372 unsigned char *output_data = NULL;
1373 size_t output_size = 0;
1374 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001375 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001376 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001377
Gilles Peskinea1cac842018-06-11 19:33:02 +02001378 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001379 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001380 TEST_ASSERT( additional_data != NULL );
1381 TEST_ASSERT( nonce != NULL );
1382 TEST_ASSERT( expected_result != NULL );
1383 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1384 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1385 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1386 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1387 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
1388
Gilles Peskine4abf7412018-06-18 16:35:34 +02001389 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001390 output_data = mbedtls_calloc( 1, output_size );
1391 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001392
Gilles Peskinea1cac842018-06-11 19:33:02 +02001393 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1394
1395 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001396 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001397 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1398
1399 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001400 key_data->x,
1401 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001402
1403 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001404 nonce->x, nonce->len,
1405 additional_data->x, additional_data->len,
1406 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001407 output_data, output_size,
1408 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001409
itayzafrir3e02b3b2018-06-12 17:06:52 +03001410 TEST_ASSERT( memcmp( output_data, expected_result->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001411 output_length ) == 0 );
1412
Gilles Peskinea1cac842018-06-11 19:33:02 +02001413exit:
1414 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001415 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001416 mbedtls_psa_crypto_free( );
1417}
1418/* END_CASE */
1419
1420/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001421void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001422 int alg_arg, data_t * input_data,
1423 data_t * additional_data, data_t * nonce,
1424 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001425{
1426 int slot = 1;
1427 psa_key_type_t key_type = key_type_arg;
1428 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001429 unsigned char *output_data = NULL;
1430 size_t output_size = 0;
1431 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001432 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001433 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001434 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001435
Gilles Peskinea1cac842018-06-11 19:33:02 +02001436 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001437 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001438 TEST_ASSERT( additional_data != NULL );
1439 TEST_ASSERT( nonce != NULL );
1440 TEST_ASSERT( expected_data != NULL );
1441 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1442 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1443 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1444 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1445 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
1446
Gilles Peskine4abf7412018-06-18 16:35:34 +02001447 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001448 output_data = mbedtls_calloc( 1, output_size );
1449 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001450
Gilles Peskinea1cac842018-06-11 19:33:02 +02001451 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1452
1453 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001454 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001455 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1456
1457 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001458 key_data->x,
1459 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001460
1461 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001462 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001463 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001464 additional_data->len,
1465 input_data->x, input_data->len,
1466 output_data, output_size,
1467 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001468
Gilles Peskine2d277862018-06-18 15:41:12 +02001469 if( expected_result == PSA_SUCCESS )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001470 {
itayzafrir3e02b3b2018-06-12 17:06:52 +03001471 TEST_ASSERT( memcmp( output_data, expected_data->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001472 output_length ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001473 }
1474
Gilles Peskinea1cac842018-06-11 19:33:02 +02001475exit:
1476 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001477 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001478 mbedtls_psa_crypto_free( );
1479}
1480/* END_CASE */
1481
1482/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001483void signature_size( int type_arg,
1484 int bits,
1485 int alg_arg,
1486 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001487{
1488 psa_key_type_t type = type_arg;
1489 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02001490 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01001491 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
1492exit:
1493 ;
1494}
1495/* END_CASE */
1496
1497/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001498void sign_deterministic( int key_type_arg, data_t *key_data,
1499 int alg_arg, data_t *input_data,
1500 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001501{
1502 int slot = 1;
1503 psa_key_type_t key_type = key_type_arg;
1504 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01001505 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01001506 unsigned char *signature = NULL;
1507 size_t signature_size;
1508 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02001509 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01001510
Gilles Peskine20035e32018-02-03 22:44:14 +01001511 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001512 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001513 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001514 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1515 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1516 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01001517
1518 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1519
mohammad1603a97cb8c2018-03-28 03:46:26 -07001520 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001521 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001522 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1523
Gilles Peskine20035e32018-02-03 22:44:14 +01001524 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001525 key_data->x,
1526 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01001527 TEST_ASSERT( psa_get_key_information( slot,
1528 NULL,
1529 &key_bits ) == PSA_SUCCESS );
1530
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001531 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
1532 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01001533 TEST_ASSERT( signature_size != 0 );
1534 signature = mbedtls_calloc( 1, signature_size );
1535 TEST_ASSERT( signature != NULL );
1536
1537 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001538 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01001539 NULL, 0,
1540 signature, signature_size,
1541 &signature_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001542 TEST_ASSERT( signature_length == output_data->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001543 TEST_ASSERT( memcmp( signature, output_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001544 output_data->len ) == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01001545
1546exit:
1547 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01001548 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01001549 mbedtls_psa_crypto_free( );
1550}
1551/* END_CASE */
1552
1553/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001554void sign_fail( int key_type_arg, data_t *key_data,
1555 int alg_arg, data_t *input_data,
Gilles Peskine20035e32018-02-03 22:44:14 +01001556 int signature_size, int expected_status_arg )
1557{
1558 int slot = 1;
1559 psa_key_type_t key_type = key_type_arg;
1560 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01001561 psa_status_t actual_status;
1562 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01001563 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01001564 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02001565 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01001566
Gilles Peskine20035e32018-02-03 22:44:14 +01001567 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001568 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001569 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1570 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1571
Gilles Peskine20035e32018-02-03 22:44:14 +01001572 signature = mbedtls_calloc( 1, signature_size );
1573 TEST_ASSERT( signature != NULL );
1574
1575 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1576
mohammad1603a97cb8c2018-03-28 03:46:26 -07001577 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001578 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001579 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1580
Gilles Peskine20035e32018-02-03 22:44:14 +01001581 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001582 key_data->x,
1583 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01001584
1585 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001586 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01001587 NULL, 0,
1588 signature, signature_size,
1589 &signature_length );
1590 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine93aa0332018-02-03 23:58:03 +01001591 TEST_ASSERT( signature_length == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01001592
1593exit:
1594 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01001595 mbedtls_free( signature );
1596 mbedtls_psa_crypto_free( );
1597}
1598/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03001599
1600/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001601void asymmetric_verify( int key_type_arg, data_t *key_data,
1602 int alg_arg, data_t *hash_data,
1603 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03001604{
1605 int slot = 1;
1606 psa_key_type_t key_type = key_type_arg;
1607 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001608 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03001609
itayzafrir5c753392018-05-08 11:18:38 +03001610 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03001611 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03001612 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001613 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1614 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
1615 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03001616
1617 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1618
1619 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001620 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03001621 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1622
1623 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001624 key_data->x,
1625 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03001626
1627 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001628 hash_data->x, hash_data->len,
itayzafrir5c753392018-05-08 11:18:38 +03001629 NULL, 0,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001630 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001631 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03001632exit:
1633 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03001634 mbedtls_psa_crypto_free( );
1635}
1636/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001637
1638/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001639void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
1640 int alg_arg, data_t *hash_data,
1641 data_t *signature_data,
1642 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001643{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001644 int slot = 1;
1645 psa_key_type_t key_type = key_type_arg;
1646 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001647 psa_status_t actual_status;
1648 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001649 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001650
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001651 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001652 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001653 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001654 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1655 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
1656 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001657
1658 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1659
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001660 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001661 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001662 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1663
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001664 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001665 key_data->x,
1666 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001667
1668 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001669 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001670 NULL, 0,
1671 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001672 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001673
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001674 TEST_ASSERT( actual_status == expected_status );
1675
1676exit:
1677 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001678 mbedtls_psa_crypto_free( );
1679}
1680/* END_CASE */
1681
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001682/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001683void asymmetric_encrypt_decrypt( int key_type_arg, data_t *key_data,
1684 int alg_arg, data_t *input_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001685{
1686 int slot = 1;
1687 psa_key_type_t key_type = key_type_arg;
1688 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001689 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001690 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001691 size_t output_length = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001692 unsigned char *output2 = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001693 size_t output2_size = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001694 size_t output2_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02001695 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001696
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001697 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001698 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001699 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1700 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1701
Gilles Peskine4abf7412018-06-18 16:35:34 +02001702 output_size = key_data->len;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001703 output2_size = output_size;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001704 output = mbedtls_calloc( 1, output_size );
1705 TEST_ASSERT( output != NULL );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001706 output2 = mbedtls_calloc( 1, output2_size );
1707 TEST_ASSERT( output2 != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001708
1709 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1710
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001711 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001712 psa_key_policy_set_usage( &policy,
1713 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001714 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001715 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1716
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001717 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001718 key_data->x,
1719 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001720
Gilles Peskineeebd7382018-06-08 18:11:54 +02001721 /* We test encryption by checking that encrypt-then-decrypt gives back
1722 * the original plaintext because of the non-optional random
1723 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02001724 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001725 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001726 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001727 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001728 &output_length ) == PSA_SUCCESS );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001729
Gilles Peskine2d277862018-06-18 15:41:12 +02001730 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001731 output, output_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001732 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001733 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001734 &output2_length ) == PSA_SUCCESS );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001735 TEST_ASSERT( memcmp( input_data->x, output2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001736 input_data->len ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001737
1738exit:
1739 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001740 mbedtls_free( output );
1741 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001742 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001743}
1744/* END_CASE */
1745
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001746/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001747void asymmetric_encrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001748 int alg_arg, data_t *input_data,
1749 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001750{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001751 int slot = 1;
1752 psa_key_type_t key_type = key_type_arg;
1753 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001754 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001755 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001756 size_t output_length = 0;
1757 psa_status_t actual_status;
1758 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001759 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001760
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001761 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001762 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001763 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1764 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1765
Gilles Peskine4abf7412018-06-18 16:35:34 +02001766 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001767 output = mbedtls_calloc( 1, output_size );
1768 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001769
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001770 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1771
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001772 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001773 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001774 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1775
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001776 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001777 key_data->x,
1778 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001779
Gilles Peskine2d277862018-06-18 15:41:12 +02001780 actual_status = psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001781 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001782 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001783 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001784 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001785 TEST_ASSERT( actual_status == expected_status );
1786
1787exit:
1788 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001789 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001790 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001791}
1792/* END_CASE */
1793
1794/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001795void asymmetric_decrypt( int key_type_arg, data_t *key_data,
1796 int alg_arg, data_t *input_data,
1797 data_t *expected_data, int expected_size )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001798{
1799 int slot = 1;
1800 psa_key_type_t key_type = key_type_arg;
1801 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001802 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001803 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001804 size_t output_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02001805 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001806
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001807 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001808 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001809 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001810 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1811 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1812 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
1813
Gilles Peskine4abf7412018-06-18 16:35:34 +02001814 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001815 output = mbedtls_calloc( 1, output_size );
1816 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001817
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001818 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1819
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001820 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001821 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001822 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1823
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001824 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001825 key_data->x,
1826 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001827
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001828 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001829 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001830 NULL, 0,
1831 output,
1832 output_size,
1833 &output_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001834 TEST_ASSERT( (size_t) expected_size == output_length );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001835 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001836
1837exit:
1838 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001839 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001840 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001841}
1842/* END_CASE */
1843
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001844/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001845void asymmetric_decrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001846 int alg_arg, data_t *input_data,
1847 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001848{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001849 int slot = 1;
1850 psa_key_type_t key_type = key_type_arg;
1851 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001852 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001853 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001854 size_t output_length = 0;
1855 psa_status_t actual_status;
1856 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001857 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001858
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001859 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001860 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001861 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1862 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1863
Gilles Peskine4abf7412018-06-18 16:35:34 +02001864 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001865 output = mbedtls_calloc( 1, output_size );
1866 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001867
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001868 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1869
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001870 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001871 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001872 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1873
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001874 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001875 key_data->x,
1876 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001877
Gilles Peskine2d277862018-06-18 15:41:12 +02001878 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001879 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001880 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001881 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001882 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001883 TEST_ASSERT( actual_status == expected_status );
1884
1885exit:
1886 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02001887 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001888 mbedtls_psa_crypto_free( );
1889}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001890/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02001891
1892/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02001893void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02001894{
Gilles Peskinea50d7392018-06-21 10:22:13 +02001895 size_t bytes = bytes_arg;
1896 const unsigned char trail[] = "don't overwrite me";
1897 unsigned char *output = mbedtls_calloc( 1, bytes + sizeof( trail ) );
1898 unsigned char *changed = mbedtls_calloc( 1, bytes );
1899 size_t i;
1900 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02001901
Gilles Peskinea50d7392018-06-21 10:22:13 +02001902 TEST_ASSERT( output != NULL );
1903 TEST_ASSERT( changed != NULL );
1904 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02001905
1906 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1907
Gilles Peskinea50d7392018-06-21 10:22:13 +02001908 /* Run several times, to ensure that every output byte will be
1909 * nonzero at least once with overwhelming probability
1910 * (2^(-8*number_of_runs)). */
1911 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02001912 {
Gilles Peskinea50d7392018-06-21 10:22:13 +02001913 memset( output, 0, bytes );
1914 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
1915
1916 /* Check that no more than bytes have been overwritten */
1917 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
1918
1919 for( i = 0; i < bytes; i++ )
1920 {
1921 if( output[i] != 0 )
1922 ++changed[i];
1923 }
Gilles Peskine05d69892018-06-19 22:00:52 +02001924 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02001925
1926 /* Check that every byte was changed to nonzero at least once. This
1927 * validates that psa_generate_random is overwriting every byte of
1928 * the output buffer. */
1929 for( i = 0; i < bytes; i++ )
1930 {
1931 TEST_ASSERT( changed[i] != 0 );
1932 }
Gilles Peskine05d69892018-06-19 22:00:52 +02001933
1934exit:
1935 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02001936 mbedtls_free( output );
1937 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02001938}
1939/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02001940
1941/* BEGIN_CASE */
1942void generate_key( int type_arg,
1943 int bits_arg,
1944 int usage_arg,
1945 int alg_arg,
1946 int expected_status_arg )
1947{
1948 int slot = 1;
1949 psa_key_type_t type = type_arg;
1950 psa_key_usage_t usage = usage_arg;
1951 size_t bits = bits_arg;
1952 psa_algorithm_t alg = alg_arg;
1953 psa_status_t expected_status = expected_status_arg;
1954 psa_key_type_t got_type;
1955 size_t got_bits;
1956 unsigned char exported[616] = {0}; /* enough for a 1024-bit RSA key */
1957 size_t exported_length;
1958 psa_status_t expected_export_status =
1959 usage & PSA_KEY_USAGE_EXPORT ? PSA_SUCCESS : PSA_ERROR_NOT_PERMITTED;
1960 psa_status_t expected_info_status =
1961 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
1962 psa_key_policy_t policy;
1963
1964 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1965
1966 psa_key_policy_init( &policy );
1967 psa_key_policy_set_usage( &policy, usage, alg );
1968 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1969
1970 /* Generate a key */
1971 TEST_ASSERT( psa_generate_key( slot, type, bits,
1972 NULL, 0 ) == expected_status );
1973
1974 /* Test the key information */
1975 TEST_ASSERT( psa_get_key_information( slot,
1976 &got_type,
1977 &got_bits ) == expected_info_status );
1978 if( expected_info_status != PSA_SUCCESS )
1979 goto exit;
1980 TEST_ASSERT( got_type == type );
1981 TEST_ASSERT( got_bits == bits );
1982
1983 /* Export the key */
1984 TEST_ASSERT( psa_export_key( slot,
1985 exported, sizeof( exported ),
1986 &exported_length ) == expected_export_status );
1987 if( expected_export_status == PSA_SUCCESS )
1988 {
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001989 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02001990 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
1991#if defined(MBEDTLS_DES_C)
1992 if( type == PSA_KEY_TYPE_DES )
1993 {
1994 /* Check the parity bits. */
1995 unsigned i;
1996 for( i = 0; i < bits / 8; i++ )
1997 {
1998 unsigned bit_count = 0;
1999 unsigned m;
2000 for( m = 1; m <= 0x100; m <<= 1 )
2001 {
2002 if( exported[i] & m )
2003 ++bit_count;
2004 }
2005 TEST_ASSERT( bit_count % 2 != 0 );
2006 }
2007 }
2008#endif
2009#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
2010 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
2011 {
2012 /* Sanity check: does this look like the beginning of a PKCS#8
2013 * RSA key pair? Assumes bits is a multiple of 8. */
2014 size_t n_bytes = bits / 8 + 1;
2015 size_t n_encoded_bytes;
2016 unsigned char *n_end;
2017 TEST_ASSERT( exported_length >= 7 + ( n_bytes + 3 ) * 9 / 2 );
2018 TEST_ASSERT( exported[0] == 0x30 );
2019 TEST_ASSERT( exported[1] == 0x82 ); // assumes >=416-bit key
2020 TEST_ASSERT( exported[4] == 0x02 );
2021 TEST_ASSERT( exported[5] == 0x01 );
2022 TEST_ASSERT( exported[6] == 0x00 );
2023 TEST_ASSERT( exported[7] == 0x02 );
2024 n_encoded_bytes = exported[8];
2025 n_end = exported + 9 + n_encoded_bytes;
2026 if( n_encoded_bytes & 0x80 )
2027 {
2028 n_encoded_bytes = ( n_encoded_bytes & 0x7f ) << 7;
2029 n_encoded_bytes |= exported[9] & 0x7f;
2030 n_end += 1;
2031 }
2032 /* The encoding of n should start with a 0 byte since it should
2033 * have its high bit set. However Mbed TLS is not compliant and
2034 * generates an invalid, but widely tolerated, encoding of
2035 * positive INTEGERs with a bit size that is a multiple of 8
2036 * with no leading 0 byte. Accept this here. */
2037 TEST_ASSERT( n_bytes == n_encoded_bytes ||
2038 n_bytes == n_encoded_bytes + 1 );
2039 if( n_bytes == n_encoded_bytes )
2040 TEST_ASSERT( exported[n_encoded_bytes <= 127 ? 9 : 10] == 0x00 );
2041 /* Sanity check: e must be 3 */
2042 TEST_ASSERT( n_end[0] == 0x02 );
2043 TEST_ASSERT( n_end[1] == 0x03 );
2044 TEST_ASSERT( n_end[2] == 0x01 );
2045 TEST_ASSERT( n_end[3] == 0x00 );
2046 TEST_ASSERT( n_end[4] == 0x01 );
2047 TEST_ASSERT( n_end[5] == 0x02 );
2048 }
2049#endif /* MBEDTLS_RSA_C */
2050#if defined(MBEDTLS_ECP_C)
2051 if( PSA_KEY_TYPE_IS_ECC( type ) )
2052 {
2053 /* Sanity check: does this look like the beginning of a PKCS#8
2054 * elliptic curve key pair? */
2055 TEST_ASSERT( exported_length >= bits * 3 / 8 + 10 );
2056 TEST_ASSERT( exported[0] == 0x30 );
2057 }
2058#endif /* MBEDTLS_ECP_C */
2059 }
2060
Gilles Peskine818ca122018-06-20 18:16:48 +02002061 /* Do something with the key according to its type and permitted usage. */
2062 if( PSA_ALG_IS_MAC( alg ) )
2063 exercise_mac_key( slot, usage, alg );
2064 else if( PSA_ALG_IS_CIPHER( alg ) )
2065 exercise_cipher_key( slot, usage, alg );
2066 else if( PSA_ALG_IS_AEAD( alg ) )
2067 exercise_aead_key( slot, usage, alg );
2068 else if( PSA_ALG_IS_SIGN( alg ) )
2069 exercise_signature_key( slot, usage, alg );
2070 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
2071 exercise_asymmetric_encryption_key( slot, usage, alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02002072
2073exit:
2074 psa_destroy_key( slot );
2075 mbedtls_psa_crypto_free( );
2076}
2077/* END_CASE */