blob: 278554f7585465c0bfe4291116ea9ea0263660e6 [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
11#if(UINT32_MAX > SIZE_MAX)
Gilles Peskine2d277862018-06-18 15:41:12 +020012#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX )
itayzafrir3e02b3b2018-06-12 17:06:52 +030013#else
Gilles Peskine2d277862018-06-18 15:41:12 +020014#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1
itayzafrir3e02b3b2018-06-12 17:06:52 +030015#endif
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020016
Jaeden Amerof24c7f82018-06-27 17:20:43 +010017/** An invalid export length that will never be set by psa_export_key(). */
18static const size_t INVALID_EXPORT_LENGTH = ~0U;
19
Gilles Peskined35a1cc2018-06-26 21:26:10 +020020/** Test if a buffer is all-bits zero.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020021 *
22 * \param buffer Pointer to the beginning of the buffer.
23 * \param size Size of the buffer in bytes.
24 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020025 * \return 1 if the buffer is all-bits-zero.
26 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020027 */
Gilles Peskine3f669c32018-06-21 09:21:51 +020028static int mem_is_zero( void *buffer, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020029{
30 size_t i;
31 for( i = 0; i < size; i++ )
32 {
33 if( ( (unsigned char *) buffer )[i] != 0 )
Gilles Peskine3f669c32018-06-21 09:21:51 +020034 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020035 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020036 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037}
Gilles Peskine818ca122018-06-20 18:16:48 +020038
Gilles Peskine48c0ea12018-06-21 14:15:31 +020039static int key_type_is_raw_bytes( psa_key_type_t type )
40{
41 psa_key_type_t category = type & PSA_KEY_TYPE_CATEGORY_MASK;
42 return( category == PSA_KEY_TYPE_RAW_DATA ||
43 category == PSA_KEY_TYPE_CATEGORY_SYMMETRIC );
44}
45
Gilles Peskine0b352bc2018-06-28 00:16:11 +020046/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
47static int asn1_write_10x( unsigned char **p,
48 unsigned char *start,
49 size_t bits,
50 unsigned char x )
51{
52 int ret;
53 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020054 if( bits == 0 )
55 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
56 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020057 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
58 if( *p < start || *p - start < (ssize_t) len )
59 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
60 *p -= len;
61 ( *p )[len-1] = x;
62 if( bits % 8 == 0 )
63 ( *p )[1] |= 1;
64 else
65 ( *p )[0] |= 1 << ( bits % 8 );
66 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
67 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
68 MBEDTLS_ASN1_INTEGER ) );
69 return( len );
70}
71
72static int construct_fake_rsa_key( unsigned char *buffer,
73 size_t buffer_size,
74 unsigned char **p,
75 size_t bits,
76 int keypair )
77{
78 size_t half_bits = ( bits + 1 ) / 2;
79 int ret;
80 int len = 0;
81 /* Construct something that looks like a DER encoding of
82 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
83 * RSAPrivateKey ::= SEQUENCE {
84 * version Version,
85 * modulus INTEGER, -- n
86 * publicExponent INTEGER, -- e
87 * privateExponent INTEGER, -- d
88 * prime1 INTEGER, -- p
89 * prime2 INTEGER, -- q
90 * exponent1 INTEGER, -- d mod (p-1)
91 * exponent2 INTEGER, -- d mod (q-1)
92 * coefficient INTEGER, -- (inverse of q) mod p
93 * otherPrimeInfos OtherPrimeInfos OPTIONAL
94 * }
95 * Or, for a public key, the same structure with only
96 * version, modulus and publicExponent.
97 */
98 *p = buffer + buffer_size;
99 if( keypair )
100 {
101 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
102 asn1_write_10x( p, buffer, half_bits, 1 ) );
103 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
104 asn1_write_10x( p, buffer, half_bits, 1 ) );
105 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
106 asn1_write_10x( p, buffer, half_bits, 1 ) );
107 MBEDTLS_ASN1_CHK_ADD( len, /* q */
108 asn1_write_10x( p, buffer, half_bits, 1 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
110 asn1_write_10x( p, buffer, half_bits, 3 ) );
111 MBEDTLS_ASN1_CHK_ADD( len, /* d */
112 asn1_write_10x( p, buffer, bits, 1 ) );
113 }
114 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
115 asn1_write_10x( p, buffer, 17, 1 ) );
116 MBEDTLS_ASN1_CHK_ADD( len, /* n */
117 asn1_write_10x( p, buffer, bits, 1 ) );
118 if( keypair )
119 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
120 mbedtls_asn1_write_int( p, buffer, 0 ) );
121 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
122 {
123 const unsigned char tag =
124 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
125 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
126 }
127 return( len );
128}
129
Gilles Peskine818ca122018-06-20 18:16:48 +0200130static int exercise_mac_key( psa_key_slot_t key,
131 psa_key_usage_t usage,
132 psa_algorithm_t alg )
133{
134 psa_mac_operation_t operation;
135 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200136 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200137 size_t mac_length = sizeof( mac );
138
139 if( usage & PSA_KEY_USAGE_SIGN )
140 {
Gilles Peskine89167cb2018-07-08 20:12:23 +0200141 TEST_ASSERT( psa_mac_sign_setup( &operation,
142 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200143 TEST_ASSERT( psa_mac_update( &operation,
144 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200145 TEST_ASSERT( psa_mac_sign_finish( &operation,
Gilles Peskineef0cb402018-07-12 16:55:59 +0200146 mac, sizeof( mac ),
Gilles Peskineacd4be32018-07-08 19:56:25 +0200147 &mac_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200148 }
149
150 if( usage & PSA_KEY_USAGE_VERIFY )
151 {
152 psa_status_t verify_status =
153 ( usage & PSA_KEY_USAGE_SIGN ?
154 PSA_SUCCESS :
155 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine89167cb2018-07-08 20:12:23 +0200156 TEST_ASSERT( psa_mac_verify_setup( &operation,
157 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200158 TEST_ASSERT( psa_mac_update( &operation,
159 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200160 TEST_ASSERT( psa_mac_verify_finish( &operation,
161 mac,
162 mac_length ) == verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200163 }
164
165 return( 1 );
166
167exit:
168 psa_mac_abort( &operation );
169 return( 0 );
170}
171
172static int exercise_cipher_key( psa_key_slot_t key,
173 psa_key_usage_t usage,
174 psa_algorithm_t alg )
175{
176 psa_cipher_operation_t operation;
177 unsigned char iv[16] = {0};
178 size_t iv_length = sizeof( iv );
179 const unsigned char plaintext[16] = "Hello, world...";
180 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
181 size_t ciphertext_length = sizeof( ciphertext );
182 unsigned char decrypted[sizeof( ciphertext )];
183 size_t part_length;
184
185 if( usage & PSA_KEY_USAGE_ENCRYPT )
186 {
Gilles Peskinefe119512018-07-08 21:39:34 +0200187 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
188 key, alg ) == PSA_SUCCESS );
189 TEST_ASSERT( psa_cipher_generate_iv( &operation,
190 iv, sizeof( iv ),
191 &iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200192 TEST_ASSERT( psa_cipher_update( &operation,
193 plaintext, sizeof( plaintext ),
194 ciphertext, sizeof( ciphertext ),
195 &ciphertext_length ) == PSA_SUCCESS );
196 TEST_ASSERT( psa_cipher_finish( &operation,
197 ciphertext + ciphertext_length,
198 sizeof( ciphertext ) - ciphertext_length,
199 &part_length ) == PSA_SUCCESS );
200 ciphertext_length += part_length;
201 }
202
203 if( usage & PSA_KEY_USAGE_DECRYPT )
204 {
205 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700206 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200207 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
208 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200209 size_t bits;
210 TEST_ASSERT( psa_get_key_information( key, &type, &bits ) );
211 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
212 }
Gilles Peskinefe119512018-07-08 21:39:34 +0200213 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
214 key, alg ) == PSA_SUCCESS );
215 TEST_ASSERT( psa_cipher_set_iv( &operation,
216 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200217 TEST_ASSERT( psa_cipher_update( &operation,
218 ciphertext, ciphertext_length,
219 decrypted, sizeof( decrypted ),
220 &part_length ) == PSA_SUCCESS );
221 status = psa_cipher_finish( &operation,
222 decrypted + part_length,
223 sizeof( decrypted ) - part_length,
224 &part_length );
225 /* For a stream cipher, all inputs are valid. For a block cipher,
226 * if the input is some aribtrary data rather than an actual
227 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700228 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700229 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine818ca122018-06-20 18:16:48 +0200230 TEST_ASSERT( status == PSA_SUCCESS );
231 else
232 TEST_ASSERT( status == PSA_SUCCESS ||
233 status == PSA_ERROR_INVALID_PADDING );
234 }
235
236 return( 1 );
237
238exit:
239 psa_cipher_abort( &operation );
240 return( 0 );
241}
242
243static int exercise_aead_key( psa_key_slot_t key,
244 psa_key_usage_t usage,
245 psa_algorithm_t alg )
246{
247 unsigned char nonce[16] = {0};
248 size_t nonce_length = sizeof( nonce );
249 unsigned char plaintext[16] = "Hello, world...";
250 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
251 size_t ciphertext_length = sizeof( ciphertext );
252 size_t plaintext_length = sizeof( ciphertext );
253
254 if( usage & PSA_KEY_USAGE_ENCRYPT )
255 {
256 TEST_ASSERT( psa_aead_encrypt( key, alg,
257 nonce, nonce_length,
258 NULL, 0,
259 plaintext, sizeof( plaintext ),
260 ciphertext, sizeof( ciphertext ),
261 &ciphertext_length ) == PSA_SUCCESS );
262 }
263
264 if( usage & PSA_KEY_USAGE_DECRYPT )
265 {
266 psa_status_t verify_status =
267 ( usage & PSA_KEY_USAGE_ENCRYPT ?
268 PSA_SUCCESS :
269 PSA_ERROR_INVALID_SIGNATURE );
270 TEST_ASSERT( psa_aead_decrypt( key, alg,
271 nonce, nonce_length,
272 NULL, 0,
273 ciphertext, ciphertext_length,
274 plaintext, sizeof( plaintext ),
275 &plaintext_length ) == verify_status );
276 }
277
278 return( 1 );
279
280exit:
281 return( 0 );
282}
283
284static int exercise_signature_key( psa_key_slot_t key,
285 psa_key_usage_t usage,
286 psa_algorithm_t alg )
287{
Gilles Peskineca45c352018-06-26 16:13:09 +0200288 unsigned char payload[16] = {1};
Gilles Peskine818ca122018-06-20 18:16:48 +0200289 size_t payload_length = sizeof( payload );
Gilles Peskine69c12672018-06-28 00:07:19 +0200290 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200291 size_t signature_length = sizeof( signature );
292
293 if( usage & PSA_KEY_USAGE_SIGN )
294 {
295 TEST_ASSERT( psa_asymmetric_sign( key, alg,
296 payload, payload_length,
297 NULL, 0,
298 signature, sizeof( signature ),
299 &signature_length ) == PSA_SUCCESS );
300 }
301
302 if( usage & PSA_KEY_USAGE_VERIFY )
303 {
304 psa_status_t verify_status =
305 ( usage & PSA_KEY_USAGE_SIGN ?
306 PSA_SUCCESS :
307 PSA_ERROR_INVALID_SIGNATURE );
308 TEST_ASSERT( psa_asymmetric_verify( key, alg,
309 payload, payload_length,
310 NULL, 0,
311 signature, signature_length ) ==
312 verify_status );
313 }
314
315 return( 1 );
316
317exit:
318 return( 0 );
319}
320
321static int exercise_asymmetric_encryption_key( psa_key_slot_t key,
322 psa_key_usage_t usage,
323 psa_algorithm_t alg )
324{
325 unsigned char plaintext[256] = "Hello, world...";
326 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
327 size_t ciphertext_length = sizeof( ciphertext );
328 size_t plaintext_length = 16;
329
330 if( usage & PSA_KEY_USAGE_ENCRYPT )
331 {
332 TEST_ASSERT(
333 psa_asymmetric_encrypt( key, alg,
334 plaintext, plaintext_length,
335 NULL, 0,
336 ciphertext, sizeof( ciphertext ),
337 &ciphertext_length ) == PSA_SUCCESS );
338 }
339
340 if( usage & PSA_KEY_USAGE_DECRYPT )
341 {
342 psa_status_t status =
343 psa_asymmetric_decrypt( key, alg,
344 ciphertext, ciphertext_length,
345 NULL, 0,
346 plaintext, sizeof( plaintext ),
347 &plaintext_length );
348 TEST_ASSERT( status == PSA_SUCCESS ||
349 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
350 ( status == PSA_ERROR_INVALID_ARGUMENT ||
351 status == PSA_ERROR_INVALID_PADDING ) ) );
352 }
353
354 return( 1 );
355
356exit:
357 return( 0 );
358}
Gilles Peskine02b75072018-07-01 22:31:34 +0200359
Gilles Peskineea0fb492018-07-12 17:17:20 +0200360static int exercise_key_derivation_key( psa_key_slot_t key,
361 psa_key_usage_t usage,
362 psa_algorithm_t alg )
363{
364 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
365 unsigned char label[16] = "This is a label.";
366 size_t label_length = sizeof( label );
367 unsigned char seed[16] = "abcdefghijklmnop";
368 size_t seed_length = sizeof( seed );
369 unsigned char output[1];
370
371 if( usage & PSA_KEY_USAGE_DERIVE )
372 {
373 TEST_ASSERT( psa_key_derivation( &generator,
374 key, alg,
375 label, label_length,
376 seed, seed_length,
377 sizeof( output ) ) == PSA_SUCCESS );
378 TEST_ASSERT( psa_generator_read( &generator,
379 output,
380 sizeof( output ) ) == PSA_SUCCESS );
381 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
382 }
383
384 return( 1 );
385
386exit:
387 return( 0 );
388}
389
Gilles Peskine02b75072018-07-01 22:31:34 +0200390static int exercise_key( psa_key_slot_t slot,
391 psa_key_usage_t usage,
392 psa_algorithm_t alg )
393{
394 int ok;
395 if( alg == 0 )
396 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
397 else if( PSA_ALG_IS_MAC( alg ) )
398 ok = exercise_mac_key( slot, usage, alg );
399 else if( PSA_ALG_IS_CIPHER( alg ) )
400 ok = exercise_cipher_key( slot, usage, alg );
401 else if( PSA_ALG_IS_AEAD( alg ) )
402 ok = exercise_aead_key( slot, usage, alg );
403 else if( PSA_ALG_IS_SIGN( alg ) )
404 ok = exercise_signature_key( slot, usage, alg );
405 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
406 ok = exercise_asymmetric_encryption_key( slot, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200407 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
408 ok = exercise_key_derivation_key( slot, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200409 else
410 {
411 char message[40];
412 mbedtls_snprintf( message, sizeof( message ),
413 "No code to exercise alg=0x%08lx",
414 (unsigned long) alg );
415 test_fail( message, __LINE__, __FILE__ );
416 ok = 0;
417 }
418 return( ok );
419}
420
Gilles Peskinee59236f2018-01-27 23:32:46 +0100421/* END_HEADER */
422
423/* BEGIN_DEPENDENCIES
424 * depends_on:MBEDTLS_PSA_CRYPTO_C
425 * END_DEPENDENCIES
426 */
427
428/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200429void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100430{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100431 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100432 int i;
433 for( i = 0; i <= 1; i++ )
434 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100435 status = psa_crypto_init( );
436 TEST_ASSERT( status == PSA_SUCCESS );
437 status = psa_crypto_init( );
438 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100439 mbedtls_psa_crypto_free( );
440 }
441}
442/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100443
444/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200445void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100446{
447 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200448 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100449 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100450
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100451 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300452 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100453 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
454
Gilles Peskine4abf7412018-06-18 16:35:34 +0200455 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200456 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100457 if( status == PSA_SUCCESS )
458 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
459
460exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100461 mbedtls_psa_crypto_free( );
462}
463/* END_CASE */
464
465/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200466void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
467{
468 int slot = 1;
469 size_t bits = bits_arg;
470 psa_status_t expected_status = expected_status_arg;
471 psa_status_t status;
472 psa_key_type_t type =
473 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
474 size_t buffer_size = /* Slight overapproximations */
475 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
476 unsigned char *buffer = mbedtls_calloc( 1, buffer_size );
477 unsigned char *p;
478 int ret;
479 size_t length;
480
481 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
482 TEST_ASSERT( buffer != NULL );
483
484 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
485 bits, keypair ) ) >= 0 );
486 length = ret;
487
488 /* Try importing the key */
489 status = psa_import_key( slot, type, p, length );
490 TEST_ASSERT( status == expected_status );
491 if( status == PSA_SUCCESS )
492 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
493
494exit:
495 mbedtls_free( buffer );
496 mbedtls_psa_crypto_free( );
497}
498/* END_CASE */
499
500/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300501void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300502 int type_arg,
503 int alg_arg,
504 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100505 int expected_bits,
506 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200507 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100508 int canonical_input )
509{
510 int slot = 1;
511 int slot2 = slot + 1;
512 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200513 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200514 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100515 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100516 unsigned char *exported = NULL;
517 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100518 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100519 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100520 size_t reexported_length;
521 psa_key_type_t got_type;
522 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200523 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100524
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100525 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300526 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
527 export_size = (ssize_t) data->len + export_size_delta;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100528 exported = mbedtls_calloc( 1, export_size );
529 TEST_ASSERT( exported != NULL );
530 if( ! canonical_input )
531 {
532 reexported = mbedtls_calloc( 1, export_size );
533 TEST_ASSERT( reexported != NULL );
534 }
535 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
536
mohammad1603a97cb8c2018-03-28 03:46:26 -0700537 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200538 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700539 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
540
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100541 /* Import the key */
542 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200543 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100544
545 /* Test the key information */
546 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200547 &got_type,
548 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100549 TEST_ASSERT( got_type == type );
550 TEST_ASSERT( got_bits == (size_t) expected_bits );
551
552 /* Export the key */
553 status = psa_export_key( slot,
554 exported, export_size,
555 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200556 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100557
558 /* The exported length must be set by psa_export_key() to a value between 0
559 * and export_size. On errors, the exported length must be 0. */
560 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
561 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
562 TEST_ASSERT( exported_length <= export_size );
563
Gilles Peskine3f669c32018-06-21 09:21:51 +0200564 TEST_ASSERT( mem_is_zero( exported + exported_length,
565 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100566 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200567 {
568 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100569 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200570 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100571
572 if( canonical_input )
573 {
Gilles Peskine4abf7412018-06-18 16:35:34 +0200574 TEST_ASSERT( exported_length == data->len );
575 TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100576 }
577 else
578 {
mohammad1603a97cb8c2018-03-28 03:46:26 -0700579 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
580
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100581 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200582 exported,
583 export_size ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100584 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200585 reexported,
586 export_size,
587 &reexported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100588 TEST_ASSERT( reexported_length == exported_length );
589 TEST_ASSERT( memcmp( reexported, exported,
590 exported_length ) == 0 );
591 }
592
593destroy:
594 /* Destroy the key */
595 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
596 TEST_ASSERT( psa_get_key_information(
597 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
598
599exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300600 mbedtls_free( exported );
601 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100602 mbedtls_psa_crypto_free( );
603}
604/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100605
Moran Pekerf709f4a2018-06-06 17:26:04 +0300606/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300607void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200608 int type_arg,
609 int alg_arg,
610 int expected_bits,
611 int public_key_expected_length,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200612 int expected_export_status_arg )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300613{
614 int slot = 1;
615 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200616 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200617 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300618 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300619 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300620 size_t export_size;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100621 size_t exported_length = INVALID_EXPORT_LENGTH;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300622 psa_key_type_t got_type;
623 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200624 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300625
Moran Pekerf709f4a2018-06-06 17:26:04 +0300626 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300627 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
628 export_size = (ssize_t) data->len;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300629 exported = mbedtls_calloc( 1, export_size );
630 TEST_ASSERT( exported != NULL );
631
632 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
633
634 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200635 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300636 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
637
638 /* Import the key */
639 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200640 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300641
642 /* Test the key information */
643 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200644 &got_type,
645 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300646 TEST_ASSERT( got_type == type );
647 TEST_ASSERT( got_bits == (size_t) expected_bits );
648
649 /* Export the key */
650 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200651 exported, export_size,
652 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200653 TEST_ASSERT( status == expected_export_status );
Jaeden Amero2a671e92018-06-27 17:47:40 +0100654 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
655 TEST_ASSERT( mem_is_zero( exported + exported_length,
656 export_size - exported_length ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300657 if( status != PSA_SUCCESS )
658 goto destroy;
659
Moran Pekerf709f4a2018-06-06 17:26:04 +0300660destroy:
661 /* Destroy the key */
662 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
663 TEST_ASSERT( psa_get_key_information(
664 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
665
666exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300667 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300668 mbedtls_psa_crypto_free( );
669}
670/* END_CASE */
671
Gilles Peskine20035e32018-02-03 22:44:14 +0100672/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200673void import_and_exercise_key( data_t *data,
674 int type_arg,
675 int bits_arg,
676 int alg_arg )
677{
678 int slot = 1;
679 psa_key_type_t type = type_arg;
680 size_t bits = bits_arg;
681 psa_algorithm_t alg = alg_arg;
682 psa_key_usage_t usage =
683 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
684 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
685 PSA_KEY_USAGE_VERIFY :
686 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
687 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
688 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
689 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
690 PSA_KEY_USAGE_ENCRYPT :
691 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
Gilles Peskineea0fb492018-07-12 17:17:20 +0200692 PSA_ALG_IS_KEY_DERIVATION( alg ) ? PSA_KEY_USAGE_DERIVE :
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200693 0 );
694 psa_key_policy_t policy;
695 psa_key_type_t got_type;
696 size_t got_bits;
697 psa_status_t status;
698
699 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
700
701 psa_key_policy_init( &policy );
702 psa_key_policy_set_usage( &policy, usage, alg );
703 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
704
705 /* Import the key */
706 status = psa_import_key( slot, type, data->x, data->len );
707 TEST_ASSERT( status == PSA_SUCCESS );
708
709 /* Test the key information */
710 TEST_ASSERT( psa_get_key_information( slot,
711 &got_type,
712 &got_bits ) == PSA_SUCCESS );
713 TEST_ASSERT( got_type == type );
714 TEST_ASSERT( got_bits == bits );
715
716 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +0200717 if( ! exercise_key( slot, usage, alg ) )
718 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200719
720exit:
721 psa_destroy_key( slot );
722 mbedtls_psa_crypto_free( );
723}
724/* END_CASE */
725
726/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +0200727void key_policy( int usage_arg, int alg_arg )
728{
729 int key_slot = 1;
730 psa_algorithm_t alg = alg_arg;
731 psa_key_usage_t usage = usage_arg;
732 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
733 unsigned char key[32] = {0};
734 psa_key_policy_t policy_set;
735 psa_key_policy_t policy_get;
736
737 memset( key, 0x2a, sizeof( key ) );
738
739 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
740
741 psa_key_policy_init( &policy_set );
742 psa_key_policy_init( &policy_get );
743
744 psa_key_policy_set_usage( &policy_set, usage, alg );
745
746 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
747 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
748 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
749
750 TEST_ASSERT( psa_import_key( key_slot, key_type,
751 key, sizeof( key ) ) == PSA_SUCCESS );
752
753 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
754
755 TEST_ASSERT( policy_get.usage == policy_set.usage );
756 TEST_ASSERT( policy_get.alg == policy_set.alg );
757
758exit:
759 psa_destroy_key( key_slot );
760 mbedtls_psa_crypto_free( );
761}
762/* END_CASE */
763
764/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200765void mac_key_policy( int policy_usage,
766 int policy_alg,
767 int key_type,
768 data_t *key_data,
769 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200770{
771 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +0200772 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200773 psa_mac_operation_t operation;
774 psa_status_t status;
775 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200776
777 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
778
779 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200780 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200781 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
782
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200783 TEST_ASSERT( psa_import_key( key_slot, key_type,
784 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +0200785
Gilles Peskine89167cb2018-07-08 20:12:23 +0200786 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200787 if( policy_alg == exercise_alg &&
788 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
789 TEST_ASSERT( status == PSA_SUCCESS );
790 else
791 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
792 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200793
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200794 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +0200795 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200796 if( policy_alg == exercise_alg &&
797 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +0200798 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200799 else
800 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
801
802exit:
803 psa_mac_abort( &operation );
804 psa_destroy_key( key_slot );
805 mbedtls_psa_crypto_free( );
806}
807/* END_CASE */
808
809/* BEGIN_CASE */
810void cipher_key_policy( int policy_usage,
811 int policy_alg,
812 int key_type,
813 data_t *key_data,
814 int exercise_alg )
815{
816 int key_slot = 1;
817 psa_key_policy_t policy;
818 psa_cipher_operation_t operation;
819 psa_status_t status;
820
821 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
822
823 psa_key_policy_init( &policy );
824 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
825 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
826
827 TEST_ASSERT( psa_import_key( key_slot, key_type,
828 key_data->x, key_data->len ) == PSA_SUCCESS );
829
Gilles Peskinefe119512018-07-08 21:39:34 +0200830 status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200831 if( policy_alg == exercise_alg &&
832 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
833 TEST_ASSERT( status == PSA_SUCCESS );
834 else
835 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
836 psa_cipher_abort( &operation );
837
Gilles Peskinefe119512018-07-08 21:39:34 +0200838 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200839 if( policy_alg == exercise_alg &&
840 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
841 TEST_ASSERT( status == PSA_SUCCESS );
842 else
843 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
844
845exit:
846 psa_cipher_abort( &operation );
847 psa_destroy_key( key_slot );
848 mbedtls_psa_crypto_free( );
849}
850/* END_CASE */
851
852/* BEGIN_CASE */
853void aead_key_policy( int policy_usage,
854 int policy_alg,
855 int key_type,
856 data_t *key_data,
857 int nonce_length_arg,
858 int tag_length_arg,
859 int exercise_alg )
860{
861 int key_slot = 1;
862 psa_key_policy_t policy;
863 psa_status_t status;
864 unsigned char nonce[16] = {0};
865 size_t nonce_length = nonce_length_arg;
866 unsigned char tag[16];
867 size_t tag_length = tag_length_arg;
868 size_t output_length;
869
870 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
871 TEST_ASSERT( tag_length <= sizeof( tag ) );
872
873 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
874
875 psa_key_policy_init( &policy );
876 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
877 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
878
879 TEST_ASSERT( psa_import_key( key_slot, key_type,
880 key_data->x, key_data->len ) == PSA_SUCCESS );
881
882 status = psa_aead_encrypt( key_slot, exercise_alg,
883 nonce, nonce_length,
884 NULL, 0,
885 NULL, 0,
886 tag, tag_length,
887 &output_length );
888 if( policy_alg == exercise_alg &&
889 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
890 TEST_ASSERT( status == PSA_SUCCESS );
891 else
892 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
893
894 memset( tag, 0, sizeof( tag ) );
895 status = psa_aead_decrypt( key_slot, exercise_alg,
896 nonce, nonce_length,
897 NULL, 0,
898 tag, tag_length,
899 NULL, 0,
900 &output_length );
901 if( policy_alg == exercise_alg &&
902 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
903 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
904 else
905 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
906
907exit:
908 psa_destroy_key( key_slot );
909 mbedtls_psa_crypto_free( );
910}
911/* END_CASE */
912
913/* BEGIN_CASE */
914void asymmetric_encryption_key_policy( int policy_usage,
915 int policy_alg,
916 int key_type,
917 data_t *key_data,
918 int exercise_alg )
919{
920 int key_slot = 1;
921 psa_key_policy_t policy;
922 psa_status_t status;
923 size_t key_bits;
924 size_t buffer_length;
925 unsigned char *buffer = NULL;
926 size_t output_length;
927
928 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
929
930 psa_key_policy_init( &policy );
931 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
932 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
933
934 TEST_ASSERT( psa_import_key( key_slot, key_type,
935 key_data->x, key_data->len ) == PSA_SUCCESS );
936
937 TEST_ASSERT( psa_get_key_information( key_slot,
938 NULL,
939 &key_bits ) == PSA_SUCCESS );
940 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
941 exercise_alg );
942 buffer = mbedtls_calloc( 1, buffer_length );
943 TEST_ASSERT( buffer != NULL );
944
945 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
946 NULL, 0,
947 NULL, 0,
948 buffer, buffer_length,
949 &output_length );
950 if( policy_alg == exercise_alg &&
951 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
952 TEST_ASSERT( status == PSA_SUCCESS );
953 else
954 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
955
956 memset( buffer, 0, buffer_length );
957 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
958 buffer, buffer_length,
959 NULL, 0,
960 buffer, buffer_length,
961 &output_length );
962 if( policy_alg == exercise_alg &&
963 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
964 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
965 else
966 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
967
968exit:
969 psa_destroy_key( key_slot );
970 mbedtls_psa_crypto_free( );
971 mbedtls_free( buffer );
972}
973/* END_CASE */
974
975/* BEGIN_CASE */
976void asymmetric_signature_key_policy( int policy_usage,
977 int policy_alg,
978 int key_type,
979 data_t *key_data,
980 int exercise_alg )
981{
982 int key_slot = 1;
983 psa_key_policy_t policy;
984 psa_status_t status;
985 unsigned char payload[16] = {1};
986 size_t payload_length = sizeof( payload );
987 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
988 size_t signature_length;
989
990 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
991
992 psa_key_policy_init( &policy );
993 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
994 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
995
996 TEST_ASSERT( psa_import_key( key_slot, key_type,
997 key_data->x, key_data->len ) == PSA_SUCCESS );
998
999 status = psa_asymmetric_sign( key_slot, exercise_alg,
1000 payload, payload_length,
1001 NULL, 0,
1002 signature, sizeof( signature ),
1003 &signature_length );
1004 if( policy_alg == exercise_alg &&
1005 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1006 TEST_ASSERT( status == PSA_SUCCESS );
1007 else
1008 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1009
1010 memset( signature, 0, sizeof( signature ) );
1011 status = psa_asymmetric_verify( key_slot, exercise_alg,
1012 payload, payload_length,
1013 NULL, 0,
1014 signature, sizeof( signature ) );
1015 if( policy_alg == exercise_alg &&
1016 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1017 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1018 else
1019 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001020
1021exit:
1022 psa_destroy_key( key_slot );
1023 mbedtls_psa_crypto_free( );
1024}
1025/* END_CASE */
1026
1027/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001028void derive_key_policy( int policy_usage,
1029 int policy_alg,
1030 int key_type,
1031 data_t *key_data,
1032 int exercise_alg )
1033{
1034 int key_slot = 1;
1035 psa_key_policy_t policy;
1036 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1037 psa_status_t status;
1038
1039 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1040
1041 psa_key_policy_init( &policy );
1042 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1043 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1044
1045 TEST_ASSERT( psa_import_key( key_slot, key_type,
1046 key_data->x, key_data->len ) == PSA_SUCCESS );
1047
1048 status = psa_key_derivation( &generator, key_slot,
1049 exercise_alg,
1050 NULL, 0,
1051 NULL, 0,
1052 1 );
1053 if( policy_alg == exercise_alg &&
1054 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1055 TEST_ASSERT( status == PSA_SUCCESS );
1056 else
1057 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1058
1059exit:
1060 psa_generator_abort( &generator );
1061 psa_destroy_key( key_slot );
1062 mbedtls_psa_crypto_free( );
1063}
1064/* END_CASE */
1065
1066/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001067void key_lifetime( int lifetime_arg )
1068{
1069 int key_slot = 1;
1070 psa_key_type_t key_type = PSA_ALG_CBC_BASE;
1071 unsigned char key[32] = {0};
1072 psa_key_lifetime_t lifetime_set = lifetime_arg;
1073 psa_key_lifetime_t lifetime_get;
1074
1075 memset( key, 0x2a, sizeof( key ) );
1076
1077 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1078
1079 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1080 lifetime_set ) == PSA_SUCCESS );
1081
1082 TEST_ASSERT( psa_import_key( key_slot, key_type,
1083 key, sizeof( key ) ) == PSA_SUCCESS );
1084
1085 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1086 &lifetime_get ) == PSA_SUCCESS );
1087
1088 TEST_ASSERT( lifetime_get == lifetime_set );
1089
1090exit:
1091 psa_destroy_key( key_slot );
1092 mbedtls_psa_crypto_free( );
1093}
1094/* END_CASE */
1095
1096/* BEGIN_CASE */
1097void key_lifetime_set_fail( int key_slot_arg,
1098 int lifetime_arg,
1099 int expected_status_arg )
1100{
1101 psa_key_slot_t key_slot = key_slot_arg;
1102 psa_key_lifetime_t lifetime_set = lifetime_arg;
1103 psa_status_t actual_status;
1104 psa_status_t expected_status = expected_status_arg;
1105
1106 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1107
1108 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1109
1110 if( actual_status == PSA_SUCCESS )
1111 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1112
1113 TEST_ASSERT( expected_status == actual_status );
1114
1115exit:
1116 psa_destroy_key( key_slot );
1117 mbedtls_psa_crypto_free( );
1118}
1119/* END_CASE */
1120
1121/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001122void hash_setup( int alg_arg,
1123 int expected_status_arg )
1124{
1125 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001126 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001127 psa_hash_operation_t operation;
1128 psa_status_t status;
1129
1130 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1131
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001132 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001133 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001134 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001135
1136exit:
1137 mbedtls_psa_crypto_free( );
1138}
1139/* END_CASE */
1140
1141/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001142void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001143{
1144 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001145 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001146 size_t actual_hash_length;
1147 psa_hash_operation_t operation;
1148
Gilles Peskine69c12672018-06-28 00:07:19 +02001149 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1150 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1151
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001152 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001153 TEST_ASSERT( expected_hash != NULL );
1154 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1155 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001156
1157 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1158
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001159 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001160 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001161 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001162 TEST_ASSERT( psa_hash_finish( &operation,
1163 actual_hash, sizeof( actual_hash ),
1164 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001165 TEST_ASSERT( actual_hash_length == expected_hash->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001166 TEST_ASSERT( memcmp( expected_hash->x, actual_hash,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001167 expected_hash->len ) == 0 );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001168
1169exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001170 mbedtls_psa_crypto_free( );
1171}
1172/* END_CASE */
1173
1174/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001175void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001176{
1177 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001178 psa_hash_operation_t operation;
1179
Gilles Peskine69c12672018-06-28 00:07:19 +02001180 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1181 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1182
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001183 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001184 TEST_ASSERT( expected_hash != NULL );
1185 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1186 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001187
1188 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1189
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001190 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001191 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001192 input->x,
1193 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001194 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001195 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001196 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001197
1198exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001199 mbedtls_psa_crypto_free( );
1200}
1201/* END_CASE */
1202
1203/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001204void mac_setup( int key_type_arg,
1205 data_t *key,
1206 int alg_arg,
1207 int expected_status_arg )
1208{
1209 int key_slot = 1;
1210 psa_key_type_t key_type = key_type_arg;
1211 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001212 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001213 psa_mac_operation_t operation;
1214 psa_key_policy_t policy;
1215 psa_status_t status;
1216
1217 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1218
1219 psa_key_policy_init( &policy );
1220 psa_key_policy_set_usage( &policy,
1221 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1222 alg );
1223 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1224
1225 TEST_ASSERT( psa_import_key( key_slot, key_type,
1226 key->x, key->len ) == PSA_SUCCESS );
1227
Gilles Peskine89167cb2018-07-08 20:12:23 +02001228 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001229 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001230 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001231
1232exit:
1233 psa_destroy_key( key_slot );
1234 mbedtls_psa_crypto_free( );
1235}
1236/* END_CASE */
1237
1238/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001239void mac_verify( int key_type_arg,
1240 data_t *key,
1241 int alg_arg,
1242 data_t *input,
1243 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001244{
1245 int key_slot = 1;
1246 psa_key_type_t key_type = key_type_arg;
1247 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001248 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001249 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001250
Gilles Peskine69c12672018-06-28 00:07:19 +02001251 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1252
Gilles Peskine8c9def32018-02-08 10:02:12 +01001253 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001254 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001255 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001256 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001257 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1258 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001259
1260 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1261
mohammad16036df908f2018-04-02 08:34:15 -07001262 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001263 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07001264 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1265
Gilles Peskine8c9def32018-02-08 10:02:12 +01001266 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001267 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001268
Gilles Peskine89167cb2018-07-08 20:12:23 +02001269 TEST_ASSERT( psa_mac_verify_setup( &operation,
1270 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001271 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1272 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001273 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02001274 TEST_ASSERT( psa_mac_verify_finish( &operation,
1275 expected_mac->x,
1276 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001277
1278exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001279 psa_destroy_key( key_slot );
1280 mbedtls_psa_crypto_free( );
1281}
1282/* END_CASE */
1283
1284/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001285void cipher_setup( int key_type_arg,
1286 data_t *key,
1287 int alg_arg,
1288 int expected_status_arg )
1289{
1290 int key_slot = 1;
1291 psa_key_type_t key_type = key_type_arg;
1292 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001293 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001294 psa_cipher_operation_t operation;
1295 psa_key_policy_t policy;
1296 psa_status_t status;
1297
1298 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1299
1300 psa_key_policy_init( &policy );
1301 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1302 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1303
1304 TEST_ASSERT( psa_import_key( key_slot, key_type,
1305 key->x, key->len ) == PSA_SUCCESS );
1306
Gilles Peskinefe119512018-07-08 21:39:34 +02001307 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001308 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001309 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001310
1311exit:
1312 psa_destroy_key( key_slot );
1313 mbedtls_psa_crypto_free( );
1314}
1315/* END_CASE */
1316
1317/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001318void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001319 data_t *key,
1320 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001321 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001322{
1323 int key_slot = 1;
1324 psa_status_t status;
1325 psa_key_type_t key_type = key_type_arg;
1326 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001327 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001328 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001329 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001330 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001331 size_t output_buffer_size = 0;
1332 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001333 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001334 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001335 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001336
Gilles Peskine50e586b2018-06-08 14:28:46 +02001337 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001338 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001339 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001340 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1341 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1342 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001343
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001344 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1345 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001346
1347 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1348
Moran Pekered346952018-07-05 15:22:45 +03001349 psa_key_policy_init( &policy );
1350 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1351 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1352
Gilles Peskine50e586b2018-06-08 14:28:46 +02001353 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001354 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001355
Gilles Peskinefe119512018-07-08 21:39:34 +02001356 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1357 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001358
Gilles Peskinefe119512018-07-08 21:39:34 +02001359 TEST_ASSERT( psa_cipher_set_iv( &operation,
1360 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001361 output_buffer_size = (size_t) input->len +
1362 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001363 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001364 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001365
Gilles Peskine4abf7412018-06-18 16:35:34 +02001366 TEST_ASSERT( psa_cipher_update( &operation,
1367 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001368 output, output_buffer_size,
1369 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001370 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001371 status = psa_cipher_finish( &operation,
1372 output + function_output_length,
1373 output_buffer_size,
1374 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001375 total_output_length += function_output_length;
1376
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001377 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001378 if( expected_status == PSA_SUCCESS )
1379 {
1380 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001381 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001382 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001383 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001384 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001385
Gilles Peskine50e586b2018-06-08 14:28:46 +02001386exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001387 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001388 psa_destroy_key( key_slot );
1389 mbedtls_psa_crypto_free( );
1390}
1391/* END_CASE */
1392
1393/* BEGIN_CASE */
1394void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001395 data_t *key,
1396 data_t *input,
1397 int first_part_size,
1398 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001399{
1400 int key_slot = 1;
1401 psa_key_type_t key_type = key_type_arg;
1402 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001403 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001404 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001405 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001406 size_t output_buffer_size = 0;
1407 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001408 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001409 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001410 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001411
Gilles Peskine50e586b2018-06-08 14:28:46 +02001412 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001413 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001414 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001415 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1416 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1417 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001418
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001419 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1420 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001421
1422 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1423
Moran Pekered346952018-07-05 15:22:45 +03001424 psa_key_policy_init( &policy );
1425 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1426 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1427
Gilles Peskine50e586b2018-06-08 14:28:46 +02001428 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001429 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001430
Gilles Peskinefe119512018-07-08 21:39:34 +02001431 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1432 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001433
Gilles Peskinefe119512018-07-08 21:39:34 +02001434 TEST_ASSERT( psa_cipher_set_iv( &operation,
1435 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001436 output_buffer_size = (size_t) input->len +
1437 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001438 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001439 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001440
Gilles Peskine4abf7412018-06-18 16:35:34 +02001441 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001442 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001443 output, output_buffer_size,
1444 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001445 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001446 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001447 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001448 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001449 output, output_buffer_size,
1450 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001451 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001452 TEST_ASSERT( psa_cipher_finish( &operation,
1453 output + function_output_length,
1454 output_buffer_size,
1455 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001456 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001457 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1458
Gilles Peskine4abf7412018-06-18 16:35:34 +02001459 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001460 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001461 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001462
1463exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001464 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001465 psa_destroy_key( key_slot );
1466 mbedtls_psa_crypto_free( );
1467}
1468/* END_CASE */
1469
1470/* BEGIN_CASE */
1471void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001472 data_t *key,
1473 data_t *input,
1474 int first_part_size,
1475 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001476{
1477 int key_slot = 1;
1478
1479 psa_key_type_t key_type = key_type_arg;
1480 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001481 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001482 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001483 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001484 size_t output_buffer_size = 0;
1485 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001486 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001487 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001488 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001489
Gilles Peskine50e586b2018-06-08 14:28:46 +02001490 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001491 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001492 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001493 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1494 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1495 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001496
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001497 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1498 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001499
1500 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1501
Moran Pekered346952018-07-05 15:22:45 +03001502 psa_key_policy_init( &policy );
1503 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1504 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1505
Gilles Peskine50e586b2018-06-08 14:28:46 +02001506 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001507 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001508
Gilles Peskinefe119512018-07-08 21:39:34 +02001509 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1510 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001511
Gilles Peskinefe119512018-07-08 21:39:34 +02001512 TEST_ASSERT( psa_cipher_set_iv( &operation,
1513 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001514
mohammad16033d91abe2018-07-03 13:15:54 +03001515 output_buffer_size = (size_t) input->len +
1516 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001517 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001518 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001519
Gilles Peskine4abf7412018-06-18 16:35:34 +02001520 TEST_ASSERT( (unsigned int) first_part_size < input->len );
1521 TEST_ASSERT( psa_cipher_update( &operation,
1522 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001523 output, output_buffer_size,
1524 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001525 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001526 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001527 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001528 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001529 output, output_buffer_size,
1530 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001531 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001532 TEST_ASSERT( psa_cipher_finish( &operation,
1533 output + function_output_length,
1534 output_buffer_size,
1535 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001536 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001537 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1538
Gilles Peskine4abf7412018-06-18 16:35:34 +02001539 TEST_ASSERT( total_output_length == expected_output->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001540 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001541 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001542
1543exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001544 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001545 psa_destroy_key( key_slot );
1546 mbedtls_psa_crypto_free( );
1547}
1548/* END_CASE */
1549
Gilles Peskine50e586b2018-06-08 14:28:46 +02001550/* BEGIN_CASE */
1551void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001552 data_t *key,
1553 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001554 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001555{
1556 int key_slot = 1;
1557 psa_status_t status;
1558 psa_key_type_t key_type = key_type_arg;
1559 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001560 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001561 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001562 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001563 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001564 size_t output_buffer_size = 0;
1565 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001566 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001567 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001568 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001569
Gilles Peskine50e586b2018-06-08 14:28:46 +02001570 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001571 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001572 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001573 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1574 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1575 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001576
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001577 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1578 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001579
1580 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1581
Moran Pekered346952018-07-05 15:22:45 +03001582 psa_key_policy_init( &policy );
1583 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1584 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1585
Gilles Peskine50e586b2018-06-08 14:28:46 +02001586 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001587 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001588
Gilles Peskinefe119512018-07-08 21:39:34 +02001589 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1590 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001591
Gilles Peskinefe119512018-07-08 21:39:34 +02001592 TEST_ASSERT( psa_cipher_set_iv( &operation,
1593 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001594
mohammad16033d91abe2018-07-03 13:15:54 +03001595 output_buffer_size = (size_t) input->len +
1596 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001597 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001598 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001599
Gilles Peskine4abf7412018-06-18 16:35:34 +02001600 TEST_ASSERT( psa_cipher_update( &operation,
1601 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001602 output, output_buffer_size,
1603 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001604 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001605 status = psa_cipher_finish( &operation,
1606 output + function_output_length,
1607 output_buffer_size,
1608 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001609 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001610 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001611
1612 if( expected_status == PSA_SUCCESS )
1613 {
1614 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001615 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001616 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001617 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001618 }
1619
Gilles Peskine50e586b2018-06-08 14:28:46 +02001620exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001621 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001622 psa_destroy_key( key_slot );
1623 mbedtls_psa_crypto_free( );
1624}
1625/* END_CASE */
1626
Gilles Peskine50e586b2018-06-08 14:28:46 +02001627/* BEGIN_CASE */
1628void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001629 data_t *key,
1630 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02001631{
1632 int key_slot = 1;
1633 psa_key_type_t key_type = key_type_arg;
1634 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07001635 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02001636 size_t iv_size = 16;
1637 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001638 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001639 size_t output1_size = 0;
1640 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001641 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001642 size_t output2_size = 0;
1643 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001644 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001645 psa_cipher_operation_t operation1;
1646 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03001647 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001648
mohammad1603d7d7ba52018-03-12 18:51:53 +02001649 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001650 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001651 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1652 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001653
mohammad1603d7d7ba52018-03-12 18:51:53 +02001654 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1655
Moran Pekered346952018-07-05 15:22:45 +03001656 psa_key_policy_init( &policy );
1657 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
1658 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1659
mohammad1603d7d7ba52018-03-12 18:51:53 +02001660 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001661 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001662
Gilles Peskinefe119512018-07-08 21:39:34 +02001663 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
1664 key_slot, alg ) == PSA_SUCCESS );
1665 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
1666 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001667
Gilles Peskinefe119512018-07-08 21:39:34 +02001668 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
1669 iv, iv_size,
1670 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001671 output1_size = (size_t) input->len +
1672 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001673 output1 = mbedtls_calloc( 1, output1_size );
1674 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001675
Gilles Peskine4abf7412018-06-18 16:35:34 +02001676 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001677 output1, output1_size,
1678 &output1_length ) == PSA_SUCCESS );
1679 TEST_ASSERT( psa_cipher_finish( &operation1,
1680 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001681 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001682
Gilles Peskine048b7f02018-06-08 14:20:49 +02001683 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001684
1685 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1686
1687 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001688 output2 = mbedtls_calloc( 1, output2_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001689 TEST_ASSERT( output2 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001690
Gilles Peskinefe119512018-07-08 21:39:34 +02001691 TEST_ASSERT( psa_cipher_set_iv( &operation2,
1692 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001693 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
1694 output2, output2_size,
1695 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001696 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001697 TEST_ASSERT( psa_cipher_finish( &operation2,
1698 output2 + output2_length,
1699 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001700 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001701
Gilles Peskine048b7f02018-06-08 14:20:49 +02001702 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001703
Janos Follath25c4fa82018-07-06 16:23:25 +01001704 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001705
Gilles Peskine4abf7412018-06-18 16:35:34 +02001706 TEST_ASSERT( input->len == output2_length );
1707 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +03001708
1709exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001710 mbedtls_free( output1 );
1711 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03001712 psa_destroy_key( key_slot );
1713 mbedtls_psa_crypto_free( );
1714}
1715/* END_CASE */
1716
1717/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001718void cipher_verify_output_multipart( int alg_arg,
1719 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001720 data_t *key,
1721 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001722 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03001723{
1724 int key_slot = 1;
1725 psa_key_type_t key_type = key_type_arg;
1726 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03001727 unsigned char iv[16] = {0};
1728 size_t iv_size = 16;
1729 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001730 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001731 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001732 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001733 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001734 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001735 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001736 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001737 psa_cipher_operation_t operation1;
1738 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03001739 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03001740
Moran Pekerded84402018-06-06 16:36:50 +03001741 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001742 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001743 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1744 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001745
Moran Pekerded84402018-06-06 16:36:50 +03001746 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1747
Moran Pekered346952018-07-05 15:22:45 +03001748 psa_key_policy_init( &policy );
1749 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
1750 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1751
Moran Pekerded84402018-06-06 16:36:50 +03001752 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001753 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001754
Gilles Peskinefe119512018-07-08 21:39:34 +02001755 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
1756 key_slot, alg ) == PSA_SUCCESS );
1757 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
1758 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001759
Gilles Peskinefe119512018-07-08 21:39:34 +02001760 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
1761 iv, iv_size,
1762 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001763 output1_buffer_size = (size_t) input->len +
1764 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001765 output1 = mbedtls_calloc( 1, output1_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001766 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001767
Gilles Peskine4abf7412018-06-18 16:35:34 +02001768 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001769
itayzafrir3e02b3b2018-06-12 17:06:52 +03001770 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001771 output1, output1_buffer_size,
1772 &function_output_length ) == PSA_SUCCESS );
1773 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001774
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001775 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001776 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001777 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001778 output1, output1_buffer_size,
1779 &function_output_length ) == PSA_SUCCESS );
1780 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001781
Gilles Peskine048b7f02018-06-08 14:20:49 +02001782 TEST_ASSERT( psa_cipher_finish( &operation1,
1783 output1 + output1_length,
1784 output1_buffer_size - output1_length,
1785 &function_output_length ) == PSA_SUCCESS );
1786 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001787
1788 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1789
Gilles Peskine048b7f02018-06-08 14:20:49 +02001790 output2_buffer_size = output1_length;
1791 output2 = mbedtls_calloc( 1, output2_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001792 TEST_ASSERT( output2 != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001793
Gilles Peskinefe119512018-07-08 21:39:34 +02001794 TEST_ASSERT( psa_cipher_set_iv( &operation2,
1795 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001796
1797 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001798 output2, output2_buffer_size,
1799 &function_output_length ) == PSA_SUCCESS );
1800 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001801
Gilles Peskine048b7f02018-06-08 14:20:49 +02001802 TEST_ASSERT( psa_cipher_update( &operation2,
1803 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001804 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001805 output2, output2_buffer_size,
1806 &function_output_length ) == PSA_SUCCESS );
1807 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001808
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001809 TEST_ASSERT( psa_cipher_finish( &operation2,
1810 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001811 output2_buffer_size - output2_length,
1812 &function_output_length ) == PSA_SUCCESS );
1813 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001814
Janos Follath25c4fa82018-07-06 16:23:25 +01001815 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001816
Gilles Peskine4abf7412018-06-18 16:35:34 +02001817 TEST_ASSERT( input->len == output2_length );
1818 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001819
1820exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001821 mbedtls_free( output1 );
1822 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001823 psa_destroy_key( key_slot );
1824 mbedtls_psa_crypto_free( );
1825}
1826/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02001827
Gilles Peskine20035e32018-02-03 22:44:14 +01001828/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001829void aead_encrypt_decrypt( int key_type_arg,
1830 data_t * key_data,
1831 int alg_arg,
1832 data_t * input_data,
1833 data_t * nonce,
1834 data_t * additional_data,
1835 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001836{
1837 int slot = 1;
1838 psa_key_type_t key_type = key_type_arg;
1839 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001840 unsigned char *output_data = NULL;
1841 size_t output_size = 0;
1842 size_t output_length = 0;
1843 unsigned char *output_data2 = NULL;
1844 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001845 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001846 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001847 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001848
Gilles Peskinea1cac842018-06-11 19:33:02 +02001849 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001850 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001851 TEST_ASSERT( nonce != NULL );
1852 TEST_ASSERT( additional_data != NULL );
1853 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1854 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1855 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1856 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1857
Gilles Peskine4abf7412018-06-18 16:35:34 +02001858 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001859 output_data = mbedtls_calloc( 1, output_size );
1860 TEST_ASSERT( output_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001861
1862 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1863
1864 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001865 psa_key_policy_set_usage( &policy,
1866 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
1867 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001868 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1869
1870 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001871 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001872
1873 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001874 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001875 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001876 additional_data->len,
1877 input_data->x, input_data->len,
1878 output_data, output_size,
1879 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001880
1881 if( PSA_SUCCESS == expected_result )
1882 {
1883 output_data2 = mbedtls_calloc( 1, output_length );
1884 TEST_ASSERT( output_data2 != NULL );
1885
1886 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001887 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001888 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001889 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001890 output_data, output_length,
1891 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001892 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02001893
itayzafrir3e02b3b2018-06-12 17:06:52 +03001894 TEST_ASSERT( memcmp( input_data->x, output_data2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001895 input_data->len ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001896 }
Gilles Peskine2d277862018-06-18 15:41:12 +02001897
Gilles Peskinea1cac842018-06-11 19:33:02 +02001898exit:
1899 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001900 mbedtls_free( output_data );
1901 mbedtls_free( output_data2 );
1902 mbedtls_psa_crypto_free( );
1903}
1904/* END_CASE */
1905
1906/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001907void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001908 int alg_arg, data_t * input_data,
1909 data_t * additional_data, data_t * nonce,
1910 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001911{
1912 int slot = 1;
1913 psa_key_type_t key_type = key_type_arg;
1914 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001915 unsigned char *output_data = NULL;
1916 size_t output_size = 0;
1917 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001918 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001919 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001920
Gilles Peskinea1cac842018-06-11 19:33:02 +02001921 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001922 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001923 TEST_ASSERT( additional_data != NULL );
1924 TEST_ASSERT( nonce != NULL );
1925 TEST_ASSERT( expected_result != NULL );
1926 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1927 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1928 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1929 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1930 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
1931
Gilles Peskine4abf7412018-06-18 16:35:34 +02001932 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001933 output_data = mbedtls_calloc( 1, output_size );
1934 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001935
Gilles Peskinea1cac842018-06-11 19:33:02 +02001936 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1937
1938 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001939 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001940 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1941
1942 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001943 key_data->x,
1944 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001945
1946 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001947 nonce->x, nonce->len,
1948 additional_data->x, additional_data->len,
1949 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001950 output_data, output_size,
1951 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001952
itayzafrir3e02b3b2018-06-12 17:06:52 +03001953 TEST_ASSERT( memcmp( output_data, expected_result->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001954 output_length ) == 0 );
1955
Gilles Peskinea1cac842018-06-11 19:33:02 +02001956exit:
1957 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001958 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001959 mbedtls_psa_crypto_free( );
1960}
1961/* END_CASE */
1962
1963/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001964void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001965 int alg_arg, data_t * input_data,
1966 data_t * additional_data, data_t * nonce,
1967 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001968{
1969 int slot = 1;
1970 psa_key_type_t key_type = key_type_arg;
1971 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001972 unsigned char *output_data = NULL;
1973 size_t output_size = 0;
1974 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001975 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001976 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001977 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001978
Gilles Peskinea1cac842018-06-11 19:33:02 +02001979 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001980 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001981 TEST_ASSERT( additional_data != NULL );
1982 TEST_ASSERT( nonce != NULL );
1983 TEST_ASSERT( expected_data != NULL );
1984 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1985 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1986 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1987 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1988 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
1989
Gilles Peskine4abf7412018-06-18 16:35:34 +02001990 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001991 output_data = mbedtls_calloc( 1, output_size );
1992 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001993
Gilles Peskinea1cac842018-06-11 19:33:02 +02001994 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1995
1996 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001997 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001998 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1999
2000 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002001 key_data->x,
2002 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002003
2004 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002005 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002006 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002007 additional_data->len,
2008 input_data->x, input_data->len,
2009 output_data, output_size,
2010 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002011
Gilles Peskine2d277862018-06-18 15:41:12 +02002012 if( expected_result == PSA_SUCCESS )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002013 {
itayzafrir3e02b3b2018-06-12 17:06:52 +03002014 TEST_ASSERT( memcmp( output_data, expected_data->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02002015 output_length ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002016 }
2017
Gilles Peskinea1cac842018-06-11 19:33:02 +02002018exit:
2019 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002020 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002021 mbedtls_psa_crypto_free( );
2022}
2023/* END_CASE */
2024
2025/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002026void signature_size( int type_arg,
2027 int bits,
2028 int alg_arg,
2029 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002030{
2031 psa_key_type_t type = type_arg;
2032 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002033 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002034 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2035exit:
2036 ;
2037}
2038/* END_CASE */
2039
2040/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002041void sign_deterministic( int key_type_arg, data_t *key_data,
2042 int alg_arg, data_t *input_data,
2043 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002044{
2045 int slot = 1;
2046 psa_key_type_t key_type = key_type_arg;
2047 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002048 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002049 unsigned char *signature = NULL;
2050 size_t signature_size;
2051 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002052 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002053
Gilles Peskine20035e32018-02-03 22:44:14 +01002054 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002055 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002056 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002057 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2058 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2059 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002060
2061 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2062
mohammad1603a97cb8c2018-03-28 03:46:26 -07002063 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002064 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002065 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2066
Gilles Peskine20035e32018-02-03 22:44:14 +01002067 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002068 key_data->x,
2069 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002070 TEST_ASSERT( psa_get_key_information( slot,
2071 NULL,
2072 &key_bits ) == PSA_SUCCESS );
2073
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002074 /* Allocate a buffer which has the size advertized by the
2075 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002076 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2077 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002078 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002079 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine20035e32018-02-03 22:44:14 +01002080 signature = mbedtls_calloc( 1, signature_size );
2081 TEST_ASSERT( signature != NULL );
2082
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002083 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002084 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002085 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002086 NULL, 0,
2087 signature, signature_size,
2088 &signature_length ) == PSA_SUCCESS );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002089 /* Verify that the signature is correct. */
Gilles Peskine4abf7412018-06-18 16:35:34 +02002090 TEST_ASSERT( signature_length == output_data->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02002091 TEST_ASSERT( memcmp( signature, output_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002092 output_data->len ) == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01002093
2094exit:
2095 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002096 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002097 mbedtls_psa_crypto_free( );
2098}
2099/* END_CASE */
2100
2101/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002102void sign_fail( int key_type_arg, data_t *key_data,
2103 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002104 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002105{
2106 int slot = 1;
2107 psa_key_type_t key_type = key_type_arg;
2108 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002109 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002110 psa_status_t actual_status;
2111 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002112 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002113 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002114 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002115
Gilles Peskine20035e32018-02-03 22:44:14 +01002116 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002117 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002118 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2119 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2120
Gilles Peskine20035e32018-02-03 22:44:14 +01002121 signature = mbedtls_calloc( 1, signature_size );
2122 TEST_ASSERT( signature != NULL );
2123
2124 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2125
mohammad1603a97cb8c2018-03-28 03:46:26 -07002126 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002127 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002128 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2129
Gilles Peskine20035e32018-02-03 22:44:14 +01002130 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002131 key_data->x,
2132 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002133
2134 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002135 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002136 NULL, 0,
2137 signature, signature_size,
2138 &signature_length );
2139 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002140 /* The value of *signature_length is unspecified on error, but
2141 * whatever it is, it should be less than signature_size, so that
2142 * if the caller tries to read *signature_length bytes without
2143 * checking the error code then they don't overflow a buffer. */
2144 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002145
2146exit:
2147 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002148 mbedtls_free( signature );
2149 mbedtls_psa_crypto_free( );
2150}
2151/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002152
2153/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002154void asymmetric_verify( int key_type_arg, data_t *key_data,
2155 int alg_arg, data_t *hash_data,
2156 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002157{
2158 int slot = 1;
2159 psa_key_type_t key_type = key_type_arg;
2160 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002161 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002162
Gilles Peskine69c12672018-06-28 00:07:19 +02002163 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2164
itayzafrir5c753392018-05-08 11:18:38 +03002165 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002166 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002167 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002168 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2169 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2170 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002171
2172 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2173
2174 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002175 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002176 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2177
2178 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002179 key_data->x,
2180 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002181
2182 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002183 hash_data->x, hash_data->len,
itayzafrir5c753392018-05-08 11:18:38 +03002184 NULL, 0,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002185 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002186 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002187exit:
2188 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002189 mbedtls_psa_crypto_free( );
2190}
2191/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002192
2193/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002194void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2195 int alg_arg, data_t *hash_data,
2196 data_t *signature_data,
2197 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002198{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002199 int slot = 1;
2200 psa_key_type_t key_type = key_type_arg;
2201 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002202 psa_status_t actual_status;
2203 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002204 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002205
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002206 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002207 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002208 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002209 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2210 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2211 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002212
2213 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2214
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002215 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002216 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002217 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2218
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002219 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002220 key_data->x,
2221 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002222
2223 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002224 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002225 NULL, 0,
2226 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002227 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002228
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002229 TEST_ASSERT( actual_status == expected_status );
2230
2231exit:
2232 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002233 mbedtls_psa_crypto_free( );
2234}
2235/* END_CASE */
2236
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002237/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002238void asymmetric_encrypt_decrypt( int key_type_arg, data_t *key_data,
2239 int alg_arg, data_t *input_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002240{
2241 int slot = 1;
2242 psa_key_type_t key_type = key_type_arg;
2243 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002244 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002245 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002246 size_t output_length = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002247 unsigned char *output2 = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002248 size_t output2_size = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002249 size_t output2_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002250 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002251
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002252 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002253 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002254 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2255 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2256
Gilles Peskine4abf7412018-06-18 16:35:34 +02002257 output_size = key_data->len;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002258 output2_size = output_size;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002259 output = mbedtls_calloc( 1, output_size );
2260 TEST_ASSERT( output != NULL );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002261 output2 = mbedtls_calloc( 1, output2_size );
2262 TEST_ASSERT( output2 != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002263
2264 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2265
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002266 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002267 psa_key_policy_set_usage( &policy,
2268 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002269 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002270 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2271
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002272 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002273 key_data->x,
2274 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002275
Gilles Peskineeebd7382018-06-08 18:11:54 +02002276 /* We test encryption by checking that encrypt-then-decrypt gives back
2277 * the original plaintext because of the non-optional random
2278 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02002279 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002280 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002281 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002282 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002283 &output_length ) == PSA_SUCCESS );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002284
Gilles Peskine2d277862018-06-18 15:41:12 +02002285 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002286 output, output_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02002287 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002288 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002289 &output2_length ) == PSA_SUCCESS );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002290 TEST_ASSERT( memcmp( input_data->x, output2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002291 input_data->len ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002292
2293exit:
2294 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002295 mbedtls_free( output );
2296 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002297 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002298}
2299/* END_CASE */
2300
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002301/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002302void asymmetric_encrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002303 int alg_arg, data_t *input_data,
2304 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002305{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002306 int slot = 1;
2307 psa_key_type_t key_type = key_type_arg;
2308 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002309 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002310 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002311 size_t output_length = 0;
2312 psa_status_t actual_status;
2313 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002314 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002315
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002316 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002317 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002318 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2319 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2320
Gilles Peskine4abf7412018-06-18 16:35:34 +02002321 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002322 output = mbedtls_calloc( 1, output_size );
2323 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002324
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002325 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2326
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002327 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002328 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002329 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2330
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002331 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002332 key_data->x,
2333 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002334
Gilles Peskine2d277862018-06-18 15:41:12 +02002335 actual_status = psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002336 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002337 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002338 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002339 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002340 TEST_ASSERT( actual_status == expected_status );
2341
2342exit:
2343 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002344 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002345 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002346}
2347/* END_CASE */
2348
2349/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002350void asymmetric_decrypt( int key_type_arg, data_t *key_data,
2351 int alg_arg, data_t *input_data,
2352 data_t *expected_data, int expected_size )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002353{
2354 int slot = 1;
2355 psa_key_type_t key_type = key_type_arg;
2356 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002357 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002358 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002359 size_t output_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002360 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002361
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002362 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002363 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002364 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002365 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2366 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2367 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2368
Gilles Peskine4abf7412018-06-18 16:35:34 +02002369 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002370 output = mbedtls_calloc( 1, output_size );
2371 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002372
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002373 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2374
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002375 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002376 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002377 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2378
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002379 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002380 key_data->x,
2381 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002382
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002383 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002384 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002385 NULL, 0,
2386 output,
2387 output_size,
2388 &output_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002389 TEST_ASSERT( (size_t) expected_size == output_length );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002390 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002391
2392exit:
2393 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002394 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002395 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002396}
2397/* END_CASE */
2398
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002399/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002400void asymmetric_decrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002401 int alg_arg, data_t *input_data,
2402 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002403{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002404 int slot = 1;
2405 psa_key_type_t key_type = key_type_arg;
2406 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002407 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002408 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002409 size_t output_length = 0;
2410 psa_status_t actual_status;
2411 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002412 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002413
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002414 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002415 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002416 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2417 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2418
Gilles Peskine4abf7412018-06-18 16:35:34 +02002419 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002420 output = mbedtls_calloc( 1, output_size );
2421 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002422
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002423 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2424
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002425 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002426 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002427 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2428
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002429 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002430 key_data->x,
2431 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002432
Gilles Peskine2d277862018-06-18 15:41:12 +02002433 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002434 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002435 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002436 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002437 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002438 TEST_ASSERT( actual_status == expected_status );
2439
2440exit:
2441 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02002442 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002443 mbedtls_psa_crypto_free( );
2444}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002445/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02002446
2447/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002448void derive_setup( int key_type_arg,
2449 data_t *key_data,
2450 int alg_arg,
2451 data_t *salt,
2452 data_t *label,
2453 int requested_capacity_arg,
2454 int expected_status_arg )
2455{
2456 psa_key_slot_t slot = 1;
2457 size_t key_type = key_type_arg;
2458 psa_algorithm_t alg = alg_arg;
2459 size_t requested_capacity = requested_capacity_arg;
2460 psa_status_t expected_status = expected_status_arg;
2461 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2462 psa_key_policy_t policy;
2463
2464 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2465
2466 psa_key_policy_init( &policy );
2467 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
2468 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2469
2470 TEST_ASSERT( psa_import_key( slot, key_type,
2471 key_data->x,
2472 key_data->len ) == PSA_SUCCESS );
2473
2474 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
2475 salt->x, salt->len,
2476 label->x, label->len,
2477 requested_capacity ) == expected_status );
2478
2479exit:
2480 psa_generator_abort( &generator );
2481 psa_destroy_key( slot );
2482 mbedtls_psa_crypto_free( );
2483}
2484/* END_CASE */
2485
2486/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02002487void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02002488{
Gilles Peskinea50d7392018-06-21 10:22:13 +02002489 size_t bytes = bytes_arg;
2490 const unsigned char trail[] = "don't overwrite me";
2491 unsigned char *output = mbedtls_calloc( 1, bytes + sizeof( trail ) );
2492 unsigned char *changed = mbedtls_calloc( 1, bytes );
2493 size_t i;
2494 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02002495
Gilles Peskinea50d7392018-06-21 10:22:13 +02002496 TEST_ASSERT( output != NULL );
2497 TEST_ASSERT( changed != NULL );
2498 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02002499
2500 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2501
Gilles Peskinea50d7392018-06-21 10:22:13 +02002502 /* Run several times, to ensure that every output byte will be
2503 * nonzero at least once with overwhelming probability
2504 * (2^(-8*number_of_runs)). */
2505 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02002506 {
Gilles Peskinea50d7392018-06-21 10:22:13 +02002507 memset( output, 0, bytes );
2508 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
2509
2510 /* Check that no more than bytes have been overwritten */
2511 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
2512
2513 for( i = 0; i < bytes; i++ )
2514 {
2515 if( output[i] != 0 )
2516 ++changed[i];
2517 }
Gilles Peskine05d69892018-06-19 22:00:52 +02002518 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02002519
2520 /* Check that every byte was changed to nonzero at least once. This
2521 * validates that psa_generate_random is overwriting every byte of
2522 * the output buffer. */
2523 for( i = 0; i < bytes; i++ )
2524 {
2525 TEST_ASSERT( changed[i] != 0 );
2526 }
Gilles Peskine05d69892018-06-19 22:00:52 +02002527
2528exit:
2529 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02002530 mbedtls_free( output );
2531 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02002532}
2533/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02002534
2535/* BEGIN_CASE */
2536void generate_key( int type_arg,
2537 int bits_arg,
2538 int usage_arg,
2539 int alg_arg,
2540 int expected_status_arg )
2541{
2542 int slot = 1;
2543 psa_key_type_t type = type_arg;
2544 psa_key_usage_t usage = usage_arg;
2545 size_t bits = bits_arg;
2546 psa_algorithm_t alg = alg_arg;
2547 psa_status_t expected_status = expected_status_arg;
2548 psa_key_type_t got_type;
2549 size_t got_bits;
2550 unsigned char exported[616] = {0}; /* enough for a 1024-bit RSA key */
2551 size_t exported_length;
2552 psa_status_t expected_export_status =
2553 usage & PSA_KEY_USAGE_EXPORT ? PSA_SUCCESS : PSA_ERROR_NOT_PERMITTED;
2554 psa_status_t expected_info_status =
2555 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
2556 psa_key_policy_t policy;
2557
2558 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2559
2560 psa_key_policy_init( &policy );
2561 psa_key_policy_set_usage( &policy, usage, alg );
2562 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2563
2564 /* Generate a key */
2565 TEST_ASSERT( psa_generate_key( slot, type, bits,
2566 NULL, 0 ) == expected_status );
2567
2568 /* Test the key information */
2569 TEST_ASSERT( psa_get_key_information( slot,
2570 &got_type,
2571 &got_bits ) == expected_info_status );
2572 if( expected_info_status != PSA_SUCCESS )
2573 goto exit;
2574 TEST_ASSERT( got_type == type );
2575 TEST_ASSERT( got_bits == bits );
2576
2577 /* Export the key */
2578 TEST_ASSERT( psa_export_key( slot,
2579 exported, sizeof( exported ),
2580 &exported_length ) == expected_export_status );
2581 if( expected_export_status == PSA_SUCCESS )
2582 {
Gilles Peskine48c0ea12018-06-21 14:15:31 +02002583 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02002584 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
2585#if defined(MBEDTLS_DES_C)
2586 if( type == PSA_KEY_TYPE_DES )
2587 {
2588 /* Check the parity bits. */
2589 unsigned i;
2590 for( i = 0; i < bits / 8; i++ )
2591 {
2592 unsigned bit_count = 0;
2593 unsigned m;
2594 for( m = 1; m <= 0x100; m <<= 1 )
2595 {
2596 if( exported[i] & m )
2597 ++bit_count;
2598 }
2599 TEST_ASSERT( bit_count % 2 != 0 );
2600 }
2601 }
2602#endif
2603#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
2604 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
2605 {
2606 /* Sanity check: does this look like the beginning of a PKCS#8
2607 * RSA key pair? Assumes bits is a multiple of 8. */
2608 size_t n_bytes = bits / 8 + 1;
2609 size_t n_encoded_bytes;
2610 unsigned char *n_end;
2611 TEST_ASSERT( exported_length >= 7 + ( n_bytes + 3 ) * 9 / 2 );
2612 TEST_ASSERT( exported[0] == 0x30 );
2613 TEST_ASSERT( exported[1] == 0x82 ); // assumes >=416-bit key
2614 TEST_ASSERT( exported[4] == 0x02 );
2615 TEST_ASSERT( exported[5] == 0x01 );
2616 TEST_ASSERT( exported[6] == 0x00 );
2617 TEST_ASSERT( exported[7] == 0x02 );
2618 n_encoded_bytes = exported[8];
2619 n_end = exported + 9 + n_encoded_bytes;
2620 if( n_encoded_bytes & 0x80 )
2621 {
2622 n_encoded_bytes = ( n_encoded_bytes & 0x7f ) << 7;
2623 n_encoded_bytes |= exported[9] & 0x7f;
2624 n_end += 1;
2625 }
2626 /* The encoding of n should start with a 0 byte since it should
2627 * have its high bit set. However Mbed TLS is not compliant and
2628 * generates an invalid, but widely tolerated, encoding of
2629 * positive INTEGERs with a bit size that is a multiple of 8
2630 * with no leading 0 byte. Accept this here. */
2631 TEST_ASSERT( n_bytes == n_encoded_bytes ||
2632 n_bytes == n_encoded_bytes + 1 );
2633 if( n_bytes == n_encoded_bytes )
2634 TEST_ASSERT( exported[n_encoded_bytes <= 127 ? 9 : 10] == 0x00 );
2635 /* Sanity check: e must be 3 */
2636 TEST_ASSERT( n_end[0] == 0x02 );
2637 TEST_ASSERT( n_end[1] == 0x03 );
2638 TEST_ASSERT( n_end[2] == 0x01 );
2639 TEST_ASSERT( n_end[3] == 0x00 );
2640 TEST_ASSERT( n_end[4] == 0x01 );
2641 TEST_ASSERT( n_end[5] == 0x02 );
2642 }
2643#endif /* MBEDTLS_RSA_C */
2644#if defined(MBEDTLS_ECP_C)
2645 if( PSA_KEY_TYPE_IS_ECC( type ) )
2646 {
2647 /* Sanity check: does this look like the beginning of a PKCS#8
2648 * elliptic curve key pair? */
2649 TEST_ASSERT( exported_length >= bits * 3 / 8 + 10 );
2650 TEST_ASSERT( exported[0] == 0x30 );
2651 }
2652#endif /* MBEDTLS_ECP_C */
2653 }
2654
Gilles Peskine818ca122018-06-20 18:16:48 +02002655 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02002656 if( ! exercise_key( slot, usage, alg ) )
2657 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02002658
2659exit:
2660 psa_destroy_key( slot );
2661 mbedtls_psa_crypto_free( );
2662}
2663/* END_CASE */