blob: 43e4794700ee207bc284bc8a79c9ee03018edbd2 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
4#if defined(MBEDTLS_PSA_CRYPTO_SPM)
5#include "spm/psa_defs.h"
6#endif
7
Gilles Peskine0b352bc2018-06-28 00:16:11 +02008#include "mbedtls/asn1write.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +01009#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030010
Gilles Peskine96ee5c72018-07-12 17:24:54 +020011#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
12
itayzafrir3e02b3b2018-06-12 17:06:52 +030013#if(UINT32_MAX > SIZE_MAX)
Gilles Peskine2d277862018-06-18 15:41:12 +020014#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX )
itayzafrir3e02b3b2018-06-12 17:06:52 +030015#else
Gilles Peskine2d277862018-06-18 15:41:12 +020016#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1
itayzafrir3e02b3b2018-06-12 17:06:52 +030017#endif
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020018
Jaeden Amerof24c7f82018-06-27 17:20:43 +010019/** An invalid export length that will never be set by psa_export_key(). */
20static const size_t INVALID_EXPORT_LENGTH = ~0U;
21
Gilles Peskined35a1cc2018-06-26 21:26:10 +020022/** Test if a buffer is all-bits zero.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020023 *
24 * \param buffer Pointer to the beginning of the buffer.
25 * \param size Size of the buffer in bytes.
26 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020027 * \return 1 if the buffer is all-bits-zero.
28 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020029 */
Gilles Peskine3f669c32018-06-21 09:21:51 +020030static int mem_is_zero( void *buffer, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020031{
32 size_t i;
33 for( i = 0; i < size; i++ )
34 {
35 if( ( (unsigned char *) buffer )[i] != 0 )
Gilles Peskine3f669c32018-06-21 09:21:51 +020036 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020038 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020039}
Gilles Peskine818ca122018-06-20 18:16:48 +020040
Gilles Peskine48c0ea12018-06-21 14:15:31 +020041static int key_type_is_raw_bytes( psa_key_type_t type )
42{
43 psa_key_type_t category = type & PSA_KEY_TYPE_CATEGORY_MASK;
44 return( category == PSA_KEY_TYPE_RAW_DATA ||
45 category == PSA_KEY_TYPE_CATEGORY_SYMMETRIC );
46}
47
Gilles Peskine0b352bc2018-06-28 00:16:11 +020048/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
49static int asn1_write_10x( unsigned char **p,
50 unsigned char *start,
51 size_t bits,
52 unsigned char x )
53{
54 int ret;
55 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020056 if( bits == 0 )
57 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
58 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020059 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030060 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020061 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
62 *p -= len;
63 ( *p )[len-1] = x;
64 if( bits % 8 == 0 )
65 ( *p )[1] |= 1;
66 else
67 ( *p )[0] |= 1 << ( bits % 8 );
68 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
69 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
70 MBEDTLS_ASN1_INTEGER ) );
71 return( len );
72}
73
74static int construct_fake_rsa_key( unsigned char *buffer,
75 size_t buffer_size,
76 unsigned char **p,
77 size_t bits,
78 int keypair )
79{
80 size_t half_bits = ( bits + 1 ) / 2;
81 int ret;
82 int len = 0;
83 /* Construct something that looks like a DER encoding of
84 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
85 * RSAPrivateKey ::= SEQUENCE {
86 * version Version,
87 * modulus INTEGER, -- n
88 * publicExponent INTEGER, -- e
89 * privateExponent INTEGER, -- d
90 * prime1 INTEGER, -- p
91 * prime2 INTEGER, -- q
92 * exponent1 INTEGER, -- d mod (p-1)
93 * exponent2 INTEGER, -- d mod (q-1)
94 * coefficient INTEGER, -- (inverse of q) mod p
95 * otherPrimeInfos OtherPrimeInfos OPTIONAL
96 * }
97 * Or, for a public key, the same structure with only
98 * version, modulus and publicExponent.
99 */
100 *p = buffer + buffer_size;
101 if( keypair )
102 {
103 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
104 asn1_write_10x( p, buffer, half_bits, 1 ) );
105 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
106 asn1_write_10x( p, buffer, half_bits, 1 ) );
107 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
108 asn1_write_10x( p, buffer, half_bits, 1 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* q */
110 asn1_write_10x( p, buffer, half_bits, 1 ) );
111 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
112 asn1_write_10x( p, buffer, half_bits, 3 ) );
113 MBEDTLS_ASN1_CHK_ADD( len, /* d */
114 asn1_write_10x( p, buffer, bits, 1 ) );
115 }
116 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
117 asn1_write_10x( p, buffer, 17, 1 ) );
118 MBEDTLS_ASN1_CHK_ADD( len, /* n */
119 asn1_write_10x( p, buffer, bits, 1 ) );
120 if( keypair )
121 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
122 mbedtls_asn1_write_int( p, buffer, 0 ) );
123 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
124 {
125 const unsigned char tag =
126 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
127 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
128 }
129 return( len );
130}
131
Gilles Peskine818ca122018-06-20 18:16:48 +0200132static int exercise_mac_key( psa_key_slot_t key,
133 psa_key_usage_t usage,
134 psa_algorithm_t alg )
135{
136 psa_mac_operation_t operation;
137 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200138 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200139 size_t mac_length = sizeof( mac );
140
141 if( usage & PSA_KEY_USAGE_SIGN )
142 {
Gilles Peskine89167cb2018-07-08 20:12:23 +0200143 TEST_ASSERT( psa_mac_sign_setup( &operation,
144 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200145 TEST_ASSERT( psa_mac_update( &operation,
146 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200147 TEST_ASSERT( psa_mac_sign_finish( &operation,
Gilles Peskineef0cb402018-07-12 16:55:59 +0200148 mac, sizeof( mac ),
Gilles Peskineacd4be32018-07-08 19:56:25 +0200149 &mac_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200150 }
151
152 if( usage & PSA_KEY_USAGE_VERIFY )
153 {
154 psa_status_t verify_status =
155 ( usage & PSA_KEY_USAGE_SIGN ?
156 PSA_SUCCESS :
157 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine89167cb2018-07-08 20:12:23 +0200158 TEST_ASSERT( psa_mac_verify_setup( &operation,
159 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200160 TEST_ASSERT( psa_mac_update( &operation,
161 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200162 TEST_ASSERT( psa_mac_verify_finish( &operation,
163 mac,
164 mac_length ) == verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200165 }
166
167 return( 1 );
168
169exit:
170 psa_mac_abort( &operation );
171 return( 0 );
172}
173
174static int exercise_cipher_key( psa_key_slot_t key,
175 psa_key_usage_t usage,
176 psa_algorithm_t alg )
177{
178 psa_cipher_operation_t operation;
179 unsigned char iv[16] = {0};
180 size_t iv_length = sizeof( iv );
181 const unsigned char plaintext[16] = "Hello, world...";
182 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
183 size_t ciphertext_length = sizeof( ciphertext );
184 unsigned char decrypted[sizeof( ciphertext )];
185 size_t part_length;
186
187 if( usage & PSA_KEY_USAGE_ENCRYPT )
188 {
Gilles Peskinefe119512018-07-08 21:39:34 +0200189 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
190 key, alg ) == PSA_SUCCESS );
191 TEST_ASSERT( psa_cipher_generate_iv( &operation,
192 iv, sizeof( iv ),
193 &iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200194 TEST_ASSERT( psa_cipher_update( &operation,
195 plaintext, sizeof( plaintext ),
196 ciphertext, sizeof( ciphertext ),
197 &ciphertext_length ) == PSA_SUCCESS );
198 TEST_ASSERT( psa_cipher_finish( &operation,
199 ciphertext + ciphertext_length,
200 sizeof( ciphertext ) - ciphertext_length,
201 &part_length ) == PSA_SUCCESS );
202 ciphertext_length += part_length;
203 }
204
205 if( usage & PSA_KEY_USAGE_DECRYPT )
206 {
207 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700208 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200209 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
210 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200211 size_t bits;
212 TEST_ASSERT( psa_get_key_information( key, &type, &bits ) );
213 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
214 }
Gilles Peskinefe119512018-07-08 21:39:34 +0200215 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
216 key, alg ) == PSA_SUCCESS );
217 TEST_ASSERT( psa_cipher_set_iv( &operation,
218 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200219 TEST_ASSERT( psa_cipher_update( &operation,
220 ciphertext, ciphertext_length,
221 decrypted, sizeof( decrypted ),
222 &part_length ) == PSA_SUCCESS );
223 status = psa_cipher_finish( &operation,
224 decrypted + part_length,
225 sizeof( decrypted ) - part_length,
226 &part_length );
227 /* For a stream cipher, all inputs are valid. For a block cipher,
228 * if the input is some aribtrary data rather than an actual
229 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700230 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700231 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine818ca122018-06-20 18:16:48 +0200232 TEST_ASSERT( status == PSA_SUCCESS );
233 else
234 TEST_ASSERT( status == PSA_SUCCESS ||
235 status == PSA_ERROR_INVALID_PADDING );
236 }
237
238 return( 1 );
239
240exit:
241 psa_cipher_abort( &operation );
242 return( 0 );
243}
244
245static int exercise_aead_key( psa_key_slot_t key,
246 psa_key_usage_t usage,
247 psa_algorithm_t alg )
248{
249 unsigned char nonce[16] = {0};
250 size_t nonce_length = sizeof( nonce );
251 unsigned char plaintext[16] = "Hello, world...";
252 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
253 size_t ciphertext_length = sizeof( ciphertext );
254 size_t plaintext_length = sizeof( ciphertext );
255
256 if( usage & PSA_KEY_USAGE_ENCRYPT )
257 {
258 TEST_ASSERT( psa_aead_encrypt( key, alg,
259 nonce, nonce_length,
260 NULL, 0,
261 plaintext, sizeof( plaintext ),
262 ciphertext, sizeof( ciphertext ),
263 &ciphertext_length ) == PSA_SUCCESS );
264 }
265
266 if( usage & PSA_KEY_USAGE_DECRYPT )
267 {
268 psa_status_t verify_status =
269 ( usage & PSA_KEY_USAGE_ENCRYPT ?
270 PSA_SUCCESS :
271 PSA_ERROR_INVALID_SIGNATURE );
272 TEST_ASSERT( psa_aead_decrypt( key, alg,
273 nonce, nonce_length,
274 NULL, 0,
275 ciphertext, ciphertext_length,
276 plaintext, sizeof( plaintext ),
277 &plaintext_length ) == verify_status );
278 }
279
280 return( 1 );
281
282exit:
283 return( 0 );
284}
285
286static int exercise_signature_key( psa_key_slot_t key,
287 psa_key_usage_t usage,
288 psa_algorithm_t alg )
289{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200290 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
291 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200292 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200293 size_t signature_length = sizeof( signature );
294
295 if( usage & PSA_KEY_USAGE_SIGN )
296 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200297 /* Some algorithms require the payload to have the size of
298 * the hash encoded in the algorithm. Use this input size
299 * even for algorithms that allow other input sizes. */
300 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
301 if( hash_alg != 0 )
302 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200303 TEST_ASSERT( psa_asymmetric_sign( key, alg,
304 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200305 signature, sizeof( signature ),
306 &signature_length ) == PSA_SUCCESS );
307 }
308
309 if( usage & PSA_KEY_USAGE_VERIFY )
310 {
311 psa_status_t verify_status =
312 ( usage & PSA_KEY_USAGE_SIGN ?
313 PSA_SUCCESS :
314 PSA_ERROR_INVALID_SIGNATURE );
315 TEST_ASSERT( psa_asymmetric_verify( key, alg,
316 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200317 signature, signature_length ) ==
318 verify_status );
319 }
320
321 return( 1 );
322
323exit:
324 return( 0 );
325}
326
327static int exercise_asymmetric_encryption_key( psa_key_slot_t key,
328 psa_key_usage_t usage,
329 psa_algorithm_t alg )
330{
331 unsigned char plaintext[256] = "Hello, world...";
332 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
333 size_t ciphertext_length = sizeof( ciphertext );
334 size_t plaintext_length = 16;
335
336 if( usage & PSA_KEY_USAGE_ENCRYPT )
337 {
338 TEST_ASSERT(
339 psa_asymmetric_encrypt( key, alg,
340 plaintext, plaintext_length,
341 NULL, 0,
342 ciphertext, sizeof( ciphertext ),
343 &ciphertext_length ) == PSA_SUCCESS );
344 }
345
346 if( usage & PSA_KEY_USAGE_DECRYPT )
347 {
348 psa_status_t status =
349 psa_asymmetric_decrypt( key, alg,
350 ciphertext, ciphertext_length,
351 NULL, 0,
352 plaintext, sizeof( plaintext ),
353 &plaintext_length );
354 TEST_ASSERT( status == PSA_SUCCESS ||
355 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
356 ( status == PSA_ERROR_INVALID_ARGUMENT ||
357 status == PSA_ERROR_INVALID_PADDING ) ) );
358 }
359
360 return( 1 );
361
362exit:
363 return( 0 );
364}
Gilles Peskine02b75072018-07-01 22:31:34 +0200365
Gilles Peskineea0fb492018-07-12 17:17:20 +0200366static int exercise_key_derivation_key( psa_key_slot_t key,
367 psa_key_usage_t usage,
368 psa_algorithm_t alg )
369{
370 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
371 unsigned char label[16] = "This is a label.";
372 size_t label_length = sizeof( label );
373 unsigned char seed[16] = "abcdefghijklmnop";
374 size_t seed_length = sizeof( seed );
375 unsigned char output[1];
376
377 if( usage & PSA_KEY_USAGE_DERIVE )
378 {
379 TEST_ASSERT( psa_key_derivation( &generator,
380 key, alg,
381 label, label_length,
382 seed, seed_length,
383 sizeof( output ) ) == PSA_SUCCESS );
384 TEST_ASSERT( psa_generator_read( &generator,
385 output,
386 sizeof( output ) ) == PSA_SUCCESS );
387 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
388 }
389
390 return( 1 );
391
392exit:
393 return( 0 );
394}
395
Gilles Peskine02b75072018-07-01 22:31:34 +0200396static int exercise_key( psa_key_slot_t slot,
397 psa_key_usage_t usage,
398 psa_algorithm_t alg )
399{
400 int ok;
401 if( alg == 0 )
402 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
403 else if( PSA_ALG_IS_MAC( alg ) )
404 ok = exercise_mac_key( slot, usage, alg );
405 else if( PSA_ALG_IS_CIPHER( alg ) )
406 ok = exercise_cipher_key( slot, usage, alg );
407 else if( PSA_ALG_IS_AEAD( alg ) )
408 ok = exercise_aead_key( slot, usage, alg );
409 else if( PSA_ALG_IS_SIGN( alg ) )
410 ok = exercise_signature_key( slot, usage, alg );
411 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
412 ok = exercise_asymmetric_encryption_key( slot, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200413 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
414 ok = exercise_key_derivation_key( slot, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200415 else
416 {
417 char message[40];
418 mbedtls_snprintf( message, sizeof( message ),
419 "No code to exercise alg=0x%08lx",
420 (unsigned long) alg );
421 test_fail( message, __LINE__, __FILE__ );
422 ok = 0;
423 }
424 return( ok );
425}
426
Gilles Peskinee59236f2018-01-27 23:32:46 +0100427/* END_HEADER */
428
429/* BEGIN_DEPENDENCIES
430 * depends_on:MBEDTLS_PSA_CRYPTO_C
431 * END_DEPENDENCIES
432 */
433
434/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200435void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100436{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100437 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100438 int i;
439 for( i = 0; i <= 1; i++ )
440 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100441 status = psa_crypto_init( );
442 TEST_ASSERT( status == PSA_SUCCESS );
443 status = psa_crypto_init( );
444 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100445 mbedtls_psa_crypto_free( );
446 }
447}
448/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100449
450/* BEGIN_CASE */
Gilles Peskine996deb12018-08-01 15:45:45 +0200451void fill_slots( int max_arg )
452{
453 /* Fill all the slots until we run out of memory or out of slots,
454 * or until some limit specified in the test data for the sake of
455 * implementations with an essentially unlimited number of slots.
456 * This test assumes that available slots are numbered from 1. */
457
458 psa_key_slot_t slot;
459 psa_key_slot_t max = 0;
460 psa_key_policy_t policy;
461 uint8_t exported[sizeof( max )];
462 size_t exported_size;
463 psa_status_t status;
464
465 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
466
467 psa_key_policy_init( &policy );
468 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
469
470 for( max = 1; max <= (size_t) max_arg; max++ )
471 {
472 status = psa_set_key_policy( max, &policy );
473 /* Stop filling slots if we run out of memory or out of
474 * available slots. */
475 TEST_ASSERT( status == PSA_SUCCESS ||
476 status == PSA_ERROR_INSUFFICIENT_MEMORY ||
477 status == PSA_ERROR_INVALID_ARGUMENT );
478 if( status != PSA_SUCCESS )
479 break;
480 status = psa_import_key( max, PSA_KEY_TYPE_RAW_DATA,
481 (uint8_t*) &max, sizeof( max ) );
482 /* Since psa_set_key_policy succeeded, we know that the slot
483 * number is valid. But we may legitimately run out of memory. */
484 TEST_ASSERT( status == PSA_SUCCESS ||
485 status == PSA_ERROR_INSUFFICIENT_MEMORY );
486 if( status != PSA_SUCCESS )
487 break;
488 }
489 /* `max` is now the first slot number that wasn't filled. */
490 max -= 1;
491
492 for( slot = 1; slot <= max; slot++ )
493 {
494 TEST_ASSERT( psa_export_key( slot,
495 exported, sizeof( exported ),
496 &exported_size ) == PSA_SUCCESS );
497 TEST_ASSERT( exported_size == sizeof( slot ) );
498 TEST_ASSERT( memcmp( exported, &slot, sizeof( slot ) ) == 0 );
Gilles Peskine996deb12018-08-01 15:45:45 +0200499 }
500
501exit:
Gilles Peskine9a056342018-08-01 15:46:54 +0200502 /* Do not destroy the keys. mbedtls_psa_crypto_free() should do it. */
Gilles Peskine996deb12018-08-01 15:45:45 +0200503 mbedtls_psa_crypto_free( );
504}
505/* END_CASE */
506
507/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200508void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100509{
510 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200511 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100512 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100513
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100514 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300515 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100516 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
517
Gilles Peskine4abf7412018-06-18 16:35:34 +0200518 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200519 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100520 if( status == PSA_SUCCESS )
521 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
522
523exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100524 mbedtls_psa_crypto_free( );
525}
526/* END_CASE */
527
528/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200529void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
530{
531 int slot = 1;
532 size_t bits = bits_arg;
533 psa_status_t expected_status = expected_status_arg;
534 psa_status_t status;
535 psa_key_type_t type =
536 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
537 size_t buffer_size = /* Slight overapproximations */
538 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
539 unsigned char *buffer = mbedtls_calloc( 1, buffer_size );
540 unsigned char *p;
541 int ret;
542 size_t length;
543
544 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
545 TEST_ASSERT( buffer != NULL );
546
547 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
548 bits, keypair ) ) >= 0 );
549 length = ret;
550
551 /* Try importing the key */
552 status = psa_import_key( slot, type, p, length );
553 TEST_ASSERT( status == expected_status );
554 if( status == PSA_SUCCESS )
555 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
556
557exit:
558 mbedtls_free( buffer );
559 mbedtls_psa_crypto_free( );
560}
561/* END_CASE */
562
563/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300564void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300565 int type_arg,
566 int alg_arg,
567 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100568 int expected_bits,
569 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200570 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100571 int canonical_input )
572{
573 int slot = 1;
574 int slot2 = slot + 1;
575 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200576 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200577 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100578 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100579 unsigned char *exported = NULL;
580 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100581 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100582 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100583 size_t reexported_length;
584 psa_key_type_t got_type;
585 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200586 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100587
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100588 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300589 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +0300590 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100591 exported = mbedtls_calloc( 1, export_size );
592 TEST_ASSERT( exported != NULL );
593 if( ! canonical_input )
594 {
595 reexported = mbedtls_calloc( 1, export_size );
596 TEST_ASSERT( reexported != NULL );
597 }
598 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
599
mohammad1603a97cb8c2018-03-28 03:46:26 -0700600 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200601 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700602 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
603
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100604 /* Import the key */
605 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200606 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100607
608 /* Test the key information */
609 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200610 &got_type,
611 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100612 TEST_ASSERT( got_type == type );
613 TEST_ASSERT( got_bits == (size_t) expected_bits );
614
615 /* Export the key */
616 status = psa_export_key( slot,
617 exported, export_size,
618 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200619 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100620
621 /* The exported length must be set by psa_export_key() to a value between 0
622 * and export_size. On errors, the exported length must be 0. */
623 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
624 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
625 TEST_ASSERT( exported_length <= export_size );
626
Gilles Peskine3f669c32018-06-21 09:21:51 +0200627 TEST_ASSERT( mem_is_zero( exported + exported_length,
628 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100629 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200630 {
631 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100632 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200633 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100634
635 if( canonical_input )
636 {
Gilles Peskine4abf7412018-06-18 16:35:34 +0200637 TEST_ASSERT( exported_length == data->len );
638 TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100639 }
640 else
641 {
mohammad1603a97cb8c2018-03-28 03:46:26 -0700642 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
643
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100644 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200645 exported,
646 export_size ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100647 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200648 reexported,
649 export_size,
650 &reexported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100651 TEST_ASSERT( reexported_length == exported_length );
652 TEST_ASSERT( memcmp( reexported, exported,
653 exported_length ) == 0 );
654 }
655
656destroy:
657 /* Destroy the key */
658 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
659 TEST_ASSERT( psa_get_key_information(
660 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
661
662exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300663 mbedtls_free( exported );
664 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100665 mbedtls_psa_crypto_free( );
666}
667/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100668
Moran Pekerf709f4a2018-06-06 17:26:04 +0300669/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300670void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200671 int type_arg,
672 int alg_arg,
673 int expected_bits,
674 int public_key_expected_length,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200675 int expected_export_status_arg )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300676{
677 int slot = 1;
678 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200679 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200680 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300681 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300682 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300683 size_t export_size;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100684 size_t exported_length = INVALID_EXPORT_LENGTH;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300685 psa_key_type_t got_type;
686 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200687 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300688
Moran Pekerf709f4a2018-06-06 17:26:04 +0300689 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300690 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +0300691 export_size = (ptrdiff_t) data->len;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300692 exported = mbedtls_calloc( 1, export_size );
693 TEST_ASSERT( exported != NULL );
694
695 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
696
697 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200698 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300699 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
700
701 /* Import the key */
702 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200703 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300704
705 /* Test the key information */
706 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200707 &got_type,
708 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300709 TEST_ASSERT( got_type == type );
710 TEST_ASSERT( got_bits == (size_t) expected_bits );
711
712 /* Export the key */
713 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200714 exported, export_size,
715 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200716 TEST_ASSERT( status == expected_export_status );
Jaeden Amero2a671e92018-06-27 17:47:40 +0100717 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
718 TEST_ASSERT( mem_is_zero( exported + exported_length,
719 export_size - exported_length ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300720 if( status != PSA_SUCCESS )
721 goto destroy;
722
Moran Pekerf709f4a2018-06-06 17:26:04 +0300723destroy:
724 /* Destroy the key */
725 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
726 TEST_ASSERT( psa_get_key_information(
727 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
728
729exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300730 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300731 mbedtls_psa_crypto_free( );
732}
733/* END_CASE */
734
Gilles Peskine20035e32018-02-03 22:44:14 +0100735/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200736void import_and_exercise_key( data_t *data,
737 int type_arg,
738 int bits_arg,
739 int alg_arg )
740{
741 int slot = 1;
742 psa_key_type_t type = type_arg;
743 size_t bits = bits_arg;
744 psa_algorithm_t alg = alg_arg;
745 psa_key_usage_t usage =
746 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
747 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
748 PSA_KEY_USAGE_VERIFY :
749 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
750 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
751 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
752 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
753 PSA_KEY_USAGE_ENCRYPT :
754 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
Gilles Peskineea0fb492018-07-12 17:17:20 +0200755 PSA_ALG_IS_KEY_DERIVATION( alg ) ? PSA_KEY_USAGE_DERIVE :
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200756 0 );
757 psa_key_policy_t policy;
758 psa_key_type_t got_type;
759 size_t got_bits;
760 psa_status_t status;
761
762 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
763
764 psa_key_policy_init( &policy );
765 psa_key_policy_set_usage( &policy, usage, alg );
766 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
767
768 /* Import the key */
769 status = psa_import_key( slot, type, data->x, data->len );
770 TEST_ASSERT( status == PSA_SUCCESS );
771
772 /* Test the key information */
773 TEST_ASSERT( psa_get_key_information( slot,
774 &got_type,
775 &got_bits ) == PSA_SUCCESS );
776 TEST_ASSERT( got_type == type );
777 TEST_ASSERT( got_bits == bits );
778
779 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +0200780 if( ! exercise_key( slot, usage, alg ) )
781 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200782
783exit:
784 psa_destroy_key( slot );
785 mbedtls_psa_crypto_free( );
786}
787/* END_CASE */
788
789/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +0200790void key_policy( int usage_arg, int alg_arg )
791{
792 int key_slot = 1;
793 psa_algorithm_t alg = alg_arg;
794 psa_key_usage_t usage = usage_arg;
795 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
796 unsigned char key[32] = {0};
797 psa_key_policy_t policy_set;
798 psa_key_policy_t policy_get;
799
800 memset( key, 0x2a, sizeof( key ) );
801
802 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
803
804 psa_key_policy_init( &policy_set );
805 psa_key_policy_init( &policy_get );
806
807 psa_key_policy_set_usage( &policy_set, usage, alg );
808
809 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
810 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
811 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
812
813 TEST_ASSERT( psa_import_key( key_slot, key_type,
814 key, sizeof( key ) ) == PSA_SUCCESS );
815
816 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
817
818 TEST_ASSERT( policy_get.usage == policy_set.usage );
819 TEST_ASSERT( policy_get.alg == policy_set.alg );
820
821exit:
822 psa_destroy_key( key_slot );
823 mbedtls_psa_crypto_free( );
824}
825/* END_CASE */
826
827/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200828void mac_key_policy( int policy_usage,
829 int policy_alg,
830 int key_type,
831 data_t *key_data,
832 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200833{
834 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +0200835 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200836 psa_mac_operation_t operation;
837 psa_status_t status;
838 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200839
840 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
841
842 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200843 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200844 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
845
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200846 TEST_ASSERT( psa_import_key( key_slot, key_type,
847 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +0200848
Gilles Peskine89167cb2018-07-08 20:12:23 +0200849 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200850 if( policy_alg == exercise_alg &&
851 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
852 TEST_ASSERT( status == PSA_SUCCESS );
853 else
854 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
855 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200856
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200857 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +0200858 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200859 if( policy_alg == exercise_alg &&
860 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +0200861 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200862 else
863 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
864
865exit:
866 psa_mac_abort( &operation );
867 psa_destroy_key( key_slot );
868 mbedtls_psa_crypto_free( );
869}
870/* END_CASE */
871
872/* BEGIN_CASE */
873void cipher_key_policy( int policy_usage,
874 int policy_alg,
875 int key_type,
876 data_t *key_data,
877 int exercise_alg )
878{
879 int key_slot = 1;
880 psa_key_policy_t policy;
881 psa_cipher_operation_t operation;
882 psa_status_t status;
883
884 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
885
886 psa_key_policy_init( &policy );
887 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
888 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
889
890 TEST_ASSERT( psa_import_key( key_slot, key_type,
891 key_data->x, key_data->len ) == PSA_SUCCESS );
892
Gilles Peskinefe119512018-07-08 21:39:34 +0200893 status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200894 if( policy_alg == exercise_alg &&
895 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
896 TEST_ASSERT( status == PSA_SUCCESS );
897 else
898 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
899 psa_cipher_abort( &operation );
900
Gilles Peskinefe119512018-07-08 21:39:34 +0200901 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200902 if( policy_alg == exercise_alg &&
903 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
904 TEST_ASSERT( status == PSA_SUCCESS );
905 else
906 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
907
908exit:
909 psa_cipher_abort( &operation );
910 psa_destroy_key( key_slot );
911 mbedtls_psa_crypto_free( );
912}
913/* END_CASE */
914
915/* BEGIN_CASE */
916void aead_key_policy( int policy_usage,
917 int policy_alg,
918 int key_type,
919 data_t *key_data,
920 int nonce_length_arg,
921 int tag_length_arg,
922 int exercise_alg )
923{
924 int key_slot = 1;
925 psa_key_policy_t policy;
926 psa_status_t status;
927 unsigned char nonce[16] = {0};
928 size_t nonce_length = nonce_length_arg;
929 unsigned char tag[16];
930 size_t tag_length = tag_length_arg;
931 size_t output_length;
932
933 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
934 TEST_ASSERT( tag_length <= sizeof( tag ) );
935
936 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
937
938 psa_key_policy_init( &policy );
939 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
940 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
941
942 TEST_ASSERT( psa_import_key( key_slot, key_type,
943 key_data->x, key_data->len ) == PSA_SUCCESS );
944
945 status = psa_aead_encrypt( key_slot, exercise_alg,
946 nonce, nonce_length,
947 NULL, 0,
948 NULL, 0,
949 tag, tag_length,
950 &output_length );
951 if( policy_alg == exercise_alg &&
952 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
953 TEST_ASSERT( status == PSA_SUCCESS );
954 else
955 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
956
957 memset( tag, 0, sizeof( tag ) );
958 status = psa_aead_decrypt( key_slot, exercise_alg,
959 nonce, nonce_length,
960 NULL, 0,
961 tag, tag_length,
962 NULL, 0,
963 &output_length );
964 if( policy_alg == exercise_alg &&
965 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
966 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
967 else
968 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
969
970exit:
971 psa_destroy_key( key_slot );
972 mbedtls_psa_crypto_free( );
973}
974/* END_CASE */
975
976/* BEGIN_CASE */
977void asymmetric_encryption_key_policy( int policy_usage,
978 int policy_alg,
979 int key_type,
980 data_t *key_data,
981 int exercise_alg )
982{
983 int key_slot = 1;
984 psa_key_policy_t policy;
985 psa_status_t status;
986 size_t key_bits;
987 size_t buffer_length;
988 unsigned char *buffer = NULL;
989 size_t output_length;
990
991 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
992
993 psa_key_policy_init( &policy );
994 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
995 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
996
997 TEST_ASSERT( psa_import_key( key_slot, key_type,
998 key_data->x, key_data->len ) == PSA_SUCCESS );
999
1000 TEST_ASSERT( psa_get_key_information( key_slot,
1001 NULL,
1002 &key_bits ) == PSA_SUCCESS );
1003 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1004 exercise_alg );
1005 buffer = mbedtls_calloc( 1, buffer_length );
1006 TEST_ASSERT( buffer != NULL );
1007
1008 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
1009 NULL, 0,
1010 NULL, 0,
1011 buffer, buffer_length,
1012 &output_length );
1013 if( policy_alg == exercise_alg &&
1014 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1015 TEST_ASSERT( status == PSA_SUCCESS );
1016 else
1017 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1018
1019 memset( buffer, 0, buffer_length );
1020 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
1021 buffer, buffer_length,
1022 NULL, 0,
1023 buffer, buffer_length,
1024 &output_length );
1025 if( policy_alg == exercise_alg &&
1026 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1027 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
1028 else
1029 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1030
1031exit:
1032 psa_destroy_key( key_slot );
1033 mbedtls_psa_crypto_free( );
1034 mbedtls_free( buffer );
1035}
1036/* END_CASE */
1037
1038/* BEGIN_CASE */
1039void asymmetric_signature_key_policy( int policy_usage,
1040 int policy_alg,
1041 int key_type,
1042 data_t *key_data,
1043 int exercise_alg )
1044{
1045 int key_slot = 1;
1046 psa_key_policy_t policy;
1047 psa_status_t status;
1048 unsigned char payload[16] = {1};
1049 size_t payload_length = sizeof( payload );
1050 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1051 size_t signature_length;
1052
1053 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1054
1055 psa_key_policy_init( &policy );
1056 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1057 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1058
1059 TEST_ASSERT( psa_import_key( key_slot, key_type,
1060 key_data->x, key_data->len ) == PSA_SUCCESS );
1061
1062 status = psa_asymmetric_sign( key_slot, exercise_alg,
1063 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001064 signature, sizeof( signature ),
1065 &signature_length );
1066 if( policy_alg == exercise_alg &&
1067 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1068 TEST_ASSERT( status == PSA_SUCCESS );
1069 else
1070 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1071
1072 memset( signature, 0, sizeof( signature ) );
1073 status = psa_asymmetric_verify( key_slot, exercise_alg,
1074 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001075 signature, sizeof( signature ) );
1076 if( policy_alg == exercise_alg &&
1077 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1078 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1079 else
1080 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001081
1082exit:
1083 psa_destroy_key( key_slot );
1084 mbedtls_psa_crypto_free( );
1085}
1086/* END_CASE */
1087
1088/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001089void derive_key_policy( int policy_usage,
1090 int policy_alg,
1091 int key_type,
1092 data_t *key_data,
1093 int exercise_alg )
1094{
1095 int key_slot = 1;
1096 psa_key_policy_t policy;
1097 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1098 psa_status_t status;
1099
1100 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1101
1102 psa_key_policy_init( &policy );
1103 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1104 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1105
1106 TEST_ASSERT( psa_import_key( key_slot, key_type,
1107 key_data->x, key_data->len ) == PSA_SUCCESS );
1108
1109 status = psa_key_derivation( &generator, key_slot,
1110 exercise_alg,
1111 NULL, 0,
1112 NULL, 0,
1113 1 );
1114 if( policy_alg == exercise_alg &&
1115 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1116 TEST_ASSERT( status == PSA_SUCCESS );
1117 else
1118 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1119
1120exit:
1121 psa_generator_abort( &generator );
1122 psa_destroy_key( key_slot );
1123 mbedtls_psa_crypto_free( );
1124}
1125/* END_CASE */
1126
1127/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001128void key_lifetime( int lifetime_arg )
1129{
1130 int key_slot = 1;
1131 psa_key_type_t key_type = PSA_ALG_CBC_BASE;
1132 unsigned char key[32] = {0};
1133 psa_key_lifetime_t lifetime_set = lifetime_arg;
1134 psa_key_lifetime_t lifetime_get;
1135
1136 memset( key, 0x2a, sizeof( key ) );
1137
1138 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1139
1140 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1141 lifetime_set ) == PSA_SUCCESS );
1142
1143 TEST_ASSERT( psa_import_key( key_slot, key_type,
1144 key, sizeof( key ) ) == PSA_SUCCESS );
1145
1146 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1147 &lifetime_get ) == PSA_SUCCESS );
1148
1149 TEST_ASSERT( lifetime_get == lifetime_set );
1150
1151exit:
1152 psa_destroy_key( key_slot );
1153 mbedtls_psa_crypto_free( );
1154}
1155/* END_CASE */
1156
1157/* BEGIN_CASE */
1158void key_lifetime_set_fail( int key_slot_arg,
1159 int lifetime_arg,
1160 int expected_status_arg )
1161{
1162 psa_key_slot_t key_slot = key_slot_arg;
1163 psa_key_lifetime_t lifetime_set = lifetime_arg;
1164 psa_status_t actual_status;
1165 psa_status_t expected_status = expected_status_arg;
1166
1167 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1168
1169 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1170
1171 if( actual_status == PSA_SUCCESS )
1172 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1173
1174 TEST_ASSERT( expected_status == actual_status );
1175
1176exit:
1177 psa_destroy_key( key_slot );
1178 mbedtls_psa_crypto_free( );
1179}
1180/* END_CASE */
1181
1182/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001183void hash_setup( int alg_arg,
1184 int expected_status_arg )
1185{
1186 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001187 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001188 psa_hash_operation_t operation;
1189 psa_status_t status;
1190
1191 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1192
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001193 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001194 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001195 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001196
1197exit:
1198 mbedtls_psa_crypto_free( );
1199}
1200/* END_CASE */
1201
1202/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001203void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001204{
1205 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001206 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001207 size_t actual_hash_length;
1208 psa_hash_operation_t operation;
1209
Gilles Peskine69c12672018-06-28 00:07:19 +02001210 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1211 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1212
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001213 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001214 TEST_ASSERT( expected_hash != NULL );
1215 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1216 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001217
1218 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1219
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001220 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001221 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001222 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001223 TEST_ASSERT( psa_hash_finish( &operation,
1224 actual_hash, sizeof( actual_hash ),
1225 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001226 TEST_ASSERT( actual_hash_length == expected_hash->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001227 TEST_ASSERT( memcmp( expected_hash->x, actual_hash,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001228 expected_hash->len ) == 0 );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001229
1230exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001231 mbedtls_psa_crypto_free( );
1232}
1233/* END_CASE */
1234
1235/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001236void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001237{
1238 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001239 psa_hash_operation_t operation;
1240
Gilles Peskine69c12672018-06-28 00:07:19 +02001241 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1242 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1243
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001244 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001245 TEST_ASSERT( expected_hash != NULL );
1246 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1247 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001248
1249 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1250
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001251 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001252 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001253 input->x,
1254 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001255 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001256 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001257 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001258
1259exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001260 mbedtls_psa_crypto_free( );
1261}
1262/* END_CASE */
1263
1264/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001265void mac_setup( int key_type_arg,
1266 data_t *key,
1267 int alg_arg,
1268 int expected_status_arg )
1269{
1270 int key_slot = 1;
1271 psa_key_type_t key_type = key_type_arg;
1272 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001273 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001274 psa_mac_operation_t operation;
1275 psa_key_policy_t policy;
1276 psa_status_t status;
1277
1278 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1279
1280 psa_key_policy_init( &policy );
1281 psa_key_policy_set_usage( &policy,
1282 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1283 alg );
1284 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1285
1286 TEST_ASSERT( psa_import_key( key_slot, key_type,
1287 key->x, key->len ) == PSA_SUCCESS );
1288
Gilles Peskine89167cb2018-07-08 20:12:23 +02001289 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001290 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001291 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001292
1293exit:
1294 psa_destroy_key( key_slot );
1295 mbedtls_psa_crypto_free( );
1296}
1297/* END_CASE */
1298
1299/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001300void mac_verify( int key_type_arg,
1301 data_t *key,
1302 int alg_arg,
1303 data_t *input,
1304 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001305{
1306 int key_slot = 1;
1307 psa_key_type_t key_type = key_type_arg;
1308 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001309 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001310 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001311
Gilles Peskine69c12672018-06-28 00:07:19 +02001312 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1313
Gilles Peskine8c9def32018-02-08 10:02:12 +01001314 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001315 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001316 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001317 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001318 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1319 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001320
1321 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1322
mohammad16036df908f2018-04-02 08:34:15 -07001323 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001324 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07001325 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1326
Gilles Peskine8c9def32018-02-08 10:02:12 +01001327 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001328 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001329
Gilles Peskine89167cb2018-07-08 20:12:23 +02001330 TEST_ASSERT( psa_mac_verify_setup( &operation,
1331 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001332 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1333 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001334 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02001335 TEST_ASSERT( psa_mac_verify_finish( &operation,
1336 expected_mac->x,
1337 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001338
1339exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001340 psa_destroy_key( key_slot );
1341 mbedtls_psa_crypto_free( );
1342}
1343/* END_CASE */
1344
1345/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001346void cipher_setup( int key_type_arg,
1347 data_t *key,
1348 int alg_arg,
1349 int expected_status_arg )
1350{
1351 int key_slot = 1;
1352 psa_key_type_t key_type = key_type_arg;
1353 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001354 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001355 psa_cipher_operation_t operation;
1356 psa_key_policy_t policy;
1357 psa_status_t status;
1358
1359 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1360
1361 psa_key_policy_init( &policy );
1362 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1363 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1364
1365 TEST_ASSERT( psa_import_key( key_slot, key_type,
1366 key->x, key->len ) == PSA_SUCCESS );
1367
Gilles Peskinefe119512018-07-08 21:39:34 +02001368 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001369 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001370 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001371
1372exit:
1373 psa_destroy_key( key_slot );
1374 mbedtls_psa_crypto_free( );
1375}
1376/* END_CASE */
1377
1378/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001379void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001380 data_t *key,
1381 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001382 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001383{
1384 int key_slot = 1;
1385 psa_status_t status;
1386 psa_key_type_t key_type = key_type_arg;
1387 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001388 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001389 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001390 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001391 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001392 size_t output_buffer_size = 0;
1393 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001394 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001395 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001396 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001397
Gilles Peskine50e586b2018-06-08 14:28:46 +02001398 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001399 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001400 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001401 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1402 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1403 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001404
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001405 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1406 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001407
1408 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1409
Moran Pekered346952018-07-05 15:22:45 +03001410 psa_key_policy_init( &policy );
1411 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1412 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1413
Gilles Peskine50e586b2018-06-08 14:28:46 +02001414 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001415 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001416
Gilles Peskinefe119512018-07-08 21:39:34 +02001417 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1418 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001419
Gilles Peskinefe119512018-07-08 21:39:34 +02001420 TEST_ASSERT( psa_cipher_set_iv( &operation,
1421 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001422 output_buffer_size = (size_t) input->len +
1423 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001424 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001425 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001426
Gilles Peskine4abf7412018-06-18 16:35:34 +02001427 TEST_ASSERT( psa_cipher_update( &operation,
1428 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001429 output, output_buffer_size,
1430 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001431 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001432 status = psa_cipher_finish( &operation,
1433 output + function_output_length,
1434 output_buffer_size,
1435 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001436 total_output_length += function_output_length;
1437
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001438 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001439 if( expected_status == PSA_SUCCESS )
1440 {
1441 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001442 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001443 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001444 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001445 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001446
Gilles Peskine50e586b2018-06-08 14:28:46 +02001447exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001448 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001449 psa_destroy_key( key_slot );
1450 mbedtls_psa_crypto_free( );
1451}
1452/* END_CASE */
1453
1454/* BEGIN_CASE */
1455void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001456 data_t *key,
1457 data_t *input,
1458 int first_part_size,
1459 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001460{
1461 int key_slot = 1;
1462 psa_key_type_t key_type = key_type_arg;
1463 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001464 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001465 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001466 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001467 size_t output_buffer_size = 0;
1468 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001469 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001470 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001471 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001472
Gilles Peskine50e586b2018-06-08 14:28:46 +02001473 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001474 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001475 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001476 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1477 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1478 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001479
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001480 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1481 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001482
1483 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1484
Moran Pekered346952018-07-05 15:22:45 +03001485 psa_key_policy_init( &policy );
1486 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1487 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1488
Gilles Peskine50e586b2018-06-08 14:28:46 +02001489 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001490 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001491
Gilles Peskinefe119512018-07-08 21:39:34 +02001492 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1493 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001494
Gilles Peskinefe119512018-07-08 21:39:34 +02001495 TEST_ASSERT( psa_cipher_set_iv( &operation,
1496 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001497 output_buffer_size = (size_t) input->len +
1498 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001499 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001500 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001501
Gilles Peskine4abf7412018-06-18 16:35:34 +02001502 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001503 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001504 output, output_buffer_size,
1505 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001506 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001507 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001508 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001509 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001510 output, output_buffer_size,
1511 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001512 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001513 TEST_ASSERT( psa_cipher_finish( &operation,
1514 output + function_output_length,
1515 output_buffer_size,
1516 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001517 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001518 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1519
Gilles Peskine4abf7412018-06-18 16:35:34 +02001520 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001521 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001522 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001523
1524exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001525 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001526 psa_destroy_key( key_slot );
1527 mbedtls_psa_crypto_free( );
1528}
1529/* END_CASE */
1530
1531/* BEGIN_CASE */
1532void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001533 data_t *key,
1534 data_t *input,
1535 int first_part_size,
1536 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001537{
1538 int key_slot = 1;
1539
1540 psa_key_type_t key_type = key_type_arg;
1541 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001542 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001543 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001544 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001545 size_t output_buffer_size = 0;
1546 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001547 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001548 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001549 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001550
Gilles Peskine50e586b2018-06-08 14:28:46 +02001551 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001552 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001553 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001554 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1555 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1556 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001557
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001558 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1559 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001560
1561 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1562
Moran Pekered346952018-07-05 15:22:45 +03001563 psa_key_policy_init( &policy );
1564 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1565 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1566
Gilles Peskine50e586b2018-06-08 14:28:46 +02001567 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001568 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001569
Gilles Peskinefe119512018-07-08 21:39:34 +02001570 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1571 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001572
Gilles Peskinefe119512018-07-08 21:39:34 +02001573 TEST_ASSERT( psa_cipher_set_iv( &operation,
1574 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001575
mohammad16033d91abe2018-07-03 13:15:54 +03001576 output_buffer_size = (size_t) input->len +
1577 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001578 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001579 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001580
Gilles Peskine4abf7412018-06-18 16:35:34 +02001581 TEST_ASSERT( (unsigned int) first_part_size < input->len );
1582 TEST_ASSERT( psa_cipher_update( &operation,
1583 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001584 output, output_buffer_size,
1585 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001586 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001587 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001588 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001589 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001590 output, output_buffer_size,
1591 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001592 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001593 TEST_ASSERT( psa_cipher_finish( &operation,
1594 output + function_output_length,
1595 output_buffer_size,
1596 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001597 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001598 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1599
Gilles Peskine4abf7412018-06-18 16:35:34 +02001600 TEST_ASSERT( total_output_length == expected_output->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001601 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001602 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001603
1604exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001605 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001606 psa_destroy_key( key_slot );
1607 mbedtls_psa_crypto_free( );
1608}
1609/* END_CASE */
1610
Gilles Peskine50e586b2018-06-08 14:28:46 +02001611/* BEGIN_CASE */
1612void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001613 data_t *key,
1614 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001615 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001616{
1617 int key_slot = 1;
1618 psa_status_t status;
1619 psa_key_type_t key_type = key_type_arg;
1620 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001621 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001622 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001623 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001624 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001625 size_t output_buffer_size = 0;
1626 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001627 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001628 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001629 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001630
Gilles Peskine50e586b2018-06-08 14:28:46 +02001631 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001632 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001633 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001634 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1635 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1636 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001637
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001638 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1639 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001640
1641 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1642
Moran Pekered346952018-07-05 15:22:45 +03001643 psa_key_policy_init( &policy );
1644 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1645 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1646
Gilles Peskine50e586b2018-06-08 14:28:46 +02001647 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001648 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001649
Gilles Peskinefe119512018-07-08 21:39:34 +02001650 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1651 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001652
Gilles Peskinefe119512018-07-08 21:39:34 +02001653 TEST_ASSERT( psa_cipher_set_iv( &operation,
1654 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001655
mohammad16033d91abe2018-07-03 13:15:54 +03001656 output_buffer_size = (size_t) input->len +
1657 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001658 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001659 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001660
Gilles Peskine4abf7412018-06-18 16:35:34 +02001661 TEST_ASSERT( psa_cipher_update( &operation,
1662 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001663 output, output_buffer_size,
1664 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001665 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001666 status = psa_cipher_finish( &operation,
1667 output + function_output_length,
1668 output_buffer_size,
1669 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001670 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001671 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001672
1673 if( expected_status == PSA_SUCCESS )
1674 {
1675 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001676 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001677 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001678 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001679 }
1680
Gilles Peskine50e586b2018-06-08 14:28:46 +02001681exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001682 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001683 psa_destroy_key( key_slot );
1684 mbedtls_psa_crypto_free( );
1685}
1686/* END_CASE */
1687
Gilles Peskine50e586b2018-06-08 14:28:46 +02001688/* BEGIN_CASE */
1689void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001690 data_t *key,
1691 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02001692{
1693 int key_slot = 1;
1694 psa_key_type_t key_type = key_type_arg;
1695 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07001696 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02001697 size_t iv_size = 16;
1698 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001699 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001700 size_t output1_size = 0;
1701 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001702 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001703 size_t output2_size = 0;
1704 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001705 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001706 psa_cipher_operation_t operation1;
1707 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03001708 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001709
mohammad1603d7d7ba52018-03-12 18:51:53 +02001710 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001711 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001712 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1713 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001714
mohammad1603d7d7ba52018-03-12 18:51:53 +02001715 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1716
Moran Pekered346952018-07-05 15:22:45 +03001717 psa_key_policy_init( &policy );
1718 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
1719 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1720
mohammad1603d7d7ba52018-03-12 18:51:53 +02001721 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001722 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001723
Gilles Peskinefe119512018-07-08 21:39:34 +02001724 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
1725 key_slot, alg ) == PSA_SUCCESS );
1726 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
1727 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001728
Gilles Peskinefe119512018-07-08 21:39:34 +02001729 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
1730 iv, iv_size,
1731 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001732 output1_size = (size_t) input->len +
1733 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001734 output1 = mbedtls_calloc( 1, output1_size );
1735 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001736
Gilles Peskine4abf7412018-06-18 16:35:34 +02001737 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001738 output1, output1_size,
1739 &output1_length ) == PSA_SUCCESS );
1740 TEST_ASSERT( psa_cipher_finish( &operation1,
1741 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001742 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001743
Gilles Peskine048b7f02018-06-08 14:20:49 +02001744 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001745
1746 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1747
1748 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001749 output2 = mbedtls_calloc( 1, output2_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001750 TEST_ASSERT( output2 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001751
Gilles Peskinefe119512018-07-08 21:39:34 +02001752 TEST_ASSERT( psa_cipher_set_iv( &operation2,
1753 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001754 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
1755 output2, output2_size,
1756 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001757 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001758 TEST_ASSERT( psa_cipher_finish( &operation2,
1759 output2 + output2_length,
1760 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001761 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001762
Gilles Peskine048b7f02018-06-08 14:20:49 +02001763 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001764
Janos Follath25c4fa82018-07-06 16:23:25 +01001765 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001766
Gilles Peskine4abf7412018-06-18 16:35:34 +02001767 TEST_ASSERT( input->len == output2_length );
1768 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +03001769
1770exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001771 mbedtls_free( output1 );
1772 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03001773 psa_destroy_key( key_slot );
1774 mbedtls_psa_crypto_free( );
1775}
1776/* END_CASE */
1777
1778/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001779void cipher_verify_output_multipart( int alg_arg,
1780 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001781 data_t *key,
1782 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001783 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03001784{
1785 int key_slot = 1;
1786 psa_key_type_t key_type = key_type_arg;
1787 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03001788 unsigned char iv[16] = {0};
1789 size_t iv_size = 16;
1790 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001791 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001792 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001793 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001794 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001795 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001796 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001797 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001798 psa_cipher_operation_t operation1;
1799 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03001800 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03001801
Moran Pekerded84402018-06-06 16:36:50 +03001802 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001803 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001804 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1805 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001806
Moran Pekerded84402018-06-06 16:36:50 +03001807 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1808
Moran Pekered346952018-07-05 15:22:45 +03001809 psa_key_policy_init( &policy );
1810 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
1811 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1812
Moran Pekerded84402018-06-06 16:36:50 +03001813 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001814 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001815
Gilles Peskinefe119512018-07-08 21:39:34 +02001816 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
1817 key_slot, alg ) == PSA_SUCCESS );
1818 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
1819 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001820
Gilles Peskinefe119512018-07-08 21:39:34 +02001821 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
1822 iv, iv_size,
1823 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001824 output1_buffer_size = (size_t) input->len +
1825 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001826 output1 = mbedtls_calloc( 1, output1_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001827 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001828
Gilles Peskine4abf7412018-06-18 16:35:34 +02001829 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001830
itayzafrir3e02b3b2018-06-12 17:06:52 +03001831 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001832 output1, output1_buffer_size,
1833 &function_output_length ) == PSA_SUCCESS );
1834 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001835
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001836 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001837 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001838 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001839 output1, output1_buffer_size,
1840 &function_output_length ) == PSA_SUCCESS );
1841 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001842
Gilles Peskine048b7f02018-06-08 14:20:49 +02001843 TEST_ASSERT( psa_cipher_finish( &operation1,
1844 output1 + output1_length,
1845 output1_buffer_size - output1_length,
1846 &function_output_length ) == PSA_SUCCESS );
1847 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001848
1849 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1850
Gilles Peskine048b7f02018-06-08 14:20:49 +02001851 output2_buffer_size = output1_length;
1852 output2 = mbedtls_calloc( 1, output2_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001853 TEST_ASSERT( output2 != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001854
Gilles Peskinefe119512018-07-08 21:39:34 +02001855 TEST_ASSERT( psa_cipher_set_iv( &operation2,
1856 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001857
1858 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001859 output2, output2_buffer_size,
1860 &function_output_length ) == PSA_SUCCESS );
1861 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001862
Gilles Peskine048b7f02018-06-08 14:20:49 +02001863 TEST_ASSERT( psa_cipher_update( &operation2,
1864 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001865 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001866 output2, output2_buffer_size,
1867 &function_output_length ) == PSA_SUCCESS );
1868 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001869
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001870 TEST_ASSERT( psa_cipher_finish( &operation2,
1871 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001872 output2_buffer_size - output2_length,
1873 &function_output_length ) == PSA_SUCCESS );
1874 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001875
Janos Follath25c4fa82018-07-06 16:23:25 +01001876 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001877
Gilles Peskine4abf7412018-06-18 16:35:34 +02001878 TEST_ASSERT( input->len == output2_length );
1879 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001880
1881exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001882 mbedtls_free( output1 );
1883 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001884 psa_destroy_key( key_slot );
1885 mbedtls_psa_crypto_free( );
1886}
1887/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02001888
Gilles Peskine20035e32018-02-03 22:44:14 +01001889/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001890void aead_encrypt_decrypt( int key_type_arg,
1891 data_t * key_data,
1892 int alg_arg,
1893 data_t * input_data,
1894 data_t * nonce,
1895 data_t * additional_data,
1896 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001897{
1898 int slot = 1;
1899 psa_key_type_t key_type = key_type_arg;
1900 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001901 unsigned char *output_data = NULL;
1902 size_t output_size = 0;
1903 size_t output_length = 0;
1904 unsigned char *output_data2 = NULL;
1905 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001906 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001907 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001908 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001909
Gilles Peskinea1cac842018-06-11 19:33:02 +02001910 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001911 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001912 TEST_ASSERT( nonce != NULL );
1913 TEST_ASSERT( additional_data != NULL );
1914 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1915 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1916 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1917 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1918
Gilles Peskine4abf7412018-06-18 16:35:34 +02001919 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001920 output_data = mbedtls_calloc( 1, output_size );
1921 TEST_ASSERT( output_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001922
1923 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1924
1925 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001926 psa_key_policy_set_usage( &policy,
1927 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
1928 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001929 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1930
1931 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001932 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001933
1934 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001935 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001936 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001937 additional_data->len,
1938 input_data->x, input_data->len,
1939 output_data, output_size,
1940 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001941
1942 if( PSA_SUCCESS == expected_result )
1943 {
1944 output_data2 = mbedtls_calloc( 1, output_length );
1945 TEST_ASSERT( output_data2 != NULL );
1946
1947 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001948 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001949 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001950 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001951 output_data, output_length,
1952 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001953 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02001954
itayzafrir3e02b3b2018-06-12 17:06:52 +03001955 TEST_ASSERT( memcmp( input_data->x, output_data2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001956 input_data->len ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001957 }
Gilles Peskine2d277862018-06-18 15:41:12 +02001958
Gilles Peskinea1cac842018-06-11 19:33:02 +02001959exit:
1960 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001961 mbedtls_free( output_data );
1962 mbedtls_free( output_data2 );
1963 mbedtls_psa_crypto_free( );
1964}
1965/* END_CASE */
1966
1967/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001968void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001969 int alg_arg, data_t * input_data,
1970 data_t * additional_data, data_t * nonce,
1971 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001972{
1973 int slot = 1;
1974 psa_key_type_t key_type = key_type_arg;
1975 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001976 unsigned char *output_data = NULL;
1977 size_t output_size = 0;
1978 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001979 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001980 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001981
Gilles Peskinea1cac842018-06-11 19:33:02 +02001982 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001983 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001984 TEST_ASSERT( additional_data != NULL );
1985 TEST_ASSERT( nonce != NULL );
1986 TEST_ASSERT( expected_result != NULL );
1987 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1988 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1989 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1990 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1991 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
1992
Gilles Peskine4abf7412018-06-18 16:35:34 +02001993 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001994 output_data = mbedtls_calloc( 1, output_size );
1995 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001996
Gilles Peskinea1cac842018-06-11 19:33:02 +02001997 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1998
1999 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002000 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002001 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2002
2003 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002004 key_data->x,
2005 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002006
2007 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002008 nonce->x, nonce->len,
2009 additional_data->x, additional_data->len,
2010 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002011 output_data, output_size,
2012 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002013
itayzafrir3e02b3b2018-06-12 17:06:52 +03002014 TEST_ASSERT( memcmp( output_data, expected_result->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02002015 output_length ) == 0 );
2016
Gilles Peskinea1cac842018-06-11 19:33:02 +02002017exit:
2018 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002019 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002020 mbedtls_psa_crypto_free( );
2021}
2022/* END_CASE */
2023
2024/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002025void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002026 int alg_arg, data_t * input_data,
2027 data_t * additional_data, data_t * nonce,
2028 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002029{
2030 int slot = 1;
2031 psa_key_type_t key_type = key_type_arg;
2032 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002033 unsigned char *output_data = NULL;
2034 size_t output_size = 0;
2035 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002036 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002037 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002038 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002039
Gilles Peskinea1cac842018-06-11 19:33:02 +02002040 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002041 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002042 TEST_ASSERT( additional_data != NULL );
2043 TEST_ASSERT( nonce != NULL );
2044 TEST_ASSERT( expected_data != NULL );
2045 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2046 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2047 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2048 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2049 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2050
Gilles Peskine4abf7412018-06-18 16:35:34 +02002051 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002052 output_data = mbedtls_calloc( 1, output_size );
2053 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02002054
Gilles Peskinea1cac842018-06-11 19:33:02 +02002055 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2056
2057 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002058 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002059 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2060
2061 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002062 key_data->x,
2063 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002064
2065 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002066 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002067 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002068 additional_data->len,
2069 input_data->x, input_data->len,
2070 output_data, output_size,
2071 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002072
Gilles Peskine2d277862018-06-18 15:41:12 +02002073 if( expected_result == PSA_SUCCESS )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002074 {
itayzafrir3e02b3b2018-06-12 17:06:52 +03002075 TEST_ASSERT( memcmp( output_data, expected_data->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02002076 output_length ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002077 }
2078
Gilles Peskinea1cac842018-06-11 19:33:02 +02002079exit:
2080 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002081 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002082 mbedtls_psa_crypto_free( );
2083}
2084/* END_CASE */
2085
2086/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002087void signature_size( int type_arg,
2088 int bits,
2089 int alg_arg,
2090 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002091{
2092 psa_key_type_t type = type_arg;
2093 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002094 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002095 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2096exit:
2097 ;
2098}
2099/* END_CASE */
2100
2101/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002102void sign_deterministic( int key_type_arg, data_t *key_data,
2103 int alg_arg, data_t *input_data,
2104 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002105{
2106 int slot = 1;
2107 psa_key_type_t key_type = key_type_arg;
2108 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002109 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002110 unsigned char *signature = NULL;
2111 size_t signature_size;
2112 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002113 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002114
Gilles Peskine20035e32018-02-03 22:44:14 +01002115 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002116 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002117 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002118 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2119 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2120 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002121
2122 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2123
mohammad1603a97cb8c2018-03-28 03:46:26 -07002124 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002125 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002126 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2127
Gilles Peskine20035e32018-02-03 22:44:14 +01002128 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002129 key_data->x,
2130 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002131 TEST_ASSERT( psa_get_key_information( slot,
2132 NULL,
2133 &key_bits ) == PSA_SUCCESS );
2134
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002135 /* Allocate a buffer which has the size advertized by the
2136 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002137 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2138 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002139 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002140 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine20035e32018-02-03 22:44:14 +01002141 signature = mbedtls_calloc( 1, signature_size );
2142 TEST_ASSERT( signature != NULL );
2143
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002144 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002145 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002146 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002147 signature, signature_size,
2148 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002149 /* Verify that the signature is what is expected. */
Gilles Peskine4abf7412018-06-18 16:35:34 +02002150 TEST_ASSERT( signature_length == output_data->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02002151 TEST_ASSERT( memcmp( signature, output_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002152 output_data->len ) == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01002153
2154exit:
2155 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002156 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002157 mbedtls_psa_crypto_free( );
2158}
2159/* END_CASE */
2160
2161/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002162void sign_fail( int key_type_arg, data_t *key_data,
2163 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002164 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002165{
2166 int slot = 1;
2167 psa_key_type_t key_type = key_type_arg;
2168 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002169 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002170 psa_status_t actual_status;
2171 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002172 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002173 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002174 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002175
Gilles Peskine20035e32018-02-03 22:44:14 +01002176 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002177 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002178 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2179 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2180
Gilles Peskine20035e32018-02-03 22:44:14 +01002181 signature = mbedtls_calloc( 1, signature_size );
2182 TEST_ASSERT( signature != NULL );
2183
2184 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2185
mohammad1603a97cb8c2018-03-28 03:46:26 -07002186 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002187 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002188 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2189
Gilles Peskine20035e32018-02-03 22:44:14 +01002190 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002191 key_data->x,
2192 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002193
2194 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002195 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002196 signature, signature_size,
2197 &signature_length );
2198 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002199 /* The value of *signature_length is unspecified on error, but
2200 * whatever it is, it should be less than signature_size, so that
2201 * if the caller tries to read *signature_length bytes without
2202 * checking the error code then they don't overflow a buffer. */
2203 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002204
2205exit:
2206 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002207 mbedtls_free( signature );
2208 mbedtls_psa_crypto_free( );
2209}
2210/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002211
2212/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002213void sign_verify( int key_type_arg, data_t *key_data,
2214 int alg_arg, data_t *input_data )
2215{
2216 int slot = 1;
2217 psa_key_type_t key_type = key_type_arg;
2218 psa_algorithm_t alg = alg_arg;
2219 size_t key_bits;
2220 unsigned char *signature = NULL;
2221 size_t signature_size;
2222 size_t signature_length = 0xdeadbeef;
2223 psa_key_policy_t policy;
2224
2225 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2226
2227 psa_key_policy_init( &policy );
2228 psa_key_policy_set_usage( &policy,
2229 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2230 alg );
2231 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2232
2233 TEST_ASSERT( psa_import_key( slot, key_type,
2234 key_data->x,
2235 key_data->len ) == PSA_SUCCESS );
2236 TEST_ASSERT( psa_get_key_information( slot,
2237 NULL,
2238 &key_bits ) == PSA_SUCCESS );
2239
2240 /* Allocate a buffer which has the size advertized by the
2241 * library. */
2242 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2243 key_bits, alg );
2244 TEST_ASSERT( signature_size != 0 );
2245 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2246 signature = mbedtls_calloc( 1, signature_size );
2247 TEST_ASSERT( signature != NULL );
2248
2249 /* Perform the signature. */
2250 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2251 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002252 signature, signature_size,
2253 &signature_length ) == PSA_SUCCESS );
2254 /* Check that the signature length looks sensible. */
2255 TEST_ASSERT( signature_length <= signature_size );
2256 TEST_ASSERT( signature_length > 0 );
2257
2258 /* Use the library to verify that the signature is correct. */
2259 TEST_ASSERT( psa_asymmetric_verify(
2260 slot, alg,
2261 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002262 signature, signature_length ) == PSA_SUCCESS );
2263
2264 if( input_data->len != 0 )
2265 {
2266 /* Flip a bit in the input and verify that the signature is now
2267 * detected as invalid. Flip a bit at the beginning, not at the end,
2268 * because ECDSA may ignore the last few bits of the input. */
2269 input_data->x[0] ^= 1;
2270 TEST_ASSERT( psa_asymmetric_verify(
2271 slot, alg,
2272 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002273 signature,
2274 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2275 }
2276
2277exit:
2278 psa_destroy_key( slot );
2279 mbedtls_free( signature );
2280 mbedtls_psa_crypto_free( );
2281}
2282/* END_CASE */
2283
2284/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002285void asymmetric_verify( int key_type_arg, data_t *key_data,
2286 int alg_arg, data_t *hash_data,
2287 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002288{
2289 int slot = 1;
2290 psa_key_type_t key_type = key_type_arg;
2291 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002292 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002293
Gilles Peskine69c12672018-06-28 00:07:19 +02002294 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2295
itayzafrir5c753392018-05-08 11:18:38 +03002296 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002297 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002298 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002299 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2300 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2301 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002302
2303 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2304
2305 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002306 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002307 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2308
2309 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002310 key_data->x,
2311 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002312
2313 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002314 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002315 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002316 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002317exit:
2318 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002319 mbedtls_psa_crypto_free( );
2320}
2321/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002322
2323/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002324void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2325 int alg_arg, data_t *hash_data,
2326 data_t *signature_data,
2327 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002328{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002329 int slot = 1;
2330 psa_key_type_t key_type = key_type_arg;
2331 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002332 psa_status_t actual_status;
2333 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002334 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002335
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002336 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002337 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002338 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002339 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2340 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2341 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002342
2343 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2344
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002345 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002346 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002347 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2348
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002349 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002350 key_data->x,
2351 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002352
2353 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002354 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002355 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002356 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002357
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002358 TEST_ASSERT( actual_status == expected_status );
2359
2360exit:
2361 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002362 mbedtls_psa_crypto_free( );
2363}
2364/* END_CASE */
2365
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002366/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002367void asymmetric_encrypt( int key_type_arg,
2368 data_t *key_data,
2369 int alg_arg,
2370 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02002371 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02002372 int expected_output_length_arg,
2373 int expected_status_arg )
2374{
2375 int slot = 1;
2376 psa_key_type_t key_type = key_type_arg;
2377 psa_algorithm_t alg = alg_arg;
2378 size_t expected_output_length = expected_output_length_arg;
2379 size_t key_bits;
2380 unsigned char *output = NULL;
2381 size_t output_size;
2382 size_t output_length = ~0;
2383 psa_status_t actual_status;
2384 psa_status_t expected_status = expected_status_arg;
2385 psa_key_policy_t policy;
2386
2387 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2388
2389 /* Import the key */
2390 psa_key_policy_init( &policy );
2391 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2392 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2393 TEST_ASSERT( psa_import_key( slot, key_type,
2394 key_data->x,
2395 key_data->len ) == PSA_SUCCESS );
2396
2397 /* Determine the maximum output length */
2398 TEST_ASSERT( psa_get_key_information( slot,
2399 NULL,
2400 &key_bits ) == PSA_SUCCESS );
2401 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
2402 output = mbedtls_calloc( 1, output_size );
2403 TEST_ASSERT( output != NULL );
2404
2405 /* Encrypt the input */
2406 actual_status = psa_asymmetric_encrypt( slot, alg,
2407 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002408 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02002409 output, output_size,
2410 &output_length );
2411 TEST_ASSERT( actual_status == expected_status );
2412 TEST_ASSERT( output_length == expected_output_length );
2413
Gilles Peskine68428122018-06-30 18:42:41 +02002414 /* If the label is empty, the test framework puts a non-null pointer
2415 * in label->x. Test that a null pointer works as well. */
2416 if( label->len == 0 )
2417 {
2418 output_length = ~0;
2419 memset( output, 0, output_size );
2420 actual_status = psa_asymmetric_encrypt( slot, alg,
2421 input_data->x, input_data->len,
2422 NULL, label->len,
2423 output, output_size,
2424 &output_length );
2425 TEST_ASSERT( actual_status == expected_status );
2426 TEST_ASSERT( output_length == expected_output_length );
2427 }
2428
Gilles Peskine656896e2018-06-29 19:12:28 +02002429exit:
2430 psa_destroy_key( slot );
2431 mbedtls_free( output );
2432 mbedtls_psa_crypto_free( );
2433}
2434/* END_CASE */
2435
2436/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002437void asymmetric_encrypt_decrypt( int key_type_arg,
2438 data_t *key_data,
2439 int alg_arg,
2440 data_t *input_data,
2441 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002442{
2443 int slot = 1;
2444 psa_key_type_t key_type = key_type_arg;
2445 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002446 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002447 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002448 size_t output_size;
2449 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002450 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002451 size_t output2_size;
2452 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002453 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002454
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002455 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002456 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002457 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2458 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2459
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002460 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2461
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002462 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002463 psa_key_policy_set_usage( &policy,
2464 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002465 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002466 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2467
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002468 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002469 key_data->x,
2470 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002471
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002472
2473 /* Determine the maximum ciphertext length */
2474 TEST_ASSERT( psa_get_key_information( slot,
2475 NULL,
2476 &key_bits ) == PSA_SUCCESS );
2477 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
2478 output = mbedtls_calloc( 1, output_size );
2479 TEST_ASSERT( output != NULL );
2480 output2_size = input_data->len;
2481 output2 = mbedtls_calloc( 1, output2_size );
2482 TEST_ASSERT( output2 != NULL );
2483
Gilles Peskineeebd7382018-06-08 18:11:54 +02002484 /* We test encryption by checking that encrypt-then-decrypt gives back
2485 * the original plaintext because of the non-optional random
2486 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02002487 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002488 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002489 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002490 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002491 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002492 /* We don't know what ciphertext length to expect, but check that
2493 * it looks sensible. */
2494 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002495
Gilles Peskine2d277862018-06-18 15:41:12 +02002496 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002497 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02002498 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002499 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002500 &output2_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002501 TEST_ASSERT( output2_length == input_data->len );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002502 TEST_ASSERT( memcmp( input_data->x, output2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002503 input_data->len ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002504
2505exit:
2506 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002507 mbedtls_free( output );
2508 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002509 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002510}
2511/* END_CASE */
2512
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002513/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002514void asymmetric_decrypt( int key_type_arg,
2515 data_t *key_data,
2516 int alg_arg,
2517 data_t *input_data,
2518 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02002519 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002520{
2521 int slot = 1;
2522 psa_key_type_t key_type = key_type_arg;
2523 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002524 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002525 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002526 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002527 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002528
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002529 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002530 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002531 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002532 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2533 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2534 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2535
Gilles Peskine4abf7412018-06-18 16:35:34 +02002536 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002537 output = mbedtls_calloc( 1, output_size );
2538 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002539
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002540 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2541
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002542 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002543 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002544 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2545
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002546 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002547 key_data->x,
2548 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002549
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002550 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002551 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002552 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002553 output,
2554 output_size,
2555 &output_length ) == PSA_SUCCESS );
Gilles Peskine66763a02018-06-29 21:54:10 +02002556 TEST_ASSERT( expected_data->len == output_length );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002557 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002558
Gilles Peskine68428122018-06-30 18:42:41 +02002559 /* If the label is empty, the test framework puts a non-null pointer
2560 * in label->x. Test that a null pointer works as well. */
2561 if( label->len == 0 )
2562 {
2563 output_length = ~0;
2564 memset( output, 0, output_size );
2565 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
2566 input_data->x, input_data->len,
2567 NULL, label->len,
2568 output,
2569 output_size,
2570 &output_length ) == PSA_SUCCESS );
2571 TEST_ASSERT( expected_data->len == output_length );
2572 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
2573 }
2574
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002575exit:
2576 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002577 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002578 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002579}
2580/* END_CASE */
2581
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002582/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002583void asymmetric_decrypt_fail( int key_type_arg,
2584 data_t *key_data,
2585 int alg_arg,
2586 data_t *input_data,
2587 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02002588 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002589{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002590 int slot = 1;
2591 psa_key_type_t key_type = key_type_arg;
2592 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002593 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002594 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002595 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002596 psa_status_t actual_status;
2597 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002598 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002599
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002600 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002601 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002602 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2603 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2604
Gilles Peskine4abf7412018-06-18 16:35:34 +02002605 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002606 output = mbedtls_calloc( 1, output_size );
2607 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002608
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002609 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2610
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002611 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002612 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002613 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2614
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002615 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002616 key_data->x,
2617 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002618
Gilles Peskine2d277862018-06-18 15:41:12 +02002619 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002620 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002621 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002622 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002623 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002624 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002625 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002626
Gilles Peskine68428122018-06-30 18:42:41 +02002627 /* If the label is empty, the test framework puts a non-null pointer
2628 * in label->x. Test that a null pointer works as well. */
2629 if( label->len == 0 )
2630 {
2631 output_length = ~0;
2632 memset( output, 0, output_size );
2633 actual_status = psa_asymmetric_decrypt( slot, alg,
2634 input_data->x, input_data->len,
2635 NULL, label->len,
2636 output, output_size,
2637 &output_length );
2638 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002639 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02002640 }
2641
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002642exit:
2643 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02002644 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002645 mbedtls_psa_crypto_free( );
2646}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002647/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02002648
2649/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002650void derive_setup( int key_type_arg,
2651 data_t *key_data,
2652 int alg_arg,
2653 data_t *salt,
2654 data_t *label,
2655 int requested_capacity_arg,
2656 int expected_status_arg )
2657{
2658 psa_key_slot_t slot = 1;
2659 size_t key_type = key_type_arg;
2660 psa_algorithm_t alg = alg_arg;
2661 size_t requested_capacity = requested_capacity_arg;
2662 psa_status_t expected_status = expected_status_arg;
2663 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2664 psa_key_policy_t policy;
2665
2666 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2667
2668 psa_key_policy_init( &policy );
2669 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2670 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2671
2672 TEST_ASSERT( psa_import_key( slot, key_type,
2673 key_data->x,
2674 key_data->len ) == PSA_SUCCESS );
2675
2676 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
2677 salt->x, salt->len,
2678 label->x, label->len,
2679 requested_capacity ) == expected_status );
2680
2681exit:
2682 psa_generator_abort( &generator );
2683 psa_destroy_key( slot );
2684 mbedtls_psa_crypto_free( );
2685}
2686/* END_CASE */
2687
2688/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02002689void derive_output( int alg_arg,
2690 data_t *key_data,
2691 data_t *salt,
2692 data_t *label,
2693 int requested_capacity_arg,
2694 data_t *expected_output1,
2695 data_t *expected_output2 )
2696{
2697 psa_key_slot_t slot = 1;
2698 psa_algorithm_t alg = alg_arg;
2699 size_t requested_capacity = requested_capacity_arg;
2700 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2701 uint8_t *expected_outputs[2] =
2702 {expected_output1->x, expected_output2->x};
2703 size_t output_sizes[2] =
2704 {expected_output1->len, expected_output2->len};
2705 size_t output_buffer_size = 0;
2706 uint8_t *output_buffer = NULL;
2707 size_t expected_capacity;
2708 size_t current_capacity;
2709 psa_key_policy_t policy;
2710 psa_status_t status;
2711 unsigned i;
2712
2713 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
2714 {
2715 if( output_sizes[i] > output_buffer_size )
2716 output_buffer_size = output_sizes[i];
2717 if( output_sizes[i] == 0 )
2718 expected_outputs[i] = NULL;
2719 }
2720 output_buffer = mbedtls_calloc( 1, output_buffer_size );
2721 TEST_ASSERT( output_buffer != NULL );
2722 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2723
2724 psa_key_policy_init( &policy );
2725 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2726 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2727
2728 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
2729 key_data->x,
2730 key_data->len ) == PSA_SUCCESS );
2731
2732 /* Extraction phase. */
2733 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
2734 salt->x, salt->len,
2735 label->x, label->len,
2736 requested_capacity ) == PSA_SUCCESS );
2737 TEST_ASSERT( psa_get_generator_capacity( &generator,
2738 &current_capacity ) ==
2739 PSA_SUCCESS );
2740 TEST_ASSERT( current_capacity == requested_capacity );
2741 expected_capacity = requested_capacity;
2742
2743 /* Expansion phase. */
2744 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
2745 {
2746 /* Read some bytes. */
2747 status = psa_generator_read( &generator,
2748 output_buffer, output_sizes[i] );
2749 if( expected_capacity == 0 && output_sizes[i] == 0 )
2750 {
2751 /* Reading 0 bytes when 0 bytes are available can go either way. */
2752 TEST_ASSERT( status == PSA_SUCCESS ||
2753 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
2754 continue;
2755 }
2756 else if( expected_capacity == 0 ||
2757 output_sizes[i] > expected_capacity )
2758 {
2759 /* Capacity exceeded. */
2760 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
2761 expected_capacity = 0;
2762 continue;
2763 }
2764 /* Success. Check the read data. */
2765 TEST_ASSERT( status == PSA_SUCCESS );
2766 if( output_sizes[i] != 0 )
2767 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
2768 output_sizes[i] ) == 0 );
2769 /* Check the generator status. */
2770 expected_capacity -= output_sizes[i];
2771 TEST_ASSERT( psa_get_generator_capacity( &generator,
2772 &current_capacity ) ==
2773 PSA_SUCCESS );
2774 TEST_ASSERT( expected_capacity == current_capacity );
2775 }
2776 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
2777
2778exit:
2779 mbedtls_free( output_buffer );
2780 psa_generator_abort( &generator );
2781 psa_destroy_key( slot );
2782 mbedtls_psa_crypto_free( );
2783}
2784/* END_CASE */
2785
2786/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02002787void derive_full( int alg_arg,
2788 data_t *key_data,
2789 data_t *salt,
2790 data_t *label,
2791 int requested_capacity_arg )
2792{
2793 psa_key_slot_t slot = 1;
2794 psa_algorithm_t alg = alg_arg;
2795 size_t requested_capacity = requested_capacity_arg;
2796 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2797 unsigned char output_buffer[16];
2798 size_t expected_capacity = requested_capacity;
2799 size_t current_capacity;
2800 psa_key_policy_t policy;
2801
2802 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2803
2804 psa_key_policy_init( &policy );
2805 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2806 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2807
2808 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
2809 key_data->x,
2810 key_data->len ) == PSA_SUCCESS );
2811
2812 /* Extraction phase. */
2813 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
2814 salt->x, salt->len,
2815 label->x, label->len,
2816 requested_capacity ) == PSA_SUCCESS );
2817 TEST_ASSERT( psa_get_generator_capacity( &generator,
2818 &current_capacity ) ==
2819 PSA_SUCCESS );
2820 TEST_ASSERT( current_capacity == expected_capacity );
2821
2822 /* Expansion phase. */
2823 while( current_capacity > 0 )
2824 {
2825 size_t read_size = sizeof( output_buffer );
2826 if( read_size > current_capacity )
2827 read_size = current_capacity;
2828 TEST_ASSERT( psa_generator_read( &generator,
2829 output_buffer,
2830 read_size ) == PSA_SUCCESS );
2831 expected_capacity -= read_size;
2832 TEST_ASSERT( psa_get_generator_capacity( &generator,
2833 &current_capacity ) ==
2834 PSA_SUCCESS );
2835 TEST_ASSERT( current_capacity == expected_capacity );
2836 }
2837
2838 /* Check that the generator refuses to go over capacity. */
2839 TEST_ASSERT( psa_generator_read( &generator,
2840 output_buffer,
2841 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
2842
2843 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
2844
2845exit:
2846 psa_generator_abort( &generator );
2847 psa_destroy_key( slot );
2848 mbedtls_psa_crypto_free( );
2849}
2850/* END_CASE */
2851
2852/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02002853void derive_key_exercise( int alg_arg,
2854 data_t *key_data,
2855 data_t *salt,
2856 data_t *label,
2857 int derived_type_arg,
2858 int derived_bits_arg,
2859 int derived_usage_arg,
2860 int derived_alg_arg )
2861{
2862 psa_key_slot_t base_key = 1;
2863 psa_key_slot_t derived_key = 2;
2864 psa_algorithm_t alg = alg_arg;
2865 psa_key_type_t derived_type = derived_type_arg;
2866 size_t derived_bits = derived_bits_arg;
2867 psa_key_usage_t derived_usage = derived_usage_arg;
2868 psa_algorithm_t derived_alg = derived_alg_arg;
2869 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
2870 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2871 psa_key_policy_t policy;
2872 psa_key_type_t got_type;
2873 size_t got_bits;
2874
2875 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2876
2877 psa_key_policy_init( &policy );
2878 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2879 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
2880 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
2881 key_data->x,
2882 key_data->len ) == PSA_SUCCESS );
2883
2884 /* Derive a key. */
2885 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
2886 salt->x, salt->len,
2887 label->x, label->len,
2888 capacity ) == PSA_SUCCESS );
2889 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
2890 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
2891 TEST_ASSERT( psa_generator_import_key( derived_key,
2892 derived_type,
2893 derived_bits,
2894 &generator ) == PSA_SUCCESS );
2895
2896 /* Test the key information */
2897 TEST_ASSERT( psa_get_key_information( derived_key,
2898 &got_type,
2899 &got_bits ) == PSA_SUCCESS );
2900 TEST_ASSERT( got_type == derived_type );
2901 TEST_ASSERT( got_bits == derived_bits );
2902
2903 /* Exercise the derived key. */
2904 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
2905 goto exit;
2906
2907exit:
2908 psa_generator_abort( &generator );
2909 psa_destroy_key( base_key );
2910 psa_destroy_key( derived_key );
2911 mbedtls_psa_crypto_free( );
2912}
2913/* END_CASE */
2914
2915/* BEGIN_CASE */
2916void derive_key_export( int alg_arg,
2917 data_t *key_data,
2918 data_t *salt,
2919 data_t *label,
2920 int bytes1_arg,
2921 int bytes2_arg )
2922{
2923 psa_key_slot_t base_key = 1;
2924 psa_key_slot_t derived_key = 2;
2925 psa_algorithm_t alg = alg_arg;
2926 size_t bytes1 = bytes1_arg;
2927 size_t bytes2 = bytes2_arg;
2928 size_t capacity = bytes1 + bytes2;
2929 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2930 uint8_t *output_buffer = mbedtls_calloc( 1, capacity );
2931 uint8_t *export_buffer = mbedtls_calloc( 1, capacity );
2932 psa_key_policy_t policy;
2933 size_t length;
2934
2935 TEST_ASSERT( output_buffer != NULL );
2936 TEST_ASSERT( export_buffer != NULL );
2937 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2938
2939 psa_key_policy_init( &policy );
2940 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2941 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
2942 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
2943 key_data->x,
2944 key_data->len ) == PSA_SUCCESS );
2945
2946 /* Derive some material and output it. */
2947 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
2948 salt->x, salt->len,
2949 label->x, label->len,
2950 capacity ) == PSA_SUCCESS );
2951 TEST_ASSERT( psa_generator_read( &generator,
2952 output_buffer,
2953 capacity ) == PSA_SUCCESS );
2954 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
2955
2956 /* Derive the same output again, but this time store it in key objects. */
2957 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
2958 salt->x, salt->len,
2959 label->x, label->len,
2960 capacity ) == PSA_SUCCESS );
2961 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
2962 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
2963 TEST_ASSERT( psa_generator_import_key( derived_key,
2964 PSA_KEY_TYPE_RAW_DATA,
2965 PSA_BYTES_TO_BITS( bytes1 ),
2966 &generator ) == PSA_SUCCESS );
2967 TEST_ASSERT( psa_export_key( derived_key,
2968 export_buffer, bytes1,
2969 &length ) == PSA_SUCCESS );
2970 TEST_ASSERT( length == bytes1 );
2971 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
2972 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
2973 TEST_ASSERT( psa_generator_import_key( derived_key,
2974 PSA_KEY_TYPE_RAW_DATA,
2975 PSA_BYTES_TO_BITS( bytes2 ),
2976 &generator ) == PSA_SUCCESS );
2977 TEST_ASSERT( psa_export_key( derived_key,
2978 export_buffer + bytes1, bytes2,
2979 &length ) == PSA_SUCCESS );
2980 TEST_ASSERT( length == bytes2 );
2981
2982 /* Compare the outputs from the two runs. */
2983 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
2984
2985exit:
2986 mbedtls_free( output_buffer );
2987 mbedtls_free( export_buffer );
2988 psa_generator_abort( &generator );
2989 psa_destroy_key( base_key );
2990 psa_destroy_key( derived_key );
2991 mbedtls_psa_crypto_free( );
2992}
2993/* END_CASE */
2994
2995/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02002996void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02002997{
Gilles Peskinea50d7392018-06-21 10:22:13 +02002998 size_t bytes = bytes_arg;
2999 const unsigned char trail[] = "don't overwrite me";
3000 unsigned char *output = mbedtls_calloc( 1, bytes + sizeof( trail ) );
3001 unsigned char *changed = mbedtls_calloc( 1, bytes );
3002 size_t i;
3003 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003004
Gilles Peskinea50d7392018-06-21 10:22:13 +02003005 TEST_ASSERT( output != NULL );
3006 TEST_ASSERT( changed != NULL );
3007 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003008
3009 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3010
Gilles Peskinea50d7392018-06-21 10:22:13 +02003011 /* Run several times, to ensure that every output byte will be
3012 * nonzero at least once with overwhelming probability
3013 * (2^(-8*number_of_runs)). */
3014 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003015 {
Gilles Peskinea50d7392018-06-21 10:22:13 +02003016 memset( output, 0, bytes );
3017 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
3018
3019 /* Check that no more than bytes have been overwritten */
3020 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
3021
3022 for( i = 0; i < bytes; i++ )
3023 {
3024 if( output[i] != 0 )
3025 ++changed[i];
3026 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003027 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003028
3029 /* Check that every byte was changed to nonzero at least once. This
3030 * validates that psa_generate_random is overwriting every byte of
3031 * the output buffer. */
3032 for( i = 0; i < bytes; i++ )
3033 {
3034 TEST_ASSERT( changed[i] != 0 );
3035 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003036
3037exit:
3038 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003039 mbedtls_free( output );
3040 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003041}
3042/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003043
3044/* BEGIN_CASE */
3045void generate_key( int type_arg,
3046 int bits_arg,
3047 int usage_arg,
3048 int alg_arg,
3049 int expected_status_arg )
3050{
3051 int slot = 1;
3052 psa_key_type_t type = type_arg;
3053 psa_key_usage_t usage = usage_arg;
3054 size_t bits = bits_arg;
3055 psa_algorithm_t alg = alg_arg;
3056 psa_status_t expected_status = expected_status_arg;
3057 psa_key_type_t got_type;
3058 size_t got_bits;
3059 unsigned char exported[616] = {0}; /* enough for a 1024-bit RSA key */
3060 size_t exported_length;
3061 psa_status_t expected_export_status =
3062 usage & PSA_KEY_USAGE_EXPORT ? PSA_SUCCESS : PSA_ERROR_NOT_PERMITTED;
3063 psa_status_t expected_info_status =
3064 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3065 psa_key_policy_t policy;
3066
3067 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3068
3069 psa_key_policy_init( &policy );
3070 psa_key_policy_set_usage( &policy, usage, alg );
3071 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3072
3073 /* Generate a key */
3074 TEST_ASSERT( psa_generate_key( slot, type, bits,
3075 NULL, 0 ) == expected_status );
3076
3077 /* Test the key information */
3078 TEST_ASSERT( psa_get_key_information( slot,
3079 &got_type,
3080 &got_bits ) == expected_info_status );
3081 if( expected_info_status != PSA_SUCCESS )
3082 goto exit;
3083 TEST_ASSERT( got_type == type );
3084 TEST_ASSERT( got_bits == bits );
3085
3086 /* Export the key */
3087 TEST_ASSERT( psa_export_key( slot,
3088 exported, sizeof( exported ),
3089 &exported_length ) == expected_export_status );
3090 if( expected_export_status == PSA_SUCCESS )
3091 {
Gilles Peskine48c0ea12018-06-21 14:15:31 +02003092 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003093 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
3094#if defined(MBEDTLS_DES_C)
3095 if( type == PSA_KEY_TYPE_DES )
3096 {
3097 /* Check the parity bits. */
3098 unsigned i;
3099 for( i = 0; i < bits / 8; i++ )
3100 {
3101 unsigned bit_count = 0;
3102 unsigned m;
3103 for( m = 1; m <= 0x100; m <<= 1 )
3104 {
3105 if( exported[i] & m )
3106 ++bit_count;
3107 }
3108 TEST_ASSERT( bit_count % 2 != 0 );
3109 }
3110 }
3111#endif
3112#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
3113 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
3114 {
3115 /* Sanity check: does this look like the beginning of a PKCS#8
3116 * RSA key pair? Assumes bits is a multiple of 8. */
3117 size_t n_bytes = bits / 8 + 1;
3118 size_t n_encoded_bytes;
3119 unsigned char *n_end;
3120 TEST_ASSERT( exported_length >= 7 + ( n_bytes + 3 ) * 9 / 2 );
3121 TEST_ASSERT( exported[0] == 0x30 );
3122 TEST_ASSERT( exported[1] == 0x82 ); // assumes >=416-bit key
3123 TEST_ASSERT( exported[4] == 0x02 );
3124 TEST_ASSERT( exported[5] == 0x01 );
3125 TEST_ASSERT( exported[6] == 0x00 );
3126 TEST_ASSERT( exported[7] == 0x02 );
3127 n_encoded_bytes = exported[8];
3128 n_end = exported + 9 + n_encoded_bytes;
3129 if( n_encoded_bytes & 0x80 )
3130 {
3131 n_encoded_bytes = ( n_encoded_bytes & 0x7f ) << 7;
3132 n_encoded_bytes |= exported[9] & 0x7f;
3133 n_end += 1;
3134 }
3135 /* The encoding of n should start with a 0 byte since it should
3136 * have its high bit set. However Mbed TLS is not compliant and
3137 * generates an invalid, but widely tolerated, encoding of
3138 * positive INTEGERs with a bit size that is a multiple of 8
3139 * with no leading 0 byte. Accept this here. */
3140 TEST_ASSERT( n_bytes == n_encoded_bytes ||
3141 n_bytes == n_encoded_bytes + 1 );
3142 if( n_bytes == n_encoded_bytes )
3143 TEST_ASSERT( exported[n_encoded_bytes <= 127 ? 9 : 10] == 0x00 );
3144 /* Sanity check: e must be 3 */
3145 TEST_ASSERT( n_end[0] == 0x02 );
3146 TEST_ASSERT( n_end[1] == 0x03 );
3147 TEST_ASSERT( n_end[2] == 0x01 );
3148 TEST_ASSERT( n_end[3] == 0x00 );
3149 TEST_ASSERT( n_end[4] == 0x01 );
3150 TEST_ASSERT( n_end[5] == 0x02 );
3151 }
3152#endif /* MBEDTLS_RSA_C */
3153#if defined(MBEDTLS_ECP_C)
3154 if( PSA_KEY_TYPE_IS_ECC( type ) )
3155 {
3156 /* Sanity check: does this look like the beginning of a PKCS#8
3157 * elliptic curve key pair? */
3158 TEST_ASSERT( exported_length >= bits * 3 / 8 + 10 );
3159 TEST_ASSERT( exported[0] == 0x30 );
3160 }
3161#endif /* MBEDTLS_ECP_C */
3162 }
3163
Gilles Peskine818ca122018-06-20 18:16:48 +02003164 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02003165 if( ! exercise_key( slot, usage, alg ) )
3166 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003167
3168exit:
3169 psa_destroy_key( slot );
3170 mbedtls_psa_crypto_free( );
3171}
3172/* END_CASE */