blob: 317475041c45f9b7c69a56570b88e2d52ca816d7 [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 Peskined14664a2018-08-10 19:07:32 +0200396int exported_key_sanity_check( psa_key_type_t type, size_t bits,
397 uint8_t *exported, size_t exported_length )
398{
399 if( key_type_is_raw_bytes( type ) )
400 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
401
402#if defined(MBEDTLS_DES_C)
403 if( type == PSA_KEY_TYPE_DES )
404 {
405 /* Check the parity bits. */
406 unsigned i;
407 for( i = 0; i < bits / 8; i++ )
408 {
409 unsigned bit_count = 0;
410 unsigned m;
411 for( m = 1; m <= 0x100; m <<= 1 )
412 {
413 if( exported[i] & m )
414 ++bit_count;
415 }
416 TEST_ASSERT( bit_count % 2 != 0 );
417 }
418 }
419#endif
420
421#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
422 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
423 {
424 /* Sanity check: does this look like the beginning of a PKCS#8
425 * RSA key pair? Assumes bits is a multiple of 8. */
426 size_t n_bytes = bits / 8 + 1;
427 size_t n_encoded_bytes;
428 unsigned char *n_end;
429 TEST_ASSERT( exported_length >= 7 + ( n_bytes + 3 ) * 9 / 2 );
430 TEST_ASSERT( exported[0] == 0x30 );
431 TEST_ASSERT( exported[1] == 0x82 ); // assumes >=416-bit key
432 TEST_ASSERT( exported[4] == 0x02 );
433 TEST_ASSERT( exported[5] == 0x01 );
434 TEST_ASSERT( exported[6] == 0x00 );
435 TEST_ASSERT( exported[7] == 0x02 );
436 n_encoded_bytes = exported[8];
437 n_end = exported + 9 + n_encoded_bytes;
438 if( n_encoded_bytes & 0x80 )
439 {
440 n_encoded_bytes = ( n_encoded_bytes & 0x7f ) << 7;
441 n_encoded_bytes |= exported[9] & 0x7f;
442 n_end += 1;
443 }
444 /* The encoding of n should start with a 0 byte since it should
445 * have its high bit set. However Mbed TLS is not compliant and
446 * generates an invalid, but widely tolerated, encoding of
447 * positive INTEGERs with a bit size that is a multiple of 8
448 * with no leading 0 byte. Accept this here. */
449 TEST_ASSERT( n_bytes == n_encoded_bytes ||
450 n_bytes == n_encoded_bytes + 1 );
451 if( n_bytes == n_encoded_bytes )
452 TEST_ASSERT( exported[n_encoded_bytes <= 127 ? 9 : 10] == 0x00 );
453 /* Sanity check: e must be 3 */
454 TEST_ASSERT( n_end[0] == 0x02 );
455 TEST_ASSERT( n_end[1] == 0x03 );
456 TEST_ASSERT( n_end[2] == 0x01 );
457 TEST_ASSERT( n_end[3] == 0x00 );
458 TEST_ASSERT( n_end[4] == 0x01 );
459 TEST_ASSERT( n_end[5] == 0x02 );
460 }
461#endif /* MBEDTLS_RSA_C */
462
463#if defined(MBEDTLS_ECP_C)
464 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
465 {
466 /* Sanity check: does this look like the beginning of a PKCS#8
467 * elliptic curve key pair? */
468 TEST_ASSERT( exported_length >= bits * 3 / 8 + 10 );
469 TEST_ASSERT( exported[0] == 0x30 );
470 }
471#endif /* MBEDTLS_ECP_C */
472
473 return( 0 );
474
475exit:
476 return( 1 );
477}
478
479static int exercise_export_key( psa_key_slot_t slot,
480 psa_key_usage_t usage )
481{
482 psa_key_type_t type;
483 size_t bits;
484 uint8_t *exported = NULL;
485 size_t exported_size = 0;
486 size_t exported_length = 0;
487 int ok = 0;
488
489 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 )
490 {
491 TEST_ASSERT( psa_export_key( slot, NULL, 0, &exported_length ) ==
492 PSA_ERROR_NOT_PERMITTED );
493 return( 1 );
494 }
495
496 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
497 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
498 exported = mbedtls_calloc( 1, exported_size );
499 TEST_ASSERT( exported != NULL );
500
501 TEST_ASSERT( psa_export_key( slot,
502 exported, exported_size,
503 &exported_length ) == PSA_SUCCESS );
504 ok = exported_key_sanity_check( type, bits, exported, exported_length );
505
506exit:
507 mbedtls_free( exported );
508 return( ok );
509}
510
511static int exercise_export_public_key( psa_key_slot_t slot )
512{
513 psa_key_type_t type;
514 psa_key_type_t public_type;
515 size_t bits;
516 uint8_t *exported = NULL;
517 size_t exported_size = 0;
518 size_t exported_length = 0;
519 int ok = 0;
520
521 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
522 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
523 {
524 TEST_ASSERT( psa_export_public_key( slot,
525 NULL, 0, &exported_length ) ==
526 PSA_ERROR_INVALID_ARGUMENT );
527 return( 1 );
528 }
529
530 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
531 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
532 exported = mbedtls_calloc( 1, exported_size );
533 TEST_ASSERT( exported != NULL );
534
535 TEST_ASSERT( psa_export_public_key( slot,
536 exported, exported_size,
537 &exported_length ) == PSA_SUCCESS );
538 ok = exported_key_sanity_check( public_type, bits,
539 exported, exported_length );
540
541exit:
542 mbedtls_free( exported );
543 return( ok );
544}
545
Gilles Peskine02b75072018-07-01 22:31:34 +0200546static int exercise_key( psa_key_slot_t slot,
547 psa_key_usage_t usage,
548 psa_algorithm_t alg )
549{
550 int ok;
551 if( alg == 0 )
552 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
553 else if( PSA_ALG_IS_MAC( alg ) )
554 ok = exercise_mac_key( slot, usage, alg );
555 else if( PSA_ALG_IS_CIPHER( alg ) )
556 ok = exercise_cipher_key( slot, usage, alg );
557 else if( PSA_ALG_IS_AEAD( alg ) )
558 ok = exercise_aead_key( slot, usage, alg );
559 else if( PSA_ALG_IS_SIGN( alg ) )
560 ok = exercise_signature_key( slot, usage, alg );
561 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
562 ok = exercise_asymmetric_encryption_key( slot, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200563 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
564 ok = exercise_key_derivation_key( slot, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200565 else
566 {
567 char message[40];
568 mbedtls_snprintf( message, sizeof( message ),
569 "No code to exercise alg=0x%08lx",
570 (unsigned long) alg );
571 test_fail( message, __LINE__, __FILE__ );
572 ok = 0;
573 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200574
575 ok = ok && exercise_export_key( slot, usage );
576 ok = ok && exercise_export_public_key( slot );
577
Gilles Peskine02b75072018-07-01 22:31:34 +0200578 return( ok );
579}
580
Gilles Peskinee59236f2018-01-27 23:32:46 +0100581/* END_HEADER */
582
583/* BEGIN_DEPENDENCIES
584 * depends_on:MBEDTLS_PSA_CRYPTO_C
585 * END_DEPENDENCIES
586 */
587
588/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200589void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100590{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100591 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100592 int i;
593 for( i = 0; i <= 1; i++ )
594 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100595 status = psa_crypto_init( );
596 TEST_ASSERT( status == PSA_SUCCESS );
597 status = psa_crypto_init( );
598 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100599 mbedtls_psa_crypto_free( );
600 }
601}
602/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100603
604/* BEGIN_CASE */
Gilles Peskine996deb12018-08-01 15:45:45 +0200605void fill_slots( int max_arg )
606{
607 /* Fill all the slots until we run out of memory or out of slots,
608 * or until some limit specified in the test data for the sake of
609 * implementations with an essentially unlimited number of slots.
610 * This test assumes that available slots are numbered from 1. */
611
612 psa_key_slot_t slot;
613 psa_key_slot_t max = 0;
614 psa_key_policy_t policy;
615 uint8_t exported[sizeof( max )];
616 size_t exported_size;
617 psa_status_t status;
618
619 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
620
621 psa_key_policy_init( &policy );
622 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
623
624 for( max = 1; max <= (size_t) max_arg; max++ )
625 {
626 status = psa_set_key_policy( max, &policy );
627 /* Stop filling slots if we run out of memory or out of
628 * available slots. */
629 TEST_ASSERT( status == PSA_SUCCESS ||
630 status == PSA_ERROR_INSUFFICIENT_MEMORY ||
631 status == PSA_ERROR_INVALID_ARGUMENT );
632 if( status != PSA_SUCCESS )
633 break;
634 status = psa_import_key( max, PSA_KEY_TYPE_RAW_DATA,
635 (uint8_t*) &max, sizeof( max ) );
636 /* Since psa_set_key_policy succeeded, we know that the slot
637 * number is valid. But we may legitimately run out of memory. */
638 TEST_ASSERT( status == PSA_SUCCESS ||
639 status == PSA_ERROR_INSUFFICIENT_MEMORY );
640 if( status != PSA_SUCCESS )
641 break;
642 }
643 /* `max` is now the first slot number that wasn't filled. */
644 max -= 1;
645
646 for( slot = 1; slot <= max; slot++ )
647 {
648 TEST_ASSERT( psa_export_key( slot,
649 exported, sizeof( exported ),
650 &exported_size ) == PSA_SUCCESS );
651 TEST_ASSERT( exported_size == sizeof( slot ) );
652 TEST_ASSERT( memcmp( exported, &slot, sizeof( slot ) ) == 0 );
Gilles Peskine996deb12018-08-01 15:45:45 +0200653 }
654
655exit:
Gilles Peskine9a056342018-08-01 15:46:54 +0200656 /* Do not destroy the keys. mbedtls_psa_crypto_free() should do it. */
Gilles Peskine996deb12018-08-01 15:45:45 +0200657 mbedtls_psa_crypto_free( );
658}
659/* END_CASE */
660
661/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200662void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100663{
664 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200665 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100666 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100667
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100668 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300669 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100670 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
671
Gilles Peskine4abf7412018-06-18 16:35:34 +0200672 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200673 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100674 if( status == PSA_SUCCESS )
675 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
676
677exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100678 mbedtls_psa_crypto_free( );
679}
680/* END_CASE */
681
682/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200683void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
684{
685 int slot = 1;
686 size_t bits = bits_arg;
687 psa_status_t expected_status = expected_status_arg;
688 psa_status_t status;
689 psa_key_type_t type =
690 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
691 size_t buffer_size = /* Slight overapproximations */
692 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
693 unsigned char *buffer = mbedtls_calloc( 1, buffer_size );
694 unsigned char *p;
695 int ret;
696 size_t length;
697
698 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
699 TEST_ASSERT( buffer != NULL );
700
701 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
702 bits, keypair ) ) >= 0 );
703 length = ret;
704
705 /* Try importing the key */
706 status = psa_import_key( slot, type, p, length );
707 TEST_ASSERT( status == expected_status );
708 if( status == PSA_SUCCESS )
709 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
710
711exit:
712 mbedtls_free( buffer );
713 mbedtls_psa_crypto_free( );
714}
715/* END_CASE */
716
717/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300718void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300719 int type_arg,
720 int alg_arg,
721 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100722 int expected_bits,
723 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200724 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100725 int canonical_input )
726{
727 int slot = 1;
728 int slot2 = slot + 1;
729 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200730 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200731 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100732 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100733 unsigned char *exported = NULL;
734 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100735 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100736 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100737 size_t reexported_length;
738 psa_key_type_t got_type;
739 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200740 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100741
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100742 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300743 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +0300744 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100745 exported = mbedtls_calloc( 1, export_size );
Darryl Green9c862252018-07-24 12:52:44 +0100746 TEST_ASSERT( export_size == 0 || exported != NULL );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100747 if( ! canonical_input )
748 {
749 reexported = mbedtls_calloc( 1, export_size );
Darryl Green9c862252018-07-24 12:52:44 +0100750 TEST_ASSERT( export_size == 0 || reexported != NULL );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100751 }
752 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
753
mohammad1603a97cb8c2018-03-28 03:46:26 -0700754 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200755 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700756 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
757
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100758 /* Import the key */
759 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200760 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100761
762 /* Test the key information */
763 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200764 &got_type,
765 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100766 TEST_ASSERT( got_type == type );
767 TEST_ASSERT( got_bits == (size_t) expected_bits );
768
769 /* Export the key */
770 status = psa_export_key( slot,
771 exported, export_size,
772 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200773 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100774
775 /* The exported length must be set by psa_export_key() to a value between 0
776 * and export_size. On errors, the exported length must be 0. */
777 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
778 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
779 TEST_ASSERT( exported_length <= export_size );
780
Gilles Peskine3f669c32018-06-21 09:21:51 +0200781 TEST_ASSERT( mem_is_zero( exported + exported_length,
782 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100783 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200784 {
785 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100786 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200787 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100788
789 if( canonical_input )
790 {
Gilles Peskine4abf7412018-06-18 16:35:34 +0200791 TEST_ASSERT( exported_length == data->len );
792 TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100793 }
794 else
795 {
mohammad1603a97cb8c2018-03-28 03:46:26 -0700796 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
797
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100798 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200799 exported,
800 export_size ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100801 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200802 reexported,
803 export_size,
804 &reexported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100805 TEST_ASSERT( reexported_length == exported_length );
806 TEST_ASSERT( memcmp( reexported, exported,
807 exported_length ) == 0 );
808 }
809
810destroy:
811 /* Destroy the key */
812 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
813 TEST_ASSERT( psa_get_key_information(
814 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
815
816exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300817 mbedtls_free( exported );
818 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100819 mbedtls_psa_crypto_free( );
820}
821/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100822
Moran Pekerf709f4a2018-06-06 17:26:04 +0300823/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300824void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200825 int type_arg,
826 int alg_arg,
827 int expected_bits,
828 int public_key_expected_length,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200829 int expected_export_status_arg )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300830{
831 int slot = 1;
832 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200833 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200834 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300835 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300836 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300837 size_t export_size;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100838 size_t exported_length = INVALID_EXPORT_LENGTH;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300839 psa_key_type_t got_type;
840 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200841 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300842
Moran Pekerf709f4a2018-06-06 17:26:04 +0300843 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300844 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +0300845 export_size = (ptrdiff_t) data->len;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300846 exported = mbedtls_calloc( 1, export_size );
847 TEST_ASSERT( exported != NULL );
848
849 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
850
851 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200852 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300853 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
854
855 /* Import the key */
856 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200857 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300858
859 /* Test the key information */
860 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200861 &got_type,
862 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300863 TEST_ASSERT( got_type == type );
864 TEST_ASSERT( got_bits == (size_t) expected_bits );
865
866 /* Export the key */
867 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200868 exported, export_size,
869 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200870 TEST_ASSERT( status == expected_export_status );
Jaeden Amero2a671e92018-06-27 17:47:40 +0100871 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
872 TEST_ASSERT( mem_is_zero( exported + exported_length,
873 export_size - exported_length ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300874 if( status != PSA_SUCCESS )
875 goto destroy;
876
Moran Pekerf709f4a2018-06-06 17:26:04 +0300877destroy:
878 /* Destroy the key */
879 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
880 TEST_ASSERT( psa_get_key_information(
881 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
882
883exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300884 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300885 mbedtls_psa_crypto_free( );
886}
887/* END_CASE */
888
Gilles Peskine20035e32018-02-03 22:44:14 +0100889/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200890void import_and_exercise_key( data_t *data,
891 int type_arg,
892 int bits_arg,
893 int alg_arg )
894{
895 int slot = 1;
896 psa_key_type_t type = type_arg;
897 size_t bits = bits_arg;
898 psa_algorithm_t alg = alg_arg;
899 psa_key_usage_t usage =
900 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
901 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
902 PSA_KEY_USAGE_VERIFY :
903 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
904 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
905 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
906 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
907 PSA_KEY_USAGE_ENCRYPT :
908 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
Gilles Peskineea0fb492018-07-12 17:17:20 +0200909 PSA_ALG_IS_KEY_DERIVATION( alg ) ? PSA_KEY_USAGE_DERIVE :
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200910 0 );
911 psa_key_policy_t policy;
912 psa_key_type_t got_type;
913 size_t got_bits;
914 psa_status_t status;
915
916 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
917
918 psa_key_policy_init( &policy );
919 psa_key_policy_set_usage( &policy, usage, alg );
920 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
921
922 /* Import the key */
923 status = psa_import_key( slot, type, data->x, data->len );
924 TEST_ASSERT( status == PSA_SUCCESS );
925
926 /* Test the key information */
927 TEST_ASSERT( psa_get_key_information( slot,
928 &got_type,
929 &got_bits ) == PSA_SUCCESS );
930 TEST_ASSERT( got_type == type );
931 TEST_ASSERT( got_bits == bits );
932
933 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +0200934 if( ! exercise_key( slot, usage, alg ) )
935 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200936
937exit:
938 psa_destroy_key( slot );
939 mbedtls_psa_crypto_free( );
940}
941/* END_CASE */
942
943/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +0200944void key_policy( int usage_arg, int alg_arg )
945{
946 int key_slot = 1;
947 psa_algorithm_t alg = alg_arg;
948 psa_key_usage_t usage = usage_arg;
949 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
950 unsigned char key[32] = {0};
951 psa_key_policy_t policy_set;
952 psa_key_policy_t policy_get;
953
954 memset( key, 0x2a, sizeof( key ) );
955
956 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
957
958 psa_key_policy_init( &policy_set );
959 psa_key_policy_init( &policy_get );
960
961 psa_key_policy_set_usage( &policy_set, usage, alg );
962
963 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
964 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
965 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
966
967 TEST_ASSERT( psa_import_key( key_slot, key_type,
968 key, sizeof( key ) ) == PSA_SUCCESS );
969
970 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
971
972 TEST_ASSERT( policy_get.usage == policy_set.usage );
973 TEST_ASSERT( policy_get.alg == policy_set.alg );
974
975exit:
976 psa_destroy_key( key_slot );
977 mbedtls_psa_crypto_free( );
978}
979/* END_CASE */
980
981/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200982void mac_key_policy( int policy_usage,
983 int policy_alg,
984 int key_type,
985 data_t *key_data,
986 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200987{
988 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +0200989 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200990 psa_mac_operation_t operation;
991 psa_status_t status;
992 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200993
994 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
995
996 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200997 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200998 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
999
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001000 TEST_ASSERT( psa_import_key( key_slot, key_type,
1001 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001002
Gilles Peskine89167cb2018-07-08 20:12:23 +02001003 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001004 if( policy_alg == exercise_alg &&
1005 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1006 TEST_ASSERT( status == PSA_SUCCESS );
1007 else
1008 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1009 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001010
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001011 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001012 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001013 if( policy_alg == exercise_alg &&
1014 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +02001015 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001016 else
1017 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1018
1019exit:
1020 psa_mac_abort( &operation );
1021 psa_destroy_key( key_slot );
1022 mbedtls_psa_crypto_free( );
1023}
1024/* END_CASE */
1025
1026/* BEGIN_CASE */
1027void cipher_key_policy( int policy_usage,
1028 int policy_alg,
1029 int key_type,
1030 data_t *key_data,
1031 int exercise_alg )
1032{
1033 int key_slot = 1;
1034 psa_key_policy_t policy;
1035 psa_cipher_operation_t operation;
1036 psa_status_t status;
1037
1038 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1039
1040 psa_key_policy_init( &policy );
1041 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1042 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1043
1044 TEST_ASSERT( psa_import_key( key_slot, key_type,
1045 key_data->x, key_data->len ) == PSA_SUCCESS );
1046
Gilles Peskinefe119512018-07-08 21:39:34 +02001047 status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001048 if( policy_alg == exercise_alg &&
1049 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1050 TEST_ASSERT( status == PSA_SUCCESS );
1051 else
1052 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1053 psa_cipher_abort( &operation );
1054
Gilles Peskinefe119512018-07-08 21:39:34 +02001055 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001056 if( policy_alg == exercise_alg &&
1057 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1058 TEST_ASSERT( status == PSA_SUCCESS );
1059 else
1060 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1061
1062exit:
1063 psa_cipher_abort( &operation );
1064 psa_destroy_key( key_slot );
1065 mbedtls_psa_crypto_free( );
1066}
1067/* END_CASE */
1068
1069/* BEGIN_CASE */
1070void aead_key_policy( int policy_usage,
1071 int policy_alg,
1072 int key_type,
1073 data_t *key_data,
1074 int nonce_length_arg,
1075 int tag_length_arg,
1076 int exercise_alg )
1077{
1078 int key_slot = 1;
1079 psa_key_policy_t policy;
1080 psa_status_t status;
1081 unsigned char nonce[16] = {0};
1082 size_t nonce_length = nonce_length_arg;
1083 unsigned char tag[16];
1084 size_t tag_length = tag_length_arg;
1085 size_t output_length;
1086
1087 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1088 TEST_ASSERT( tag_length <= sizeof( tag ) );
1089
1090 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1091
1092 psa_key_policy_init( &policy );
1093 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1094 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1095
1096 TEST_ASSERT( psa_import_key( key_slot, key_type,
1097 key_data->x, key_data->len ) == PSA_SUCCESS );
1098
1099 status = psa_aead_encrypt( key_slot, exercise_alg,
1100 nonce, nonce_length,
1101 NULL, 0,
1102 NULL, 0,
1103 tag, tag_length,
1104 &output_length );
1105 if( policy_alg == exercise_alg &&
1106 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1107 TEST_ASSERT( status == PSA_SUCCESS );
1108 else
1109 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1110
1111 memset( tag, 0, sizeof( tag ) );
1112 status = psa_aead_decrypt( key_slot, exercise_alg,
1113 nonce, nonce_length,
1114 NULL, 0,
1115 tag, tag_length,
1116 NULL, 0,
1117 &output_length );
1118 if( policy_alg == exercise_alg &&
1119 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1120 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1121 else
1122 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1123
1124exit:
1125 psa_destroy_key( key_slot );
1126 mbedtls_psa_crypto_free( );
1127}
1128/* END_CASE */
1129
1130/* BEGIN_CASE */
1131void asymmetric_encryption_key_policy( int policy_usage,
1132 int policy_alg,
1133 int key_type,
1134 data_t *key_data,
1135 int exercise_alg )
1136{
1137 int key_slot = 1;
1138 psa_key_policy_t policy;
1139 psa_status_t status;
1140 size_t key_bits;
1141 size_t buffer_length;
1142 unsigned char *buffer = NULL;
1143 size_t output_length;
1144
1145 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1146
1147 psa_key_policy_init( &policy );
1148 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1149 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1150
1151 TEST_ASSERT( psa_import_key( key_slot, key_type,
1152 key_data->x, key_data->len ) == PSA_SUCCESS );
1153
1154 TEST_ASSERT( psa_get_key_information( key_slot,
1155 NULL,
1156 &key_bits ) == PSA_SUCCESS );
1157 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1158 exercise_alg );
1159 buffer = mbedtls_calloc( 1, buffer_length );
1160 TEST_ASSERT( buffer != NULL );
1161
1162 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
1163 NULL, 0,
1164 NULL, 0,
1165 buffer, buffer_length,
1166 &output_length );
1167 if( policy_alg == exercise_alg &&
1168 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1169 TEST_ASSERT( status == PSA_SUCCESS );
1170 else
1171 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1172
1173 memset( buffer, 0, buffer_length );
1174 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
1175 buffer, buffer_length,
1176 NULL, 0,
1177 buffer, buffer_length,
1178 &output_length );
1179 if( policy_alg == exercise_alg &&
1180 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1181 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
1182 else
1183 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1184
1185exit:
1186 psa_destroy_key( key_slot );
1187 mbedtls_psa_crypto_free( );
1188 mbedtls_free( buffer );
1189}
1190/* END_CASE */
1191
1192/* BEGIN_CASE */
1193void asymmetric_signature_key_policy( int policy_usage,
1194 int policy_alg,
1195 int key_type,
1196 data_t *key_data,
1197 int exercise_alg )
1198{
1199 int key_slot = 1;
1200 psa_key_policy_t policy;
1201 psa_status_t status;
1202 unsigned char payload[16] = {1};
1203 size_t payload_length = sizeof( payload );
1204 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1205 size_t signature_length;
1206
1207 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1208
1209 psa_key_policy_init( &policy );
1210 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1211 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1212
1213 TEST_ASSERT( psa_import_key( key_slot, key_type,
1214 key_data->x, key_data->len ) == PSA_SUCCESS );
1215
1216 status = psa_asymmetric_sign( key_slot, exercise_alg,
1217 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001218 signature, sizeof( signature ),
1219 &signature_length );
1220 if( policy_alg == exercise_alg &&
1221 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1222 TEST_ASSERT( status == PSA_SUCCESS );
1223 else
1224 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1225
1226 memset( signature, 0, sizeof( signature ) );
1227 status = psa_asymmetric_verify( key_slot, exercise_alg,
1228 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001229 signature, sizeof( signature ) );
1230 if( policy_alg == exercise_alg &&
1231 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1232 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1233 else
1234 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001235
1236exit:
1237 psa_destroy_key( key_slot );
1238 mbedtls_psa_crypto_free( );
1239}
1240/* END_CASE */
1241
1242/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001243void derive_key_policy( int policy_usage,
1244 int policy_alg,
1245 int key_type,
1246 data_t *key_data,
1247 int exercise_alg )
1248{
1249 int key_slot = 1;
1250 psa_key_policy_t policy;
1251 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1252 psa_status_t status;
1253
1254 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1255
1256 psa_key_policy_init( &policy );
1257 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1258 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1259
1260 TEST_ASSERT( psa_import_key( key_slot, key_type,
1261 key_data->x, key_data->len ) == PSA_SUCCESS );
1262
1263 status = psa_key_derivation( &generator, key_slot,
1264 exercise_alg,
1265 NULL, 0,
1266 NULL, 0,
1267 1 );
1268 if( policy_alg == exercise_alg &&
1269 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1270 TEST_ASSERT( status == PSA_SUCCESS );
1271 else
1272 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1273
1274exit:
1275 psa_generator_abort( &generator );
1276 psa_destroy_key( key_slot );
1277 mbedtls_psa_crypto_free( );
1278}
1279/* END_CASE */
1280
1281/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001282void key_lifetime( int lifetime_arg )
1283{
1284 int key_slot = 1;
Gilles Peskinec32f0302018-08-10 16:02:11 +02001285 psa_key_type_t key_type = PSA_KEY_TYPE_RAW_DATA;
Gilles Peskined5b33222018-06-18 22:20:03 +02001286 unsigned char key[32] = {0};
1287 psa_key_lifetime_t lifetime_set = lifetime_arg;
1288 psa_key_lifetime_t lifetime_get;
1289
1290 memset( key, 0x2a, sizeof( key ) );
1291
1292 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1293
1294 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1295 lifetime_set ) == PSA_SUCCESS );
1296
1297 TEST_ASSERT( psa_import_key( key_slot, key_type,
1298 key, sizeof( key ) ) == PSA_SUCCESS );
1299
1300 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1301 &lifetime_get ) == PSA_SUCCESS );
1302
1303 TEST_ASSERT( lifetime_get == lifetime_set );
1304
1305exit:
1306 psa_destroy_key( key_slot );
1307 mbedtls_psa_crypto_free( );
1308}
1309/* END_CASE */
1310
1311/* BEGIN_CASE */
1312void key_lifetime_set_fail( int key_slot_arg,
1313 int lifetime_arg,
1314 int expected_status_arg )
1315{
1316 psa_key_slot_t key_slot = key_slot_arg;
1317 psa_key_lifetime_t lifetime_set = lifetime_arg;
1318 psa_status_t actual_status;
1319 psa_status_t expected_status = expected_status_arg;
1320
1321 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1322
1323 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1324
1325 if( actual_status == PSA_SUCCESS )
1326 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1327
1328 TEST_ASSERT( expected_status == actual_status );
1329
1330exit:
1331 psa_destroy_key( key_slot );
1332 mbedtls_psa_crypto_free( );
1333}
1334/* END_CASE */
1335
1336/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001337void hash_setup( int alg_arg,
1338 int expected_status_arg )
1339{
1340 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001341 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001342 psa_hash_operation_t operation;
1343 psa_status_t status;
1344
1345 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1346
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001347 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001348 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001349 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001350
1351exit:
1352 mbedtls_psa_crypto_free( );
1353}
1354/* END_CASE */
1355
1356/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001357void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001358{
1359 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001360 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001361 size_t actual_hash_length;
1362 psa_hash_operation_t operation;
1363
Gilles Peskine69c12672018-06-28 00:07:19 +02001364 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1365 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1366
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001367 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001368 TEST_ASSERT( expected_hash != NULL );
1369 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1370 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001371
1372 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1373
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001374 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001375 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001376 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001377 TEST_ASSERT( psa_hash_finish( &operation,
1378 actual_hash, sizeof( actual_hash ),
1379 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001380 TEST_ASSERT( actual_hash_length == expected_hash->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001381 TEST_ASSERT( memcmp( expected_hash->x, actual_hash,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001382 expected_hash->len ) == 0 );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001383
1384exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001385 mbedtls_psa_crypto_free( );
1386}
1387/* END_CASE */
1388
1389/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001390void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001391{
1392 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001393 psa_hash_operation_t operation;
1394
Gilles Peskine69c12672018-06-28 00:07:19 +02001395 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1396 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1397
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001398 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001399 TEST_ASSERT( expected_hash != NULL );
1400 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1401 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001402
1403 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1404
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001405 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001406 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001407 input->x,
1408 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001409 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001410 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001411 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001412
1413exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001414 mbedtls_psa_crypto_free( );
1415}
1416/* END_CASE */
1417
1418/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001419void mac_setup( int key_type_arg,
1420 data_t *key,
1421 int alg_arg,
1422 int expected_status_arg )
1423{
1424 int key_slot = 1;
1425 psa_key_type_t key_type = key_type_arg;
1426 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001427 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001428 psa_mac_operation_t operation;
1429 psa_key_policy_t policy;
1430 psa_status_t status;
1431
1432 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1433
1434 psa_key_policy_init( &policy );
1435 psa_key_policy_set_usage( &policy,
1436 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1437 alg );
1438 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1439
1440 TEST_ASSERT( psa_import_key( key_slot, key_type,
1441 key->x, key->len ) == PSA_SUCCESS );
1442
Gilles Peskine89167cb2018-07-08 20:12:23 +02001443 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001444 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001445 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001446
1447exit:
1448 psa_destroy_key( key_slot );
1449 mbedtls_psa_crypto_free( );
1450}
1451/* END_CASE */
1452
1453/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001454void mac_verify( int key_type_arg,
1455 data_t *key,
1456 int alg_arg,
1457 data_t *input,
1458 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001459{
1460 int key_slot = 1;
1461 psa_key_type_t key_type = key_type_arg;
1462 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001463 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001464 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001465
Gilles Peskine69c12672018-06-28 00:07:19 +02001466 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1467
Gilles Peskine8c9def32018-02-08 10:02:12 +01001468 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001469 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001470 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001471 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001472 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1473 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001474
1475 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1476
mohammad16036df908f2018-04-02 08:34:15 -07001477 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001478 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07001479 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1480
Gilles Peskine8c9def32018-02-08 10:02:12 +01001481 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001482 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001483
Gilles Peskine89167cb2018-07-08 20:12:23 +02001484 TEST_ASSERT( psa_mac_verify_setup( &operation,
1485 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001486 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1487 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001488 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02001489 TEST_ASSERT( psa_mac_verify_finish( &operation,
1490 expected_mac->x,
1491 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001492
1493exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001494 psa_destroy_key( key_slot );
1495 mbedtls_psa_crypto_free( );
1496}
1497/* END_CASE */
1498
1499/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001500void cipher_setup( int key_type_arg,
1501 data_t *key,
1502 int alg_arg,
1503 int expected_status_arg )
1504{
1505 int key_slot = 1;
1506 psa_key_type_t key_type = key_type_arg;
1507 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001508 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001509 psa_cipher_operation_t operation;
1510 psa_key_policy_t policy;
1511 psa_status_t status;
1512
1513 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1514
1515 psa_key_policy_init( &policy );
1516 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1517 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1518
1519 TEST_ASSERT( psa_import_key( key_slot, key_type,
1520 key->x, key->len ) == PSA_SUCCESS );
1521
Gilles Peskinefe119512018-07-08 21:39:34 +02001522 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001523 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001524 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001525
1526exit:
1527 psa_destroy_key( key_slot );
1528 mbedtls_psa_crypto_free( );
1529}
1530/* END_CASE */
1531
1532/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001533void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001534 data_t *key,
1535 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001536 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001537{
1538 int key_slot = 1;
1539 psa_status_t status;
1540 psa_key_type_t key_type = key_type_arg;
1541 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001542 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001543 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001544 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001545 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001546 size_t output_buffer_size = 0;
1547 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001548 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001549 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001550 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001551
Gilles Peskine50e586b2018-06-08 14:28:46 +02001552 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001553 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001554 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001555 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1556 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1557 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001558
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001559 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1560 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001561
1562 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1563
Moran Pekered346952018-07-05 15:22:45 +03001564 psa_key_policy_init( &policy );
1565 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1566 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1567
Gilles Peskine50e586b2018-06-08 14:28:46 +02001568 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001569 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001570
Gilles Peskinefe119512018-07-08 21:39:34 +02001571 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1572 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001573
Gilles Peskinefe119512018-07-08 21:39:34 +02001574 TEST_ASSERT( psa_cipher_set_iv( &operation,
1575 iv, iv_size ) == PSA_SUCCESS );
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( psa_cipher_update( &operation,
1582 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001583 output, output_buffer_size,
1584 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001585 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001586 status = psa_cipher_finish( &operation,
1587 output + function_output_length,
1588 output_buffer_size,
1589 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001590 total_output_length += function_output_length;
1591
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001592 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001593 if( expected_status == PSA_SUCCESS )
1594 {
1595 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001596 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001597 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001598 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001599 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001600
Gilles Peskine50e586b2018-06-08 14:28:46 +02001601exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001602 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001603 psa_destroy_key( key_slot );
1604 mbedtls_psa_crypto_free( );
1605}
1606/* END_CASE */
1607
1608/* BEGIN_CASE */
1609void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001610 data_t *key,
1611 data_t *input,
1612 int first_part_size,
1613 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001614{
1615 int key_slot = 1;
1616 psa_key_type_t key_type = key_type_arg;
1617 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001618 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001619 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001620 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001621 size_t output_buffer_size = 0;
1622 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001623 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001624 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001625 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001626
Gilles Peskine50e586b2018-06-08 14:28:46 +02001627 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001628 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001629 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001630 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1631 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1632 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001633
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001634 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1635 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001636
1637 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1638
Moran Pekered346952018-07-05 15:22:45 +03001639 psa_key_policy_init( &policy );
1640 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1641 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1642
Gilles Peskine50e586b2018-06-08 14:28:46 +02001643 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001644 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001645
Gilles Peskinefe119512018-07-08 21:39:34 +02001646 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1647 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001648
Gilles Peskinefe119512018-07-08 21:39:34 +02001649 TEST_ASSERT( psa_cipher_set_iv( &operation,
1650 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001651 output_buffer_size = (size_t) input->len +
1652 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001653 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001654 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001655
Gilles Peskine4abf7412018-06-18 16:35:34 +02001656 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001657 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001658 output, output_buffer_size,
1659 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001660 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001661 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001662 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001663 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001664 output, output_buffer_size,
1665 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001666 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001667 TEST_ASSERT( psa_cipher_finish( &operation,
1668 output + function_output_length,
1669 output_buffer_size,
1670 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001671 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001672 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1673
Gilles Peskine4abf7412018-06-18 16:35:34 +02001674 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001675 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001676 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001677
1678exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001679 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001680 psa_destroy_key( key_slot );
1681 mbedtls_psa_crypto_free( );
1682}
1683/* END_CASE */
1684
1685/* BEGIN_CASE */
1686void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001687 data_t *key,
1688 data_t *input,
1689 int first_part_size,
1690 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001691{
1692 int key_slot = 1;
1693
1694 psa_key_type_t key_type = key_type_arg;
1695 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001696 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001697 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001698 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001699 size_t output_buffer_size = 0;
1700 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001701 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001702 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001703 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001704
Gilles Peskine50e586b2018-06-08 14:28:46 +02001705 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001706 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001707 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001708 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1709 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1710 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001711
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001712 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1713 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001714
1715 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_DECRYPT, alg );
1719 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1720
Gilles Peskine50e586b2018-06-08 14:28:46 +02001721 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001722 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001723
Gilles Peskinefe119512018-07-08 21:39:34 +02001724 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1725 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001726
Gilles Peskinefe119512018-07-08 21:39:34 +02001727 TEST_ASSERT( psa_cipher_set_iv( &operation,
1728 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001729
mohammad16033d91abe2018-07-03 13:15:54 +03001730 output_buffer_size = (size_t) input->len +
1731 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001732 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001733 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001734
Gilles Peskine4abf7412018-06-18 16:35:34 +02001735 TEST_ASSERT( (unsigned int) first_part_size < input->len );
1736 TEST_ASSERT( psa_cipher_update( &operation,
1737 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001738 output, output_buffer_size,
1739 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001740 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001741 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001742 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001743 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001744 output, output_buffer_size,
1745 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001746 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001747 TEST_ASSERT( psa_cipher_finish( &operation,
1748 output + function_output_length,
1749 output_buffer_size,
1750 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001751 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001752 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1753
Gilles Peskine4abf7412018-06-18 16:35:34 +02001754 TEST_ASSERT( total_output_length == expected_output->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001755 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001756 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001757
1758exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001759 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001760 psa_destroy_key( key_slot );
1761 mbedtls_psa_crypto_free( );
1762}
1763/* END_CASE */
1764
Gilles Peskine50e586b2018-06-08 14:28:46 +02001765/* BEGIN_CASE */
1766void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001767 data_t *key,
1768 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001769 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001770{
1771 int key_slot = 1;
1772 psa_status_t status;
1773 psa_key_type_t key_type = key_type_arg;
1774 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001775 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001776 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001777 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001778 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001779 size_t output_buffer_size = 0;
1780 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001781 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001782 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001783 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001784
Gilles Peskine50e586b2018-06-08 14:28:46 +02001785 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001786 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001787 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001788 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1789 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1790 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001791
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001792 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1793 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001794
1795 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1796
Moran Pekered346952018-07-05 15:22:45 +03001797 psa_key_policy_init( &policy );
1798 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1799 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1800
Gilles Peskine50e586b2018-06-08 14:28:46 +02001801 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001802 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001803
Gilles Peskinefe119512018-07-08 21:39:34 +02001804 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1805 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001806
Gilles Peskinefe119512018-07-08 21:39:34 +02001807 TEST_ASSERT( psa_cipher_set_iv( &operation,
1808 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001809
mohammad16033d91abe2018-07-03 13:15:54 +03001810 output_buffer_size = (size_t) input->len +
1811 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001812 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001813 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001814
Gilles Peskine4abf7412018-06-18 16:35:34 +02001815 TEST_ASSERT( psa_cipher_update( &operation,
1816 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001817 output, output_buffer_size,
1818 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001819 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001820 status = psa_cipher_finish( &operation,
1821 output + function_output_length,
1822 output_buffer_size,
1823 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001824 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001825 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001826
1827 if( expected_status == PSA_SUCCESS )
1828 {
1829 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001830 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001831 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001832 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001833 }
1834
Gilles Peskine50e586b2018-06-08 14:28:46 +02001835exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001836 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001837 psa_destroy_key( key_slot );
1838 mbedtls_psa_crypto_free( );
1839}
1840/* END_CASE */
1841
Gilles Peskine50e586b2018-06-08 14:28:46 +02001842/* BEGIN_CASE */
1843void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001844 data_t *key,
1845 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02001846{
1847 int key_slot = 1;
1848 psa_key_type_t key_type = key_type_arg;
1849 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07001850 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02001851 size_t iv_size = 16;
1852 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001853 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001854 size_t output1_size = 0;
1855 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001856 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001857 size_t output2_size = 0;
1858 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001859 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001860 psa_cipher_operation_t operation1;
1861 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03001862 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001863
mohammad1603d7d7ba52018-03-12 18:51:53 +02001864 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001865 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001866 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1867 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001868
mohammad1603d7d7ba52018-03-12 18:51:53 +02001869 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1870
Moran Pekered346952018-07-05 15:22:45 +03001871 psa_key_policy_init( &policy );
1872 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
1873 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1874
mohammad1603d7d7ba52018-03-12 18:51:53 +02001875 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001876 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001877
Gilles Peskinefe119512018-07-08 21:39:34 +02001878 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
1879 key_slot, alg ) == PSA_SUCCESS );
1880 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
1881 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001882
Gilles Peskinefe119512018-07-08 21:39:34 +02001883 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
1884 iv, iv_size,
1885 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001886 output1_size = (size_t) input->len +
1887 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001888 output1 = mbedtls_calloc( 1, output1_size );
1889 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001890
Gilles Peskine4abf7412018-06-18 16:35:34 +02001891 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001892 output1, output1_size,
1893 &output1_length ) == PSA_SUCCESS );
1894 TEST_ASSERT( psa_cipher_finish( &operation1,
1895 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001896 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001897
Gilles Peskine048b7f02018-06-08 14:20:49 +02001898 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001899
1900 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1901
1902 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001903 output2 = mbedtls_calloc( 1, output2_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001904 TEST_ASSERT( output2 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001905
Gilles Peskinefe119512018-07-08 21:39:34 +02001906 TEST_ASSERT( psa_cipher_set_iv( &operation2,
1907 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001908 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
1909 output2, output2_size,
1910 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001911 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001912 TEST_ASSERT( psa_cipher_finish( &operation2,
1913 output2 + output2_length,
1914 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001915 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001916
Gilles Peskine048b7f02018-06-08 14:20:49 +02001917 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001918
Janos Follath25c4fa82018-07-06 16:23:25 +01001919 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001920
Gilles Peskine4abf7412018-06-18 16:35:34 +02001921 TEST_ASSERT( input->len == output2_length );
1922 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +03001923
1924exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001925 mbedtls_free( output1 );
1926 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03001927 psa_destroy_key( key_slot );
1928 mbedtls_psa_crypto_free( );
1929}
1930/* END_CASE */
1931
1932/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001933void cipher_verify_output_multipart( int alg_arg,
1934 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001935 data_t *key,
1936 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001937 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03001938{
1939 int key_slot = 1;
1940 psa_key_type_t key_type = key_type_arg;
1941 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03001942 unsigned char iv[16] = {0};
1943 size_t iv_size = 16;
1944 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001945 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001946 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001947 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001948 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001949 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001950 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001951 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001952 psa_cipher_operation_t operation1;
1953 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03001954 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03001955
Moran Pekerded84402018-06-06 16:36:50 +03001956 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001957 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001958 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1959 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001960
Moran Pekerded84402018-06-06 16:36:50 +03001961 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1962
Moran Pekered346952018-07-05 15:22:45 +03001963 psa_key_policy_init( &policy );
1964 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
1965 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1966
Moran Pekerded84402018-06-06 16:36:50 +03001967 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001968 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001969
Gilles Peskinefe119512018-07-08 21:39:34 +02001970 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
1971 key_slot, alg ) == PSA_SUCCESS );
1972 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
1973 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001974
Gilles Peskinefe119512018-07-08 21:39:34 +02001975 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
1976 iv, iv_size,
1977 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001978 output1_buffer_size = (size_t) input->len +
1979 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001980 output1 = mbedtls_calloc( 1, output1_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001981 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001982
Gilles Peskine4abf7412018-06-18 16:35:34 +02001983 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001984
itayzafrir3e02b3b2018-06-12 17:06:52 +03001985 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001986 output1, output1_buffer_size,
1987 &function_output_length ) == PSA_SUCCESS );
1988 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001989
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001990 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001991 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001992 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001993 output1, output1_buffer_size,
1994 &function_output_length ) == PSA_SUCCESS );
1995 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001996
Gilles Peskine048b7f02018-06-08 14:20:49 +02001997 TEST_ASSERT( psa_cipher_finish( &operation1,
1998 output1 + output1_length,
1999 output1_buffer_size - output1_length,
2000 &function_output_length ) == PSA_SUCCESS );
2001 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002002
2003 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2004
Gilles Peskine048b7f02018-06-08 14:20:49 +02002005 output2_buffer_size = output1_length;
2006 output2 = mbedtls_calloc( 1, output2_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002007 TEST_ASSERT( output2 != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002008
Gilles Peskinefe119512018-07-08 21:39:34 +02002009 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2010 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002011
2012 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002013 output2, output2_buffer_size,
2014 &function_output_length ) == PSA_SUCCESS );
2015 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002016
Gilles Peskine048b7f02018-06-08 14:20:49 +02002017 TEST_ASSERT( psa_cipher_update( &operation2,
2018 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002019 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002020 output2, output2_buffer_size,
2021 &function_output_length ) == PSA_SUCCESS );
2022 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002023
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002024 TEST_ASSERT( psa_cipher_finish( &operation2,
2025 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002026 output2_buffer_size - output2_length,
2027 &function_output_length ) == PSA_SUCCESS );
2028 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002029
Janos Follath25c4fa82018-07-06 16:23:25 +01002030 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002031
Gilles Peskine4abf7412018-06-18 16:35:34 +02002032 TEST_ASSERT( input->len == output2_length );
2033 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002034
2035exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002036 mbedtls_free( output1 );
2037 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002038 psa_destroy_key( key_slot );
2039 mbedtls_psa_crypto_free( );
2040}
2041/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002042
Gilles Peskine20035e32018-02-03 22:44:14 +01002043/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002044void aead_encrypt_decrypt( int key_type_arg,
2045 data_t * key_data,
2046 int alg_arg,
2047 data_t * input_data,
2048 data_t * nonce,
2049 data_t * additional_data,
2050 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002051{
2052 int slot = 1;
2053 psa_key_type_t key_type = key_type_arg;
2054 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002055 unsigned char *output_data = NULL;
2056 size_t output_size = 0;
2057 size_t output_length = 0;
2058 unsigned char *output_data2 = NULL;
2059 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002060 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002061 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002062 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002063
Gilles Peskinea1cac842018-06-11 19:33:02 +02002064 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002065 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002066 TEST_ASSERT( nonce != NULL );
2067 TEST_ASSERT( additional_data != NULL );
2068 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2069 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2070 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2071 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2072
Gilles Peskine4abf7412018-06-18 16:35:34 +02002073 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002074 output_data = mbedtls_calloc( 1, output_size );
2075 TEST_ASSERT( output_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002076
2077 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2078
2079 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002080 psa_key_policy_set_usage( &policy,
2081 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2082 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002083 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2084
2085 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002086 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002087
2088 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002089 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002090 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002091 additional_data->len,
2092 input_data->x, input_data->len,
2093 output_data, output_size,
2094 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002095
2096 if( PSA_SUCCESS == expected_result )
2097 {
2098 output_data2 = mbedtls_calloc( 1, output_length );
2099 TEST_ASSERT( output_data2 != NULL );
2100
2101 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002102 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002103 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002104 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002105 output_data, output_length,
2106 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002107 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002108
itayzafrir3e02b3b2018-06-12 17:06:52 +03002109 TEST_ASSERT( memcmp( input_data->x, output_data2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002110 input_data->len ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002111 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002112
Gilles Peskinea1cac842018-06-11 19:33:02 +02002113exit:
2114 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002115 mbedtls_free( output_data );
2116 mbedtls_free( output_data2 );
2117 mbedtls_psa_crypto_free( );
2118}
2119/* END_CASE */
2120
2121/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002122void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002123 int alg_arg, data_t * input_data,
2124 data_t * additional_data, data_t * nonce,
2125 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002126{
2127 int slot = 1;
2128 psa_key_type_t key_type = key_type_arg;
2129 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002130 unsigned char *output_data = NULL;
2131 size_t output_size = 0;
2132 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002133 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002134 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002135
Gilles Peskinea1cac842018-06-11 19:33:02 +02002136 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002137 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002138 TEST_ASSERT( additional_data != NULL );
2139 TEST_ASSERT( nonce != NULL );
2140 TEST_ASSERT( expected_result != NULL );
2141 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2142 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2143 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2144 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2145 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2146
Gilles Peskine4abf7412018-06-18 16:35:34 +02002147 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002148 output_data = mbedtls_calloc( 1, output_size );
2149 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02002150
Gilles Peskinea1cac842018-06-11 19:33:02 +02002151 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2152
2153 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002154 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002155 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2156
2157 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002158 key_data->x,
2159 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002160
2161 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002162 nonce->x, nonce->len,
2163 additional_data->x, additional_data->len,
2164 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002165 output_data, output_size,
2166 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002167
itayzafrir3e02b3b2018-06-12 17:06:52 +03002168 TEST_ASSERT( memcmp( output_data, expected_result->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02002169 output_length ) == 0 );
2170
Gilles Peskinea1cac842018-06-11 19:33:02 +02002171exit:
2172 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002173 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002174 mbedtls_psa_crypto_free( );
2175}
2176/* END_CASE */
2177
2178/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002179void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002180 int alg_arg, data_t * input_data,
2181 data_t * additional_data, data_t * nonce,
2182 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002183{
2184 int slot = 1;
2185 psa_key_type_t key_type = key_type_arg;
2186 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002187 unsigned char *output_data = NULL;
2188 size_t output_size = 0;
2189 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002190 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002191 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002192 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002193
Gilles Peskinea1cac842018-06-11 19:33:02 +02002194 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002195 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002196 TEST_ASSERT( additional_data != NULL );
2197 TEST_ASSERT( nonce != NULL );
2198 TEST_ASSERT( expected_data != NULL );
2199 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2200 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2201 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2202 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2203 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2204
Gilles Peskine4abf7412018-06-18 16:35:34 +02002205 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002206 output_data = mbedtls_calloc( 1, output_size );
2207 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02002208
Gilles Peskinea1cac842018-06-11 19:33:02 +02002209 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2210
2211 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002212 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002213 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2214
2215 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002216 key_data->x,
2217 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002218
2219 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002220 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002221 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002222 additional_data->len,
2223 input_data->x, input_data->len,
2224 output_data, output_size,
2225 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002226
Gilles Peskine2d277862018-06-18 15:41:12 +02002227 if( expected_result == PSA_SUCCESS )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002228 {
itayzafrir3e02b3b2018-06-12 17:06:52 +03002229 TEST_ASSERT( memcmp( output_data, expected_data->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02002230 output_length ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002231 }
2232
Gilles Peskinea1cac842018-06-11 19:33:02 +02002233exit:
2234 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002235 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002236 mbedtls_psa_crypto_free( );
2237}
2238/* END_CASE */
2239
2240/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002241void signature_size( int type_arg,
2242 int bits,
2243 int alg_arg,
2244 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002245{
2246 psa_key_type_t type = type_arg;
2247 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002248 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002249 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2250exit:
2251 ;
2252}
2253/* END_CASE */
2254
2255/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002256void sign_deterministic( int key_type_arg, data_t *key_data,
2257 int alg_arg, data_t *input_data,
2258 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002259{
2260 int slot = 1;
2261 psa_key_type_t key_type = key_type_arg;
2262 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002263 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002264 unsigned char *signature = NULL;
2265 size_t signature_size;
2266 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002267 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002268
Gilles Peskine20035e32018-02-03 22:44:14 +01002269 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002270 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002271 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002272 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2273 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2274 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002275
2276 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2277
mohammad1603a97cb8c2018-03-28 03:46:26 -07002278 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002279 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002280 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2281
Gilles Peskine20035e32018-02-03 22:44:14 +01002282 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002283 key_data->x,
2284 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002285 TEST_ASSERT( psa_get_key_information( slot,
2286 NULL,
2287 &key_bits ) == PSA_SUCCESS );
2288
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002289 /* Allocate a buffer which has the size advertized by the
2290 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002291 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2292 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002293 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002294 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine20035e32018-02-03 22:44:14 +01002295 signature = mbedtls_calloc( 1, signature_size );
2296 TEST_ASSERT( signature != NULL );
2297
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002298 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002299 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002300 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002301 signature, signature_size,
2302 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002303 /* Verify that the signature is what is expected. */
Gilles Peskine4abf7412018-06-18 16:35:34 +02002304 TEST_ASSERT( signature_length == output_data->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02002305 TEST_ASSERT( memcmp( signature, output_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002306 output_data->len ) == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01002307
2308exit:
2309 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002310 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002311 mbedtls_psa_crypto_free( );
2312}
2313/* END_CASE */
2314
2315/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002316void sign_fail( int key_type_arg, data_t *key_data,
2317 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002318 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002319{
2320 int slot = 1;
2321 psa_key_type_t key_type = key_type_arg;
2322 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002323 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002324 psa_status_t actual_status;
2325 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002326 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002327 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002328 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002329
Gilles Peskine20035e32018-02-03 22:44:14 +01002330 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002331 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002332 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2333 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2334
Gilles Peskine20035e32018-02-03 22:44:14 +01002335 signature = mbedtls_calloc( 1, signature_size );
2336 TEST_ASSERT( signature != NULL );
2337
2338 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2339
mohammad1603a97cb8c2018-03-28 03:46:26 -07002340 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002341 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002342 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2343
Gilles Peskine20035e32018-02-03 22:44:14 +01002344 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002345 key_data->x,
2346 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002347
2348 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002349 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002350 signature, signature_size,
2351 &signature_length );
2352 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002353 /* The value of *signature_length is unspecified on error, but
2354 * whatever it is, it should be less than signature_size, so that
2355 * if the caller tries to read *signature_length bytes without
2356 * checking the error code then they don't overflow a buffer. */
2357 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002358
2359exit:
2360 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002361 mbedtls_free( signature );
2362 mbedtls_psa_crypto_free( );
2363}
2364/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002365
2366/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002367void sign_verify( int key_type_arg, data_t *key_data,
2368 int alg_arg, data_t *input_data )
2369{
2370 int slot = 1;
2371 psa_key_type_t key_type = key_type_arg;
2372 psa_algorithm_t alg = alg_arg;
2373 size_t key_bits;
2374 unsigned char *signature = NULL;
2375 size_t signature_size;
2376 size_t signature_length = 0xdeadbeef;
2377 psa_key_policy_t policy;
2378
2379 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2380
2381 psa_key_policy_init( &policy );
2382 psa_key_policy_set_usage( &policy,
2383 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2384 alg );
2385 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2386
2387 TEST_ASSERT( psa_import_key( slot, key_type,
2388 key_data->x,
2389 key_data->len ) == PSA_SUCCESS );
2390 TEST_ASSERT( psa_get_key_information( slot,
2391 NULL,
2392 &key_bits ) == PSA_SUCCESS );
2393
2394 /* Allocate a buffer which has the size advertized by the
2395 * library. */
2396 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2397 key_bits, alg );
2398 TEST_ASSERT( signature_size != 0 );
2399 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2400 signature = mbedtls_calloc( 1, signature_size );
2401 TEST_ASSERT( signature != NULL );
2402
2403 /* Perform the signature. */
2404 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2405 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002406 signature, signature_size,
2407 &signature_length ) == PSA_SUCCESS );
2408 /* Check that the signature length looks sensible. */
2409 TEST_ASSERT( signature_length <= signature_size );
2410 TEST_ASSERT( signature_length > 0 );
2411
2412 /* Use the library to verify that the signature is correct. */
2413 TEST_ASSERT( psa_asymmetric_verify(
2414 slot, alg,
2415 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002416 signature, signature_length ) == PSA_SUCCESS );
2417
2418 if( input_data->len != 0 )
2419 {
2420 /* Flip a bit in the input and verify that the signature is now
2421 * detected as invalid. Flip a bit at the beginning, not at the end,
2422 * because ECDSA may ignore the last few bits of the input. */
2423 input_data->x[0] ^= 1;
2424 TEST_ASSERT( psa_asymmetric_verify(
2425 slot, alg,
2426 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002427 signature,
2428 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2429 }
2430
2431exit:
2432 psa_destroy_key( slot );
2433 mbedtls_free( signature );
2434 mbedtls_psa_crypto_free( );
2435}
2436/* END_CASE */
2437
2438/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002439void asymmetric_verify( int key_type_arg, data_t *key_data,
2440 int alg_arg, data_t *hash_data,
2441 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002442{
2443 int slot = 1;
2444 psa_key_type_t key_type = key_type_arg;
2445 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002446 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002447
Gilles Peskine69c12672018-06-28 00:07:19 +02002448 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2449
itayzafrir5c753392018-05-08 11:18:38 +03002450 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002451 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002452 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002453 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2454 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2455 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002456
2457 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2458
2459 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002460 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002461 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2462
2463 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002464 key_data->x,
2465 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002466
2467 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002468 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002469 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002470 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002471exit:
2472 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002473 mbedtls_psa_crypto_free( );
2474}
2475/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002476
2477/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002478void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2479 int alg_arg, data_t *hash_data,
2480 data_t *signature_data,
2481 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002482{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002483 int slot = 1;
2484 psa_key_type_t key_type = key_type_arg;
2485 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002486 psa_status_t actual_status;
2487 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002488 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002489
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002490 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002491 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002492 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002493 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2494 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2495 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002496
2497 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2498
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002499 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002500 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002501 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2502
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002503 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002504 key_data->x,
2505 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002506
2507 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002508 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002509 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002510 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002511
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002512 TEST_ASSERT( actual_status == expected_status );
2513
2514exit:
2515 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002516 mbedtls_psa_crypto_free( );
2517}
2518/* END_CASE */
2519
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002520/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002521void asymmetric_encrypt( int key_type_arg,
2522 data_t *key_data,
2523 int alg_arg,
2524 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02002525 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02002526 int expected_output_length_arg,
2527 int expected_status_arg )
2528{
2529 int slot = 1;
2530 psa_key_type_t key_type = key_type_arg;
2531 psa_algorithm_t alg = alg_arg;
2532 size_t expected_output_length = expected_output_length_arg;
2533 size_t key_bits;
2534 unsigned char *output = NULL;
2535 size_t output_size;
2536 size_t output_length = ~0;
2537 psa_status_t actual_status;
2538 psa_status_t expected_status = expected_status_arg;
2539 psa_key_policy_t policy;
2540
2541 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2542
2543 /* Import the key */
2544 psa_key_policy_init( &policy );
2545 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2546 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2547 TEST_ASSERT( psa_import_key( slot, key_type,
2548 key_data->x,
2549 key_data->len ) == PSA_SUCCESS );
2550
2551 /* Determine the maximum output length */
2552 TEST_ASSERT( psa_get_key_information( slot,
2553 NULL,
2554 &key_bits ) == PSA_SUCCESS );
2555 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
2556 output = mbedtls_calloc( 1, output_size );
Darryl Green9c862252018-07-24 12:52:44 +01002557 TEST_ASSERT( output_size == 0 || output != NULL );
Gilles Peskine656896e2018-06-29 19:12:28 +02002558
2559 /* Encrypt the input */
2560 actual_status = psa_asymmetric_encrypt( slot, alg,
2561 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002562 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02002563 output, output_size,
2564 &output_length );
2565 TEST_ASSERT( actual_status == expected_status );
2566 TEST_ASSERT( output_length == expected_output_length );
2567
Gilles Peskine68428122018-06-30 18:42:41 +02002568 /* If the label is empty, the test framework puts a non-null pointer
2569 * in label->x. Test that a null pointer works as well. */
2570 if( label->len == 0 )
2571 {
2572 output_length = ~0;
2573 memset( output, 0, output_size );
2574 actual_status = psa_asymmetric_encrypt( slot, alg,
2575 input_data->x, input_data->len,
2576 NULL, label->len,
2577 output, output_size,
2578 &output_length );
2579 TEST_ASSERT( actual_status == expected_status );
2580 TEST_ASSERT( output_length == expected_output_length );
2581 }
2582
Gilles Peskine656896e2018-06-29 19:12:28 +02002583exit:
2584 psa_destroy_key( slot );
2585 mbedtls_free( output );
2586 mbedtls_psa_crypto_free( );
2587}
2588/* END_CASE */
2589
2590/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002591void asymmetric_encrypt_decrypt( int key_type_arg,
2592 data_t *key_data,
2593 int alg_arg,
2594 data_t *input_data,
2595 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002596{
2597 int slot = 1;
2598 psa_key_type_t key_type = key_type_arg;
2599 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002600 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002601 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002602 size_t output_size;
2603 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002604 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002605 size_t output2_size;
2606 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002607 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002608
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002609 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002610 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002611 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2612 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2613
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002614 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2615
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002616 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002617 psa_key_policy_set_usage( &policy,
2618 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002619 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002620 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2621
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002622 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002623 key_data->x,
2624 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002625
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002626
2627 /* Determine the maximum ciphertext length */
2628 TEST_ASSERT( psa_get_key_information( slot,
2629 NULL,
2630 &key_bits ) == PSA_SUCCESS );
2631 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
2632 output = mbedtls_calloc( 1, output_size );
2633 TEST_ASSERT( output != NULL );
2634 output2_size = input_data->len;
2635 output2 = mbedtls_calloc( 1, output2_size );
2636 TEST_ASSERT( output2 != NULL );
2637
Gilles Peskineeebd7382018-06-08 18:11:54 +02002638 /* We test encryption by checking that encrypt-then-decrypt gives back
2639 * the original plaintext because of the non-optional random
2640 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02002641 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002642 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002643 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002644 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002645 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002646 /* We don't know what ciphertext length to expect, but check that
2647 * it looks sensible. */
2648 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002649
Gilles Peskine2d277862018-06-18 15:41:12 +02002650 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002651 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02002652 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002653 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002654 &output2_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002655 TEST_ASSERT( output2_length == input_data->len );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002656 TEST_ASSERT( memcmp( input_data->x, output2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002657 input_data->len ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002658
2659exit:
2660 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002661 mbedtls_free( output );
2662 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002663 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002664}
2665/* END_CASE */
2666
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002667/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002668void asymmetric_decrypt( int key_type_arg,
2669 data_t *key_data,
2670 int alg_arg,
2671 data_t *input_data,
2672 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02002673 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002674{
2675 int slot = 1;
2676 psa_key_type_t key_type = key_type_arg;
2677 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002678 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002679 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002680 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002681 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002682
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002683 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002684 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002685 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002686 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2687 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2688 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2689
Gilles Peskine4abf7412018-06-18 16:35:34 +02002690 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002691 output = mbedtls_calloc( 1, output_size );
2692 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002693
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002694 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2695
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002696 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002697 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002698 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2699
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002700 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002701 key_data->x,
2702 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002703
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002704 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002705 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002706 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002707 output,
2708 output_size,
2709 &output_length ) == PSA_SUCCESS );
Gilles Peskine66763a02018-06-29 21:54:10 +02002710 TEST_ASSERT( expected_data->len == output_length );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002711 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002712
Gilles Peskine68428122018-06-30 18:42:41 +02002713 /* If the label is empty, the test framework puts a non-null pointer
2714 * in label->x. Test that a null pointer works as well. */
2715 if( label->len == 0 )
2716 {
2717 output_length = ~0;
2718 memset( output, 0, output_size );
2719 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
2720 input_data->x, input_data->len,
2721 NULL, label->len,
2722 output,
2723 output_size,
2724 &output_length ) == PSA_SUCCESS );
2725 TEST_ASSERT( expected_data->len == output_length );
2726 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
2727 }
2728
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002729exit:
2730 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002731 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002732 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002733}
2734/* END_CASE */
2735
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002736/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002737void asymmetric_decrypt_fail( int key_type_arg,
2738 data_t *key_data,
2739 int alg_arg,
2740 data_t *input_data,
2741 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02002742 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002743{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002744 int slot = 1;
2745 psa_key_type_t key_type = key_type_arg;
2746 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002747 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002748 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002749 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002750 psa_status_t actual_status;
2751 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002752 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002753
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002754 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002755 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002756 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2757 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2758
Gilles Peskine4abf7412018-06-18 16:35:34 +02002759 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002760 output = mbedtls_calloc( 1, output_size );
2761 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002762
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002763 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2764
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002765 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002766 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002767 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2768
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002769 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002770 key_data->x,
2771 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002772
Gilles Peskine2d277862018-06-18 15:41:12 +02002773 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002774 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002775 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002776 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002777 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002778 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002779 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002780
Gilles Peskine68428122018-06-30 18:42:41 +02002781 /* If the label is empty, the test framework puts a non-null pointer
2782 * in label->x. Test that a null pointer works as well. */
2783 if( label->len == 0 )
2784 {
2785 output_length = ~0;
2786 memset( output, 0, output_size );
2787 actual_status = psa_asymmetric_decrypt( slot, alg,
2788 input_data->x, input_data->len,
2789 NULL, label->len,
2790 output, output_size,
2791 &output_length );
2792 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002793 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02002794 }
2795
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002796exit:
2797 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02002798 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002799 mbedtls_psa_crypto_free( );
2800}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002801/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02002802
2803/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002804void derive_setup( int key_type_arg,
2805 data_t *key_data,
2806 int alg_arg,
2807 data_t *salt,
2808 data_t *label,
2809 int requested_capacity_arg,
2810 int expected_status_arg )
2811{
2812 psa_key_slot_t slot = 1;
2813 size_t key_type = key_type_arg;
2814 psa_algorithm_t alg = alg_arg;
2815 size_t requested_capacity = requested_capacity_arg;
2816 psa_status_t expected_status = expected_status_arg;
2817 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2818 psa_key_policy_t policy;
2819
2820 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2821
2822 psa_key_policy_init( &policy );
2823 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2824 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2825
2826 TEST_ASSERT( psa_import_key( slot, key_type,
2827 key_data->x,
2828 key_data->len ) == PSA_SUCCESS );
2829
2830 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
2831 salt->x, salt->len,
2832 label->x, label->len,
2833 requested_capacity ) == expected_status );
2834
2835exit:
2836 psa_generator_abort( &generator );
2837 psa_destroy_key( slot );
2838 mbedtls_psa_crypto_free( );
2839}
2840/* END_CASE */
2841
2842/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02002843void derive_output( int alg_arg,
2844 data_t *key_data,
2845 data_t *salt,
2846 data_t *label,
2847 int requested_capacity_arg,
2848 data_t *expected_output1,
2849 data_t *expected_output2 )
2850{
2851 psa_key_slot_t slot = 1;
2852 psa_algorithm_t alg = alg_arg;
2853 size_t requested_capacity = requested_capacity_arg;
2854 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2855 uint8_t *expected_outputs[2] =
2856 {expected_output1->x, expected_output2->x};
2857 size_t output_sizes[2] =
2858 {expected_output1->len, expected_output2->len};
2859 size_t output_buffer_size = 0;
2860 uint8_t *output_buffer = NULL;
2861 size_t expected_capacity;
2862 size_t current_capacity;
2863 psa_key_policy_t policy;
2864 psa_status_t status;
2865 unsigned i;
2866
2867 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
2868 {
2869 if( output_sizes[i] > output_buffer_size )
2870 output_buffer_size = output_sizes[i];
2871 if( output_sizes[i] == 0 )
2872 expected_outputs[i] = NULL;
2873 }
2874 output_buffer = mbedtls_calloc( 1, output_buffer_size );
2875 TEST_ASSERT( output_buffer != NULL );
2876 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2877
2878 psa_key_policy_init( &policy );
2879 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2880 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2881
2882 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
2883 key_data->x,
2884 key_data->len ) == PSA_SUCCESS );
2885
2886 /* Extraction phase. */
2887 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
2888 salt->x, salt->len,
2889 label->x, label->len,
2890 requested_capacity ) == PSA_SUCCESS );
2891 TEST_ASSERT( psa_get_generator_capacity( &generator,
2892 &current_capacity ) ==
2893 PSA_SUCCESS );
2894 TEST_ASSERT( current_capacity == requested_capacity );
2895 expected_capacity = requested_capacity;
2896
2897 /* Expansion phase. */
2898 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
2899 {
2900 /* Read some bytes. */
2901 status = psa_generator_read( &generator,
2902 output_buffer, output_sizes[i] );
2903 if( expected_capacity == 0 && output_sizes[i] == 0 )
2904 {
2905 /* Reading 0 bytes when 0 bytes are available can go either way. */
2906 TEST_ASSERT( status == PSA_SUCCESS ||
2907 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
2908 continue;
2909 }
2910 else if( expected_capacity == 0 ||
2911 output_sizes[i] > expected_capacity )
2912 {
2913 /* Capacity exceeded. */
2914 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
2915 expected_capacity = 0;
2916 continue;
2917 }
2918 /* Success. Check the read data. */
2919 TEST_ASSERT( status == PSA_SUCCESS );
2920 if( output_sizes[i] != 0 )
2921 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
2922 output_sizes[i] ) == 0 );
2923 /* Check the generator status. */
2924 expected_capacity -= output_sizes[i];
2925 TEST_ASSERT( psa_get_generator_capacity( &generator,
2926 &current_capacity ) ==
2927 PSA_SUCCESS );
2928 TEST_ASSERT( expected_capacity == current_capacity );
2929 }
2930 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
2931
2932exit:
2933 mbedtls_free( output_buffer );
2934 psa_generator_abort( &generator );
2935 psa_destroy_key( slot );
2936 mbedtls_psa_crypto_free( );
2937}
2938/* END_CASE */
2939
2940/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02002941void derive_full( int alg_arg,
2942 data_t *key_data,
2943 data_t *salt,
2944 data_t *label,
2945 int requested_capacity_arg )
2946{
2947 psa_key_slot_t slot = 1;
2948 psa_algorithm_t alg = alg_arg;
2949 size_t requested_capacity = requested_capacity_arg;
2950 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2951 unsigned char output_buffer[16];
2952 size_t expected_capacity = requested_capacity;
2953 size_t current_capacity;
2954 psa_key_policy_t policy;
2955
2956 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2957
2958 psa_key_policy_init( &policy );
2959 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2960 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2961
2962 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
2963 key_data->x,
2964 key_data->len ) == PSA_SUCCESS );
2965
2966 /* Extraction phase. */
2967 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
2968 salt->x, salt->len,
2969 label->x, label->len,
2970 requested_capacity ) == PSA_SUCCESS );
2971 TEST_ASSERT( psa_get_generator_capacity( &generator,
2972 &current_capacity ) ==
2973 PSA_SUCCESS );
2974 TEST_ASSERT( current_capacity == expected_capacity );
2975
2976 /* Expansion phase. */
2977 while( current_capacity > 0 )
2978 {
2979 size_t read_size = sizeof( output_buffer );
2980 if( read_size > current_capacity )
2981 read_size = current_capacity;
2982 TEST_ASSERT( psa_generator_read( &generator,
2983 output_buffer,
2984 read_size ) == PSA_SUCCESS );
2985 expected_capacity -= read_size;
2986 TEST_ASSERT( psa_get_generator_capacity( &generator,
2987 &current_capacity ) ==
2988 PSA_SUCCESS );
2989 TEST_ASSERT( current_capacity == expected_capacity );
2990 }
2991
2992 /* Check that the generator refuses to go over capacity. */
2993 TEST_ASSERT( psa_generator_read( &generator,
2994 output_buffer,
2995 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
2996
2997 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
2998
2999exit:
3000 psa_generator_abort( &generator );
3001 psa_destroy_key( slot );
3002 mbedtls_psa_crypto_free( );
3003}
3004/* END_CASE */
3005
3006/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003007void derive_key_exercise( int alg_arg,
3008 data_t *key_data,
3009 data_t *salt,
3010 data_t *label,
3011 int derived_type_arg,
3012 int derived_bits_arg,
3013 int derived_usage_arg,
3014 int derived_alg_arg )
3015{
3016 psa_key_slot_t base_key = 1;
3017 psa_key_slot_t derived_key = 2;
3018 psa_algorithm_t alg = alg_arg;
3019 psa_key_type_t derived_type = derived_type_arg;
3020 size_t derived_bits = derived_bits_arg;
3021 psa_key_usage_t derived_usage = derived_usage_arg;
3022 psa_algorithm_t derived_alg = derived_alg_arg;
3023 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3024 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3025 psa_key_policy_t policy;
3026 psa_key_type_t got_type;
3027 size_t got_bits;
3028
3029 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3030
3031 psa_key_policy_init( &policy );
3032 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3033 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3034 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3035 key_data->x,
3036 key_data->len ) == PSA_SUCCESS );
3037
3038 /* Derive a key. */
3039 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3040 salt->x, salt->len,
3041 label->x, label->len,
3042 capacity ) == PSA_SUCCESS );
3043 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
3044 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3045 TEST_ASSERT( psa_generator_import_key( derived_key,
3046 derived_type,
3047 derived_bits,
3048 &generator ) == PSA_SUCCESS );
3049
3050 /* Test the key information */
3051 TEST_ASSERT( psa_get_key_information( derived_key,
3052 &got_type,
3053 &got_bits ) == PSA_SUCCESS );
3054 TEST_ASSERT( got_type == derived_type );
3055 TEST_ASSERT( got_bits == derived_bits );
3056
3057 /* Exercise the derived key. */
3058 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
3059 goto exit;
3060
3061exit:
3062 psa_generator_abort( &generator );
3063 psa_destroy_key( base_key );
3064 psa_destroy_key( derived_key );
3065 mbedtls_psa_crypto_free( );
3066}
3067/* END_CASE */
3068
3069/* BEGIN_CASE */
3070void derive_key_export( int alg_arg,
3071 data_t *key_data,
3072 data_t *salt,
3073 data_t *label,
3074 int bytes1_arg,
3075 int bytes2_arg )
3076{
3077 psa_key_slot_t base_key = 1;
3078 psa_key_slot_t derived_key = 2;
3079 psa_algorithm_t alg = alg_arg;
3080 size_t bytes1 = bytes1_arg;
3081 size_t bytes2 = bytes2_arg;
3082 size_t capacity = bytes1 + bytes2;
3083 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3084 uint8_t *output_buffer = mbedtls_calloc( 1, capacity );
3085 uint8_t *export_buffer = mbedtls_calloc( 1, capacity );
3086 psa_key_policy_t policy;
3087 size_t length;
3088
3089 TEST_ASSERT( output_buffer != NULL );
3090 TEST_ASSERT( export_buffer != NULL );
3091 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3092
3093 psa_key_policy_init( &policy );
3094 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3095 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3096 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3097 key_data->x,
3098 key_data->len ) == PSA_SUCCESS );
3099
3100 /* Derive some material and output it. */
3101 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3102 salt->x, salt->len,
3103 label->x, label->len,
3104 capacity ) == PSA_SUCCESS );
3105 TEST_ASSERT( psa_generator_read( &generator,
3106 output_buffer,
3107 capacity ) == PSA_SUCCESS );
3108 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3109
3110 /* Derive the same output again, but this time store it in key objects. */
3111 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3112 salt->x, salt->len,
3113 label->x, label->len,
3114 capacity ) == PSA_SUCCESS );
3115 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
3116 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3117 TEST_ASSERT( psa_generator_import_key( derived_key,
3118 PSA_KEY_TYPE_RAW_DATA,
3119 PSA_BYTES_TO_BITS( bytes1 ),
3120 &generator ) == PSA_SUCCESS );
3121 TEST_ASSERT( psa_export_key( derived_key,
3122 export_buffer, bytes1,
3123 &length ) == PSA_SUCCESS );
3124 TEST_ASSERT( length == bytes1 );
3125 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
3126 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3127 TEST_ASSERT( psa_generator_import_key( derived_key,
3128 PSA_KEY_TYPE_RAW_DATA,
3129 PSA_BYTES_TO_BITS( bytes2 ),
3130 &generator ) == PSA_SUCCESS );
3131 TEST_ASSERT( psa_export_key( derived_key,
3132 export_buffer + bytes1, bytes2,
3133 &length ) == PSA_SUCCESS );
3134 TEST_ASSERT( length == bytes2 );
3135
3136 /* Compare the outputs from the two runs. */
3137 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
3138
3139exit:
3140 mbedtls_free( output_buffer );
3141 mbedtls_free( export_buffer );
3142 psa_generator_abort( &generator );
3143 psa_destroy_key( base_key );
3144 psa_destroy_key( derived_key );
3145 mbedtls_psa_crypto_free( );
3146}
3147/* END_CASE */
3148
3149/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003150void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003151{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003152 size_t bytes = bytes_arg;
3153 const unsigned char trail[] = "don't overwrite me";
3154 unsigned char *output = mbedtls_calloc( 1, bytes + sizeof( trail ) );
3155 unsigned char *changed = mbedtls_calloc( 1, bytes );
3156 size_t i;
3157 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003158
Gilles Peskinea50d7392018-06-21 10:22:13 +02003159 TEST_ASSERT( output != NULL );
Darryl Green9c862252018-07-24 12:52:44 +01003160 TEST_ASSERT( bytes == 0 || changed != NULL );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003161 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003162
3163 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3164
Gilles Peskinea50d7392018-06-21 10:22:13 +02003165 /* Run several times, to ensure that every output byte will be
3166 * nonzero at least once with overwhelming probability
3167 * (2^(-8*number_of_runs)). */
3168 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003169 {
Gilles Peskinea50d7392018-06-21 10:22:13 +02003170 memset( output, 0, bytes );
3171 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
3172
3173 /* Check that no more than bytes have been overwritten */
3174 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
3175
3176 for( i = 0; i < bytes; i++ )
3177 {
3178 if( output[i] != 0 )
3179 ++changed[i];
3180 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003181 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003182
3183 /* Check that every byte was changed to nonzero at least once. This
3184 * validates that psa_generate_random is overwriting every byte of
3185 * the output buffer. */
3186 for( i = 0; i < bytes; i++ )
3187 {
3188 TEST_ASSERT( changed[i] != 0 );
3189 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003190
3191exit:
3192 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003193 mbedtls_free( output );
3194 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003195}
3196/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003197
3198/* BEGIN_CASE */
3199void generate_key( int type_arg,
3200 int bits_arg,
3201 int usage_arg,
3202 int alg_arg,
3203 int expected_status_arg )
3204{
3205 int slot = 1;
3206 psa_key_type_t type = type_arg;
3207 psa_key_usage_t usage = usage_arg;
3208 size_t bits = bits_arg;
3209 psa_algorithm_t alg = alg_arg;
3210 psa_status_t expected_status = expected_status_arg;
3211 psa_key_type_t got_type;
3212 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003213 psa_status_t expected_info_status =
3214 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3215 psa_key_policy_t policy;
3216
3217 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3218
3219 psa_key_policy_init( &policy );
3220 psa_key_policy_set_usage( &policy, usage, alg );
3221 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3222
3223 /* Generate a key */
3224 TEST_ASSERT( psa_generate_key( slot, type, bits,
3225 NULL, 0 ) == expected_status );
3226
3227 /* Test the key information */
3228 TEST_ASSERT( psa_get_key_information( slot,
3229 &got_type,
3230 &got_bits ) == expected_info_status );
3231 if( expected_info_status != PSA_SUCCESS )
3232 goto exit;
3233 TEST_ASSERT( got_type == type );
3234 TEST_ASSERT( got_bits == bits );
3235
Gilles Peskine818ca122018-06-20 18:16:48 +02003236 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02003237 if( ! exercise_key( slot, usage, alg ) )
3238 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003239
3240exit:
3241 psa_destroy_key( slot );
3242 mbedtls_psa_crypto_free( );
3243}
3244/* END_CASE */