blob: c6413822367cdb95b0ef6dcf93de383d7696e699 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
Gilles Peskinee59236f2018-01-27 23:32:46 +01003#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +03004
5#if(UINT32_MAX > SIZE_MAX)
Gilles Peskine2d277862018-06-18 15:41:12 +02006#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX )
itayzafrir3e02b3b2018-06-12 17:06:52 +03007#else
Gilles Peskine2d277862018-06-18 15:41:12 +02008#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1
itayzafrir3e02b3b2018-06-12 17:06:52 +03009#endif
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020010
11/** Test if a buffer is not all-bits zero.
12 *
13 * \param buffer Pointer to the beginning of the buffer.
14 * \param size Size of the buffer in bytes.
15 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020016 * \return 1 if the buffer is all-bits-zero.
17 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020018 */
Gilles Peskine3f669c32018-06-21 09:21:51 +020019static int mem_is_zero( void *buffer, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020020{
21 size_t i;
22 for( i = 0; i < size; i++ )
23 {
24 if( ( (unsigned char *) buffer )[i] != 0 )
Gilles Peskine3f669c32018-06-21 09:21:51 +020025 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020026 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020027 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028}
Gilles Peskine818ca122018-06-20 18:16:48 +020029
30static int exercise_mac_key( psa_key_slot_t key,
31 psa_key_usage_t usage,
32 psa_algorithm_t alg )
33{
34 psa_mac_operation_t operation;
35 const unsigned char input[] = "foo";
36 unsigned char mac[64] = {0};
37 size_t mac_length = sizeof( mac );
38
39 if( usage & PSA_KEY_USAGE_SIGN )
40 {
41 TEST_ASSERT( psa_mac_start( &operation, key, alg ) == PSA_SUCCESS );
42 TEST_ASSERT( psa_mac_update( &operation,
43 input, sizeof( input ) ) == PSA_SUCCESS );
44 TEST_ASSERT( psa_mac_finish( &operation,
45 mac, sizeof( input ),
46 &mac_length ) == PSA_SUCCESS );
47 }
48
49 if( usage & PSA_KEY_USAGE_VERIFY )
50 {
51 psa_status_t verify_status =
52 ( usage & PSA_KEY_USAGE_SIGN ?
53 PSA_SUCCESS :
54 PSA_ERROR_INVALID_SIGNATURE );
55 TEST_ASSERT( psa_mac_start( &operation, key, alg ) == PSA_SUCCESS );
56 TEST_ASSERT( psa_mac_update( &operation,
57 input, sizeof( input ) ) == PSA_SUCCESS );
58 TEST_ASSERT( psa_mac_verify( &operation, mac, mac_length ) == verify_status );
59 }
60
61 return( 1 );
62
63exit:
64 psa_mac_abort( &operation );
65 return( 0 );
66}
67
68static int exercise_cipher_key( psa_key_slot_t key,
69 psa_key_usage_t usage,
70 psa_algorithm_t alg )
71{
72 psa_cipher_operation_t operation;
73 unsigned char iv[16] = {0};
74 size_t iv_length = sizeof( iv );
75 const unsigned char plaintext[16] = "Hello, world...";
76 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
77 size_t ciphertext_length = sizeof( ciphertext );
78 unsigned char decrypted[sizeof( ciphertext )];
79 size_t part_length;
80
81 if( usage & PSA_KEY_USAGE_ENCRYPT )
82 {
83 TEST_ASSERT( psa_encrypt_setup( &operation, key, alg ) == PSA_SUCCESS );
84 TEST_ASSERT( psa_encrypt_generate_iv( &operation,
85 iv, sizeof( iv ),
86 &iv_length ) == PSA_SUCCESS );
87 TEST_ASSERT( psa_cipher_update( &operation,
88 plaintext, sizeof( plaintext ),
89 ciphertext, sizeof( ciphertext ),
90 &ciphertext_length ) == PSA_SUCCESS );
91 TEST_ASSERT( psa_cipher_finish( &operation,
92 ciphertext + ciphertext_length,
93 sizeof( ciphertext ) - ciphertext_length,
94 &part_length ) == PSA_SUCCESS );
95 ciphertext_length += part_length;
96 }
97
98 if( usage & PSA_KEY_USAGE_DECRYPT )
99 {
100 psa_status_t status;
101 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
102 {
103 psa_key_type_t type;
104 size_t bits;
105 TEST_ASSERT( psa_get_key_information( key, &type, &bits ) );
106 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
107 }
108 TEST_ASSERT( psa_decrypt_setup( &operation, key, alg ) == PSA_SUCCESS );
109 TEST_ASSERT( psa_encrypt_set_iv( &operation,
110 iv, iv_length ) == PSA_SUCCESS );
111 TEST_ASSERT( psa_cipher_update( &operation,
112 ciphertext, ciphertext_length,
113 decrypted, sizeof( decrypted ),
114 &part_length ) == PSA_SUCCESS );
115 status = psa_cipher_finish( &operation,
116 decrypted + part_length,
117 sizeof( decrypted ) - part_length,
118 &part_length );
119 /* For a stream cipher, all inputs are valid. For a block cipher,
120 * if the input is some aribtrary data rather than an actual
121 ciphertext, a padding error is likely. */
122 if( ( usage & PSA_KEY_USAGE_DECRYPT ) ||
123 PSA_BLOCK_CIPHER_BLOCK_SIZE( alg ) == 1 )
124 TEST_ASSERT( status == PSA_SUCCESS );
125 else
126 TEST_ASSERT( status == PSA_SUCCESS ||
127 status == PSA_ERROR_INVALID_PADDING );
128 }
129
130 return( 1 );
131
132exit:
133 psa_cipher_abort( &operation );
134 return( 0 );
135}
136
137static int exercise_aead_key( psa_key_slot_t key,
138 psa_key_usage_t usage,
139 psa_algorithm_t alg )
140{
141 unsigned char nonce[16] = {0};
142 size_t nonce_length = sizeof( nonce );
143 unsigned char plaintext[16] = "Hello, world...";
144 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
145 size_t ciphertext_length = sizeof( ciphertext );
146 size_t plaintext_length = sizeof( ciphertext );
147
148 if( usage & PSA_KEY_USAGE_ENCRYPT )
149 {
150 TEST_ASSERT( psa_aead_encrypt( key, alg,
151 nonce, nonce_length,
152 NULL, 0,
153 plaintext, sizeof( plaintext ),
154 ciphertext, sizeof( ciphertext ),
155 &ciphertext_length ) == PSA_SUCCESS );
156 }
157
158 if( usage & PSA_KEY_USAGE_DECRYPT )
159 {
160 psa_status_t verify_status =
161 ( usage & PSA_KEY_USAGE_ENCRYPT ?
162 PSA_SUCCESS :
163 PSA_ERROR_INVALID_SIGNATURE );
164 TEST_ASSERT( psa_aead_decrypt( key, alg,
165 nonce, nonce_length,
166 NULL, 0,
167 ciphertext, ciphertext_length,
168 plaintext, sizeof( plaintext ),
169 &plaintext_length ) == verify_status );
170 }
171
172 return( 1 );
173
174exit:
175 return( 0 );
176}
177
178static int exercise_signature_key( psa_key_slot_t key,
179 psa_key_usage_t usage,
180 psa_algorithm_t alg )
181{
182 unsigned char payload[16] = {0};
183 size_t payload_length = sizeof( payload );
184 unsigned char signature[256] = {0};
185 size_t signature_length = sizeof( signature );
186
187 if( usage & PSA_KEY_USAGE_SIGN )
188 {
189 TEST_ASSERT( psa_asymmetric_sign( key, alg,
190 payload, payload_length,
191 NULL, 0,
192 signature, sizeof( signature ),
193 &signature_length ) == PSA_SUCCESS );
194 }
195
196 if( usage & PSA_KEY_USAGE_VERIFY )
197 {
198 psa_status_t verify_status =
199 ( usage & PSA_KEY_USAGE_SIGN ?
200 PSA_SUCCESS :
201 PSA_ERROR_INVALID_SIGNATURE );
202 TEST_ASSERT( psa_asymmetric_verify( key, alg,
203 payload, payload_length,
204 NULL, 0,
205 signature, signature_length ) ==
206 verify_status );
207 }
208
209 return( 1 );
210
211exit:
212 return( 0 );
213}
214
215static int exercise_asymmetric_encryption_key( psa_key_slot_t key,
216 psa_key_usage_t usage,
217 psa_algorithm_t alg )
218{
219 unsigned char plaintext[256] = "Hello, world...";
220 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
221 size_t ciphertext_length = sizeof( ciphertext );
222 size_t plaintext_length = 16;
223
224 if( usage & PSA_KEY_USAGE_ENCRYPT )
225 {
226 TEST_ASSERT(
227 psa_asymmetric_encrypt( key, alg,
228 plaintext, plaintext_length,
229 NULL, 0,
230 ciphertext, sizeof( ciphertext ),
231 &ciphertext_length ) == PSA_SUCCESS );
232 }
233
234 if( usage & PSA_KEY_USAGE_DECRYPT )
235 {
236 psa_status_t status =
237 psa_asymmetric_decrypt( key, alg,
238 ciphertext, ciphertext_length,
239 NULL, 0,
240 plaintext, sizeof( plaintext ),
241 &plaintext_length );
242 TEST_ASSERT( status == PSA_SUCCESS ||
243 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
244 ( status == PSA_ERROR_INVALID_ARGUMENT ||
245 status == PSA_ERROR_INVALID_PADDING ) ) );
246 }
247
248 return( 1 );
249
250exit:
251 return( 0 );
252}
Gilles Peskinee59236f2018-01-27 23:32:46 +0100253/* END_HEADER */
254
255/* BEGIN_DEPENDENCIES
256 * depends_on:MBEDTLS_PSA_CRYPTO_C
257 * END_DEPENDENCIES
258 */
259
260/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200261void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100262{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100263 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100264 int i;
265 for( i = 0; i <= 1; i++ )
266 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100267 status = psa_crypto_init( );
268 TEST_ASSERT( status == PSA_SUCCESS );
269 status = psa_crypto_init( );
270 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100271 mbedtls_psa_crypto_free( );
272 }
273}
274/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100275
276/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200277void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100278{
279 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200280 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100281 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100282
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100283 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300284 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100285 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
286
Gilles Peskine4abf7412018-06-18 16:35:34 +0200287 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200288 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100289 if( status == PSA_SUCCESS )
290 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
291
292exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100293 mbedtls_psa_crypto_free( );
294}
295/* END_CASE */
296
297/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300298void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300299 int type_arg,
300 int alg_arg,
301 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100302 int expected_bits,
303 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200304 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100305 int canonical_input )
306{
307 int slot = 1;
308 int slot2 = slot + 1;
309 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200310 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200311 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100312 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100313 unsigned char *exported = NULL;
314 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100315 size_t export_size;
316 size_t exported_length;
317 size_t reexported_length;
318 psa_key_type_t got_type;
319 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200320 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100321
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100322 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300323 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
324 export_size = (ssize_t) data->len + export_size_delta;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100325 exported = mbedtls_calloc( 1, export_size );
326 TEST_ASSERT( exported != NULL );
327 if( ! canonical_input )
328 {
329 reexported = mbedtls_calloc( 1, export_size );
330 TEST_ASSERT( reexported != NULL );
331 }
332 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
333
mohammad1603a97cb8c2018-03-28 03:46:26 -0700334 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200335 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700336 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
337
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100338 /* Import the key */
339 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200340 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100341
342 /* Test the key information */
343 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200344 &got_type,
345 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100346 TEST_ASSERT( got_type == type );
347 TEST_ASSERT( got_bits == (size_t) expected_bits );
348
349 /* Export the key */
350 status = psa_export_key( slot,
351 exported, export_size,
352 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200353 TEST_ASSERT( status == expected_export_status );
Gilles Peskine3f669c32018-06-21 09:21:51 +0200354 TEST_ASSERT( mem_is_zero( exported + exported_length,
355 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100356 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200357 {
358 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100359 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200360 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100361
362 if( canonical_input )
363 {
Gilles Peskine4abf7412018-06-18 16:35:34 +0200364 TEST_ASSERT( exported_length == data->len );
365 TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100366 }
367 else
368 {
mohammad1603a97cb8c2018-03-28 03:46:26 -0700369 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
370
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100371 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200372 exported,
373 export_size ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100374 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200375 reexported,
376 export_size,
377 &reexported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100378 TEST_ASSERT( reexported_length == exported_length );
379 TEST_ASSERT( memcmp( reexported, exported,
380 exported_length ) == 0 );
381 }
382
383destroy:
384 /* Destroy the key */
385 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
386 TEST_ASSERT( psa_get_key_information(
387 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
388
389exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300390 mbedtls_free( exported );
391 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100392 mbedtls_psa_crypto_free( );
393}
394/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100395
Moran Pekerf709f4a2018-06-06 17:26:04 +0300396/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300397void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200398 int type_arg,
399 int alg_arg,
400 int expected_bits,
401 int public_key_expected_length,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200402 int expected_export_status_arg )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300403{
404 int slot = 1;
405 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200406 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200407 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300408 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300409 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300410 size_t export_size;
411 size_t exported_length;
412 psa_key_type_t got_type;
413 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200414 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300415
Moran Pekerf709f4a2018-06-06 17:26:04 +0300416 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300417 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
418 export_size = (ssize_t) data->len;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300419 exported = mbedtls_calloc( 1, export_size );
420 TEST_ASSERT( exported != NULL );
421
422 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
423
424 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200425 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300426 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
427
428 /* Import the key */
429 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200430 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300431
432 /* Test the key information */
433 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200434 &got_type,
435 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300436 TEST_ASSERT( got_type == type );
437 TEST_ASSERT( got_bits == (size_t) expected_bits );
438
439 /* Export the key */
440 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200441 exported, export_size,
442 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200443 TEST_ASSERT( status == expected_export_status );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300444 if( status != PSA_SUCCESS )
445 goto destroy;
446
447 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
448
449destroy:
450 /* Destroy the key */
451 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
452 TEST_ASSERT( psa_get_key_information(
453 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
454
455exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300456 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300457 mbedtls_psa_crypto_free( );
458}
459/* END_CASE */
460
Gilles Peskine20035e32018-02-03 22:44:14 +0100461/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +0200462void key_policy( int usage_arg, int alg_arg )
463{
464 int key_slot = 1;
465 psa_algorithm_t alg = alg_arg;
466 psa_key_usage_t usage = usage_arg;
467 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
468 unsigned char key[32] = {0};
469 psa_key_policy_t policy_set;
470 psa_key_policy_t policy_get;
471
472 memset( key, 0x2a, sizeof( key ) );
473
474 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
475
476 psa_key_policy_init( &policy_set );
477 psa_key_policy_init( &policy_get );
478
479 psa_key_policy_set_usage( &policy_set, usage, alg );
480
481 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
482 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
483 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
484
485 TEST_ASSERT( psa_import_key( key_slot, key_type,
486 key, sizeof( key ) ) == PSA_SUCCESS );
487
488 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
489
490 TEST_ASSERT( policy_get.usage == policy_set.usage );
491 TEST_ASSERT( policy_get.alg == policy_set.alg );
492
493exit:
494 psa_destroy_key( key_slot );
495 mbedtls_psa_crypto_free( );
496}
497/* END_CASE */
498
499/* BEGIN_CASE */
500void key_policy_fail( int usage_arg, int alg_arg, int expected_status,
501 data_t *keypair )
502{
503 int key_slot = 1;
504 psa_algorithm_t alg = alg_arg;
505 psa_key_usage_t usage = usage_arg;
506 size_t signature_length = 0;
507 psa_key_policy_t policy;
508 int actual_status = PSA_SUCCESS;
509
510 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
511
512 psa_key_policy_init( &policy );
513 psa_key_policy_set_usage( &policy, usage, alg );
514 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
515
516 if( usage & PSA_KEY_USAGE_EXPORT )
517 {
518 TEST_ASSERT( keypair != NULL );
519 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) );
520 TEST_ASSERT( psa_import_key( key_slot,
521 PSA_KEY_TYPE_RSA_KEYPAIR,
522 keypair->x,
523 keypair->len ) == PSA_SUCCESS );
524 actual_status = psa_asymmetric_sign( key_slot, alg,
525 NULL, 0,
526 NULL, 0,
527 NULL, 0, &signature_length );
528 }
529
530 if( usage & PSA_KEY_USAGE_SIGN )
531 {
532 TEST_ASSERT( keypair != NULL );
533 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) );
534 TEST_ASSERT( psa_import_key( key_slot,
535 PSA_KEY_TYPE_RSA_KEYPAIR,
536 keypair->x,
537 keypair->len ) == PSA_SUCCESS );
538 actual_status = psa_export_key( key_slot, NULL, 0, NULL );
539 }
540
541 TEST_ASSERT( actual_status == expected_status );
542
543exit:
544 psa_destroy_key( key_slot );
545 mbedtls_psa_crypto_free( );
546}
547/* END_CASE */
548
549/* BEGIN_CASE */
550void key_lifetime( int lifetime_arg )
551{
552 int key_slot = 1;
553 psa_key_type_t key_type = PSA_ALG_CBC_BASE;
554 unsigned char key[32] = {0};
555 psa_key_lifetime_t lifetime_set = lifetime_arg;
556 psa_key_lifetime_t lifetime_get;
557
558 memset( key, 0x2a, sizeof( key ) );
559
560 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
561
562 TEST_ASSERT( psa_set_key_lifetime( key_slot,
563 lifetime_set ) == PSA_SUCCESS );
564
565 TEST_ASSERT( psa_import_key( key_slot, key_type,
566 key, sizeof( key ) ) == PSA_SUCCESS );
567
568 TEST_ASSERT( psa_get_key_lifetime( key_slot,
569 &lifetime_get ) == PSA_SUCCESS );
570
571 TEST_ASSERT( lifetime_get == lifetime_set );
572
573exit:
574 psa_destroy_key( key_slot );
575 mbedtls_psa_crypto_free( );
576}
577/* END_CASE */
578
579/* BEGIN_CASE */
580void key_lifetime_set_fail( int key_slot_arg,
581 int lifetime_arg,
582 int expected_status_arg )
583{
584 psa_key_slot_t key_slot = key_slot_arg;
585 psa_key_lifetime_t lifetime_set = lifetime_arg;
586 psa_status_t actual_status;
587 psa_status_t expected_status = expected_status_arg;
588
589 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
590
591 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
592
593 if( actual_status == PSA_SUCCESS )
594 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
595
596 TEST_ASSERT( expected_status == actual_status );
597
598exit:
599 psa_destroy_key( key_slot );
600 mbedtls_psa_crypto_free( );
601}
602/* END_CASE */
603
604/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200605void hash_setup( int alg_arg,
606 int expected_status_arg )
607{
608 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200609 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200610 psa_hash_operation_t operation;
611 psa_status_t status;
612
613 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
614
615 status = psa_hash_start( &operation, alg );
616 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200617 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200618
619exit:
620 mbedtls_psa_crypto_free( );
621}
622/* END_CASE */
623
624/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300625void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100626{
627 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +0200628 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100629 size_t actual_hash_length;
630 psa_hash_operation_t operation;
631
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100632 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300633 TEST_ASSERT( expected_hash != NULL );
634 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
635 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100636
637 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
638
639 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
640 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200641 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100642 TEST_ASSERT( psa_hash_finish( &operation,
643 actual_hash, sizeof( actual_hash ),
644 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200645 TEST_ASSERT( actual_hash_length == expected_hash->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300646 TEST_ASSERT( memcmp( expected_hash->x, actual_hash,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200647 expected_hash->len ) == 0 );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100648
649exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100650 mbedtls_psa_crypto_free( );
651}
652/* END_CASE */
653
654/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300655void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100656{
657 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100658 psa_hash_operation_t operation;
659
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100660 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300661 TEST_ASSERT( expected_hash != NULL );
662 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
663 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100664
665 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
666
667 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
668 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200669 input->x,
670 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100671 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300672 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200673 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100674
675exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100676 mbedtls_psa_crypto_free( );
677}
678/* END_CASE */
679
680/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200681void mac_setup( int key_type_arg,
682 data_t *key,
683 int alg_arg,
684 int expected_status_arg )
685{
686 int key_slot = 1;
687 psa_key_type_t key_type = key_type_arg;
688 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200689 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200690 psa_mac_operation_t operation;
691 psa_key_policy_t policy;
692 psa_status_t status;
693
694 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
695
696 psa_key_policy_init( &policy );
697 psa_key_policy_set_usage( &policy,
698 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
699 alg );
700 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
701
702 TEST_ASSERT( psa_import_key( key_slot, key_type,
703 key->x, key->len ) == PSA_SUCCESS );
704
705 status = psa_mac_start( &operation, key_slot, alg );
706 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200707 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200708
709exit:
710 psa_destroy_key( key_slot );
711 mbedtls_psa_crypto_free( );
712}
713/* END_CASE */
714
715/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +0200716void mac_verify( int key_type_arg,
717 data_t *key,
718 int alg_arg,
719 data_t *input,
720 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +0100721{
722 int key_slot = 1;
723 psa_key_type_t key_type = key_type_arg;
724 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100725 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -0700726 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100727
Gilles Peskine8c9def32018-02-08 10:02:12 +0100728 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100729 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100730 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300731 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300732 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
733 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100734
735 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
736
mohammad16036df908f2018-04-02 08:34:15 -0700737 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200738 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -0700739 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
740
Gilles Peskine8c9def32018-02-08 10:02:12 +0100741 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200742 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +0200743
Gilles Peskine8c9def32018-02-08 10:02:12 +0100744 TEST_ASSERT( psa_mac_start( &operation, key_slot, alg ) == PSA_SUCCESS );
745 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
746 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200747 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100748 TEST_ASSERT( psa_mac_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300749 expected_mac->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200750 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100751
752exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +0100753 psa_destroy_key( key_slot );
754 mbedtls_psa_crypto_free( );
755}
756/* END_CASE */
757
758/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200759void cipher_setup( int key_type_arg,
760 data_t *key,
761 int alg_arg,
762 int expected_status_arg )
763{
764 int key_slot = 1;
765 psa_key_type_t key_type = key_type_arg;
766 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200767 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200768 psa_cipher_operation_t operation;
769 psa_key_policy_t policy;
770 psa_status_t status;
771
772 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
773
774 psa_key_policy_init( &policy );
775 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
776 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
777
778 TEST_ASSERT( psa_import_key( key_slot, key_type,
779 key->x, key->len ) == PSA_SUCCESS );
780
781 status = psa_encrypt_setup( &operation, key_slot, alg );
782 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200783 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200784
785exit:
786 psa_destroy_key( key_slot );
787 mbedtls_psa_crypto_free( );
788}
789/* END_CASE */
790
791/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +0200792void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300793 data_t *key,
794 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200795 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +0200796{
797 int key_slot = 1;
798 psa_status_t status;
799 psa_key_type_t key_type = key_type_arg;
800 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200801 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200802 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200803 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300804 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200805 size_t output_buffer_size = 0;
806 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200807 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200808 psa_cipher_operation_t operation;
809
Gilles Peskine50e586b2018-06-08 14:28:46 +0200810 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200811 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200812 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300813 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
814 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
815 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200816
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200817 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
818 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200819
820 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
821
822 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200823 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200824
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200825 TEST_ASSERT( psa_encrypt_setup( &operation,
826 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200827
828 TEST_ASSERT( psa_encrypt_set_iv( &operation,
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200829 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200830 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200831 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300832 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200833
Gilles Peskine4abf7412018-06-18 16:35:34 +0200834 TEST_ASSERT( psa_cipher_update( &operation,
835 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200836 output, output_buffer_size,
837 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200838 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200839 status = psa_cipher_finish( &operation,
840 output + function_output_length,
841 output_buffer_size,
842 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200843 total_output_length += function_output_length;
844
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200845 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200846 if( expected_status == PSA_SUCCESS )
847 {
848 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200849 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300850 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200851 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200852 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200853
Gilles Peskine50e586b2018-06-08 14:28:46 +0200854exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300855 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200856 psa_destroy_key( key_slot );
857 mbedtls_psa_crypto_free( );
858}
859/* END_CASE */
860
861/* BEGIN_CASE */
862void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300863 data_t *key,
864 data_t *input,
865 int first_part_size,
866 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +0200867{
868 int key_slot = 1;
869 psa_key_type_t key_type = key_type_arg;
870 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200871 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200872 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300873 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200874 size_t output_buffer_size = 0;
875 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200876 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200877 psa_cipher_operation_t operation;
878
Gilles Peskine50e586b2018-06-08 14:28:46 +0200879 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200880 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200881 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300882 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
883 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
884 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200885
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200886 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
887 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200888
889 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
890
891 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200892 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200893
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200894 TEST_ASSERT( psa_encrypt_setup( &operation,
895 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200896
897 TEST_ASSERT( psa_encrypt_set_iv( &operation,
898 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200899 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200900 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300901 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200902
Gilles Peskine4abf7412018-06-18 16:35:34 +0200903 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300904 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200905 output, output_buffer_size,
906 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200907 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200908 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300909 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200910 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200911 output, output_buffer_size,
912 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200913 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200914 TEST_ASSERT( psa_cipher_finish( &operation,
915 output + function_output_length,
916 output_buffer_size,
917 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200918 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200919 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
920
Gilles Peskine4abf7412018-06-18 16:35:34 +0200921 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300922 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200923 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200924
925exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300926 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200927 psa_destroy_key( key_slot );
928 mbedtls_psa_crypto_free( );
929}
930/* END_CASE */
931
932/* BEGIN_CASE */
933void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300934 data_t *key,
935 data_t *input,
936 int first_part_size,
937 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +0200938{
939 int key_slot = 1;
940
941 psa_key_type_t key_type = key_type_arg;
942 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200943 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200944 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +0300945 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200946 size_t output_buffer_size = 0;
947 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200948 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200949 psa_cipher_operation_t operation;
950
Gilles Peskine50e586b2018-06-08 14:28:46 +0200951 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200952 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200953 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300954 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
955 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
956 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200957
Gilles Peskine9ad29e22018-06-21 09:40:04 +0200958 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
959 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200960
961 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
962
963 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200964 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200965
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200966 TEST_ASSERT( psa_decrypt_setup( &operation,
967 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200968
969 TEST_ASSERT( psa_encrypt_set_iv( &operation,
970 iv, sizeof( iv ) ) == PSA_SUCCESS );
971
Gilles Peskine4abf7412018-06-18 16:35:34 +0200972 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200973 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300974 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200975
Gilles Peskine4abf7412018-06-18 16:35:34 +0200976 TEST_ASSERT( (unsigned int) first_part_size < input->len );
977 TEST_ASSERT( psa_cipher_update( &operation,
978 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200979 output, output_buffer_size,
980 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200981 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200982 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300983 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200984 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +0200985 output, output_buffer_size,
986 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200987 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200988 TEST_ASSERT( psa_cipher_finish( &operation,
989 output + function_output_length,
990 output_buffer_size,
991 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +0200992 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +0200993 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
994
Gilles Peskine4abf7412018-06-18 16:35:34 +0200995 TEST_ASSERT( total_output_length == expected_output->len );
Gilles Peskine2d277862018-06-18 15:41:12 +0200996 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200997 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +0200998
999exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001000 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001001 psa_destroy_key( key_slot );
1002 mbedtls_psa_crypto_free( );
1003}
1004/* END_CASE */
1005
Gilles Peskine50e586b2018-06-08 14:28:46 +02001006/* BEGIN_CASE */
1007void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001008 data_t *key,
1009 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001010 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001011{
1012 int key_slot = 1;
1013 psa_status_t status;
1014 psa_key_type_t key_type = key_type_arg;
1015 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001016 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001017 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001018 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001019 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001020 size_t output_buffer_size = 0;
1021 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001022 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001023 psa_cipher_operation_t operation;
1024
Gilles Peskine50e586b2018-06-08 14:28:46 +02001025 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001026 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001027 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001028 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1029 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1030 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001031
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001032 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1033 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001034
1035 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1036
1037 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001038 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001039
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001040 TEST_ASSERT( psa_decrypt_setup( &operation,
1041 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001042
1043 TEST_ASSERT( psa_encrypt_set_iv( &operation,
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001044 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001045
Gilles Peskine4abf7412018-06-18 16:35:34 +02001046 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001047 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001048 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001049
Gilles Peskine4abf7412018-06-18 16:35:34 +02001050 TEST_ASSERT( psa_cipher_update( &operation,
1051 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001052 output, output_buffer_size,
1053 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001054 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001055 status = psa_cipher_finish( &operation,
1056 output + function_output_length,
1057 output_buffer_size,
1058 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001059 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001060 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001061
1062 if( expected_status == PSA_SUCCESS )
1063 {
1064 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001065 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001066 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001067 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001068 }
1069
Gilles Peskine50e586b2018-06-08 14:28:46 +02001070exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001071 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001072 psa_destroy_key( key_slot );
1073 mbedtls_psa_crypto_free( );
1074}
1075/* END_CASE */
1076
Gilles Peskine50e586b2018-06-08 14:28:46 +02001077/* BEGIN_CASE */
1078void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001079 data_t *key,
1080 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02001081{
1082 int key_slot = 1;
1083 psa_key_type_t key_type = key_type_arg;
1084 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07001085 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02001086 size_t iv_size = 16;
1087 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001088 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001089 size_t output1_size = 0;
1090 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001091 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001092 size_t output2_size = 0;
1093 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001094 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001095 psa_cipher_operation_t operation1;
1096 psa_cipher_operation_t operation2;
1097
mohammad1603d7d7ba52018-03-12 18:51:53 +02001098 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001099 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001100 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1101 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001102
mohammad1603d7d7ba52018-03-12 18:51:53 +02001103 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1104
1105 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001106 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001107
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001108 TEST_ASSERT( psa_encrypt_setup( &operation1,
1109 key_slot, alg ) == PSA_SUCCESS );
1110 TEST_ASSERT( psa_decrypt_setup( &operation2,
1111 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001112
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001113 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
1114 iv, iv_size,
1115 &iv_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001116 output1_size = input->len + operation1.block_size;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001117 output1 = mbedtls_calloc( 1, output1_size );
1118 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001119
Gilles Peskine4abf7412018-06-18 16:35:34 +02001120 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001121 output1, output1_size,
1122 &output1_length ) == PSA_SUCCESS );
1123 TEST_ASSERT( psa_cipher_finish( &operation1,
1124 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001125 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001126
Gilles Peskine048b7f02018-06-08 14:20:49 +02001127 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001128
1129 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1130
1131 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001132 output2 = mbedtls_calloc( 1, output2_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001133 TEST_ASSERT( output2 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001134
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001135 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
1136 iv, iv_length ) == PSA_SUCCESS );
1137 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
1138 output2, output2_size,
1139 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001140 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001141 TEST_ASSERT( psa_cipher_finish( &operation2,
1142 output2 + output2_length,
1143 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001144 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001145
Gilles Peskine048b7f02018-06-08 14:20:49 +02001146 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001147
Moran Pekerded84402018-06-06 16:36:50 +03001148 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1149
Gilles Peskine4abf7412018-06-18 16:35:34 +02001150 TEST_ASSERT( input->len == output2_length );
1151 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +03001152
1153exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001154 mbedtls_free( output1 );
1155 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03001156 psa_destroy_key( key_slot );
1157 mbedtls_psa_crypto_free( );
1158}
1159/* END_CASE */
1160
1161/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001162void cipher_verify_output_multipart( int alg_arg,
1163 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001164 data_t *key,
1165 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001166 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03001167{
1168 int key_slot = 1;
1169 psa_key_type_t key_type = key_type_arg;
1170 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03001171 unsigned char iv[16] = {0};
1172 size_t iv_size = 16;
1173 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001174 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001175 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001176 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001177 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001178 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001179 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001180 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001181 psa_cipher_operation_t operation1;
1182 psa_cipher_operation_t operation2;
1183
Moran Pekerded84402018-06-06 16:36:50 +03001184 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001185 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001186 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1187 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001188
Moran Pekerded84402018-06-06 16:36:50 +03001189 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1190
1191 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001192 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001193
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001194 TEST_ASSERT( psa_encrypt_setup( &operation1,
1195 key_slot, alg ) == PSA_SUCCESS );
1196 TEST_ASSERT( psa_decrypt_setup( &operation2,
1197 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001198
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001199 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
1200 iv, iv_size,
1201 &iv_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001202 output1_buffer_size = input->len + operation1.block_size;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001203 output1 = mbedtls_calloc( 1, output1_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001204 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001205
Gilles Peskine4abf7412018-06-18 16:35:34 +02001206 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001207
itayzafrir3e02b3b2018-06-12 17:06:52 +03001208 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001209 output1, output1_buffer_size,
1210 &function_output_length ) == PSA_SUCCESS );
1211 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001212
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001213 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001214 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001215 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001216 output1, output1_buffer_size,
1217 &function_output_length ) == PSA_SUCCESS );
1218 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001219
Gilles Peskine048b7f02018-06-08 14:20:49 +02001220 TEST_ASSERT( psa_cipher_finish( &operation1,
1221 output1 + output1_length,
1222 output1_buffer_size - output1_length,
1223 &function_output_length ) == PSA_SUCCESS );
1224 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001225
1226 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1227
Gilles Peskine048b7f02018-06-08 14:20:49 +02001228 output2_buffer_size = output1_length;
1229 output2 = mbedtls_calloc( 1, output2_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001230 TEST_ASSERT( output2 != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001231
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001232 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
1233 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001234
1235 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001236 output2, output2_buffer_size,
1237 &function_output_length ) == PSA_SUCCESS );
1238 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001239
Gilles Peskine048b7f02018-06-08 14:20:49 +02001240 TEST_ASSERT( psa_cipher_update( &operation2,
1241 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001242 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001243 output2, output2_buffer_size,
1244 &function_output_length ) == PSA_SUCCESS );
1245 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001246
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001247 TEST_ASSERT( psa_cipher_finish( &operation2,
1248 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001249 output2_buffer_size - output2_length,
1250 &function_output_length ) == PSA_SUCCESS );
1251 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001252
mohammad1603d7d7ba52018-03-12 18:51:53 +02001253 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1254
Gilles Peskine4abf7412018-06-18 16:35:34 +02001255 TEST_ASSERT( input->len == output2_length );
1256 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001257
1258exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001259 mbedtls_free( output1 );
1260 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001261 psa_destroy_key( key_slot );
1262 mbedtls_psa_crypto_free( );
1263}
1264/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02001265
Gilles Peskine20035e32018-02-03 22:44:14 +01001266/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001267void aead_encrypt_decrypt( int key_type_arg,
1268 data_t * key_data,
1269 int alg_arg,
1270 data_t * input_data,
1271 data_t * nonce,
1272 data_t * additional_data,
1273 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001274{
1275 int slot = 1;
1276 psa_key_type_t key_type = key_type_arg;
1277 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001278 unsigned char *output_data = NULL;
1279 size_t output_size = 0;
1280 size_t output_length = 0;
1281 unsigned char *output_data2 = NULL;
1282 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001283 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001284 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001285 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001286
Gilles Peskinea1cac842018-06-11 19:33:02 +02001287 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001288 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001289 TEST_ASSERT( nonce != NULL );
1290 TEST_ASSERT( additional_data != NULL );
1291 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1292 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1293 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1294 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1295
Gilles Peskine4abf7412018-06-18 16:35:34 +02001296 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001297 output_data = mbedtls_calloc( 1, output_size );
1298 TEST_ASSERT( output_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001299
1300 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1301
1302 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001303 psa_key_policy_set_usage( &policy,
1304 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
1305 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001306 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1307
1308 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001309 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001310
1311 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001312 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001313 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001314 additional_data->len,
1315 input_data->x, input_data->len,
1316 output_data, output_size,
1317 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001318
1319 if( PSA_SUCCESS == expected_result )
1320 {
1321 output_data2 = mbedtls_calloc( 1, output_length );
1322 TEST_ASSERT( output_data2 != NULL );
1323
1324 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001325 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001326 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001327 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001328 output_data, output_length,
1329 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001330 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02001331
itayzafrir3e02b3b2018-06-12 17:06:52 +03001332 TEST_ASSERT( memcmp( input_data->x, output_data2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001333 input_data->len ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001334 }
Gilles Peskine2d277862018-06-18 15:41:12 +02001335
Gilles Peskinea1cac842018-06-11 19:33:02 +02001336exit:
1337 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001338 mbedtls_free( output_data );
1339 mbedtls_free( output_data2 );
1340 mbedtls_psa_crypto_free( );
1341}
1342/* END_CASE */
1343
1344/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001345void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001346 int alg_arg, data_t * input_data,
1347 data_t * additional_data, data_t * nonce,
1348 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001349{
1350 int slot = 1;
1351 psa_key_type_t key_type = key_type_arg;
1352 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001353 unsigned char *output_data = NULL;
1354 size_t output_size = 0;
1355 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001356 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001357 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001358
Gilles Peskinea1cac842018-06-11 19:33:02 +02001359 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001360 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001361 TEST_ASSERT( additional_data != NULL );
1362 TEST_ASSERT( nonce != NULL );
1363 TEST_ASSERT( expected_result != NULL );
1364 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1365 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1366 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1367 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1368 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
1369
Gilles Peskine4abf7412018-06-18 16:35:34 +02001370 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001371 output_data = mbedtls_calloc( 1, output_size );
1372 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001373
Gilles Peskinea1cac842018-06-11 19:33:02 +02001374 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1375
1376 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001377 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001378 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1379
1380 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001381 key_data->x,
1382 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001383
1384 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001385 nonce->x, nonce->len,
1386 additional_data->x, additional_data->len,
1387 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001388 output_data, output_size,
1389 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001390
itayzafrir3e02b3b2018-06-12 17:06:52 +03001391 TEST_ASSERT( memcmp( output_data, expected_result->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001392 output_length ) == 0 );
1393
Gilles Peskinea1cac842018-06-11 19:33:02 +02001394exit:
1395 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001396 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001397 mbedtls_psa_crypto_free( );
1398}
1399/* END_CASE */
1400
1401/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001402void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001403 int alg_arg, data_t * input_data,
1404 data_t * additional_data, data_t * nonce,
1405 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001406{
1407 int slot = 1;
1408 psa_key_type_t key_type = key_type_arg;
1409 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001410 unsigned char *output_data = NULL;
1411 size_t output_size = 0;
1412 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001413 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001414 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001415 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001416
Gilles Peskinea1cac842018-06-11 19:33:02 +02001417 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001418 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001419 TEST_ASSERT( additional_data != NULL );
1420 TEST_ASSERT( nonce != NULL );
1421 TEST_ASSERT( expected_data != NULL );
1422 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1423 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1424 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1425 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1426 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
1427
Gilles Peskine4abf7412018-06-18 16:35:34 +02001428 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001429 output_data = mbedtls_calloc( 1, output_size );
1430 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001431
Gilles Peskinea1cac842018-06-11 19:33:02 +02001432 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1433
1434 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001435 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001436 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1437
1438 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001439 key_data->x,
1440 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001441
1442 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001443 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001444 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001445 additional_data->len,
1446 input_data->x, input_data->len,
1447 output_data, output_size,
1448 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001449
Gilles Peskine2d277862018-06-18 15:41:12 +02001450 if( expected_result == PSA_SUCCESS )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001451 {
itayzafrir3e02b3b2018-06-12 17:06:52 +03001452 TEST_ASSERT( memcmp( output_data, expected_data->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001453 output_length ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001454 }
1455
Gilles Peskinea1cac842018-06-11 19:33:02 +02001456exit:
1457 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001458 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001459 mbedtls_psa_crypto_free( );
1460}
1461/* END_CASE */
1462
1463/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001464void signature_size( int type_arg,
1465 int bits,
1466 int alg_arg,
1467 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001468{
1469 psa_key_type_t type = type_arg;
1470 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02001471 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01001472 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
1473exit:
1474 ;
1475}
1476/* END_CASE */
1477
1478/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001479void sign_deterministic( int key_type_arg, data_t *key_data,
1480 int alg_arg, data_t *input_data,
1481 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001482{
1483 int slot = 1;
1484 psa_key_type_t key_type = key_type_arg;
1485 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01001486 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01001487 unsigned char *signature = NULL;
1488 size_t signature_size;
1489 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02001490 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01001491
Gilles Peskine20035e32018-02-03 22:44:14 +01001492 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001493 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001494 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001495 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1496 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1497 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01001498
1499 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1500
mohammad1603a97cb8c2018-03-28 03:46:26 -07001501 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001502 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001503 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1504
Gilles Peskine20035e32018-02-03 22:44:14 +01001505 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001506 key_data->x,
1507 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01001508 TEST_ASSERT( psa_get_key_information( slot,
1509 NULL,
1510 &key_bits ) == PSA_SUCCESS );
1511
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001512 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
1513 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01001514 TEST_ASSERT( signature_size != 0 );
1515 signature = mbedtls_calloc( 1, signature_size );
1516 TEST_ASSERT( signature != NULL );
1517
1518 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001519 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01001520 NULL, 0,
1521 signature, signature_size,
1522 &signature_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001523 TEST_ASSERT( signature_length == output_data->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001524 TEST_ASSERT( memcmp( signature, output_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001525 output_data->len ) == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01001526
1527exit:
1528 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01001529 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01001530 mbedtls_psa_crypto_free( );
1531}
1532/* END_CASE */
1533
1534/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001535void sign_fail( int key_type_arg, data_t *key_data,
1536 int alg_arg, data_t *input_data,
Gilles Peskine20035e32018-02-03 22:44:14 +01001537 int signature_size, int expected_status_arg )
1538{
1539 int slot = 1;
1540 psa_key_type_t key_type = key_type_arg;
1541 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01001542 psa_status_t actual_status;
1543 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01001544 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01001545 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02001546 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01001547
Gilles Peskine20035e32018-02-03 22:44:14 +01001548 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001549 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001550 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1551 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1552
Gilles Peskine20035e32018-02-03 22:44:14 +01001553 signature = mbedtls_calloc( 1, signature_size );
1554 TEST_ASSERT( signature != NULL );
1555
1556 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1557
mohammad1603a97cb8c2018-03-28 03:46:26 -07001558 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001559 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001560 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1561
Gilles Peskine20035e32018-02-03 22:44:14 +01001562 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001563 key_data->x,
1564 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01001565
1566 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001567 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01001568 NULL, 0,
1569 signature, signature_size,
1570 &signature_length );
1571 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine93aa0332018-02-03 23:58:03 +01001572 TEST_ASSERT( signature_length == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01001573
1574exit:
1575 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01001576 mbedtls_free( signature );
1577 mbedtls_psa_crypto_free( );
1578}
1579/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03001580
1581/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001582void asymmetric_verify( int key_type_arg, data_t *key_data,
1583 int alg_arg, data_t *hash_data,
1584 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03001585{
1586 int slot = 1;
1587 psa_key_type_t key_type = key_type_arg;
1588 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001589 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03001590
itayzafrir5c753392018-05-08 11:18:38 +03001591 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03001592 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03001593 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001594 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1595 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
1596 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03001597
1598 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1599
1600 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001601 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03001602 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1603
1604 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001605 key_data->x,
1606 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03001607
1608 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001609 hash_data->x, hash_data->len,
itayzafrir5c753392018-05-08 11:18:38 +03001610 NULL, 0,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001611 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001612 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03001613exit:
1614 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03001615 mbedtls_psa_crypto_free( );
1616}
1617/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001618
1619/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001620void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
1621 int alg_arg, data_t *hash_data,
1622 data_t *signature_data,
1623 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001624{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001625 int slot = 1;
1626 psa_key_type_t key_type = key_type_arg;
1627 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001628 psa_status_t actual_status;
1629 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001630 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001631
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001632 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001633 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001634 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001635 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1636 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
1637 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001638
1639 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1640
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001641 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001642 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001643 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1644
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001645 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001646 key_data->x,
1647 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001648
1649 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001650 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001651 NULL, 0,
1652 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001653 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001654
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001655 TEST_ASSERT( actual_status == expected_status );
1656
1657exit:
1658 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001659 mbedtls_psa_crypto_free( );
1660}
1661/* END_CASE */
1662
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001663/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001664void asymmetric_encrypt_decrypt( int key_type_arg, data_t *key_data,
1665 int alg_arg, data_t *input_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001666{
1667 int slot = 1;
1668 psa_key_type_t key_type = key_type_arg;
1669 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001670 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001671 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001672 size_t output_length = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001673 unsigned char *output2 = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001674 size_t output2_size = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001675 size_t output2_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02001676 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001677
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001678 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001679 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001680 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1681 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1682
Gilles Peskine4abf7412018-06-18 16:35:34 +02001683 output_size = key_data->len;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001684 output2_size = output_size;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001685 output = mbedtls_calloc( 1, output_size );
1686 TEST_ASSERT( output != NULL );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001687 output2 = mbedtls_calloc( 1, output2_size );
1688 TEST_ASSERT( output2 != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001689
1690 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1691
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001692 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001693 psa_key_policy_set_usage( &policy,
1694 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001695 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001696 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1697
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001698 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001699 key_data->x,
1700 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001701
Gilles Peskineeebd7382018-06-08 18:11:54 +02001702 /* We test encryption by checking that encrypt-then-decrypt gives back
1703 * the original plaintext because of the non-optional random
1704 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02001705 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001706 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001707 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001708 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001709 &output_length ) == PSA_SUCCESS );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001710
Gilles Peskine2d277862018-06-18 15:41:12 +02001711 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001712 output, output_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001713 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001714 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001715 &output2_length ) == PSA_SUCCESS );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001716 TEST_ASSERT( memcmp( input_data->x, output2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001717 input_data->len ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001718
1719exit:
1720 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001721 mbedtls_free( output );
1722 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001723 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001724}
1725/* END_CASE */
1726
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001727/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001728void asymmetric_encrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001729 int alg_arg, data_t *input_data,
1730 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001731{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001732 int slot = 1;
1733 psa_key_type_t key_type = key_type_arg;
1734 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001735 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001736 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001737 size_t output_length = 0;
1738 psa_status_t actual_status;
1739 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001740 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001741
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001742 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001743 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001744 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1745 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1746
Gilles Peskine4abf7412018-06-18 16:35:34 +02001747 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001748 output = mbedtls_calloc( 1, output_size );
1749 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001750
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001751 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1752
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001753 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001754 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001755 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1756
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001757 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001758 key_data->x,
1759 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001760
Gilles Peskine2d277862018-06-18 15:41:12 +02001761 actual_status = psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001762 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001763 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001764 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001765 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001766 TEST_ASSERT( actual_status == expected_status );
1767
1768exit:
1769 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001770 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001771 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001772}
1773/* END_CASE */
1774
1775/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001776void asymmetric_decrypt( int key_type_arg, data_t *key_data,
1777 int alg_arg, data_t *input_data,
1778 data_t *expected_data, int expected_size )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001779{
1780 int slot = 1;
1781 psa_key_type_t key_type = key_type_arg;
1782 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001783 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001784 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001785 size_t output_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02001786 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001787
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001788 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001789 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001790 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001791 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1792 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1793 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
1794
Gilles Peskine4abf7412018-06-18 16:35:34 +02001795 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001796 output = mbedtls_calloc( 1, output_size );
1797 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001798
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001799 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1800
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001801 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001802 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001803 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1804
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001805 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001806 key_data->x,
1807 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001808
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001809 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001810 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001811 NULL, 0,
1812 output,
1813 output_size,
1814 &output_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001815 TEST_ASSERT( (size_t) expected_size == output_length );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001816 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001817
1818exit:
1819 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001820 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001821 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001822}
1823/* END_CASE */
1824
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001825/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001826void asymmetric_decrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001827 int alg_arg, data_t *input_data,
1828 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001829{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001830 int slot = 1;
1831 psa_key_type_t key_type = key_type_arg;
1832 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001833 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001834 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001835 size_t output_length = 0;
1836 psa_status_t actual_status;
1837 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001838 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001839
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001840 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001841 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001842 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1843 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1844
Gilles Peskine4abf7412018-06-18 16:35:34 +02001845 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001846 output = mbedtls_calloc( 1, output_size );
1847 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001848
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001849 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1850
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001851 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001852 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001853 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1854
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001855 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001856 key_data->x,
1857 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001858
Gilles Peskine2d277862018-06-18 15:41:12 +02001859 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001860 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001861 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001862 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001863 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001864 TEST_ASSERT( actual_status == expected_status );
1865
1866exit:
1867 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02001868 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001869 mbedtls_psa_crypto_free( );
1870}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001871/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02001872
1873/* BEGIN_CASE */
1874void generate_random( int bytes, int retries )
1875{
1876 const unsigned char trail[] = "foobar";
1877 unsigned char *buffer1 = mbedtls_calloc( 1, bytes + sizeof( trail ) );
1878 unsigned char *buffer2 = mbedtls_calloc( 1, bytes );
1879
1880 TEST_ASSERT( buffer1 != NULL );
1881 TEST_ASSERT( buffer2 != NULL );
1882 memcpy( buffer1 + bytes, trail, sizeof( trail ) );
1883
1884 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1885
1886 TEST_ASSERT( psa_generate_random( buffer1, bytes ) == PSA_SUCCESS );
1887
1888 /* Check that no more than bytes have been overwritten */
1889 TEST_ASSERT( memcmp( buffer1 + bytes, trail, sizeof( trail ) ) == 0 );
1890
1891 if( bytes == 0 )
1892 goto exit;
1893
1894 /* We can't validate that the data is really random, but we can
1895 * validate that it doesn't repeat between calls. There's a
1896 * 1/256^bytes chance that it does repeat, of course, so allow
1897 * a few retries. */
1898 ++retries; /* The first time isn't a REtry */
1899 do
1900 {
1901 --retries;
1902 TEST_ASSERT( psa_generate_random( buffer2, bytes ) == PSA_SUCCESS );
1903 }
1904 while( memcmp( buffer1, buffer2, bytes ) == 0 && retries >= -1 );
1905 TEST_ASSERT( retries >= 0 );
1906
1907exit:
1908 mbedtls_psa_crypto_free( );
1909 mbedtls_free( buffer1 );
1910 mbedtls_free( buffer2 );
1911}
1912/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02001913
1914/* BEGIN_CASE */
1915void generate_key( int type_arg,
1916 int bits_arg,
1917 int usage_arg,
1918 int alg_arg,
1919 int expected_status_arg )
1920{
1921 int slot = 1;
1922 psa_key_type_t type = type_arg;
1923 psa_key_usage_t usage = usage_arg;
1924 size_t bits = bits_arg;
1925 psa_algorithm_t alg = alg_arg;
1926 psa_status_t expected_status = expected_status_arg;
1927 psa_key_type_t got_type;
1928 size_t got_bits;
1929 unsigned char exported[616] = {0}; /* enough for a 1024-bit RSA key */
1930 size_t exported_length;
1931 psa_status_t expected_export_status =
1932 usage & PSA_KEY_USAGE_EXPORT ? PSA_SUCCESS : PSA_ERROR_NOT_PERMITTED;
1933 psa_status_t expected_info_status =
1934 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
1935 psa_key_policy_t policy;
1936
1937 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1938
1939 psa_key_policy_init( &policy );
1940 psa_key_policy_set_usage( &policy, usage, alg );
1941 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1942
1943 /* Generate a key */
1944 TEST_ASSERT( psa_generate_key( slot, type, bits,
1945 NULL, 0 ) == expected_status );
1946
1947 /* Test the key information */
1948 TEST_ASSERT( psa_get_key_information( slot,
1949 &got_type,
1950 &got_bits ) == expected_info_status );
1951 if( expected_info_status != PSA_SUCCESS )
1952 goto exit;
1953 TEST_ASSERT( got_type == type );
1954 TEST_ASSERT( got_bits == bits );
1955
1956 /* Export the key */
1957 TEST_ASSERT( psa_export_key( slot,
1958 exported, sizeof( exported ),
1959 &exported_length ) == expected_export_status );
1960 if( expected_export_status == PSA_SUCCESS )
1961 {
1962 if( PSA_KEY_TYPE_IS_RAW_BYTES( type ) )
1963 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
1964#if defined(MBEDTLS_DES_C)
1965 if( type == PSA_KEY_TYPE_DES )
1966 {
1967 /* Check the parity bits. */
1968 unsigned i;
1969 for( i = 0; i < bits / 8; i++ )
1970 {
1971 unsigned bit_count = 0;
1972 unsigned m;
1973 for( m = 1; m <= 0x100; m <<= 1 )
1974 {
1975 if( exported[i] & m )
1976 ++bit_count;
1977 }
1978 TEST_ASSERT( bit_count % 2 != 0 );
1979 }
1980 }
1981#endif
1982#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
1983 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
1984 {
1985 /* Sanity check: does this look like the beginning of a PKCS#8
1986 * RSA key pair? Assumes bits is a multiple of 8. */
1987 size_t n_bytes = bits / 8 + 1;
1988 size_t n_encoded_bytes;
1989 unsigned char *n_end;
1990 TEST_ASSERT( exported_length >= 7 + ( n_bytes + 3 ) * 9 / 2 );
1991 TEST_ASSERT( exported[0] == 0x30 );
1992 TEST_ASSERT( exported[1] == 0x82 ); // assumes >=416-bit key
1993 TEST_ASSERT( exported[4] == 0x02 );
1994 TEST_ASSERT( exported[5] == 0x01 );
1995 TEST_ASSERT( exported[6] == 0x00 );
1996 TEST_ASSERT( exported[7] == 0x02 );
1997 n_encoded_bytes = exported[8];
1998 n_end = exported + 9 + n_encoded_bytes;
1999 if( n_encoded_bytes & 0x80 )
2000 {
2001 n_encoded_bytes = ( n_encoded_bytes & 0x7f ) << 7;
2002 n_encoded_bytes |= exported[9] & 0x7f;
2003 n_end += 1;
2004 }
2005 /* The encoding of n should start with a 0 byte since it should
2006 * have its high bit set. However Mbed TLS is not compliant and
2007 * generates an invalid, but widely tolerated, encoding of
2008 * positive INTEGERs with a bit size that is a multiple of 8
2009 * with no leading 0 byte. Accept this here. */
2010 TEST_ASSERT( n_bytes == n_encoded_bytes ||
2011 n_bytes == n_encoded_bytes + 1 );
2012 if( n_bytes == n_encoded_bytes )
2013 TEST_ASSERT( exported[n_encoded_bytes <= 127 ? 9 : 10] == 0x00 );
2014 /* Sanity check: e must be 3 */
2015 TEST_ASSERT( n_end[0] == 0x02 );
2016 TEST_ASSERT( n_end[1] == 0x03 );
2017 TEST_ASSERT( n_end[2] == 0x01 );
2018 TEST_ASSERT( n_end[3] == 0x00 );
2019 TEST_ASSERT( n_end[4] == 0x01 );
2020 TEST_ASSERT( n_end[5] == 0x02 );
2021 }
2022#endif /* MBEDTLS_RSA_C */
2023#if defined(MBEDTLS_ECP_C)
2024 if( PSA_KEY_TYPE_IS_ECC( type ) )
2025 {
2026 /* Sanity check: does this look like the beginning of a PKCS#8
2027 * elliptic curve key pair? */
2028 TEST_ASSERT( exported_length >= bits * 3 / 8 + 10 );
2029 TEST_ASSERT( exported[0] == 0x30 );
2030 }
2031#endif /* MBEDTLS_ECP_C */
2032 }
2033
Gilles Peskine818ca122018-06-20 18:16:48 +02002034 /* Do something with the key according to its type and permitted usage. */
2035 if( PSA_ALG_IS_MAC( alg ) )
2036 exercise_mac_key( slot, usage, alg );
2037 else if( PSA_ALG_IS_CIPHER( alg ) )
2038 exercise_cipher_key( slot, usage, alg );
2039 else if( PSA_ALG_IS_AEAD( alg ) )
2040 exercise_aead_key( slot, usage, alg );
2041 else if( PSA_ALG_IS_SIGN( alg ) )
2042 exercise_signature_key( slot, usage, alg );
2043 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
2044 exercise_asymmetric_encryption_key( slot, usage, alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02002045
2046exit:
2047 psa_destroy_key( slot );
2048 mbedtls_psa_crypto_free( );
2049}
2050/* END_CASE */