blob: 65a0365ba688ce8c7fc51c19a95ba8463da51324 [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 Peskineb866e2b2018-06-21 09:25:10 +0200451void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100452{
453 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200454 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100455 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100456
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100457 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300458 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100459 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
460
Gilles Peskine4abf7412018-06-18 16:35:34 +0200461 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200462 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100463 if( status == PSA_SUCCESS )
464 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
465
466exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100467 mbedtls_psa_crypto_free( );
468}
469/* END_CASE */
470
471/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200472void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
473{
474 int slot = 1;
475 size_t bits = bits_arg;
476 psa_status_t expected_status = expected_status_arg;
477 psa_status_t status;
478 psa_key_type_t type =
479 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
480 size_t buffer_size = /* Slight overapproximations */
481 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
482 unsigned char *buffer = mbedtls_calloc( 1, buffer_size );
483 unsigned char *p;
484 int ret;
485 size_t length;
486
487 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
488 TEST_ASSERT( buffer != NULL );
489
490 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
491 bits, keypair ) ) >= 0 );
492 length = ret;
493
494 /* Try importing the key */
495 status = psa_import_key( slot, type, p, length );
496 TEST_ASSERT( status == expected_status );
497 if( status == PSA_SUCCESS )
498 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
499
500exit:
501 mbedtls_free( buffer );
502 mbedtls_psa_crypto_free( );
503}
504/* END_CASE */
505
506/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300507void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300508 int type_arg,
509 int alg_arg,
510 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100511 int expected_bits,
512 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200513 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100514 int canonical_input )
515{
516 int slot = 1;
517 int slot2 = slot + 1;
518 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200519 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200520 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100521 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100522 unsigned char *exported = NULL;
523 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100524 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100525 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100526 size_t reexported_length;
527 psa_key_type_t got_type;
528 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200529 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100530
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100531 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300532 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +0300533 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100534 exported = mbedtls_calloc( 1, export_size );
Darryl Green9c862252018-07-24 12:52:44 +0100535 TEST_ASSERT( export_size == 0 || exported != NULL );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100536 if( ! canonical_input )
537 {
538 reexported = mbedtls_calloc( 1, export_size );
Darryl Green9c862252018-07-24 12:52:44 +0100539 TEST_ASSERT( export_size == 0 || reexported != NULL );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100540 }
541 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
542
mohammad1603a97cb8c2018-03-28 03:46:26 -0700543 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200544 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700545 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
546
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100547 /* Import the key */
548 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200549 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100550
551 /* Test the key information */
552 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200553 &got_type,
554 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100555 TEST_ASSERT( got_type == type );
556 TEST_ASSERT( got_bits == (size_t) expected_bits );
557
558 /* Export the key */
559 status = psa_export_key( slot,
560 exported, export_size,
561 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200562 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100563
564 /* The exported length must be set by psa_export_key() to a value between 0
565 * and export_size. On errors, the exported length must be 0. */
566 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
567 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
568 TEST_ASSERT( exported_length <= export_size );
569
Gilles Peskine3f669c32018-06-21 09:21:51 +0200570 TEST_ASSERT( mem_is_zero( exported + exported_length,
571 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100572 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200573 {
574 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100575 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200576 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100577
578 if( canonical_input )
579 {
Gilles Peskine4abf7412018-06-18 16:35:34 +0200580 TEST_ASSERT( exported_length == data->len );
581 TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100582 }
583 else
584 {
mohammad1603a97cb8c2018-03-28 03:46:26 -0700585 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
586
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100587 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200588 exported,
589 export_size ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100590 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200591 reexported,
592 export_size,
593 &reexported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100594 TEST_ASSERT( reexported_length == exported_length );
595 TEST_ASSERT( memcmp( reexported, exported,
596 exported_length ) == 0 );
597 }
598
599destroy:
600 /* Destroy the key */
601 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
602 TEST_ASSERT( psa_get_key_information(
603 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
604
605exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300606 mbedtls_free( exported );
607 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100608 mbedtls_psa_crypto_free( );
609}
610/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100611
Moran Pekerf709f4a2018-06-06 17:26:04 +0300612/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300613void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200614 int type_arg,
615 int alg_arg,
616 int expected_bits,
617 int public_key_expected_length,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200618 int expected_export_status_arg )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300619{
620 int slot = 1;
621 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200622 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200623 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300624 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300625 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300626 size_t export_size;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100627 size_t exported_length = INVALID_EXPORT_LENGTH;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300628 psa_key_type_t got_type;
629 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200630 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300631
Moran Pekerf709f4a2018-06-06 17:26:04 +0300632 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300633 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +0300634 export_size = (ptrdiff_t) data->len;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300635 exported = mbedtls_calloc( 1, export_size );
636 TEST_ASSERT( exported != NULL );
637
638 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
639
640 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200641 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300642 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
643
644 /* Import the key */
645 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200646 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300647
648 /* Test the key information */
649 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200650 &got_type,
651 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300652 TEST_ASSERT( got_type == type );
653 TEST_ASSERT( got_bits == (size_t) expected_bits );
654
655 /* Export the key */
656 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200657 exported, export_size,
658 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200659 TEST_ASSERT( status == expected_export_status );
Jaeden Amero2a671e92018-06-27 17:47:40 +0100660 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
661 TEST_ASSERT( mem_is_zero( exported + exported_length,
662 export_size - exported_length ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300663 if( status != PSA_SUCCESS )
664 goto destroy;
665
Moran Pekerf709f4a2018-06-06 17:26:04 +0300666destroy:
667 /* Destroy the key */
668 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
669 TEST_ASSERT( psa_get_key_information(
670 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
671
672exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300673 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300674 mbedtls_psa_crypto_free( );
675}
676/* END_CASE */
677
Gilles Peskine20035e32018-02-03 22:44:14 +0100678/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200679void import_and_exercise_key( data_t *data,
680 int type_arg,
681 int bits_arg,
682 int alg_arg )
683{
684 int slot = 1;
685 psa_key_type_t type = type_arg;
686 size_t bits = bits_arg;
687 psa_algorithm_t alg = alg_arg;
688 psa_key_usage_t usage =
689 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
690 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
691 PSA_KEY_USAGE_VERIFY :
692 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
693 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
694 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
695 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
696 PSA_KEY_USAGE_ENCRYPT :
697 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
Gilles Peskineea0fb492018-07-12 17:17:20 +0200698 PSA_ALG_IS_KEY_DERIVATION( alg ) ? PSA_KEY_USAGE_DERIVE :
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200699 0 );
700 psa_key_policy_t policy;
701 psa_key_type_t got_type;
702 size_t got_bits;
703 psa_status_t status;
704
705 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
706
707 psa_key_policy_init( &policy );
708 psa_key_policy_set_usage( &policy, usage, alg );
709 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
710
711 /* Import the key */
712 status = psa_import_key( slot, type, data->x, data->len );
713 TEST_ASSERT( status == PSA_SUCCESS );
714
715 /* Test the key information */
716 TEST_ASSERT( psa_get_key_information( slot,
717 &got_type,
718 &got_bits ) == PSA_SUCCESS );
719 TEST_ASSERT( got_type == type );
720 TEST_ASSERT( got_bits == bits );
721
722 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +0200723 if( ! exercise_key( slot, usage, alg ) )
724 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200725
726exit:
727 psa_destroy_key( slot );
728 mbedtls_psa_crypto_free( );
729}
730/* END_CASE */
731
732/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +0200733void key_policy( int usage_arg, int alg_arg )
734{
735 int key_slot = 1;
736 psa_algorithm_t alg = alg_arg;
737 psa_key_usage_t usage = usage_arg;
738 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
739 unsigned char key[32] = {0};
740 psa_key_policy_t policy_set;
741 psa_key_policy_t policy_get;
742
743 memset( key, 0x2a, sizeof( key ) );
744
745 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
746
747 psa_key_policy_init( &policy_set );
748 psa_key_policy_init( &policy_get );
749
750 psa_key_policy_set_usage( &policy_set, usage, alg );
751
752 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
753 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
754 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
755
756 TEST_ASSERT( psa_import_key( key_slot, key_type,
757 key, sizeof( key ) ) == PSA_SUCCESS );
758
759 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
760
761 TEST_ASSERT( policy_get.usage == policy_set.usage );
762 TEST_ASSERT( policy_get.alg == policy_set.alg );
763
764exit:
765 psa_destroy_key( key_slot );
766 mbedtls_psa_crypto_free( );
767}
768/* END_CASE */
769
770/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200771void mac_key_policy( int policy_usage,
772 int policy_alg,
773 int key_type,
774 data_t *key_data,
775 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200776{
777 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +0200778 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200779 psa_mac_operation_t operation;
780 psa_status_t status;
781 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200782
783 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
784
785 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200786 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200787 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
788
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200789 TEST_ASSERT( psa_import_key( key_slot, key_type,
790 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +0200791
Gilles Peskine89167cb2018-07-08 20:12:23 +0200792 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200793 if( policy_alg == exercise_alg &&
794 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
795 TEST_ASSERT( status == PSA_SUCCESS );
796 else
797 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
798 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200799
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200800 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +0200801 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200802 if( policy_alg == exercise_alg &&
803 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +0200804 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200805 else
806 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
807
808exit:
809 psa_mac_abort( &operation );
810 psa_destroy_key( key_slot );
811 mbedtls_psa_crypto_free( );
812}
813/* END_CASE */
814
815/* BEGIN_CASE */
816void cipher_key_policy( int policy_usage,
817 int policy_alg,
818 int key_type,
819 data_t *key_data,
820 int exercise_alg )
821{
822 int key_slot = 1;
823 psa_key_policy_t policy;
824 psa_cipher_operation_t operation;
825 psa_status_t status;
826
827 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
828
829 psa_key_policy_init( &policy );
830 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
831 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
832
833 TEST_ASSERT( psa_import_key( key_slot, key_type,
834 key_data->x, key_data->len ) == PSA_SUCCESS );
835
Gilles Peskinefe119512018-07-08 21:39:34 +0200836 status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200837 if( policy_alg == exercise_alg &&
838 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
839 TEST_ASSERT( status == PSA_SUCCESS );
840 else
841 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
842 psa_cipher_abort( &operation );
843
Gilles Peskinefe119512018-07-08 21:39:34 +0200844 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200845 if( policy_alg == exercise_alg &&
846 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
847 TEST_ASSERT( status == PSA_SUCCESS );
848 else
849 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
850
851exit:
852 psa_cipher_abort( &operation );
853 psa_destroy_key( key_slot );
854 mbedtls_psa_crypto_free( );
855}
856/* END_CASE */
857
858/* BEGIN_CASE */
859void aead_key_policy( int policy_usage,
860 int policy_alg,
861 int key_type,
862 data_t *key_data,
863 int nonce_length_arg,
864 int tag_length_arg,
865 int exercise_alg )
866{
867 int key_slot = 1;
868 psa_key_policy_t policy;
869 psa_status_t status;
870 unsigned char nonce[16] = {0};
871 size_t nonce_length = nonce_length_arg;
872 unsigned char tag[16];
873 size_t tag_length = tag_length_arg;
874 size_t output_length;
875
876 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
877 TEST_ASSERT( tag_length <= sizeof( tag ) );
878
879 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
880
881 psa_key_policy_init( &policy );
882 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
883 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
884
885 TEST_ASSERT( psa_import_key( key_slot, key_type,
886 key_data->x, key_data->len ) == PSA_SUCCESS );
887
888 status = psa_aead_encrypt( key_slot, exercise_alg,
889 nonce, nonce_length,
890 NULL, 0,
891 NULL, 0,
892 tag, tag_length,
893 &output_length );
894 if( policy_alg == exercise_alg &&
895 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
896 TEST_ASSERT( status == PSA_SUCCESS );
897 else
898 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
899
900 memset( tag, 0, sizeof( tag ) );
901 status = psa_aead_decrypt( key_slot, exercise_alg,
902 nonce, nonce_length,
903 NULL, 0,
904 tag, tag_length,
905 NULL, 0,
906 &output_length );
907 if( policy_alg == exercise_alg &&
908 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
909 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
910 else
911 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
912
913exit:
914 psa_destroy_key( key_slot );
915 mbedtls_psa_crypto_free( );
916}
917/* END_CASE */
918
919/* BEGIN_CASE */
920void asymmetric_encryption_key_policy( int policy_usage,
921 int policy_alg,
922 int key_type,
923 data_t *key_data,
924 int exercise_alg )
925{
926 int key_slot = 1;
927 psa_key_policy_t policy;
928 psa_status_t status;
929 size_t key_bits;
930 size_t buffer_length;
931 unsigned char *buffer = NULL;
932 size_t output_length;
933
934 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
935
936 psa_key_policy_init( &policy );
937 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
938 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
939
940 TEST_ASSERT( psa_import_key( key_slot, key_type,
941 key_data->x, key_data->len ) == PSA_SUCCESS );
942
943 TEST_ASSERT( psa_get_key_information( key_slot,
944 NULL,
945 &key_bits ) == PSA_SUCCESS );
946 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
947 exercise_alg );
948 buffer = mbedtls_calloc( 1, buffer_length );
949 TEST_ASSERT( buffer != NULL );
950
951 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
952 NULL, 0,
953 NULL, 0,
954 buffer, buffer_length,
955 &output_length );
956 if( policy_alg == exercise_alg &&
957 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
958 TEST_ASSERT( status == PSA_SUCCESS );
959 else
960 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
961
962 memset( buffer, 0, buffer_length );
963 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
964 buffer, buffer_length,
965 NULL, 0,
966 buffer, buffer_length,
967 &output_length );
968 if( policy_alg == exercise_alg &&
969 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
970 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
971 else
972 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
973
974exit:
975 psa_destroy_key( key_slot );
976 mbedtls_psa_crypto_free( );
977 mbedtls_free( buffer );
978}
979/* END_CASE */
980
981/* BEGIN_CASE */
982void asymmetric_signature_key_policy( int policy_usage,
983 int policy_alg,
984 int key_type,
985 data_t *key_data,
986 int exercise_alg )
987{
988 int key_slot = 1;
989 psa_key_policy_t policy;
990 psa_status_t status;
991 unsigned char payload[16] = {1};
992 size_t payload_length = sizeof( payload );
993 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
994 size_t signature_length;
995
996 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
997
998 psa_key_policy_init( &policy );
999 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1000 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1001
1002 TEST_ASSERT( psa_import_key( key_slot, key_type,
1003 key_data->x, key_data->len ) == PSA_SUCCESS );
1004
1005 status = psa_asymmetric_sign( key_slot, exercise_alg,
1006 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001007 signature, sizeof( signature ),
1008 &signature_length );
1009 if( policy_alg == exercise_alg &&
1010 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1011 TEST_ASSERT( status == PSA_SUCCESS );
1012 else
1013 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1014
1015 memset( signature, 0, sizeof( signature ) );
1016 status = psa_asymmetric_verify( key_slot, exercise_alg,
1017 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001018 signature, sizeof( signature ) );
1019 if( policy_alg == exercise_alg &&
1020 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1021 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1022 else
1023 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001024
1025exit:
1026 psa_destroy_key( key_slot );
1027 mbedtls_psa_crypto_free( );
1028}
1029/* END_CASE */
1030
1031/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001032void derive_key_policy( int policy_usage,
1033 int policy_alg,
1034 int key_type,
1035 data_t *key_data,
1036 int exercise_alg )
1037{
1038 int key_slot = 1;
1039 psa_key_policy_t policy;
1040 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1041 psa_status_t status;
1042
1043 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1044
1045 psa_key_policy_init( &policy );
1046 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1047 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1048
1049 TEST_ASSERT( psa_import_key( key_slot, key_type,
1050 key_data->x, key_data->len ) == PSA_SUCCESS );
1051
1052 status = psa_key_derivation( &generator, key_slot,
1053 exercise_alg,
1054 NULL, 0,
1055 NULL, 0,
1056 1 );
1057 if( policy_alg == exercise_alg &&
1058 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1059 TEST_ASSERT( status == PSA_SUCCESS );
1060 else
1061 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1062
1063exit:
1064 psa_generator_abort( &generator );
1065 psa_destroy_key( key_slot );
1066 mbedtls_psa_crypto_free( );
1067}
1068/* END_CASE */
1069
1070/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001071void key_lifetime( int lifetime_arg )
1072{
1073 int key_slot = 1;
1074 psa_key_type_t key_type = PSA_ALG_CBC_BASE;
1075 unsigned char key[32] = {0};
1076 psa_key_lifetime_t lifetime_set = lifetime_arg;
1077 psa_key_lifetime_t lifetime_get;
1078
1079 memset( key, 0x2a, sizeof( key ) );
1080
1081 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1082
1083 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1084 lifetime_set ) == PSA_SUCCESS );
1085
1086 TEST_ASSERT( psa_import_key( key_slot, key_type,
1087 key, sizeof( key ) ) == PSA_SUCCESS );
1088
1089 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1090 &lifetime_get ) == PSA_SUCCESS );
1091
1092 TEST_ASSERT( lifetime_get == lifetime_set );
1093
1094exit:
1095 psa_destroy_key( key_slot );
1096 mbedtls_psa_crypto_free( );
1097}
1098/* END_CASE */
1099
1100/* BEGIN_CASE */
1101void key_lifetime_set_fail( int key_slot_arg,
1102 int lifetime_arg,
1103 int expected_status_arg )
1104{
1105 psa_key_slot_t key_slot = key_slot_arg;
1106 psa_key_lifetime_t lifetime_set = lifetime_arg;
1107 psa_status_t actual_status;
1108 psa_status_t expected_status = expected_status_arg;
1109
1110 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1111
1112 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1113
1114 if( actual_status == PSA_SUCCESS )
1115 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1116
1117 TEST_ASSERT( expected_status == actual_status );
1118
1119exit:
1120 psa_destroy_key( key_slot );
1121 mbedtls_psa_crypto_free( );
1122}
1123/* END_CASE */
1124
1125/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001126void hash_setup( int alg_arg,
1127 int expected_status_arg )
1128{
1129 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001130 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001131 psa_hash_operation_t operation;
1132 psa_status_t status;
1133
1134 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1135
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001136 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001137 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001138 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001139
1140exit:
1141 mbedtls_psa_crypto_free( );
1142}
1143/* END_CASE */
1144
1145/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001146void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001147{
1148 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001149 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001150 size_t actual_hash_length;
1151 psa_hash_operation_t operation;
1152
Gilles Peskine69c12672018-06-28 00:07:19 +02001153 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1154 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1155
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001156 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001157 TEST_ASSERT( expected_hash != NULL );
1158 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1159 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001160
1161 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1162
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001163 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001164 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001165 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001166 TEST_ASSERT( psa_hash_finish( &operation,
1167 actual_hash, sizeof( actual_hash ),
1168 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001169 TEST_ASSERT( actual_hash_length == expected_hash->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001170 TEST_ASSERT( memcmp( expected_hash->x, actual_hash,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001171 expected_hash->len ) == 0 );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001172
1173exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001174 mbedtls_psa_crypto_free( );
1175}
1176/* END_CASE */
1177
1178/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001179void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001180{
1181 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001182 psa_hash_operation_t operation;
1183
Gilles Peskine69c12672018-06-28 00:07:19 +02001184 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1185 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1186
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001187 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001188 TEST_ASSERT( expected_hash != NULL );
1189 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1190 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001191
1192 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1193
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001194 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001195 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001196 input->x,
1197 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001198 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001199 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001200 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001201
1202exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001203 mbedtls_psa_crypto_free( );
1204}
1205/* END_CASE */
1206
1207/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001208void mac_setup( int key_type_arg,
1209 data_t *key,
1210 int alg_arg,
1211 int expected_status_arg )
1212{
1213 int key_slot = 1;
1214 psa_key_type_t key_type = key_type_arg;
1215 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001216 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001217 psa_mac_operation_t operation;
1218 psa_key_policy_t policy;
1219 psa_status_t status;
1220
1221 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1222
1223 psa_key_policy_init( &policy );
1224 psa_key_policy_set_usage( &policy,
1225 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1226 alg );
1227 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1228
1229 TEST_ASSERT( psa_import_key( key_slot, key_type,
1230 key->x, key->len ) == PSA_SUCCESS );
1231
Gilles Peskine89167cb2018-07-08 20:12:23 +02001232 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001233 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001234 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001235
1236exit:
1237 psa_destroy_key( key_slot );
1238 mbedtls_psa_crypto_free( );
1239}
1240/* END_CASE */
1241
1242/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001243void mac_verify( int key_type_arg,
1244 data_t *key,
1245 int alg_arg,
1246 data_t *input,
1247 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001248{
1249 int key_slot = 1;
1250 psa_key_type_t key_type = key_type_arg;
1251 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001252 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001253 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001254
Gilles Peskine69c12672018-06-28 00:07:19 +02001255 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1256
Gilles Peskine8c9def32018-02-08 10:02:12 +01001257 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001258 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001259 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001260 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001261 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1262 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001263
1264 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1265
mohammad16036df908f2018-04-02 08:34:15 -07001266 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001267 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07001268 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1269
Gilles Peskine8c9def32018-02-08 10:02:12 +01001270 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001271 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001272
Gilles Peskine89167cb2018-07-08 20:12:23 +02001273 TEST_ASSERT( psa_mac_verify_setup( &operation,
1274 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001275 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1276 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001277 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02001278 TEST_ASSERT( psa_mac_verify_finish( &operation,
1279 expected_mac->x,
1280 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001281
1282exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001283 psa_destroy_key( key_slot );
1284 mbedtls_psa_crypto_free( );
1285}
1286/* END_CASE */
1287
1288/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001289void cipher_setup( int key_type_arg,
1290 data_t *key,
1291 int alg_arg,
1292 int expected_status_arg )
1293{
1294 int key_slot = 1;
1295 psa_key_type_t key_type = key_type_arg;
1296 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001297 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001298 psa_cipher_operation_t operation;
1299 psa_key_policy_t policy;
1300 psa_status_t status;
1301
1302 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1303
1304 psa_key_policy_init( &policy );
1305 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1306 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1307
1308 TEST_ASSERT( psa_import_key( key_slot, key_type,
1309 key->x, key->len ) == PSA_SUCCESS );
1310
Gilles Peskinefe119512018-07-08 21:39:34 +02001311 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001312 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001313 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001314
1315exit:
1316 psa_destroy_key( key_slot );
1317 mbedtls_psa_crypto_free( );
1318}
1319/* END_CASE */
1320
1321/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001322void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001323 data_t *key,
1324 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001325 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001326{
1327 int key_slot = 1;
1328 psa_status_t status;
1329 psa_key_type_t key_type = key_type_arg;
1330 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001331 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001332 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001333 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001334 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001335 size_t output_buffer_size = 0;
1336 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001337 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001338 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001339 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001340
Gilles Peskine50e586b2018-06-08 14:28:46 +02001341 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001342 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001343 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001344 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1345 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1346 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001347
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001348 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1349 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001350
1351 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1352
Moran Pekered346952018-07-05 15:22:45 +03001353 psa_key_policy_init( &policy );
1354 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1355 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1356
Gilles Peskine50e586b2018-06-08 14:28:46 +02001357 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001358 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001359
Gilles Peskinefe119512018-07-08 21:39:34 +02001360 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1361 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001362
Gilles Peskinefe119512018-07-08 21:39:34 +02001363 TEST_ASSERT( psa_cipher_set_iv( &operation,
1364 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001365 output_buffer_size = (size_t) input->len +
1366 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001367 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001368 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001369
Gilles Peskine4abf7412018-06-18 16:35:34 +02001370 TEST_ASSERT( psa_cipher_update( &operation,
1371 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001372 output, output_buffer_size,
1373 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001374 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001375 status = psa_cipher_finish( &operation,
1376 output + function_output_length,
1377 output_buffer_size,
1378 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001379 total_output_length += function_output_length;
1380
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001381 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001382 if( expected_status == PSA_SUCCESS )
1383 {
1384 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001385 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001386 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001387 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001388 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001389
Gilles Peskine50e586b2018-06-08 14:28:46 +02001390exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001391 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001392 psa_destroy_key( key_slot );
1393 mbedtls_psa_crypto_free( );
1394}
1395/* END_CASE */
1396
1397/* BEGIN_CASE */
1398void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001399 data_t *key,
1400 data_t *input,
1401 int first_part_size,
1402 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001403{
1404 int key_slot = 1;
1405 psa_key_type_t key_type = key_type_arg;
1406 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001407 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001408 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001409 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001410 size_t output_buffer_size = 0;
1411 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001412 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001413 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001414 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001415
Gilles Peskine50e586b2018-06-08 14:28:46 +02001416 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001417 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001418 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001419 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1420 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1421 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001422
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001423 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1424 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001425
1426 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1427
Moran Pekered346952018-07-05 15:22:45 +03001428 psa_key_policy_init( &policy );
1429 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1430 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1431
Gilles Peskine50e586b2018-06-08 14:28:46 +02001432 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001433 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001434
Gilles Peskinefe119512018-07-08 21:39:34 +02001435 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1436 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001437
Gilles Peskinefe119512018-07-08 21:39:34 +02001438 TEST_ASSERT( psa_cipher_set_iv( &operation,
1439 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001440 output_buffer_size = (size_t) input->len +
1441 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001442 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001443 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001444
Gilles Peskine4abf7412018-06-18 16:35:34 +02001445 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001446 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001447 output, output_buffer_size,
1448 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001449 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001450 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001451 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001452 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001453 output, output_buffer_size,
1454 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001455 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001456 TEST_ASSERT( psa_cipher_finish( &operation,
1457 output + function_output_length,
1458 output_buffer_size,
1459 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001460 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001461 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1462
Gilles Peskine4abf7412018-06-18 16:35:34 +02001463 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001464 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001465 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001466
1467exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001468 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001469 psa_destroy_key( key_slot );
1470 mbedtls_psa_crypto_free( );
1471}
1472/* END_CASE */
1473
1474/* BEGIN_CASE */
1475void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001476 data_t *key,
1477 data_t *input,
1478 int first_part_size,
1479 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001480{
1481 int key_slot = 1;
1482
1483 psa_key_type_t key_type = key_type_arg;
1484 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001485 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001486 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001487 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001488 size_t output_buffer_size = 0;
1489 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001490 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001491 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001492 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001493
Gilles Peskine50e586b2018-06-08 14:28:46 +02001494 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001495 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001496 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001497 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1498 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1499 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001500
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001501 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1502 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001503
1504 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1505
Moran Pekered346952018-07-05 15:22:45 +03001506 psa_key_policy_init( &policy );
1507 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1508 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1509
Gilles Peskine50e586b2018-06-08 14:28:46 +02001510 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001511 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001512
Gilles Peskinefe119512018-07-08 21:39:34 +02001513 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1514 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001515
Gilles Peskinefe119512018-07-08 21:39:34 +02001516 TEST_ASSERT( psa_cipher_set_iv( &operation,
1517 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001518
mohammad16033d91abe2018-07-03 13:15:54 +03001519 output_buffer_size = (size_t) input->len +
1520 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001521 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001522 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001523
Gilles Peskine4abf7412018-06-18 16:35:34 +02001524 TEST_ASSERT( (unsigned int) first_part_size < input->len );
1525 TEST_ASSERT( psa_cipher_update( &operation,
1526 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001527 output, output_buffer_size,
1528 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001529 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001530 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001531 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001532 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001533 output, output_buffer_size,
1534 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001535 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001536 TEST_ASSERT( psa_cipher_finish( &operation,
1537 output + function_output_length,
1538 output_buffer_size,
1539 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001540 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001541 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1542
Gilles Peskine4abf7412018-06-18 16:35:34 +02001543 TEST_ASSERT( total_output_length == expected_output->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001544 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001545 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001546
1547exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001548 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001549 psa_destroy_key( key_slot );
1550 mbedtls_psa_crypto_free( );
1551}
1552/* END_CASE */
1553
Gilles Peskine50e586b2018-06-08 14:28:46 +02001554/* BEGIN_CASE */
1555void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001556 data_t *key,
1557 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001558 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001559{
1560 int key_slot = 1;
1561 psa_status_t status;
1562 psa_key_type_t key_type = key_type_arg;
1563 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001564 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001565 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001566 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001567 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001568 size_t output_buffer_size = 0;
1569 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001570 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001571 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001572 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001573
Gilles Peskine50e586b2018-06-08 14:28:46 +02001574 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001575 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001576 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001577 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1578 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1579 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001580
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001581 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1582 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001583
1584 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1585
Moran Pekered346952018-07-05 15:22:45 +03001586 psa_key_policy_init( &policy );
1587 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1588 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1589
Gilles Peskine50e586b2018-06-08 14:28:46 +02001590 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001591 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001592
Gilles Peskinefe119512018-07-08 21:39:34 +02001593 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1594 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001595
Gilles Peskinefe119512018-07-08 21:39:34 +02001596 TEST_ASSERT( psa_cipher_set_iv( &operation,
1597 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001598
mohammad16033d91abe2018-07-03 13:15:54 +03001599 output_buffer_size = (size_t) input->len +
1600 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001601 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001602 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001603
Gilles Peskine4abf7412018-06-18 16:35:34 +02001604 TEST_ASSERT( psa_cipher_update( &operation,
1605 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001606 output, output_buffer_size,
1607 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001608 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001609 status = psa_cipher_finish( &operation,
1610 output + function_output_length,
1611 output_buffer_size,
1612 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001613 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001614 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001615
1616 if( expected_status == PSA_SUCCESS )
1617 {
1618 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001619 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001620 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001621 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001622 }
1623
Gilles Peskine50e586b2018-06-08 14:28:46 +02001624exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001625 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001626 psa_destroy_key( key_slot );
1627 mbedtls_psa_crypto_free( );
1628}
1629/* END_CASE */
1630
Gilles Peskine50e586b2018-06-08 14:28:46 +02001631/* BEGIN_CASE */
1632void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001633 data_t *key,
1634 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02001635{
1636 int key_slot = 1;
1637 psa_key_type_t key_type = key_type_arg;
1638 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07001639 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02001640 size_t iv_size = 16;
1641 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001642 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001643 size_t output1_size = 0;
1644 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001645 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001646 size_t output2_size = 0;
1647 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001648 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001649 psa_cipher_operation_t operation1;
1650 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03001651 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001652
mohammad1603d7d7ba52018-03-12 18:51:53 +02001653 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001654 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001655 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1656 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001657
mohammad1603d7d7ba52018-03-12 18:51:53 +02001658 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1659
Moran Pekered346952018-07-05 15:22:45 +03001660 psa_key_policy_init( &policy );
1661 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
1662 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1663
mohammad1603d7d7ba52018-03-12 18:51:53 +02001664 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001665 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001666
Gilles Peskinefe119512018-07-08 21:39:34 +02001667 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
1668 key_slot, alg ) == PSA_SUCCESS );
1669 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
1670 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001671
Gilles Peskinefe119512018-07-08 21:39:34 +02001672 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
1673 iv, iv_size,
1674 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001675 output1_size = (size_t) input->len +
1676 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001677 output1 = mbedtls_calloc( 1, output1_size );
1678 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001679
Gilles Peskine4abf7412018-06-18 16:35:34 +02001680 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001681 output1, output1_size,
1682 &output1_length ) == PSA_SUCCESS );
1683 TEST_ASSERT( psa_cipher_finish( &operation1,
1684 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001685 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001686
Gilles Peskine048b7f02018-06-08 14:20:49 +02001687 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001688
1689 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1690
1691 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001692 output2 = mbedtls_calloc( 1, output2_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001693 TEST_ASSERT( output2 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001694
Gilles Peskinefe119512018-07-08 21:39:34 +02001695 TEST_ASSERT( psa_cipher_set_iv( &operation2,
1696 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001697 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
1698 output2, output2_size,
1699 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001700 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001701 TEST_ASSERT( psa_cipher_finish( &operation2,
1702 output2 + output2_length,
1703 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001704 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001705
Gilles Peskine048b7f02018-06-08 14:20:49 +02001706 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001707
Janos Follath25c4fa82018-07-06 16:23:25 +01001708 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001709
Gilles Peskine4abf7412018-06-18 16:35:34 +02001710 TEST_ASSERT( input->len == output2_length );
1711 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +03001712
1713exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001714 mbedtls_free( output1 );
1715 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03001716 psa_destroy_key( key_slot );
1717 mbedtls_psa_crypto_free( );
1718}
1719/* END_CASE */
1720
1721/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001722void cipher_verify_output_multipart( int alg_arg,
1723 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001724 data_t *key,
1725 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001726 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03001727{
1728 int key_slot = 1;
1729 psa_key_type_t key_type = key_type_arg;
1730 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03001731 unsigned char iv[16] = {0};
1732 size_t iv_size = 16;
1733 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001734 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001735 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001736 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001737 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001738 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001739 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001740 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001741 psa_cipher_operation_t operation1;
1742 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03001743 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03001744
Moran Pekerded84402018-06-06 16:36:50 +03001745 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001746 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001747 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1748 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001749
Moran Pekerded84402018-06-06 16:36:50 +03001750 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1751
Moran Pekered346952018-07-05 15:22:45 +03001752 psa_key_policy_init( &policy );
1753 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
1754 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1755
Moran Pekerded84402018-06-06 16:36:50 +03001756 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001757 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001758
Gilles Peskinefe119512018-07-08 21:39:34 +02001759 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
1760 key_slot, alg ) == PSA_SUCCESS );
1761 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
1762 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001763
Gilles Peskinefe119512018-07-08 21:39:34 +02001764 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
1765 iv, iv_size,
1766 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001767 output1_buffer_size = (size_t) input->len +
1768 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001769 output1 = mbedtls_calloc( 1, output1_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001770 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001771
Gilles Peskine4abf7412018-06-18 16:35:34 +02001772 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001773
itayzafrir3e02b3b2018-06-12 17:06:52 +03001774 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001775 output1, output1_buffer_size,
1776 &function_output_length ) == PSA_SUCCESS );
1777 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001778
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001779 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001780 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001781 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001782 output1, output1_buffer_size,
1783 &function_output_length ) == PSA_SUCCESS );
1784 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001785
Gilles Peskine048b7f02018-06-08 14:20:49 +02001786 TEST_ASSERT( psa_cipher_finish( &operation1,
1787 output1 + output1_length,
1788 output1_buffer_size - output1_length,
1789 &function_output_length ) == PSA_SUCCESS );
1790 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001791
1792 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1793
Gilles Peskine048b7f02018-06-08 14:20:49 +02001794 output2_buffer_size = output1_length;
1795 output2 = mbedtls_calloc( 1, output2_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001796 TEST_ASSERT( output2 != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001797
Gilles Peskinefe119512018-07-08 21:39:34 +02001798 TEST_ASSERT( psa_cipher_set_iv( &operation2,
1799 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001800
1801 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001802 output2, output2_buffer_size,
1803 &function_output_length ) == PSA_SUCCESS );
1804 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001805
Gilles Peskine048b7f02018-06-08 14:20:49 +02001806 TEST_ASSERT( psa_cipher_update( &operation2,
1807 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001808 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001809 output2, output2_buffer_size,
1810 &function_output_length ) == PSA_SUCCESS );
1811 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001812
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001813 TEST_ASSERT( psa_cipher_finish( &operation2,
1814 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001815 output2_buffer_size - output2_length,
1816 &function_output_length ) == PSA_SUCCESS );
1817 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001818
Janos Follath25c4fa82018-07-06 16:23:25 +01001819 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001820
Gilles Peskine4abf7412018-06-18 16:35:34 +02001821 TEST_ASSERT( input->len == output2_length );
1822 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001823
1824exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001825 mbedtls_free( output1 );
1826 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001827 psa_destroy_key( key_slot );
1828 mbedtls_psa_crypto_free( );
1829}
1830/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02001831
Gilles Peskine20035e32018-02-03 22:44:14 +01001832/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001833void aead_encrypt_decrypt( int key_type_arg,
1834 data_t * key_data,
1835 int alg_arg,
1836 data_t * input_data,
1837 data_t * nonce,
1838 data_t * additional_data,
1839 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001840{
1841 int slot = 1;
1842 psa_key_type_t key_type = key_type_arg;
1843 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001844 unsigned char *output_data = NULL;
1845 size_t output_size = 0;
1846 size_t output_length = 0;
1847 unsigned char *output_data2 = NULL;
1848 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001849 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001850 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001851 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001852
Gilles Peskinea1cac842018-06-11 19:33:02 +02001853 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001854 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001855 TEST_ASSERT( nonce != NULL );
1856 TEST_ASSERT( additional_data != NULL );
1857 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1858 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1859 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1860 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1861
Gilles Peskine4abf7412018-06-18 16:35:34 +02001862 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001863 output_data = mbedtls_calloc( 1, output_size );
1864 TEST_ASSERT( output_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001865
1866 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1867
1868 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001869 psa_key_policy_set_usage( &policy,
1870 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
1871 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001872 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1873
1874 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001875 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001876
1877 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001878 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001879 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001880 additional_data->len,
1881 input_data->x, input_data->len,
1882 output_data, output_size,
1883 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001884
1885 if( PSA_SUCCESS == expected_result )
1886 {
1887 output_data2 = mbedtls_calloc( 1, output_length );
1888 TEST_ASSERT( output_data2 != NULL );
1889
1890 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001891 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001892 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001893 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001894 output_data, output_length,
1895 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001896 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02001897
itayzafrir3e02b3b2018-06-12 17:06:52 +03001898 TEST_ASSERT( memcmp( input_data->x, output_data2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001899 input_data->len ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001900 }
Gilles Peskine2d277862018-06-18 15:41:12 +02001901
Gilles Peskinea1cac842018-06-11 19:33:02 +02001902exit:
1903 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001904 mbedtls_free( output_data );
1905 mbedtls_free( output_data2 );
1906 mbedtls_psa_crypto_free( );
1907}
1908/* END_CASE */
1909
1910/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001911void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001912 int alg_arg, data_t * input_data,
1913 data_t * additional_data, data_t * nonce,
1914 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001915{
1916 int slot = 1;
1917 psa_key_type_t key_type = key_type_arg;
1918 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001919 unsigned char *output_data = NULL;
1920 size_t output_size = 0;
1921 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001922 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001923 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001924
Gilles Peskinea1cac842018-06-11 19:33:02 +02001925 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001926 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001927 TEST_ASSERT( additional_data != NULL );
1928 TEST_ASSERT( nonce != NULL );
1929 TEST_ASSERT( expected_result != NULL );
1930 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1931 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1932 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1933 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1934 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
1935
Gilles Peskine4abf7412018-06-18 16:35:34 +02001936 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001937 output_data = mbedtls_calloc( 1, output_size );
1938 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001939
Gilles Peskinea1cac842018-06-11 19:33:02 +02001940 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1941
1942 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001943 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001944 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1945
1946 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001947 key_data->x,
1948 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001949
1950 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001951 nonce->x, nonce->len,
1952 additional_data->x, additional_data->len,
1953 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001954 output_data, output_size,
1955 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001956
itayzafrir3e02b3b2018-06-12 17:06:52 +03001957 TEST_ASSERT( memcmp( output_data, expected_result->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001958 output_length ) == 0 );
1959
Gilles Peskinea1cac842018-06-11 19:33:02 +02001960exit:
1961 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001962 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001963 mbedtls_psa_crypto_free( );
1964}
1965/* END_CASE */
1966
1967/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001968void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001969 int alg_arg, data_t * input_data,
1970 data_t * additional_data, data_t * nonce,
1971 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001972{
1973 int slot = 1;
1974 psa_key_type_t key_type = key_type_arg;
1975 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001976 unsigned char *output_data = NULL;
1977 size_t output_size = 0;
1978 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001979 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001980 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001981 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001982
Gilles Peskinea1cac842018-06-11 19:33:02 +02001983 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001984 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001985 TEST_ASSERT( additional_data != NULL );
1986 TEST_ASSERT( nonce != NULL );
1987 TEST_ASSERT( expected_data != NULL );
1988 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1989 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1990 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1991 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1992 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
1993
Gilles Peskine4abf7412018-06-18 16:35:34 +02001994 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001995 output_data = mbedtls_calloc( 1, output_size );
1996 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001997
Gilles Peskinea1cac842018-06-11 19:33:02 +02001998 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1999
2000 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002001 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002002 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2003
2004 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002005 key_data->x,
2006 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002007
2008 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002009 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002010 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002011 additional_data->len,
2012 input_data->x, input_data->len,
2013 output_data, output_size,
2014 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002015
Gilles Peskine2d277862018-06-18 15:41:12 +02002016 if( expected_result == PSA_SUCCESS )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002017 {
itayzafrir3e02b3b2018-06-12 17:06:52 +03002018 TEST_ASSERT( memcmp( output_data, expected_data->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02002019 output_length ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002020 }
2021
Gilles Peskinea1cac842018-06-11 19:33:02 +02002022exit:
2023 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002024 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002025 mbedtls_psa_crypto_free( );
2026}
2027/* END_CASE */
2028
2029/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002030void signature_size( int type_arg,
2031 int bits,
2032 int alg_arg,
2033 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002034{
2035 psa_key_type_t type = type_arg;
2036 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002037 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002038 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2039exit:
2040 ;
2041}
2042/* END_CASE */
2043
2044/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002045void sign_deterministic( int key_type_arg, data_t *key_data,
2046 int alg_arg, data_t *input_data,
2047 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002048{
2049 int slot = 1;
2050 psa_key_type_t key_type = key_type_arg;
2051 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002052 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002053 unsigned char *signature = NULL;
2054 size_t signature_size;
2055 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002056 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002057
Gilles Peskine20035e32018-02-03 22:44:14 +01002058 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002059 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002060 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002061 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2062 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2063 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002064
2065 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2066
mohammad1603a97cb8c2018-03-28 03:46:26 -07002067 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002068 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002069 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2070
Gilles Peskine20035e32018-02-03 22:44:14 +01002071 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002072 key_data->x,
2073 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002074 TEST_ASSERT( psa_get_key_information( slot,
2075 NULL,
2076 &key_bits ) == PSA_SUCCESS );
2077
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002078 /* Allocate a buffer which has the size advertized by the
2079 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002080 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2081 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002082 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002083 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine20035e32018-02-03 22:44:14 +01002084 signature = mbedtls_calloc( 1, signature_size );
2085 TEST_ASSERT( signature != NULL );
2086
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002087 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002088 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002089 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002090 signature, signature_size,
2091 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002092 /* Verify that the signature is what is expected. */
Gilles Peskine4abf7412018-06-18 16:35:34 +02002093 TEST_ASSERT( signature_length == output_data->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02002094 TEST_ASSERT( memcmp( signature, output_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002095 output_data->len ) == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01002096
2097exit:
2098 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002099 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002100 mbedtls_psa_crypto_free( );
2101}
2102/* END_CASE */
2103
2104/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002105void sign_fail( int key_type_arg, data_t *key_data,
2106 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002107 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002108{
2109 int slot = 1;
2110 psa_key_type_t key_type = key_type_arg;
2111 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002112 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002113 psa_status_t actual_status;
2114 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002115 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002116 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002117 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002118
Gilles Peskine20035e32018-02-03 22:44:14 +01002119 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002120 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002121 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2122 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2123
Gilles Peskine20035e32018-02-03 22:44:14 +01002124 signature = mbedtls_calloc( 1, signature_size );
2125 TEST_ASSERT( signature != NULL );
2126
2127 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2128
mohammad1603a97cb8c2018-03-28 03:46:26 -07002129 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002130 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002131 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2132
Gilles Peskine20035e32018-02-03 22:44:14 +01002133 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002134 key_data->x,
2135 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002136
2137 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002138 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002139 signature, signature_size,
2140 &signature_length );
2141 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002142 /* The value of *signature_length is unspecified on error, but
2143 * whatever it is, it should be less than signature_size, so that
2144 * if the caller tries to read *signature_length bytes without
2145 * checking the error code then they don't overflow a buffer. */
2146 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002147
2148exit:
2149 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002150 mbedtls_free( signature );
2151 mbedtls_psa_crypto_free( );
2152}
2153/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002154
2155/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002156void sign_verify( int key_type_arg, data_t *key_data,
2157 int alg_arg, data_t *input_data )
2158{
2159 int slot = 1;
2160 psa_key_type_t key_type = key_type_arg;
2161 psa_algorithm_t alg = alg_arg;
2162 size_t key_bits;
2163 unsigned char *signature = NULL;
2164 size_t signature_size;
2165 size_t signature_length = 0xdeadbeef;
2166 psa_key_policy_t policy;
2167
2168 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2169
2170 psa_key_policy_init( &policy );
2171 psa_key_policy_set_usage( &policy,
2172 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2173 alg );
2174 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2175
2176 TEST_ASSERT( psa_import_key( slot, key_type,
2177 key_data->x,
2178 key_data->len ) == PSA_SUCCESS );
2179 TEST_ASSERT( psa_get_key_information( slot,
2180 NULL,
2181 &key_bits ) == PSA_SUCCESS );
2182
2183 /* Allocate a buffer which has the size advertized by the
2184 * library. */
2185 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2186 key_bits, alg );
2187 TEST_ASSERT( signature_size != 0 );
2188 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2189 signature = mbedtls_calloc( 1, signature_size );
2190 TEST_ASSERT( signature != NULL );
2191
2192 /* Perform the signature. */
2193 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2194 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002195 signature, signature_size,
2196 &signature_length ) == PSA_SUCCESS );
2197 /* Check that the signature length looks sensible. */
2198 TEST_ASSERT( signature_length <= signature_size );
2199 TEST_ASSERT( signature_length > 0 );
2200
2201 /* Use the library to verify that the signature is correct. */
2202 TEST_ASSERT( psa_asymmetric_verify(
2203 slot, alg,
2204 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002205 signature, signature_length ) == PSA_SUCCESS );
2206
2207 if( input_data->len != 0 )
2208 {
2209 /* Flip a bit in the input and verify that the signature is now
2210 * detected as invalid. Flip a bit at the beginning, not at the end,
2211 * because ECDSA may ignore the last few bits of the input. */
2212 input_data->x[0] ^= 1;
2213 TEST_ASSERT( psa_asymmetric_verify(
2214 slot, alg,
2215 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002216 signature,
2217 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2218 }
2219
2220exit:
2221 psa_destroy_key( slot );
2222 mbedtls_free( signature );
2223 mbedtls_psa_crypto_free( );
2224}
2225/* END_CASE */
2226
2227/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002228void asymmetric_verify( int key_type_arg, data_t *key_data,
2229 int alg_arg, data_t *hash_data,
2230 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002231{
2232 int slot = 1;
2233 psa_key_type_t key_type = key_type_arg;
2234 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002235 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002236
Gilles Peskine69c12672018-06-28 00:07:19 +02002237 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2238
itayzafrir5c753392018-05-08 11:18:38 +03002239 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002240 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002241 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002242 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2243 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2244 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002245
2246 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2247
2248 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002249 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002250 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2251
2252 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002253 key_data->x,
2254 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002255
2256 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002257 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002258 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002259 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002260exit:
2261 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002262 mbedtls_psa_crypto_free( );
2263}
2264/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002265
2266/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002267void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2268 int alg_arg, data_t *hash_data,
2269 data_t *signature_data,
2270 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002271{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002272 int slot = 1;
2273 psa_key_type_t key_type = key_type_arg;
2274 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002275 psa_status_t actual_status;
2276 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002277 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002278
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002279 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002280 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002281 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002282 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2283 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2284 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002285
2286 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2287
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002288 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002289 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002290 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2291
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002292 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002293 key_data->x,
2294 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002295
2296 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002297 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002298 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002299 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002300
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002301 TEST_ASSERT( actual_status == expected_status );
2302
2303exit:
2304 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002305 mbedtls_psa_crypto_free( );
2306}
2307/* END_CASE */
2308
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002309/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002310void asymmetric_encrypt( int key_type_arg,
2311 data_t *key_data,
2312 int alg_arg,
2313 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02002314 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02002315 int expected_output_length_arg,
2316 int expected_status_arg )
2317{
2318 int slot = 1;
2319 psa_key_type_t key_type = key_type_arg;
2320 psa_algorithm_t alg = alg_arg;
2321 size_t expected_output_length = expected_output_length_arg;
2322 size_t key_bits;
2323 unsigned char *output = NULL;
2324 size_t output_size;
2325 size_t output_length = ~0;
2326 psa_status_t actual_status;
2327 psa_status_t expected_status = expected_status_arg;
2328 psa_key_policy_t policy;
2329
2330 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2331
2332 /* Import the key */
2333 psa_key_policy_init( &policy );
2334 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2335 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2336 TEST_ASSERT( psa_import_key( slot, key_type,
2337 key_data->x,
2338 key_data->len ) == PSA_SUCCESS );
2339
2340 /* Determine the maximum output length */
2341 TEST_ASSERT( psa_get_key_information( slot,
2342 NULL,
2343 &key_bits ) == PSA_SUCCESS );
2344 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
2345 output = mbedtls_calloc( 1, output_size );
Darryl Green9c862252018-07-24 12:52:44 +01002346 TEST_ASSERT( output_size == 0 || output != NULL );
Gilles Peskine656896e2018-06-29 19:12:28 +02002347
2348 /* Encrypt the input */
2349 actual_status = psa_asymmetric_encrypt( slot, alg,
2350 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002351 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02002352 output, output_size,
2353 &output_length );
2354 TEST_ASSERT( actual_status == expected_status );
2355 TEST_ASSERT( output_length == expected_output_length );
2356
Gilles Peskine68428122018-06-30 18:42:41 +02002357 /* If the label is empty, the test framework puts a non-null pointer
2358 * in label->x. Test that a null pointer works as well. */
2359 if( label->len == 0 )
2360 {
2361 output_length = ~0;
2362 memset( output, 0, output_size );
2363 actual_status = psa_asymmetric_encrypt( slot, alg,
2364 input_data->x, input_data->len,
2365 NULL, label->len,
2366 output, output_size,
2367 &output_length );
2368 TEST_ASSERT( actual_status == expected_status );
2369 TEST_ASSERT( output_length == expected_output_length );
2370 }
2371
Gilles Peskine656896e2018-06-29 19:12:28 +02002372exit:
2373 psa_destroy_key( slot );
2374 mbedtls_free( output );
2375 mbedtls_psa_crypto_free( );
2376}
2377/* END_CASE */
2378
2379/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002380void asymmetric_encrypt_decrypt( int key_type_arg,
2381 data_t *key_data,
2382 int alg_arg,
2383 data_t *input_data,
2384 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002385{
2386 int slot = 1;
2387 psa_key_type_t key_type = key_type_arg;
2388 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002389 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002390 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002391 size_t output_size;
2392 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002393 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002394 size_t output2_size;
2395 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002396 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002397
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002398 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002399 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002400 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2401 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2402
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002403 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2404
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002405 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002406 psa_key_policy_set_usage( &policy,
2407 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002408 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002409 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2410
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002411 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002412 key_data->x,
2413 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002414
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002415
2416 /* Determine the maximum ciphertext length */
2417 TEST_ASSERT( psa_get_key_information( slot,
2418 NULL,
2419 &key_bits ) == PSA_SUCCESS );
2420 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
2421 output = mbedtls_calloc( 1, output_size );
2422 TEST_ASSERT( output != NULL );
2423 output2_size = input_data->len;
2424 output2 = mbedtls_calloc( 1, output2_size );
2425 TEST_ASSERT( output2 != NULL );
2426
Gilles Peskineeebd7382018-06-08 18:11:54 +02002427 /* We test encryption by checking that encrypt-then-decrypt gives back
2428 * the original plaintext because of the non-optional random
2429 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02002430 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002431 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002432 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002433 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002434 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002435 /* We don't know what ciphertext length to expect, but check that
2436 * it looks sensible. */
2437 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002438
Gilles Peskine2d277862018-06-18 15:41:12 +02002439 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002440 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02002441 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002442 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002443 &output2_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002444 TEST_ASSERT( output2_length == input_data->len );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002445 TEST_ASSERT( memcmp( input_data->x, output2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002446 input_data->len ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002447
2448exit:
2449 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002450 mbedtls_free( output );
2451 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002452 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002453}
2454/* END_CASE */
2455
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002456/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002457void asymmetric_decrypt( int key_type_arg,
2458 data_t *key_data,
2459 int alg_arg,
2460 data_t *input_data,
2461 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02002462 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002463{
2464 int slot = 1;
2465 psa_key_type_t key_type = key_type_arg;
2466 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002467 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002468 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002469 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002470 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002471
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002472 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002473 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002474 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002475 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2476 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2477 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2478
Gilles Peskine4abf7412018-06-18 16:35:34 +02002479 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002480 output = mbedtls_calloc( 1, output_size );
2481 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002482
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002483 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2484
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002485 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002486 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002487 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2488
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002489 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002490 key_data->x,
2491 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002492
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002493 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002494 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002495 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002496 output,
2497 output_size,
2498 &output_length ) == PSA_SUCCESS );
Gilles Peskine66763a02018-06-29 21:54:10 +02002499 TEST_ASSERT( expected_data->len == output_length );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002500 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002501
Gilles Peskine68428122018-06-30 18:42:41 +02002502 /* If the label is empty, the test framework puts a non-null pointer
2503 * in label->x. Test that a null pointer works as well. */
2504 if( label->len == 0 )
2505 {
2506 output_length = ~0;
2507 memset( output, 0, output_size );
2508 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
2509 input_data->x, input_data->len,
2510 NULL, label->len,
2511 output,
2512 output_size,
2513 &output_length ) == PSA_SUCCESS );
2514 TEST_ASSERT( expected_data->len == output_length );
2515 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
2516 }
2517
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002518exit:
2519 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002520 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002521 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002522}
2523/* END_CASE */
2524
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002525/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002526void asymmetric_decrypt_fail( int key_type_arg,
2527 data_t *key_data,
2528 int alg_arg,
2529 data_t *input_data,
2530 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02002531 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002532{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002533 int slot = 1;
2534 psa_key_type_t key_type = key_type_arg;
2535 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002536 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002537 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002538 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002539 psa_status_t actual_status;
2540 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002541 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002542
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002543 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002544 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002545 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2546 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2547
Gilles Peskine4abf7412018-06-18 16:35:34 +02002548 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002549 output = mbedtls_calloc( 1, output_size );
2550 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002551
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002552 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2553
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002554 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002555 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002556 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2557
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002558 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002559 key_data->x,
2560 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002561
Gilles Peskine2d277862018-06-18 15:41:12 +02002562 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002563 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002564 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002565 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002566 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002567 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002568 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002569
Gilles Peskine68428122018-06-30 18:42:41 +02002570 /* If the label is empty, the test framework puts a non-null pointer
2571 * in label->x. Test that a null pointer works as well. */
2572 if( label->len == 0 )
2573 {
2574 output_length = ~0;
2575 memset( output, 0, output_size );
2576 actual_status = psa_asymmetric_decrypt( slot, alg,
2577 input_data->x, input_data->len,
2578 NULL, label->len,
2579 output, output_size,
2580 &output_length );
2581 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002582 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02002583 }
2584
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002585exit:
2586 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02002587 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002588 mbedtls_psa_crypto_free( );
2589}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002590/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02002591
2592/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002593void derive_setup( int key_type_arg,
2594 data_t *key_data,
2595 int alg_arg,
2596 data_t *salt,
2597 data_t *label,
2598 int requested_capacity_arg,
2599 int expected_status_arg )
2600{
2601 psa_key_slot_t slot = 1;
2602 size_t key_type = key_type_arg;
2603 psa_algorithm_t alg = alg_arg;
2604 size_t requested_capacity = requested_capacity_arg;
2605 psa_status_t expected_status = expected_status_arg;
2606 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2607 psa_key_policy_t policy;
2608
2609 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2610
2611 psa_key_policy_init( &policy );
2612 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2613 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2614
2615 TEST_ASSERT( psa_import_key( slot, key_type,
2616 key_data->x,
2617 key_data->len ) == PSA_SUCCESS );
2618
2619 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
2620 salt->x, salt->len,
2621 label->x, label->len,
2622 requested_capacity ) == expected_status );
2623
2624exit:
2625 psa_generator_abort( &generator );
2626 psa_destroy_key( slot );
2627 mbedtls_psa_crypto_free( );
2628}
2629/* END_CASE */
2630
2631/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02002632void derive_output( int alg_arg,
2633 data_t *key_data,
2634 data_t *salt,
2635 data_t *label,
2636 int requested_capacity_arg,
2637 data_t *expected_output1,
2638 data_t *expected_output2 )
2639{
2640 psa_key_slot_t slot = 1;
2641 psa_algorithm_t alg = alg_arg;
2642 size_t requested_capacity = requested_capacity_arg;
2643 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2644 uint8_t *expected_outputs[2] =
2645 {expected_output1->x, expected_output2->x};
2646 size_t output_sizes[2] =
2647 {expected_output1->len, expected_output2->len};
2648 size_t output_buffer_size = 0;
2649 uint8_t *output_buffer = NULL;
2650 size_t expected_capacity;
2651 size_t current_capacity;
2652 psa_key_policy_t policy;
2653 psa_status_t status;
2654 unsigned i;
2655
2656 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
2657 {
2658 if( output_sizes[i] > output_buffer_size )
2659 output_buffer_size = output_sizes[i];
2660 if( output_sizes[i] == 0 )
2661 expected_outputs[i] = NULL;
2662 }
2663 output_buffer = mbedtls_calloc( 1, output_buffer_size );
2664 TEST_ASSERT( output_buffer != NULL );
2665 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2666
2667 psa_key_policy_init( &policy );
2668 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2669 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2670
2671 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
2672 key_data->x,
2673 key_data->len ) == PSA_SUCCESS );
2674
2675 /* Extraction phase. */
2676 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
2677 salt->x, salt->len,
2678 label->x, label->len,
2679 requested_capacity ) == PSA_SUCCESS );
2680 TEST_ASSERT( psa_get_generator_capacity( &generator,
2681 &current_capacity ) ==
2682 PSA_SUCCESS );
2683 TEST_ASSERT( current_capacity == requested_capacity );
2684 expected_capacity = requested_capacity;
2685
2686 /* Expansion phase. */
2687 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
2688 {
2689 /* Read some bytes. */
2690 status = psa_generator_read( &generator,
2691 output_buffer, output_sizes[i] );
2692 if( expected_capacity == 0 && output_sizes[i] == 0 )
2693 {
2694 /* Reading 0 bytes when 0 bytes are available can go either way. */
2695 TEST_ASSERT( status == PSA_SUCCESS ||
2696 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
2697 continue;
2698 }
2699 else if( expected_capacity == 0 ||
2700 output_sizes[i] > expected_capacity )
2701 {
2702 /* Capacity exceeded. */
2703 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
2704 expected_capacity = 0;
2705 continue;
2706 }
2707 /* Success. Check the read data. */
2708 TEST_ASSERT( status == PSA_SUCCESS );
2709 if( output_sizes[i] != 0 )
2710 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
2711 output_sizes[i] ) == 0 );
2712 /* Check the generator status. */
2713 expected_capacity -= output_sizes[i];
2714 TEST_ASSERT( psa_get_generator_capacity( &generator,
2715 &current_capacity ) ==
2716 PSA_SUCCESS );
2717 TEST_ASSERT( expected_capacity == current_capacity );
2718 }
2719 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
2720
2721exit:
2722 mbedtls_free( output_buffer );
2723 psa_generator_abort( &generator );
2724 psa_destroy_key( slot );
2725 mbedtls_psa_crypto_free( );
2726}
2727/* END_CASE */
2728
2729/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02002730void derive_full( int alg_arg,
2731 data_t *key_data,
2732 data_t *salt,
2733 data_t *label,
2734 int requested_capacity_arg )
2735{
2736 psa_key_slot_t slot = 1;
2737 psa_algorithm_t alg = alg_arg;
2738 size_t requested_capacity = requested_capacity_arg;
2739 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2740 unsigned char output_buffer[16];
2741 size_t expected_capacity = requested_capacity;
2742 size_t current_capacity;
2743 psa_key_policy_t policy;
2744
2745 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2746
2747 psa_key_policy_init( &policy );
2748 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2749 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2750
2751 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
2752 key_data->x,
2753 key_data->len ) == PSA_SUCCESS );
2754
2755 /* Extraction phase. */
2756 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
2757 salt->x, salt->len,
2758 label->x, label->len,
2759 requested_capacity ) == PSA_SUCCESS );
2760 TEST_ASSERT( psa_get_generator_capacity( &generator,
2761 &current_capacity ) ==
2762 PSA_SUCCESS );
2763 TEST_ASSERT( current_capacity == expected_capacity );
2764
2765 /* Expansion phase. */
2766 while( current_capacity > 0 )
2767 {
2768 size_t read_size = sizeof( output_buffer );
2769 if( read_size > current_capacity )
2770 read_size = current_capacity;
2771 TEST_ASSERT( psa_generator_read( &generator,
2772 output_buffer,
2773 read_size ) == PSA_SUCCESS );
2774 expected_capacity -= read_size;
2775 TEST_ASSERT( psa_get_generator_capacity( &generator,
2776 &current_capacity ) ==
2777 PSA_SUCCESS );
2778 TEST_ASSERT( current_capacity == expected_capacity );
2779 }
2780
2781 /* Check that the generator refuses to go over capacity. */
2782 TEST_ASSERT( psa_generator_read( &generator,
2783 output_buffer,
2784 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
2785
2786 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
2787
2788exit:
2789 psa_generator_abort( &generator );
2790 psa_destroy_key( slot );
2791 mbedtls_psa_crypto_free( );
2792}
2793/* END_CASE */
2794
2795/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02002796void derive_key_exercise( int alg_arg,
2797 data_t *key_data,
2798 data_t *salt,
2799 data_t *label,
2800 int derived_type_arg,
2801 int derived_bits_arg,
2802 int derived_usage_arg,
2803 int derived_alg_arg )
2804{
2805 psa_key_slot_t base_key = 1;
2806 psa_key_slot_t derived_key = 2;
2807 psa_algorithm_t alg = alg_arg;
2808 psa_key_type_t derived_type = derived_type_arg;
2809 size_t derived_bits = derived_bits_arg;
2810 psa_key_usage_t derived_usage = derived_usage_arg;
2811 psa_algorithm_t derived_alg = derived_alg_arg;
2812 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
2813 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2814 psa_key_policy_t policy;
2815 psa_key_type_t got_type;
2816 size_t got_bits;
2817
2818 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2819
2820 psa_key_policy_init( &policy );
2821 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2822 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
2823 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
2824 key_data->x,
2825 key_data->len ) == PSA_SUCCESS );
2826
2827 /* Derive a key. */
2828 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
2829 salt->x, salt->len,
2830 label->x, label->len,
2831 capacity ) == PSA_SUCCESS );
2832 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
2833 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
2834 TEST_ASSERT( psa_generator_import_key( derived_key,
2835 derived_type,
2836 derived_bits,
2837 &generator ) == PSA_SUCCESS );
2838
2839 /* Test the key information */
2840 TEST_ASSERT( psa_get_key_information( derived_key,
2841 &got_type,
2842 &got_bits ) == PSA_SUCCESS );
2843 TEST_ASSERT( got_type == derived_type );
2844 TEST_ASSERT( got_bits == derived_bits );
2845
2846 /* Exercise the derived key. */
2847 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
2848 goto exit;
2849
2850exit:
2851 psa_generator_abort( &generator );
2852 psa_destroy_key( base_key );
2853 psa_destroy_key( derived_key );
2854 mbedtls_psa_crypto_free( );
2855}
2856/* END_CASE */
2857
2858/* BEGIN_CASE */
2859void derive_key_export( int alg_arg,
2860 data_t *key_data,
2861 data_t *salt,
2862 data_t *label,
2863 int bytes1_arg,
2864 int bytes2_arg )
2865{
2866 psa_key_slot_t base_key = 1;
2867 psa_key_slot_t derived_key = 2;
2868 psa_algorithm_t alg = alg_arg;
2869 size_t bytes1 = bytes1_arg;
2870 size_t bytes2 = bytes2_arg;
2871 size_t capacity = bytes1 + bytes2;
2872 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2873 uint8_t *output_buffer = mbedtls_calloc( 1, capacity );
2874 uint8_t *export_buffer = mbedtls_calloc( 1, capacity );
2875 psa_key_policy_t policy;
2876 size_t length;
2877
2878 TEST_ASSERT( output_buffer != NULL );
2879 TEST_ASSERT( export_buffer != NULL );
2880 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2881
2882 psa_key_policy_init( &policy );
2883 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2884 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
2885 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
2886 key_data->x,
2887 key_data->len ) == PSA_SUCCESS );
2888
2889 /* Derive some material and output it. */
2890 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
2891 salt->x, salt->len,
2892 label->x, label->len,
2893 capacity ) == PSA_SUCCESS );
2894 TEST_ASSERT( psa_generator_read( &generator,
2895 output_buffer,
2896 capacity ) == PSA_SUCCESS );
2897 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
2898
2899 /* Derive the same output again, but this time store it in key objects. */
2900 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
2901 salt->x, salt->len,
2902 label->x, label->len,
2903 capacity ) == PSA_SUCCESS );
2904 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
2905 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
2906 TEST_ASSERT( psa_generator_import_key( derived_key,
2907 PSA_KEY_TYPE_RAW_DATA,
2908 PSA_BYTES_TO_BITS( bytes1 ),
2909 &generator ) == PSA_SUCCESS );
2910 TEST_ASSERT( psa_export_key( derived_key,
2911 export_buffer, bytes1,
2912 &length ) == PSA_SUCCESS );
2913 TEST_ASSERT( length == bytes1 );
2914 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
2915 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
2916 TEST_ASSERT( psa_generator_import_key( derived_key,
2917 PSA_KEY_TYPE_RAW_DATA,
2918 PSA_BYTES_TO_BITS( bytes2 ),
2919 &generator ) == PSA_SUCCESS );
2920 TEST_ASSERT( psa_export_key( derived_key,
2921 export_buffer + bytes1, bytes2,
2922 &length ) == PSA_SUCCESS );
2923 TEST_ASSERT( length == bytes2 );
2924
2925 /* Compare the outputs from the two runs. */
2926 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
2927
2928exit:
2929 mbedtls_free( output_buffer );
2930 mbedtls_free( export_buffer );
2931 psa_generator_abort( &generator );
2932 psa_destroy_key( base_key );
2933 psa_destroy_key( derived_key );
2934 mbedtls_psa_crypto_free( );
2935}
2936/* END_CASE */
2937
2938/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02002939void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02002940{
Gilles Peskinea50d7392018-06-21 10:22:13 +02002941 size_t bytes = bytes_arg;
2942 const unsigned char trail[] = "don't overwrite me";
2943 unsigned char *output = mbedtls_calloc( 1, bytes + sizeof( trail ) );
2944 unsigned char *changed = mbedtls_calloc( 1, bytes );
2945 size_t i;
2946 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02002947
Gilles Peskinea50d7392018-06-21 10:22:13 +02002948 TEST_ASSERT( output != NULL );
Darryl Green9c862252018-07-24 12:52:44 +01002949 TEST_ASSERT( bytes == 0 || changed != NULL );
Gilles Peskinea50d7392018-06-21 10:22:13 +02002950 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02002951
2952 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2953
Gilles Peskinea50d7392018-06-21 10:22:13 +02002954 /* Run several times, to ensure that every output byte will be
2955 * nonzero at least once with overwhelming probability
2956 * (2^(-8*number_of_runs)). */
2957 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02002958 {
Gilles Peskinea50d7392018-06-21 10:22:13 +02002959 memset( output, 0, bytes );
2960 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
2961
2962 /* Check that no more than bytes have been overwritten */
2963 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
2964
2965 for( i = 0; i < bytes; i++ )
2966 {
2967 if( output[i] != 0 )
2968 ++changed[i];
2969 }
Gilles Peskine05d69892018-06-19 22:00:52 +02002970 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02002971
2972 /* Check that every byte was changed to nonzero at least once. This
2973 * validates that psa_generate_random is overwriting every byte of
2974 * the output buffer. */
2975 for( i = 0; i < bytes; i++ )
2976 {
2977 TEST_ASSERT( changed[i] != 0 );
2978 }
Gilles Peskine05d69892018-06-19 22:00:52 +02002979
2980exit:
2981 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02002982 mbedtls_free( output );
2983 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02002984}
2985/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02002986
2987/* BEGIN_CASE */
2988void generate_key( int type_arg,
2989 int bits_arg,
2990 int usage_arg,
2991 int alg_arg,
2992 int expected_status_arg )
2993{
2994 int slot = 1;
2995 psa_key_type_t type = type_arg;
2996 psa_key_usage_t usage = usage_arg;
2997 size_t bits = bits_arg;
2998 psa_algorithm_t alg = alg_arg;
2999 psa_status_t expected_status = expected_status_arg;
3000 psa_key_type_t got_type;
3001 size_t got_bits;
3002 unsigned char exported[616] = {0}; /* enough for a 1024-bit RSA key */
3003 size_t exported_length;
3004 psa_status_t expected_export_status =
3005 usage & PSA_KEY_USAGE_EXPORT ? PSA_SUCCESS : PSA_ERROR_NOT_PERMITTED;
3006 psa_status_t expected_info_status =
3007 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3008 psa_key_policy_t policy;
3009
3010 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3011
3012 psa_key_policy_init( &policy );
3013 psa_key_policy_set_usage( &policy, usage, alg );
3014 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3015
3016 /* Generate a key */
3017 TEST_ASSERT( psa_generate_key( slot, type, bits,
3018 NULL, 0 ) == expected_status );
3019
3020 /* Test the key information */
3021 TEST_ASSERT( psa_get_key_information( slot,
3022 &got_type,
3023 &got_bits ) == expected_info_status );
3024 if( expected_info_status != PSA_SUCCESS )
3025 goto exit;
3026 TEST_ASSERT( got_type == type );
3027 TEST_ASSERT( got_bits == bits );
3028
3029 /* Export the key */
3030 TEST_ASSERT( psa_export_key( slot,
3031 exported, sizeof( exported ),
3032 &exported_length ) == expected_export_status );
3033 if( expected_export_status == PSA_SUCCESS )
3034 {
Gilles Peskine48c0ea12018-06-21 14:15:31 +02003035 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003036 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
3037#if defined(MBEDTLS_DES_C)
3038 if( type == PSA_KEY_TYPE_DES )
3039 {
3040 /* Check the parity bits. */
3041 unsigned i;
3042 for( i = 0; i < bits / 8; i++ )
3043 {
3044 unsigned bit_count = 0;
3045 unsigned m;
3046 for( m = 1; m <= 0x100; m <<= 1 )
3047 {
3048 if( exported[i] & m )
3049 ++bit_count;
3050 }
3051 TEST_ASSERT( bit_count % 2 != 0 );
3052 }
3053 }
3054#endif
3055#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
3056 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
3057 {
3058 /* Sanity check: does this look like the beginning of a PKCS#8
3059 * RSA key pair? Assumes bits is a multiple of 8. */
3060 size_t n_bytes = bits / 8 + 1;
3061 size_t n_encoded_bytes;
3062 unsigned char *n_end;
3063 TEST_ASSERT( exported_length >= 7 + ( n_bytes + 3 ) * 9 / 2 );
3064 TEST_ASSERT( exported[0] == 0x30 );
3065 TEST_ASSERT( exported[1] == 0x82 ); // assumes >=416-bit key
3066 TEST_ASSERT( exported[4] == 0x02 );
3067 TEST_ASSERT( exported[5] == 0x01 );
3068 TEST_ASSERT( exported[6] == 0x00 );
3069 TEST_ASSERT( exported[7] == 0x02 );
3070 n_encoded_bytes = exported[8];
3071 n_end = exported + 9 + n_encoded_bytes;
3072 if( n_encoded_bytes & 0x80 )
3073 {
3074 n_encoded_bytes = ( n_encoded_bytes & 0x7f ) << 7;
3075 n_encoded_bytes |= exported[9] & 0x7f;
3076 n_end += 1;
3077 }
3078 /* The encoding of n should start with a 0 byte since it should
3079 * have its high bit set. However Mbed TLS is not compliant and
3080 * generates an invalid, but widely tolerated, encoding of
3081 * positive INTEGERs with a bit size that is a multiple of 8
3082 * with no leading 0 byte. Accept this here. */
3083 TEST_ASSERT( n_bytes == n_encoded_bytes ||
3084 n_bytes == n_encoded_bytes + 1 );
3085 if( n_bytes == n_encoded_bytes )
3086 TEST_ASSERT( exported[n_encoded_bytes <= 127 ? 9 : 10] == 0x00 );
3087 /* Sanity check: e must be 3 */
3088 TEST_ASSERT( n_end[0] == 0x02 );
3089 TEST_ASSERT( n_end[1] == 0x03 );
3090 TEST_ASSERT( n_end[2] == 0x01 );
3091 TEST_ASSERT( n_end[3] == 0x00 );
3092 TEST_ASSERT( n_end[4] == 0x01 );
3093 TEST_ASSERT( n_end[5] == 0x02 );
3094 }
3095#endif /* MBEDTLS_RSA_C */
3096#if defined(MBEDTLS_ECP_C)
3097 if( PSA_KEY_TYPE_IS_ECC( type ) )
3098 {
3099 /* Sanity check: does this look like the beginning of a PKCS#8
3100 * elliptic curve key pair? */
3101 TEST_ASSERT( exported_length >= bits * 3 / 8 + 10 );
3102 TEST_ASSERT( exported[0] == 0x30 );
3103 }
3104#endif /* MBEDTLS_ECP_C */
3105 }
3106
Gilles Peskine818ca122018-06-20 18:16:48 +02003107 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02003108 if( ! exercise_key( slot, usage, alg ) )
3109 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003110
3111exit:
3112 psa_destroy_key( slot );
3113 mbedtls_psa_crypto_free( );
3114}
3115/* END_CASE */