blob: 3681a2ee1096f6d08048d4d0b73dbe71306f0c05 [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 {
141 TEST_ASSERT( psa_mac_start( &operation, key, alg ) == PSA_SUCCESS );
142 TEST_ASSERT( psa_mac_update( &operation,
143 input, sizeof( input ) ) == PSA_SUCCESS );
144 TEST_ASSERT( psa_mac_finish( &operation,
145 mac, sizeof( input ),
146 &mac_length ) == PSA_SUCCESS );
147 }
148
149 if( usage & PSA_KEY_USAGE_VERIFY )
150 {
151 psa_status_t verify_status =
152 ( usage & PSA_KEY_USAGE_SIGN ?
153 PSA_SUCCESS :
154 PSA_ERROR_INVALID_SIGNATURE );
155 TEST_ASSERT( psa_mac_start( &operation, key, alg ) == PSA_SUCCESS );
156 TEST_ASSERT( psa_mac_update( &operation,
157 input, sizeof( input ) ) == PSA_SUCCESS );
158 TEST_ASSERT( psa_mac_verify( &operation, mac, mac_length ) == verify_status );
159 }
160
161 return( 1 );
162
163exit:
164 psa_mac_abort( &operation );
165 return( 0 );
166}
167
168static int exercise_cipher_key( psa_key_slot_t key,
169 psa_key_usage_t usage,
170 psa_algorithm_t alg )
171{
172 psa_cipher_operation_t operation;
173 unsigned char iv[16] = {0};
174 size_t iv_length = sizeof( iv );
175 const unsigned char plaintext[16] = "Hello, world...";
176 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
177 size_t ciphertext_length = sizeof( ciphertext );
178 unsigned char decrypted[sizeof( ciphertext )];
179 size_t part_length;
180
181 if( usage & PSA_KEY_USAGE_ENCRYPT )
182 {
183 TEST_ASSERT( psa_encrypt_setup( &operation, key, alg ) == PSA_SUCCESS );
184 TEST_ASSERT( psa_encrypt_generate_iv( &operation,
185 iv, sizeof( iv ),
186 &iv_length ) == PSA_SUCCESS );
187 TEST_ASSERT( psa_cipher_update( &operation,
188 plaintext, sizeof( plaintext ),
189 ciphertext, sizeof( ciphertext ),
190 &ciphertext_length ) == PSA_SUCCESS );
191 TEST_ASSERT( psa_cipher_finish( &operation,
192 ciphertext + ciphertext_length,
193 sizeof( ciphertext ) - ciphertext_length,
194 &part_length ) == PSA_SUCCESS );
195 ciphertext_length += part_length;
196 }
197
198 if( usage & PSA_KEY_USAGE_DECRYPT )
199 {
200 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700201 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200202 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
203 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200204 size_t bits;
205 TEST_ASSERT( psa_get_key_information( key, &type, &bits ) );
206 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
207 }
208 TEST_ASSERT( psa_decrypt_setup( &operation, key, alg ) == PSA_SUCCESS );
209 TEST_ASSERT( psa_encrypt_set_iv( &operation,
210 iv, iv_length ) == PSA_SUCCESS );
211 TEST_ASSERT( psa_cipher_update( &operation,
212 ciphertext, ciphertext_length,
213 decrypted, sizeof( decrypted ),
214 &part_length ) == PSA_SUCCESS );
215 status = psa_cipher_finish( &operation,
216 decrypted + part_length,
217 sizeof( decrypted ) - part_length,
218 &part_length );
219 /* For a stream cipher, all inputs are valid. For a block cipher,
220 * if the input is some aribtrary data rather than an actual
221 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700222 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700223 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine818ca122018-06-20 18:16:48 +0200224 TEST_ASSERT( status == PSA_SUCCESS );
225 else
226 TEST_ASSERT( status == PSA_SUCCESS ||
227 status == PSA_ERROR_INVALID_PADDING );
228 }
229
230 return( 1 );
231
232exit:
233 psa_cipher_abort( &operation );
234 return( 0 );
235}
236
237static int exercise_aead_key( psa_key_slot_t key,
238 psa_key_usage_t usage,
239 psa_algorithm_t alg )
240{
241 unsigned char nonce[16] = {0};
242 size_t nonce_length = sizeof( nonce );
243 unsigned char plaintext[16] = "Hello, world...";
244 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
245 size_t ciphertext_length = sizeof( ciphertext );
246 size_t plaintext_length = sizeof( ciphertext );
247
248 if( usage & PSA_KEY_USAGE_ENCRYPT )
249 {
250 TEST_ASSERT( psa_aead_encrypt( key, alg,
251 nonce, nonce_length,
252 NULL, 0,
253 plaintext, sizeof( plaintext ),
254 ciphertext, sizeof( ciphertext ),
255 &ciphertext_length ) == PSA_SUCCESS );
256 }
257
258 if( usage & PSA_KEY_USAGE_DECRYPT )
259 {
260 psa_status_t verify_status =
261 ( usage & PSA_KEY_USAGE_ENCRYPT ?
262 PSA_SUCCESS :
263 PSA_ERROR_INVALID_SIGNATURE );
264 TEST_ASSERT( psa_aead_decrypt( key, alg,
265 nonce, nonce_length,
266 NULL, 0,
267 ciphertext, ciphertext_length,
268 plaintext, sizeof( plaintext ),
269 &plaintext_length ) == verify_status );
270 }
271
272 return( 1 );
273
274exit:
275 return( 0 );
276}
277
278static int exercise_signature_key( psa_key_slot_t key,
279 psa_key_usage_t usage,
280 psa_algorithm_t alg )
281{
Gilles Peskineca45c352018-06-26 16:13:09 +0200282 unsigned char payload[16] = {1};
Gilles Peskine818ca122018-06-20 18:16:48 +0200283 size_t payload_length = sizeof( payload );
Gilles Peskine69c12672018-06-28 00:07:19 +0200284 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200285 size_t signature_length = sizeof( signature );
286
287 if( usage & PSA_KEY_USAGE_SIGN )
288 {
289 TEST_ASSERT( psa_asymmetric_sign( key, alg,
290 payload, payload_length,
291 NULL, 0,
292 signature, sizeof( signature ),
293 &signature_length ) == PSA_SUCCESS );
294 }
295
296 if( usage & PSA_KEY_USAGE_VERIFY )
297 {
298 psa_status_t verify_status =
299 ( usage & PSA_KEY_USAGE_SIGN ?
300 PSA_SUCCESS :
301 PSA_ERROR_INVALID_SIGNATURE );
302 TEST_ASSERT( psa_asymmetric_verify( key, alg,
303 payload, payload_length,
304 NULL, 0,
305 signature, signature_length ) ==
306 verify_status );
307 }
308
309 return( 1 );
310
311exit:
312 return( 0 );
313}
314
315static int exercise_asymmetric_encryption_key( psa_key_slot_t key,
316 psa_key_usage_t usage,
317 psa_algorithm_t alg )
318{
319 unsigned char plaintext[256] = "Hello, world...";
320 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
321 size_t ciphertext_length = sizeof( ciphertext );
322 size_t plaintext_length = 16;
323
324 if( usage & PSA_KEY_USAGE_ENCRYPT )
325 {
326 TEST_ASSERT(
327 psa_asymmetric_encrypt( key, alg,
328 plaintext, plaintext_length,
329 NULL, 0,
330 ciphertext, sizeof( ciphertext ),
331 &ciphertext_length ) == PSA_SUCCESS );
332 }
333
334 if( usage & PSA_KEY_USAGE_DECRYPT )
335 {
336 psa_status_t status =
337 psa_asymmetric_decrypt( key, alg,
338 ciphertext, ciphertext_length,
339 NULL, 0,
340 plaintext, sizeof( plaintext ),
341 &plaintext_length );
342 TEST_ASSERT( status == PSA_SUCCESS ||
343 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
344 ( status == PSA_ERROR_INVALID_ARGUMENT ||
345 status == PSA_ERROR_INVALID_PADDING ) ) );
346 }
347
348 return( 1 );
349
350exit:
351 return( 0 );
352}
Gilles Peskine02b75072018-07-01 22:31:34 +0200353
354static int exercise_key( psa_key_slot_t slot,
355 psa_key_usage_t usage,
356 psa_algorithm_t alg )
357{
358 int ok;
359 if( alg == 0 )
360 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
361 else if( PSA_ALG_IS_MAC( alg ) )
362 ok = exercise_mac_key( slot, usage, alg );
363 else if( PSA_ALG_IS_CIPHER( alg ) )
364 ok = exercise_cipher_key( slot, usage, alg );
365 else if( PSA_ALG_IS_AEAD( alg ) )
366 ok = exercise_aead_key( slot, usage, alg );
367 else if( PSA_ALG_IS_SIGN( alg ) )
368 ok = exercise_signature_key( slot, usage, alg );
369 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
370 ok = exercise_asymmetric_encryption_key( slot, usage, alg );
371 else
372 {
373 char message[40];
374 mbedtls_snprintf( message, sizeof( message ),
375 "No code to exercise alg=0x%08lx",
376 (unsigned long) alg );
377 test_fail( message, __LINE__, __FILE__ );
378 ok = 0;
379 }
380 return( ok );
381}
382
Gilles Peskinee59236f2018-01-27 23:32:46 +0100383/* END_HEADER */
384
385/* BEGIN_DEPENDENCIES
386 * depends_on:MBEDTLS_PSA_CRYPTO_C
387 * END_DEPENDENCIES
388 */
389
390/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200391void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100392{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100393 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100394 int i;
395 for( i = 0; i <= 1; i++ )
396 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100397 status = psa_crypto_init( );
398 TEST_ASSERT( status == PSA_SUCCESS );
399 status = psa_crypto_init( );
400 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100401 mbedtls_psa_crypto_free( );
402 }
403}
404/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100405
406/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200407void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100408{
409 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200410 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100411 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100412
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100413 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300414 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100415 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
416
Gilles Peskine4abf7412018-06-18 16:35:34 +0200417 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200418 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100419 if( status == PSA_SUCCESS )
420 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
421
422exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100423 mbedtls_psa_crypto_free( );
424}
425/* END_CASE */
426
427/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200428void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
429{
430 int slot = 1;
431 size_t bits = bits_arg;
432 psa_status_t expected_status = expected_status_arg;
433 psa_status_t status;
434 psa_key_type_t type =
435 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
436 size_t buffer_size = /* Slight overapproximations */
437 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
438 unsigned char *buffer = mbedtls_calloc( 1, buffer_size );
439 unsigned char *p;
440 int ret;
441 size_t length;
442
443 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
444 TEST_ASSERT( buffer != NULL );
445
446 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
447 bits, keypair ) ) >= 0 );
448 length = ret;
449
450 /* Try importing the key */
451 status = psa_import_key( slot, type, p, length );
452 TEST_ASSERT( status == expected_status );
453 if( status == PSA_SUCCESS )
454 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
455
456exit:
457 mbedtls_free( buffer );
458 mbedtls_psa_crypto_free( );
459}
460/* END_CASE */
461
462/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300463void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300464 int type_arg,
465 int alg_arg,
466 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100467 int expected_bits,
468 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200469 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100470 int canonical_input )
471{
472 int slot = 1;
473 int slot2 = slot + 1;
474 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200475 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200476 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100477 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100478 unsigned char *exported = NULL;
479 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100480 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100481 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100482 size_t reexported_length;
483 psa_key_type_t got_type;
484 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200485 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100486
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100487 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300488 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
489 export_size = (ssize_t) data->len + export_size_delta;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100490 exported = mbedtls_calloc( 1, export_size );
491 TEST_ASSERT( exported != NULL );
492 if( ! canonical_input )
493 {
494 reexported = mbedtls_calloc( 1, export_size );
495 TEST_ASSERT( reexported != NULL );
496 }
497 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
498
mohammad1603a97cb8c2018-03-28 03:46:26 -0700499 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200500 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700501 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
502
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100503 /* Import the key */
504 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200505 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100506
507 /* Test the key information */
508 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200509 &got_type,
510 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100511 TEST_ASSERT( got_type == type );
512 TEST_ASSERT( got_bits == (size_t) expected_bits );
513
514 /* Export the key */
515 status = psa_export_key( slot,
516 exported, export_size,
517 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200518 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100519
520 /* The exported length must be set by psa_export_key() to a value between 0
521 * and export_size. On errors, the exported length must be 0. */
522 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
523 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
524 TEST_ASSERT( exported_length <= export_size );
525
Gilles Peskine3f669c32018-06-21 09:21:51 +0200526 TEST_ASSERT( mem_is_zero( exported + exported_length,
527 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100528 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200529 {
530 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100531 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200532 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100533
534 if( canonical_input )
535 {
Gilles Peskine4abf7412018-06-18 16:35:34 +0200536 TEST_ASSERT( exported_length == data->len );
537 TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100538 }
539 else
540 {
mohammad1603a97cb8c2018-03-28 03:46:26 -0700541 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
542
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100543 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200544 exported,
545 export_size ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100546 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200547 reexported,
548 export_size,
549 &reexported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100550 TEST_ASSERT( reexported_length == exported_length );
551 TEST_ASSERT( memcmp( reexported, exported,
552 exported_length ) == 0 );
553 }
554
555destroy:
556 /* Destroy the key */
557 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
558 TEST_ASSERT( psa_get_key_information(
559 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
560
561exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300562 mbedtls_free( exported );
563 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100564 mbedtls_psa_crypto_free( );
565}
566/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100567
Moran Pekerf709f4a2018-06-06 17:26:04 +0300568/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300569void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200570 int type_arg,
571 int alg_arg,
572 int expected_bits,
573 int public_key_expected_length,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200574 int expected_export_status_arg )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300575{
576 int slot = 1;
577 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200578 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200579 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300580 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300581 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300582 size_t export_size;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100583 size_t exported_length = INVALID_EXPORT_LENGTH;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300584 psa_key_type_t got_type;
585 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200586 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300587
Moran Pekerf709f4a2018-06-06 17:26:04 +0300588 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300589 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
590 export_size = (ssize_t) data->len;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300591 exported = mbedtls_calloc( 1, export_size );
592 TEST_ASSERT( exported != NULL );
593
594 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
595
596 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200597 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300598 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
599
600 /* Import the key */
601 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200602 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300603
604 /* Test the key information */
605 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200606 &got_type,
607 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300608 TEST_ASSERT( got_type == type );
609 TEST_ASSERT( got_bits == (size_t) expected_bits );
610
611 /* Export the key */
612 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200613 exported, export_size,
614 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200615 TEST_ASSERT( status == expected_export_status );
Jaeden Amero2a671e92018-06-27 17:47:40 +0100616 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
617 TEST_ASSERT( mem_is_zero( exported + exported_length,
618 export_size - exported_length ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300619 if( status != PSA_SUCCESS )
620 goto destroy;
621
Moran Pekerf709f4a2018-06-06 17:26:04 +0300622destroy:
623 /* Destroy the key */
624 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
625 TEST_ASSERT( psa_get_key_information(
626 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
627
628exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300629 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300630 mbedtls_psa_crypto_free( );
631}
632/* END_CASE */
633
Gilles Peskine20035e32018-02-03 22:44:14 +0100634/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200635void import_and_exercise_key( data_t *data,
636 int type_arg,
637 int bits_arg,
638 int alg_arg )
639{
640 int slot = 1;
641 psa_key_type_t type = type_arg;
642 size_t bits = bits_arg;
643 psa_algorithm_t alg = alg_arg;
644 psa_key_usage_t usage =
645 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
646 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
647 PSA_KEY_USAGE_VERIFY :
648 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
649 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
650 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
651 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
652 PSA_KEY_USAGE_ENCRYPT :
653 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
654 0 );
655 psa_key_policy_t policy;
656 psa_key_type_t got_type;
657 size_t got_bits;
658 psa_status_t status;
659
660 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
661
662 psa_key_policy_init( &policy );
663 psa_key_policy_set_usage( &policy, usage, alg );
664 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
665
666 /* Import the key */
667 status = psa_import_key( slot, type, data->x, data->len );
668 TEST_ASSERT( status == PSA_SUCCESS );
669
670 /* Test the key information */
671 TEST_ASSERT( psa_get_key_information( slot,
672 &got_type,
673 &got_bits ) == PSA_SUCCESS );
674 TEST_ASSERT( got_type == type );
675 TEST_ASSERT( got_bits == bits );
676
677 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +0200678 if( ! exercise_key( slot, usage, alg ) )
679 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200680
681exit:
682 psa_destroy_key( slot );
683 mbedtls_psa_crypto_free( );
684}
685/* END_CASE */
686
687/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +0200688void key_policy( int usage_arg, int alg_arg )
689{
690 int key_slot = 1;
691 psa_algorithm_t alg = alg_arg;
692 psa_key_usage_t usage = usage_arg;
693 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
694 unsigned char key[32] = {0};
695 psa_key_policy_t policy_set;
696 psa_key_policy_t policy_get;
697
698 memset( key, 0x2a, sizeof( key ) );
699
700 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
701
702 psa_key_policy_init( &policy_set );
703 psa_key_policy_init( &policy_get );
704
705 psa_key_policy_set_usage( &policy_set, usage, alg );
706
707 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
708 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
709 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
710
711 TEST_ASSERT( psa_import_key( key_slot, key_type,
712 key, sizeof( key ) ) == PSA_SUCCESS );
713
714 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
715
716 TEST_ASSERT( policy_get.usage == policy_set.usage );
717 TEST_ASSERT( policy_get.alg == policy_set.alg );
718
719exit:
720 psa_destroy_key( key_slot );
721 mbedtls_psa_crypto_free( );
722}
723/* END_CASE */
724
725/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200726void mac_key_policy( int policy_usage,
727 int policy_alg,
728 int key_type,
729 data_t *key_data,
730 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200731{
732 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +0200733 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200734 psa_mac_operation_t operation;
735 psa_status_t status;
736 unsigned char mac[PSA_MAC_MAX_SIZE];
737 size_t output_length;
Gilles Peskined5b33222018-06-18 22:20:03 +0200738
739 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
740
741 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200742 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200743 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
744
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200745 TEST_ASSERT( psa_import_key( key_slot, key_type,
746 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +0200747
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200748 status = psa_mac_start( &operation, key_slot, exercise_alg );
749 if( status == PSA_SUCCESS )
750 status = psa_mac_finish( &operation,
751 mac, sizeof( mac ), &output_length );
752 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 ) );
760 status = psa_mac_start( &operation, key_slot, exercise_alg );
761 if( status == PSA_SUCCESS )
762 status = psa_mac_verify( &operation, mac, sizeof( mac ) );
763 if( policy_alg == exercise_alg &&
764 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
765 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
766 else
767 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
768
769exit:
770 psa_mac_abort( &operation );
771 psa_destroy_key( key_slot );
772 mbedtls_psa_crypto_free( );
773}
774/* END_CASE */
775
776/* BEGIN_CASE */
777void cipher_key_policy( int policy_usage,
778 int policy_alg,
779 int key_type,
780 data_t *key_data,
781 int exercise_alg )
782{
783 int key_slot = 1;
784 psa_key_policy_t policy;
785 psa_cipher_operation_t operation;
786 psa_status_t status;
787
788 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
789
790 psa_key_policy_init( &policy );
791 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
792 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
793
794 TEST_ASSERT( psa_import_key( key_slot, key_type,
795 key_data->x, key_data->len ) == PSA_SUCCESS );
796
797 status = psa_encrypt_setup( &operation, key_slot, exercise_alg );
798 if( policy_alg == exercise_alg &&
799 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
800 TEST_ASSERT( status == PSA_SUCCESS );
801 else
802 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
803 psa_cipher_abort( &operation );
804
805 status = psa_decrypt_setup( &operation, key_slot, exercise_alg );
806 if( policy_alg == exercise_alg &&
807 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
808 TEST_ASSERT( status == PSA_SUCCESS );
809 else
810 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
811
812exit:
813 psa_cipher_abort( &operation );
814 psa_destroy_key( key_slot );
815 mbedtls_psa_crypto_free( );
816}
817/* END_CASE */
818
819/* BEGIN_CASE */
820void aead_key_policy( int policy_usage,
821 int policy_alg,
822 int key_type,
823 data_t *key_data,
824 int nonce_length_arg,
825 int tag_length_arg,
826 int exercise_alg )
827{
828 int key_slot = 1;
829 psa_key_policy_t policy;
830 psa_status_t status;
831 unsigned char nonce[16] = {0};
832 size_t nonce_length = nonce_length_arg;
833 unsigned char tag[16];
834 size_t tag_length = tag_length_arg;
835 size_t output_length;
836
837 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
838 TEST_ASSERT( tag_length <= sizeof( tag ) );
839
840 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
841
842 psa_key_policy_init( &policy );
843 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
844 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
845
846 TEST_ASSERT( psa_import_key( key_slot, key_type,
847 key_data->x, key_data->len ) == PSA_SUCCESS );
848
849 status = psa_aead_encrypt( key_slot, exercise_alg,
850 nonce, nonce_length,
851 NULL, 0,
852 NULL, 0,
853 tag, tag_length,
854 &output_length );
855 if( policy_alg == exercise_alg &&
856 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
857 TEST_ASSERT( status == PSA_SUCCESS );
858 else
859 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
860
861 memset( tag, 0, sizeof( tag ) );
862 status = psa_aead_decrypt( key_slot, exercise_alg,
863 nonce, nonce_length,
864 NULL, 0,
865 tag, tag_length,
866 NULL, 0,
867 &output_length );
868 if( policy_alg == exercise_alg &&
869 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
870 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
871 else
872 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
873
874exit:
875 psa_destroy_key( key_slot );
876 mbedtls_psa_crypto_free( );
877}
878/* END_CASE */
879
880/* BEGIN_CASE */
881void asymmetric_encryption_key_policy( int policy_usage,
882 int policy_alg,
883 int key_type,
884 data_t *key_data,
885 int exercise_alg )
886{
887 int key_slot = 1;
888 psa_key_policy_t policy;
889 psa_status_t status;
890 size_t key_bits;
891 size_t buffer_length;
892 unsigned char *buffer = NULL;
893 size_t output_length;
894
895 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
896
897 psa_key_policy_init( &policy );
898 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
899 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
900
901 TEST_ASSERT( psa_import_key( key_slot, key_type,
902 key_data->x, key_data->len ) == PSA_SUCCESS );
903
904 TEST_ASSERT( psa_get_key_information( key_slot,
905 NULL,
906 &key_bits ) == PSA_SUCCESS );
907 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
908 exercise_alg );
909 buffer = mbedtls_calloc( 1, buffer_length );
910 TEST_ASSERT( buffer != NULL );
911
912 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
913 NULL, 0,
914 NULL, 0,
915 buffer, buffer_length,
916 &output_length );
917 if( policy_alg == exercise_alg &&
918 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
919 TEST_ASSERT( status == PSA_SUCCESS );
920 else
921 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
922
923 memset( buffer, 0, buffer_length );
924 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
925 buffer, buffer_length,
926 NULL, 0,
927 buffer, buffer_length,
928 &output_length );
929 if( policy_alg == exercise_alg &&
930 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
931 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
932 else
933 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
934
935exit:
936 psa_destroy_key( key_slot );
937 mbedtls_psa_crypto_free( );
938 mbedtls_free( buffer );
939}
940/* END_CASE */
941
942/* BEGIN_CASE */
943void asymmetric_signature_key_policy( int policy_usage,
944 int policy_alg,
945 int key_type,
946 data_t *key_data,
947 int exercise_alg )
948{
949 int key_slot = 1;
950 psa_key_policy_t policy;
951 psa_status_t status;
952 unsigned char payload[16] = {1};
953 size_t payload_length = sizeof( payload );
954 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
955 size_t signature_length;
956
957 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
958
959 psa_key_policy_init( &policy );
960 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
961 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
962
963 TEST_ASSERT( psa_import_key( key_slot, key_type,
964 key_data->x, key_data->len ) == PSA_SUCCESS );
965
966 status = psa_asymmetric_sign( key_slot, exercise_alg,
967 payload, payload_length,
968 NULL, 0,
969 signature, sizeof( signature ),
970 &signature_length );
971 if( policy_alg == exercise_alg &&
972 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
973 TEST_ASSERT( status == PSA_SUCCESS );
974 else
975 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
976
977 memset( signature, 0, sizeof( signature ) );
978 status = psa_asymmetric_verify( key_slot, exercise_alg,
979 payload, payload_length,
980 NULL, 0,
981 signature, sizeof( signature ) );
982 if( policy_alg == exercise_alg &&
983 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
984 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
985 else
986 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +0200987
988exit:
989 psa_destroy_key( key_slot );
990 mbedtls_psa_crypto_free( );
991}
992/* END_CASE */
993
994/* BEGIN_CASE */
995void key_lifetime( int lifetime_arg )
996{
997 int key_slot = 1;
998 psa_key_type_t key_type = PSA_ALG_CBC_BASE;
999 unsigned char key[32] = {0};
1000 psa_key_lifetime_t lifetime_set = lifetime_arg;
1001 psa_key_lifetime_t lifetime_get;
1002
1003 memset( key, 0x2a, sizeof( key ) );
1004
1005 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1006
1007 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1008 lifetime_set ) == PSA_SUCCESS );
1009
1010 TEST_ASSERT( psa_import_key( key_slot, key_type,
1011 key, sizeof( key ) ) == PSA_SUCCESS );
1012
1013 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1014 &lifetime_get ) == PSA_SUCCESS );
1015
1016 TEST_ASSERT( lifetime_get == lifetime_set );
1017
1018exit:
1019 psa_destroy_key( key_slot );
1020 mbedtls_psa_crypto_free( );
1021}
1022/* END_CASE */
1023
1024/* BEGIN_CASE */
1025void key_lifetime_set_fail( int key_slot_arg,
1026 int lifetime_arg,
1027 int expected_status_arg )
1028{
1029 psa_key_slot_t key_slot = key_slot_arg;
1030 psa_key_lifetime_t lifetime_set = lifetime_arg;
1031 psa_status_t actual_status;
1032 psa_status_t expected_status = expected_status_arg;
1033
1034 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1035
1036 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1037
1038 if( actual_status == PSA_SUCCESS )
1039 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1040
1041 TEST_ASSERT( expected_status == actual_status );
1042
1043exit:
1044 psa_destroy_key( key_slot );
1045 mbedtls_psa_crypto_free( );
1046}
1047/* END_CASE */
1048
1049/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001050void hash_setup( int alg_arg,
1051 int expected_status_arg )
1052{
1053 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001054 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001055 psa_hash_operation_t operation;
1056 psa_status_t status;
1057
1058 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1059
1060 status = psa_hash_start( &operation, alg );
1061 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001062 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001063
1064exit:
1065 mbedtls_psa_crypto_free( );
1066}
1067/* END_CASE */
1068
1069/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001070void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001071{
1072 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001073 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001074 size_t actual_hash_length;
1075 psa_hash_operation_t operation;
1076
Gilles Peskine69c12672018-06-28 00:07:19 +02001077 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1078 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1079
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001080 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001081 TEST_ASSERT( expected_hash != NULL );
1082 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1083 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001084
1085 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1086
1087 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
1088 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001089 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001090 TEST_ASSERT( psa_hash_finish( &operation,
1091 actual_hash, sizeof( actual_hash ),
1092 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001093 TEST_ASSERT( actual_hash_length == expected_hash->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001094 TEST_ASSERT( memcmp( expected_hash->x, actual_hash,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001095 expected_hash->len ) == 0 );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001096
1097exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001098 mbedtls_psa_crypto_free( );
1099}
1100/* END_CASE */
1101
1102/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001103void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001104{
1105 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001106 psa_hash_operation_t operation;
1107
Gilles Peskine69c12672018-06-28 00:07:19 +02001108 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1109 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1110
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001111 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001112 TEST_ASSERT( expected_hash != NULL );
1113 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1114 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001115
1116 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1117
1118 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
1119 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001120 input->x,
1121 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001122 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001123 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001124 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001125
1126exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001127 mbedtls_psa_crypto_free( );
1128}
1129/* END_CASE */
1130
1131/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001132void mac_setup( int key_type_arg,
1133 data_t *key,
1134 int alg_arg,
1135 int expected_status_arg )
1136{
1137 int key_slot = 1;
1138 psa_key_type_t key_type = key_type_arg;
1139 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001140 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001141 psa_mac_operation_t operation;
1142 psa_key_policy_t policy;
1143 psa_status_t status;
1144
1145 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1146
1147 psa_key_policy_init( &policy );
1148 psa_key_policy_set_usage( &policy,
1149 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1150 alg );
1151 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1152
1153 TEST_ASSERT( psa_import_key( key_slot, key_type,
1154 key->x, key->len ) == PSA_SUCCESS );
1155
1156 status = psa_mac_start( &operation, key_slot, alg );
1157 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001158 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001159
1160exit:
1161 psa_destroy_key( key_slot );
1162 mbedtls_psa_crypto_free( );
1163}
1164/* END_CASE */
1165
1166/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001167void mac_verify( int key_type_arg,
1168 data_t *key,
1169 int alg_arg,
1170 data_t *input,
1171 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001172{
1173 int key_slot = 1;
1174 psa_key_type_t key_type = key_type_arg;
1175 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001176 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001177 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001178
Gilles Peskine69c12672018-06-28 00:07:19 +02001179 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1180
Gilles Peskine8c9def32018-02-08 10:02:12 +01001181 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001182 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001183 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001184 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001185 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1186 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001187
1188 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1189
mohammad16036df908f2018-04-02 08:34:15 -07001190 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001191 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07001192 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1193
Gilles Peskine8c9def32018-02-08 10:02:12 +01001194 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001195 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001196
Gilles Peskine8c9def32018-02-08 10:02:12 +01001197 TEST_ASSERT( psa_mac_start( &operation, key_slot, alg ) == PSA_SUCCESS );
1198 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1199 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001200 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001201 TEST_ASSERT( psa_mac_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001202 expected_mac->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001203 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001204
1205exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001206 psa_destroy_key( key_slot );
1207 mbedtls_psa_crypto_free( );
1208}
1209/* END_CASE */
1210
1211/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001212void cipher_setup( int key_type_arg,
1213 data_t *key,
1214 int alg_arg,
1215 int expected_status_arg )
1216{
1217 int key_slot = 1;
1218 psa_key_type_t key_type = key_type_arg;
1219 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001220 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001221 psa_cipher_operation_t operation;
1222 psa_key_policy_t policy;
1223 psa_status_t status;
1224
1225 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1226
1227 psa_key_policy_init( &policy );
1228 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1229 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1230
1231 TEST_ASSERT( psa_import_key( key_slot, key_type,
1232 key->x, key->len ) == PSA_SUCCESS );
1233
1234 status = psa_encrypt_setup( &operation, key_slot, alg );
1235 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001236 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001237
1238exit:
1239 psa_destroy_key( key_slot );
1240 mbedtls_psa_crypto_free( );
1241}
1242/* END_CASE */
1243
1244/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001245void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001246 data_t *key,
1247 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001248 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001249{
1250 int key_slot = 1;
1251 psa_status_t status;
1252 psa_key_type_t key_type = key_type_arg;
1253 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001254 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001255 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001256 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001257 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001258 size_t output_buffer_size = 0;
1259 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001260 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001261 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001262 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001263
Gilles Peskine50e586b2018-06-08 14:28:46 +02001264 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001265 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001266 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001267 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1268 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1269 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001270
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001271 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1272 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001273
1274 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1275
Moran Pekered346952018-07-05 15:22:45 +03001276 psa_key_policy_init( &policy );
1277 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1278 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1279
Gilles Peskine50e586b2018-06-08 14:28:46 +02001280 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001281 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001282
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001283 TEST_ASSERT( psa_encrypt_setup( &operation,
1284 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001285
1286 TEST_ASSERT( psa_encrypt_set_iv( &operation,
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001287 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001288 output_buffer_size = (size_t) input->len +
1289 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001290 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001291 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001292
Gilles Peskine4abf7412018-06-18 16:35:34 +02001293 TEST_ASSERT( psa_cipher_update( &operation,
1294 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001295 output, output_buffer_size,
1296 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001297 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001298 status = psa_cipher_finish( &operation,
1299 output + function_output_length,
1300 output_buffer_size,
1301 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001302 total_output_length += function_output_length;
1303
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001304 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001305 if( expected_status == PSA_SUCCESS )
1306 {
1307 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001308 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001309 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001310 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001311 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001312
Gilles Peskine50e586b2018-06-08 14:28:46 +02001313exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001314 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001315 psa_destroy_key( key_slot );
1316 mbedtls_psa_crypto_free( );
1317}
1318/* END_CASE */
1319
1320/* BEGIN_CASE */
1321void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001322 data_t *key,
1323 data_t *input,
1324 int first_part_size,
1325 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001326{
1327 int key_slot = 1;
1328 psa_key_type_t key_type = key_type_arg;
1329 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001330 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001331 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001332 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001333 size_t output_buffer_size = 0;
1334 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001335 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001336 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001337 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001338
Gilles Peskine50e586b2018-06-08 14:28:46 +02001339 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001340 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001341 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001342 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1343 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1344 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001345
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001346 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1347 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001348
1349 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1350
Moran Pekered346952018-07-05 15:22:45 +03001351 psa_key_policy_init( &policy );
1352 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1353 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1354
Gilles Peskine50e586b2018-06-08 14:28:46 +02001355 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001356 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001357
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001358 TEST_ASSERT( psa_encrypt_setup( &operation,
1359 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001360
1361 TEST_ASSERT( psa_encrypt_set_iv( &operation,
1362 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001363 output_buffer_size = (size_t) input->len +
1364 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001365 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001366 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001367
Gilles Peskine4abf7412018-06-18 16:35:34 +02001368 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001369 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001370 output, output_buffer_size,
1371 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001372 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001373 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001374 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001375 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001376 output, output_buffer_size,
1377 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001378 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001379 TEST_ASSERT( psa_cipher_finish( &operation,
1380 output + function_output_length,
1381 output_buffer_size,
1382 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001383 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001384 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1385
Gilles Peskine4abf7412018-06-18 16:35:34 +02001386 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001387 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001388 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001389
1390exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001391 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001392 psa_destroy_key( key_slot );
1393 mbedtls_psa_crypto_free( );
1394}
1395/* END_CASE */
1396
1397/* BEGIN_CASE */
1398void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001399 data_t *key,
1400 data_t *input,
1401 int first_part_size,
1402 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001403{
1404 int key_slot = 1;
1405
1406 psa_key_type_t key_type = key_type_arg;
1407 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001408 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001409 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001410 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001411 size_t output_buffer_size = 0;
1412 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001413 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001414 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001415 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001416
Gilles Peskine50e586b2018-06-08 14:28:46 +02001417 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001418 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001419 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001420 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1421 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1422 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001423
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001424 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1425 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001426
1427 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1428
Moran Pekered346952018-07-05 15:22:45 +03001429 psa_key_policy_init( &policy );
1430 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1431 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1432
Gilles Peskine50e586b2018-06-08 14:28:46 +02001433 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001434 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001435
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001436 TEST_ASSERT( psa_decrypt_setup( &operation,
1437 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001438
1439 TEST_ASSERT( psa_encrypt_set_iv( &operation,
1440 iv, sizeof( iv ) ) == PSA_SUCCESS );
1441
mohammad16033d91abe2018-07-03 13:15:54 +03001442 output_buffer_size = (size_t) input->len +
1443 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001444 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001445 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001446
Gilles Peskine4abf7412018-06-18 16:35:34 +02001447 TEST_ASSERT( (unsigned int) first_part_size < input->len );
1448 TEST_ASSERT( psa_cipher_update( &operation,
1449 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001450 output, output_buffer_size,
1451 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001452 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001453 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001454 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001455 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001456 output, output_buffer_size,
1457 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001458 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001459 TEST_ASSERT( psa_cipher_finish( &operation,
1460 output + function_output_length,
1461 output_buffer_size,
1462 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001463 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001464 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1465
Gilles Peskine4abf7412018-06-18 16:35:34 +02001466 TEST_ASSERT( total_output_length == expected_output->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001467 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001468 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001469
1470exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001471 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001472 psa_destroy_key( key_slot );
1473 mbedtls_psa_crypto_free( );
1474}
1475/* END_CASE */
1476
Gilles Peskine50e586b2018-06-08 14:28:46 +02001477/* BEGIN_CASE */
1478void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001479 data_t *key,
1480 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001481 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001482{
1483 int key_slot = 1;
1484 psa_status_t status;
1485 psa_key_type_t key_type = key_type_arg;
1486 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001487 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001488 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001489 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001490 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001491 size_t output_buffer_size = 0;
1492 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001493 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001494 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001495 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001496
Gilles Peskine50e586b2018-06-08 14:28:46 +02001497 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001498 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001499 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001500 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1501 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1502 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001503
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001504 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1505 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001506
1507 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1508
Moran Pekered346952018-07-05 15:22:45 +03001509 psa_key_policy_init( &policy );
1510 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1511 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1512
Gilles Peskine50e586b2018-06-08 14:28:46 +02001513 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001514 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001515
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001516 TEST_ASSERT( psa_decrypt_setup( &operation,
1517 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001518
1519 TEST_ASSERT( psa_encrypt_set_iv( &operation,
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001520 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001521
mohammad16033d91abe2018-07-03 13:15:54 +03001522 output_buffer_size = (size_t) input->len +
1523 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001524 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001525 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001526
Gilles Peskine4abf7412018-06-18 16:35:34 +02001527 TEST_ASSERT( psa_cipher_update( &operation,
1528 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001529 output, output_buffer_size,
1530 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001531 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001532 status = psa_cipher_finish( &operation,
1533 output + function_output_length,
1534 output_buffer_size,
1535 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001536 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001537 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001538
1539 if( expected_status == PSA_SUCCESS )
1540 {
1541 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001542 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001543 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001544 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001545 }
1546
Gilles Peskine50e586b2018-06-08 14:28:46 +02001547exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001548 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001549 psa_destroy_key( key_slot );
1550 mbedtls_psa_crypto_free( );
1551}
1552/* END_CASE */
1553
Gilles Peskine50e586b2018-06-08 14:28:46 +02001554/* BEGIN_CASE */
1555void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001556 data_t *key,
1557 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02001558{
1559 int key_slot = 1;
1560 psa_key_type_t key_type = key_type_arg;
1561 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07001562 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02001563 size_t iv_size = 16;
1564 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001565 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001566 size_t output1_size = 0;
1567 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001568 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001569 size_t output2_size = 0;
1570 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001571 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001572 psa_cipher_operation_t operation1;
1573 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03001574 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001575
mohammad1603d7d7ba52018-03-12 18:51:53 +02001576 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001577 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001578 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1579 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001580
mohammad1603d7d7ba52018-03-12 18:51:53 +02001581 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1582
Moran Pekered346952018-07-05 15:22:45 +03001583 psa_key_policy_init( &policy );
1584 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
1585 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1586
mohammad1603d7d7ba52018-03-12 18:51:53 +02001587 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001588 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001589
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001590 TEST_ASSERT( psa_encrypt_setup( &operation1,
1591 key_slot, alg ) == PSA_SUCCESS );
1592 TEST_ASSERT( psa_decrypt_setup( &operation2,
1593 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001594
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001595 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
1596 iv, iv_size,
1597 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001598 output1_size = (size_t) input->len +
1599 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001600 output1 = mbedtls_calloc( 1, output1_size );
1601 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001602
Gilles Peskine4abf7412018-06-18 16:35:34 +02001603 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001604 output1, output1_size,
1605 &output1_length ) == PSA_SUCCESS );
1606 TEST_ASSERT( psa_cipher_finish( &operation1,
1607 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001608 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001609
Gilles Peskine048b7f02018-06-08 14:20:49 +02001610 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001611
1612 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1613
1614 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001615 output2 = mbedtls_calloc( 1, output2_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001616 TEST_ASSERT( output2 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001617
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001618 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
1619 iv, iv_length ) == PSA_SUCCESS );
1620 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
1621 output2, output2_size,
1622 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001623 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001624 TEST_ASSERT( psa_cipher_finish( &operation2,
1625 output2 + output2_length,
1626 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001627 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001628
Gilles Peskine048b7f02018-06-08 14:20:49 +02001629 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001630
Janos Follath25c4fa82018-07-06 16:23:25 +01001631 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001632
Gilles Peskine4abf7412018-06-18 16:35:34 +02001633 TEST_ASSERT( input->len == output2_length );
1634 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +03001635
1636exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001637 mbedtls_free( output1 );
1638 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03001639 psa_destroy_key( key_slot );
1640 mbedtls_psa_crypto_free( );
1641}
1642/* END_CASE */
1643
1644/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001645void cipher_verify_output_multipart( int alg_arg,
1646 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001647 data_t *key,
1648 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001649 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03001650{
1651 int key_slot = 1;
1652 psa_key_type_t key_type = key_type_arg;
1653 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03001654 unsigned char iv[16] = {0};
1655 size_t iv_size = 16;
1656 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001657 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001658 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001659 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001660 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001661 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001662 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001663 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001664 psa_cipher_operation_t operation1;
1665 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03001666 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03001667
Moran Pekerded84402018-06-06 16:36:50 +03001668 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001669 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001670 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1671 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001672
Moran Pekerded84402018-06-06 16:36:50 +03001673 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1674
Moran Pekered346952018-07-05 15:22:45 +03001675 psa_key_policy_init( &policy );
1676 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
1677 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1678
Moran Pekerded84402018-06-06 16:36:50 +03001679 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001680 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001681
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001682 TEST_ASSERT( psa_encrypt_setup( &operation1,
1683 key_slot, alg ) == PSA_SUCCESS );
1684 TEST_ASSERT( psa_decrypt_setup( &operation2,
1685 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001686
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001687 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
1688 iv, iv_size,
1689 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001690 output1_buffer_size = (size_t) input->len +
1691 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001692 output1 = mbedtls_calloc( 1, output1_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001693 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001694
Gilles Peskine4abf7412018-06-18 16:35:34 +02001695 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001696
itayzafrir3e02b3b2018-06-12 17:06:52 +03001697 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001698 output1, output1_buffer_size,
1699 &function_output_length ) == PSA_SUCCESS );
1700 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001701
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001702 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001703 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001704 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001705 output1, output1_buffer_size,
1706 &function_output_length ) == PSA_SUCCESS );
1707 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001708
Gilles Peskine048b7f02018-06-08 14:20:49 +02001709 TEST_ASSERT( psa_cipher_finish( &operation1,
1710 output1 + output1_length,
1711 output1_buffer_size - output1_length,
1712 &function_output_length ) == PSA_SUCCESS );
1713 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001714
1715 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1716
Gilles Peskine048b7f02018-06-08 14:20:49 +02001717 output2_buffer_size = output1_length;
1718 output2 = mbedtls_calloc( 1, output2_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001719 TEST_ASSERT( output2 != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001720
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001721 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
1722 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001723
1724 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001725 output2, output2_buffer_size,
1726 &function_output_length ) == PSA_SUCCESS );
1727 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001728
Gilles Peskine048b7f02018-06-08 14:20:49 +02001729 TEST_ASSERT( psa_cipher_update( &operation2,
1730 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001731 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001732 output2, output2_buffer_size,
1733 &function_output_length ) == PSA_SUCCESS );
1734 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001735
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001736 TEST_ASSERT( psa_cipher_finish( &operation2,
1737 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001738 output2_buffer_size - output2_length,
1739 &function_output_length ) == PSA_SUCCESS );
1740 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001741
Janos Follath25c4fa82018-07-06 16:23:25 +01001742 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001743
Gilles Peskine4abf7412018-06-18 16:35:34 +02001744 TEST_ASSERT( input->len == output2_length );
1745 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001746
1747exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001748 mbedtls_free( output1 );
1749 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001750 psa_destroy_key( key_slot );
1751 mbedtls_psa_crypto_free( );
1752}
1753/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02001754
Gilles Peskine20035e32018-02-03 22:44:14 +01001755/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001756void aead_encrypt_decrypt( int key_type_arg,
1757 data_t * key_data,
1758 int alg_arg,
1759 data_t * input_data,
1760 data_t * nonce,
1761 data_t * additional_data,
1762 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001763{
1764 int slot = 1;
1765 psa_key_type_t key_type = key_type_arg;
1766 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001767 unsigned char *output_data = NULL;
1768 size_t output_size = 0;
1769 size_t output_length = 0;
1770 unsigned char *output_data2 = NULL;
1771 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001772 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001773 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001774 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001775
Gilles Peskinea1cac842018-06-11 19:33:02 +02001776 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001777 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001778 TEST_ASSERT( nonce != NULL );
1779 TEST_ASSERT( additional_data != NULL );
1780 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1781 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1782 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1783 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1784
Gilles Peskine4abf7412018-06-18 16:35:34 +02001785 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001786 output_data = mbedtls_calloc( 1, output_size );
1787 TEST_ASSERT( output_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001788
1789 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1790
1791 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001792 psa_key_policy_set_usage( &policy,
1793 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
1794 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001795 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1796
1797 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001798 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001799
1800 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001801 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001802 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001803 additional_data->len,
1804 input_data->x, input_data->len,
1805 output_data, output_size,
1806 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001807
1808 if( PSA_SUCCESS == expected_result )
1809 {
1810 output_data2 = mbedtls_calloc( 1, output_length );
1811 TEST_ASSERT( output_data2 != NULL );
1812
1813 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001814 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001815 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001816 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001817 output_data, output_length,
1818 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001819 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02001820
itayzafrir3e02b3b2018-06-12 17:06:52 +03001821 TEST_ASSERT( memcmp( input_data->x, output_data2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001822 input_data->len ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001823 }
Gilles Peskine2d277862018-06-18 15:41:12 +02001824
Gilles Peskinea1cac842018-06-11 19:33:02 +02001825exit:
1826 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001827 mbedtls_free( output_data );
1828 mbedtls_free( output_data2 );
1829 mbedtls_psa_crypto_free( );
1830}
1831/* END_CASE */
1832
1833/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001834void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001835 int alg_arg, data_t * input_data,
1836 data_t * additional_data, data_t * nonce,
1837 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001838{
1839 int slot = 1;
1840 psa_key_type_t key_type = key_type_arg;
1841 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001842 unsigned char *output_data = NULL;
1843 size_t output_size = 0;
1844 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001845 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001846 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001847
Gilles Peskinea1cac842018-06-11 19:33:02 +02001848 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001849 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001850 TEST_ASSERT( additional_data != NULL );
1851 TEST_ASSERT( nonce != NULL );
1852 TEST_ASSERT( expected_result != NULL );
1853 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1854 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1855 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1856 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1857 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
1858
Gilles Peskine4abf7412018-06-18 16:35:34 +02001859 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001860 output_data = mbedtls_calloc( 1, output_size );
1861 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001862
Gilles Peskinea1cac842018-06-11 19:33:02 +02001863 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1864
1865 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001866 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001867 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1868
1869 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001870 key_data->x,
1871 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001872
1873 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001874 nonce->x, nonce->len,
1875 additional_data->x, additional_data->len,
1876 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001877 output_data, output_size,
1878 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001879
itayzafrir3e02b3b2018-06-12 17:06:52 +03001880 TEST_ASSERT( memcmp( output_data, expected_result->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001881 output_length ) == 0 );
1882
Gilles Peskinea1cac842018-06-11 19:33:02 +02001883exit:
1884 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001885 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001886 mbedtls_psa_crypto_free( );
1887}
1888/* END_CASE */
1889
1890/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001891void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001892 int alg_arg, data_t * input_data,
1893 data_t * additional_data, data_t * nonce,
1894 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001895{
1896 int slot = 1;
1897 psa_key_type_t key_type = key_type_arg;
1898 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001899 unsigned char *output_data = NULL;
1900 size_t output_size = 0;
1901 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001902 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001903 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001904 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001905
Gilles Peskinea1cac842018-06-11 19:33:02 +02001906 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001907 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001908 TEST_ASSERT( additional_data != NULL );
1909 TEST_ASSERT( nonce != NULL );
1910 TEST_ASSERT( expected_data != NULL );
1911 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1912 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1913 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1914 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1915 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
1916
Gilles Peskine4abf7412018-06-18 16:35:34 +02001917 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001918 output_data = mbedtls_calloc( 1, output_size );
1919 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001920
Gilles Peskinea1cac842018-06-11 19:33:02 +02001921 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1922
1923 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001924 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001925 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1926
1927 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001928 key_data->x,
1929 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001930
1931 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001932 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001933 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001934 additional_data->len,
1935 input_data->x, input_data->len,
1936 output_data, output_size,
1937 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001938
Gilles Peskine2d277862018-06-18 15:41:12 +02001939 if( expected_result == PSA_SUCCESS )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001940 {
itayzafrir3e02b3b2018-06-12 17:06:52 +03001941 TEST_ASSERT( memcmp( output_data, expected_data->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001942 output_length ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001943 }
1944
Gilles Peskinea1cac842018-06-11 19:33:02 +02001945exit:
1946 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001947 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001948 mbedtls_psa_crypto_free( );
1949}
1950/* END_CASE */
1951
1952/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001953void signature_size( int type_arg,
1954 int bits,
1955 int alg_arg,
1956 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001957{
1958 psa_key_type_t type = type_arg;
1959 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02001960 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01001961 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
1962exit:
1963 ;
1964}
1965/* END_CASE */
1966
1967/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001968void sign_deterministic( int key_type_arg, data_t *key_data,
1969 int alg_arg, data_t *input_data,
1970 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001971{
1972 int slot = 1;
1973 psa_key_type_t key_type = key_type_arg;
1974 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01001975 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01001976 unsigned char *signature = NULL;
1977 size_t signature_size;
1978 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02001979 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01001980
Gilles Peskine20035e32018-02-03 22:44:14 +01001981 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001982 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001983 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001984 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1985 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1986 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01001987
1988 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1989
mohammad1603a97cb8c2018-03-28 03:46:26 -07001990 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001991 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001992 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1993
Gilles Peskine20035e32018-02-03 22:44:14 +01001994 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001995 key_data->x,
1996 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01001997 TEST_ASSERT( psa_get_key_information( slot,
1998 NULL,
1999 &key_bits ) == PSA_SUCCESS );
2000
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002001 /* Allocate a buffer which has the size advertized by the
2002 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002003 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2004 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002005 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002006 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine20035e32018-02-03 22:44:14 +01002007 signature = mbedtls_calloc( 1, signature_size );
2008 TEST_ASSERT( signature != NULL );
2009
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002010 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002011 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002012 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002013 NULL, 0,
2014 signature, signature_size,
2015 &signature_length ) == PSA_SUCCESS );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002016 /* Verify that the signature is correct. */
Gilles Peskine4abf7412018-06-18 16:35:34 +02002017 TEST_ASSERT( signature_length == output_data->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02002018 TEST_ASSERT( memcmp( signature, output_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002019 output_data->len ) == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01002020
2021exit:
2022 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002023 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002024 mbedtls_psa_crypto_free( );
2025}
2026/* END_CASE */
2027
2028/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002029void sign_fail( int key_type_arg, data_t *key_data,
2030 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002031 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002032{
2033 int slot = 1;
2034 psa_key_type_t key_type = key_type_arg;
2035 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002036 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002037 psa_status_t actual_status;
2038 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002039 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002040 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002041 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002042
Gilles Peskine20035e32018-02-03 22:44:14 +01002043 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002044 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002045 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2046 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2047
Gilles Peskine20035e32018-02-03 22:44:14 +01002048 signature = mbedtls_calloc( 1, signature_size );
2049 TEST_ASSERT( signature != NULL );
2050
2051 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2052
mohammad1603a97cb8c2018-03-28 03:46:26 -07002053 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002054 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002055 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2056
Gilles Peskine20035e32018-02-03 22:44:14 +01002057 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002058 key_data->x,
2059 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002060
2061 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002062 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002063 NULL, 0,
2064 signature, signature_size,
2065 &signature_length );
2066 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002067 /* The value of *signature_length is unspecified on error, but
2068 * whatever it is, it should be less than signature_size, so that
2069 * if the caller tries to read *signature_length bytes without
2070 * checking the error code then they don't overflow a buffer. */
2071 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002072
2073exit:
2074 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002075 mbedtls_free( signature );
2076 mbedtls_psa_crypto_free( );
2077}
2078/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002079
2080/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002081void asymmetric_verify( int key_type_arg, data_t *key_data,
2082 int alg_arg, data_t *hash_data,
2083 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002084{
2085 int slot = 1;
2086 psa_key_type_t key_type = key_type_arg;
2087 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002088 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002089
Gilles Peskine69c12672018-06-28 00:07:19 +02002090 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2091
itayzafrir5c753392018-05-08 11:18:38 +03002092 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002093 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002094 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002095 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2096 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2097 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002098
2099 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2100
2101 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002102 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002103 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2104
2105 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002106 key_data->x,
2107 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002108
2109 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002110 hash_data->x, hash_data->len,
itayzafrir5c753392018-05-08 11:18:38 +03002111 NULL, 0,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002112 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002113 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002114exit:
2115 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002116 mbedtls_psa_crypto_free( );
2117}
2118/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002119
2120/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002121void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2122 int alg_arg, data_t *hash_data,
2123 data_t *signature_data,
2124 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002125{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002126 int slot = 1;
2127 psa_key_type_t key_type = key_type_arg;
2128 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002129 psa_status_t actual_status;
2130 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002131 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002132
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002133 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002134 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002135 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002136 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2137 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2138 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002139
2140 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2141
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002142 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002143 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002144 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2145
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002146 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002147 key_data->x,
2148 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002149
2150 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002151 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002152 NULL, 0,
2153 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002154 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002155
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002156 TEST_ASSERT( actual_status == expected_status );
2157
2158exit:
2159 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002160 mbedtls_psa_crypto_free( );
2161}
2162/* END_CASE */
2163
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002164/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002165void asymmetric_encrypt_decrypt( int key_type_arg, data_t *key_data,
2166 int alg_arg, data_t *input_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002167{
2168 int slot = 1;
2169 psa_key_type_t key_type = key_type_arg;
2170 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002171 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002172 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002173 size_t output_length = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002174 unsigned char *output2 = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002175 size_t output2_size = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002176 size_t output2_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002177 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002178
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002179 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002180 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002181 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2182 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2183
Gilles Peskine4abf7412018-06-18 16:35:34 +02002184 output_size = key_data->len;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002185 output2_size = output_size;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002186 output = mbedtls_calloc( 1, output_size );
2187 TEST_ASSERT( output != NULL );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002188 output2 = mbedtls_calloc( 1, output2_size );
2189 TEST_ASSERT( output2 != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002190
2191 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2192
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002193 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002194 psa_key_policy_set_usage( &policy,
2195 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002196 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002197 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2198
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002199 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002200 key_data->x,
2201 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002202
Gilles Peskineeebd7382018-06-08 18:11:54 +02002203 /* We test encryption by checking that encrypt-then-decrypt gives back
2204 * the original plaintext because of the non-optional random
2205 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02002206 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002207 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002208 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002209 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002210 &output_length ) == PSA_SUCCESS );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002211
Gilles Peskine2d277862018-06-18 15:41:12 +02002212 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002213 output, output_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02002214 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002215 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002216 &output2_length ) == PSA_SUCCESS );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002217 TEST_ASSERT( memcmp( input_data->x, output2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002218 input_data->len ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002219
2220exit:
2221 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002222 mbedtls_free( output );
2223 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002224 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002225}
2226/* END_CASE */
2227
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002228/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002229void asymmetric_encrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002230 int alg_arg, data_t *input_data,
2231 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002232{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002233 int slot = 1;
2234 psa_key_type_t key_type = key_type_arg;
2235 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002236 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002237 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002238 size_t output_length = 0;
2239 psa_status_t actual_status;
2240 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002241 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002242
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002243 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002244 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002245 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2246 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2247
Gilles Peskine4abf7412018-06-18 16:35:34 +02002248 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002249 output = mbedtls_calloc( 1, output_size );
2250 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002251
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002252 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2253
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002254 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002255 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002256 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2257
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002258 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002259 key_data->x,
2260 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002261
Gilles Peskine2d277862018-06-18 15:41:12 +02002262 actual_status = psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002263 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002264 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002265 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002266 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002267 TEST_ASSERT( actual_status == expected_status );
2268
2269exit:
2270 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002271 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002272 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002273}
2274/* END_CASE */
2275
2276/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002277void asymmetric_decrypt( int key_type_arg, data_t *key_data,
2278 int alg_arg, data_t *input_data,
2279 data_t *expected_data, int expected_size )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002280{
2281 int slot = 1;
2282 psa_key_type_t key_type = key_type_arg;
2283 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002284 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002285 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002286 size_t output_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002287 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002288
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002289 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002290 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002291 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002292 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2293 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2294 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2295
Gilles Peskine4abf7412018-06-18 16:35:34 +02002296 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002297 output = mbedtls_calloc( 1, output_size );
2298 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002299
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002300 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2301
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002302 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002303 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002304 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2305
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002306 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002307 key_data->x,
2308 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002309
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002310 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002311 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002312 NULL, 0,
2313 output,
2314 output_size,
2315 &output_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002316 TEST_ASSERT( (size_t) expected_size == output_length );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002317 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002318
2319exit:
2320 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002321 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002322 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002323}
2324/* END_CASE */
2325
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002326/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002327void asymmetric_decrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002328 int alg_arg, data_t *input_data,
2329 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002330{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002331 int slot = 1;
2332 psa_key_type_t key_type = key_type_arg;
2333 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002334 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002335 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002336 size_t output_length = 0;
2337 psa_status_t actual_status;
2338 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002339 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002340
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002341 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002342 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002343 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2344 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2345
Gilles Peskine4abf7412018-06-18 16:35:34 +02002346 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002347 output = mbedtls_calloc( 1, output_size );
2348 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002349
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002350 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2351
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002352 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002353 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002354 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2355
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002356 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002357 key_data->x,
2358 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002359
Gilles Peskine2d277862018-06-18 15:41:12 +02002360 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002361 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002362 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002363 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002364 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002365 TEST_ASSERT( actual_status == expected_status );
2366
2367exit:
2368 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02002369 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002370 mbedtls_psa_crypto_free( );
2371}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002372/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02002373
2374/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02002375void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02002376{
Gilles Peskinea50d7392018-06-21 10:22:13 +02002377 size_t bytes = bytes_arg;
2378 const unsigned char trail[] = "don't overwrite me";
2379 unsigned char *output = mbedtls_calloc( 1, bytes + sizeof( trail ) );
2380 unsigned char *changed = mbedtls_calloc( 1, bytes );
2381 size_t i;
2382 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02002383
Gilles Peskinea50d7392018-06-21 10:22:13 +02002384 TEST_ASSERT( output != NULL );
2385 TEST_ASSERT( changed != NULL );
2386 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02002387
2388 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2389
Gilles Peskinea50d7392018-06-21 10:22:13 +02002390 /* Run several times, to ensure that every output byte will be
2391 * nonzero at least once with overwhelming probability
2392 * (2^(-8*number_of_runs)). */
2393 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02002394 {
Gilles Peskinea50d7392018-06-21 10:22:13 +02002395 memset( output, 0, bytes );
2396 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
2397
2398 /* Check that no more than bytes have been overwritten */
2399 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
2400
2401 for( i = 0; i < bytes; i++ )
2402 {
2403 if( output[i] != 0 )
2404 ++changed[i];
2405 }
Gilles Peskine05d69892018-06-19 22:00:52 +02002406 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02002407
2408 /* Check that every byte was changed to nonzero at least once. This
2409 * validates that psa_generate_random is overwriting every byte of
2410 * the output buffer. */
2411 for( i = 0; i < bytes; i++ )
2412 {
2413 TEST_ASSERT( changed[i] != 0 );
2414 }
Gilles Peskine05d69892018-06-19 22:00:52 +02002415
2416exit:
2417 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02002418 mbedtls_free( output );
2419 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02002420}
2421/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02002422
2423/* BEGIN_CASE */
2424void generate_key( int type_arg,
2425 int bits_arg,
2426 int usage_arg,
2427 int alg_arg,
2428 int expected_status_arg )
2429{
2430 int slot = 1;
2431 psa_key_type_t type = type_arg;
2432 psa_key_usage_t usage = usage_arg;
2433 size_t bits = bits_arg;
2434 psa_algorithm_t alg = alg_arg;
2435 psa_status_t expected_status = expected_status_arg;
2436 psa_key_type_t got_type;
2437 size_t got_bits;
2438 unsigned char exported[616] = {0}; /* enough for a 1024-bit RSA key */
2439 size_t exported_length;
2440 psa_status_t expected_export_status =
2441 usage & PSA_KEY_USAGE_EXPORT ? PSA_SUCCESS : PSA_ERROR_NOT_PERMITTED;
2442 psa_status_t expected_info_status =
2443 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
2444 psa_key_policy_t policy;
2445
2446 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2447
2448 psa_key_policy_init( &policy );
2449 psa_key_policy_set_usage( &policy, usage, alg );
2450 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2451
2452 /* Generate a key */
2453 TEST_ASSERT( psa_generate_key( slot, type, bits,
2454 NULL, 0 ) == expected_status );
2455
2456 /* Test the key information */
2457 TEST_ASSERT( psa_get_key_information( slot,
2458 &got_type,
2459 &got_bits ) == expected_info_status );
2460 if( expected_info_status != PSA_SUCCESS )
2461 goto exit;
2462 TEST_ASSERT( got_type == type );
2463 TEST_ASSERT( got_bits == bits );
2464
2465 /* Export the key */
2466 TEST_ASSERT( psa_export_key( slot,
2467 exported, sizeof( exported ),
2468 &exported_length ) == expected_export_status );
2469 if( expected_export_status == PSA_SUCCESS )
2470 {
Gilles Peskine48c0ea12018-06-21 14:15:31 +02002471 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02002472 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
2473#if defined(MBEDTLS_DES_C)
2474 if( type == PSA_KEY_TYPE_DES )
2475 {
2476 /* Check the parity bits. */
2477 unsigned i;
2478 for( i = 0; i < bits / 8; i++ )
2479 {
2480 unsigned bit_count = 0;
2481 unsigned m;
2482 for( m = 1; m <= 0x100; m <<= 1 )
2483 {
2484 if( exported[i] & m )
2485 ++bit_count;
2486 }
2487 TEST_ASSERT( bit_count % 2 != 0 );
2488 }
2489 }
2490#endif
2491#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
2492 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
2493 {
2494 /* Sanity check: does this look like the beginning of a PKCS#8
2495 * RSA key pair? Assumes bits is a multiple of 8. */
2496 size_t n_bytes = bits / 8 + 1;
2497 size_t n_encoded_bytes;
2498 unsigned char *n_end;
2499 TEST_ASSERT( exported_length >= 7 + ( n_bytes + 3 ) * 9 / 2 );
2500 TEST_ASSERT( exported[0] == 0x30 );
2501 TEST_ASSERT( exported[1] == 0x82 ); // assumes >=416-bit key
2502 TEST_ASSERT( exported[4] == 0x02 );
2503 TEST_ASSERT( exported[5] == 0x01 );
2504 TEST_ASSERT( exported[6] == 0x00 );
2505 TEST_ASSERT( exported[7] == 0x02 );
2506 n_encoded_bytes = exported[8];
2507 n_end = exported + 9 + n_encoded_bytes;
2508 if( n_encoded_bytes & 0x80 )
2509 {
2510 n_encoded_bytes = ( n_encoded_bytes & 0x7f ) << 7;
2511 n_encoded_bytes |= exported[9] & 0x7f;
2512 n_end += 1;
2513 }
2514 /* The encoding of n should start with a 0 byte since it should
2515 * have its high bit set. However Mbed TLS is not compliant and
2516 * generates an invalid, but widely tolerated, encoding of
2517 * positive INTEGERs with a bit size that is a multiple of 8
2518 * with no leading 0 byte. Accept this here. */
2519 TEST_ASSERT( n_bytes == n_encoded_bytes ||
2520 n_bytes == n_encoded_bytes + 1 );
2521 if( n_bytes == n_encoded_bytes )
2522 TEST_ASSERT( exported[n_encoded_bytes <= 127 ? 9 : 10] == 0x00 );
2523 /* Sanity check: e must be 3 */
2524 TEST_ASSERT( n_end[0] == 0x02 );
2525 TEST_ASSERT( n_end[1] == 0x03 );
2526 TEST_ASSERT( n_end[2] == 0x01 );
2527 TEST_ASSERT( n_end[3] == 0x00 );
2528 TEST_ASSERT( n_end[4] == 0x01 );
2529 TEST_ASSERT( n_end[5] == 0x02 );
2530 }
2531#endif /* MBEDTLS_RSA_C */
2532#if defined(MBEDTLS_ECP_C)
2533 if( PSA_KEY_TYPE_IS_ECC( type ) )
2534 {
2535 /* Sanity check: does this look like the beginning of a PKCS#8
2536 * elliptic curve key pair? */
2537 TEST_ASSERT( exported_length >= bits * 3 / 8 + 10 );
2538 TEST_ASSERT( exported[0] == 0x30 );
2539 }
2540#endif /* MBEDTLS_ECP_C */
2541 }
2542
Gilles Peskine818ca122018-06-20 18:16:48 +02002543 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02002544 if( ! exercise_key( slot, usage, alg ) )
2545 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02002546
2547exit:
2548 psa_destroy_key( slot );
2549 mbedtls_psa_crypto_free( );
2550}
2551/* END_CASE */