blob: 3a03a76bfd511cea3cd8a314aea620b604817957 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
4#if defined(MBEDTLS_PSA_CRYPTO_SPM)
5#include "spm/psa_defs.h"
6#endif
7
Gilles Peskine0b352bc2018-06-28 00:16:11 +02008#include "mbedtls/asn1write.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +01009#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030010
11#if(UINT32_MAX > SIZE_MAX)
Gilles Peskine2d277862018-06-18 15:41:12 +020012#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX )
itayzafrir3e02b3b2018-06-12 17:06:52 +030013#else
Gilles Peskine2d277862018-06-18 15:41:12 +020014#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1
itayzafrir3e02b3b2018-06-12 17:06:52 +030015#endif
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020016
Jaeden Amerof24c7f82018-06-27 17:20:43 +010017/** An invalid export length that will never be set by psa_export_key(). */
18static const size_t INVALID_EXPORT_LENGTH = ~0U;
19
Gilles Peskined35a1cc2018-06-26 21:26:10 +020020/** Test if a buffer is all-bits zero.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020021 *
22 * \param buffer Pointer to the beginning of the buffer.
23 * \param size Size of the buffer in bytes.
24 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020025 * \return 1 if the buffer is all-bits-zero.
26 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020027 */
Gilles Peskine3f669c32018-06-21 09:21:51 +020028static int mem_is_zero( void *buffer, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020029{
30 size_t i;
31 for( i = 0; i < size; i++ )
32 {
33 if( ( (unsigned char *) buffer )[i] != 0 )
Gilles Peskine3f669c32018-06-21 09:21:51 +020034 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020035 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020036 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037}
Gilles Peskine818ca122018-06-20 18:16:48 +020038
Gilles Peskine48c0ea12018-06-21 14:15:31 +020039static int key_type_is_raw_bytes( psa_key_type_t type )
40{
41 psa_key_type_t category = type & PSA_KEY_TYPE_CATEGORY_MASK;
42 return( category == PSA_KEY_TYPE_RAW_DATA ||
43 category == PSA_KEY_TYPE_CATEGORY_SYMMETRIC );
44}
45
Gilles Peskine0b352bc2018-06-28 00:16:11 +020046/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
47static int asn1_write_10x( unsigned char **p,
48 unsigned char *start,
49 size_t bits,
50 unsigned char x )
51{
52 int ret;
53 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020054 if( bits == 0 )
55 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
56 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020057 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
58 if( *p < start || *p - start < (ssize_t) len )
59 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
60 *p -= len;
61 ( *p )[len-1] = x;
62 if( bits % 8 == 0 )
63 ( *p )[1] |= 1;
64 else
65 ( *p )[0] |= 1 << ( bits % 8 );
66 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
67 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
68 MBEDTLS_ASN1_INTEGER ) );
69 return( len );
70}
71
72static int construct_fake_rsa_key( unsigned char *buffer,
73 size_t buffer_size,
74 unsigned char **p,
75 size_t bits,
76 int keypair )
77{
78 size_t half_bits = ( bits + 1 ) / 2;
79 int ret;
80 int len = 0;
81 /* Construct something that looks like a DER encoding of
82 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
83 * RSAPrivateKey ::= SEQUENCE {
84 * version Version,
85 * modulus INTEGER, -- n
86 * publicExponent INTEGER, -- e
87 * privateExponent INTEGER, -- d
88 * prime1 INTEGER, -- p
89 * prime2 INTEGER, -- q
90 * exponent1 INTEGER, -- d mod (p-1)
91 * exponent2 INTEGER, -- d mod (q-1)
92 * coefficient INTEGER, -- (inverse of q) mod p
93 * otherPrimeInfos OtherPrimeInfos OPTIONAL
94 * }
95 * Or, for a public key, the same structure with only
96 * version, modulus and publicExponent.
97 */
98 *p = buffer + buffer_size;
99 if( keypair )
100 {
101 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
102 asn1_write_10x( p, buffer, half_bits, 1 ) );
103 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
104 asn1_write_10x( p, buffer, half_bits, 1 ) );
105 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
106 asn1_write_10x( p, buffer, half_bits, 1 ) );
107 MBEDTLS_ASN1_CHK_ADD( len, /* q */
108 asn1_write_10x( p, buffer, half_bits, 1 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
110 asn1_write_10x( p, buffer, half_bits, 3 ) );
111 MBEDTLS_ASN1_CHK_ADD( len, /* d */
112 asn1_write_10x( p, buffer, bits, 1 ) );
113 }
114 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
115 asn1_write_10x( p, buffer, 17, 1 ) );
116 MBEDTLS_ASN1_CHK_ADD( len, /* n */
117 asn1_write_10x( p, buffer, bits, 1 ) );
118 if( keypair )
119 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
120 mbedtls_asn1_write_int( p, buffer, 0 ) );
121 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
122 {
123 const unsigned char tag =
124 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
125 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
126 }
127 return( len );
128}
129
Gilles Peskine818ca122018-06-20 18:16:48 +0200130static int exercise_mac_key( psa_key_slot_t key,
131 psa_key_usage_t usage,
132 psa_algorithm_t alg )
133{
134 psa_mac_operation_t operation;
135 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200136 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200137 size_t mac_length = sizeof( mac );
138
139 if( usage & PSA_KEY_USAGE_SIGN )
140 {
Gilles Peskine89167cb2018-07-08 20:12:23 +0200141 TEST_ASSERT( psa_mac_sign_setup( &operation,
142 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200143 TEST_ASSERT( psa_mac_update( &operation,
144 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200145 TEST_ASSERT( psa_mac_sign_finish( &operation,
146 mac, sizeof( input ),
147 &mac_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200148 }
149
150 if( usage & PSA_KEY_USAGE_VERIFY )
151 {
152 psa_status_t verify_status =
153 ( usage & PSA_KEY_USAGE_SIGN ?
154 PSA_SUCCESS :
155 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine89167cb2018-07-08 20:12:23 +0200156 TEST_ASSERT( psa_mac_verify_setup( &operation,
157 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200158 TEST_ASSERT( psa_mac_update( &operation,
159 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200160 TEST_ASSERT( psa_mac_verify_finish( &operation,
161 mac,
162 mac_length ) == verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200163 }
164
165 return( 1 );
166
167exit:
168 psa_mac_abort( &operation );
169 return( 0 );
170}
171
172static int exercise_cipher_key( psa_key_slot_t key,
173 psa_key_usage_t usage,
174 psa_algorithm_t alg )
175{
176 psa_cipher_operation_t operation;
177 unsigned char iv[16] = {0};
178 size_t iv_length = sizeof( iv );
179 const unsigned char plaintext[16] = "Hello, world...";
180 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
181 size_t ciphertext_length = sizeof( ciphertext );
182 unsigned char decrypted[sizeof( ciphertext )];
183 size_t part_length;
184
185 if( usage & PSA_KEY_USAGE_ENCRYPT )
186 {
187 TEST_ASSERT( psa_encrypt_setup( &operation, key, alg ) == PSA_SUCCESS );
188 TEST_ASSERT( psa_encrypt_generate_iv( &operation,
189 iv, sizeof( iv ),
190 &iv_length ) == PSA_SUCCESS );
191 TEST_ASSERT( psa_cipher_update( &operation,
192 plaintext, sizeof( plaintext ),
193 ciphertext, sizeof( ciphertext ),
194 &ciphertext_length ) == PSA_SUCCESS );
195 TEST_ASSERT( psa_cipher_finish( &operation,
196 ciphertext + ciphertext_length,
197 sizeof( ciphertext ) - ciphertext_length,
198 &part_length ) == PSA_SUCCESS );
199 ciphertext_length += part_length;
200 }
201
202 if( usage & PSA_KEY_USAGE_DECRYPT )
203 {
204 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700205 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200206 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
207 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200208 size_t bits;
209 TEST_ASSERT( psa_get_key_information( key, &type, &bits ) );
210 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
211 }
212 TEST_ASSERT( psa_decrypt_setup( &operation, key, alg ) == PSA_SUCCESS );
213 TEST_ASSERT( psa_encrypt_set_iv( &operation,
214 iv, iv_length ) == PSA_SUCCESS );
215 TEST_ASSERT( psa_cipher_update( &operation,
216 ciphertext, ciphertext_length,
217 decrypted, sizeof( decrypted ),
218 &part_length ) == PSA_SUCCESS );
219 status = psa_cipher_finish( &operation,
220 decrypted + part_length,
221 sizeof( decrypted ) - part_length,
222 &part_length );
223 /* For a stream cipher, all inputs are valid. For a block cipher,
224 * if the input is some aribtrary data rather than an actual
225 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700226 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700227 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine818ca122018-06-20 18:16:48 +0200228 TEST_ASSERT( status == PSA_SUCCESS );
229 else
230 TEST_ASSERT( status == PSA_SUCCESS ||
231 status == PSA_ERROR_INVALID_PADDING );
232 }
233
234 return( 1 );
235
236exit:
237 psa_cipher_abort( &operation );
238 return( 0 );
239}
240
241static int exercise_aead_key( psa_key_slot_t key,
242 psa_key_usage_t usage,
243 psa_algorithm_t alg )
244{
245 unsigned char nonce[16] = {0};
246 size_t nonce_length = sizeof( nonce );
247 unsigned char plaintext[16] = "Hello, world...";
248 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
249 size_t ciphertext_length = sizeof( ciphertext );
250 size_t plaintext_length = sizeof( ciphertext );
251
252 if( usage & PSA_KEY_USAGE_ENCRYPT )
253 {
254 TEST_ASSERT( psa_aead_encrypt( key, alg,
255 nonce, nonce_length,
256 NULL, 0,
257 plaintext, sizeof( plaintext ),
258 ciphertext, sizeof( ciphertext ),
259 &ciphertext_length ) == PSA_SUCCESS );
260 }
261
262 if( usage & PSA_KEY_USAGE_DECRYPT )
263 {
264 psa_status_t verify_status =
265 ( usage & PSA_KEY_USAGE_ENCRYPT ?
266 PSA_SUCCESS :
267 PSA_ERROR_INVALID_SIGNATURE );
268 TEST_ASSERT( psa_aead_decrypt( key, alg,
269 nonce, nonce_length,
270 NULL, 0,
271 ciphertext, ciphertext_length,
272 plaintext, sizeof( plaintext ),
273 &plaintext_length ) == verify_status );
274 }
275
276 return( 1 );
277
278exit:
279 return( 0 );
280}
281
282static int exercise_signature_key( psa_key_slot_t key,
283 psa_key_usage_t usage,
284 psa_algorithm_t alg )
285{
Gilles Peskineca45c352018-06-26 16:13:09 +0200286 unsigned char payload[16] = {1};
Gilles Peskine818ca122018-06-20 18:16:48 +0200287 size_t payload_length = sizeof( payload );
Gilles Peskine69c12672018-06-28 00:07:19 +0200288 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200289 size_t signature_length = sizeof( signature );
290
291 if( usage & PSA_KEY_USAGE_SIGN )
292 {
293 TEST_ASSERT( psa_asymmetric_sign( key, alg,
294 payload, payload_length,
295 NULL, 0,
296 signature, sizeof( signature ),
297 &signature_length ) == PSA_SUCCESS );
298 }
299
300 if( usage & PSA_KEY_USAGE_VERIFY )
301 {
302 psa_status_t verify_status =
303 ( usage & PSA_KEY_USAGE_SIGN ?
304 PSA_SUCCESS :
305 PSA_ERROR_INVALID_SIGNATURE );
306 TEST_ASSERT( psa_asymmetric_verify( key, alg,
307 payload, payload_length,
308 NULL, 0,
309 signature, signature_length ) ==
310 verify_status );
311 }
312
313 return( 1 );
314
315exit:
316 return( 0 );
317}
318
319static int exercise_asymmetric_encryption_key( psa_key_slot_t key,
320 psa_key_usage_t usage,
321 psa_algorithm_t alg )
322{
323 unsigned char plaintext[256] = "Hello, world...";
324 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
325 size_t ciphertext_length = sizeof( ciphertext );
326 size_t plaintext_length = 16;
327
328 if( usage & PSA_KEY_USAGE_ENCRYPT )
329 {
330 TEST_ASSERT(
331 psa_asymmetric_encrypt( key, alg,
332 plaintext, plaintext_length,
333 NULL, 0,
334 ciphertext, sizeof( ciphertext ),
335 &ciphertext_length ) == PSA_SUCCESS );
336 }
337
338 if( usage & PSA_KEY_USAGE_DECRYPT )
339 {
340 psa_status_t status =
341 psa_asymmetric_decrypt( key, alg,
342 ciphertext, ciphertext_length,
343 NULL, 0,
344 plaintext, sizeof( plaintext ),
345 &plaintext_length );
346 TEST_ASSERT( status == PSA_SUCCESS ||
347 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
348 ( status == PSA_ERROR_INVALID_ARGUMENT ||
349 status == PSA_ERROR_INVALID_PADDING ) ) );
350 }
351
352 return( 1 );
353
354exit:
355 return( 0 );
356}
Gilles Peskine02b75072018-07-01 22:31:34 +0200357
358static int exercise_key( psa_key_slot_t slot,
359 psa_key_usage_t usage,
360 psa_algorithm_t alg )
361{
362 int ok;
363 if( alg == 0 )
364 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
365 else if( PSA_ALG_IS_MAC( alg ) )
366 ok = exercise_mac_key( slot, usage, alg );
367 else if( PSA_ALG_IS_CIPHER( alg ) )
368 ok = exercise_cipher_key( slot, usage, alg );
369 else if( PSA_ALG_IS_AEAD( alg ) )
370 ok = exercise_aead_key( slot, usage, alg );
371 else if( PSA_ALG_IS_SIGN( alg ) )
372 ok = exercise_signature_key( slot, usage, alg );
373 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
374 ok = exercise_asymmetric_encryption_key( slot, usage, alg );
375 else
376 {
377 char message[40];
378 mbedtls_snprintf( message, sizeof( message ),
379 "No code to exercise alg=0x%08lx",
380 (unsigned long) alg );
381 test_fail( message, __LINE__, __FILE__ );
382 ok = 0;
383 }
384 return( ok );
385}
386
Gilles Peskinee59236f2018-01-27 23:32:46 +0100387/* END_HEADER */
388
389/* BEGIN_DEPENDENCIES
390 * depends_on:MBEDTLS_PSA_CRYPTO_C
391 * END_DEPENDENCIES
392 */
393
394/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200395void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100396{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100397 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100398 int i;
399 for( i = 0; i <= 1; i++ )
400 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100401 status = psa_crypto_init( );
402 TEST_ASSERT( status == PSA_SUCCESS );
403 status = psa_crypto_init( );
404 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100405 mbedtls_psa_crypto_free( );
406 }
407}
408/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100409
410/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200411void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100412{
413 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200414 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100415 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100416
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100417 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300418 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100419 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
420
Gilles Peskine4abf7412018-06-18 16:35:34 +0200421 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200422 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100423 if( status == PSA_SUCCESS )
424 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
425
426exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100427 mbedtls_psa_crypto_free( );
428}
429/* END_CASE */
430
431/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200432void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
433{
434 int slot = 1;
435 size_t bits = bits_arg;
436 psa_status_t expected_status = expected_status_arg;
437 psa_status_t status;
438 psa_key_type_t type =
439 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
440 size_t buffer_size = /* Slight overapproximations */
441 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
442 unsigned char *buffer = mbedtls_calloc( 1, buffer_size );
443 unsigned char *p;
444 int ret;
445 size_t length;
446
447 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
448 TEST_ASSERT( buffer != NULL );
449
450 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
451 bits, keypair ) ) >= 0 );
452 length = ret;
453
454 /* Try importing the key */
455 status = psa_import_key( slot, type, p, length );
456 TEST_ASSERT( status == expected_status );
457 if( status == PSA_SUCCESS )
458 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
459
460exit:
461 mbedtls_free( buffer );
462 mbedtls_psa_crypto_free( );
463}
464/* END_CASE */
465
466/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300467void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300468 int type_arg,
469 int alg_arg,
470 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100471 int expected_bits,
472 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200473 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100474 int canonical_input )
475{
476 int slot = 1;
477 int slot2 = slot + 1;
478 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200479 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200480 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100481 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100482 unsigned char *exported = NULL;
483 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100484 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100485 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100486 size_t reexported_length;
487 psa_key_type_t got_type;
488 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200489 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100490
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100491 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300492 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
493 export_size = (ssize_t) data->len + export_size_delta;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100494 exported = mbedtls_calloc( 1, export_size );
495 TEST_ASSERT( exported != NULL );
496 if( ! canonical_input )
497 {
498 reexported = mbedtls_calloc( 1, export_size );
499 TEST_ASSERT( reexported != NULL );
500 }
501 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
502
mohammad1603a97cb8c2018-03-28 03:46:26 -0700503 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200504 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700505 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
506
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100507 /* Import the key */
508 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200509 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100510
511 /* Test the key information */
512 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200513 &got_type,
514 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100515 TEST_ASSERT( got_type == type );
516 TEST_ASSERT( got_bits == (size_t) expected_bits );
517
518 /* Export the key */
519 status = psa_export_key( slot,
520 exported, export_size,
521 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200522 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100523
524 /* The exported length must be set by psa_export_key() to a value between 0
525 * and export_size. On errors, the exported length must be 0. */
526 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
527 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
528 TEST_ASSERT( exported_length <= export_size );
529
Gilles Peskine3f669c32018-06-21 09:21:51 +0200530 TEST_ASSERT( mem_is_zero( exported + exported_length,
531 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100532 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200533 {
534 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100535 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200536 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100537
538 if( canonical_input )
539 {
Gilles Peskine4abf7412018-06-18 16:35:34 +0200540 TEST_ASSERT( exported_length == data->len );
541 TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100542 }
543 else
544 {
mohammad1603a97cb8c2018-03-28 03:46:26 -0700545 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
546
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100547 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200548 exported,
549 export_size ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100550 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200551 reexported,
552 export_size,
553 &reexported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100554 TEST_ASSERT( reexported_length == exported_length );
555 TEST_ASSERT( memcmp( reexported, exported,
556 exported_length ) == 0 );
557 }
558
559destroy:
560 /* Destroy the key */
561 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
562 TEST_ASSERT( psa_get_key_information(
563 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
564
565exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300566 mbedtls_free( exported );
567 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100568 mbedtls_psa_crypto_free( );
569}
570/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100571
Moran Pekerf709f4a2018-06-06 17:26:04 +0300572/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300573void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200574 int type_arg,
575 int alg_arg,
576 int expected_bits,
577 int public_key_expected_length,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200578 int expected_export_status_arg )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300579{
580 int slot = 1;
581 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200582 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200583 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300584 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300585 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300586 size_t export_size;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100587 size_t exported_length = INVALID_EXPORT_LENGTH;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300588 psa_key_type_t got_type;
589 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200590 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300591
Moran Pekerf709f4a2018-06-06 17:26:04 +0300592 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300593 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
594 export_size = (ssize_t) data->len;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300595 exported = mbedtls_calloc( 1, export_size );
596 TEST_ASSERT( exported != NULL );
597
598 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
599
600 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200601 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300602 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
603
604 /* Import the key */
605 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200606 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300607
608 /* Test the key information */
609 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200610 &got_type,
611 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300612 TEST_ASSERT( got_type == type );
613 TEST_ASSERT( got_bits == (size_t) expected_bits );
614
615 /* Export the key */
616 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200617 exported, export_size,
618 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200619 TEST_ASSERT( status == expected_export_status );
Jaeden Amero2a671e92018-06-27 17:47:40 +0100620 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
621 TEST_ASSERT( mem_is_zero( exported + exported_length,
622 export_size - exported_length ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300623 if( status != PSA_SUCCESS )
624 goto destroy;
625
Moran Pekerf709f4a2018-06-06 17:26:04 +0300626destroy:
627 /* Destroy the key */
628 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
629 TEST_ASSERT( psa_get_key_information(
630 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
631
632exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300633 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300634 mbedtls_psa_crypto_free( );
635}
636/* END_CASE */
637
Gilles Peskine20035e32018-02-03 22:44:14 +0100638/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200639void import_and_exercise_key( data_t *data,
640 int type_arg,
641 int bits_arg,
642 int alg_arg )
643{
644 int slot = 1;
645 psa_key_type_t type = type_arg;
646 size_t bits = bits_arg;
647 psa_algorithm_t alg = alg_arg;
648 psa_key_usage_t usage =
649 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
650 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
651 PSA_KEY_USAGE_VERIFY :
652 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
653 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
654 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
655 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
656 PSA_KEY_USAGE_ENCRYPT :
657 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
658 0 );
659 psa_key_policy_t policy;
660 psa_key_type_t got_type;
661 size_t got_bits;
662 psa_status_t status;
663
664 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
665
666 psa_key_policy_init( &policy );
667 psa_key_policy_set_usage( &policy, usage, alg );
668 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
669
670 /* Import the key */
671 status = psa_import_key( slot, type, data->x, data->len );
672 TEST_ASSERT( status == PSA_SUCCESS );
673
674 /* Test the key information */
675 TEST_ASSERT( psa_get_key_information( slot,
676 &got_type,
677 &got_bits ) == PSA_SUCCESS );
678 TEST_ASSERT( got_type == type );
679 TEST_ASSERT( got_bits == bits );
680
681 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +0200682 if( ! exercise_key( slot, usage, alg ) )
683 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200684
685exit:
686 psa_destroy_key( slot );
687 mbedtls_psa_crypto_free( );
688}
689/* END_CASE */
690
691/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +0200692void key_policy( int usage_arg, int alg_arg )
693{
694 int key_slot = 1;
695 psa_algorithm_t alg = alg_arg;
696 psa_key_usage_t usage = usage_arg;
697 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
698 unsigned char key[32] = {0};
699 psa_key_policy_t policy_set;
700 psa_key_policy_t policy_get;
701
702 memset( key, 0x2a, sizeof( key ) );
703
704 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
705
706 psa_key_policy_init( &policy_set );
707 psa_key_policy_init( &policy_get );
708
709 psa_key_policy_set_usage( &policy_set, usage, alg );
710
711 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
712 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
713 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
714
715 TEST_ASSERT( psa_import_key( key_slot, key_type,
716 key, sizeof( key ) ) == PSA_SUCCESS );
717
718 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
719
720 TEST_ASSERT( policy_get.usage == policy_set.usage );
721 TEST_ASSERT( policy_get.alg == policy_set.alg );
722
723exit:
724 psa_destroy_key( key_slot );
725 mbedtls_psa_crypto_free( );
726}
727/* END_CASE */
728
729/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200730void mac_key_policy( int policy_usage,
731 int policy_alg,
732 int key_type,
733 data_t *key_data,
734 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200735{
736 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +0200737 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200738 psa_mac_operation_t operation;
739 psa_status_t status;
740 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200741
742 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
743
744 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200745 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200746 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
747
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200748 TEST_ASSERT( psa_import_key( key_slot, key_type,
749 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +0200750
Gilles Peskine89167cb2018-07-08 20:12:23 +0200751 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200752 if( policy_alg == exercise_alg &&
753 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
754 TEST_ASSERT( status == PSA_SUCCESS );
755 else
756 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
757 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200758
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200759 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +0200760 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200761 if( policy_alg == exercise_alg &&
762 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +0200763 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200764 else
765 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
766
767exit:
768 psa_mac_abort( &operation );
769 psa_destroy_key( key_slot );
770 mbedtls_psa_crypto_free( );
771}
772/* END_CASE */
773
774/* BEGIN_CASE */
775void cipher_key_policy( int policy_usage,
776 int policy_alg,
777 int key_type,
778 data_t *key_data,
779 int exercise_alg )
780{
781 int key_slot = 1;
782 psa_key_policy_t policy;
783 psa_cipher_operation_t operation;
784 psa_status_t status;
785
786 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
787
788 psa_key_policy_init( &policy );
789 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
790 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
791
792 TEST_ASSERT( psa_import_key( key_slot, key_type,
793 key_data->x, key_data->len ) == PSA_SUCCESS );
794
795 status = psa_encrypt_setup( &operation, key_slot, exercise_alg );
796 if( policy_alg == exercise_alg &&
797 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
798 TEST_ASSERT( status == PSA_SUCCESS );
799 else
800 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
801 psa_cipher_abort( &operation );
802
803 status = psa_decrypt_setup( &operation, key_slot, exercise_alg );
804 if( policy_alg == exercise_alg &&
805 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
806 TEST_ASSERT( status == PSA_SUCCESS );
807 else
808 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
809
810exit:
811 psa_cipher_abort( &operation );
812 psa_destroy_key( key_slot );
813 mbedtls_psa_crypto_free( );
814}
815/* END_CASE */
816
817/* BEGIN_CASE */
818void aead_key_policy( int policy_usage,
819 int policy_alg,
820 int key_type,
821 data_t *key_data,
822 int nonce_length_arg,
823 int tag_length_arg,
824 int exercise_alg )
825{
826 int key_slot = 1;
827 psa_key_policy_t policy;
828 psa_status_t status;
829 unsigned char nonce[16] = {0};
830 size_t nonce_length = nonce_length_arg;
831 unsigned char tag[16];
832 size_t tag_length = tag_length_arg;
833 size_t output_length;
834
835 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
836 TEST_ASSERT( tag_length <= sizeof( tag ) );
837
838 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
839
840 psa_key_policy_init( &policy );
841 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
842 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
843
844 TEST_ASSERT( psa_import_key( key_slot, key_type,
845 key_data->x, key_data->len ) == PSA_SUCCESS );
846
847 status = psa_aead_encrypt( key_slot, exercise_alg,
848 nonce, nonce_length,
849 NULL, 0,
850 NULL, 0,
851 tag, tag_length,
852 &output_length );
853 if( policy_alg == exercise_alg &&
854 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
855 TEST_ASSERT( status == PSA_SUCCESS );
856 else
857 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
858
859 memset( tag, 0, sizeof( tag ) );
860 status = psa_aead_decrypt( key_slot, exercise_alg,
861 nonce, nonce_length,
862 NULL, 0,
863 tag, tag_length,
864 NULL, 0,
865 &output_length );
866 if( policy_alg == exercise_alg &&
867 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
868 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
869 else
870 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
871
872exit:
873 psa_destroy_key( key_slot );
874 mbedtls_psa_crypto_free( );
875}
876/* END_CASE */
877
878/* BEGIN_CASE */
879void asymmetric_encryption_key_policy( int policy_usage,
880 int policy_alg,
881 int key_type,
882 data_t *key_data,
883 int exercise_alg )
884{
885 int key_slot = 1;
886 psa_key_policy_t policy;
887 psa_status_t status;
888 size_t key_bits;
889 size_t buffer_length;
890 unsigned char *buffer = NULL;
891 size_t output_length;
892
893 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
894
895 psa_key_policy_init( &policy );
896 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
897 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
898
899 TEST_ASSERT( psa_import_key( key_slot, key_type,
900 key_data->x, key_data->len ) == PSA_SUCCESS );
901
902 TEST_ASSERT( psa_get_key_information( key_slot,
903 NULL,
904 &key_bits ) == PSA_SUCCESS );
905 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
906 exercise_alg );
907 buffer = mbedtls_calloc( 1, buffer_length );
908 TEST_ASSERT( buffer != NULL );
909
910 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
911 NULL, 0,
912 NULL, 0,
913 buffer, buffer_length,
914 &output_length );
915 if( policy_alg == exercise_alg &&
916 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
917 TEST_ASSERT( status == PSA_SUCCESS );
918 else
919 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
920
921 memset( buffer, 0, buffer_length );
922 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
923 buffer, buffer_length,
924 NULL, 0,
925 buffer, buffer_length,
926 &output_length );
927 if( policy_alg == exercise_alg &&
928 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
929 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
930 else
931 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
932
933exit:
934 psa_destroy_key( key_slot );
935 mbedtls_psa_crypto_free( );
936 mbedtls_free( buffer );
937}
938/* END_CASE */
939
940/* BEGIN_CASE */
941void asymmetric_signature_key_policy( int policy_usage,
942 int policy_alg,
943 int key_type,
944 data_t *key_data,
945 int exercise_alg )
946{
947 int key_slot = 1;
948 psa_key_policy_t policy;
949 psa_status_t status;
950 unsigned char payload[16] = {1};
951 size_t payload_length = sizeof( payload );
952 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
953 size_t signature_length;
954
955 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
956
957 psa_key_policy_init( &policy );
958 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
959 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
960
961 TEST_ASSERT( psa_import_key( key_slot, key_type,
962 key_data->x, key_data->len ) == PSA_SUCCESS );
963
964 status = psa_asymmetric_sign( key_slot, exercise_alg,
965 payload, payload_length,
966 NULL, 0,
967 signature, sizeof( signature ),
968 &signature_length );
969 if( policy_alg == exercise_alg &&
970 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
971 TEST_ASSERT( status == PSA_SUCCESS );
972 else
973 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
974
975 memset( signature, 0, sizeof( signature ) );
976 status = psa_asymmetric_verify( key_slot, exercise_alg,
977 payload, payload_length,
978 NULL, 0,
979 signature, sizeof( signature ) );
980 if( policy_alg == exercise_alg &&
981 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
982 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
983 else
984 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +0200985
986exit:
987 psa_destroy_key( key_slot );
988 mbedtls_psa_crypto_free( );
989}
990/* END_CASE */
991
992/* BEGIN_CASE */
993void key_lifetime( int lifetime_arg )
994{
995 int key_slot = 1;
996 psa_key_type_t key_type = PSA_ALG_CBC_BASE;
997 unsigned char key[32] = {0};
998 psa_key_lifetime_t lifetime_set = lifetime_arg;
999 psa_key_lifetime_t lifetime_get;
1000
1001 memset( key, 0x2a, sizeof( key ) );
1002
1003 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1004
1005 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1006 lifetime_set ) == PSA_SUCCESS );
1007
1008 TEST_ASSERT( psa_import_key( key_slot, key_type,
1009 key, sizeof( key ) ) == PSA_SUCCESS );
1010
1011 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1012 &lifetime_get ) == PSA_SUCCESS );
1013
1014 TEST_ASSERT( lifetime_get == lifetime_set );
1015
1016exit:
1017 psa_destroy_key( key_slot );
1018 mbedtls_psa_crypto_free( );
1019}
1020/* END_CASE */
1021
1022/* BEGIN_CASE */
1023void key_lifetime_set_fail( int key_slot_arg,
1024 int lifetime_arg,
1025 int expected_status_arg )
1026{
1027 psa_key_slot_t key_slot = key_slot_arg;
1028 psa_key_lifetime_t lifetime_set = lifetime_arg;
1029 psa_status_t actual_status;
1030 psa_status_t expected_status = expected_status_arg;
1031
1032 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1033
1034 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1035
1036 if( actual_status == PSA_SUCCESS )
1037 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1038
1039 TEST_ASSERT( expected_status == actual_status );
1040
1041exit:
1042 psa_destroy_key( key_slot );
1043 mbedtls_psa_crypto_free( );
1044}
1045/* END_CASE */
1046
1047/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001048void hash_setup( int alg_arg,
1049 int expected_status_arg )
1050{
1051 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001052 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001053 psa_hash_operation_t operation;
1054 psa_status_t status;
1055
1056 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1057
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001058 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001059 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001060 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001061
1062exit:
1063 mbedtls_psa_crypto_free( );
1064}
1065/* END_CASE */
1066
1067/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001068void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001069{
1070 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001071 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001072 size_t actual_hash_length;
1073 psa_hash_operation_t operation;
1074
Gilles Peskine69c12672018-06-28 00:07:19 +02001075 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1076 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1077
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001078 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001079 TEST_ASSERT( expected_hash != NULL );
1080 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1081 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001082
1083 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1084
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001085 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001086 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001087 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001088 TEST_ASSERT( psa_hash_finish( &operation,
1089 actual_hash, sizeof( actual_hash ),
1090 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001091 TEST_ASSERT( actual_hash_length == expected_hash->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001092 TEST_ASSERT( memcmp( expected_hash->x, actual_hash,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001093 expected_hash->len ) == 0 );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001094
1095exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001096 mbedtls_psa_crypto_free( );
1097}
1098/* END_CASE */
1099
1100/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001101void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001102{
1103 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001104 psa_hash_operation_t operation;
1105
Gilles Peskine69c12672018-06-28 00:07:19 +02001106 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1107 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1108
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001109 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001110 TEST_ASSERT( expected_hash != NULL );
1111 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1112 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001113
1114 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1115
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001116 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001117 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001118 input->x,
1119 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001120 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001121 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001122 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001123
1124exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001125 mbedtls_psa_crypto_free( );
1126}
1127/* END_CASE */
1128
1129/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001130void mac_setup( int key_type_arg,
1131 data_t *key,
1132 int alg_arg,
1133 int expected_status_arg )
1134{
1135 int key_slot = 1;
1136 psa_key_type_t key_type = key_type_arg;
1137 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001138 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001139 psa_mac_operation_t operation;
1140 psa_key_policy_t policy;
1141 psa_status_t status;
1142
1143 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1144
1145 psa_key_policy_init( &policy );
1146 psa_key_policy_set_usage( &policy,
1147 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1148 alg );
1149 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1150
1151 TEST_ASSERT( psa_import_key( key_slot, key_type,
1152 key->x, key->len ) == PSA_SUCCESS );
1153
Gilles Peskine89167cb2018-07-08 20:12:23 +02001154 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001155 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001156 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001157
1158exit:
1159 psa_destroy_key( key_slot );
1160 mbedtls_psa_crypto_free( );
1161}
1162/* END_CASE */
1163
1164/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001165void mac_verify( int key_type_arg,
1166 data_t *key,
1167 int alg_arg,
1168 data_t *input,
1169 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001170{
1171 int key_slot = 1;
1172 psa_key_type_t key_type = key_type_arg;
1173 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001174 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001175 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001176
Gilles Peskine69c12672018-06-28 00:07:19 +02001177 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1178
Gilles Peskine8c9def32018-02-08 10:02:12 +01001179 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001180 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001181 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001182 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001183 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1184 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001185
1186 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1187
mohammad16036df908f2018-04-02 08:34:15 -07001188 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001189 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07001190 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1191
Gilles Peskine8c9def32018-02-08 10:02:12 +01001192 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001193 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001194
Gilles Peskine89167cb2018-07-08 20:12:23 +02001195 TEST_ASSERT( psa_mac_verify_setup( &operation,
1196 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001197 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1198 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001199 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02001200 TEST_ASSERT( psa_mac_verify_finish( &operation,
1201 expected_mac->x,
1202 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001203
1204exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001205 psa_destroy_key( key_slot );
1206 mbedtls_psa_crypto_free( );
1207}
1208/* END_CASE */
1209
1210/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001211void cipher_setup( int key_type_arg,
1212 data_t *key,
1213 int alg_arg,
1214 int expected_status_arg )
1215{
1216 int key_slot = 1;
1217 psa_key_type_t key_type = key_type_arg;
1218 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001219 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001220 psa_cipher_operation_t operation;
1221 psa_key_policy_t policy;
1222 psa_status_t status;
1223
1224 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1225
1226 psa_key_policy_init( &policy );
1227 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1228 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1229
1230 TEST_ASSERT( psa_import_key( key_slot, key_type,
1231 key->x, key->len ) == PSA_SUCCESS );
1232
1233 status = psa_encrypt_setup( &operation, key_slot, alg );
1234 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001235 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001236
1237exit:
1238 psa_destroy_key( key_slot );
1239 mbedtls_psa_crypto_free( );
1240}
1241/* END_CASE */
1242
1243/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001244void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001245 data_t *key,
1246 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001247 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001248{
1249 int key_slot = 1;
1250 psa_status_t status;
1251 psa_key_type_t key_type = key_type_arg;
1252 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001253 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001254 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001255 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001256 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001257 size_t output_buffer_size = 0;
1258 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001259 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001260 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001261 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001262
Gilles Peskine50e586b2018-06-08 14:28:46 +02001263 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001264 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001265 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001266 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1267 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1268 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001269
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001270 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1271 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001272
1273 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1274
Moran Pekered346952018-07-05 15:22:45 +03001275 psa_key_policy_init( &policy );
1276 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1277 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1278
Gilles Peskine50e586b2018-06-08 14:28:46 +02001279 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001280 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001281
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001282 TEST_ASSERT( psa_encrypt_setup( &operation,
1283 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001284
1285 TEST_ASSERT( psa_encrypt_set_iv( &operation,
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001286 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001287 output_buffer_size = (size_t) input->len +
1288 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001289 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001290 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001291
Gilles Peskine4abf7412018-06-18 16:35:34 +02001292 TEST_ASSERT( psa_cipher_update( &operation,
1293 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001294 output, output_buffer_size,
1295 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001296 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001297 status = psa_cipher_finish( &operation,
1298 output + function_output_length,
1299 output_buffer_size,
1300 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001301 total_output_length += function_output_length;
1302
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001303 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001304 if( expected_status == PSA_SUCCESS )
1305 {
1306 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001307 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001308 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001309 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001310 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001311
Gilles Peskine50e586b2018-06-08 14:28:46 +02001312exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001313 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001314 psa_destroy_key( key_slot );
1315 mbedtls_psa_crypto_free( );
1316}
1317/* END_CASE */
1318
1319/* BEGIN_CASE */
1320void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001321 data_t *key,
1322 data_t *input,
1323 int first_part_size,
1324 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001325{
1326 int key_slot = 1;
1327 psa_key_type_t key_type = key_type_arg;
1328 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001329 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001330 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001331 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001332 size_t output_buffer_size = 0;
1333 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001334 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001335 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001336 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001337
Gilles Peskine50e586b2018-06-08 14:28:46 +02001338 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001339 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001340 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001341 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1342 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1343 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001344
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001345 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1346 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001347
1348 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1349
Moran Pekered346952018-07-05 15:22:45 +03001350 psa_key_policy_init( &policy );
1351 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1352 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1353
Gilles Peskine50e586b2018-06-08 14:28:46 +02001354 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001355 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001356
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001357 TEST_ASSERT( psa_encrypt_setup( &operation,
1358 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001359
1360 TEST_ASSERT( psa_encrypt_set_iv( &operation,
1361 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001362 output_buffer_size = (size_t) input->len +
1363 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001364 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001365 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001366
Gilles Peskine4abf7412018-06-18 16:35:34 +02001367 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001368 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001369 output, output_buffer_size,
1370 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001371 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001372 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001373 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001374 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001375 output, output_buffer_size,
1376 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001377 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001378 TEST_ASSERT( psa_cipher_finish( &operation,
1379 output + function_output_length,
1380 output_buffer_size,
1381 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001382 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001383 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1384
Gilles Peskine4abf7412018-06-18 16:35:34 +02001385 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001386 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001387 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001388
1389exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001390 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001391 psa_destroy_key( key_slot );
1392 mbedtls_psa_crypto_free( );
1393}
1394/* END_CASE */
1395
1396/* BEGIN_CASE */
1397void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001398 data_t *key,
1399 data_t *input,
1400 int first_part_size,
1401 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001402{
1403 int key_slot = 1;
1404
1405 psa_key_type_t key_type = key_type_arg;
1406 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001407 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001408 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001409 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001410 size_t output_buffer_size = 0;
1411 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001412 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001413 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001414 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001415
Gilles Peskine50e586b2018-06-08 14:28:46 +02001416 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001417 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001418 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001419 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1420 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1421 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001422
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001423 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1424 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001425
1426 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1427
Moran Pekered346952018-07-05 15:22:45 +03001428 psa_key_policy_init( &policy );
1429 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1430 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1431
Gilles Peskine50e586b2018-06-08 14:28:46 +02001432 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001433 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001434
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001435 TEST_ASSERT( psa_decrypt_setup( &operation,
1436 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001437
1438 TEST_ASSERT( psa_encrypt_set_iv( &operation,
1439 iv, sizeof( iv ) ) == PSA_SUCCESS );
1440
mohammad16033d91abe2018-07-03 13:15:54 +03001441 output_buffer_size = (size_t) input->len +
1442 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001443 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001444 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001445
Gilles Peskine4abf7412018-06-18 16:35:34 +02001446 TEST_ASSERT( (unsigned int) first_part_size < input->len );
1447 TEST_ASSERT( psa_cipher_update( &operation,
1448 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001449 output, output_buffer_size,
1450 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001451 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001452 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001453 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001454 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001455 output, output_buffer_size,
1456 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001457 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001458 TEST_ASSERT( psa_cipher_finish( &operation,
1459 output + function_output_length,
1460 output_buffer_size,
1461 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001462 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001463 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1464
Gilles Peskine4abf7412018-06-18 16:35:34 +02001465 TEST_ASSERT( total_output_length == expected_output->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001466 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001467 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001468
1469exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001470 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001471 psa_destroy_key( key_slot );
1472 mbedtls_psa_crypto_free( );
1473}
1474/* END_CASE */
1475
Gilles Peskine50e586b2018-06-08 14:28:46 +02001476/* BEGIN_CASE */
1477void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001478 data_t *key,
1479 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001480 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001481{
1482 int key_slot = 1;
1483 psa_status_t status;
1484 psa_key_type_t key_type = key_type_arg;
1485 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001486 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001487 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001488 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001489 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001490 size_t output_buffer_size = 0;
1491 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001492 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001493 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001494 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001495
Gilles Peskine50e586b2018-06-08 14:28:46 +02001496 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001497 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001498 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001499 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1500 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1501 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001502
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001503 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1504 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001505
1506 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1507
Moran Pekered346952018-07-05 15:22:45 +03001508 psa_key_policy_init( &policy );
1509 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1510 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1511
Gilles Peskine50e586b2018-06-08 14:28:46 +02001512 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001513 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001514
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001515 TEST_ASSERT( psa_decrypt_setup( &operation,
1516 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001517
1518 TEST_ASSERT( psa_encrypt_set_iv( &operation,
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001519 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001520
mohammad16033d91abe2018-07-03 13:15:54 +03001521 output_buffer_size = (size_t) input->len +
1522 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001523 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001524 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001525
Gilles Peskine4abf7412018-06-18 16:35:34 +02001526 TEST_ASSERT( psa_cipher_update( &operation,
1527 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001528 output, output_buffer_size,
1529 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001530 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001531 status = psa_cipher_finish( &operation,
1532 output + function_output_length,
1533 output_buffer_size,
1534 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001535 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001536 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001537
1538 if( expected_status == PSA_SUCCESS )
1539 {
1540 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001541 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001542 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001543 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001544 }
1545
Gilles Peskine50e586b2018-06-08 14:28:46 +02001546exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001547 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001548 psa_destroy_key( key_slot );
1549 mbedtls_psa_crypto_free( );
1550}
1551/* END_CASE */
1552
Gilles Peskine50e586b2018-06-08 14:28:46 +02001553/* BEGIN_CASE */
1554void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001555 data_t *key,
1556 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02001557{
1558 int key_slot = 1;
1559 psa_key_type_t key_type = key_type_arg;
1560 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07001561 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02001562 size_t iv_size = 16;
1563 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001564 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001565 size_t output1_size = 0;
1566 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001567 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001568 size_t output2_size = 0;
1569 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001570 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001571 psa_cipher_operation_t operation1;
1572 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03001573 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001574
mohammad1603d7d7ba52018-03-12 18:51:53 +02001575 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001576 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001577 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1578 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001579
mohammad1603d7d7ba52018-03-12 18:51:53 +02001580 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1581
Moran Pekered346952018-07-05 15:22:45 +03001582 psa_key_policy_init( &policy );
1583 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
1584 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1585
mohammad1603d7d7ba52018-03-12 18:51:53 +02001586 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001587 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001588
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001589 TEST_ASSERT( psa_encrypt_setup( &operation1,
1590 key_slot, alg ) == PSA_SUCCESS );
1591 TEST_ASSERT( psa_decrypt_setup( &operation2,
1592 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001593
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001594 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
1595 iv, iv_size,
1596 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001597 output1_size = (size_t) input->len +
1598 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001599 output1 = mbedtls_calloc( 1, output1_size );
1600 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001601
Gilles Peskine4abf7412018-06-18 16:35:34 +02001602 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001603 output1, output1_size,
1604 &output1_length ) == PSA_SUCCESS );
1605 TEST_ASSERT( psa_cipher_finish( &operation1,
1606 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001607 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001608
Gilles Peskine048b7f02018-06-08 14:20:49 +02001609 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001610
1611 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1612
1613 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001614 output2 = mbedtls_calloc( 1, output2_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001615 TEST_ASSERT( output2 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001616
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001617 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
1618 iv, iv_length ) == PSA_SUCCESS );
1619 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
1620 output2, output2_size,
1621 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001622 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001623 TEST_ASSERT( psa_cipher_finish( &operation2,
1624 output2 + output2_length,
1625 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001626 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001627
Gilles Peskine048b7f02018-06-08 14:20:49 +02001628 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001629
Janos Follath25c4fa82018-07-06 16:23:25 +01001630 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001631
Gilles Peskine4abf7412018-06-18 16:35:34 +02001632 TEST_ASSERT( input->len == output2_length );
1633 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +03001634
1635exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001636 mbedtls_free( output1 );
1637 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03001638 psa_destroy_key( key_slot );
1639 mbedtls_psa_crypto_free( );
1640}
1641/* END_CASE */
1642
1643/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001644void cipher_verify_output_multipart( int alg_arg,
1645 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001646 data_t *key,
1647 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001648 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03001649{
1650 int key_slot = 1;
1651 psa_key_type_t key_type = key_type_arg;
1652 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03001653 unsigned char iv[16] = {0};
1654 size_t iv_size = 16;
1655 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001656 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001657 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001658 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001659 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001660 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001661 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001662 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001663 psa_cipher_operation_t operation1;
1664 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03001665 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03001666
Moran Pekerded84402018-06-06 16:36:50 +03001667 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001668 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001669 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1670 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001671
Moran Pekerded84402018-06-06 16:36:50 +03001672 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1673
Moran Pekered346952018-07-05 15:22:45 +03001674 psa_key_policy_init( &policy );
1675 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
1676 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1677
Moran Pekerded84402018-06-06 16:36:50 +03001678 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001679 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001680
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001681 TEST_ASSERT( psa_encrypt_setup( &operation1,
1682 key_slot, alg ) == PSA_SUCCESS );
1683 TEST_ASSERT( psa_decrypt_setup( &operation2,
1684 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001685
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001686 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
1687 iv, iv_size,
1688 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001689 output1_buffer_size = (size_t) input->len +
1690 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001691 output1 = mbedtls_calloc( 1, output1_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001692 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001693
Gilles Peskine4abf7412018-06-18 16:35:34 +02001694 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001695
itayzafrir3e02b3b2018-06-12 17:06:52 +03001696 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001697 output1, output1_buffer_size,
1698 &function_output_length ) == PSA_SUCCESS );
1699 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001700
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001701 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001702 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001703 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001704 output1, output1_buffer_size,
1705 &function_output_length ) == PSA_SUCCESS );
1706 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001707
Gilles Peskine048b7f02018-06-08 14:20:49 +02001708 TEST_ASSERT( psa_cipher_finish( &operation1,
1709 output1 + output1_length,
1710 output1_buffer_size - output1_length,
1711 &function_output_length ) == PSA_SUCCESS );
1712 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001713
1714 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1715
Gilles Peskine048b7f02018-06-08 14:20:49 +02001716 output2_buffer_size = output1_length;
1717 output2 = mbedtls_calloc( 1, output2_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001718 TEST_ASSERT( output2 != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001719
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001720 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
1721 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001722
1723 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001724 output2, output2_buffer_size,
1725 &function_output_length ) == PSA_SUCCESS );
1726 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001727
Gilles Peskine048b7f02018-06-08 14:20:49 +02001728 TEST_ASSERT( psa_cipher_update( &operation2,
1729 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001730 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001731 output2, output2_buffer_size,
1732 &function_output_length ) == PSA_SUCCESS );
1733 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001734
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001735 TEST_ASSERT( psa_cipher_finish( &operation2,
1736 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001737 output2_buffer_size - output2_length,
1738 &function_output_length ) == PSA_SUCCESS );
1739 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001740
Janos Follath25c4fa82018-07-06 16:23:25 +01001741 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001742
Gilles Peskine4abf7412018-06-18 16:35:34 +02001743 TEST_ASSERT( input->len == output2_length );
1744 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001745
1746exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001747 mbedtls_free( output1 );
1748 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001749 psa_destroy_key( key_slot );
1750 mbedtls_psa_crypto_free( );
1751}
1752/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02001753
Gilles Peskine20035e32018-02-03 22:44:14 +01001754/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001755void aead_encrypt_decrypt( int key_type_arg,
1756 data_t * key_data,
1757 int alg_arg,
1758 data_t * input_data,
1759 data_t * nonce,
1760 data_t * additional_data,
1761 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001762{
1763 int slot = 1;
1764 psa_key_type_t key_type = key_type_arg;
1765 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001766 unsigned char *output_data = NULL;
1767 size_t output_size = 0;
1768 size_t output_length = 0;
1769 unsigned char *output_data2 = NULL;
1770 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001771 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001772 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001773 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001774
Gilles Peskinea1cac842018-06-11 19:33:02 +02001775 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001776 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001777 TEST_ASSERT( nonce != NULL );
1778 TEST_ASSERT( additional_data != NULL );
1779 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1780 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1781 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1782 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1783
Gilles Peskine4abf7412018-06-18 16:35:34 +02001784 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001785 output_data = mbedtls_calloc( 1, output_size );
1786 TEST_ASSERT( output_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001787
1788 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1789
1790 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001791 psa_key_policy_set_usage( &policy,
1792 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
1793 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001794 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1795
1796 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001797 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001798
1799 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001800 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001801 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001802 additional_data->len,
1803 input_data->x, input_data->len,
1804 output_data, output_size,
1805 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001806
1807 if( PSA_SUCCESS == expected_result )
1808 {
1809 output_data2 = mbedtls_calloc( 1, output_length );
1810 TEST_ASSERT( output_data2 != NULL );
1811
1812 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001813 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001814 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001815 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001816 output_data, output_length,
1817 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001818 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02001819
itayzafrir3e02b3b2018-06-12 17:06:52 +03001820 TEST_ASSERT( memcmp( input_data->x, output_data2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001821 input_data->len ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001822 }
Gilles Peskine2d277862018-06-18 15:41:12 +02001823
Gilles Peskinea1cac842018-06-11 19:33:02 +02001824exit:
1825 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001826 mbedtls_free( output_data );
1827 mbedtls_free( output_data2 );
1828 mbedtls_psa_crypto_free( );
1829}
1830/* END_CASE */
1831
1832/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001833void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001834 int alg_arg, data_t * input_data,
1835 data_t * additional_data, data_t * nonce,
1836 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001837{
1838 int slot = 1;
1839 psa_key_type_t key_type = key_type_arg;
1840 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001841 unsigned char *output_data = NULL;
1842 size_t output_size = 0;
1843 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001844 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001845 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001846
Gilles Peskinea1cac842018-06-11 19:33:02 +02001847 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001848 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001849 TEST_ASSERT( additional_data != NULL );
1850 TEST_ASSERT( nonce != NULL );
1851 TEST_ASSERT( expected_result != NULL );
1852 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1853 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1854 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1855 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1856 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
1857
Gilles Peskine4abf7412018-06-18 16:35:34 +02001858 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001859 output_data = mbedtls_calloc( 1, output_size );
1860 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001861
Gilles Peskinea1cac842018-06-11 19:33:02 +02001862 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1863
1864 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001865 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001866 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1867
1868 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001869 key_data->x,
1870 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001871
1872 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001873 nonce->x, nonce->len,
1874 additional_data->x, additional_data->len,
1875 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001876 output_data, output_size,
1877 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001878
itayzafrir3e02b3b2018-06-12 17:06:52 +03001879 TEST_ASSERT( memcmp( output_data, expected_result->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001880 output_length ) == 0 );
1881
Gilles Peskinea1cac842018-06-11 19:33:02 +02001882exit:
1883 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001884 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001885 mbedtls_psa_crypto_free( );
1886}
1887/* END_CASE */
1888
1889/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001890void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001891 int alg_arg, data_t * input_data,
1892 data_t * additional_data, data_t * nonce,
1893 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001894{
1895 int slot = 1;
1896 psa_key_type_t key_type = key_type_arg;
1897 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001898 unsigned char *output_data = NULL;
1899 size_t output_size = 0;
1900 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001901 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001902 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001903 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001904
Gilles Peskinea1cac842018-06-11 19:33:02 +02001905 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001906 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001907 TEST_ASSERT( additional_data != NULL );
1908 TEST_ASSERT( nonce != NULL );
1909 TEST_ASSERT( expected_data != NULL );
1910 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1911 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1912 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1913 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1914 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
1915
Gilles Peskine4abf7412018-06-18 16:35:34 +02001916 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001917 output_data = mbedtls_calloc( 1, output_size );
1918 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001919
Gilles Peskinea1cac842018-06-11 19:33:02 +02001920 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1921
1922 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001923 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001924 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1925
1926 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001927 key_data->x,
1928 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001929
1930 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001931 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001932 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001933 additional_data->len,
1934 input_data->x, input_data->len,
1935 output_data, output_size,
1936 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001937
Gilles Peskine2d277862018-06-18 15:41:12 +02001938 if( expected_result == PSA_SUCCESS )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001939 {
itayzafrir3e02b3b2018-06-12 17:06:52 +03001940 TEST_ASSERT( memcmp( output_data, expected_data->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001941 output_length ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001942 }
1943
Gilles Peskinea1cac842018-06-11 19:33:02 +02001944exit:
1945 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001946 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001947 mbedtls_psa_crypto_free( );
1948}
1949/* END_CASE */
1950
1951/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001952void signature_size( int type_arg,
1953 int bits,
1954 int alg_arg,
1955 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001956{
1957 psa_key_type_t type = type_arg;
1958 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02001959 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01001960 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
1961exit:
1962 ;
1963}
1964/* END_CASE */
1965
1966/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001967void sign_deterministic( int key_type_arg, data_t *key_data,
1968 int alg_arg, data_t *input_data,
1969 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001970{
1971 int slot = 1;
1972 psa_key_type_t key_type = key_type_arg;
1973 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01001974 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01001975 unsigned char *signature = NULL;
1976 size_t signature_size;
1977 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02001978 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01001979
Gilles Peskine20035e32018-02-03 22:44:14 +01001980 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001981 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001982 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001983 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1984 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1985 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01001986
1987 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1988
mohammad1603a97cb8c2018-03-28 03:46:26 -07001989 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001990 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001991 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1992
Gilles Peskine20035e32018-02-03 22:44:14 +01001993 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001994 key_data->x,
1995 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01001996 TEST_ASSERT( psa_get_key_information( slot,
1997 NULL,
1998 &key_bits ) == PSA_SUCCESS );
1999
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002000 /* Allocate a buffer which has the size advertized by the
2001 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002002 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2003 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002004 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002005 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine20035e32018-02-03 22:44:14 +01002006 signature = mbedtls_calloc( 1, signature_size );
2007 TEST_ASSERT( signature != NULL );
2008
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002009 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002010 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002011 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002012 NULL, 0,
2013 signature, signature_size,
2014 &signature_length ) == PSA_SUCCESS );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002015 /* Verify that the signature is correct. */
Gilles Peskine4abf7412018-06-18 16:35:34 +02002016 TEST_ASSERT( signature_length == output_data->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02002017 TEST_ASSERT( memcmp( signature, output_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002018 output_data->len ) == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01002019
2020exit:
2021 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002022 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002023 mbedtls_psa_crypto_free( );
2024}
2025/* END_CASE */
2026
2027/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002028void sign_fail( int key_type_arg, data_t *key_data,
2029 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002030 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002031{
2032 int slot = 1;
2033 psa_key_type_t key_type = key_type_arg;
2034 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002035 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002036 psa_status_t actual_status;
2037 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002038 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002039 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002040 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002041
Gilles Peskine20035e32018-02-03 22:44:14 +01002042 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002043 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002044 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2045 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2046
Gilles Peskine20035e32018-02-03 22:44:14 +01002047 signature = mbedtls_calloc( 1, signature_size );
2048 TEST_ASSERT( signature != NULL );
2049
2050 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2051
mohammad1603a97cb8c2018-03-28 03:46:26 -07002052 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002053 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002054 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2055
Gilles Peskine20035e32018-02-03 22:44:14 +01002056 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002057 key_data->x,
2058 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002059
2060 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002061 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002062 NULL, 0,
2063 signature, signature_size,
2064 &signature_length );
2065 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002066 /* The value of *signature_length is unspecified on error, but
2067 * whatever it is, it should be less than signature_size, so that
2068 * if the caller tries to read *signature_length bytes without
2069 * checking the error code then they don't overflow a buffer. */
2070 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002071
2072exit:
2073 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002074 mbedtls_free( signature );
2075 mbedtls_psa_crypto_free( );
2076}
2077/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002078
2079/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002080void asymmetric_verify( int key_type_arg, data_t *key_data,
2081 int alg_arg, data_t *hash_data,
2082 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002083{
2084 int slot = 1;
2085 psa_key_type_t key_type = key_type_arg;
2086 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002087 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002088
Gilles Peskine69c12672018-06-28 00:07:19 +02002089 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2090
itayzafrir5c753392018-05-08 11:18:38 +03002091 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002092 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002093 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002094 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2095 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2096 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002097
2098 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2099
2100 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002101 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002102 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2103
2104 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002105 key_data->x,
2106 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002107
2108 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002109 hash_data->x, hash_data->len,
itayzafrir5c753392018-05-08 11:18:38 +03002110 NULL, 0,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002111 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002112 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002113exit:
2114 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002115 mbedtls_psa_crypto_free( );
2116}
2117/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002118
2119/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002120void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2121 int alg_arg, data_t *hash_data,
2122 data_t *signature_data,
2123 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002124{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002125 int slot = 1;
2126 psa_key_type_t key_type = key_type_arg;
2127 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002128 psa_status_t actual_status;
2129 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002130 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002131
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002132 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002133 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002134 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002135 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2136 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2137 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002138
2139 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2140
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002141 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002142 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002143 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2144
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002145 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002146 key_data->x,
2147 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002148
2149 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002150 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002151 NULL, 0,
2152 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002153 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002154
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002155 TEST_ASSERT( actual_status == expected_status );
2156
2157exit:
2158 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002159 mbedtls_psa_crypto_free( );
2160}
2161/* END_CASE */
2162
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002163/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002164void asymmetric_encrypt_decrypt( int key_type_arg, data_t *key_data,
2165 int alg_arg, data_t *input_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002166{
2167 int slot = 1;
2168 psa_key_type_t key_type = key_type_arg;
2169 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002170 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002171 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002172 size_t output_length = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002173 unsigned char *output2 = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002174 size_t output2_size = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002175 size_t output2_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002176 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002177
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002178 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002179 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002180 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2181 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2182
Gilles Peskine4abf7412018-06-18 16:35:34 +02002183 output_size = key_data->len;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002184 output2_size = output_size;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002185 output = mbedtls_calloc( 1, output_size );
2186 TEST_ASSERT( output != NULL );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002187 output2 = mbedtls_calloc( 1, output2_size );
2188 TEST_ASSERT( output2 != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002189
2190 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2191
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002192 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002193 psa_key_policy_set_usage( &policy,
2194 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002195 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002196 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2197
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002198 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002199 key_data->x,
2200 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002201
Gilles Peskineeebd7382018-06-08 18:11:54 +02002202 /* We test encryption by checking that encrypt-then-decrypt gives back
2203 * the original plaintext because of the non-optional random
2204 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02002205 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002206 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002207 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002208 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002209 &output_length ) == PSA_SUCCESS );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002210
Gilles Peskine2d277862018-06-18 15:41:12 +02002211 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002212 output, output_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02002213 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002214 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002215 &output2_length ) == PSA_SUCCESS );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002216 TEST_ASSERT( memcmp( input_data->x, output2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002217 input_data->len ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002218
2219exit:
2220 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002221 mbedtls_free( output );
2222 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002223 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002224}
2225/* END_CASE */
2226
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002227/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002228void asymmetric_encrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002229 int alg_arg, data_t *input_data,
2230 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002231{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002232 int slot = 1;
2233 psa_key_type_t key_type = key_type_arg;
2234 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002235 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002236 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002237 size_t output_length = 0;
2238 psa_status_t actual_status;
2239 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002240 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002241
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002242 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002243 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002244 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2245 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2246
Gilles Peskine4abf7412018-06-18 16:35:34 +02002247 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002248 output = mbedtls_calloc( 1, output_size );
2249 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002250
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002251 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2252
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002253 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002254 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002255 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2256
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002257 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002258 key_data->x,
2259 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002260
Gilles Peskine2d277862018-06-18 15:41:12 +02002261 actual_status = psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002262 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002263 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002264 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002265 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002266 TEST_ASSERT( actual_status == expected_status );
2267
2268exit:
2269 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002270 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002271 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002272}
2273/* END_CASE */
2274
2275/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002276void asymmetric_decrypt( int key_type_arg, data_t *key_data,
2277 int alg_arg, data_t *input_data,
2278 data_t *expected_data, int expected_size )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002279{
2280 int slot = 1;
2281 psa_key_type_t key_type = key_type_arg;
2282 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002283 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002284 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002285 size_t output_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002286 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002287
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002288 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002289 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002290 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002291 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2292 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2293 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2294
Gilles Peskine4abf7412018-06-18 16:35:34 +02002295 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002296 output = mbedtls_calloc( 1, output_size );
2297 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002298
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002299 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2300
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002301 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002302 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002303 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2304
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002305 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002306 key_data->x,
2307 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002308
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002309 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002310 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002311 NULL, 0,
2312 output,
2313 output_size,
2314 &output_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002315 TEST_ASSERT( (size_t) expected_size == output_length );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002316 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002317
2318exit:
2319 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002320 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002321 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002322}
2323/* END_CASE */
2324
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002325/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002326void asymmetric_decrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002327 int alg_arg, data_t *input_data,
2328 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002329{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002330 int slot = 1;
2331 psa_key_type_t key_type = key_type_arg;
2332 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002333 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002334 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002335 size_t output_length = 0;
2336 psa_status_t actual_status;
2337 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002338 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002339
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002340 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002341 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002342 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2343 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2344
Gilles Peskine4abf7412018-06-18 16:35:34 +02002345 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002346 output = mbedtls_calloc( 1, output_size );
2347 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002348
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002349 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2350
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002351 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002352 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002353 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2354
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002355 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002356 key_data->x,
2357 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002358
Gilles Peskine2d277862018-06-18 15:41:12 +02002359 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002360 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002361 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002362 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002363 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002364 TEST_ASSERT( actual_status == expected_status );
2365
2366exit:
2367 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02002368 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002369 mbedtls_psa_crypto_free( );
2370}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002371/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02002372
2373/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02002374void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02002375{
Gilles Peskinea50d7392018-06-21 10:22:13 +02002376 size_t bytes = bytes_arg;
2377 const unsigned char trail[] = "don't overwrite me";
2378 unsigned char *output = mbedtls_calloc( 1, bytes + sizeof( trail ) );
2379 unsigned char *changed = mbedtls_calloc( 1, bytes );
2380 size_t i;
2381 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02002382
Gilles Peskinea50d7392018-06-21 10:22:13 +02002383 TEST_ASSERT( output != NULL );
2384 TEST_ASSERT( changed != NULL );
2385 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02002386
2387 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2388
Gilles Peskinea50d7392018-06-21 10:22:13 +02002389 /* Run several times, to ensure that every output byte will be
2390 * nonzero at least once with overwhelming probability
2391 * (2^(-8*number_of_runs)). */
2392 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02002393 {
Gilles Peskinea50d7392018-06-21 10:22:13 +02002394 memset( output, 0, bytes );
2395 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
2396
2397 /* Check that no more than bytes have been overwritten */
2398 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
2399
2400 for( i = 0; i < bytes; i++ )
2401 {
2402 if( output[i] != 0 )
2403 ++changed[i];
2404 }
Gilles Peskine05d69892018-06-19 22:00:52 +02002405 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02002406
2407 /* Check that every byte was changed to nonzero at least once. This
2408 * validates that psa_generate_random is overwriting every byte of
2409 * the output buffer. */
2410 for( i = 0; i < bytes; i++ )
2411 {
2412 TEST_ASSERT( changed[i] != 0 );
2413 }
Gilles Peskine05d69892018-06-19 22:00:52 +02002414
2415exit:
2416 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02002417 mbedtls_free( output );
2418 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02002419}
2420/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02002421
2422/* BEGIN_CASE */
2423void generate_key( int type_arg,
2424 int bits_arg,
2425 int usage_arg,
2426 int alg_arg,
2427 int expected_status_arg )
2428{
2429 int slot = 1;
2430 psa_key_type_t type = type_arg;
2431 psa_key_usage_t usage = usage_arg;
2432 size_t bits = bits_arg;
2433 psa_algorithm_t alg = alg_arg;
2434 psa_status_t expected_status = expected_status_arg;
2435 psa_key_type_t got_type;
2436 size_t got_bits;
2437 unsigned char exported[616] = {0}; /* enough for a 1024-bit RSA key */
2438 size_t exported_length;
2439 psa_status_t expected_export_status =
2440 usage & PSA_KEY_USAGE_EXPORT ? PSA_SUCCESS : PSA_ERROR_NOT_PERMITTED;
2441 psa_status_t expected_info_status =
2442 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
2443 psa_key_policy_t policy;
2444
2445 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2446
2447 psa_key_policy_init( &policy );
2448 psa_key_policy_set_usage( &policy, usage, alg );
2449 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2450
2451 /* Generate a key */
2452 TEST_ASSERT( psa_generate_key( slot, type, bits,
2453 NULL, 0 ) == expected_status );
2454
2455 /* Test the key information */
2456 TEST_ASSERT( psa_get_key_information( slot,
2457 &got_type,
2458 &got_bits ) == expected_info_status );
2459 if( expected_info_status != PSA_SUCCESS )
2460 goto exit;
2461 TEST_ASSERT( got_type == type );
2462 TEST_ASSERT( got_bits == bits );
2463
2464 /* Export the key */
2465 TEST_ASSERT( psa_export_key( slot,
2466 exported, sizeof( exported ),
2467 &exported_length ) == expected_export_status );
2468 if( expected_export_status == PSA_SUCCESS )
2469 {
Gilles Peskine48c0ea12018-06-21 14:15:31 +02002470 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02002471 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
2472#if defined(MBEDTLS_DES_C)
2473 if( type == PSA_KEY_TYPE_DES )
2474 {
2475 /* Check the parity bits. */
2476 unsigned i;
2477 for( i = 0; i < bits / 8; i++ )
2478 {
2479 unsigned bit_count = 0;
2480 unsigned m;
2481 for( m = 1; m <= 0x100; m <<= 1 )
2482 {
2483 if( exported[i] & m )
2484 ++bit_count;
2485 }
2486 TEST_ASSERT( bit_count % 2 != 0 );
2487 }
2488 }
2489#endif
2490#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
2491 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
2492 {
2493 /* Sanity check: does this look like the beginning of a PKCS#8
2494 * RSA key pair? Assumes bits is a multiple of 8. */
2495 size_t n_bytes = bits / 8 + 1;
2496 size_t n_encoded_bytes;
2497 unsigned char *n_end;
2498 TEST_ASSERT( exported_length >= 7 + ( n_bytes + 3 ) * 9 / 2 );
2499 TEST_ASSERT( exported[0] == 0x30 );
2500 TEST_ASSERT( exported[1] == 0x82 ); // assumes >=416-bit key
2501 TEST_ASSERT( exported[4] == 0x02 );
2502 TEST_ASSERT( exported[5] == 0x01 );
2503 TEST_ASSERT( exported[6] == 0x00 );
2504 TEST_ASSERT( exported[7] == 0x02 );
2505 n_encoded_bytes = exported[8];
2506 n_end = exported + 9 + n_encoded_bytes;
2507 if( n_encoded_bytes & 0x80 )
2508 {
2509 n_encoded_bytes = ( n_encoded_bytes & 0x7f ) << 7;
2510 n_encoded_bytes |= exported[9] & 0x7f;
2511 n_end += 1;
2512 }
2513 /* The encoding of n should start with a 0 byte since it should
2514 * have its high bit set. However Mbed TLS is not compliant and
2515 * generates an invalid, but widely tolerated, encoding of
2516 * positive INTEGERs with a bit size that is a multiple of 8
2517 * with no leading 0 byte. Accept this here. */
2518 TEST_ASSERT( n_bytes == n_encoded_bytes ||
2519 n_bytes == n_encoded_bytes + 1 );
2520 if( n_bytes == n_encoded_bytes )
2521 TEST_ASSERT( exported[n_encoded_bytes <= 127 ? 9 : 10] == 0x00 );
2522 /* Sanity check: e must be 3 */
2523 TEST_ASSERT( n_end[0] == 0x02 );
2524 TEST_ASSERT( n_end[1] == 0x03 );
2525 TEST_ASSERT( n_end[2] == 0x01 );
2526 TEST_ASSERT( n_end[3] == 0x00 );
2527 TEST_ASSERT( n_end[4] == 0x01 );
2528 TEST_ASSERT( n_end[5] == 0x02 );
2529 }
2530#endif /* MBEDTLS_RSA_C */
2531#if defined(MBEDTLS_ECP_C)
2532 if( PSA_KEY_TYPE_IS_ECC( type ) )
2533 {
2534 /* Sanity check: does this look like the beginning of a PKCS#8
2535 * elliptic curve key pair? */
2536 TEST_ASSERT( exported_length >= bits * 3 / 8 + 10 );
2537 TEST_ASSERT( exported[0] == 0x30 );
2538 }
2539#endif /* MBEDTLS_ECP_C */
2540 }
2541
Gilles Peskine818ca122018-06-20 18:16:48 +02002542 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02002543 if( ! exercise_key( slot, usage, alg ) )
2544 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02002545
2546exit:
2547 psa_destroy_key( slot );
2548 mbedtls_psa_crypto_free( );
2549}
2550/* END_CASE */