blob: 1017e88c804eaf37545de73150ae049ed42e2fb2 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
Gilles Peskine0b352bc2018-06-28 00:16:11 +02003#include "mbedtls/asn1write.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +01004#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +03005
6#if(UINT32_MAX > SIZE_MAX)
Gilles Peskine2d277862018-06-18 15:41:12 +02007#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX )
itayzafrir3e02b3b2018-06-12 17:06:52 +03008#else
Gilles Peskine2d277862018-06-18 15:41:12 +02009#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1
itayzafrir3e02b3b2018-06-12 17:06:52 +030010#endif
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020011
Jaeden Amerof24c7f82018-06-27 17:20:43 +010012/** An invalid export length that will never be set by psa_export_key(). */
13static const size_t INVALID_EXPORT_LENGTH = ~0U;
14
Gilles Peskined35a1cc2018-06-26 21:26:10 +020015/** Test if a buffer is all-bits zero.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020016 *
17 * \param buffer Pointer to the beginning of the buffer.
18 * \param size Size of the buffer in bytes.
19 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020020 * \return 1 if the buffer is all-bits-zero.
21 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020022 */
Gilles Peskine3f669c32018-06-21 09:21:51 +020023static int mem_is_zero( void *buffer, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020024{
25 size_t i;
26 for( i = 0; i < size; i++ )
27 {
28 if( ( (unsigned char *) buffer )[i] != 0 )
Gilles Peskine3f669c32018-06-21 09:21:51 +020029 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020030 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020031 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020032}
Gilles Peskine818ca122018-06-20 18:16:48 +020033
Gilles Peskine48c0ea12018-06-21 14:15:31 +020034static int key_type_is_raw_bytes( psa_key_type_t type )
35{
36 psa_key_type_t category = type & PSA_KEY_TYPE_CATEGORY_MASK;
37 return( category == PSA_KEY_TYPE_RAW_DATA ||
38 category == PSA_KEY_TYPE_CATEGORY_SYMMETRIC );
39}
40
Gilles Peskine0b352bc2018-06-28 00:16:11 +020041/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
42static int asn1_write_10x( unsigned char **p,
43 unsigned char *start,
44 size_t bits,
45 unsigned char x )
46{
47 int ret;
48 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020049 if( bits == 0 )
50 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
51 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020052 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
53 if( *p < start || *p - start < (ssize_t) len )
54 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
55 *p -= len;
56 ( *p )[len-1] = x;
57 if( bits % 8 == 0 )
58 ( *p )[1] |= 1;
59 else
60 ( *p )[0] |= 1 << ( bits % 8 );
61 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
62 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
63 MBEDTLS_ASN1_INTEGER ) );
64 return( len );
65}
66
67static int construct_fake_rsa_key( unsigned char *buffer,
68 size_t buffer_size,
69 unsigned char **p,
70 size_t bits,
71 int keypair )
72{
73 size_t half_bits = ( bits + 1 ) / 2;
74 int ret;
75 int len = 0;
76 /* Construct something that looks like a DER encoding of
77 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
78 * RSAPrivateKey ::= SEQUENCE {
79 * version Version,
80 * modulus INTEGER, -- n
81 * publicExponent INTEGER, -- e
82 * privateExponent INTEGER, -- d
83 * prime1 INTEGER, -- p
84 * prime2 INTEGER, -- q
85 * exponent1 INTEGER, -- d mod (p-1)
86 * exponent2 INTEGER, -- d mod (q-1)
87 * coefficient INTEGER, -- (inverse of q) mod p
88 * otherPrimeInfos OtherPrimeInfos OPTIONAL
89 * }
90 * Or, for a public key, the same structure with only
91 * version, modulus and publicExponent.
92 */
93 *p = buffer + buffer_size;
94 if( keypair )
95 {
96 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
97 asn1_write_10x( p, buffer, half_bits, 1 ) );
98 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
99 asn1_write_10x( p, buffer, half_bits, 1 ) );
100 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
101 asn1_write_10x( p, buffer, half_bits, 1 ) );
102 MBEDTLS_ASN1_CHK_ADD( len, /* q */
103 asn1_write_10x( p, buffer, half_bits, 1 ) );
104 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
105 asn1_write_10x( p, buffer, half_bits, 3 ) );
106 MBEDTLS_ASN1_CHK_ADD( len, /* d */
107 asn1_write_10x( p, buffer, bits, 1 ) );
108 }
109 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
110 asn1_write_10x( p, buffer, 17, 1 ) );
111 MBEDTLS_ASN1_CHK_ADD( len, /* n */
112 asn1_write_10x( p, buffer, bits, 1 ) );
113 if( keypair )
114 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
115 mbedtls_asn1_write_int( p, buffer, 0 ) );
116 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
117 {
118 const unsigned char tag =
119 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
120 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
121 }
122 return( len );
123}
124
Gilles Peskine818ca122018-06-20 18:16:48 +0200125static int exercise_mac_key( psa_key_slot_t key,
126 psa_key_usage_t usage,
127 psa_algorithm_t alg )
128{
129 psa_mac_operation_t operation;
130 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200131 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200132 size_t mac_length = sizeof( mac );
133
134 if( usage & PSA_KEY_USAGE_SIGN )
135 {
136 TEST_ASSERT( psa_mac_start( &operation, key, alg ) == PSA_SUCCESS );
137 TEST_ASSERT( psa_mac_update( &operation,
138 input, sizeof( input ) ) == PSA_SUCCESS );
139 TEST_ASSERT( psa_mac_finish( &operation,
140 mac, sizeof( input ),
141 &mac_length ) == PSA_SUCCESS );
142 }
143
144 if( usage & PSA_KEY_USAGE_VERIFY )
145 {
146 psa_status_t verify_status =
147 ( usage & PSA_KEY_USAGE_SIGN ?
148 PSA_SUCCESS :
149 PSA_ERROR_INVALID_SIGNATURE );
150 TEST_ASSERT( psa_mac_start( &operation, key, alg ) == PSA_SUCCESS );
151 TEST_ASSERT( psa_mac_update( &operation,
152 input, sizeof( input ) ) == PSA_SUCCESS );
153 TEST_ASSERT( psa_mac_verify( &operation, mac, mac_length ) == verify_status );
154 }
155
156 return( 1 );
157
158exit:
159 psa_mac_abort( &operation );
160 return( 0 );
161}
162
163static int exercise_cipher_key( psa_key_slot_t key,
164 psa_key_usage_t usage,
165 psa_algorithm_t alg )
166{
167 psa_cipher_operation_t operation;
168 unsigned char iv[16] = {0};
169 size_t iv_length = sizeof( iv );
170 const unsigned char plaintext[16] = "Hello, world...";
171 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
172 size_t ciphertext_length = sizeof( ciphertext );
173 unsigned char decrypted[sizeof( ciphertext )];
174 size_t part_length;
175
176 if( usage & PSA_KEY_USAGE_ENCRYPT )
177 {
178 TEST_ASSERT( psa_encrypt_setup( &operation, key, alg ) == PSA_SUCCESS );
179 TEST_ASSERT( psa_encrypt_generate_iv( &operation,
180 iv, sizeof( iv ),
181 &iv_length ) == PSA_SUCCESS );
182 TEST_ASSERT( psa_cipher_update( &operation,
183 plaintext, sizeof( plaintext ),
184 ciphertext, sizeof( ciphertext ),
185 &ciphertext_length ) == PSA_SUCCESS );
186 TEST_ASSERT( psa_cipher_finish( &operation,
187 ciphertext + ciphertext_length,
188 sizeof( ciphertext ) - ciphertext_length,
189 &part_length ) == PSA_SUCCESS );
190 ciphertext_length += part_length;
191 }
192
193 if( usage & PSA_KEY_USAGE_DECRYPT )
194 {
195 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700196 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200197 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
198 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200199 size_t bits;
200 TEST_ASSERT( psa_get_key_information( key, &type, &bits ) );
201 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
202 }
203 TEST_ASSERT( psa_decrypt_setup( &operation, key, alg ) == PSA_SUCCESS );
204 TEST_ASSERT( psa_encrypt_set_iv( &operation,
205 iv, iv_length ) == PSA_SUCCESS );
206 TEST_ASSERT( psa_cipher_update( &operation,
207 ciphertext, ciphertext_length,
208 decrypted, sizeof( decrypted ),
209 &part_length ) == PSA_SUCCESS );
210 status = psa_cipher_finish( &operation,
211 decrypted + part_length,
212 sizeof( decrypted ) - part_length,
213 &part_length );
214 /* For a stream cipher, all inputs are valid. For a block cipher,
215 * if the input is some aribtrary data rather than an actual
216 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700217 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700218 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine818ca122018-06-20 18:16:48 +0200219 TEST_ASSERT( status == PSA_SUCCESS );
220 else
221 TEST_ASSERT( status == PSA_SUCCESS ||
222 status == PSA_ERROR_INVALID_PADDING );
223 }
224
225 return( 1 );
226
227exit:
228 psa_cipher_abort( &operation );
229 return( 0 );
230}
231
232static int exercise_aead_key( psa_key_slot_t key,
233 psa_key_usage_t usage,
234 psa_algorithm_t alg )
235{
236 unsigned char nonce[16] = {0};
237 size_t nonce_length = sizeof( nonce );
238 unsigned char plaintext[16] = "Hello, world...";
239 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
240 size_t ciphertext_length = sizeof( ciphertext );
241 size_t plaintext_length = sizeof( ciphertext );
242
243 if( usage & PSA_KEY_USAGE_ENCRYPT )
244 {
245 TEST_ASSERT( psa_aead_encrypt( key, alg,
246 nonce, nonce_length,
247 NULL, 0,
248 plaintext, sizeof( plaintext ),
249 ciphertext, sizeof( ciphertext ),
250 &ciphertext_length ) == PSA_SUCCESS );
251 }
252
253 if( usage & PSA_KEY_USAGE_DECRYPT )
254 {
255 psa_status_t verify_status =
256 ( usage & PSA_KEY_USAGE_ENCRYPT ?
257 PSA_SUCCESS :
258 PSA_ERROR_INVALID_SIGNATURE );
259 TEST_ASSERT( psa_aead_decrypt( key, alg,
260 nonce, nonce_length,
261 NULL, 0,
262 ciphertext, ciphertext_length,
263 plaintext, sizeof( plaintext ),
264 &plaintext_length ) == verify_status );
265 }
266
267 return( 1 );
268
269exit:
270 return( 0 );
271}
272
273static int exercise_signature_key( psa_key_slot_t key,
274 psa_key_usage_t usage,
275 psa_algorithm_t alg )
276{
Gilles Peskineca45c352018-06-26 16:13:09 +0200277 unsigned char payload[16] = {1};
Gilles Peskine818ca122018-06-20 18:16:48 +0200278 size_t payload_length = sizeof( payload );
Gilles Peskine69c12672018-06-28 00:07:19 +0200279 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200280 size_t signature_length = sizeof( signature );
281
282 if( usage & PSA_KEY_USAGE_SIGN )
283 {
284 TEST_ASSERT( psa_asymmetric_sign( key, alg,
285 payload, payload_length,
286 NULL, 0,
287 signature, sizeof( signature ),
288 &signature_length ) == PSA_SUCCESS );
289 }
290
291 if( usage & PSA_KEY_USAGE_VERIFY )
292 {
293 psa_status_t verify_status =
294 ( usage & PSA_KEY_USAGE_SIGN ?
295 PSA_SUCCESS :
296 PSA_ERROR_INVALID_SIGNATURE );
297 TEST_ASSERT( psa_asymmetric_verify( key, alg,
298 payload, payload_length,
299 NULL, 0,
300 signature, signature_length ) ==
301 verify_status );
302 }
303
304 return( 1 );
305
306exit:
307 return( 0 );
308}
309
310static int exercise_asymmetric_encryption_key( psa_key_slot_t key,
311 psa_key_usage_t usage,
312 psa_algorithm_t alg )
313{
314 unsigned char plaintext[256] = "Hello, world...";
315 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
316 size_t ciphertext_length = sizeof( ciphertext );
317 size_t plaintext_length = 16;
318
319 if( usage & PSA_KEY_USAGE_ENCRYPT )
320 {
321 TEST_ASSERT(
322 psa_asymmetric_encrypt( key, alg,
323 plaintext, plaintext_length,
324 NULL, 0,
325 ciphertext, sizeof( ciphertext ),
326 &ciphertext_length ) == PSA_SUCCESS );
327 }
328
329 if( usage & PSA_KEY_USAGE_DECRYPT )
330 {
331 psa_status_t status =
332 psa_asymmetric_decrypt( key, alg,
333 ciphertext, ciphertext_length,
334 NULL, 0,
335 plaintext, sizeof( plaintext ),
336 &plaintext_length );
337 TEST_ASSERT( status == PSA_SUCCESS ||
338 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
339 ( status == PSA_ERROR_INVALID_ARGUMENT ||
340 status == PSA_ERROR_INVALID_PADDING ) ) );
341 }
342
343 return( 1 );
344
345exit:
346 return( 0 );
347}
Gilles Peskine02b75072018-07-01 22:31:34 +0200348
349static int exercise_key( psa_key_slot_t slot,
350 psa_key_usage_t usage,
351 psa_algorithm_t alg )
352{
353 int ok;
354 if( alg == 0 )
355 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
356 else if( PSA_ALG_IS_MAC( alg ) )
357 ok = exercise_mac_key( slot, usage, alg );
358 else if( PSA_ALG_IS_CIPHER( alg ) )
359 ok = exercise_cipher_key( slot, usage, alg );
360 else if( PSA_ALG_IS_AEAD( alg ) )
361 ok = exercise_aead_key( slot, usage, alg );
362 else if( PSA_ALG_IS_SIGN( alg ) )
363 ok = exercise_signature_key( slot, usage, alg );
364 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
365 ok = exercise_asymmetric_encryption_key( slot, usage, alg );
366 else
367 {
368 char message[40];
369 mbedtls_snprintf( message, sizeof( message ),
370 "No code to exercise alg=0x%08lx",
371 (unsigned long) alg );
372 test_fail( message, __LINE__, __FILE__ );
373 ok = 0;
374 }
375 return( ok );
376}
377
Gilles Peskinee59236f2018-01-27 23:32:46 +0100378/* END_HEADER */
379
380/* BEGIN_DEPENDENCIES
381 * depends_on:MBEDTLS_PSA_CRYPTO_C
382 * END_DEPENDENCIES
383 */
384
385/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200386void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100387{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100388 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100389 int i;
390 for( i = 0; i <= 1; i++ )
391 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100392 status = psa_crypto_init( );
393 TEST_ASSERT( status == PSA_SUCCESS );
394 status = psa_crypto_init( );
395 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100396 mbedtls_psa_crypto_free( );
397 }
398}
399/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100400
401/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200402void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100403{
404 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200405 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100406 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100407
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100408 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300409 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100410 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
411
Gilles Peskine4abf7412018-06-18 16:35:34 +0200412 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200413 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100414 if( status == PSA_SUCCESS )
415 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
416
417exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100418 mbedtls_psa_crypto_free( );
419}
420/* END_CASE */
421
422/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200423void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
424{
425 int slot = 1;
426 size_t bits = bits_arg;
427 psa_status_t expected_status = expected_status_arg;
428 psa_status_t status;
429 psa_key_type_t type =
430 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
431 size_t buffer_size = /* Slight overapproximations */
432 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
433 unsigned char *buffer = mbedtls_calloc( 1, buffer_size );
434 unsigned char *p;
435 int ret;
436 size_t length;
437
438 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
439 TEST_ASSERT( buffer != NULL );
440
441 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
442 bits, keypair ) ) >= 0 );
443 length = ret;
444
445 /* Try importing the key */
446 status = psa_import_key( slot, type, p, length );
447 TEST_ASSERT( status == expected_status );
448 if( status == PSA_SUCCESS )
449 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
450
451exit:
452 mbedtls_free( buffer );
453 mbedtls_psa_crypto_free( );
454}
455/* END_CASE */
456
457/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300458void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300459 int type_arg,
460 int alg_arg,
461 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100462 int expected_bits,
463 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200464 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100465 int canonical_input )
466{
467 int slot = 1;
468 int slot2 = slot + 1;
469 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200470 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200471 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100472 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100473 unsigned char *exported = NULL;
474 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100475 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100476 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100477 size_t reexported_length;
478 psa_key_type_t got_type;
479 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200480 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100481
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100482 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300483 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
484 export_size = (ssize_t) data->len + export_size_delta;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100485 exported = mbedtls_calloc( 1, export_size );
486 TEST_ASSERT( exported != NULL );
487 if( ! canonical_input )
488 {
489 reexported = mbedtls_calloc( 1, export_size );
490 TEST_ASSERT( reexported != NULL );
491 }
492 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
493
mohammad1603a97cb8c2018-03-28 03:46:26 -0700494 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200495 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700496 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
497
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100498 /* Import the key */
499 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200500 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100501
502 /* Test the key information */
503 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200504 &got_type,
505 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100506 TEST_ASSERT( got_type == type );
507 TEST_ASSERT( got_bits == (size_t) expected_bits );
508
509 /* Export the key */
510 status = psa_export_key( slot,
511 exported, export_size,
512 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200513 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100514
515 /* The exported length must be set by psa_export_key() to a value between 0
516 * and export_size. On errors, the exported length must be 0. */
517 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
518 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
519 TEST_ASSERT( exported_length <= export_size );
520
Gilles Peskine3f669c32018-06-21 09:21:51 +0200521 TEST_ASSERT( mem_is_zero( exported + exported_length,
522 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100523 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200524 {
525 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100526 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200527 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100528
529 if( canonical_input )
530 {
Gilles Peskine4abf7412018-06-18 16:35:34 +0200531 TEST_ASSERT( exported_length == data->len );
532 TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100533 }
534 else
535 {
mohammad1603a97cb8c2018-03-28 03:46:26 -0700536 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
537
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100538 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200539 exported,
540 export_size ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100541 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200542 reexported,
543 export_size,
544 &reexported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100545 TEST_ASSERT( reexported_length == exported_length );
546 TEST_ASSERT( memcmp( reexported, exported,
547 exported_length ) == 0 );
548 }
549
550destroy:
551 /* Destroy the key */
552 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
553 TEST_ASSERT( psa_get_key_information(
554 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
555
556exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300557 mbedtls_free( exported );
558 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100559 mbedtls_psa_crypto_free( );
560}
561/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100562
Moran Pekerf709f4a2018-06-06 17:26:04 +0300563/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300564void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200565 int type_arg,
566 int alg_arg,
567 int expected_bits,
568 int public_key_expected_length,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200569 int expected_export_status_arg )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300570{
571 int slot = 1;
572 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200573 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200574 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300575 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300576 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300577 size_t export_size;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100578 size_t exported_length = INVALID_EXPORT_LENGTH;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300579 psa_key_type_t got_type;
580 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200581 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300582
Moran Pekerf709f4a2018-06-06 17:26:04 +0300583 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300584 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
585 export_size = (ssize_t) data->len;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300586 exported = mbedtls_calloc( 1, export_size );
587 TEST_ASSERT( exported != NULL );
588
589 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
590
591 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200592 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300593 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
594
595 /* Import the key */
596 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200597 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300598
599 /* Test the key information */
600 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200601 &got_type,
602 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300603 TEST_ASSERT( got_type == type );
604 TEST_ASSERT( got_bits == (size_t) expected_bits );
605
606 /* Export the key */
607 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200608 exported, export_size,
609 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200610 TEST_ASSERT( status == expected_export_status );
Jaeden Amero2a671e92018-06-27 17:47:40 +0100611 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
612 TEST_ASSERT( mem_is_zero( exported + exported_length,
613 export_size - exported_length ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300614 if( status != PSA_SUCCESS )
615 goto destroy;
616
Moran Pekerf709f4a2018-06-06 17:26:04 +0300617destroy:
618 /* Destroy the key */
619 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
620 TEST_ASSERT( psa_get_key_information(
621 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
622
623exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300624 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300625 mbedtls_psa_crypto_free( );
626}
627/* END_CASE */
628
Gilles Peskine20035e32018-02-03 22:44:14 +0100629/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200630void import_and_exercise_key( data_t *data,
631 int type_arg,
632 int bits_arg,
633 int alg_arg )
634{
635 int slot = 1;
636 psa_key_type_t type = type_arg;
637 size_t bits = bits_arg;
638 psa_algorithm_t alg = alg_arg;
639 psa_key_usage_t usage =
640 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
641 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
642 PSA_KEY_USAGE_VERIFY :
643 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
644 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
645 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
646 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
647 PSA_KEY_USAGE_ENCRYPT :
648 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
649 0 );
650 psa_key_policy_t policy;
651 psa_key_type_t got_type;
652 size_t got_bits;
653 psa_status_t status;
654
655 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
656
657 psa_key_policy_init( &policy );
658 psa_key_policy_set_usage( &policy, usage, alg );
659 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
660
661 /* Import the key */
662 status = psa_import_key( slot, type, data->x, data->len );
663 TEST_ASSERT( status == PSA_SUCCESS );
664
665 /* Test the key information */
666 TEST_ASSERT( psa_get_key_information( slot,
667 &got_type,
668 &got_bits ) == PSA_SUCCESS );
669 TEST_ASSERT( got_type == type );
670 TEST_ASSERT( got_bits == bits );
671
672 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +0200673 if( ! exercise_key( slot, usage, alg ) )
674 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200675
676exit:
677 psa_destroy_key( slot );
678 mbedtls_psa_crypto_free( );
679}
680/* END_CASE */
681
682/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +0200683void key_policy( int usage_arg, int alg_arg )
684{
685 int key_slot = 1;
686 psa_algorithm_t alg = alg_arg;
687 psa_key_usage_t usage = usage_arg;
688 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
689 unsigned char key[32] = {0};
690 psa_key_policy_t policy_set;
691 psa_key_policy_t policy_get;
692
693 memset( key, 0x2a, sizeof( key ) );
694
695 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
696
697 psa_key_policy_init( &policy_set );
698 psa_key_policy_init( &policy_get );
699
700 psa_key_policy_set_usage( &policy_set, usage, alg );
701
702 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
703 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
704 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
705
706 TEST_ASSERT( psa_import_key( key_slot, key_type,
707 key, sizeof( key ) ) == PSA_SUCCESS );
708
709 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
710
711 TEST_ASSERT( policy_get.usage == policy_set.usage );
712 TEST_ASSERT( policy_get.alg == policy_set.alg );
713
714exit:
715 psa_destroy_key( key_slot );
716 mbedtls_psa_crypto_free( );
717}
718/* END_CASE */
719
720/* BEGIN_CASE */
721void key_policy_fail( int usage_arg, int alg_arg, int expected_status,
722 data_t *keypair )
723{
724 int key_slot = 1;
725 psa_algorithm_t alg = alg_arg;
726 psa_key_usage_t usage = usage_arg;
727 size_t signature_length = 0;
728 psa_key_policy_t policy;
729 int actual_status = PSA_SUCCESS;
730
731 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
732
733 psa_key_policy_init( &policy );
734 psa_key_policy_set_usage( &policy, usage, alg );
735 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
736
737 if( usage & PSA_KEY_USAGE_EXPORT )
738 {
739 TEST_ASSERT( keypair != NULL );
740 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) );
741 TEST_ASSERT( psa_import_key( key_slot,
742 PSA_KEY_TYPE_RSA_KEYPAIR,
743 keypair->x,
744 keypair->len ) == PSA_SUCCESS );
745 actual_status = psa_asymmetric_sign( key_slot, alg,
746 NULL, 0,
747 NULL, 0,
748 NULL, 0, &signature_length );
749 }
750
751 if( usage & PSA_KEY_USAGE_SIGN )
752 {
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100753 size_t data_length;
Gilles Peskined5b33222018-06-18 22:20:03 +0200754 TEST_ASSERT( keypair != NULL );
755 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) );
756 TEST_ASSERT( psa_import_key( key_slot,
757 PSA_KEY_TYPE_RSA_KEYPAIR,
758 keypair->x,
759 keypair->len ) == PSA_SUCCESS );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100760 actual_status = psa_export_key( key_slot, NULL, 0, &data_length );
Gilles Peskined5b33222018-06-18 22:20:03 +0200761 }
762
763 TEST_ASSERT( actual_status == expected_status );
764
765exit:
766 psa_destroy_key( key_slot );
767 mbedtls_psa_crypto_free( );
768}
769/* END_CASE */
770
771/* BEGIN_CASE */
772void key_lifetime( int lifetime_arg )
773{
774 int key_slot = 1;
775 psa_key_type_t key_type = PSA_ALG_CBC_BASE;
776 unsigned char key[32] = {0};
777 psa_key_lifetime_t lifetime_set = lifetime_arg;
778 psa_key_lifetime_t lifetime_get;
779
780 memset( key, 0x2a, sizeof( key ) );
781
782 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
783
784 TEST_ASSERT( psa_set_key_lifetime( key_slot,
785 lifetime_set ) == PSA_SUCCESS );
786
787 TEST_ASSERT( psa_import_key( key_slot, key_type,
788 key, sizeof( key ) ) == PSA_SUCCESS );
789
790 TEST_ASSERT( psa_get_key_lifetime( key_slot,
791 &lifetime_get ) == PSA_SUCCESS );
792
793 TEST_ASSERT( lifetime_get == lifetime_set );
794
795exit:
796 psa_destroy_key( key_slot );
797 mbedtls_psa_crypto_free( );
798}
799/* END_CASE */
800
801/* BEGIN_CASE */
802void key_lifetime_set_fail( int key_slot_arg,
803 int lifetime_arg,
804 int expected_status_arg )
805{
806 psa_key_slot_t key_slot = key_slot_arg;
807 psa_key_lifetime_t lifetime_set = lifetime_arg;
808 psa_status_t actual_status;
809 psa_status_t expected_status = expected_status_arg;
810
811 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
812
813 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
814
815 if( actual_status == PSA_SUCCESS )
816 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
817
818 TEST_ASSERT( expected_status == actual_status );
819
820exit:
821 psa_destroy_key( key_slot );
822 mbedtls_psa_crypto_free( );
823}
824/* END_CASE */
825
826/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200827void hash_setup( int alg_arg,
828 int expected_status_arg )
829{
830 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200831 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200832 psa_hash_operation_t operation;
833 psa_status_t status;
834
835 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
836
837 status = psa_hash_start( &operation, alg );
838 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200839 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200840
841exit:
842 mbedtls_psa_crypto_free( );
843}
844/* END_CASE */
845
846/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300847void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100848{
849 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +0200850 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100851 size_t actual_hash_length;
852 psa_hash_operation_t operation;
853
Gilles Peskine69c12672018-06-28 00:07:19 +0200854 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
855 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
856
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100857 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300858 TEST_ASSERT( expected_hash != NULL );
859 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
860 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100861
862 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
863
864 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
865 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200866 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100867 TEST_ASSERT( psa_hash_finish( &operation,
868 actual_hash, sizeof( actual_hash ),
869 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200870 TEST_ASSERT( actual_hash_length == expected_hash->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300871 TEST_ASSERT( memcmp( expected_hash->x, actual_hash,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200872 expected_hash->len ) == 0 );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100873
874exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100875 mbedtls_psa_crypto_free( );
876}
877/* END_CASE */
878
879/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300880void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100881{
882 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100883 psa_hash_operation_t operation;
884
Gilles Peskine69c12672018-06-28 00:07:19 +0200885 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
886 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
887
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100888 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300889 TEST_ASSERT( expected_hash != NULL );
890 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
891 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100892
893 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
894
895 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
896 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200897 input->x,
898 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100899 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300900 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200901 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100902
903exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100904 mbedtls_psa_crypto_free( );
905}
906/* END_CASE */
907
908/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200909void mac_setup( int key_type_arg,
910 data_t *key,
911 int alg_arg,
912 int expected_status_arg )
913{
914 int key_slot = 1;
915 psa_key_type_t key_type = key_type_arg;
916 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200917 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200918 psa_mac_operation_t operation;
919 psa_key_policy_t policy;
920 psa_status_t status;
921
922 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
923
924 psa_key_policy_init( &policy );
925 psa_key_policy_set_usage( &policy,
926 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
927 alg );
928 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
929
930 TEST_ASSERT( psa_import_key( key_slot, key_type,
931 key->x, key->len ) == PSA_SUCCESS );
932
933 status = psa_mac_start( &operation, key_slot, alg );
934 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200935 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200936
937exit:
938 psa_destroy_key( key_slot );
939 mbedtls_psa_crypto_free( );
940}
941/* END_CASE */
942
943/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +0200944void mac_verify( int key_type_arg,
945 data_t *key,
946 int alg_arg,
947 data_t *input,
948 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +0100949{
950 int key_slot = 1;
951 psa_key_type_t key_type = key_type_arg;
952 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100953 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -0700954 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +0100955
Gilles Peskine69c12672018-06-28 00:07:19 +0200956 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
957
Gilles Peskine8c9def32018-02-08 10:02:12 +0100958 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100959 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100960 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300961 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300962 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
963 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100964
965 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
966
mohammad16036df908f2018-04-02 08:34:15 -0700967 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200968 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -0700969 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
970
Gilles Peskine8c9def32018-02-08 10:02:12 +0100971 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200972 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +0200973
Gilles Peskine8c9def32018-02-08 10:02:12 +0100974 TEST_ASSERT( psa_mac_start( &operation, key_slot, alg ) == PSA_SUCCESS );
975 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
976 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200977 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100978 TEST_ASSERT( psa_mac_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +0300979 expected_mac->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200980 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100981
982exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +0100983 psa_destroy_key( key_slot );
984 mbedtls_psa_crypto_free( );
985}
986/* END_CASE */
987
988/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200989void cipher_setup( int key_type_arg,
990 data_t *key,
991 int alg_arg,
992 int expected_status_arg )
993{
994 int key_slot = 1;
995 psa_key_type_t key_type = key_type_arg;
996 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200997 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +0200998 psa_cipher_operation_t operation;
999 psa_key_policy_t policy;
1000 psa_status_t status;
1001
1002 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1003
1004 psa_key_policy_init( &policy );
1005 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1006 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1007
1008 TEST_ASSERT( psa_import_key( key_slot, key_type,
1009 key->x, key->len ) == PSA_SUCCESS );
1010
1011 status = psa_encrypt_setup( &operation, key_slot, alg );
1012 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001013 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001014
1015exit:
1016 psa_destroy_key( key_slot );
1017 mbedtls_psa_crypto_free( );
1018}
1019/* END_CASE */
1020
1021/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001022void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001023 data_t *key,
1024 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001025 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001026{
1027 int key_slot = 1;
1028 psa_status_t status;
1029 psa_key_type_t key_type = key_type_arg;
1030 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001031 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001032 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001033 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001034 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001035 size_t output_buffer_size = 0;
1036 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001037 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001038 psa_cipher_operation_t operation;
1039
Gilles Peskine50e586b2018-06-08 14:28:46 +02001040 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001041 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001042 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001043 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1044 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1045 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001046
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001047 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1048 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001049
1050 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1051
1052 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001053 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001054
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001055 TEST_ASSERT( psa_encrypt_setup( &operation,
1056 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001057
1058 TEST_ASSERT( psa_encrypt_set_iv( &operation,
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001059 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001060 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001061 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001062 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001063
Gilles Peskine4abf7412018-06-18 16:35:34 +02001064 TEST_ASSERT( psa_cipher_update( &operation,
1065 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001066 output, output_buffer_size,
1067 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001068 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001069 status = psa_cipher_finish( &operation,
1070 output + function_output_length,
1071 output_buffer_size,
1072 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001073 total_output_length += function_output_length;
1074
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001075 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001076 if( expected_status == PSA_SUCCESS )
1077 {
1078 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001079 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001080 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001081 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001082 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001083
Gilles Peskine50e586b2018-06-08 14:28:46 +02001084exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001085 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001086 psa_destroy_key( key_slot );
1087 mbedtls_psa_crypto_free( );
1088}
1089/* END_CASE */
1090
1091/* BEGIN_CASE */
1092void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001093 data_t *key,
1094 data_t *input,
1095 int first_part_size,
1096 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001097{
1098 int key_slot = 1;
1099 psa_key_type_t key_type = key_type_arg;
1100 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001101 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001102 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001103 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001104 size_t output_buffer_size = 0;
1105 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001106 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001107 psa_cipher_operation_t operation;
1108
Gilles Peskine50e586b2018-06-08 14:28:46 +02001109 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001110 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001111 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001112 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1113 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1114 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001115
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001116 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1117 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001118
1119 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1120
1121 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001122 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001123
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001124 TEST_ASSERT( psa_encrypt_setup( &operation,
1125 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001126
1127 TEST_ASSERT( psa_encrypt_set_iv( &operation,
1128 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001129 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001130 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001131 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001132
Gilles Peskine4abf7412018-06-18 16:35:34 +02001133 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001134 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001135 output, output_buffer_size,
1136 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001137 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001138 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001139 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001140 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001141 output, output_buffer_size,
1142 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001143 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001144 TEST_ASSERT( psa_cipher_finish( &operation,
1145 output + function_output_length,
1146 output_buffer_size,
1147 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001148 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001149 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1150
Gilles Peskine4abf7412018-06-18 16:35:34 +02001151 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001152 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001153 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001154
1155exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001156 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001157 psa_destroy_key( key_slot );
1158 mbedtls_psa_crypto_free( );
1159}
1160/* END_CASE */
1161
1162/* BEGIN_CASE */
1163void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001164 data_t *key,
1165 data_t *input,
1166 int first_part_size,
1167 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001168{
1169 int key_slot = 1;
1170
1171 psa_key_type_t key_type = key_type_arg;
1172 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001173 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001174 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001175 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001176 size_t output_buffer_size = 0;
1177 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001178 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001179 psa_cipher_operation_t operation;
1180
Gilles Peskine50e586b2018-06-08 14:28:46 +02001181 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001182 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001183 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001184 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1185 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1186 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001187
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001188 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1189 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001190
1191 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1192
1193 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001194 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001195
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001196 TEST_ASSERT( psa_decrypt_setup( &operation,
1197 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001198
1199 TEST_ASSERT( psa_encrypt_set_iv( &operation,
1200 iv, sizeof( iv ) ) == PSA_SUCCESS );
1201
Gilles Peskine4abf7412018-06-18 16:35:34 +02001202 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001203 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001204 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001205
Gilles Peskine4abf7412018-06-18 16:35:34 +02001206 TEST_ASSERT( (unsigned int) first_part_size < input->len );
1207 TEST_ASSERT( psa_cipher_update( &operation,
1208 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001209 output, output_buffer_size,
1210 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001211 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001212 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001213 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001214 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001215 output, output_buffer_size,
1216 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001217 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001218 TEST_ASSERT( psa_cipher_finish( &operation,
1219 output + function_output_length,
1220 output_buffer_size,
1221 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001222 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001223 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1224
Gilles Peskine4abf7412018-06-18 16:35:34 +02001225 TEST_ASSERT( total_output_length == expected_output->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001226 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001227 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001228
1229exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001230 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001231 psa_destroy_key( key_slot );
1232 mbedtls_psa_crypto_free( );
1233}
1234/* END_CASE */
1235
Gilles Peskine50e586b2018-06-08 14:28:46 +02001236/* BEGIN_CASE */
1237void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001238 data_t *key,
1239 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001240 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001241{
1242 int key_slot = 1;
1243 psa_status_t status;
1244 psa_key_type_t key_type = key_type_arg;
1245 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001246 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001247 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001248 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001249 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001250 size_t output_buffer_size = 0;
1251 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001252 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001253 psa_cipher_operation_t operation;
1254
Gilles Peskine50e586b2018-06-08 14:28:46 +02001255 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001256 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001257 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001258 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1259 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1260 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001261
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001262 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1263 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001264
1265 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1266
1267 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001268 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001269
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001270 TEST_ASSERT( psa_decrypt_setup( &operation,
1271 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001272
1273 TEST_ASSERT( psa_encrypt_set_iv( &operation,
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001274 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001275
Gilles Peskine4abf7412018-06-18 16:35:34 +02001276 output_buffer_size = input->len + operation.block_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001277 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001278 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001279
Gilles Peskine4abf7412018-06-18 16:35:34 +02001280 TEST_ASSERT( psa_cipher_update( &operation,
1281 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001282 output, output_buffer_size,
1283 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001284 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001285 status = psa_cipher_finish( &operation,
1286 output + function_output_length,
1287 output_buffer_size,
1288 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001289 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001290 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001291
1292 if( expected_status == PSA_SUCCESS )
1293 {
1294 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001295 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001296 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001297 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001298 }
1299
Gilles Peskine50e586b2018-06-08 14:28:46 +02001300exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001301 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001302 psa_destroy_key( key_slot );
1303 mbedtls_psa_crypto_free( );
1304}
1305/* END_CASE */
1306
Gilles Peskine50e586b2018-06-08 14:28:46 +02001307/* BEGIN_CASE */
1308void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001309 data_t *key,
1310 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02001311{
1312 int key_slot = 1;
1313 psa_key_type_t key_type = key_type_arg;
1314 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07001315 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02001316 size_t iv_size = 16;
1317 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001318 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001319 size_t output1_size = 0;
1320 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001321 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001322 size_t output2_size = 0;
1323 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001324 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001325 psa_cipher_operation_t operation1;
1326 psa_cipher_operation_t operation2;
1327
mohammad1603d7d7ba52018-03-12 18:51:53 +02001328 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001329 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001330 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1331 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001332
mohammad1603d7d7ba52018-03-12 18:51:53 +02001333 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1334
1335 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001336 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001337
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001338 TEST_ASSERT( psa_encrypt_setup( &operation1,
1339 key_slot, alg ) == PSA_SUCCESS );
1340 TEST_ASSERT( psa_decrypt_setup( &operation2,
1341 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001342
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001343 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
1344 iv, iv_size,
1345 &iv_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001346 output1_size = input->len + operation1.block_size;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001347 output1 = mbedtls_calloc( 1, output1_size );
1348 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001349
Gilles Peskine4abf7412018-06-18 16:35:34 +02001350 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001351 output1, output1_size,
1352 &output1_length ) == PSA_SUCCESS );
1353 TEST_ASSERT( psa_cipher_finish( &operation1,
1354 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001355 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001356
Gilles Peskine048b7f02018-06-08 14:20:49 +02001357 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001358
1359 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1360
1361 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001362 output2 = mbedtls_calloc( 1, output2_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001363 TEST_ASSERT( output2 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001364
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001365 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
1366 iv, iv_length ) == PSA_SUCCESS );
1367 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
1368 output2, output2_size,
1369 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001370 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001371 TEST_ASSERT( psa_cipher_finish( &operation2,
1372 output2 + output2_length,
1373 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001374 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001375
Gilles Peskine048b7f02018-06-08 14:20:49 +02001376 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001377
Moran Pekerded84402018-06-06 16:36:50 +03001378 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1379
Gilles Peskine4abf7412018-06-18 16:35:34 +02001380 TEST_ASSERT( input->len == output2_length );
1381 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +03001382
1383exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001384 mbedtls_free( output1 );
1385 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03001386 psa_destroy_key( key_slot );
1387 mbedtls_psa_crypto_free( );
1388}
1389/* END_CASE */
1390
1391/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001392void cipher_verify_output_multipart( int alg_arg,
1393 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001394 data_t *key,
1395 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001396 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03001397{
1398 int key_slot = 1;
1399 psa_key_type_t key_type = key_type_arg;
1400 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03001401 unsigned char iv[16] = {0};
1402 size_t iv_size = 16;
1403 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001404 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001405 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001406 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001407 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001408 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001409 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001410 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001411 psa_cipher_operation_t operation1;
1412 psa_cipher_operation_t operation2;
1413
Moran Pekerded84402018-06-06 16:36:50 +03001414 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001415 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001416 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1417 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001418
Moran Pekerded84402018-06-06 16:36:50 +03001419 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1420
1421 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001422 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001423
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001424 TEST_ASSERT( psa_encrypt_setup( &operation1,
1425 key_slot, alg ) == PSA_SUCCESS );
1426 TEST_ASSERT( psa_decrypt_setup( &operation2,
1427 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001428
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001429 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
1430 iv, iv_size,
1431 &iv_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001432 output1_buffer_size = input->len + operation1.block_size;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001433 output1 = mbedtls_calloc( 1, output1_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001434 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001435
Gilles Peskine4abf7412018-06-18 16:35:34 +02001436 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001437
itayzafrir3e02b3b2018-06-12 17:06:52 +03001438 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001439 output1, output1_buffer_size,
1440 &function_output_length ) == PSA_SUCCESS );
1441 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001442
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001443 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001444 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001445 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001446 output1, output1_buffer_size,
1447 &function_output_length ) == PSA_SUCCESS );
1448 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001449
Gilles Peskine048b7f02018-06-08 14:20:49 +02001450 TEST_ASSERT( psa_cipher_finish( &operation1,
1451 output1 + output1_length,
1452 output1_buffer_size - output1_length,
1453 &function_output_length ) == PSA_SUCCESS );
1454 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001455
1456 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1457
Gilles Peskine048b7f02018-06-08 14:20:49 +02001458 output2_buffer_size = output1_length;
1459 output2 = mbedtls_calloc( 1, output2_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001460 TEST_ASSERT( output2 != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001461
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001462 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
1463 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001464
1465 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001466 output2, output2_buffer_size,
1467 &function_output_length ) == PSA_SUCCESS );
1468 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001469
Gilles Peskine048b7f02018-06-08 14:20:49 +02001470 TEST_ASSERT( psa_cipher_update( &operation2,
1471 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001472 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001473 output2, output2_buffer_size,
1474 &function_output_length ) == PSA_SUCCESS );
1475 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001476
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001477 TEST_ASSERT( psa_cipher_finish( &operation2,
1478 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001479 output2_buffer_size - output2_length,
1480 &function_output_length ) == PSA_SUCCESS );
1481 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001482
mohammad1603d7d7ba52018-03-12 18:51:53 +02001483 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1484
Gilles Peskine4abf7412018-06-18 16:35:34 +02001485 TEST_ASSERT( input->len == output2_length );
1486 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001487
1488exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001489 mbedtls_free( output1 );
1490 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001491 psa_destroy_key( key_slot );
1492 mbedtls_psa_crypto_free( );
1493}
1494/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02001495
Gilles Peskine20035e32018-02-03 22:44:14 +01001496/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001497void aead_encrypt_decrypt( int key_type_arg,
1498 data_t * key_data,
1499 int alg_arg,
1500 data_t * input_data,
1501 data_t * nonce,
1502 data_t * additional_data,
1503 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001504{
1505 int slot = 1;
1506 psa_key_type_t key_type = key_type_arg;
1507 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001508 unsigned char *output_data = NULL;
1509 size_t output_size = 0;
1510 size_t output_length = 0;
1511 unsigned char *output_data2 = NULL;
1512 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001513 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001514 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001515 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001516
Gilles Peskinea1cac842018-06-11 19:33:02 +02001517 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001518 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001519 TEST_ASSERT( nonce != NULL );
1520 TEST_ASSERT( additional_data != NULL );
1521 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1522 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1523 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1524 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1525
Gilles Peskine4abf7412018-06-18 16:35:34 +02001526 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001527 output_data = mbedtls_calloc( 1, output_size );
1528 TEST_ASSERT( output_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001529
1530 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1531
1532 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001533 psa_key_policy_set_usage( &policy,
1534 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
1535 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001536 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1537
1538 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001539 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001540
1541 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001542 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001543 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001544 additional_data->len,
1545 input_data->x, input_data->len,
1546 output_data, output_size,
1547 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001548
1549 if( PSA_SUCCESS == expected_result )
1550 {
1551 output_data2 = mbedtls_calloc( 1, output_length );
1552 TEST_ASSERT( output_data2 != NULL );
1553
1554 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001555 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001556 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001557 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001558 output_data, output_length,
1559 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001560 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02001561
itayzafrir3e02b3b2018-06-12 17:06:52 +03001562 TEST_ASSERT( memcmp( input_data->x, output_data2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001563 input_data->len ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001564 }
Gilles Peskine2d277862018-06-18 15:41:12 +02001565
Gilles Peskinea1cac842018-06-11 19:33:02 +02001566exit:
1567 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001568 mbedtls_free( output_data );
1569 mbedtls_free( output_data2 );
1570 mbedtls_psa_crypto_free( );
1571}
1572/* END_CASE */
1573
1574/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001575void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001576 int alg_arg, data_t * input_data,
1577 data_t * additional_data, data_t * nonce,
1578 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001579{
1580 int slot = 1;
1581 psa_key_type_t key_type = key_type_arg;
1582 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001583 unsigned char *output_data = NULL;
1584 size_t output_size = 0;
1585 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001586 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001587 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001588
Gilles Peskinea1cac842018-06-11 19:33:02 +02001589 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001590 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001591 TEST_ASSERT( additional_data != NULL );
1592 TEST_ASSERT( nonce != NULL );
1593 TEST_ASSERT( expected_result != NULL );
1594 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1595 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1596 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1597 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1598 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
1599
Gilles Peskine4abf7412018-06-18 16:35:34 +02001600 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001601 output_data = mbedtls_calloc( 1, output_size );
1602 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001603
Gilles Peskinea1cac842018-06-11 19:33:02 +02001604 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1605
1606 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001607 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001608 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1609
1610 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001611 key_data->x,
1612 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001613
1614 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001615 nonce->x, nonce->len,
1616 additional_data->x, additional_data->len,
1617 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001618 output_data, output_size,
1619 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001620
itayzafrir3e02b3b2018-06-12 17:06:52 +03001621 TEST_ASSERT( memcmp( output_data, expected_result->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001622 output_length ) == 0 );
1623
Gilles Peskinea1cac842018-06-11 19:33:02 +02001624exit:
1625 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001626 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001627 mbedtls_psa_crypto_free( );
1628}
1629/* END_CASE */
1630
1631/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001632void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001633 int alg_arg, data_t * input_data,
1634 data_t * additional_data, data_t * nonce,
1635 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001636{
1637 int slot = 1;
1638 psa_key_type_t key_type = key_type_arg;
1639 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001640 unsigned char *output_data = NULL;
1641 size_t output_size = 0;
1642 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001643 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001644 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001645 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001646
Gilles Peskinea1cac842018-06-11 19:33:02 +02001647 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001648 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001649 TEST_ASSERT( additional_data != NULL );
1650 TEST_ASSERT( nonce != NULL );
1651 TEST_ASSERT( expected_data != NULL );
1652 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1653 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1654 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1655 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1656 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
1657
Gilles Peskine4abf7412018-06-18 16:35:34 +02001658 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001659 output_data = mbedtls_calloc( 1, output_size );
1660 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001661
Gilles Peskinea1cac842018-06-11 19:33:02 +02001662 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1663
1664 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001665 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001666 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1667
1668 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001669 key_data->x,
1670 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001671
1672 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001673 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001674 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001675 additional_data->len,
1676 input_data->x, input_data->len,
1677 output_data, output_size,
1678 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001679
Gilles Peskine2d277862018-06-18 15:41:12 +02001680 if( expected_result == PSA_SUCCESS )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001681 {
itayzafrir3e02b3b2018-06-12 17:06:52 +03001682 TEST_ASSERT( memcmp( output_data, expected_data->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001683 output_length ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001684 }
1685
Gilles Peskinea1cac842018-06-11 19:33:02 +02001686exit:
1687 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001688 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001689 mbedtls_psa_crypto_free( );
1690}
1691/* END_CASE */
1692
1693/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001694void signature_size( int type_arg,
1695 int bits,
1696 int alg_arg,
1697 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001698{
1699 psa_key_type_t type = type_arg;
1700 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02001701 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01001702 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
1703exit:
1704 ;
1705}
1706/* END_CASE */
1707
1708/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001709void sign_deterministic( int key_type_arg, data_t *key_data,
1710 int alg_arg, data_t *input_data,
1711 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001712{
1713 int slot = 1;
1714 psa_key_type_t key_type = key_type_arg;
1715 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01001716 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01001717 unsigned char *signature = NULL;
1718 size_t signature_size;
1719 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02001720 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01001721
Gilles Peskine20035e32018-02-03 22:44:14 +01001722 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001723 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001724 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001725 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1726 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1727 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01001728
1729 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1730
mohammad1603a97cb8c2018-03-28 03:46:26 -07001731 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001732 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001733 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1734
Gilles Peskine20035e32018-02-03 22:44:14 +01001735 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001736 key_data->x,
1737 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01001738 TEST_ASSERT( psa_get_key_information( slot,
1739 NULL,
1740 &key_bits ) == PSA_SUCCESS );
1741
Gilles Peskine860ce9d2018-06-28 12:23:00 +02001742 /* Allocate a buffer which has the size advertized by the
1743 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001744 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
1745 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01001746 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02001747 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine20035e32018-02-03 22:44:14 +01001748 signature = mbedtls_calloc( 1, signature_size );
1749 TEST_ASSERT( signature != NULL );
1750
Gilles Peskine860ce9d2018-06-28 12:23:00 +02001751 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01001752 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001753 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01001754 NULL, 0,
1755 signature, signature_size,
1756 &signature_length ) == PSA_SUCCESS );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02001757 /* Verify that the signature is correct. */
Gilles Peskine4abf7412018-06-18 16:35:34 +02001758 TEST_ASSERT( signature_length == output_data->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001759 TEST_ASSERT( memcmp( signature, output_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001760 output_data->len ) == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01001761
1762exit:
1763 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01001764 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01001765 mbedtls_psa_crypto_free( );
1766}
1767/* END_CASE */
1768
1769/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001770void sign_fail( int key_type_arg, data_t *key_data,
1771 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02001772 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001773{
1774 int slot = 1;
1775 psa_key_type_t key_type = key_type_arg;
1776 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02001777 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01001778 psa_status_t actual_status;
1779 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01001780 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01001781 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02001782 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01001783
Gilles Peskine20035e32018-02-03 22:44:14 +01001784 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001785 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001786 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1787 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1788
Gilles Peskine20035e32018-02-03 22:44:14 +01001789 signature = mbedtls_calloc( 1, signature_size );
1790 TEST_ASSERT( signature != NULL );
1791
1792 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1793
mohammad1603a97cb8c2018-03-28 03:46:26 -07001794 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001795 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001796 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1797
Gilles Peskine20035e32018-02-03 22:44:14 +01001798 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001799 key_data->x,
1800 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01001801
1802 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001803 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01001804 NULL, 0,
1805 signature, signature_size,
1806 &signature_length );
1807 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02001808 /* The value of *signature_length is unspecified on error, but
1809 * whatever it is, it should be less than signature_size, so that
1810 * if the caller tries to read *signature_length bytes without
1811 * checking the error code then they don't overflow a buffer. */
1812 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01001813
1814exit:
1815 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01001816 mbedtls_free( signature );
1817 mbedtls_psa_crypto_free( );
1818}
1819/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03001820
1821/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001822void asymmetric_verify( int key_type_arg, data_t *key_data,
1823 int alg_arg, data_t *hash_data,
1824 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03001825{
1826 int slot = 1;
1827 psa_key_type_t key_type = key_type_arg;
1828 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001829 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03001830
Gilles Peskine69c12672018-06-28 00:07:19 +02001831 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
1832
itayzafrir5c753392018-05-08 11:18:38 +03001833 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03001834 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03001835 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001836 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1837 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
1838 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03001839
1840 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1841
1842 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001843 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03001844 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1845
1846 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001847 key_data->x,
1848 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03001849
1850 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001851 hash_data->x, hash_data->len,
itayzafrir5c753392018-05-08 11:18:38 +03001852 NULL, 0,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001853 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001854 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03001855exit:
1856 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03001857 mbedtls_psa_crypto_free( );
1858}
1859/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001860
1861/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001862void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
1863 int alg_arg, data_t *hash_data,
1864 data_t *signature_data,
1865 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001866{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001867 int slot = 1;
1868 psa_key_type_t key_type = key_type_arg;
1869 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001870 psa_status_t actual_status;
1871 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001872 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001873
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001874 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001875 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001876 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001877 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1878 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
1879 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001880
1881 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1882
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001883 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001884 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001885 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1886
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001887 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001888 key_data->x,
1889 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001890
1891 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001892 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001893 NULL, 0,
1894 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001895 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001896
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001897 TEST_ASSERT( actual_status == expected_status );
1898
1899exit:
1900 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001901 mbedtls_psa_crypto_free( );
1902}
1903/* END_CASE */
1904
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001905/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001906void asymmetric_encrypt_decrypt( int key_type_arg, data_t *key_data,
1907 int alg_arg, data_t *input_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001908{
1909 int slot = 1;
1910 psa_key_type_t key_type = key_type_arg;
1911 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001912 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001913 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001914 size_t output_length = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001915 unsigned char *output2 = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001916 size_t output2_size = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001917 size_t output2_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02001918 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001919
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001920 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001921 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001922 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1923 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1924
Gilles Peskine4abf7412018-06-18 16:35:34 +02001925 output_size = key_data->len;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001926 output2_size = output_size;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001927 output = mbedtls_calloc( 1, output_size );
1928 TEST_ASSERT( output != NULL );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001929 output2 = mbedtls_calloc( 1, output2_size );
1930 TEST_ASSERT( output2 != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001931
1932 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1933
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001934 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001935 psa_key_policy_set_usage( &policy,
1936 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001937 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001938 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1939
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001940 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001941 key_data->x,
1942 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001943
Gilles Peskineeebd7382018-06-08 18:11:54 +02001944 /* We test encryption by checking that encrypt-then-decrypt gives back
1945 * the original plaintext because of the non-optional random
1946 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02001947 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001948 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001949 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001950 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001951 &output_length ) == PSA_SUCCESS );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03001952
Gilles Peskine2d277862018-06-18 15:41:12 +02001953 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001954 output, output_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001955 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001956 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02001957 &output2_length ) == PSA_SUCCESS );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001958 TEST_ASSERT( memcmp( input_data->x, output2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001959 input_data->len ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001960
1961exit:
1962 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001963 mbedtls_free( output );
1964 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001965 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001966}
1967/* END_CASE */
1968
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001969/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001970void asymmetric_encrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001971 int alg_arg, data_t *input_data,
1972 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001973{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001974 int slot = 1;
1975 psa_key_type_t key_type = key_type_arg;
1976 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001977 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03001978 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001979 size_t output_length = 0;
1980 psa_status_t actual_status;
1981 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001982 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001983
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001984 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001985 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001986 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1987 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1988
Gilles Peskine4abf7412018-06-18 16:35:34 +02001989 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001990 output = mbedtls_calloc( 1, output_size );
1991 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001992
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001993 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1994
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001995 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001996 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03001997 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1998
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001999 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002000 key_data->x,
2001 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002002
Gilles Peskine2d277862018-06-18 15:41:12 +02002003 actual_status = psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002004 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002005 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002006 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002007 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002008 TEST_ASSERT( actual_status == expected_status );
2009
2010exit:
2011 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002012 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002013 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002014}
2015/* END_CASE */
2016
2017/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002018void asymmetric_decrypt( int key_type_arg, data_t *key_data,
2019 int alg_arg, data_t *input_data,
2020 data_t *expected_data, int expected_size )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002021{
2022 int slot = 1;
2023 psa_key_type_t key_type = key_type_arg;
2024 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002025 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002026 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002027 size_t output_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002028 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002029
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002030 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002031 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002032 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002033 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2034 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2035 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2036
Gilles Peskine4abf7412018-06-18 16:35:34 +02002037 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002038 output = mbedtls_calloc( 1, output_size );
2039 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002040
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002041 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2042
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002043 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002044 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002045 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2046
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002047 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002048 key_data->x,
2049 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002050
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002051 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002052 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002053 NULL, 0,
2054 output,
2055 output_size,
2056 &output_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002057 TEST_ASSERT( (size_t) expected_size == output_length );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002058 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002059
2060exit:
2061 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002062 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002063 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002064}
2065/* END_CASE */
2066
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002067/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002068void asymmetric_decrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002069 int alg_arg, data_t *input_data,
2070 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002071{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002072 int slot = 1;
2073 psa_key_type_t key_type = key_type_arg;
2074 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002075 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002076 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002077 size_t output_length = 0;
2078 psa_status_t actual_status;
2079 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002080 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002081
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002082 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002083 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002084 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2085 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2086
Gilles Peskine4abf7412018-06-18 16:35:34 +02002087 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002088 output = mbedtls_calloc( 1, output_size );
2089 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002090
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002091 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2092
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002093 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002094 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002095 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2096
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002097 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002098 key_data->x,
2099 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002100
Gilles Peskine2d277862018-06-18 15:41:12 +02002101 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002102 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002103 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002104 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002105 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002106 TEST_ASSERT( actual_status == expected_status );
2107
2108exit:
2109 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02002110 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002111 mbedtls_psa_crypto_free( );
2112}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002113/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02002114
2115/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02002116void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02002117{
Gilles Peskinea50d7392018-06-21 10:22:13 +02002118 size_t bytes = bytes_arg;
2119 const unsigned char trail[] = "don't overwrite me";
2120 unsigned char *output = mbedtls_calloc( 1, bytes + sizeof( trail ) );
2121 unsigned char *changed = mbedtls_calloc( 1, bytes );
2122 size_t i;
2123 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02002124
Gilles Peskinea50d7392018-06-21 10:22:13 +02002125 TEST_ASSERT( output != NULL );
2126 TEST_ASSERT( changed != NULL );
2127 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02002128
2129 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2130
Gilles Peskinea50d7392018-06-21 10:22:13 +02002131 /* Run several times, to ensure that every output byte will be
2132 * nonzero at least once with overwhelming probability
2133 * (2^(-8*number_of_runs)). */
2134 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02002135 {
Gilles Peskinea50d7392018-06-21 10:22:13 +02002136 memset( output, 0, bytes );
2137 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
2138
2139 /* Check that no more than bytes have been overwritten */
2140 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
2141
2142 for( i = 0; i < bytes; i++ )
2143 {
2144 if( output[i] != 0 )
2145 ++changed[i];
2146 }
Gilles Peskine05d69892018-06-19 22:00:52 +02002147 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02002148
2149 /* Check that every byte was changed to nonzero at least once. This
2150 * validates that psa_generate_random is overwriting every byte of
2151 * the output buffer. */
2152 for( i = 0; i < bytes; i++ )
2153 {
2154 TEST_ASSERT( changed[i] != 0 );
2155 }
Gilles Peskine05d69892018-06-19 22:00:52 +02002156
2157exit:
2158 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02002159 mbedtls_free( output );
2160 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02002161}
2162/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02002163
2164/* BEGIN_CASE */
2165void generate_key( int type_arg,
2166 int bits_arg,
2167 int usage_arg,
2168 int alg_arg,
2169 int expected_status_arg )
2170{
2171 int slot = 1;
2172 psa_key_type_t type = type_arg;
2173 psa_key_usage_t usage = usage_arg;
2174 size_t bits = bits_arg;
2175 psa_algorithm_t alg = alg_arg;
2176 psa_status_t expected_status = expected_status_arg;
2177 psa_key_type_t got_type;
2178 size_t got_bits;
2179 unsigned char exported[616] = {0}; /* enough for a 1024-bit RSA key */
2180 size_t exported_length;
2181 psa_status_t expected_export_status =
2182 usage & PSA_KEY_USAGE_EXPORT ? PSA_SUCCESS : PSA_ERROR_NOT_PERMITTED;
2183 psa_status_t expected_info_status =
2184 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
2185 psa_key_policy_t policy;
2186
2187 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2188
2189 psa_key_policy_init( &policy );
2190 psa_key_policy_set_usage( &policy, usage, alg );
2191 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2192
2193 /* Generate a key */
2194 TEST_ASSERT( psa_generate_key( slot, type, bits,
2195 NULL, 0 ) == expected_status );
2196
2197 /* Test the key information */
2198 TEST_ASSERT( psa_get_key_information( slot,
2199 &got_type,
2200 &got_bits ) == expected_info_status );
2201 if( expected_info_status != PSA_SUCCESS )
2202 goto exit;
2203 TEST_ASSERT( got_type == type );
2204 TEST_ASSERT( got_bits == bits );
2205
2206 /* Export the key */
2207 TEST_ASSERT( psa_export_key( slot,
2208 exported, sizeof( exported ),
2209 &exported_length ) == expected_export_status );
2210 if( expected_export_status == PSA_SUCCESS )
2211 {
Gilles Peskine48c0ea12018-06-21 14:15:31 +02002212 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02002213 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
2214#if defined(MBEDTLS_DES_C)
2215 if( type == PSA_KEY_TYPE_DES )
2216 {
2217 /* Check the parity bits. */
2218 unsigned i;
2219 for( i = 0; i < bits / 8; i++ )
2220 {
2221 unsigned bit_count = 0;
2222 unsigned m;
2223 for( m = 1; m <= 0x100; m <<= 1 )
2224 {
2225 if( exported[i] & m )
2226 ++bit_count;
2227 }
2228 TEST_ASSERT( bit_count % 2 != 0 );
2229 }
2230 }
2231#endif
2232#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
2233 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
2234 {
2235 /* Sanity check: does this look like the beginning of a PKCS#8
2236 * RSA key pair? Assumes bits is a multiple of 8. */
2237 size_t n_bytes = bits / 8 + 1;
2238 size_t n_encoded_bytes;
2239 unsigned char *n_end;
2240 TEST_ASSERT( exported_length >= 7 + ( n_bytes + 3 ) * 9 / 2 );
2241 TEST_ASSERT( exported[0] == 0x30 );
2242 TEST_ASSERT( exported[1] == 0x82 ); // assumes >=416-bit key
2243 TEST_ASSERT( exported[4] == 0x02 );
2244 TEST_ASSERT( exported[5] == 0x01 );
2245 TEST_ASSERT( exported[6] == 0x00 );
2246 TEST_ASSERT( exported[7] == 0x02 );
2247 n_encoded_bytes = exported[8];
2248 n_end = exported + 9 + n_encoded_bytes;
2249 if( n_encoded_bytes & 0x80 )
2250 {
2251 n_encoded_bytes = ( n_encoded_bytes & 0x7f ) << 7;
2252 n_encoded_bytes |= exported[9] & 0x7f;
2253 n_end += 1;
2254 }
2255 /* The encoding of n should start with a 0 byte since it should
2256 * have its high bit set. However Mbed TLS is not compliant and
2257 * generates an invalid, but widely tolerated, encoding of
2258 * positive INTEGERs with a bit size that is a multiple of 8
2259 * with no leading 0 byte. Accept this here. */
2260 TEST_ASSERT( n_bytes == n_encoded_bytes ||
2261 n_bytes == n_encoded_bytes + 1 );
2262 if( n_bytes == n_encoded_bytes )
2263 TEST_ASSERT( exported[n_encoded_bytes <= 127 ? 9 : 10] == 0x00 );
2264 /* Sanity check: e must be 3 */
2265 TEST_ASSERT( n_end[0] == 0x02 );
2266 TEST_ASSERT( n_end[1] == 0x03 );
2267 TEST_ASSERT( n_end[2] == 0x01 );
2268 TEST_ASSERT( n_end[3] == 0x00 );
2269 TEST_ASSERT( n_end[4] == 0x01 );
2270 TEST_ASSERT( n_end[5] == 0x02 );
2271 }
2272#endif /* MBEDTLS_RSA_C */
2273#if defined(MBEDTLS_ECP_C)
2274 if( PSA_KEY_TYPE_IS_ECC( type ) )
2275 {
2276 /* Sanity check: does this look like the beginning of a PKCS#8
2277 * elliptic curve key pair? */
2278 TEST_ASSERT( exported_length >= bits * 3 / 8 + 10 );
2279 TEST_ASSERT( exported[0] == 0x30 );
2280 }
2281#endif /* MBEDTLS_ECP_C */
2282 }
2283
Gilles Peskine818ca122018-06-20 18:16:48 +02002284 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02002285 if( ! exercise_key( slot, usage, alg ) )
2286 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02002287
2288exit:
2289 psa_destroy_key( slot );
2290 mbedtls_psa_crypto_free( );
2291}
2292/* END_CASE */