blob: 1f1732e659fd97f7bf21ed9658db27a420e87ab0 [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 );
60 if( *p < start || *p - start < (ssize_t) len )
61 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 Peskineca45c352018-06-26 16:13:09 +0200290 unsigned char payload[16] = {1};
Gilles Peskine818ca122018-06-20 18:16:48 +0200291 size_t payload_length = sizeof( payload );
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 {
297 TEST_ASSERT( psa_asymmetric_sign( key, alg,
298 payload, payload_length,
299 NULL, 0,
300 signature, sizeof( signature ),
301 &signature_length ) == PSA_SUCCESS );
302 }
303
304 if( usage & PSA_KEY_USAGE_VERIFY )
305 {
306 psa_status_t verify_status =
307 ( usage & PSA_KEY_USAGE_SIGN ?
308 PSA_SUCCESS :
309 PSA_ERROR_INVALID_SIGNATURE );
310 TEST_ASSERT( psa_asymmetric_verify( key, alg,
311 payload, payload_length,
312 NULL, 0,
313 signature, signature_length ) ==
314 verify_status );
315 }
316
317 return( 1 );
318
319exit:
320 return( 0 );
321}
322
323static int exercise_asymmetric_encryption_key( psa_key_slot_t key,
324 psa_key_usage_t usage,
325 psa_algorithm_t alg )
326{
327 unsigned char plaintext[256] = "Hello, world...";
328 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
329 size_t ciphertext_length = sizeof( ciphertext );
330 size_t plaintext_length = 16;
331
332 if( usage & PSA_KEY_USAGE_ENCRYPT )
333 {
334 TEST_ASSERT(
335 psa_asymmetric_encrypt( key, alg,
336 plaintext, plaintext_length,
337 NULL, 0,
338 ciphertext, sizeof( ciphertext ),
339 &ciphertext_length ) == PSA_SUCCESS );
340 }
341
342 if( usage & PSA_KEY_USAGE_DECRYPT )
343 {
344 psa_status_t status =
345 psa_asymmetric_decrypt( key, alg,
346 ciphertext, ciphertext_length,
347 NULL, 0,
348 plaintext, sizeof( plaintext ),
349 &plaintext_length );
350 TEST_ASSERT( status == PSA_SUCCESS ||
351 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
352 ( status == PSA_ERROR_INVALID_ARGUMENT ||
353 status == PSA_ERROR_INVALID_PADDING ) ) );
354 }
355
356 return( 1 );
357
358exit:
359 return( 0 );
360}
Gilles Peskine02b75072018-07-01 22:31:34 +0200361
Gilles Peskineea0fb492018-07-12 17:17:20 +0200362static int exercise_key_derivation_key( psa_key_slot_t key,
363 psa_key_usage_t usage,
364 psa_algorithm_t alg )
365{
366 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
367 unsigned char label[16] = "This is a label.";
368 size_t label_length = sizeof( label );
369 unsigned char seed[16] = "abcdefghijklmnop";
370 size_t seed_length = sizeof( seed );
371 unsigned char output[1];
372
373 if( usage & PSA_KEY_USAGE_DERIVE )
374 {
375 TEST_ASSERT( psa_key_derivation( &generator,
376 key, alg,
377 label, label_length,
378 seed, seed_length,
379 sizeof( output ) ) == PSA_SUCCESS );
380 TEST_ASSERT( psa_generator_read( &generator,
381 output,
382 sizeof( output ) ) == PSA_SUCCESS );
383 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
384 }
385
386 return( 1 );
387
388exit:
389 return( 0 );
390}
391
Gilles Peskine02b75072018-07-01 22:31:34 +0200392static int exercise_key( psa_key_slot_t slot,
393 psa_key_usage_t usage,
394 psa_algorithm_t alg )
395{
396 int ok;
397 if( alg == 0 )
398 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
399 else if( PSA_ALG_IS_MAC( alg ) )
400 ok = exercise_mac_key( slot, usage, alg );
401 else if( PSA_ALG_IS_CIPHER( alg ) )
402 ok = exercise_cipher_key( slot, usage, alg );
403 else if( PSA_ALG_IS_AEAD( alg ) )
404 ok = exercise_aead_key( slot, usage, alg );
405 else if( PSA_ALG_IS_SIGN( alg ) )
406 ok = exercise_signature_key( slot, usage, alg );
407 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
408 ok = exercise_asymmetric_encryption_key( slot, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200409 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
410 ok = exercise_key_derivation_key( slot, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200411 else
412 {
413 char message[40];
414 mbedtls_snprintf( message, sizeof( message ),
415 "No code to exercise alg=0x%08lx",
416 (unsigned long) alg );
417 test_fail( message, __LINE__, __FILE__ );
418 ok = 0;
419 }
420 return( ok );
421}
422
Gilles Peskinee59236f2018-01-27 23:32:46 +0100423/* END_HEADER */
424
425/* BEGIN_DEPENDENCIES
426 * depends_on:MBEDTLS_PSA_CRYPTO_C
427 * END_DEPENDENCIES
428 */
429
430/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200431void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100432{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100433 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100434 int i;
435 for( i = 0; i <= 1; i++ )
436 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100437 status = psa_crypto_init( );
438 TEST_ASSERT( status == PSA_SUCCESS );
439 status = psa_crypto_init( );
440 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100441 mbedtls_psa_crypto_free( );
442 }
443}
444/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100445
446/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200447void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100448{
449 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200450 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100451 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100452
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100453 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300454 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100455 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
456
Gilles Peskine4abf7412018-06-18 16:35:34 +0200457 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200458 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100459 if( status == PSA_SUCCESS )
460 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
461
462exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100463 mbedtls_psa_crypto_free( );
464}
465/* END_CASE */
466
467/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200468void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
469{
470 int slot = 1;
471 size_t bits = bits_arg;
472 psa_status_t expected_status = expected_status_arg;
473 psa_status_t status;
474 psa_key_type_t type =
475 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
476 size_t buffer_size = /* Slight overapproximations */
477 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
478 unsigned char *buffer = mbedtls_calloc( 1, buffer_size );
479 unsigned char *p;
480 int ret;
481 size_t length;
482
483 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
484 TEST_ASSERT( buffer != NULL );
485
486 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
487 bits, keypair ) ) >= 0 );
488 length = ret;
489
490 /* Try importing the key */
491 status = psa_import_key( slot, type, p, length );
492 TEST_ASSERT( status == expected_status );
493 if( status == PSA_SUCCESS )
494 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
495
496exit:
497 mbedtls_free( buffer );
498 mbedtls_psa_crypto_free( );
499}
500/* END_CASE */
501
502/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300503void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300504 int type_arg,
505 int alg_arg,
506 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100507 int expected_bits,
508 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200509 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100510 int canonical_input )
511{
512 int slot = 1;
513 int slot2 = slot + 1;
514 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200515 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200516 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100517 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100518 unsigned char *exported = NULL;
519 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100520 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100521 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100522 size_t reexported_length;
523 psa_key_type_t got_type;
524 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200525 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100526
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100527 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300528 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
529 export_size = (ssize_t) data->len + export_size_delta;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100530 exported = mbedtls_calloc( 1, export_size );
531 TEST_ASSERT( exported != NULL );
532 if( ! canonical_input )
533 {
534 reexported = mbedtls_calloc( 1, export_size );
535 TEST_ASSERT( reexported != NULL );
536 }
537 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
538
mohammad1603a97cb8c2018-03-28 03:46:26 -0700539 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200540 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700541 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
542
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100543 /* Import the key */
544 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200545 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100546
547 /* Test the key information */
548 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200549 &got_type,
550 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100551 TEST_ASSERT( got_type == type );
552 TEST_ASSERT( got_bits == (size_t) expected_bits );
553
554 /* Export the key */
555 status = psa_export_key( slot,
556 exported, export_size,
557 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200558 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100559
560 /* The exported length must be set by psa_export_key() to a value between 0
561 * and export_size. On errors, the exported length must be 0. */
562 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
563 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
564 TEST_ASSERT( exported_length <= export_size );
565
Gilles Peskine3f669c32018-06-21 09:21:51 +0200566 TEST_ASSERT( mem_is_zero( exported + exported_length,
567 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100568 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200569 {
570 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100571 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200572 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100573
574 if( canonical_input )
575 {
Gilles Peskine4abf7412018-06-18 16:35:34 +0200576 TEST_ASSERT( exported_length == data->len );
577 TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100578 }
579 else
580 {
mohammad1603a97cb8c2018-03-28 03:46:26 -0700581 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
582
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100583 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200584 exported,
585 export_size ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100586 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200587 reexported,
588 export_size,
589 &reexported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100590 TEST_ASSERT( reexported_length == exported_length );
591 TEST_ASSERT( memcmp( reexported, exported,
592 exported_length ) == 0 );
593 }
594
595destroy:
596 /* Destroy the key */
597 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
598 TEST_ASSERT( psa_get_key_information(
599 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
600
601exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300602 mbedtls_free( exported );
603 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100604 mbedtls_psa_crypto_free( );
605}
606/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100607
Moran Pekerf709f4a2018-06-06 17:26:04 +0300608/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300609void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200610 int type_arg,
611 int alg_arg,
612 int expected_bits,
613 int public_key_expected_length,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200614 int expected_export_status_arg )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300615{
616 int slot = 1;
617 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200618 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200619 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300620 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300621 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300622 size_t export_size;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100623 size_t exported_length = INVALID_EXPORT_LENGTH;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300624 psa_key_type_t got_type;
625 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200626 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300627
Moran Pekerf709f4a2018-06-06 17:26:04 +0300628 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300629 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
630 export_size = (ssize_t) data->len;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300631 exported = mbedtls_calloc( 1, export_size );
632 TEST_ASSERT( exported != NULL );
633
634 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
635
636 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200637 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300638 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
639
640 /* Import the key */
641 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200642 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300643
644 /* Test the key information */
645 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200646 &got_type,
647 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300648 TEST_ASSERT( got_type == type );
649 TEST_ASSERT( got_bits == (size_t) expected_bits );
650
651 /* Export the key */
652 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200653 exported, export_size,
654 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200655 TEST_ASSERT( status == expected_export_status );
Jaeden Amero2a671e92018-06-27 17:47:40 +0100656 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
657 TEST_ASSERT( mem_is_zero( exported + exported_length,
658 export_size - exported_length ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300659 if( status != PSA_SUCCESS )
660 goto destroy;
661
Moran Pekerf709f4a2018-06-06 17:26:04 +0300662destroy:
663 /* Destroy the key */
664 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
665 TEST_ASSERT( psa_get_key_information(
666 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
667
668exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300669 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300670 mbedtls_psa_crypto_free( );
671}
672/* END_CASE */
673
Gilles Peskine20035e32018-02-03 22:44:14 +0100674/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200675void import_and_exercise_key( data_t *data,
676 int type_arg,
677 int bits_arg,
678 int alg_arg )
679{
680 int slot = 1;
681 psa_key_type_t type = type_arg;
682 size_t bits = bits_arg;
683 psa_algorithm_t alg = alg_arg;
684 psa_key_usage_t usage =
685 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
686 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
687 PSA_KEY_USAGE_VERIFY :
688 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
689 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
690 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
691 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
692 PSA_KEY_USAGE_ENCRYPT :
693 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
Gilles Peskineea0fb492018-07-12 17:17:20 +0200694 PSA_ALG_IS_KEY_DERIVATION( alg ) ? PSA_KEY_USAGE_DERIVE :
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200695 0 );
696 psa_key_policy_t policy;
697 psa_key_type_t got_type;
698 size_t got_bits;
699 psa_status_t status;
700
701 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
702
703 psa_key_policy_init( &policy );
704 psa_key_policy_set_usage( &policy, usage, alg );
705 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
706
707 /* Import the key */
708 status = psa_import_key( slot, type, data->x, data->len );
709 TEST_ASSERT( status == PSA_SUCCESS );
710
711 /* Test the key information */
712 TEST_ASSERT( psa_get_key_information( slot,
713 &got_type,
714 &got_bits ) == PSA_SUCCESS );
715 TEST_ASSERT( got_type == type );
716 TEST_ASSERT( got_bits == bits );
717
718 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +0200719 if( ! exercise_key( slot, usage, alg ) )
720 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200721
722exit:
723 psa_destroy_key( slot );
724 mbedtls_psa_crypto_free( );
725}
726/* END_CASE */
727
728/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +0200729void key_policy( int usage_arg, int alg_arg )
730{
731 int key_slot = 1;
732 psa_algorithm_t alg = alg_arg;
733 psa_key_usage_t usage = usage_arg;
734 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
735 unsigned char key[32] = {0};
736 psa_key_policy_t policy_set;
737 psa_key_policy_t policy_get;
738
739 memset( key, 0x2a, sizeof( key ) );
740
741 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
742
743 psa_key_policy_init( &policy_set );
744 psa_key_policy_init( &policy_get );
745
746 psa_key_policy_set_usage( &policy_set, usage, alg );
747
748 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
749 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
750 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
751
752 TEST_ASSERT( psa_import_key( key_slot, key_type,
753 key, sizeof( key ) ) == PSA_SUCCESS );
754
755 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
756
757 TEST_ASSERT( policy_get.usage == policy_set.usage );
758 TEST_ASSERT( policy_get.alg == policy_set.alg );
759
760exit:
761 psa_destroy_key( key_slot );
762 mbedtls_psa_crypto_free( );
763}
764/* END_CASE */
765
766/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200767void mac_key_policy( int policy_usage,
768 int policy_alg,
769 int key_type,
770 data_t *key_data,
771 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200772{
773 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +0200774 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200775 psa_mac_operation_t operation;
776 psa_status_t status;
777 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200778
779 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
780
781 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200782 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200783 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
784
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200785 TEST_ASSERT( psa_import_key( key_slot, key_type,
786 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +0200787
Gilles Peskine89167cb2018-07-08 20:12:23 +0200788 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200789 if( policy_alg == exercise_alg &&
790 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
791 TEST_ASSERT( status == PSA_SUCCESS );
792 else
793 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
794 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200795
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200796 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +0200797 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200798 if( policy_alg == exercise_alg &&
799 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +0200800 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200801 else
802 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
803
804exit:
805 psa_mac_abort( &operation );
806 psa_destroy_key( key_slot );
807 mbedtls_psa_crypto_free( );
808}
809/* END_CASE */
810
811/* BEGIN_CASE */
812void cipher_key_policy( int policy_usage,
813 int policy_alg,
814 int key_type,
815 data_t *key_data,
816 int exercise_alg )
817{
818 int key_slot = 1;
819 psa_key_policy_t policy;
820 psa_cipher_operation_t operation;
821 psa_status_t status;
822
823 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
824
825 psa_key_policy_init( &policy );
826 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
827 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
828
829 TEST_ASSERT( psa_import_key( key_slot, key_type,
830 key_data->x, key_data->len ) == PSA_SUCCESS );
831
Gilles Peskinefe119512018-07-08 21:39:34 +0200832 status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200833 if( policy_alg == exercise_alg &&
834 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
835 TEST_ASSERT( status == PSA_SUCCESS );
836 else
837 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
838 psa_cipher_abort( &operation );
839
Gilles Peskinefe119512018-07-08 21:39:34 +0200840 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200841 if( policy_alg == exercise_alg &&
842 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
843 TEST_ASSERT( status == PSA_SUCCESS );
844 else
845 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
846
847exit:
848 psa_cipher_abort( &operation );
849 psa_destroy_key( key_slot );
850 mbedtls_psa_crypto_free( );
851}
852/* END_CASE */
853
854/* BEGIN_CASE */
855void aead_key_policy( int policy_usage,
856 int policy_alg,
857 int key_type,
858 data_t *key_data,
859 int nonce_length_arg,
860 int tag_length_arg,
861 int exercise_alg )
862{
863 int key_slot = 1;
864 psa_key_policy_t policy;
865 psa_status_t status;
866 unsigned char nonce[16] = {0};
867 size_t nonce_length = nonce_length_arg;
868 unsigned char tag[16];
869 size_t tag_length = tag_length_arg;
870 size_t output_length;
871
872 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
873 TEST_ASSERT( tag_length <= sizeof( tag ) );
874
875 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
876
877 psa_key_policy_init( &policy );
878 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
879 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
880
881 TEST_ASSERT( psa_import_key( key_slot, key_type,
882 key_data->x, key_data->len ) == PSA_SUCCESS );
883
884 status = psa_aead_encrypt( key_slot, exercise_alg,
885 nonce, nonce_length,
886 NULL, 0,
887 NULL, 0,
888 tag, tag_length,
889 &output_length );
890 if( policy_alg == exercise_alg &&
891 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
892 TEST_ASSERT( status == PSA_SUCCESS );
893 else
894 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
895
896 memset( tag, 0, sizeof( tag ) );
897 status = psa_aead_decrypt( key_slot, exercise_alg,
898 nonce, nonce_length,
899 NULL, 0,
900 tag, tag_length,
901 NULL, 0,
902 &output_length );
903 if( policy_alg == exercise_alg &&
904 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
905 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
906 else
907 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
908
909exit:
910 psa_destroy_key( key_slot );
911 mbedtls_psa_crypto_free( );
912}
913/* END_CASE */
914
915/* BEGIN_CASE */
916void asymmetric_encryption_key_policy( int policy_usage,
917 int policy_alg,
918 int key_type,
919 data_t *key_data,
920 int exercise_alg )
921{
922 int key_slot = 1;
923 psa_key_policy_t policy;
924 psa_status_t status;
925 size_t key_bits;
926 size_t buffer_length;
927 unsigned char *buffer = NULL;
928 size_t output_length;
929
930 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
931
932 psa_key_policy_init( &policy );
933 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
934 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
935
936 TEST_ASSERT( psa_import_key( key_slot, key_type,
937 key_data->x, key_data->len ) == PSA_SUCCESS );
938
939 TEST_ASSERT( psa_get_key_information( key_slot,
940 NULL,
941 &key_bits ) == PSA_SUCCESS );
942 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
943 exercise_alg );
944 buffer = mbedtls_calloc( 1, buffer_length );
945 TEST_ASSERT( buffer != NULL );
946
947 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
948 NULL, 0,
949 NULL, 0,
950 buffer, buffer_length,
951 &output_length );
952 if( policy_alg == exercise_alg &&
953 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
954 TEST_ASSERT( status == PSA_SUCCESS );
955 else
956 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
957
958 memset( buffer, 0, buffer_length );
959 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
960 buffer, buffer_length,
961 NULL, 0,
962 buffer, buffer_length,
963 &output_length );
964 if( policy_alg == exercise_alg &&
965 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
966 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
967 else
968 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
969
970exit:
971 psa_destroy_key( key_slot );
972 mbedtls_psa_crypto_free( );
973 mbedtls_free( buffer );
974}
975/* END_CASE */
976
977/* BEGIN_CASE */
978void asymmetric_signature_key_policy( int policy_usage,
979 int policy_alg,
980 int key_type,
981 data_t *key_data,
982 int exercise_alg )
983{
984 int key_slot = 1;
985 psa_key_policy_t policy;
986 psa_status_t status;
987 unsigned char payload[16] = {1};
988 size_t payload_length = sizeof( payload );
989 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
990 size_t signature_length;
991
992 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
993
994 psa_key_policy_init( &policy );
995 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
996 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
997
998 TEST_ASSERT( psa_import_key( key_slot, key_type,
999 key_data->x, key_data->len ) == PSA_SUCCESS );
1000
1001 status = psa_asymmetric_sign( key_slot, exercise_alg,
1002 payload, payload_length,
1003 NULL, 0,
1004 signature, sizeof( signature ),
1005 &signature_length );
1006 if( policy_alg == exercise_alg &&
1007 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1008 TEST_ASSERT( status == PSA_SUCCESS );
1009 else
1010 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1011
1012 memset( signature, 0, sizeof( signature ) );
1013 status = psa_asymmetric_verify( key_slot, exercise_alg,
1014 payload, payload_length,
1015 NULL, 0,
1016 signature, sizeof( signature ) );
1017 if( policy_alg == exercise_alg &&
1018 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1019 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1020 else
1021 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001022
1023exit:
1024 psa_destroy_key( key_slot );
1025 mbedtls_psa_crypto_free( );
1026}
1027/* END_CASE */
1028
1029/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001030void derive_key_policy( int policy_usage,
1031 int policy_alg,
1032 int key_type,
1033 data_t *key_data,
1034 int exercise_alg )
1035{
1036 int key_slot = 1;
1037 psa_key_policy_t policy;
1038 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1039 psa_status_t status;
1040
1041 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1042
1043 psa_key_policy_init( &policy );
1044 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1045 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1046
1047 TEST_ASSERT( psa_import_key( key_slot, key_type,
1048 key_data->x, key_data->len ) == PSA_SUCCESS );
1049
1050 status = psa_key_derivation( &generator, key_slot,
1051 exercise_alg,
1052 NULL, 0,
1053 NULL, 0,
1054 1 );
1055 if( policy_alg == exercise_alg &&
1056 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1057 TEST_ASSERT( status == PSA_SUCCESS );
1058 else
1059 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1060
1061exit:
1062 psa_generator_abort( &generator );
1063 psa_destroy_key( key_slot );
1064 mbedtls_psa_crypto_free( );
1065}
1066/* END_CASE */
1067
1068/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001069void key_lifetime( int lifetime_arg )
1070{
1071 int key_slot = 1;
1072 psa_key_type_t key_type = PSA_ALG_CBC_BASE;
1073 unsigned char key[32] = {0};
1074 psa_key_lifetime_t lifetime_set = lifetime_arg;
1075 psa_key_lifetime_t lifetime_get;
1076
1077 memset( key, 0x2a, sizeof( key ) );
1078
1079 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1080
1081 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1082 lifetime_set ) == PSA_SUCCESS );
1083
1084 TEST_ASSERT( psa_import_key( key_slot, key_type,
1085 key, sizeof( key ) ) == PSA_SUCCESS );
1086
1087 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1088 &lifetime_get ) == PSA_SUCCESS );
1089
1090 TEST_ASSERT( lifetime_get == lifetime_set );
1091
1092exit:
1093 psa_destroy_key( key_slot );
1094 mbedtls_psa_crypto_free( );
1095}
1096/* END_CASE */
1097
1098/* BEGIN_CASE */
1099void key_lifetime_set_fail( int key_slot_arg,
1100 int lifetime_arg,
1101 int expected_status_arg )
1102{
1103 psa_key_slot_t key_slot = key_slot_arg;
1104 psa_key_lifetime_t lifetime_set = lifetime_arg;
1105 psa_status_t actual_status;
1106 psa_status_t expected_status = expected_status_arg;
1107
1108 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1109
1110 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1111
1112 if( actual_status == PSA_SUCCESS )
1113 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1114
1115 TEST_ASSERT( expected_status == actual_status );
1116
1117exit:
1118 psa_destroy_key( key_slot );
1119 mbedtls_psa_crypto_free( );
1120}
1121/* END_CASE */
1122
1123/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001124void hash_setup( int alg_arg,
1125 int expected_status_arg )
1126{
1127 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001128 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001129 psa_hash_operation_t operation;
1130 psa_status_t status;
1131
1132 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1133
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001134 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001135 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001136 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001137
1138exit:
1139 mbedtls_psa_crypto_free( );
1140}
1141/* END_CASE */
1142
1143/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001144void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001145{
1146 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001147 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001148 size_t actual_hash_length;
1149 psa_hash_operation_t operation;
1150
Gilles Peskine69c12672018-06-28 00:07:19 +02001151 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1152 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1153
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001154 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001155 TEST_ASSERT( expected_hash != NULL );
1156 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1157 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001158
1159 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1160
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001161 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001162 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001163 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001164 TEST_ASSERT( psa_hash_finish( &operation,
1165 actual_hash, sizeof( actual_hash ),
1166 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001167 TEST_ASSERT( actual_hash_length == expected_hash->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001168 TEST_ASSERT( memcmp( expected_hash->x, actual_hash,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001169 expected_hash->len ) == 0 );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001170
1171exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001172 mbedtls_psa_crypto_free( );
1173}
1174/* END_CASE */
1175
1176/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001177void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001178{
1179 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001180 psa_hash_operation_t operation;
1181
Gilles Peskine69c12672018-06-28 00:07:19 +02001182 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1183 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1184
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001185 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001186 TEST_ASSERT( expected_hash != NULL );
1187 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1188 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001189
1190 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1191
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001192 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001193 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001194 input->x,
1195 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001196 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001197 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001198 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001199
1200exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001201 mbedtls_psa_crypto_free( );
1202}
1203/* END_CASE */
1204
1205/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001206void mac_setup( int key_type_arg,
1207 data_t *key,
1208 int alg_arg,
1209 int expected_status_arg )
1210{
1211 int key_slot = 1;
1212 psa_key_type_t key_type = key_type_arg;
1213 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001214 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001215 psa_mac_operation_t operation;
1216 psa_key_policy_t policy;
1217 psa_status_t status;
1218
1219 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1220
1221 psa_key_policy_init( &policy );
1222 psa_key_policy_set_usage( &policy,
1223 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1224 alg );
1225 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1226
1227 TEST_ASSERT( psa_import_key( key_slot, key_type,
1228 key->x, key->len ) == PSA_SUCCESS );
1229
Gilles Peskine89167cb2018-07-08 20:12:23 +02001230 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001231 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001232 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001233
1234exit:
1235 psa_destroy_key( key_slot );
1236 mbedtls_psa_crypto_free( );
1237}
1238/* END_CASE */
1239
1240/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001241void mac_verify( int key_type_arg,
1242 data_t *key,
1243 int alg_arg,
1244 data_t *input,
1245 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001246{
1247 int key_slot = 1;
1248 psa_key_type_t key_type = key_type_arg;
1249 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001250 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001251 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001252
Gilles Peskine69c12672018-06-28 00:07:19 +02001253 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1254
Gilles Peskine8c9def32018-02-08 10:02:12 +01001255 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001256 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001257 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001258 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001259 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1260 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001261
1262 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1263
mohammad16036df908f2018-04-02 08:34:15 -07001264 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001265 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07001266 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1267
Gilles Peskine8c9def32018-02-08 10:02:12 +01001268 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001269 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001270
Gilles Peskine89167cb2018-07-08 20:12:23 +02001271 TEST_ASSERT( psa_mac_verify_setup( &operation,
1272 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001273 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1274 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001275 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02001276 TEST_ASSERT( psa_mac_verify_finish( &operation,
1277 expected_mac->x,
1278 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001279
1280exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001281 psa_destroy_key( key_slot );
1282 mbedtls_psa_crypto_free( );
1283}
1284/* END_CASE */
1285
1286/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001287void cipher_setup( int key_type_arg,
1288 data_t *key,
1289 int alg_arg,
1290 int expected_status_arg )
1291{
1292 int key_slot = 1;
1293 psa_key_type_t key_type = key_type_arg;
1294 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001295 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001296 psa_cipher_operation_t operation;
1297 psa_key_policy_t policy;
1298 psa_status_t status;
1299
1300 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1301
1302 psa_key_policy_init( &policy );
1303 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1304 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1305
1306 TEST_ASSERT( psa_import_key( key_slot, key_type,
1307 key->x, key->len ) == PSA_SUCCESS );
1308
Gilles Peskinefe119512018-07-08 21:39:34 +02001309 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001310 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001311 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001312
1313exit:
1314 psa_destroy_key( key_slot );
1315 mbedtls_psa_crypto_free( );
1316}
1317/* END_CASE */
1318
1319/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001320void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001321 data_t *key,
1322 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001323 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001324{
1325 int key_slot = 1;
1326 psa_status_t status;
1327 psa_key_type_t key_type = key_type_arg;
1328 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001329 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001330 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001331 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001332 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001333 size_t output_buffer_size = 0;
1334 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001335 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001336 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001337 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001338
Gilles Peskine50e586b2018-06-08 14:28:46 +02001339 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001340 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001341 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001342 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1343 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1344 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001345
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001346 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1347 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001348
1349 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1350
Moran Pekered346952018-07-05 15:22:45 +03001351 psa_key_policy_init( &policy );
1352 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1353 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1354
Gilles Peskine50e586b2018-06-08 14:28:46 +02001355 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001356 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001357
Gilles Peskinefe119512018-07-08 21:39:34 +02001358 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1359 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001360
Gilles Peskinefe119512018-07-08 21:39:34 +02001361 TEST_ASSERT( psa_cipher_set_iv( &operation,
1362 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001363 output_buffer_size = (size_t) input->len +
1364 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001365 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001366 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001367
Gilles Peskine4abf7412018-06-18 16:35:34 +02001368 TEST_ASSERT( psa_cipher_update( &operation,
1369 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001370 output, output_buffer_size,
1371 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001372 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001373 status = psa_cipher_finish( &operation,
1374 output + function_output_length,
1375 output_buffer_size,
1376 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001377 total_output_length += function_output_length;
1378
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001379 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001380 if( expected_status == PSA_SUCCESS )
1381 {
1382 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001383 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001384 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001385 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001386 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001387
Gilles Peskine50e586b2018-06-08 14:28:46 +02001388exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001389 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001390 psa_destroy_key( key_slot );
1391 mbedtls_psa_crypto_free( );
1392}
1393/* END_CASE */
1394
1395/* BEGIN_CASE */
1396void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001397 data_t *key,
1398 data_t *input,
1399 int first_part_size,
1400 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001401{
1402 int key_slot = 1;
1403 psa_key_type_t key_type = key_type_arg;
1404 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001405 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001406 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001407 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001408 size_t output_buffer_size = 0;
1409 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001410 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001411 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001412 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001413
Gilles Peskine50e586b2018-06-08 14:28:46 +02001414 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001415 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001416 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001417 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1418 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1419 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001420
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001421 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1422 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001423
1424 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1425
Moran Pekered346952018-07-05 15:22:45 +03001426 psa_key_policy_init( &policy );
1427 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1428 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1429
Gilles Peskine50e586b2018-06-08 14:28:46 +02001430 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001431 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001432
Gilles Peskinefe119512018-07-08 21:39:34 +02001433 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1434 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001435
Gilles Peskinefe119512018-07-08 21:39:34 +02001436 TEST_ASSERT( psa_cipher_set_iv( &operation,
1437 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001438 output_buffer_size = (size_t) input->len +
1439 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001440 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001441 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001442
Gilles Peskine4abf7412018-06-18 16:35:34 +02001443 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001444 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001445 output, output_buffer_size,
1446 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001447 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001448 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001449 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001450 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001451 output, output_buffer_size,
1452 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001453 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001454 TEST_ASSERT( psa_cipher_finish( &operation,
1455 output + function_output_length,
1456 output_buffer_size,
1457 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001458 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001459 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1460
Gilles Peskine4abf7412018-06-18 16:35:34 +02001461 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001462 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001463 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001464
1465exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001466 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001467 psa_destroy_key( key_slot );
1468 mbedtls_psa_crypto_free( );
1469}
1470/* END_CASE */
1471
1472/* BEGIN_CASE */
1473void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001474 data_t *key,
1475 data_t *input,
1476 int first_part_size,
1477 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001478{
1479 int key_slot = 1;
1480
1481 psa_key_type_t key_type = key_type_arg;
1482 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001483 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001484 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001485 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001486 size_t output_buffer_size = 0;
1487 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001488 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001489 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001490 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001491
Gilles Peskine50e586b2018-06-08 14:28:46 +02001492 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001493 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001494 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001495 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1496 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1497 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001498
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001499 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1500 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001501
1502 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1503
Moran Pekered346952018-07-05 15:22:45 +03001504 psa_key_policy_init( &policy );
1505 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1506 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1507
Gilles Peskine50e586b2018-06-08 14:28:46 +02001508 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001509 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001510
Gilles Peskinefe119512018-07-08 21:39:34 +02001511 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1512 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001513
Gilles Peskinefe119512018-07-08 21:39:34 +02001514 TEST_ASSERT( psa_cipher_set_iv( &operation,
1515 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001516
mohammad16033d91abe2018-07-03 13:15:54 +03001517 output_buffer_size = (size_t) input->len +
1518 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001519 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001520 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001521
Gilles Peskine4abf7412018-06-18 16:35:34 +02001522 TEST_ASSERT( (unsigned int) first_part_size < input->len );
1523 TEST_ASSERT( psa_cipher_update( &operation,
1524 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001525 output, output_buffer_size,
1526 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001527 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001528 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001529 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001530 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001531 output, output_buffer_size,
1532 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001533 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001534 TEST_ASSERT( psa_cipher_finish( &operation,
1535 output + function_output_length,
1536 output_buffer_size,
1537 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001538 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001539 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1540
Gilles Peskine4abf7412018-06-18 16:35:34 +02001541 TEST_ASSERT( total_output_length == expected_output->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001542 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001543 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001544
1545exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001546 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001547 psa_destroy_key( key_slot );
1548 mbedtls_psa_crypto_free( );
1549}
1550/* END_CASE */
1551
Gilles Peskine50e586b2018-06-08 14:28:46 +02001552/* BEGIN_CASE */
1553void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001554 data_t *key,
1555 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001556 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001557{
1558 int key_slot = 1;
1559 psa_status_t status;
1560 psa_key_type_t key_type = key_type_arg;
1561 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001562 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001563 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001564 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001565 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001566 size_t output_buffer_size = 0;
1567 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001568 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001569 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001570 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001571
Gilles Peskine50e586b2018-06-08 14:28:46 +02001572 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001573 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001574 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001575 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1576 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1577 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001578
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001579 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1580 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001581
1582 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1583
Moran Pekered346952018-07-05 15:22:45 +03001584 psa_key_policy_init( &policy );
1585 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1586 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1587
Gilles Peskine50e586b2018-06-08 14:28:46 +02001588 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001589 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001590
Gilles Peskinefe119512018-07-08 21:39:34 +02001591 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1592 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001593
Gilles Peskinefe119512018-07-08 21:39:34 +02001594 TEST_ASSERT( psa_cipher_set_iv( &operation,
1595 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001596
mohammad16033d91abe2018-07-03 13:15:54 +03001597 output_buffer_size = (size_t) input->len +
1598 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001599 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001600 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001601
Gilles Peskine4abf7412018-06-18 16:35:34 +02001602 TEST_ASSERT( psa_cipher_update( &operation,
1603 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001604 output, output_buffer_size,
1605 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001606 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001607 status = psa_cipher_finish( &operation,
1608 output + function_output_length,
1609 output_buffer_size,
1610 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001611 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001612 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001613
1614 if( expected_status == PSA_SUCCESS )
1615 {
1616 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001617 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001618 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001619 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001620 }
1621
Gilles Peskine50e586b2018-06-08 14:28:46 +02001622exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001623 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001624 psa_destroy_key( key_slot );
1625 mbedtls_psa_crypto_free( );
1626}
1627/* END_CASE */
1628
Gilles Peskine50e586b2018-06-08 14:28:46 +02001629/* BEGIN_CASE */
1630void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001631 data_t *key,
1632 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02001633{
1634 int key_slot = 1;
1635 psa_key_type_t key_type = key_type_arg;
1636 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07001637 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02001638 size_t iv_size = 16;
1639 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001640 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001641 size_t output1_size = 0;
1642 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001643 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001644 size_t output2_size = 0;
1645 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001646 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001647 psa_cipher_operation_t operation1;
1648 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03001649 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001650
mohammad1603d7d7ba52018-03-12 18:51:53 +02001651 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001652 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001653 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1654 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001655
mohammad1603d7d7ba52018-03-12 18:51:53 +02001656 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1657
Moran Pekered346952018-07-05 15:22:45 +03001658 psa_key_policy_init( &policy );
1659 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
1660 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1661
mohammad1603d7d7ba52018-03-12 18:51:53 +02001662 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001663 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001664
Gilles Peskinefe119512018-07-08 21:39:34 +02001665 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
1666 key_slot, alg ) == PSA_SUCCESS );
1667 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
1668 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001669
Gilles Peskinefe119512018-07-08 21:39:34 +02001670 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
1671 iv, iv_size,
1672 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001673 output1_size = (size_t) input->len +
1674 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001675 output1 = mbedtls_calloc( 1, output1_size );
1676 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001677
Gilles Peskine4abf7412018-06-18 16:35:34 +02001678 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001679 output1, output1_size,
1680 &output1_length ) == PSA_SUCCESS );
1681 TEST_ASSERT( psa_cipher_finish( &operation1,
1682 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001683 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001684
Gilles Peskine048b7f02018-06-08 14:20:49 +02001685 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001686
1687 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1688
1689 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001690 output2 = mbedtls_calloc( 1, output2_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001691 TEST_ASSERT( output2 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001692
Gilles Peskinefe119512018-07-08 21:39:34 +02001693 TEST_ASSERT( psa_cipher_set_iv( &operation2,
1694 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001695 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
1696 output2, output2_size,
1697 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001698 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001699 TEST_ASSERT( psa_cipher_finish( &operation2,
1700 output2 + output2_length,
1701 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001702 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001703
Gilles Peskine048b7f02018-06-08 14:20:49 +02001704 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001705
Janos Follath25c4fa82018-07-06 16:23:25 +01001706 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001707
Gilles Peskine4abf7412018-06-18 16:35:34 +02001708 TEST_ASSERT( input->len == output2_length );
1709 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +03001710
1711exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001712 mbedtls_free( output1 );
1713 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03001714 psa_destroy_key( key_slot );
1715 mbedtls_psa_crypto_free( );
1716}
1717/* END_CASE */
1718
1719/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001720void cipher_verify_output_multipart( int alg_arg,
1721 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001722 data_t *key,
1723 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001724 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03001725{
1726 int key_slot = 1;
1727 psa_key_type_t key_type = key_type_arg;
1728 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03001729 unsigned char iv[16] = {0};
1730 size_t iv_size = 16;
1731 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001732 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001733 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001734 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001735 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001736 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001737 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001738 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001739 psa_cipher_operation_t operation1;
1740 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03001741 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03001742
Moran Pekerded84402018-06-06 16:36:50 +03001743 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001744 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001745 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1746 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001747
Moran Pekerded84402018-06-06 16:36:50 +03001748 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1749
Moran Pekered346952018-07-05 15:22:45 +03001750 psa_key_policy_init( &policy );
1751 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
1752 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1753
Moran Pekerded84402018-06-06 16:36:50 +03001754 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001755 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001756
Gilles Peskinefe119512018-07-08 21:39:34 +02001757 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
1758 key_slot, alg ) == PSA_SUCCESS );
1759 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
1760 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001761
Gilles Peskinefe119512018-07-08 21:39:34 +02001762 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
1763 iv, iv_size,
1764 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001765 output1_buffer_size = (size_t) input->len +
1766 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001767 output1 = mbedtls_calloc( 1, output1_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001768 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001769
Gilles Peskine4abf7412018-06-18 16:35:34 +02001770 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001771
itayzafrir3e02b3b2018-06-12 17:06:52 +03001772 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001773 output1, output1_buffer_size,
1774 &function_output_length ) == PSA_SUCCESS );
1775 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001776
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001777 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001778 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001779 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001780 output1, output1_buffer_size,
1781 &function_output_length ) == PSA_SUCCESS );
1782 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001783
Gilles Peskine048b7f02018-06-08 14:20:49 +02001784 TEST_ASSERT( psa_cipher_finish( &operation1,
1785 output1 + output1_length,
1786 output1_buffer_size - output1_length,
1787 &function_output_length ) == PSA_SUCCESS );
1788 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001789
1790 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1791
Gilles Peskine048b7f02018-06-08 14:20:49 +02001792 output2_buffer_size = output1_length;
1793 output2 = mbedtls_calloc( 1, output2_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001794 TEST_ASSERT( output2 != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001795
Gilles Peskinefe119512018-07-08 21:39:34 +02001796 TEST_ASSERT( psa_cipher_set_iv( &operation2,
1797 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001798
1799 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001800 output2, output2_buffer_size,
1801 &function_output_length ) == PSA_SUCCESS );
1802 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001803
Gilles Peskine048b7f02018-06-08 14:20:49 +02001804 TEST_ASSERT( psa_cipher_update( &operation2,
1805 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001806 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001807 output2, output2_buffer_size,
1808 &function_output_length ) == PSA_SUCCESS );
1809 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001810
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001811 TEST_ASSERT( psa_cipher_finish( &operation2,
1812 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001813 output2_buffer_size - output2_length,
1814 &function_output_length ) == PSA_SUCCESS );
1815 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001816
Janos Follath25c4fa82018-07-06 16:23:25 +01001817 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001818
Gilles Peskine4abf7412018-06-18 16:35:34 +02001819 TEST_ASSERT( input->len == output2_length );
1820 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001821
1822exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001823 mbedtls_free( output1 );
1824 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001825 psa_destroy_key( key_slot );
1826 mbedtls_psa_crypto_free( );
1827}
1828/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02001829
Gilles Peskine20035e32018-02-03 22:44:14 +01001830/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001831void aead_encrypt_decrypt( int key_type_arg,
1832 data_t * key_data,
1833 int alg_arg,
1834 data_t * input_data,
1835 data_t * nonce,
1836 data_t * additional_data,
1837 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001838{
1839 int slot = 1;
1840 psa_key_type_t key_type = key_type_arg;
1841 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001842 unsigned char *output_data = NULL;
1843 size_t output_size = 0;
1844 size_t output_length = 0;
1845 unsigned char *output_data2 = NULL;
1846 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001847 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001848 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001849 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001850
Gilles Peskinea1cac842018-06-11 19:33:02 +02001851 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001852 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001853 TEST_ASSERT( nonce != NULL );
1854 TEST_ASSERT( additional_data != NULL );
1855 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1856 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1857 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1858 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1859
Gilles Peskine4abf7412018-06-18 16:35:34 +02001860 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001861 output_data = mbedtls_calloc( 1, output_size );
1862 TEST_ASSERT( output_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001863
1864 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1865
1866 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001867 psa_key_policy_set_usage( &policy,
1868 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
1869 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001870 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1871
1872 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001873 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001874
1875 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001876 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001877 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001878 additional_data->len,
1879 input_data->x, input_data->len,
1880 output_data, output_size,
1881 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001882
1883 if( PSA_SUCCESS == expected_result )
1884 {
1885 output_data2 = mbedtls_calloc( 1, output_length );
1886 TEST_ASSERT( output_data2 != NULL );
1887
1888 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001889 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001890 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001891 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001892 output_data, output_length,
1893 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001894 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02001895
itayzafrir3e02b3b2018-06-12 17:06:52 +03001896 TEST_ASSERT( memcmp( input_data->x, output_data2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001897 input_data->len ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001898 }
Gilles Peskine2d277862018-06-18 15:41:12 +02001899
Gilles Peskinea1cac842018-06-11 19:33:02 +02001900exit:
1901 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001902 mbedtls_free( output_data );
1903 mbedtls_free( output_data2 );
1904 mbedtls_psa_crypto_free( );
1905}
1906/* END_CASE */
1907
1908/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001909void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001910 int alg_arg, data_t * input_data,
1911 data_t * additional_data, data_t * nonce,
1912 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001913{
1914 int slot = 1;
1915 psa_key_type_t key_type = key_type_arg;
1916 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001917 unsigned char *output_data = NULL;
1918 size_t output_size = 0;
1919 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001920 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001921 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001922
Gilles Peskinea1cac842018-06-11 19:33:02 +02001923 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001924 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001925 TEST_ASSERT( additional_data != NULL );
1926 TEST_ASSERT( nonce != NULL );
1927 TEST_ASSERT( expected_result != NULL );
1928 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1929 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1930 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1931 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1932 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
1933
Gilles Peskine4abf7412018-06-18 16:35:34 +02001934 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001935 output_data = mbedtls_calloc( 1, output_size );
1936 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001937
Gilles Peskinea1cac842018-06-11 19:33:02 +02001938 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1939
1940 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001941 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001942 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1943
1944 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001945 key_data->x,
1946 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001947
1948 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001949 nonce->x, nonce->len,
1950 additional_data->x, additional_data->len,
1951 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001952 output_data, output_size,
1953 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001954
itayzafrir3e02b3b2018-06-12 17:06:52 +03001955 TEST_ASSERT( memcmp( output_data, expected_result->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001956 output_length ) == 0 );
1957
Gilles Peskinea1cac842018-06-11 19:33:02 +02001958exit:
1959 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001960 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001961 mbedtls_psa_crypto_free( );
1962}
1963/* END_CASE */
1964
1965/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001966void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001967 int alg_arg, data_t * input_data,
1968 data_t * additional_data, data_t * nonce,
1969 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001970{
1971 int slot = 1;
1972 psa_key_type_t key_type = key_type_arg;
1973 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001974 unsigned char *output_data = NULL;
1975 size_t output_size = 0;
1976 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001977 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001978 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001979 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001980
Gilles Peskinea1cac842018-06-11 19:33:02 +02001981 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001982 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001983 TEST_ASSERT( additional_data != NULL );
1984 TEST_ASSERT( nonce != NULL );
1985 TEST_ASSERT( expected_data != NULL );
1986 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1987 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1988 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1989 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1990 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
1991
Gilles Peskine4abf7412018-06-18 16:35:34 +02001992 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001993 output_data = mbedtls_calloc( 1, output_size );
1994 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001995
Gilles Peskinea1cac842018-06-11 19:33:02 +02001996 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1997
1998 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001999 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002000 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2001
2002 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002003 key_data->x,
2004 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002005
2006 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002007 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002008 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002009 additional_data->len,
2010 input_data->x, input_data->len,
2011 output_data, output_size,
2012 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002013
Gilles Peskine2d277862018-06-18 15:41:12 +02002014 if( expected_result == PSA_SUCCESS )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002015 {
itayzafrir3e02b3b2018-06-12 17:06:52 +03002016 TEST_ASSERT( memcmp( output_data, expected_data->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02002017 output_length ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002018 }
2019
Gilles Peskinea1cac842018-06-11 19:33:02 +02002020exit:
2021 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002022 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002023 mbedtls_psa_crypto_free( );
2024}
2025/* END_CASE */
2026
2027/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002028void signature_size( int type_arg,
2029 int bits,
2030 int alg_arg,
2031 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002032{
2033 psa_key_type_t type = type_arg;
2034 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002035 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002036 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2037exit:
2038 ;
2039}
2040/* END_CASE */
2041
2042/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002043void sign_deterministic( int key_type_arg, data_t *key_data,
2044 int alg_arg, data_t *input_data,
2045 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002046{
2047 int slot = 1;
2048 psa_key_type_t key_type = key_type_arg;
2049 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002050 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002051 unsigned char *signature = NULL;
2052 size_t signature_size;
2053 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002054 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002055
Gilles Peskine20035e32018-02-03 22:44:14 +01002056 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002057 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002058 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002059 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2060 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2061 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002062
2063 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2064
mohammad1603a97cb8c2018-03-28 03:46:26 -07002065 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002066 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002067 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2068
Gilles Peskine20035e32018-02-03 22:44:14 +01002069 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002070 key_data->x,
2071 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002072 TEST_ASSERT( psa_get_key_information( slot,
2073 NULL,
2074 &key_bits ) == PSA_SUCCESS );
2075
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002076 /* Allocate a buffer which has the size advertized by the
2077 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002078 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2079 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002080 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002081 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine20035e32018-02-03 22:44:14 +01002082 signature = mbedtls_calloc( 1, signature_size );
2083 TEST_ASSERT( signature != NULL );
2084
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002085 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002086 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002087 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002088 NULL, 0,
2089 signature, signature_size,
2090 &signature_length ) == PSA_SUCCESS );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002091 /* Verify that the signature is correct. */
Gilles Peskine4abf7412018-06-18 16:35:34 +02002092 TEST_ASSERT( signature_length == output_data->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02002093 TEST_ASSERT( memcmp( signature, output_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002094 output_data->len ) == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01002095
2096exit:
2097 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002098 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002099 mbedtls_psa_crypto_free( );
2100}
2101/* END_CASE */
2102
2103/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002104void sign_fail( int key_type_arg, data_t *key_data,
2105 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002106 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002107{
2108 int slot = 1;
2109 psa_key_type_t key_type = key_type_arg;
2110 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002111 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002112 psa_status_t actual_status;
2113 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002114 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002115 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002116 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002117
Gilles Peskine20035e32018-02-03 22:44:14 +01002118 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002119 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002120 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2121 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2122
Gilles Peskine20035e32018-02-03 22:44:14 +01002123 signature = mbedtls_calloc( 1, signature_size );
2124 TEST_ASSERT( signature != NULL );
2125
2126 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2127
mohammad1603a97cb8c2018-03-28 03:46:26 -07002128 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002129 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002130 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2131
Gilles Peskine20035e32018-02-03 22:44:14 +01002132 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002133 key_data->x,
2134 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002135
2136 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002137 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002138 NULL, 0,
2139 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 */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002156void asymmetric_verify( int key_type_arg, data_t *key_data,
2157 int alg_arg, data_t *hash_data,
2158 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002159{
2160 int slot = 1;
2161 psa_key_type_t key_type = key_type_arg;
2162 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002163 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002164
Gilles Peskine69c12672018-06-28 00:07:19 +02002165 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2166
itayzafrir5c753392018-05-08 11:18:38 +03002167 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002168 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002169 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002170 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2171 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2172 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002173
2174 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2175
2176 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002177 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002178 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2179
2180 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002181 key_data->x,
2182 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002183
2184 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002185 hash_data->x, hash_data->len,
itayzafrir5c753392018-05-08 11:18:38 +03002186 NULL, 0,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002187 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002188 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002189exit:
2190 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002191 mbedtls_psa_crypto_free( );
2192}
2193/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002194
2195/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002196void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2197 int alg_arg, data_t *hash_data,
2198 data_t *signature_data,
2199 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002200{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002201 int slot = 1;
2202 psa_key_type_t key_type = key_type_arg;
2203 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002204 psa_status_t actual_status;
2205 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002206 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002207
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002208 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002209 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002210 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002211 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2212 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2213 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002214
2215 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2216
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002217 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002218 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002219 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2220
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002221 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002222 key_data->x,
2223 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002224
2225 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002226 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002227 NULL, 0,
2228 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002229 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002230
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002231 TEST_ASSERT( actual_status == expected_status );
2232
2233exit:
2234 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002235 mbedtls_psa_crypto_free( );
2236}
2237/* END_CASE */
2238
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002239/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002240void asymmetric_encrypt_decrypt( int key_type_arg, data_t *key_data,
2241 int alg_arg, data_t *input_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002242{
2243 int slot = 1;
2244 psa_key_type_t key_type = key_type_arg;
2245 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002246 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002247 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002248 size_t output_length = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002249 unsigned char *output2 = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002250 size_t output2_size = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002251 size_t output2_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002252 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002253
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002254 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002255 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002256 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2257 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2258
Gilles Peskine4abf7412018-06-18 16:35:34 +02002259 output_size = key_data->len;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002260 output2_size = output_size;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002261 output = mbedtls_calloc( 1, output_size );
2262 TEST_ASSERT( output != NULL );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002263 output2 = mbedtls_calloc( 1, output2_size );
2264 TEST_ASSERT( output2 != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002265
2266 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2267
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002268 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002269 psa_key_policy_set_usage( &policy,
2270 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002271 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002272 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2273
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002274 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002275 key_data->x,
2276 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002277
Gilles Peskineeebd7382018-06-08 18:11:54 +02002278 /* We test encryption by checking that encrypt-then-decrypt gives back
2279 * the original plaintext because of the non-optional random
2280 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02002281 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002282 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002283 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002284 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002285 &output_length ) == PSA_SUCCESS );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002286
Gilles Peskine2d277862018-06-18 15:41:12 +02002287 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002288 output, output_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02002289 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002290 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002291 &output2_length ) == PSA_SUCCESS );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002292 TEST_ASSERT( memcmp( input_data->x, output2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002293 input_data->len ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002294
2295exit:
2296 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002297 mbedtls_free( output );
2298 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002299 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002300}
2301/* END_CASE */
2302
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002303/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002304void asymmetric_encrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002305 int alg_arg, data_t *input_data,
2306 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002307{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002308 int slot = 1;
2309 psa_key_type_t key_type = key_type_arg;
2310 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002311 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002312 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002313 size_t output_length = 0;
2314 psa_status_t actual_status;
2315 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002316 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002317
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002318 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002319 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002320 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2321 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2322
Gilles Peskine4abf7412018-06-18 16:35:34 +02002323 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002324 output = mbedtls_calloc( 1, output_size );
2325 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002326
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002327 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2328
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002329 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002330 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002331 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2332
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002333 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002334 key_data->x,
2335 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002336
Gilles Peskine2d277862018-06-18 15:41:12 +02002337 actual_status = psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002338 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002339 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002340 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002341 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002342 TEST_ASSERT( actual_status == expected_status );
2343
2344exit:
2345 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002346 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002347 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002348}
2349/* END_CASE */
2350
2351/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002352void asymmetric_decrypt( int key_type_arg, data_t *key_data,
2353 int alg_arg, data_t *input_data,
2354 data_t *expected_data, int expected_size )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002355{
2356 int slot = 1;
2357 psa_key_type_t key_type = key_type_arg;
2358 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002359 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002360 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002361 size_t output_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002362 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002363
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002364 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002365 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002366 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002367 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2368 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2369 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2370
Gilles Peskine4abf7412018-06-18 16:35:34 +02002371 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002372 output = mbedtls_calloc( 1, output_size );
2373 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002374
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002375 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2376
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002377 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002378 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002379 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2380
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002381 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002382 key_data->x,
2383 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002384
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002385 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002386 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002387 NULL, 0,
2388 output,
2389 output_size,
2390 &output_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002391 TEST_ASSERT( (size_t) expected_size == output_length );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002392 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002393
2394exit:
2395 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002396 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002397 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002398}
2399/* END_CASE */
2400
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002401/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002402void asymmetric_decrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002403 int alg_arg, data_t *input_data,
2404 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002405{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002406 int slot = 1;
2407 psa_key_type_t key_type = key_type_arg;
2408 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002409 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002410 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002411 size_t output_length = 0;
2412 psa_status_t actual_status;
2413 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002414 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002415
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002416 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002417 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002418 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2419 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2420
Gilles Peskine4abf7412018-06-18 16:35:34 +02002421 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002422 output = mbedtls_calloc( 1, output_size );
2423 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002424
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002425 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2426
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002427 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002428 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002429 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2430
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002431 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002432 key_data->x,
2433 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002434
Gilles Peskine2d277862018-06-18 15:41:12 +02002435 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002436 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002437 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002438 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002439 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002440 TEST_ASSERT( actual_status == expected_status );
2441
2442exit:
2443 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02002444 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002445 mbedtls_psa_crypto_free( );
2446}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002447/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02002448
2449/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002450void derive_setup( int key_type_arg,
2451 data_t *key_data,
2452 int alg_arg,
2453 data_t *salt,
2454 data_t *label,
2455 int requested_capacity_arg,
2456 int expected_status_arg )
2457{
2458 psa_key_slot_t slot = 1;
2459 size_t key_type = key_type_arg;
2460 psa_algorithm_t alg = alg_arg;
2461 size_t requested_capacity = requested_capacity_arg;
2462 psa_status_t expected_status = expected_status_arg;
2463 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2464 psa_key_policy_t policy;
2465
2466 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2467
2468 psa_key_policy_init( &policy );
2469 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2470 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2471
2472 TEST_ASSERT( psa_import_key( slot, key_type,
2473 key_data->x,
2474 key_data->len ) == PSA_SUCCESS );
2475
2476 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
2477 salt->x, salt->len,
2478 label->x, label->len,
2479 requested_capacity ) == expected_status );
2480
2481exit:
2482 psa_generator_abort( &generator );
2483 psa_destroy_key( slot );
2484 mbedtls_psa_crypto_free( );
2485}
2486/* END_CASE */
2487
2488/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02002489void derive_output( int alg_arg,
2490 data_t *key_data,
2491 data_t *salt,
2492 data_t *label,
2493 int requested_capacity_arg,
2494 data_t *expected_output1,
2495 data_t *expected_output2 )
2496{
2497 psa_key_slot_t slot = 1;
2498 psa_algorithm_t alg = alg_arg;
2499 size_t requested_capacity = requested_capacity_arg;
2500 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2501 uint8_t *expected_outputs[2] =
2502 {expected_output1->x, expected_output2->x};
2503 size_t output_sizes[2] =
2504 {expected_output1->len, expected_output2->len};
2505 size_t output_buffer_size = 0;
2506 uint8_t *output_buffer = NULL;
2507 size_t expected_capacity;
2508 size_t current_capacity;
2509 psa_key_policy_t policy;
2510 psa_status_t status;
2511 unsigned i;
2512
2513 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
2514 {
2515 if( output_sizes[i] > output_buffer_size )
2516 output_buffer_size = output_sizes[i];
2517 if( output_sizes[i] == 0 )
2518 expected_outputs[i] = NULL;
2519 }
2520 output_buffer = mbedtls_calloc( 1, output_buffer_size );
2521 TEST_ASSERT( output_buffer != NULL );
2522 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2523
2524 psa_key_policy_init( &policy );
2525 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2526 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2527
2528 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
2529 key_data->x,
2530 key_data->len ) == PSA_SUCCESS );
2531
2532 /* Extraction phase. */
2533 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
2534 salt->x, salt->len,
2535 label->x, label->len,
2536 requested_capacity ) == PSA_SUCCESS );
2537 TEST_ASSERT( psa_get_generator_capacity( &generator,
2538 &current_capacity ) ==
2539 PSA_SUCCESS );
2540 TEST_ASSERT( current_capacity == requested_capacity );
2541 expected_capacity = requested_capacity;
2542
2543 /* Expansion phase. */
2544 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
2545 {
2546 /* Read some bytes. */
2547 status = psa_generator_read( &generator,
2548 output_buffer, output_sizes[i] );
2549 if( expected_capacity == 0 && output_sizes[i] == 0 )
2550 {
2551 /* Reading 0 bytes when 0 bytes are available can go either way. */
2552 TEST_ASSERT( status == PSA_SUCCESS ||
2553 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
2554 continue;
2555 }
2556 else if( expected_capacity == 0 ||
2557 output_sizes[i] > expected_capacity )
2558 {
2559 /* Capacity exceeded. */
2560 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
2561 expected_capacity = 0;
2562 continue;
2563 }
2564 /* Success. Check the read data. */
2565 TEST_ASSERT( status == PSA_SUCCESS );
2566 if( output_sizes[i] != 0 )
2567 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
2568 output_sizes[i] ) == 0 );
2569 /* Check the generator status. */
2570 expected_capacity -= output_sizes[i];
2571 TEST_ASSERT( psa_get_generator_capacity( &generator,
2572 &current_capacity ) ==
2573 PSA_SUCCESS );
2574 TEST_ASSERT( expected_capacity == current_capacity );
2575 }
2576 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
2577
2578exit:
2579 mbedtls_free( output_buffer );
2580 psa_generator_abort( &generator );
2581 psa_destroy_key( slot );
2582 mbedtls_psa_crypto_free( );
2583}
2584/* END_CASE */
2585
2586/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02002587void derive_key_exercise( int alg_arg,
2588 data_t *key_data,
2589 data_t *salt,
2590 data_t *label,
2591 int derived_type_arg,
2592 int derived_bits_arg,
2593 int derived_usage_arg,
2594 int derived_alg_arg )
2595{
2596 psa_key_slot_t base_key = 1;
2597 psa_key_slot_t derived_key = 2;
2598 psa_algorithm_t alg = alg_arg;
2599 psa_key_type_t derived_type = derived_type_arg;
2600 size_t derived_bits = derived_bits_arg;
2601 psa_key_usage_t derived_usage = derived_usage_arg;
2602 psa_algorithm_t derived_alg = derived_alg_arg;
2603 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
2604 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2605 psa_key_policy_t policy;
2606 psa_key_type_t got_type;
2607 size_t got_bits;
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( base_key, &policy ) == PSA_SUCCESS );
2614 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
2615 key_data->x,
2616 key_data->len ) == PSA_SUCCESS );
2617
2618 /* Derive a key. */
2619 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
2620 salt->x, salt->len,
2621 label->x, label->len,
2622 capacity ) == PSA_SUCCESS );
2623 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
2624 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
2625 TEST_ASSERT( psa_generator_import_key( derived_key,
2626 derived_type,
2627 derived_bits,
2628 &generator ) == PSA_SUCCESS );
2629
2630 /* Test the key information */
2631 TEST_ASSERT( psa_get_key_information( derived_key,
2632 &got_type,
2633 &got_bits ) == PSA_SUCCESS );
2634 TEST_ASSERT( got_type == derived_type );
2635 TEST_ASSERT( got_bits == derived_bits );
2636
2637 /* Exercise the derived key. */
2638 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
2639 goto exit;
2640
2641exit:
2642 psa_generator_abort( &generator );
2643 psa_destroy_key( base_key );
2644 psa_destroy_key( derived_key );
2645 mbedtls_psa_crypto_free( );
2646}
2647/* END_CASE */
2648
2649/* BEGIN_CASE */
2650void derive_key_export( int alg_arg,
2651 data_t *key_data,
2652 data_t *salt,
2653 data_t *label,
2654 int bytes1_arg,
2655 int bytes2_arg )
2656{
2657 psa_key_slot_t base_key = 1;
2658 psa_key_slot_t derived_key = 2;
2659 psa_algorithm_t alg = alg_arg;
2660 size_t bytes1 = bytes1_arg;
2661 size_t bytes2 = bytes2_arg;
2662 size_t capacity = bytes1 + bytes2;
2663 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2664 uint8_t *output_buffer = mbedtls_calloc( 1, capacity );
2665 uint8_t *export_buffer = mbedtls_calloc( 1, capacity );
2666 psa_key_policy_t policy;
2667 size_t length;
2668
2669 TEST_ASSERT( output_buffer != NULL );
2670 TEST_ASSERT( export_buffer != NULL );
2671 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2672
2673 psa_key_policy_init( &policy );
2674 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2675 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
2676 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
2677 key_data->x,
2678 key_data->len ) == PSA_SUCCESS );
2679
2680 /* Derive some material and output it. */
2681 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
2682 salt->x, salt->len,
2683 label->x, label->len,
2684 capacity ) == PSA_SUCCESS );
2685 TEST_ASSERT( psa_generator_read( &generator,
2686 output_buffer,
2687 capacity ) == PSA_SUCCESS );
2688 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
2689
2690 /* Derive the same output again, but this time store it in key objects. */
2691 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
2692 salt->x, salt->len,
2693 label->x, label->len,
2694 capacity ) == PSA_SUCCESS );
2695 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
2696 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
2697 TEST_ASSERT( psa_generator_import_key( derived_key,
2698 PSA_KEY_TYPE_RAW_DATA,
2699 PSA_BYTES_TO_BITS( bytes1 ),
2700 &generator ) == PSA_SUCCESS );
2701 TEST_ASSERT( psa_export_key( derived_key,
2702 export_buffer, bytes1,
2703 &length ) == PSA_SUCCESS );
2704 TEST_ASSERT( length == bytes1 );
2705 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
2706 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
2707 TEST_ASSERT( psa_generator_import_key( derived_key,
2708 PSA_KEY_TYPE_RAW_DATA,
2709 PSA_BYTES_TO_BITS( bytes2 ),
2710 &generator ) == PSA_SUCCESS );
2711 TEST_ASSERT( psa_export_key( derived_key,
2712 export_buffer + bytes1, bytes2,
2713 &length ) == PSA_SUCCESS );
2714 TEST_ASSERT( length == bytes2 );
2715
2716 /* Compare the outputs from the two runs. */
2717 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
2718
2719exit:
2720 mbedtls_free( output_buffer );
2721 mbedtls_free( export_buffer );
2722 psa_generator_abort( &generator );
2723 psa_destroy_key( base_key );
2724 psa_destroy_key( derived_key );
2725 mbedtls_psa_crypto_free( );
2726}
2727/* END_CASE */
2728
2729/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02002730void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02002731{
Gilles Peskinea50d7392018-06-21 10:22:13 +02002732 size_t bytes = bytes_arg;
2733 const unsigned char trail[] = "don't overwrite me";
2734 unsigned char *output = mbedtls_calloc( 1, bytes + sizeof( trail ) );
2735 unsigned char *changed = mbedtls_calloc( 1, bytes );
2736 size_t i;
2737 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02002738
Gilles Peskinea50d7392018-06-21 10:22:13 +02002739 TEST_ASSERT( output != NULL );
2740 TEST_ASSERT( changed != NULL );
2741 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02002742
2743 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2744
Gilles Peskinea50d7392018-06-21 10:22:13 +02002745 /* Run several times, to ensure that every output byte will be
2746 * nonzero at least once with overwhelming probability
2747 * (2^(-8*number_of_runs)). */
2748 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02002749 {
Gilles Peskinea50d7392018-06-21 10:22:13 +02002750 memset( output, 0, bytes );
2751 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
2752
2753 /* Check that no more than bytes have been overwritten */
2754 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
2755
2756 for( i = 0; i < bytes; i++ )
2757 {
2758 if( output[i] != 0 )
2759 ++changed[i];
2760 }
Gilles Peskine05d69892018-06-19 22:00:52 +02002761 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02002762
2763 /* Check that every byte was changed to nonzero at least once. This
2764 * validates that psa_generate_random is overwriting every byte of
2765 * the output buffer. */
2766 for( i = 0; i < bytes; i++ )
2767 {
2768 TEST_ASSERT( changed[i] != 0 );
2769 }
Gilles Peskine05d69892018-06-19 22:00:52 +02002770
2771exit:
2772 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02002773 mbedtls_free( output );
2774 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02002775}
2776/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02002777
2778/* BEGIN_CASE */
2779void generate_key( int type_arg,
2780 int bits_arg,
2781 int usage_arg,
2782 int alg_arg,
2783 int expected_status_arg )
2784{
2785 int slot = 1;
2786 psa_key_type_t type = type_arg;
2787 psa_key_usage_t usage = usage_arg;
2788 size_t bits = bits_arg;
2789 psa_algorithm_t alg = alg_arg;
2790 psa_status_t expected_status = expected_status_arg;
2791 psa_key_type_t got_type;
2792 size_t got_bits;
2793 unsigned char exported[616] = {0}; /* enough for a 1024-bit RSA key */
2794 size_t exported_length;
2795 psa_status_t expected_export_status =
2796 usage & PSA_KEY_USAGE_EXPORT ? PSA_SUCCESS : PSA_ERROR_NOT_PERMITTED;
2797 psa_status_t expected_info_status =
2798 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
2799 psa_key_policy_t policy;
2800
2801 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2802
2803 psa_key_policy_init( &policy );
2804 psa_key_policy_set_usage( &policy, usage, alg );
2805 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2806
2807 /* Generate a key */
2808 TEST_ASSERT( psa_generate_key( slot, type, bits,
2809 NULL, 0 ) == expected_status );
2810
2811 /* Test the key information */
2812 TEST_ASSERT( psa_get_key_information( slot,
2813 &got_type,
2814 &got_bits ) == expected_info_status );
2815 if( expected_info_status != PSA_SUCCESS )
2816 goto exit;
2817 TEST_ASSERT( got_type == type );
2818 TEST_ASSERT( got_bits == bits );
2819
2820 /* Export the key */
2821 TEST_ASSERT( psa_export_key( slot,
2822 exported, sizeof( exported ),
2823 &exported_length ) == expected_export_status );
2824 if( expected_export_status == PSA_SUCCESS )
2825 {
Gilles Peskine48c0ea12018-06-21 14:15:31 +02002826 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02002827 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
2828#if defined(MBEDTLS_DES_C)
2829 if( type == PSA_KEY_TYPE_DES )
2830 {
2831 /* Check the parity bits. */
2832 unsigned i;
2833 for( i = 0; i < bits / 8; i++ )
2834 {
2835 unsigned bit_count = 0;
2836 unsigned m;
2837 for( m = 1; m <= 0x100; m <<= 1 )
2838 {
2839 if( exported[i] & m )
2840 ++bit_count;
2841 }
2842 TEST_ASSERT( bit_count % 2 != 0 );
2843 }
2844 }
2845#endif
2846#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
2847 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
2848 {
2849 /* Sanity check: does this look like the beginning of a PKCS#8
2850 * RSA key pair? Assumes bits is a multiple of 8. */
2851 size_t n_bytes = bits / 8 + 1;
2852 size_t n_encoded_bytes;
2853 unsigned char *n_end;
2854 TEST_ASSERT( exported_length >= 7 + ( n_bytes + 3 ) * 9 / 2 );
2855 TEST_ASSERT( exported[0] == 0x30 );
2856 TEST_ASSERT( exported[1] == 0x82 ); // assumes >=416-bit key
2857 TEST_ASSERT( exported[4] == 0x02 );
2858 TEST_ASSERT( exported[5] == 0x01 );
2859 TEST_ASSERT( exported[6] == 0x00 );
2860 TEST_ASSERT( exported[7] == 0x02 );
2861 n_encoded_bytes = exported[8];
2862 n_end = exported + 9 + n_encoded_bytes;
2863 if( n_encoded_bytes & 0x80 )
2864 {
2865 n_encoded_bytes = ( n_encoded_bytes & 0x7f ) << 7;
2866 n_encoded_bytes |= exported[9] & 0x7f;
2867 n_end += 1;
2868 }
2869 /* The encoding of n should start with a 0 byte since it should
2870 * have its high bit set. However Mbed TLS is not compliant and
2871 * generates an invalid, but widely tolerated, encoding of
2872 * positive INTEGERs with a bit size that is a multiple of 8
2873 * with no leading 0 byte. Accept this here. */
2874 TEST_ASSERT( n_bytes == n_encoded_bytes ||
2875 n_bytes == n_encoded_bytes + 1 );
2876 if( n_bytes == n_encoded_bytes )
2877 TEST_ASSERT( exported[n_encoded_bytes <= 127 ? 9 : 10] == 0x00 );
2878 /* Sanity check: e must be 3 */
2879 TEST_ASSERT( n_end[0] == 0x02 );
2880 TEST_ASSERT( n_end[1] == 0x03 );
2881 TEST_ASSERT( n_end[2] == 0x01 );
2882 TEST_ASSERT( n_end[3] == 0x00 );
2883 TEST_ASSERT( n_end[4] == 0x01 );
2884 TEST_ASSERT( n_end[5] == 0x02 );
2885 }
2886#endif /* MBEDTLS_RSA_C */
2887#if defined(MBEDTLS_ECP_C)
2888 if( PSA_KEY_TYPE_IS_ECC( type ) )
2889 {
2890 /* Sanity check: does this look like the beginning of a PKCS#8
2891 * elliptic curve key pair? */
2892 TEST_ASSERT( exported_length >= bits * 3 / 8 + 10 );
2893 TEST_ASSERT( exported[0] == 0x30 );
2894 }
2895#endif /* MBEDTLS_ECP_C */
2896 }
2897
Gilles Peskine818ca122018-06-20 18:16:48 +02002898 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02002899 if( ! exercise_key( slot, usage, alg ) )
2900 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02002901
2902exit:
2903 psa_destroy_key( slot );
2904 mbedtls_psa_crypto_free( );
2905}
2906/* END_CASE */