blob: 88ef27fbbd89b12f3ce2fec3624676ef7f56942c [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
4#if defined(MBEDTLS_PSA_CRYPTO_SPM)
5#include "spm/psa_defs.h"
6#endif
7
Gilles Peskine0b352bc2018-06-28 00:16:11 +02008#include "mbedtls/asn1write.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +01009#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030010
Gilles Peskine96ee5c72018-07-12 17:24:54 +020011#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
12
itayzafrir3e02b3b2018-06-12 17:06:52 +030013#if(UINT32_MAX > SIZE_MAX)
Gilles Peskine2d277862018-06-18 15:41:12 +020014#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX )
itayzafrir3e02b3b2018-06-12 17:06:52 +030015#else
Gilles Peskine2d277862018-06-18 15:41:12 +020016#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1
itayzafrir3e02b3b2018-06-12 17:06:52 +030017#endif
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020018
Jaeden Amerof24c7f82018-06-27 17:20:43 +010019/** An invalid export length that will never be set by psa_export_key(). */
20static const size_t INVALID_EXPORT_LENGTH = ~0U;
21
Gilles Peskined35a1cc2018-06-26 21:26:10 +020022/** Test if a buffer is all-bits zero.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020023 *
24 * \param buffer Pointer to the beginning of the buffer.
25 * \param size Size of the buffer in bytes.
26 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020027 * \return 1 if the buffer is all-bits-zero.
28 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020029 */
Gilles Peskine3f669c32018-06-21 09:21:51 +020030static int mem_is_zero( void *buffer, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020031{
32 size_t i;
33 for( i = 0; i < size; i++ )
34 {
35 if( ( (unsigned char *) buffer )[i] != 0 )
Gilles Peskine3f669c32018-06-21 09:21:51 +020036 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020038 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020039}
Gilles Peskine818ca122018-06-20 18:16:48 +020040
Gilles Peskine48c0ea12018-06-21 14:15:31 +020041static int key_type_is_raw_bytes( psa_key_type_t type )
42{
43 psa_key_type_t category = type & PSA_KEY_TYPE_CATEGORY_MASK;
44 return( category == PSA_KEY_TYPE_RAW_DATA ||
45 category == PSA_KEY_TYPE_CATEGORY_SYMMETRIC );
46}
47
Gilles Peskine0b352bc2018-06-28 00:16:11 +020048/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
49static int asn1_write_10x( unsigned char **p,
50 unsigned char *start,
51 size_t bits,
52 unsigned char x )
53{
54 int ret;
55 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020056 if( bits == 0 )
57 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
58 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020059 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030060 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020061 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
62 *p -= len;
63 ( *p )[len-1] = x;
64 if( bits % 8 == 0 )
65 ( *p )[1] |= 1;
66 else
67 ( *p )[0] |= 1 << ( bits % 8 );
68 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
69 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
70 MBEDTLS_ASN1_INTEGER ) );
71 return( len );
72}
73
74static int construct_fake_rsa_key( unsigned char *buffer,
75 size_t buffer_size,
76 unsigned char **p,
77 size_t bits,
78 int keypair )
79{
80 size_t half_bits = ( bits + 1 ) / 2;
81 int ret;
82 int len = 0;
83 /* Construct something that looks like a DER encoding of
84 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
85 * RSAPrivateKey ::= SEQUENCE {
86 * version Version,
87 * modulus INTEGER, -- n
88 * publicExponent INTEGER, -- e
89 * privateExponent INTEGER, -- d
90 * prime1 INTEGER, -- p
91 * prime2 INTEGER, -- q
92 * exponent1 INTEGER, -- d mod (p-1)
93 * exponent2 INTEGER, -- d mod (q-1)
94 * coefficient INTEGER, -- (inverse of q) mod p
95 * otherPrimeInfos OtherPrimeInfos OPTIONAL
96 * }
97 * Or, for a public key, the same structure with only
98 * version, modulus and publicExponent.
99 */
100 *p = buffer + buffer_size;
101 if( keypair )
102 {
103 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
104 asn1_write_10x( p, buffer, half_bits, 1 ) );
105 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
106 asn1_write_10x( p, buffer, half_bits, 1 ) );
107 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
108 asn1_write_10x( p, buffer, half_bits, 1 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* q */
110 asn1_write_10x( p, buffer, half_bits, 1 ) );
111 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
112 asn1_write_10x( p, buffer, half_bits, 3 ) );
113 MBEDTLS_ASN1_CHK_ADD( len, /* d */
114 asn1_write_10x( p, buffer, bits, 1 ) );
115 }
116 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
117 asn1_write_10x( p, buffer, 17, 1 ) );
118 MBEDTLS_ASN1_CHK_ADD( len, /* n */
119 asn1_write_10x( p, buffer, bits, 1 ) );
120 if( keypair )
121 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
122 mbedtls_asn1_write_int( p, buffer, 0 ) );
123 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
124 {
125 const unsigned char tag =
126 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
127 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
128 }
129 return( len );
130}
131
Gilles Peskine818ca122018-06-20 18:16:48 +0200132static int exercise_mac_key( psa_key_slot_t key,
133 psa_key_usage_t usage,
134 psa_algorithm_t alg )
135{
136 psa_mac_operation_t operation;
137 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200138 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200139 size_t mac_length = sizeof( mac );
140
141 if( usage & PSA_KEY_USAGE_SIGN )
142 {
Gilles Peskine89167cb2018-07-08 20:12:23 +0200143 TEST_ASSERT( psa_mac_sign_setup( &operation,
144 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200145 TEST_ASSERT( psa_mac_update( &operation,
146 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200147 TEST_ASSERT( psa_mac_sign_finish( &operation,
Gilles Peskineef0cb402018-07-12 16:55:59 +0200148 mac, sizeof( mac ),
Gilles Peskineacd4be32018-07-08 19:56:25 +0200149 &mac_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200150 }
151
152 if( usage & PSA_KEY_USAGE_VERIFY )
153 {
154 psa_status_t verify_status =
155 ( usage & PSA_KEY_USAGE_SIGN ?
156 PSA_SUCCESS :
157 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine89167cb2018-07-08 20:12:23 +0200158 TEST_ASSERT( psa_mac_verify_setup( &operation,
159 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200160 TEST_ASSERT( psa_mac_update( &operation,
161 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200162 TEST_ASSERT( psa_mac_verify_finish( &operation,
163 mac,
164 mac_length ) == verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200165 }
166
167 return( 1 );
168
169exit:
170 psa_mac_abort( &operation );
171 return( 0 );
172}
173
174static int exercise_cipher_key( psa_key_slot_t key,
175 psa_key_usage_t usage,
176 psa_algorithm_t alg )
177{
178 psa_cipher_operation_t operation;
179 unsigned char iv[16] = {0};
180 size_t iv_length = sizeof( iv );
181 const unsigned char plaintext[16] = "Hello, world...";
182 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
183 size_t ciphertext_length = sizeof( ciphertext );
184 unsigned char decrypted[sizeof( ciphertext )];
185 size_t part_length;
186
187 if( usage & PSA_KEY_USAGE_ENCRYPT )
188 {
Gilles Peskinefe119512018-07-08 21:39:34 +0200189 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
190 key, alg ) == PSA_SUCCESS );
191 TEST_ASSERT( psa_cipher_generate_iv( &operation,
192 iv, sizeof( iv ),
193 &iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200194 TEST_ASSERT( psa_cipher_update( &operation,
195 plaintext, sizeof( plaintext ),
196 ciphertext, sizeof( ciphertext ),
197 &ciphertext_length ) == PSA_SUCCESS );
198 TEST_ASSERT( psa_cipher_finish( &operation,
199 ciphertext + ciphertext_length,
200 sizeof( ciphertext ) - ciphertext_length,
201 &part_length ) == PSA_SUCCESS );
202 ciphertext_length += part_length;
203 }
204
205 if( usage & PSA_KEY_USAGE_DECRYPT )
206 {
207 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700208 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200209 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
210 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200211 size_t bits;
212 TEST_ASSERT( psa_get_key_information( key, &type, &bits ) );
213 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
214 }
Gilles Peskinefe119512018-07-08 21:39:34 +0200215 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
216 key, alg ) == PSA_SUCCESS );
217 TEST_ASSERT( psa_cipher_set_iv( &operation,
218 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200219 TEST_ASSERT( psa_cipher_update( &operation,
220 ciphertext, ciphertext_length,
221 decrypted, sizeof( decrypted ),
222 &part_length ) == PSA_SUCCESS );
223 status = psa_cipher_finish( &operation,
224 decrypted + part_length,
225 sizeof( decrypted ) - part_length,
226 &part_length );
227 /* For a stream cipher, all inputs are valid. For a block cipher,
228 * if the input is some aribtrary data rather than an actual
229 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700230 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700231 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine818ca122018-06-20 18:16:48 +0200232 TEST_ASSERT( status == PSA_SUCCESS );
233 else
234 TEST_ASSERT( status == PSA_SUCCESS ||
235 status == PSA_ERROR_INVALID_PADDING );
236 }
237
238 return( 1 );
239
240exit:
241 psa_cipher_abort( &operation );
242 return( 0 );
243}
244
245static int exercise_aead_key( psa_key_slot_t key,
246 psa_key_usage_t usage,
247 psa_algorithm_t alg )
248{
249 unsigned char nonce[16] = {0};
250 size_t nonce_length = sizeof( nonce );
251 unsigned char plaintext[16] = "Hello, world...";
252 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
253 size_t ciphertext_length = sizeof( ciphertext );
254 size_t plaintext_length = sizeof( ciphertext );
255
256 if( usage & PSA_KEY_USAGE_ENCRYPT )
257 {
258 TEST_ASSERT( psa_aead_encrypt( key, alg,
259 nonce, nonce_length,
260 NULL, 0,
261 plaintext, sizeof( plaintext ),
262 ciphertext, sizeof( ciphertext ),
263 &ciphertext_length ) == PSA_SUCCESS );
264 }
265
266 if( usage & PSA_KEY_USAGE_DECRYPT )
267 {
268 psa_status_t verify_status =
269 ( usage & PSA_KEY_USAGE_ENCRYPT ?
270 PSA_SUCCESS :
271 PSA_ERROR_INVALID_SIGNATURE );
272 TEST_ASSERT( psa_aead_decrypt( key, alg,
273 nonce, nonce_length,
274 NULL, 0,
275 ciphertext, ciphertext_length,
276 plaintext, sizeof( plaintext ),
277 &plaintext_length ) == verify_status );
278 }
279
280 return( 1 );
281
282exit:
283 return( 0 );
284}
285
286static int exercise_signature_key( psa_key_slot_t key,
287 psa_key_usage_t usage,
288 psa_algorithm_t alg )
289{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200290 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
291 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200292 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200293 size_t signature_length = sizeof( signature );
294
295 if( usage & PSA_KEY_USAGE_SIGN )
296 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200297 /* Some algorithms require the payload to have the size of
298 * the hash encoded in the algorithm. Use this input size
299 * even for algorithms that allow other input sizes. */
300 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
301 if( hash_alg != 0 )
302 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200303 TEST_ASSERT( psa_asymmetric_sign( key, alg,
304 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200305 signature, sizeof( signature ),
306 &signature_length ) == PSA_SUCCESS );
307 }
308
309 if( usage & PSA_KEY_USAGE_VERIFY )
310 {
311 psa_status_t verify_status =
312 ( usage & PSA_KEY_USAGE_SIGN ?
313 PSA_SUCCESS :
314 PSA_ERROR_INVALID_SIGNATURE );
315 TEST_ASSERT( psa_asymmetric_verify( key, alg,
316 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200317 signature, signature_length ) ==
318 verify_status );
319 }
320
321 return( 1 );
322
323exit:
324 return( 0 );
325}
326
327static int exercise_asymmetric_encryption_key( psa_key_slot_t key,
328 psa_key_usage_t usage,
329 psa_algorithm_t alg )
330{
331 unsigned char plaintext[256] = "Hello, world...";
332 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
333 size_t ciphertext_length = sizeof( ciphertext );
334 size_t plaintext_length = 16;
335
336 if( usage & PSA_KEY_USAGE_ENCRYPT )
337 {
338 TEST_ASSERT(
339 psa_asymmetric_encrypt( key, alg,
340 plaintext, plaintext_length,
341 NULL, 0,
342 ciphertext, sizeof( ciphertext ),
343 &ciphertext_length ) == PSA_SUCCESS );
344 }
345
346 if( usage & PSA_KEY_USAGE_DECRYPT )
347 {
348 psa_status_t status =
349 psa_asymmetric_decrypt( key, alg,
350 ciphertext, ciphertext_length,
351 NULL, 0,
352 plaintext, sizeof( plaintext ),
353 &plaintext_length );
354 TEST_ASSERT( status == PSA_SUCCESS ||
355 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
356 ( status == PSA_ERROR_INVALID_ARGUMENT ||
357 status == PSA_ERROR_INVALID_PADDING ) ) );
358 }
359
360 return( 1 );
361
362exit:
363 return( 0 );
364}
Gilles Peskine02b75072018-07-01 22:31:34 +0200365
Gilles Peskineea0fb492018-07-12 17:17:20 +0200366static int exercise_key_derivation_key( psa_key_slot_t key,
367 psa_key_usage_t usage,
368 psa_algorithm_t alg )
369{
370 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
371 unsigned char label[16] = "This is a label.";
372 size_t label_length = sizeof( label );
373 unsigned char seed[16] = "abcdefghijklmnop";
374 size_t seed_length = sizeof( seed );
375 unsigned char output[1];
376
377 if( usage & PSA_KEY_USAGE_DERIVE )
378 {
379 TEST_ASSERT( psa_key_derivation( &generator,
380 key, alg,
381 label, label_length,
382 seed, seed_length,
383 sizeof( output ) ) == PSA_SUCCESS );
384 TEST_ASSERT( psa_generator_read( &generator,
385 output,
386 sizeof( output ) ) == PSA_SUCCESS );
387 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
388 }
389
390 return( 1 );
391
392exit:
393 return( 0 );
394}
395
Gilles Peskine02b75072018-07-01 22:31:34 +0200396static int exercise_key( psa_key_slot_t slot,
397 psa_key_usage_t usage,
398 psa_algorithm_t alg )
399{
400 int ok;
401 if( alg == 0 )
402 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
403 else if( PSA_ALG_IS_MAC( alg ) )
404 ok = exercise_mac_key( slot, usage, alg );
405 else if( PSA_ALG_IS_CIPHER( alg ) )
406 ok = exercise_cipher_key( slot, usage, alg );
407 else if( PSA_ALG_IS_AEAD( alg ) )
408 ok = exercise_aead_key( slot, usage, alg );
409 else if( PSA_ALG_IS_SIGN( alg ) )
410 ok = exercise_signature_key( slot, usage, alg );
411 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
412 ok = exercise_asymmetric_encryption_key( slot, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200413 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
414 ok = exercise_key_derivation_key( slot, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200415 else
416 {
417 char message[40];
418 mbedtls_snprintf( message, sizeof( message ),
419 "No code to exercise alg=0x%08lx",
420 (unsigned long) alg );
421 test_fail( message, __LINE__, __FILE__ );
422 ok = 0;
423 }
424 return( ok );
425}
426
Gilles Peskinee59236f2018-01-27 23:32:46 +0100427/* END_HEADER */
428
429/* BEGIN_DEPENDENCIES
430 * depends_on:MBEDTLS_PSA_CRYPTO_C
431 * END_DEPENDENCIES
432 */
433
434/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200435void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100436{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100437 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100438 int i;
439 for( i = 0; i <= 1; i++ )
440 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100441 status = psa_crypto_init( );
442 TEST_ASSERT( status == PSA_SUCCESS );
443 status = psa_crypto_init( );
444 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100445 mbedtls_psa_crypto_free( );
446 }
447}
448/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100449
450/* BEGIN_CASE */
Gilles Peskine996deb12018-08-01 15:45:45 +0200451void fill_slots( int max_arg )
452{
453 /* Fill all the slots until we run out of memory or out of slots,
454 * or until some limit specified in the test data for the sake of
455 * implementations with an essentially unlimited number of slots.
456 * This test assumes that available slots are numbered from 1. */
457
458 psa_key_slot_t slot;
459 psa_key_slot_t max = 0;
460 psa_key_policy_t policy;
461 uint8_t exported[sizeof( max )];
462 size_t exported_size;
463 psa_status_t status;
464
465 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
466
467 psa_key_policy_init( &policy );
468 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
469
470 for( max = 1; max <= (size_t) max_arg; max++ )
471 {
472 status = psa_set_key_policy( max, &policy );
473 /* Stop filling slots if we run out of memory or out of
474 * available slots. */
475 TEST_ASSERT( status == PSA_SUCCESS ||
476 status == PSA_ERROR_INSUFFICIENT_MEMORY ||
477 status == PSA_ERROR_INVALID_ARGUMENT );
478 if( status != PSA_SUCCESS )
479 break;
480 status = psa_import_key( max, PSA_KEY_TYPE_RAW_DATA,
481 (uint8_t*) &max, sizeof( max ) );
482 /* Since psa_set_key_policy succeeded, we know that the slot
483 * number is valid. But we may legitimately run out of memory. */
484 TEST_ASSERT( status == PSA_SUCCESS ||
485 status == PSA_ERROR_INSUFFICIENT_MEMORY );
486 if( status != PSA_SUCCESS )
487 break;
488 }
489 /* `max` is now the first slot number that wasn't filled. */
490 max -= 1;
491
492 for( slot = 1; slot <= max; slot++ )
493 {
494 TEST_ASSERT( psa_export_key( slot,
495 exported, sizeof( exported ),
496 &exported_size ) == PSA_SUCCESS );
497 TEST_ASSERT( exported_size == sizeof( slot ) );
498 TEST_ASSERT( memcmp( exported, &slot, sizeof( slot ) ) == 0 );
499 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
500 }
501
502exit:
503 for( slot = 1; slot <= max; slot++ )
504 psa_destroy_key( slot );
505 mbedtls_psa_crypto_free( );
506}
507/* END_CASE */
508
509/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200510void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100511{
512 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200513 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100514 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100515
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100516 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300517 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100518 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
519
Gilles Peskine4abf7412018-06-18 16:35:34 +0200520 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200521 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100522 if( status == PSA_SUCCESS )
523 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
524
525exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100526 mbedtls_psa_crypto_free( );
527}
528/* END_CASE */
529
530/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200531void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
532{
533 int slot = 1;
534 size_t bits = bits_arg;
535 psa_status_t expected_status = expected_status_arg;
536 psa_status_t status;
537 psa_key_type_t type =
538 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
539 size_t buffer_size = /* Slight overapproximations */
540 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
541 unsigned char *buffer = mbedtls_calloc( 1, buffer_size );
542 unsigned char *p;
543 int ret;
544 size_t length;
545
546 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
547 TEST_ASSERT( buffer != NULL );
548
549 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
550 bits, keypair ) ) >= 0 );
551 length = ret;
552
553 /* Try importing the key */
554 status = psa_import_key( slot, type, p, length );
555 TEST_ASSERT( status == expected_status );
556 if( status == PSA_SUCCESS )
557 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
558
559exit:
560 mbedtls_free( buffer );
561 mbedtls_psa_crypto_free( );
562}
563/* END_CASE */
564
565/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300566void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300567 int type_arg,
568 int alg_arg,
569 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100570 int expected_bits,
571 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200572 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100573 int canonical_input )
574{
575 int slot = 1;
576 int slot2 = slot + 1;
577 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200578 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200579 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100580 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100581 unsigned char *exported = NULL;
582 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100583 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100584 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100585 size_t reexported_length;
586 psa_key_type_t got_type;
587 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200588 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100589
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100590 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300591 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +0300592 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100593 exported = mbedtls_calloc( 1, export_size );
594 TEST_ASSERT( exported != NULL );
595 if( ! canonical_input )
596 {
597 reexported = mbedtls_calloc( 1, export_size );
598 TEST_ASSERT( reexported != NULL );
599 }
600 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
601
mohammad1603a97cb8c2018-03-28 03:46:26 -0700602 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200603 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700604 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
605
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100606 /* Import the key */
607 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200608 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100609
610 /* Test the key information */
611 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200612 &got_type,
613 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100614 TEST_ASSERT( got_type == type );
615 TEST_ASSERT( got_bits == (size_t) expected_bits );
616
617 /* Export the key */
618 status = psa_export_key( slot,
619 exported, export_size,
620 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200621 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100622
623 /* The exported length must be set by psa_export_key() to a value between 0
624 * and export_size. On errors, the exported length must be 0. */
625 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
626 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
627 TEST_ASSERT( exported_length <= export_size );
628
Gilles Peskine3f669c32018-06-21 09:21:51 +0200629 TEST_ASSERT( mem_is_zero( exported + exported_length,
630 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100631 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200632 {
633 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100634 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200635 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100636
637 if( canonical_input )
638 {
Gilles Peskine4abf7412018-06-18 16:35:34 +0200639 TEST_ASSERT( exported_length == data->len );
640 TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100641 }
642 else
643 {
mohammad1603a97cb8c2018-03-28 03:46:26 -0700644 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
645
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100646 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200647 exported,
648 export_size ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100649 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200650 reexported,
651 export_size,
652 &reexported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100653 TEST_ASSERT( reexported_length == exported_length );
654 TEST_ASSERT( memcmp( reexported, exported,
655 exported_length ) == 0 );
656 }
657
658destroy:
659 /* Destroy the key */
660 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
661 TEST_ASSERT( psa_get_key_information(
662 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
663
664exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300665 mbedtls_free( exported );
666 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100667 mbedtls_psa_crypto_free( );
668}
669/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100670
Moran Pekerf709f4a2018-06-06 17:26:04 +0300671/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300672void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200673 int type_arg,
674 int alg_arg,
675 int expected_bits,
676 int public_key_expected_length,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200677 int expected_export_status_arg )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300678{
679 int slot = 1;
680 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200681 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200682 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300683 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300684 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300685 size_t export_size;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100686 size_t exported_length = INVALID_EXPORT_LENGTH;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300687 psa_key_type_t got_type;
688 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200689 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300690
Moran Pekerf709f4a2018-06-06 17:26:04 +0300691 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300692 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +0300693 export_size = (ptrdiff_t) data->len;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300694 exported = mbedtls_calloc( 1, export_size );
695 TEST_ASSERT( exported != NULL );
696
697 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
698
699 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200700 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300701 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
702
703 /* Import the key */
704 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200705 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300706
707 /* Test the key information */
708 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200709 &got_type,
710 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300711 TEST_ASSERT( got_type == type );
712 TEST_ASSERT( got_bits == (size_t) expected_bits );
713
714 /* Export the key */
715 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200716 exported, export_size,
717 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200718 TEST_ASSERT( status == expected_export_status );
Jaeden Amero2a671e92018-06-27 17:47:40 +0100719 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
720 TEST_ASSERT( mem_is_zero( exported + exported_length,
721 export_size - exported_length ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300722 if( status != PSA_SUCCESS )
723 goto destroy;
724
Moran Pekerf709f4a2018-06-06 17:26:04 +0300725destroy:
726 /* Destroy the key */
727 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
728 TEST_ASSERT( psa_get_key_information(
729 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
730
731exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300732 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300733 mbedtls_psa_crypto_free( );
734}
735/* END_CASE */
736
Gilles Peskine20035e32018-02-03 22:44:14 +0100737/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200738void import_and_exercise_key( data_t *data,
739 int type_arg,
740 int bits_arg,
741 int alg_arg )
742{
743 int slot = 1;
744 psa_key_type_t type = type_arg;
745 size_t bits = bits_arg;
746 psa_algorithm_t alg = alg_arg;
747 psa_key_usage_t usage =
748 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
749 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
750 PSA_KEY_USAGE_VERIFY :
751 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
752 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
753 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
754 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
755 PSA_KEY_USAGE_ENCRYPT :
756 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
Gilles Peskineea0fb492018-07-12 17:17:20 +0200757 PSA_ALG_IS_KEY_DERIVATION( alg ) ? PSA_KEY_USAGE_DERIVE :
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200758 0 );
759 psa_key_policy_t policy;
760 psa_key_type_t got_type;
761 size_t got_bits;
762 psa_status_t status;
763
764 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
765
766 psa_key_policy_init( &policy );
767 psa_key_policy_set_usage( &policy, usage, alg );
768 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
769
770 /* Import the key */
771 status = psa_import_key( slot, type, data->x, data->len );
772 TEST_ASSERT( status == PSA_SUCCESS );
773
774 /* Test the key information */
775 TEST_ASSERT( psa_get_key_information( slot,
776 &got_type,
777 &got_bits ) == PSA_SUCCESS );
778 TEST_ASSERT( got_type == type );
779 TEST_ASSERT( got_bits == bits );
780
781 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +0200782 if( ! exercise_key( slot, usage, alg ) )
783 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200784
785exit:
786 psa_destroy_key( slot );
787 mbedtls_psa_crypto_free( );
788}
789/* END_CASE */
790
791/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +0200792void key_policy( int usage_arg, int alg_arg )
793{
794 int key_slot = 1;
795 psa_algorithm_t alg = alg_arg;
796 psa_key_usage_t usage = usage_arg;
797 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
798 unsigned char key[32] = {0};
799 psa_key_policy_t policy_set;
800 psa_key_policy_t policy_get;
801
802 memset( key, 0x2a, sizeof( key ) );
803
804 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
805
806 psa_key_policy_init( &policy_set );
807 psa_key_policy_init( &policy_get );
808
809 psa_key_policy_set_usage( &policy_set, usage, alg );
810
811 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
812 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
813 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
814
815 TEST_ASSERT( psa_import_key( key_slot, key_type,
816 key, sizeof( key ) ) == PSA_SUCCESS );
817
818 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
819
820 TEST_ASSERT( policy_get.usage == policy_set.usage );
821 TEST_ASSERT( policy_get.alg == policy_set.alg );
822
823exit:
824 psa_destroy_key( key_slot );
825 mbedtls_psa_crypto_free( );
826}
827/* END_CASE */
828
829/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200830void mac_key_policy( int policy_usage,
831 int policy_alg,
832 int key_type,
833 data_t *key_data,
834 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200835{
836 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +0200837 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200838 psa_mac_operation_t operation;
839 psa_status_t status;
840 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200841
842 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
843
844 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200845 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200846 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
847
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200848 TEST_ASSERT( psa_import_key( key_slot, key_type,
849 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +0200850
Gilles Peskine89167cb2018-07-08 20:12:23 +0200851 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200852 if( policy_alg == exercise_alg &&
853 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
854 TEST_ASSERT( status == PSA_SUCCESS );
855 else
856 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
857 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200858
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200859 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +0200860 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200861 if( policy_alg == exercise_alg &&
862 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +0200863 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200864 else
865 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
866
867exit:
868 psa_mac_abort( &operation );
869 psa_destroy_key( key_slot );
870 mbedtls_psa_crypto_free( );
871}
872/* END_CASE */
873
874/* BEGIN_CASE */
875void cipher_key_policy( int policy_usage,
876 int policy_alg,
877 int key_type,
878 data_t *key_data,
879 int exercise_alg )
880{
881 int key_slot = 1;
882 psa_key_policy_t policy;
883 psa_cipher_operation_t operation;
884 psa_status_t status;
885
886 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
887
888 psa_key_policy_init( &policy );
889 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
890 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
891
892 TEST_ASSERT( psa_import_key( key_slot, key_type,
893 key_data->x, key_data->len ) == PSA_SUCCESS );
894
Gilles Peskinefe119512018-07-08 21:39:34 +0200895 status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200896 if( policy_alg == exercise_alg &&
897 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
898 TEST_ASSERT( status == PSA_SUCCESS );
899 else
900 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
901 psa_cipher_abort( &operation );
902
Gilles Peskinefe119512018-07-08 21:39:34 +0200903 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200904 if( policy_alg == exercise_alg &&
905 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
906 TEST_ASSERT( status == PSA_SUCCESS );
907 else
908 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
909
910exit:
911 psa_cipher_abort( &operation );
912 psa_destroy_key( key_slot );
913 mbedtls_psa_crypto_free( );
914}
915/* END_CASE */
916
917/* BEGIN_CASE */
918void aead_key_policy( int policy_usage,
919 int policy_alg,
920 int key_type,
921 data_t *key_data,
922 int nonce_length_arg,
923 int tag_length_arg,
924 int exercise_alg )
925{
926 int key_slot = 1;
927 psa_key_policy_t policy;
928 psa_status_t status;
929 unsigned char nonce[16] = {0};
930 size_t nonce_length = nonce_length_arg;
931 unsigned char tag[16];
932 size_t tag_length = tag_length_arg;
933 size_t output_length;
934
935 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
936 TEST_ASSERT( tag_length <= sizeof( tag ) );
937
938 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
939
940 psa_key_policy_init( &policy );
941 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
942 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
943
944 TEST_ASSERT( psa_import_key( key_slot, key_type,
945 key_data->x, key_data->len ) == PSA_SUCCESS );
946
947 status = psa_aead_encrypt( key_slot, exercise_alg,
948 nonce, nonce_length,
949 NULL, 0,
950 NULL, 0,
951 tag, tag_length,
952 &output_length );
953 if( policy_alg == exercise_alg &&
954 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
955 TEST_ASSERT( status == PSA_SUCCESS );
956 else
957 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
958
959 memset( tag, 0, sizeof( tag ) );
960 status = psa_aead_decrypt( key_slot, exercise_alg,
961 nonce, nonce_length,
962 NULL, 0,
963 tag, tag_length,
964 NULL, 0,
965 &output_length );
966 if( policy_alg == exercise_alg &&
967 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
968 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
969 else
970 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
971
972exit:
973 psa_destroy_key( key_slot );
974 mbedtls_psa_crypto_free( );
975}
976/* END_CASE */
977
978/* BEGIN_CASE */
979void asymmetric_encryption_key_policy( int policy_usage,
980 int policy_alg,
981 int key_type,
982 data_t *key_data,
983 int exercise_alg )
984{
985 int key_slot = 1;
986 psa_key_policy_t policy;
987 psa_status_t status;
988 size_t key_bits;
989 size_t buffer_length;
990 unsigned char *buffer = NULL;
991 size_t output_length;
992
993 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
994
995 psa_key_policy_init( &policy );
996 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
997 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
998
999 TEST_ASSERT( psa_import_key( key_slot, key_type,
1000 key_data->x, key_data->len ) == PSA_SUCCESS );
1001
1002 TEST_ASSERT( psa_get_key_information( key_slot,
1003 NULL,
1004 &key_bits ) == PSA_SUCCESS );
1005 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1006 exercise_alg );
1007 buffer = mbedtls_calloc( 1, buffer_length );
1008 TEST_ASSERT( buffer != NULL );
1009
1010 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
1011 NULL, 0,
1012 NULL, 0,
1013 buffer, buffer_length,
1014 &output_length );
1015 if( policy_alg == exercise_alg &&
1016 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1017 TEST_ASSERT( status == PSA_SUCCESS );
1018 else
1019 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1020
1021 memset( buffer, 0, buffer_length );
1022 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
1023 buffer, buffer_length,
1024 NULL, 0,
1025 buffer, buffer_length,
1026 &output_length );
1027 if( policy_alg == exercise_alg &&
1028 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1029 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
1030 else
1031 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1032
1033exit:
1034 psa_destroy_key( key_slot );
1035 mbedtls_psa_crypto_free( );
1036 mbedtls_free( buffer );
1037}
1038/* END_CASE */
1039
1040/* BEGIN_CASE */
1041void asymmetric_signature_key_policy( int policy_usage,
1042 int policy_alg,
1043 int key_type,
1044 data_t *key_data,
1045 int exercise_alg )
1046{
1047 int key_slot = 1;
1048 psa_key_policy_t policy;
1049 psa_status_t status;
1050 unsigned char payload[16] = {1};
1051 size_t payload_length = sizeof( payload );
1052 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1053 size_t signature_length;
1054
1055 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1056
1057 psa_key_policy_init( &policy );
1058 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1059 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1060
1061 TEST_ASSERT( psa_import_key( key_slot, key_type,
1062 key_data->x, key_data->len ) == PSA_SUCCESS );
1063
1064 status = psa_asymmetric_sign( key_slot, exercise_alg,
1065 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001066 signature, sizeof( signature ),
1067 &signature_length );
1068 if( policy_alg == exercise_alg &&
1069 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1070 TEST_ASSERT( status == PSA_SUCCESS );
1071 else
1072 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1073
1074 memset( signature, 0, sizeof( signature ) );
1075 status = psa_asymmetric_verify( key_slot, exercise_alg,
1076 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001077 signature, sizeof( signature ) );
1078 if( policy_alg == exercise_alg &&
1079 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1080 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1081 else
1082 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001083
1084exit:
1085 psa_destroy_key( key_slot );
1086 mbedtls_psa_crypto_free( );
1087}
1088/* END_CASE */
1089
1090/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001091void derive_key_policy( int policy_usage,
1092 int policy_alg,
1093 int key_type,
1094 data_t *key_data,
1095 int exercise_alg )
1096{
1097 int key_slot = 1;
1098 psa_key_policy_t policy;
1099 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1100 psa_status_t status;
1101
1102 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1103
1104 psa_key_policy_init( &policy );
1105 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1106 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1107
1108 TEST_ASSERT( psa_import_key( key_slot, key_type,
1109 key_data->x, key_data->len ) == PSA_SUCCESS );
1110
1111 status = psa_key_derivation( &generator, key_slot,
1112 exercise_alg,
1113 NULL, 0,
1114 NULL, 0,
1115 1 );
1116 if( policy_alg == exercise_alg &&
1117 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1118 TEST_ASSERT( status == PSA_SUCCESS );
1119 else
1120 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1121
1122exit:
1123 psa_generator_abort( &generator );
1124 psa_destroy_key( key_slot );
1125 mbedtls_psa_crypto_free( );
1126}
1127/* END_CASE */
1128
1129/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001130void key_lifetime( int lifetime_arg )
1131{
1132 int key_slot = 1;
1133 psa_key_type_t key_type = PSA_ALG_CBC_BASE;
1134 unsigned char key[32] = {0};
1135 psa_key_lifetime_t lifetime_set = lifetime_arg;
1136 psa_key_lifetime_t lifetime_get;
1137
1138 memset( key, 0x2a, sizeof( key ) );
1139
1140 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1141
1142 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1143 lifetime_set ) == PSA_SUCCESS );
1144
1145 TEST_ASSERT( psa_import_key( key_slot, key_type,
1146 key, sizeof( key ) ) == PSA_SUCCESS );
1147
1148 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1149 &lifetime_get ) == PSA_SUCCESS );
1150
1151 TEST_ASSERT( lifetime_get == lifetime_set );
1152
1153exit:
1154 psa_destroy_key( key_slot );
1155 mbedtls_psa_crypto_free( );
1156}
1157/* END_CASE */
1158
1159/* BEGIN_CASE */
1160void key_lifetime_set_fail( int key_slot_arg,
1161 int lifetime_arg,
1162 int expected_status_arg )
1163{
1164 psa_key_slot_t key_slot = key_slot_arg;
1165 psa_key_lifetime_t lifetime_set = lifetime_arg;
1166 psa_status_t actual_status;
1167 psa_status_t expected_status = expected_status_arg;
1168
1169 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1170
1171 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1172
1173 if( actual_status == PSA_SUCCESS )
1174 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1175
1176 TEST_ASSERT( expected_status == actual_status );
1177
1178exit:
1179 psa_destroy_key( key_slot );
1180 mbedtls_psa_crypto_free( );
1181}
1182/* END_CASE */
1183
1184/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001185void hash_setup( int alg_arg,
1186 int expected_status_arg )
1187{
1188 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001189 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001190 psa_hash_operation_t operation;
1191 psa_status_t status;
1192
1193 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1194
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001195 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001196 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001197 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001198
1199exit:
1200 mbedtls_psa_crypto_free( );
1201}
1202/* END_CASE */
1203
1204/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001205void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001206{
1207 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001208 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001209 size_t actual_hash_length;
1210 psa_hash_operation_t operation;
1211
Gilles Peskine69c12672018-06-28 00:07:19 +02001212 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1213 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1214
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001215 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001216 TEST_ASSERT( expected_hash != NULL );
1217 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1218 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001219
1220 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1221
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001222 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001223 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001224 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001225 TEST_ASSERT( psa_hash_finish( &operation,
1226 actual_hash, sizeof( actual_hash ),
1227 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001228 TEST_ASSERT( actual_hash_length == expected_hash->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001229 TEST_ASSERT( memcmp( expected_hash->x, actual_hash,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001230 expected_hash->len ) == 0 );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001231
1232exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001233 mbedtls_psa_crypto_free( );
1234}
1235/* END_CASE */
1236
1237/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001238void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001239{
1240 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001241 psa_hash_operation_t operation;
1242
Gilles Peskine69c12672018-06-28 00:07:19 +02001243 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1244 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1245
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001246 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001247 TEST_ASSERT( expected_hash != NULL );
1248 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1249 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001250
1251 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1252
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001253 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001254 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001255 input->x,
1256 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001257 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001258 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001259 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001260
1261exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001262 mbedtls_psa_crypto_free( );
1263}
1264/* END_CASE */
1265
1266/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001267void mac_setup( int key_type_arg,
1268 data_t *key,
1269 int alg_arg,
1270 int expected_status_arg )
1271{
1272 int key_slot = 1;
1273 psa_key_type_t key_type = key_type_arg;
1274 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001275 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001276 psa_mac_operation_t operation;
1277 psa_key_policy_t policy;
1278 psa_status_t status;
1279
1280 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1281
1282 psa_key_policy_init( &policy );
1283 psa_key_policy_set_usage( &policy,
1284 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1285 alg );
1286 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1287
1288 TEST_ASSERT( psa_import_key( key_slot, key_type,
1289 key->x, key->len ) == PSA_SUCCESS );
1290
Gilles Peskine89167cb2018-07-08 20:12:23 +02001291 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001292 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001293 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001294
1295exit:
1296 psa_destroy_key( key_slot );
1297 mbedtls_psa_crypto_free( );
1298}
1299/* END_CASE */
1300
1301/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001302void mac_verify( int key_type_arg,
1303 data_t *key,
1304 int alg_arg,
1305 data_t *input,
1306 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001307{
1308 int key_slot = 1;
1309 psa_key_type_t key_type = key_type_arg;
1310 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001311 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001312 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001313
Gilles Peskine69c12672018-06-28 00:07:19 +02001314 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1315
Gilles Peskine8c9def32018-02-08 10:02:12 +01001316 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001317 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001318 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001319 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001320 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1321 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001322
1323 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1324
mohammad16036df908f2018-04-02 08:34:15 -07001325 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001326 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07001327 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1328
Gilles Peskine8c9def32018-02-08 10:02:12 +01001329 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001330 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001331
Gilles Peskine89167cb2018-07-08 20:12:23 +02001332 TEST_ASSERT( psa_mac_verify_setup( &operation,
1333 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001334 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1335 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001336 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02001337 TEST_ASSERT( psa_mac_verify_finish( &operation,
1338 expected_mac->x,
1339 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001340
1341exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001342 psa_destroy_key( key_slot );
1343 mbedtls_psa_crypto_free( );
1344}
1345/* END_CASE */
1346
1347/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001348void cipher_setup( int key_type_arg,
1349 data_t *key,
1350 int alg_arg,
1351 int expected_status_arg )
1352{
1353 int key_slot = 1;
1354 psa_key_type_t key_type = key_type_arg;
1355 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001356 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001357 psa_cipher_operation_t operation;
1358 psa_key_policy_t policy;
1359 psa_status_t status;
1360
1361 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1362
1363 psa_key_policy_init( &policy );
1364 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1365 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1366
1367 TEST_ASSERT( psa_import_key( key_slot, key_type,
1368 key->x, key->len ) == PSA_SUCCESS );
1369
Gilles Peskinefe119512018-07-08 21:39:34 +02001370 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001371 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001372 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001373
1374exit:
1375 psa_destroy_key( key_slot );
1376 mbedtls_psa_crypto_free( );
1377}
1378/* END_CASE */
1379
1380/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001381void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001382 data_t *key,
1383 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001384 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001385{
1386 int key_slot = 1;
1387 psa_status_t status;
1388 psa_key_type_t key_type = key_type_arg;
1389 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001390 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001391 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001392 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001393 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001394 size_t output_buffer_size = 0;
1395 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001396 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001397 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001398 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001399
Gilles Peskine50e586b2018-06-08 14:28:46 +02001400 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001401 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001402 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001403 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1404 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1405 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001406
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001407 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1408 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001409
1410 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1411
Moran Pekered346952018-07-05 15:22:45 +03001412 psa_key_policy_init( &policy );
1413 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1414 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1415
Gilles Peskine50e586b2018-06-08 14:28:46 +02001416 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001417 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001418
Gilles Peskinefe119512018-07-08 21:39:34 +02001419 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1420 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001421
Gilles Peskinefe119512018-07-08 21:39:34 +02001422 TEST_ASSERT( psa_cipher_set_iv( &operation,
1423 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001424 output_buffer_size = (size_t) input->len +
1425 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001426 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001427 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001428
Gilles Peskine4abf7412018-06-18 16:35:34 +02001429 TEST_ASSERT( psa_cipher_update( &operation,
1430 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001431 output, output_buffer_size,
1432 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001433 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001434 status = psa_cipher_finish( &operation,
1435 output + function_output_length,
1436 output_buffer_size,
1437 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001438 total_output_length += function_output_length;
1439
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001440 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001441 if( expected_status == PSA_SUCCESS )
1442 {
1443 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001444 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001445 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001446 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001447 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001448
Gilles Peskine50e586b2018-06-08 14:28:46 +02001449exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001450 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001451 psa_destroy_key( key_slot );
1452 mbedtls_psa_crypto_free( );
1453}
1454/* END_CASE */
1455
1456/* BEGIN_CASE */
1457void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001458 data_t *key,
1459 data_t *input,
1460 int first_part_size,
1461 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001462{
1463 int key_slot = 1;
1464 psa_key_type_t key_type = key_type_arg;
1465 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001466 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001467 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001468 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001469 size_t output_buffer_size = 0;
1470 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001471 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001472 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001473 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001474
Gilles Peskine50e586b2018-06-08 14:28:46 +02001475 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001476 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001477 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001478 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1479 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1480 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001481
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001482 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1483 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001484
1485 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1486
Moran Pekered346952018-07-05 15:22:45 +03001487 psa_key_policy_init( &policy );
1488 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1489 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1490
Gilles Peskine50e586b2018-06-08 14:28:46 +02001491 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001492 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001493
Gilles Peskinefe119512018-07-08 21:39:34 +02001494 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1495 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001496
Gilles Peskinefe119512018-07-08 21:39:34 +02001497 TEST_ASSERT( psa_cipher_set_iv( &operation,
1498 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001499 output_buffer_size = (size_t) input->len +
1500 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001501 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001502 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001503
Gilles Peskine4abf7412018-06-18 16:35:34 +02001504 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001505 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001506 output, output_buffer_size,
1507 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001508 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001509 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001510 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001511 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001512 output, output_buffer_size,
1513 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001514 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001515 TEST_ASSERT( psa_cipher_finish( &operation,
1516 output + function_output_length,
1517 output_buffer_size,
1518 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001519 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001520 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1521
Gilles Peskine4abf7412018-06-18 16:35:34 +02001522 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001523 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001524 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001525
1526exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001527 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001528 psa_destroy_key( key_slot );
1529 mbedtls_psa_crypto_free( );
1530}
1531/* END_CASE */
1532
1533/* BEGIN_CASE */
1534void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001535 data_t *key,
1536 data_t *input,
1537 int first_part_size,
1538 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001539{
1540 int key_slot = 1;
1541
1542 psa_key_type_t key_type = key_type_arg;
1543 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001544 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001545 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001546 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001547 size_t output_buffer_size = 0;
1548 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001549 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001550 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001551 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001552
Gilles Peskine50e586b2018-06-08 14:28:46 +02001553 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001554 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001555 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001556 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1557 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1558 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001559
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001560 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1561 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001562
1563 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1564
Moran Pekered346952018-07-05 15:22:45 +03001565 psa_key_policy_init( &policy );
1566 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1567 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1568
Gilles Peskine50e586b2018-06-08 14:28:46 +02001569 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001570 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001571
Gilles Peskinefe119512018-07-08 21:39:34 +02001572 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1573 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001574
Gilles Peskinefe119512018-07-08 21:39:34 +02001575 TEST_ASSERT( psa_cipher_set_iv( &operation,
1576 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001577
mohammad16033d91abe2018-07-03 13:15:54 +03001578 output_buffer_size = (size_t) input->len +
1579 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001580 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001581 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001582
Gilles Peskine4abf7412018-06-18 16:35:34 +02001583 TEST_ASSERT( (unsigned int) first_part_size < input->len );
1584 TEST_ASSERT( psa_cipher_update( &operation,
1585 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001586 output, output_buffer_size,
1587 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001588 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001589 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001590 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001591 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001592 output, output_buffer_size,
1593 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001594 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001595 TEST_ASSERT( psa_cipher_finish( &operation,
1596 output + function_output_length,
1597 output_buffer_size,
1598 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001599 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001600 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1601
Gilles Peskine4abf7412018-06-18 16:35:34 +02001602 TEST_ASSERT( total_output_length == expected_output->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001603 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001604 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001605
1606exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001607 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001608 psa_destroy_key( key_slot );
1609 mbedtls_psa_crypto_free( );
1610}
1611/* END_CASE */
1612
Gilles Peskine50e586b2018-06-08 14:28:46 +02001613/* BEGIN_CASE */
1614void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001615 data_t *key,
1616 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001617 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001618{
1619 int key_slot = 1;
1620 psa_status_t status;
1621 psa_key_type_t key_type = key_type_arg;
1622 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001623 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001624 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001625 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001626 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001627 size_t output_buffer_size = 0;
1628 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001629 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001630 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001631 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001632
Gilles Peskine50e586b2018-06-08 14:28:46 +02001633 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001634 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001635 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001636 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1637 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1638 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001639
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001640 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1641 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001642
1643 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1644
Moran Pekered346952018-07-05 15:22:45 +03001645 psa_key_policy_init( &policy );
1646 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1647 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1648
Gilles Peskine50e586b2018-06-08 14:28:46 +02001649 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001650 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001651
Gilles Peskinefe119512018-07-08 21:39:34 +02001652 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1653 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001654
Gilles Peskinefe119512018-07-08 21:39:34 +02001655 TEST_ASSERT( psa_cipher_set_iv( &operation,
1656 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001657
mohammad16033d91abe2018-07-03 13:15:54 +03001658 output_buffer_size = (size_t) input->len +
1659 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001660 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001661 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001662
Gilles Peskine4abf7412018-06-18 16:35:34 +02001663 TEST_ASSERT( psa_cipher_update( &operation,
1664 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001665 output, output_buffer_size,
1666 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001667 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001668 status = psa_cipher_finish( &operation,
1669 output + function_output_length,
1670 output_buffer_size,
1671 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001672 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001673 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001674
1675 if( expected_status == PSA_SUCCESS )
1676 {
1677 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001678 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001679 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001680 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001681 }
1682
Gilles Peskine50e586b2018-06-08 14:28:46 +02001683exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001684 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001685 psa_destroy_key( key_slot );
1686 mbedtls_psa_crypto_free( );
1687}
1688/* END_CASE */
1689
Gilles Peskine50e586b2018-06-08 14:28:46 +02001690/* BEGIN_CASE */
1691void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001692 data_t *key,
1693 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02001694{
1695 int key_slot = 1;
1696 psa_key_type_t key_type = key_type_arg;
1697 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07001698 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02001699 size_t iv_size = 16;
1700 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001701 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001702 size_t output1_size = 0;
1703 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001704 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001705 size_t output2_size = 0;
1706 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001707 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001708 psa_cipher_operation_t operation1;
1709 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03001710 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001711
mohammad1603d7d7ba52018-03-12 18:51:53 +02001712 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001713 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001714 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1715 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001716
mohammad1603d7d7ba52018-03-12 18:51:53 +02001717 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1718
Moran Pekered346952018-07-05 15:22:45 +03001719 psa_key_policy_init( &policy );
1720 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
1721 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1722
mohammad1603d7d7ba52018-03-12 18:51:53 +02001723 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001724 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001725
Gilles Peskinefe119512018-07-08 21:39:34 +02001726 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
1727 key_slot, alg ) == PSA_SUCCESS );
1728 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
1729 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001730
Gilles Peskinefe119512018-07-08 21:39:34 +02001731 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
1732 iv, iv_size,
1733 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001734 output1_size = (size_t) input->len +
1735 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001736 output1 = mbedtls_calloc( 1, output1_size );
1737 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001738
Gilles Peskine4abf7412018-06-18 16:35:34 +02001739 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001740 output1, output1_size,
1741 &output1_length ) == PSA_SUCCESS );
1742 TEST_ASSERT( psa_cipher_finish( &operation1,
1743 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001744 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001745
Gilles Peskine048b7f02018-06-08 14:20:49 +02001746 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001747
1748 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1749
1750 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001751 output2 = mbedtls_calloc( 1, output2_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001752 TEST_ASSERT( output2 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001753
Gilles Peskinefe119512018-07-08 21:39:34 +02001754 TEST_ASSERT( psa_cipher_set_iv( &operation2,
1755 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001756 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
1757 output2, output2_size,
1758 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001759 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001760 TEST_ASSERT( psa_cipher_finish( &operation2,
1761 output2 + output2_length,
1762 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001763 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001764
Gilles Peskine048b7f02018-06-08 14:20:49 +02001765 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001766
Janos Follath25c4fa82018-07-06 16:23:25 +01001767 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001768
Gilles Peskine4abf7412018-06-18 16:35:34 +02001769 TEST_ASSERT( input->len == output2_length );
1770 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +03001771
1772exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001773 mbedtls_free( output1 );
1774 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03001775 psa_destroy_key( key_slot );
1776 mbedtls_psa_crypto_free( );
1777}
1778/* END_CASE */
1779
1780/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001781void cipher_verify_output_multipart( int alg_arg,
1782 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001783 data_t *key,
1784 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001785 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03001786{
1787 int key_slot = 1;
1788 psa_key_type_t key_type = key_type_arg;
1789 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03001790 unsigned char iv[16] = {0};
1791 size_t iv_size = 16;
1792 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001793 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001794 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001795 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001796 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001797 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001798 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001799 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001800 psa_cipher_operation_t operation1;
1801 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03001802 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03001803
Moran Pekerded84402018-06-06 16:36:50 +03001804 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001805 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001806 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1807 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001808
Moran Pekerded84402018-06-06 16:36:50 +03001809 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1810
Moran Pekered346952018-07-05 15:22:45 +03001811 psa_key_policy_init( &policy );
1812 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
1813 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1814
Moran Pekerded84402018-06-06 16:36:50 +03001815 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001816 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001817
Gilles Peskinefe119512018-07-08 21:39:34 +02001818 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
1819 key_slot, alg ) == PSA_SUCCESS );
1820 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
1821 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001822
Gilles Peskinefe119512018-07-08 21:39:34 +02001823 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
1824 iv, iv_size,
1825 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001826 output1_buffer_size = (size_t) input->len +
1827 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001828 output1 = mbedtls_calloc( 1, output1_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001829 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001830
Gilles Peskine4abf7412018-06-18 16:35:34 +02001831 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001832
itayzafrir3e02b3b2018-06-12 17:06:52 +03001833 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001834 output1, output1_buffer_size,
1835 &function_output_length ) == PSA_SUCCESS );
1836 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001837
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001838 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001839 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001840 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001841 output1, output1_buffer_size,
1842 &function_output_length ) == PSA_SUCCESS );
1843 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001844
Gilles Peskine048b7f02018-06-08 14:20:49 +02001845 TEST_ASSERT( psa_cipher_finish( &operation1,
1846 output1 + output1_length,
1847 output1_buffer_size - output1_length,
1848 &function_output_length ) == PSA_SUCCESS );
1849 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001850
1851 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1852
Gilles Peskine048b7f02018-06-08 14:20:49 +02001853 output2_buffer_size = output1_length;
1854 output2 = mbedtls_calloc( 1, output2_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001855 TEST_ASSERT( output2 != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001856
Gilles Peskinefe119512018-07-08 21:39:34 +02001857 TEST_ASSERT( psa_cipher_set_iv( &operation2,
1858 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001859
1860 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001861 output2, output2_buffer_size,
1862 &function_output_length ) == PSA_SUCCESS );
1863 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001864
Gilles Peskine048b7f02018-06-08 14:20:49 +02001865 TEST_ASSERT( psa_cipher_update( &operation2,
1866 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001867 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001868 output2, output2_buffer_size,
1869 &function_output_length ) == PSA_SUCCESS );
1870 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001871
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001872 TEST_ASSERT( psa_cipher_finish( &operation2,
1873 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001874 output2_buffer_size - output2_length,
1875 &function_output_length ) == PSA_SUCCESS );
1876 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001877
Janos Follath25c4fa82018-07-06 16:23:25 +01001878 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001879
Gilles Peskine4abf7412018-06-18 16:35:34 +02001880 TEST_ASSERT( input->len == output2_length );
1881 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001882
1883exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001884 mbedtls_free( output1 );
1885 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001886 psa_destroy_key( key_slot );
1887 mbedtls_psa_crypto_free( );
1888}
1889/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02001890
Gilles Peskine20035e32018-02-03 22:44:14 +01001891/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001892void aead_encrypt_decrypt( int key_type_arg,
1893 data_t * key_data,
1894 int alg_arg,
1895 data_t * input_data,
1896 data_t * nonce,
1897 data_t * additional_data,
1898 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001899{
1900 int slot = 1;
1901 psa_key_type_t key_type = key_type_arg;
1902 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001903 unsigned char *output_data = NULL;
1904 size_t output_size = 0;
1905 size_t output_length = 0;
1906 unsigned char *output_data2 = NULL;
1907 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001908 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001909 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001910 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001911
Gilles Peskinea1cac842018-06-11 19:33:02 +02001912 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001913 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001914 TEST_ASSERT( nonce != NULL );
1915 TEST_ASSERT( additional_data != NULL );
1916 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1917 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1918 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1919 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1920
Gilles Peskine4abf7412018-06-18 16:35:34 +02001921 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001922 output_data = mbedtls_calloc( 1, output_size );
1923 TEST_ASSERT( output_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001924
1925 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1926
1927 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001928 psa_key_policy_set_usage( &policy,
1929 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
1930 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001931 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1932
1933 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001934 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001935
1936 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001937 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001938 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001939 additional_data->len,
1940 input_data->x, input_data->len,
1941 output_data, output_size,
1942 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001943
1944 if( PSA_SUCCESS == expected_result )
1945 {
1946 output_data2 = mbedtls_calloc( 1, output_length );
1947 TEST_ASSERT( output_data2 != NULL );
1948
1949 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001950 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001951 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001952 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001953 output_data, output_length,
1954 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001955 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02001956
itayzafrir3e02b3b2018-06-12 17:06:52 +03001957 TEST_ASSERT( memcmp( input_data->x, output_data2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001958 input_data->len ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001959 }
Gilles Peskine2d277862018-06-18 15:41:12 +02001960
Gilles Peskinea1cac842018-06-11 19:33:02 +02001961exit:
1962 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001963 mbedtls_free( output_data );
1964 mbedtls_free( output_data2 );
1965 mbedtls_psa_crypto_free( );
1966}
1967/* END_CASE */
1968
1969/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001970void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001971 int alg_arg, data_t * input_data,
1972 data_t * additional_data, data_t * nonce,
1973 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001974{
1975 int slot = 1;
1976 psa_key_type_t key_type = key_type_arg;
1977 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001978 unsigned char *output_data = NULL;
1979 size_t output_size = 0;
1980 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001981 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001982 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001983
Gilles Peskinea1cac842018-06-11 19:33:02 +02001984 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001985 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001986 TEST_ASSERT( additional_data != NULL );
1987 TEST_ASSERT( nonce != NULL );
1988 TEST_ASSERT( expected_result != NULL );
1989 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1990 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1991 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1992 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1993 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
1994
Gilles Peskine4abf7412018-06-18 16:35:34 +02001995 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001996 output_data = mbedtls_calloc( 1, output_size );
1997 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001998
Gilles Peskinea1cac842018-06-11 19:33:02 +02001999 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2000
2001 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002002 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002003 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2004
2005 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002006 key_data->x,
2007 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002008
2009 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002010 nonce->x, nonce->len,
2011 additional_data->x, additional_data->len,
2012 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002013 output_data, output_size,
2014 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002015
itayzafrir3e02b3b2018-06-12 17:06:52 +03002016 TEST_ASSERT( memcmp( output_data, expected_result->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02002017 output_length ) == 0 );
2018
Gilles Peskinea1cac842018-06-11 19:33:02 +02002019exit:
2020 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002021 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002022 mbedtls_psa_crypto_free( );
2023}
2024/* END_CASE */
2025
2026/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002027void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002028 int alg_arg, data_t * input_data,
2029 data_t * additional_data, data_t * nonce,
2030 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002031{
2032 int slot = 1;
2033 psa_key_type_t key_type = key_type_arg;
2034 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002035 unsigned char *output_data = NULL;
2036 size_t output_size = 0;
2037 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002038 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002039 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002040 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002041
Gilles Peskinea1cac842018-06-11 19:33:02 +02002042 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002043 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002044 TEST_ASSERT( additional_data != NULL );
2045 TEST_ASSERT( nonce != NULL );
2046 TEST_ASSERT( expected_data != NULL );
2047 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2048 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2049 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2050 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2051 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2052
Gilles Peskine4abf7412018-06-18 16:35:34 +02002053 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002054 output_data = mbedtls_calloc( 1, output_size );
2055 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02002056
Gilles Peskinea1cac842018-06-11 19:33:02 +02002057 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2058
2059 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002060 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002061 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2062
2063 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002064 key_data->x,
2065 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002066
2067 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002068 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002069 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002070 additional_data->len,
2071 input_data->x, input_data->len,
2072 output_data, output_size,
2073 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002074
Gilles Peskine2d277862018-06-18 15:41:12 +02002075 if( expected_result == PSA_SUCCESS )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002076 {
itayzafrir3e02b3b2018-06-12 17:06:52 +03002077 TEST_ASSERT( memcmp( output_data, expected_data->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02002078 output_length ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002079 }
2080
Gilles Peskinea1cac842018-06-11 19:33:02 +02002081exit:
2082 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002083 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002084 mbedtls_psa_crypto_free( );
2085}
2086/* END_CASE */
2087
2088/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002089void signature_size( int type_arg,
2090 int bits,
2091 int alg_arg,
2092 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002093{
2094 psa_key_type_t type = type_arg;
2095 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002096 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002097 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2098exit:
2099 ;
2100}
2101/* END_CASE */
2102
2103/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002104void sign_deterministic( int key_type_arg, data_t *key_data,
2105 int alg_arg, data_t *input_data,
2106 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002107{
2108 int slot = 1;
2109 psa_key_type_t key_type = key_type_arg;
2110 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002111 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002112 unsigned char *signature = NULL;
2113 size_t signature_size;
2114 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002115 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002116
Gilles Peskine20035e32018-02-03 22:44:14 +01002117 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002118 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002119 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002120 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2121 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2122 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002123
2124 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2125
mohammad1603a97cb8c2018-03-28 03:46:26 -07002126 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002127 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002128 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2129
Gilles Peskine20035e32018-02-03 22:44:14 +01002130 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002131 key_data->x,
2132 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002133 TEST_ASSERT( psa_get_key_information( slot,
2134 NULL,
2135 &key_bits ) == PSA_SUCCESS );
2136
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002137 /* Allocate a buffer which has the size advertized by the
2138 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002139 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2140 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002141 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002142 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine20035e32018-02-03 22:44:14 +01002143 signature = mbedtls_calloc( 1, signature_size );
2144 TEST_ASSERT( signature != NULL );
2145
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002146 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002147 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002148 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002149 signature, signature_size,
2150 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002151 /* Verify that the signature is what is expected. */
Gilles Peskine4abf7412018-06-18 16:35:34 +02002152 TEST_ASSERT( signature_length == output_data->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02002153 TEST_ASSERT( memcmp( signature, output_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002154 output_data->len ) == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01002155
2156exit:
2157 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002158 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002159 mbedtls_psa_crypto_free( );
2160}
2161/* END_CASE */
2162
2163/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002164void sign_fail( int key_type_arg, data_t *key_data,
2165 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002166 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002167{
2168 int slot = 1;
2169 psa_key_type_t key_type = key_type_arg;
2170 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002171 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002172 psa_status_t actual_status;
2173 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002174 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002175 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002176 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002177
Gilles Peskine20035e32018-02-03 22:44:14 +01002178 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002179 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002180 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2181 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2182
Gilles Peskine20035e32018-02-03 22:44:14 +01002183 signature = mbedtls_calloc( 1, signature_size );
2184 TEST_ASSERT( signature != NULL );
2185
2186 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2187
mohammad1603a97cb8c2018-03-28 03:46:26 -07002188 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002189 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002190 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2191
Gilles Peskine20035e32018-02-03 22:44:14 +01002192 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002193 key_data->x,
2194 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002195
2196 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002197 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002198 signature, signature_size,
2199 &signature_length );
2200 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002201 /* The value of *signature_length is unspecified on error, but
2202 * whatever it is, it should be less than signature_size, so that
2203 * if the caller tries to read *signature_length bytes without
2204 * checking the error code then they don't overflow a buffer. */
2205 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002206
2207exit:
2208 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002209 mbedtls_free( signature );
2210 mbedtls_psa_crypto_free( );
2211}
2212/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002213
2214/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002215void sign_verify( int key_type_arg, data_t *key_data,
2216 int alg_arg, data_t *input_data )
2217{
2218 int slot = 1;
2219 psa_key_type_t key_type = key_type_arg;
2220 psa_algorithm_t alg = alg_arg;
2221 size_t key_bits;
2222 unsigned char *signature = NULL;
2223 size_t signature_size;
2224 size_t signature_length = 0xdeadbeef;
2225 psa_key_policy_t policy;
2226
2227 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2228
2229 psa_key_policy_init( &policy );
2230 psa_key_policy_set_usage( &policy,
2231 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2232 alg );
2233 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2234
2235 TEST_ASSERT( psa_import_key( slot, key_type,
2236 key_data->x,
2237 key_data->len ) == PSA_SUCCESS );
2238 TEST_ASSERT( psa_get_key_information( slot,
2239 NULL,
2240 &key_bits ) == PSA_SUCCESS );
2241
2242 /* Allocate a buffer which has the size advertized by the
2243 * library. */
2244 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2245 key_bits, alg );
2246 TEST_ASSERT( signature_size != 0 );
2247 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2248 signature = mbedtls_calloc( 1, signature_size );
2249 TEST_ASSERT( signature != NULL );
2250
2251 /* Perform the signature. */
2252 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2253 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002254 signature, signature_size,
2255 &signature_length ) == PSA_SUCCESS );
2256 /* Check that the signature length looks sensible. */
2257 TEST_ASSERT( signature_length <= signature_size );
2258 TEST_ASSERT( signature_length > 0 );
2259
2260 /* Use the library to verify that the signature is correct. */
2261 TEST_ASSERT( psa_asymmetric_verify(
2262 slot, alg,
2263 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002264 signature, signature_length ) == PSA_SUCCESS );
2265
2266 if( input_data->len != 0 )
2267 {
2268 /* Flip a bit in the input and verify that the signature is now
2269 * detected as invalid. Flip a bit at the beginning, not at the end,
2270 * because ECDSA may ignore the last few bits of the input. */
2271 input_data->x[0] ^= 1;
2272 TEST_ASSERT( psa_asymmetric_verify(
2273 slot, alg,
2274 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002275 signature,
2276 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2277 }
2278
2279exit:
2280 psa_destroy_key( slot );
2281 mbedtls_free( signature );
2282 mbedtls_psa_crypto_free( );
2283}
2284/* END_CASE */
2285
2286/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002287void asymmetric_verify( int key_type_arg, data_t *key_data,
2288 int alg_arg, data_t *hash_data,
2289 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002290{
2291 int slot = 1;
2292 psa_key_type_t key_type = key_type_arg;
2293 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002294 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002295
Gilles Peskine69c12672018-06-28 00:07:19 +02002296 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2297
itayzafrir5c753392018-05-08 11:18:38 +03002298 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002299 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002300 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002301 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2302 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2303 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002304
2305 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2306
2307 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002308 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002309 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2310
2311 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002312 key_data->x,
2313 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002314
2315 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002316 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002317 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002318 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002319exit:
2320 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002321 mbedtls_psa_crypto_free( );
2322}
2323/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002324
2325/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002326void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2327 int alg_arg, data_t *hash_data,
2328 data_t *signature_data,
2329 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002330{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002331 int slot = 1;
2332 psa_key_type_t key_type = key_type_arg;
2333 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002334 psa_status_t actual_status;
2335 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002336 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002337
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002338 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002339 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002340 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002341 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2342 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2343 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002344
2345 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2346
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002347 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002348 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002349 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2350
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002351 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002352 key_data->x,
2353 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002354
2355 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002356 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002357 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002358 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002359
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002360 TEST_ASSERT( actual_status == expected_status );
2361
2362exit:
2363 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002364 mbedtls_psa_crypto_free( );
2365}
2366/* END_CASE */
2367
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002368/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002369void asymmetric_encrypt( int key_type_arg,
2370 data_t *key_data,
2371 int alg_arg,
2372 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02002373 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02002374 int expected_output_length_arg,
2375 int expected_status_arg )
2376{
2377 int slot = 1;
2378 psa_key_type_t key_type = key_type_arg;
2379 psa_algorithm_t alg = alg_arg;
2380 size_t expected_output_length = expected_output_length_arg;
2381 size_t key_bits;
2382 unsigned char *output = NULL;
2383 size_t output_size;
2384 size_t output_length = ~0;
2385 psa_status_t actual_status;
2386 psa_status_t expected_status = expected_status_arg;
2387 psa_key_policy_t policy;
2388
2389 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2390
2391 /* Import the key */
2392 psa_key_policy_init( &policy );
2393 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2394 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2395 TEST_ASSERT( psa_import_key( slot, key_type,
2396 key_data->x,
2397 key_data->len ) == PSA_SUCCESS );
2398
2399 /* Determine the maximum output length */
2400 TEST_ASSERT( psa_get_key_information( slot,
2401 NULL,
2402 &key_bits ) == PSA_SUCCESS );
2403 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
2404 output = mbedtls_calloc( 1, output_size );
2405 TEST_ASSERT( output != NULL );
2406
2407 /* Encrypt the input */
2408 actual_status = psa_asymmetric_encrypt( slot, alg,
2409 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002410 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02002411 output, output_size,
2412 &output_length );
2413 TEST_ASSERT( actual_status == expected_status );
2414 TEST_ASSERT( output_length == expected_output_length );
2415
Gilles Peskine68428122018-06-30 18:42:41 +02002416 /* If the label is empty, the test framework puts a non-null pointer
2417 * in label->x. Test that a null pointer works as well. */
2418 if( label->len == 0 )
2419 {
2420 output_length = ~0;
2421 memset( output, 0, output_size );
2422 actual_status = psa_asymmetric_encrypt( slot, alg,
2423 input_data->x, input_data->len,
2424 NULL, label->len,
2425 output, output_size,
2426 &output_length );
2427 TEST_ASSERT( actual_status == expected_status );
2428 TEST_ASSERT( output_length == expected_output_length );
2429 }
2430
Gilles Peskine656896e2018-06-29 19:12:28 +02002431exit:
2432 psa_destroy_key( slot );
2433 mbedtls_free( output );
2434 mbedtls_psa_crypto_free( );
2435}
2436/* END_CASE */
2437
2438/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002439void asymmetric_encrypt_decrypt( int key_type_arg,
2440 data_t *key_data,
2441 int alg_arg,
2442 data_t *input_data,
2443 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002444{
2445 int slot = 1;
2446 psa_key_type_t key_type = key_type_arg;
2447 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002448 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002449 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002450 size_t output_size;
2451 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002452 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002453 size_t output2_size;
2454 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002455 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002456
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002457 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002458 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002459 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2460 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2461
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002462 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2463
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002464 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002465 psa_key_policy_set_usage( &policy,
2466 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002467 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002468 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2469
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002470 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002471 key_data->x,
2472 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002473
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002474
2475 /* Determine the maximum ciphertext length */
2476 TEST_ASSERT( psa_get_key_information( slot,
2477 NULL,
2478 &key_bits ) == PSA_SUCCESS );
2479 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
2480 output = mbedtls_calloc( 1, output_size );
2481 TEST_ASSERT( output != NULL );
2482 output2_size = input_data->len;
2483 output2 = mbedtls_calloc( 1, output2_size );
2484 TEST_ASSERT( output2 != NULL );
2485
Gilles Peskineeebd7382018-06-08 18:11:54 +02002486 /* We test encryption by checking that encrypt-then-decrypt gives back
2487 * the original plaintext because of the non-optional random
2488 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02002489 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002490 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002491 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002492 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002493 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002494 /* We don't know what ciphertext length to expect, but check that
2495 * it looks sensible. */
2496 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002497
Gilles Peskine2d277862018-06-18 15:41:12 +02002498 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002499 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02002500 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002501 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002502 &output2_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002503 TEST_ASSERT( output2_length == input_data->len );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002504 TEST_ASSERT( memcmp( input_data->x, output2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002505 input_data->len ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002506
2507exit:
2508 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002509 mbedtls_free( output );
2510 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002511 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002512}
2513/* END_CASE */
2514
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002515/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002516void asymmetric_decrypt( int key_type_arg,
2517 data_t *key_data,
2518 int alg_arg,
2519 data_t *input_data,
2520 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02002521 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002522{
2523 int slot = 1;
2524 psa_key_type_t key_type = key_type_arg;
2525 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002526 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002527 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002528 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002529 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002530
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002531 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002532 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002533 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002534 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2535 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2536 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2537
Gilles Peskine4abf7412018-06-18 16:35:34 +02002538 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002539 output = mbedtls_calloc( 1, output_size );
2540 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002541
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002542 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2543
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002544 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002545 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002546 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2547
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002548 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002549 key_data->x,
2550 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002551
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002552 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002553 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002554 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002555 output,
2556 output_size,
2557 &output_length ) == PSA_SUCCESS );
Gilles Peskine66763a02018-06-29 21:54:10 +02002558 TEST_ASSERT( expected_data->len == output_length );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002559 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002560
Gilles Peskine68428122018-06-30 18:42:41 +02002561 /* If the label is empty, the test framework puts a non-null pointer
2562 * in label->x. Test that a null pointer works as well. */
2563 if( label->len == 0 )
2564 {
2565 output_length = ~0;
2566 memset( output, 0, output_size );
2567 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
2568 input_data->x, input_data->len,
2569 NULL, label->len,
2570 output,
2571 output_size,
2572 &output_length ) == PSA_SUCCESS );
2573 TEST_ASSERT( expected_data->len == output_length );
2574 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
2575 }
2576
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002577exit:
2578 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002579 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002580 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002581}
2582/* END_CASE */
2583
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002584/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002585void asymmetric_decrypt_fail( int key_type_arg,
2586 data_t *key_data,
2587 int alg_arg,
2588 data_t *input_data,
2589 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02002590 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002591{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002592 int slot = 1;
2593 psa_key_type_t key_type = key_type_arg;
2594 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002595 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002596 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002597 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002598 psa_status_t actual_status;
2599 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002600 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002601
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002602 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002603 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002604 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2605 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2606
Gilles Peskine4abf7412018-06-18 16:35:34 +02002607 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002608 output = mbedtls_calloc( 1, output_size );
2609 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002610
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002611 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2612
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002613 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002614 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002615 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2616
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002617 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002618 key_data->x,
2619 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002620
Gilles Peskine2d277862018-06-18 15:41:12 +02002621 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002622 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002623 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002624 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002625 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002626 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002627 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002628
Gilles Peskine68428122018-06-30 18:42:41 +02002629 /* If the label is empty, the test framework puts a non-null pointer
2630 * in label->x. Test that a null pointer works as well. */
2631 if( label->len == 0 )
2632 {
2633 output_length = ~0;
2634 memset( output, 0, output_size );
2635 actual_status = psa_asymmetric_decrypt( slot, alg,
2636 input_data->x, input_data->len,
2637 NULL, label->len,
2638 output, output_size,
2639 &output_length );
2640 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002641 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02002642 }
2643
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002644exit:
2645 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02002646 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002647 mbedtls_psa_crypto_free( );
2648}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002649/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02002650
2651/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002652void derive_setup( int key_type_arg,
2653 data_t *key_data,
2654 int alg_arg,
2655 data_t *salt,
2656 data_t *label,
2657 int requested_capacity_arg,
2658 int expected_status_arg )
2659{
2660 psa_key_slot_t slot = 1;
2661 size_t key_type = key_type_arg;
2662 psa_algorithm_t alg = alg_arg;
2663 size_t requested_capacity = requested_capacity_arg;
2664 psa_status_t expected_status = expected_status_arg;
2665 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2666 psa_key_policy_t policy;
2667
2668 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2669
2670 psa_key_policy_init( &policy );
2671 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2672 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2673
2674 TEST_ASSERT( psa_import_key( slot, key_type,
2675 key_data->x,
2676 key_data->len ) == PSA_SUCCESS );
2677
2678 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
2679 salt->x, salt->len,
2680 label->x, label->len,
2681 requested_capacity ) == expected_status );
2682
2683exit:
2684 psa_generator_abort( &generator );
2685 psa_destroy_key( slot );
2686 mbedtls_psa_crypto_free( );
2687}
2688/* END_CASE */
2689
2690/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02002691void derive_output( int alg_arg,
2692 data_t *key_data,
2693 data_t *salt,
2694 data_t *label,
2695 int requested_capacity_arg,
2696 data_t *expected_output1,
2697 data_t *expected_output2 )
2698{
2699 psa_key_slot_t slot = 1;
2700 psa_algorithm_t alg = alg_arg;
2701 size_t requested_capacity = requested_capacity_arg;
2702 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2703 uint8_t *expected_outputs[2] =
2704 {expected_output1->x, expected_output2->x};
2705 size_t output_sizes[2] =
2706 {expected_output1->len, expected_output2->len};
2707 size_t output_buffer_size = 0;
2708 uint8_t *output_buffer = NULL;
2709 size_t expected_capacity;
2710 size_t current_capacity;
2711 psa_key_policy_t policy;
2712 psa_status_t status;
2713 unsigned i;
2714
2715 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
2716 {
2717 if( output_sizes[i] > output_buffer_size )
2718 output_buffer_size = output_sizes[i];
2719 if( output_sizes[i] == 0 )
2720 expected_outputs[i] = NULL;
2721 }
2722 output_buffer = mbedtls_calloc( 1, output_buffer_size );
2723 TEST_ASSERT( output_buffer != NULL );
2724 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2725
2726 psa_key_policy_init( &policy );
2727 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2728 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2729
2730 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
2731 key_data->x,
2732 key_data->len ) == PSA_SUCCESS );
2733
2734 /* Extraction phase. */
2735 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
2736 salt->x, salt->len,
2737 label->x, label->len,
2738 requested_capacity ) == PSA_SUCCESS );
2739 TEST_ASSERT( psa_get_generator_capacity( &generator,
2740 &current_capacity ) ==
2741 PSA_SUCCESS );
2742 TEST_ASSERT( current_capacity == requested_capacity );
2743 expected_capacity = requested_capacity;
2744
2745 /* Expansion phase. */
2746 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
2747 {
2748 /* Read some bytes. */
2749 status = psa_generator_read( &generator,
2750 output_buffer, output_sizes[i] );
2751 if( expected_capacity == 0 && output_sizes[i] == 0 )
2752 {
2753 /* Reading 0 bytes when 0 bytes are available can go either way. */
2754 TEST_ASSERT( status == PSA_SUCCESS ||
2755 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
2756 continue;
2757 }
2758 else if( expected_capacity == 0 ||
2759 output_sizes[i] > expected_capacity )
2760 {
2761 /* Capacity exceeded. */
2762 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
2763 expected_capacity = 0;
2764 continue;
2765 }
2766 /* Success. Check the read data. */
2767 TEST_ASSERT( status == PSA_SUCCESS );
2768 if( output_sizes[i] != 0 )
2769 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
2770 output_sizes[i] ) == 0 );
2771 /* Check the generator status. */
2772 expected_capacity -= output_sizes[i];
2773 TEST_ASSERT( psa_get_generator_capacity( &generator,
2774 &current_capacity ) ==
2775 PSA_SUCCESS );
2776 TEST_ASSERT( expected_capacity == current_capacity );
2777 }
2778 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
2779
2780exit:
2781 mbedtls_free( output_buffer );
2782 psa_generator_abort( &generator );
2783 psa_destroy_key( slot );
2784 mbedtls_psa_crypto_free( );
2785}
2786/* END_CASE */
2787
2788/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02002789void derive_full( int alg_arg,
2790 data_t *key_data,
2791 data_t *salt,
2792 data_t *label,
2793 int requested_capacity_arg )
2794{
2795 psa_key_slot_t slot = 1;
2796 psa_algorithm_t alg = alg_arg;
2797 size_t requested_capacity = requested_capacity_arg;
2798 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2799 unsigned char output_buffer[16];
2800 size_t expected_capacity = requested_capacity;
2801 size_t current_capacity;
2802 psa_key_policy_t policy;
2803
2804 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2805
2806 psa_key_policy_init( &policy );
2807 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2808 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2809
2810 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
2811 key_data->x,
2812 key_data->len ) == PSA_SUCCESS );
2813
2814 /* Extraction phase. */
2815 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
2816 salt->x, salt->len,
2817 label->x, label->len,
2818 requested_capacity ) == PSA_SUCCESS );
2819 TEST_ASSERT( psa_get_generator_capacity( &generator,
2820 &current_capacity ) ==
2821 PSA_SUCCESS );
2822 TEST_ASSERT( current_capacity == expected_capacity );
2823
2824 /* Expansion phase. */
2825 while( current_capacity > 0 )
2826 {
2827 size_t read_size = sizeof( output_buffer );
2828 if( read_size > current_capacity )
2829 read_size = current_capacity;
2830 TEST_ASSERT( psa_generator_read( &generator,
2831 output_buffer,
2832 read_size ) == PSA_SUCCESS );
2833 expected_capacity -= read_size;
2834 TEST_ASSERT( psa_get_generator_capacity( &generator,
2835 &current_capacity ) ==
2836 PSA_SUCCESS );
2837 TEST_ASSERT( current_capacity == expected_capacity );
2838 }
2839
2840 /* Check that the generator refuses to go over capacity. */
2841 TEST_ASSERT( psa_generator_read( &generator,
2842 output_buffer,
2843 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
2844
2845 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
2846
2847exit:
2848 psa_generator_abort( &generator );
2849 psa_destroy_key( slot );
2850 mbedtls_psa_crypto_free( );
2851}
2852/* END_CASE */
2853
2854/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02002855void derive_key_exercise( int alg_arg,
2856 data_t *key_data,
2857 data_t *salt,
2858 data_t *label,
2859 int derived_type_arg,
2860 int derived_bits_arg,
2861 int derived_usage_arg,
2862 int derived_alg_arg )
2863{
2864 psa_key_slot_t base_key = 1;
2865 psa_key_slot_t derived_key = 2;
2866 psa_algorithm_t alg = alg_arg;
2867 psa_key_type_t derived_type = derived_type_arg;
2868 size_t derived_bits = derived_bits_arg;
2869 psa_key_usage_t derived_usage = derived_usage_arg;
2870 psa_algorithm_t derived_alg = derived_alg_arg;
2871 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
2872 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2873 psa_key_policy_t policy;
2874 psa_key_type_t got_type;
2875 size_t got_bits;
2876
2877 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2878
2879 psa_key_policy_init( &policy );
2880 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2881 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
2882 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
2883 key_data->x,
2884 key_data->len ) == PSA_SUCCESS );
2885
2886 /* Derive a key. */
2887 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
2888 salt->x, salt->len,
2889 label->x, label->len,
2890 capacity ) == PSA_SUCCESS );
2891 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
2892 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
2893 TEST_ASSERT( psa_generator_import_key( derived_key,
2894 derived_type,
2895 derived_bits,
2896 &generator ) == PSA_SUCCESS );
2897
2898 /* Test the key information */
2899 TEST_ASSERT( psa_get_key_information( derived_key,
2900 &got_type,
2901 &got_bits ) == PSA_SUCCESS );
2902 TEST_ASSERT( got_type == derived_type );
2903 TEST_ASSERT( got_bits == derived_bits );
2904
2905 /* Exercise the derived key. */
2906 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
2907 goto exit;
2908
2909exit:
2910 psa_generator_abort( &generator );
2911 psa_destroy_key( base_key );
2912 psa_destroy_key( derived_key );
2913 mbedtls_psa_crypto_free( );
2914}
2915/* END_CASE */
2916
2917/* BEGIN_CASE */
2918void derive_key_export( int alg_arg,
2919 data_t *key_data,
2920 data_t *salt,
2921 data_t *label,
2922 int bytes1_arg,
2923 int bytes2_arg )
2924{
2925 psa_key_slot_t base_key = 1;
2926 psa_key_slot_t derived_key = 2;
2927 psa_algorithm_t alg = alg_arg;
2928 size_t bytes1 = bytes1_arg;
2929 size_t bytes2 = bytes2_arg;
2930 size_t capacity = bytes1 + bytes2;
2931 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2932 uint8_t *output_buffer = mbedtls_calloc( 1, capacity );
2933 uint8_t *export_buffer = mbedtls_calloc( 1, capacity );
2934 psa_key_policy_t policy;
2935 size_t length;
2936
2937 TEST_ASSERT( output_buffer != NULL );
2938 TEST_ASSERT( export_buffer != NULL );
2939 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2940
2941 psa_key_policy_init( &policy );
2942 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2943 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
2944 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
2945 key_data->x,
2946 key_data->len ) == PSA_SUCCESS );
2947
2948 /* Derive some material and output it. */
2949 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
2950 salt->x, salt->len,
2951 label->x, label->len,
2952 capacity ) == PSA_SUCCESS );
2953 TEST_ASSERT( psa_generator_read( &generator,
2954 output_buffer,
2955 capacity ) == PSA_SUCCESS );
2956 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
2957
2958 /* Derive the same output again, but this time store it in key objects. */
2959 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
2960 salt->x, salt->len,
2961 label->x, label->len,
2962 capacity ) == PSA_SUCCESS );
2963 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
2964 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
2965 TEST_ASSERT( psa_generator_import_key( derived_key,
2966 PSA_KEY_TYPE_RAW_DATA,
2967 PSA_BYTES_TO_BITS( bytes1 ),
2968 &generator ) == PSA_SUCCESS );
2969 TEST_ASSERT( psa_export_key( derived_key,
2970 export_buffer, bytes1,
2971 &length ) == PSA_SUCCESS );
2972 TEST_ASSERT( length == bytes1 );
2973 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
2974 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
2975 TEST_ASSERT( psa_generator_import_key( derived_key,
2976 PSA_KEY_TYPE_RAW_DATA,
2977 PSA_BYTES_TO_BITS( bytes2 ),
2978 &generator ) == PSA_SUCCESS );
2979 TEST_ASSERT( psa_export_key( derived_key,
2980 export_buffer + bytes1, bytes2,
2981 &length ) == PSA_SUCCESS );
2982 TEST_ASSERT( length == bytes2 );
2983
2984 /* Compare the outputs from the two runs. */
2985 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
2986
2987exit:
2988 mbedtls_free( output_buffer );
2989 mbedtls_free( export_buffer );
2990 psa_generator_abort( &generator );
2991 psa_destroy_key( base_key );
2992 psa_destroy_key( derived_key );
2993 mbedtls_psa_crypto_free( );
2994}
2995/* END_CASE */
2996
2997/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02002998void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02002999{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003000 size_t bytes = bytes_arg;
3001 const unsigned char trail[] = "don't overwrite me";
3002 unsigned char *output = mbedtls_calloc( 1, bytes + sizeof( trail ) );
3003 unsigned char *changed = mbedtls_calloc( 1, bytes );
3004 size_t i;
3005 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003006
Gilles Peskinea50d7392018-06-21 10:22:13 +02003007 TEST_ASSERT( output != NULL );
3008 TEST_ASSERT( changed != NULL );
3009 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003010
3011 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3012
Gilles Peskinea50d7392018-06-21 10:22:13 +02003013 /* Run several times, to ensure that every output byte will be
3014 * nonzero at least once with overwhelming probability
3015 * (2^(-8*number_of_runs)). */
3016 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003017 {
Gilles Peskinea50d7392018-06-21 10:22:13 +02003018 memset( output, 0, bytes );
3019 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
3020
3021 /* Check that no more than bytes have been overwritten */
3022 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
3023
3024 for( i = 0; i < bytes; i++ )
3025 {
3026 if( output[i] != 0 )
3027 ++changed[i];
3028 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003029 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003030
3031 /* Check that every byte was changed to nonzero at least once. This
3032 * validates that psa_generate_random is overwriting every byte of
3033 * the output buffer. */
3034 for( i = 0; i < bytes; i++ )
3035 {
3036 TEST_ASSERT( changed[i] != 0 );
3037 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003038
3039exit:
3040 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003041 mbedtls_free( output );
3042 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003043}
3044/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003045
3046/* BEGIN_CASE */
3047void generate_key( int type_arg,
3048 int bits_arg,
3049 int usage_arg,
3050 int alg_arg,
3051 int expected_status_arg )
3052{
3053 int slot = 1;
3054 psa_key_type_t type = type_arg;
3055 psa_key_usage_t usage = usage_arg;
3056 size_t bits = bits_arg;
3057 psa_algorithm_t alg = alg_arg;
3058 psa_status_t expected_status = expected_status_arg;
3059 psa_key_type_t got_type;
3060 size_t got_bits;
3061 unsigned char exported[616] = {0}; /* enough for a 1024-bit RSA key */
3062 size_t exported_length;
3063 psa_status_t expected_export_status =
3064 usage & PSA_KEY_USAGE_EXPORT ? PSA_SUCCESS : PSA_ERROR_NOT_PERMITTED;
3065 psa_status_t expected_info_status =
3066 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3067 psa_key_policy_t policy;
3068
3069 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3070
3071 psa_key_policy_init( &policy );
3072 psa_key_policy_set_usage( &policy, usage, alg );
3073 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3074
3075 /* Generate a key */
3076 TEST_ASSERT( psa_generate_key( slot, type, bits,
3077 NULL, 0 ) == expected_status );
3078
3079 /* Test the key information */
3080 TEST_ASSERT( psa_get_key_information( slot,
3081 &got_type,
3082 &got_bits ) == expected_info_status );
3083 if( expected_info_status != PSA_SUCCESS )
3084 goto exit;
3085 TEST_ASSERT( got_type == type );
3086 TEST_ASSERT( got_bits == bits );
3087
3088 /* Export the key */
3089 TEST_ASSERT( psa_export_key( slot,
3090 exported, sizeof( exported ),
3091 &exported_length ) == expected_export_status );
3092 if( expected_export_status == PSA_SUCCESS )
3093 {
Gilles Peskine48c0ea12018-06-21 14:15:31 +02003094 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003095 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
3096#if defined(MBEDTLS_DES_C)
3097 if( type == PSA_KEY_TYPE_DES )
3098 {
3099 /* Check the parity bits. */
3100 unsigned i;
3101 for( i = 0; i < bits / 8; i++ )
3102 {
3103 unsigned bit_count = 0;
3104 unsigned m;
3105 for( m = 1; m <= 0x100; m <<= 1 )
3106 {
3107 if( exported[i] & m )
3108 ++bit_count;
3109 }
3110 TEST_ASSERT( bit_count % 2 != 0 );
3111 }
3112 }
3113#endif
3114#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
3115 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
3116 {
3117 /* Sanity check: does this look like the beginning of a PKCS#8
3118 * RSA key pair? Assumes bits is a multiple of 8. */
3119 size_t n_bytes = bits / 8 + 1;
3120 size_t n_encoded_bytes;
3121 unsigned char *n_end;
3122 TEST_ASSERT( exported_length >= 7 + ( n_bytes + 3 ) * 9 / 2 );
3123 TEST_ASSERT( exported[0] == 0x30 );
3124 TEST_ASSERT( exported[1] == 0x82 ); // assumes >=416-bit key
3125 TEST_ASSERT( exported[4] == 0x02 );
3126 TEST_ASSERT( exported[5] == 0x01 );
3127 TEST_ASSERT( exported[6] == 0x00 );
3128 TEST_ASSERT( exported[7] == 0x02 );
3129 n_encoded_bytes = exported[8];
3130 n_end = exported + 9 + n_encoded_bytes;
3131 if( n_encoded_bytes & 0x80 )
3132 {
3133 n_encoded_bytes = ( n_encoded_bytes & 0x7f ) << 7;
3134 n_encoded_bytes |= exported[9] & 0x7f;
3135 n_end += 1;
3136 }
3137 /* The encoding of n should start with a 0 byte since it should
3138 * have its high bit set. However Mbed TLS is not compliant and
3139 * generates an invalid, but widely tolerated, encoding of
3140 * positive INTEGERs with a bit size that is a multiple of 8
3141 * with no leading 0 byte. Accept this here. */
3142 TEST_ASSERT( n_bytes == n_encoded_bytes ||
3143 n_bytes == n_encoded_bytes + 1 );
3144 if( n_bytes == n_encoded_bytes )
3145 TEST_ASSERT( exported[n_encoded_bytes <= 127 ? 9 : 10] == 0x00 );
3146 /* Sanity check: e must be 3 */
3147 TEST_ASSERT( n_end[0] == 0x02 );
3148 TEST_ASSERT( n_end[1] == 0x03 );
3149 TEST_ASSERT( n_end[2] == 0x01 );
3150 TEST_ASSERT( n_end[3] == 0x00 );
3151 TEST_ASSERT( n_end[4] == 0x01 );
3152 TEST_ASSERT( n_end[5] == 0x02 );
3153 }
3154#endif /* MBEDTLS_RSA_C */
3155#if defined(MBEDTLS_ECP_C)
3156 if( PSA_KEY_TYPE_IS_ECC( type ) )
3157 {
3158 /* Sanity check: does this look like the beginning of a PKCS#8
3159 * elliptic curve key pair? */
3160 TEST_ASSERT( exported_length >= bits * 3 / 8 + 10 );
3161 TEST_ASSERT( exported[0] == 0x30 );
3162 }
3163#endif /* MBEDTLS_ECP_C */
3164 }
3165
Gilles Peskine818ca122018-06-20 18:16:48 +02003166 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02003167 if( ! exercise_key( slot, usage, alg ) )
3168 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003169
3170exit:
3171 psa_destroy_key( slot );
3172 mbedtls_psa_crypto_free( );
3173}
3174/* END_CASE */