blob: 8c8d41d26942a0f7cfb42511fb148479e0fed147 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
4#if defined(MBEDTLS_PSA_CRYPTO_SPM)
5#include "spm/psa_defs.h"
6#endif
7
Gilles Peskine0b352bc2018-06-28 00:16:11 +02008#include "mbedtls/asn1write.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +01009#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030010
11#if(UINT32_MAX > SIZE_MAX)
Gilles Peskine2d277862018-06-18 15:41:12 +020012#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX )
itayzafrir3e02b3b2018-06-12 17:06:52 +030013#else
Gilles Peskine2d277862018-06-18 15:41:12 +020014#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1
itayzafrir3e02b3b2018-06-12 17:06:52 +030015#endif
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020016
Jaeden Amerof24c7f82018-06-27 17:20:43 +010017/** An invalid export length that will never be set by psa_export_key(). */
18static const size_t INVALID_EXPORT_LENGTH = ~0U;
19
Gilles Peskined35a1cc2018-06-26 21:26:10 +020020/** Test if a buffer is all-bits zero.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020021 *
22 * \param buffer Pointer to the beginning of the buffer.
23 * \param size Size of the buffer in bytes.
24 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020025 * \return 1 if the buffer is all-bits-zero.
26 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020027 */
Gilles Peskine3f669c32018-06-21 09:21:51 +020028static int mem_is_zero( void *buffer, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020029{
30 size_t i;
31 for( i = 0; i < size; i++ )
32 {
33 if( ( (unsigned char *) buffer )[i] != 0 )
Gilles Peskine3f669c32018-06-21 09:21:51 +020034 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020035 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020036 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037}
Gilles Peskine818ca122018-06-20 18:16:48 +020038
Gilles Peskine48c0ea12018-06-21 14:15:31 +020039static int key_type_is_raw_bytes( psa_key_type_t type )
40{
41 psa_key_type_t category = type & PSA_KEY_TYPE_CATEGORY_MASK;
42 return( category == PSA_KEY_TYPE_RAW_DATA ||
43 category == PSA_KEY_TYPE_CATEGORY_SYMMETRIC );
44}
45
Gilles Peskine0b352bc2018-06-28 00:16:11 +020046/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
47static int asn1_write_10x( unsigned char **p,
48 unsigned char *start,
49 size_t bits,
50 unsigned char x )
51{
52 int ret;
53 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020054 if( bits == 0 )
55 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
56 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020057 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
58 if( *p < start || *p - start < (ssize_t) len )
59 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
60 *p -= len;
61 ( *p )[len-1] = x;
62 if( bits % 8 == 0 )
63 ( *p )[1] |= 1;
64 else
65 ( *p )[0] |= 1 << ( bits % 8 );
66 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
67 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
68 MBEDTLS_ASN1_INTEGER ) );
69 return( len );
70}
71
72static int construct_fake_rsa_key( unsigned char *buffer,
73 size_t buffer_size,
74 unsigned char **p,
75 size_t bits,
76 int keypair )
77{
78 size_t half_bits = ( bits + 1 ) / 2;
79 int ret;
80 int len = 0;
81 /* Construct something that looks like a DER encoding of
82 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
83 * RSAPrivateKey ::= SEQUENCE {
84 * version Version,
85 * modulus INTEGER, -- n
86 * publicExponent INTEGER, -- e
87 * privateExponent INTEGER, -- d
88 * prime1 INTEGER, -- p
89 * prime2 INTEGER, -- q
90 * exponent1 INTEGER, -- d mod (p-1)
91 * exponent2 INTEGER, -- d mod (q-1)
92 * coefficient INTEGER, -- (inverse of q) mod p
93 * otherPrimeInfos OtherPrimeInfos OPTIONAL
94 * }
95 * Or, for a public key, the same structure with only
96 * version, modulus and publicExponent.
97 */
98 *p = buffer + buffer_size;
99 if( keypair )
100 {
101 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
102 asn1_write_10x( p, buffer, half_bits, 1 ) );
103 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
104 asn1_write_10x( p, buffer, half_bits, 1 ) );
105 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
106 asn1_write_10x( p, buffer, half_bits, 1 ) );
107 MBEDTLS_ASN1_CHK_ADD( len, /* q */
108 asn1_write_10x( p, buffer, half_bits, 1 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
110 asn1_write_10x( p, buffer, half_bits, 3 ) );
111 MBEDTLS_ASN1_CHK_ADD( len, /* d */
112 asn1_write_10x( p, buffer, bits, 1 ) );
113 }
114 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
115 asn1_write_10x( p, buffer, 17, 1 ) );
116 MBEDTLS_ASN1_CHK_ADD( len, /* n */
117 asn1_write_10x( p, buffer, bits, 1 ) );
118 if( keypair )
119 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
120 mbedtls_asn1_write_int( p, buffer, 0 ) );
121 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
122 {
123 const unsigned char tag =
124 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
125 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
126 }
127 return( len );
128}
129
Gilles Peskine818ca122018-06-20 18:16:48 +0200130static int exercise_mac_key( psa_key_slot_t key,
131 psa_key_usage_t usage,
132 psa_algorithm_t alg )
133{
134 psa_mac_operation_t operation;
135 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200136 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200137 size_t mac_length = sizeof( mac );
138
139 if( usage & PSA_KEY_USAGE_SIGN )
140 {
Gilles Peskine89167cb2018-07-08 20:12:23 +0200141 TEST_ASSERT( psa_mac_sign_setup( &operation,
142 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200143 TEST_ASSERT( psa_mac_update( &operation,
144 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200145 TEST_ASSERT( psa_mac_sign_finish( &operation,
146 mac, sizeof( input ),
147 &mac_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200148 }
149
150 if( usage & PSA_KEY_USAGE_VERIFY )
151 {
152 psa_status_t verify_status =
153 ( usage & PSA_KEY_USAGE_SIGN ?
154 PSA_SUCCESS :
155 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine89167cb2018-07-08 20:12:23 +0200156 TEST_ASSERT( psa_mac_verify_setup( &operation,
157 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200158 TEST_ASSERT( psa_mac_update( &operation,
159 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200160 TEST_ASSERT( psa_mac_verify_finish( &operation,
161 mac,
162 mac_length ) == verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200163 }
164
165 return( 1 );
166
167exit:
168 psa_mac_abort( &operation );
169 return( 0 );
170}
171
172static int exercise_cipher_key( psa_key_slot_t key,
173 psa_key_usage_t usage,
174 psa_algorithm_t alg )
175{
176 psa_cipher_operation_t operation;
177 unsigned char iv[16] = {0};
178 size_t iv_length = sizeof( iv );
179 const unsigned char plaintext[16] = "Hello, world...";
180 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
181 size_t ciphertext_length = sizeof( ciphertext );
182 unsigned char decrypted[sizeof( ciphertext )];
183 size_t part_length;
184
185 if( usage & PSA_KEY_USAGE_ENCRYPT )
186 {
Gilles Peskinefe119512018-07-08 21:39:34 +0200187 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
188 key, alg ) == PSA_SUCCESS );
189 TEST_ASSERT( psa_cipher_generate_iv( &operation,
190 iv, sizeof( iv ),
191 &iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200192 TEST_ASSERT( psa_cipher_update( &operation,
193 plaintext, sizeof( plaintext ),
194 ciphertext, sizeof( ciphertext ),
195 &ciphertext_length ) == PSA_SUCCESS );
196 TEST_ASSERT( psa_cipher_finish( &operation,
197 ciphertext + ciphertext_length,
198 sizeof( ciphertext ) - ciphertext_length,
199 &part_length ) == PSA_SUCCESS );
200 ciphertext_length += part_length;
201 }
202
203 if( usage & PSA_KEY_USAGE_DECRYPT )
204 {
205 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700206 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200207 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
208 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200209 size_t bits;
210 TEST_ASSERT( psa_get_key_information( key, &type, &bits ) );
211 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
212 }
Gilles Peskinefe119512018-07-08 21:39:34 +0200213 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
214 key, alg ) == PSA_SUCCESS );
215 TEST_ASSERT( psa_cipher_set_iv( &operation,
216 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200217 TEST_ASSERT( psa_cipher_update( &operation,
218 ciphertext, ciphertext_length,
219 decrypted, sizeof( decrypted ),
220 &part_length ) == PSA_SUCCESS );
221 status = psa_cipher_finish( &operation,
222 decrypted + part_length,
223 sizeof( decrypted ) - part_length,
224 &part_length );
225 /* For a stream cipher, all inputs are valid. For a block cipher,
226 * if the input is some aribtrary data rather than an actual
227 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700228 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700229 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine818ca122018-06-20 18:16:48 +0200230 TEST_ASSERT( status == PSA_SUCCESS );
231 else
232 TEST_ASSERT( status == PSA_SUCCESS ||
233 status == PSA_ERROR_INVALID_PADDING );
234 }
235
236 return( 1 );
237
238exit:
239 psa_cipher_abort( &operation );
240 return( 0 );
241}
242
243static int exercise_aead_key( psa_key_slot_t key,
244 psa_key_usage_t usage,
245 psa_algorithm_t alg )
246{
247 unsigned char nonce[16] = {0};
248 size_t nonce_length = sizeof( nonce );
249 unsigned char plaintext[16] = "Hello, world...";
250 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
251 size_t ciphertext_length = sizeof( ciphertext );
252 size_t plaintext_length = sizeof( ciphertext );
253
254 if( usage & PSA_KEY_USAGE_ENCRYPT )
255 {
256 TEST_ASSERT( psa_aead_encrypt( key, alg,
257 nonce, nonce_length,
258 NULL, 0,
259 plaintext, sizeof( plaintext ),
260 ciphertext, sizeof( ciphertext ),
261 &ciphertext_length ) == PSA_SUCCESS );
262 }
263
264 if( usage & PSA_KEY_USAGE_DECRYPT )
265 {
266 psa_status_t verify_status =
267 ( usage & PSA_KEY_USAGE_ENCRYPT ?
268 PSA_SUCCESS :
269 PSA_ERROR_INVALID_SIGNATURE );
270 TEST_ASSERT( psa_aead_decrypt( key, alg,
271 nonce, nonce_length,
272 NULL, 0,
273 ciphertext, ciphertext_length,
274 plaintext, sizeof( plaintext ),
275 &plaintext_length ) == verify_status );
276 }
277
278 return( 1 );
279
280exit:
281 return( 0 );
282}
283
284static int exercise_signature_key( psa_key_slot_t key,
285 psa_key_usage_t usage,
286 psa_algorithm_t alg )
287{
Gilles Peskineca45c352018-06-26 16:13:09 +0200288 unsigned char payload[16] = {1};
Gilles Peskine818ca122018-06-20 18:16:48 +0200289 size_t payload_length = sizeof( payload );
Gilles Peskine69c12672018-06-28 00:07:19 +0200290 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200291 size_t signature_length = sizeof( signature );
292
293 if( usage & PSA_KEY_USAGE_SIGN )
294 {
295 TEST_ASSERT( psa_asymmetric_sign( key, alg,
296 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200297 signature, sizeof( signature ),
298 &signature_length ) == PSA_SUCCESS );
299 }
300
301 if( usage & PSA_KEY_USAGE_VERIFY )
302 {
303 psa_status_t verify_status =
304 ( usage & PSA_KEY_USAGE_SIGN ?
305 PSA_SUCCESS :
306 PSA_ERROR_INVALID_SIGNATURE );
307 TEST_ASSERT( psa_asymmetric_verify( key, alg,
308 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200309 signature, signature_length ) ==
310 verify_status );
311 }
312
313 return( 1 );
314
315exit:
316 return( 0 );
317}
318
319static int exercise_asymmetric_encryption_key( psa_key_slot_t key,
320 psa_key_usage_t usage,
321 psa_algorithm_t alg )
322{
323 unsigned char plaintext[256] = "Hello, world...";
324 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
325 size_t ciphertext_length = sizeof( ciphertext );
326 size_t plaintext_length = 16;
327
328 if( usage & PSA_KEY_USAGE_ENCRYPT )
329 {
330 TEST_ASSERT(
331 psa_asymmetric_encrypt( key, alg,
332 plaintext, plaintext_length,
333 NULL, 0,
334 ciphertext, sizeof( ciphertext ),
335 &ciphertext_length ) == PSA_SUCCESS );
336 }
337
338 if( usage & PSA_KEY_USAGE_DECRYPT )
339 {
340 psa_status_t status =
341 psa_asymmetric_decrypt( key, alg,
342 ciphertext, ciphertext_length,
343 NULL, 0,
344 plaintext, sizeof( plaintext ),
345 &plaintext_length );
346 TEST_ASSERT( status == PSA_SUCCESS ||
347 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
348 ( status == PSA_ERROR_INVALID_ARGUMENT ||
349 status == PSA_ERROR_INVALID_PADDING ) ) );
350 }
351
352 return( 1 );
353
354exit:
355 return( 0 );
356}
Gilles Peskine02b75072018-07-01 22:31:34 +0200357
358static int exercise_key( psa_key_slot_t slot,
359 psa_key_usage_t usage,
360 psa_algorithm_t alg )
361{
362 int ok;
363 if( alg == 0 )
364 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
365 else if( PSA_ALG_IS_MAC( alg ) )
366 ok = exercise_mac_key( slot, usage, alg );
367 else if( PSA_ALG_IS_CIPHER( alg ) )
368 ok = exercise_cipher_key( slot, usage, alg );
369 else if( PSA_ALG_IS_AEAD( alg ) )
370 ok = exercise_aead_key( slot, usage, alg );
371 else if( PSA_ALG_IS_SIGN( alg ) )
372 ok = exercise_signature_key( slot, usage, alg );
373 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
374 ok = exercise_asymmetric_encryption_key( slot, usage, alg );
375 else
376 {
377 char message[40];
378 mbedtls_snprintf( message, sizeof( message ),
379 "No code to exercise alg=0x%08lx",
380 (unsigned long) alg );
381 test_fail( message, __LINE__, __FILE__ );
382 ok = 0;
383 }
384 return( ok );
385}
386
Gilles Peskinee59236f2018-01-27 23:32:46 +0100387/* END_HEADER */
388
389/* BEGIN_DEPENDENCIES
390 * depends_on:MBEDTLS_PSA_CRYPTO_C
391 * END_DEPENDENCIES
392 */
393
394/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200395void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100396{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100397 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100398 int i;
399 for( i = 0; i <= 1; i++ )
400 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100401 status = psa_crypto_init( );
402 TEST_ASSERT( status == PSA_SUCCESS );
403 status = psa_crypto_init( );
404 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100405 mbedtls_psa_crypto_free( );
406 }
407}
408/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100409
410/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200411void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100412{
413 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200414 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100415 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100416
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100417 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300418 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100419 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
420
Gilles Peskine4abf7412018-06-18 16:35:34 +0200421 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200422 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100423 if( status == PSA_SUCCESS )
424 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
425
426exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100427 mbedtls_psa_crypto_free( );
428}
429/* END_CASE */
430
431/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200432void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
433{
434 int slot = 1;
435 size_t bits = bits_arg;
436 psa_status_t expected_status = expected_status_arg;
437 psa_status_t status;
438 psa_key_type_t type =
439 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
440 size_t buffer_size = /* Slight overapproximations */
441 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
442 unsigned char *buffer = mbedtls_calloc( 1, buffer_size );
443 unsigned char *p;
444 int ret;
445 size_t length;
446
447 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
448 TEST_ASSERT( buffer != NULL );
449
450 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
451 bits, keypair ) ) >= 0 );
452 length = ret;
453
454 /* Try importing the key */
455 status = psa_import_key( slot, type, p, length );
456 TEST_ASSERT( status == expected_status );
457 if( status == PSA_SUCCESS )
458 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
459
460exit:
461 mbedtls_free( buffer );
462 mbedtls_psa_crypto_free( );
463}
464/* END_CASE */
465
466/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300467void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300468 int type_arg,
469 int alg_arg,
470 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100471 int expected_bits,
472 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200473 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100474 int canonical_input )
475{
476 int slot = 1;
477 int slot2 = slot + 1;
478 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200479 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200480 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100481 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100482 unsigned char *exported = NULL;
483 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100484 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100485 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100486 size_t reexported_length;
487 psa_key_type_t got_type;
488 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200489 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100490
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100491 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300492 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
493 export_size = (ssize_t) data->len + export_size_delta;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100494 exported = mbedtls_calloc( 1, export_size );
495 TEST_ASSERT( exported != NULL );
496 if( ! canonical_input )
497 {
498 reexported = mbedtls_calloc( 1, export_size );
499 TEST_ASSERT( reexported != NULL );
500 }
501 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
502
mohammad1603a97cb8c2018-03-28 03:46:26 -0700503 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200504 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700505 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
506
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100507 /* Import the key */
508 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200509 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100510
511 /* Test the key information */
512 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200513 &got_type,
514 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100515 TEST_ASSERT( got_type == type );
516 TEST_ASSERT( got_bits == (size_t) expected_bits );
517
518 /* Export the key */
519 status = psa_export_key( slot,
520 exported, export_size,
521 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200522 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100523
524 /* The exported length must be set by psa_export_key() to a value between 0
525 * and export_size. On errors, the exported length must be 0. */
526 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
527 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
528 TEST_ASSERT( exported_length <= export_size );
529
Gilles Peskine3f669c32018-06-21 09:21:51 +0200530 TEST_ASSERT( mem_is_zero( exported + exported_length,
531 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100532 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200533 {
534 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100535 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200536 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100537
538 if( canonical_input )
539 {
Gilles Peskine4abf7412018-06-18 16:35:34 +0200540 TEST_ASSERT( exported_length == data->len );
541 TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100542 }
543 else
544 {
mohammad1603a97cb8c2018-03-28 03:46:26 -0700545 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
546
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100547 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200548 exported,
549 export_size ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100550 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200551 reexported,
552 export_size,
553 &reexported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100554 TEST_ASSERT( reexported_length == exported_length );
555 TEST_ASSERT( memcmp( reexported, exported,
556 exported_length ) == 0 );
557 }
558
559destroy:
560 /* Destroy the key */
561 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
562 TEST_ASSERT( psa_get_key_information(
563 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
564
565exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300566 mbedtls_free( exported );
567 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100568 mbedtls_psa_crypto_free( );
569}
570/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100571
Moran Pekerf709f4a2018-06-06 17:26:04 +0300572/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300573void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200574 int type_arg,
575 int alg_arg,
576 int expected_bits,
577 int public_key_expected_length,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200578 int expected_export_status_arg )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300579{
580 int slot = 1;
581 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200582 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200583 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300584 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300585 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300586 size_t export_size;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100587 size_t exported_length = INVALID_EXPORT_LENGTH;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300588 psa_key_type_t got_type;
589 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200590 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300591
Moran Pekerf709f4a2018-06-06 17:26:04 +0300592 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300593 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
594 export_size = (ssize_t) data->len;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300595 exported = mbedtls_calloc( 1, export_size );
596 TEST_ASSERT( exported != NULL );
597
598 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
599
600 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200601 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300602 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
603
604 /* Import the key */
605 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200606 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300607
608 /* Test the key information */
609 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200610 &got_type,
611 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300612 TEST_ASSERT( got_type == type );
613 TEST_ASSERT( got_bits == (size_t) expected_bits );
614
615 /* Export the key */
616 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200617 exported, export_size,
618 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200619 TEST_ASSERT( status == expected_export_status );
Jaeden Amero2a671e92018-06-27 17:47:40 +0100620 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
621 TEST_ASSERT( mem_is_zero( exported + exported_length,
622 export_size - exported_length ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300623 if( status != PSA_SUCCESS )
624 goto destroy;
625
Moran Pekerf709f4a2018-06-06 17:26:04 +0300626destroy:
627 /* Destroy the key */
628 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
629 TEST_ASSERT( psa_get_key_information(
630 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
631
632exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300633 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300634 mbedtls_psa_crypto_free( );
635}
636/* END_CASE */
637
Gilles Peskine20035e32018-02-03 22:44:14 +0100638/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200639void import_and_exercise_key( data_t *data,
640 int type_arg,
641 int bits_arg,
642 int alg_arg )
643{
644 int slot = 1;
645 psa_key_type_t type = type_arg;
646 size_t bits = bits_arg;
647 psa_algorithm_t alg = alg_arg;
648 psa_key_usage_t usage =
649 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
650 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
651 PSA_KEY_USAGE_VERIFY :
652 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
653 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
654 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
655 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
656 PSA_KEY_USAGE_ENCRYPT :
657 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
658 0 );
659 psa_key_policy_t policy;
660 psa_key_type_t got_type;
661 size_t got_bits;
662 psa_status_t status;
663
664 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
665
666 psa_key_policy_init( &policy );
667 psa_key_policy_set_usage( &policy, usage, alg );
668 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
669
670 /* Import the key */
671 status = psa_import_key( slot, type, data->x, data->len );
672 TEST_ASSERT( status == PSA_SUCCESS );
673
674 /* Test the key information */
675 TEST_ASSERT( psa_get_key_information( slot,
676 &got_type,
677 &got_bits ) == PSA_SUCCESS );
678 TEST_ASSERT( got_type == type );
679 TEST_ASSERT( got_bits == bits );
680
681 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +0200682 if( ! exercise_key( slot, usage, alg ) )
683 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200684
685exit:
686 psa_destroy_key( slot );
687 mbedtls_psa_crypto_free( );
688}
689/* END_CASE */
690
691/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +0200692void key_policy( int usage_arg, int alg_arg )
693{
694 int key_slot = 1;
695 psa_algorithm_t alg = alg_arg;
696 psa_key_usage_t usage = usage_arg;
697 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
698 unsigned char key[32] = {0};
699 psa_key_policy_t policy_set;
700 psa_key_policy_t policy_get;
701
702 memset( key, 0x2a, sizeof( key ) );
703
704 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
705
706 psa_key_policy_init( &policy_set );
707 psa_key_policy_init( &policy_get );
708
709 psa_key_policy_set_usage( &policy_set, usage, alg );
710
711 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
712 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
713 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
714
715 TEST_ASSERT( psa_import_key( key_slot, key_type,
716 key, sizeof( key ) ) == PSA_SUCCESS );
717
718 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
719
720 TEST_ASSERT( policy_get.usage == policy_set.usage );
721 TEST_ASSERT( policy_get.alg == policy_set.alg );
722
723exit:
724 psa_destroy_key( key_slot );
725 mbedtls_psa_crypto_free( );
726}
727/* END_CASE */
728
729/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200730void mac_key_policy( int policy_usage,
731 int policy_alg,
732 int key_type,
733 data_t *key_data,
734 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200735{
736 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +0200737 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200738 psa_mac_operation_t operation;
739 psa_status_t status;
740 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200741
742 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
743
744 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200745 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200746 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
747
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200748 TEST_ASSERT( psa_import_key( key_slot, key_type,
749 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +0200750
Gilles Peskine89167cb2018-07-08 20:12:23 +0200751 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200752 if( policy_alg == exercise_alg &&
753 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
754 TEST_ASSERT( status == PSA_SUCCESS );
755 else
756 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
757 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200758
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200759 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +0200760 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200761 if( policy_alg == exercise_alg &&
762 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +0200763 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200764 else
765 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
766
767exit:
768 psa_mac_abort( &operation );
769 psa_destroy_key( key_slot );
770 mbedtls_psa_crypto_free( );
771}
772/* END_CASE */
773
774/* BEGIN_CASE */
775void cipher_key_policy( int policy_usage,
776 int policy_alg,
777 int key_type,
778 data_t *key_data,
779 int exercise_alg )
780{
781 int key_slot = 1;
782 psa_key_policy_t policy;
783 psa_cipher_operation_t operation;
784 psa_status_t status;
785
786 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
787
788 psa_key_policy_init( &policy );
789 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
790 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
791
792 TEST_ASSERT( psa_import_key( key_slot, key_type,
793 key_data->x, key_data->len ) == PSA_SUCCESS );
794
Gilles Peskinefe119512018-07-08 21:39:34 +0200795 status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200796 if( policy_alg == exercise_alg &&
797 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
798 TEST_ASSERT( status == PSA_SUCCESS );
799 else
800 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
801 psa_cipher_abort( &operation );
802
Gilles Peskinefe119512018-07-08 21:39:34 +0200803 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200804 if( policy_alg == exercise_alg &&
805 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
806 TEST_ASSERT( status == PSA_SUCCESS );
807 else
808 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
809
810exit:
811 psa_cipher_abort( &operation );
812 psa_destroy_key( key_slot );
813 mbedtls_psa_crypto_free( );
814}
815/* END_CASE */
816
817/* BEGIN_CASE */
818void aead_key_policy( int policy_usage,
819 int policy_alg,
820 int key_type,
821 data_t *key_data,
822 int nonce_length_arg,
823 int tag_length_arg,
824 int exercise_alg )
825{
826 int key_slot = 1;
827 psa_key_policy_t policy;
828 psa_status_t status;
829 unsigned char nonce[16] = {0};
830 size_t nonce_length = nonce_length_arg;
831 unsigned char tag[16];
832 size_t tag_length = tag_length_arg;
833 size_t output_length;
834
835 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
836 TEST_ASSERT( tag_length <= sizeof( tag ) );
837
838 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
839
840 psa_key_policy_init( &policy );
841 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
842 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
843
844 TEST_ASSERT( psa_import_key( key_slot, key_type,
845 key_data->x, key_data->len ) == PSA_SUCCESS );
846
847 status = psa_aead_encrypt( key_slot, exercise_alg,
848 nonce, nonce_length,
849 NULL, 0,
850 NULL, 0,
851 tag, tag_length,
852 &output_length );
853 if( policy_alg == exercise_alg &&
854 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
855 TEST_ASSERT( status == PSA_SUCCESS );
856 else
857 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
858
859 memset( tag, 0, sizeof( tag ) );
860 status = psa_aead_decrypt( key_slot, exercise_alg,
861 nonce, nonce_length,
862 NULL, 0,
863 tag, tag_length,
864 NULL, 0,
865 &output_length );
866 if( policy_alg == exercise_alg &&
867 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
868 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
869 else
870 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
871
872exit:
873 psa_destroy_key( key_slot );
874 mbedtls_psa_crypto_free( );
875}
876/* END_CASE */
877
878/* BEGIN_CASE */
879void asymmetric_encryption_key_policy( int policy_usage,
880 int policy_alg,
881 int key_type,
882 data_t *key_data,
883 int exercise_alg )
884{
885 int key_slot = 1;
886 psa_key_policy_t policy;
887 psa_status_t status;
888 size_t key_bits;
889 size_t buffer_length;
890 unsigned char *buffer = NULL;
891 size_t output_length;
892
893 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
894
895 psa_key_policy_init( &policy );
896 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
897 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
898
899 TEST_ASSERT( psa_import_key( key_slot, key_type,
900 key_data->x, key_data->len ) == PSA_SUCCESS );
901
902 TEST_ASSERT( psa_get_key_information( key_slot,
903 NULL,
904 &key_bits ) == PSA_SUCCESS );
905 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
906 exercise_alg );
907 buffer = mbedtls_calloc( 1, buffer_length );
908 TEST_ASSERT( buffer != NULL );
909
910 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
911 NULL, 0,
912 NULL, 0,
913 buffer, buffer_length,
914 &output_length );
915 if( policy_alg == exercise_alg &&
916 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
917 TEST_ASSERT( status == PSA_SUCCESS );
918 else
919 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
920
921 memset( buffer, 0, buffer_length );
922 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
923 buffer, buffer_length,
924 NULL, 0,
925 buffer, buffer_length,
926 &output_length );
927 if( policy_alg == exercise_alg &&
928 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
929 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
930 else
931 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
932
933exit:
934 psa_destroy_key( key_slot );
935 mbedtls_psa_crypto_free( );
936 mbedtls_free( buffer );
937}
938/* END_CASE */
939
940/* BEGIN_CASE */
941void asymmetric_signature_key_policy( int policy_usage,
942 int policy_alg,
943 int key_type,
944 data_t *key_data,
945 int exercise_alg )
946{
947 int key_slot = 1;
948 psa_key_policy_t policy;
949 psa_status_t status;
950 unsigned char payload[16] = {1};
951 size_t payload_length = sizeof( payload );
952 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
953 size_t signature_length;
954
955 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
956
957 psa_key_policy_init( &policy );
958 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
959 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
960
961 TEST_ASSERT( psa_import_key( key_slot, key_type,
962 key_data->x, key_data->len ) == PSA_SUCCESS );
963
964 status = psa_asymmetric_sign( key_slot, exercise_alg,
965 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200966 signature, sizeof( signature ),
967 &signature_length );
968 if( policy_alg == exercise_alg &&
969 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
970 TEST_ASSERT( status == PSA_SUCCESS );
971 else
972 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
973
974 memset( signature, 0, sizeof( signature ) );
975 status = psa_asymmetric_verify( key_slot, exercise_alg,
976 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200977 signature, sizeof( signature ) );
978 if( policy_alg == exercise_alg &&
979 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
980 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
981 else
982 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +0200983
984exit:
985 psa_destroy_key( key_slot );
986 mbedtls_psa_crypto_free( );
987}
988/* END_CASE */
989
990/* BEGIN_CASE */
991void key_lifetime( int lifetime_arg )
992{
993 int key_slot = 1;
994 psa_key_type_t key_type = PSA_ALG_CBC_BASE;
995 unsigned char key[32] = {0};
996 psa_key_lifetime_t lifetime_set = lifetime_arg;
997 psa_key_lifetime_t lifetime_get;
998
999 memset( key, 0x2a, sizeof( key ) );
1000
1001 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1002
1003 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1004 lifetime_set ) == PSA_SUCCESS );
1005
1006 TEST_ASSERT( psa_import_key( key_slot, key_type,
1007 key, sizeof( key ) ) == PSA_SUCCESS );
1008
1009 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1010 &lifetime_get ) == PSA_SUCCESS );
1011
1012 TEST_ASSERT( lifetime_get == lifetime_set );
1013
1014exit:
1015 psa_destroy_key( key_slot );
1016 mbedtls_psa_crypto_free( );
1017}
1018/* END_CASE */
1019
1020/* BEGIN_CASE */
1021void key_lifetime_set_fail( int key_slot_arg,
1022 int lifetime_arg,
1023 int expected_status_arg )
1024{
1025 psa_key_slot_t key_slot = key_slot_arg;
1026 psa_key_lifetime_t lifetime_set = lifetime_arg;
1027 psa_status_t actual_status;
1028 psa_status_t expected_status = expected_status_arg;
1029
1030 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1031
1032 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1033
1034 if( actual_status == PSA_SUCCESS )
1035 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1036
1037 TEST_ASSERT( expected_status == actual_status );
1038
1039exit:
1040 psa_destroy_key( key_slot );
1041 mbedtls_psa_crypto_free( );
1042}
1043/* END_CASE */
1044
1045/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001046void hash_setup( int alg_arg,
1047 int expected_status_arg )
1048{
1049 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001050 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001051 psa_hash_operation_t operation;
1052 psa_status_t status;
1053
1054 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1055
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001056 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001057 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001058 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001059
1060exit:
1061 mbedtls_psa_crypto_free( );
1062}
1063/* END_CASE */
1064
1065/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001066void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001067{
1068 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001069 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001070 size_t actual_hash_length;
1071 psa_hash_operation_t operation;
1072
Gilles Peskine69c12672018-06-28 00:07:19 +02001073 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1074 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1075
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001076 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001077 TEST_ASSERT( expected_hash != NULL );
1078 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1079 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001080
1081 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1082
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001083 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001084 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001085 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001086 TEST_ASSERT( psa_hash_finish( &operation,
1087 actual_hash, sizeof( actual_hash ),
1088 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001089 TEST_ASSERT( actual_hash_length == expected_hash->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001090 TEST_ASSERT( memcmp( expected_hash->x, actual_hash,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001091 expected_hash->len ) == 0 );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001092
1093exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001094 mbedtls_psa_crypto_free( );
1095}
1096/* END_CASE */
1097
1098/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001099void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001100{
1101 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001102 psa_hash_operation_t operation;
1103
Gilles Peskine69c12672018-06-28 00:07:19 +02001104 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1105 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1106
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001107 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001108 TEST_ASSERT( expected_hash != NULL );
1109 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1110 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001111
1112 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1113
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001114 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001115 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001116 input->x,
1117 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001118 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001119 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001120 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001121
1122exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001123 mbedtls_psa_crypto_free( );
1124}
1125/* END_CASE */
1126
1127/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001128void mac_setup( int key_type_arg,
1129 data_t *key,
1130 int alg_arg,
1131 int expected_status_arg )
1132{
1133 int key_slot = 1;
1134 psa_key_type_t key_type = key_type_arg;
1135 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001136 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001137 psa_mac_operation_t operation;
1138 psa_key_policy_t policy;
1139 psa_status_t status;
1140
1141 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1142
1143 psa_key_policy_init( &policy );
1144 psa_key_policy_set_usage( &policy,
1145 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1146 alg );
1147 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1148
1149 TEST_ASSERT( psa_import_key( key_slot, key_type,
1150 key->x, key->len ) == PSA_SUCCESS );
1151
Gilles Peskine89167cb2018-07-08 20:12:23 +02001152 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001153 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001154 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001155
1156exit:
1157 psa_destroy_key( key_slot );
1158 mbedtls_psa_crypto_free( );
1159}
1160/* END_CASE */
1161
1162/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001163void mac_verify( int key_type_arg,
1164 data_t *key,
1165 int alg_arg,
1166 data_t *input,
1167 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001168{
1169 int key_slot = 1;
1170 psa_key_type_t key_type = key_type_arg;
1171 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001172 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001173 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001174
Gilles Peskine69c12672018-06-28 00:07:19 +02001175 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1176
Gilles Peskine8c9def32018-02-08 10:02:12 +01001177 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001178 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001179 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001180 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001181 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1182 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001183
1184 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1185
mohammad16036df908f2018-04-02 08:34:15 -07001186 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001187 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07001188 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1189
Gilles Peskine8c9def32018-02-08 10:02:12 +01001190 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001191 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001192
Gilles Peskine89167cb2018-07-08 20:12:23 +02001193 TEST_ASSERT( psa_mac_verify_setup( &operation,
1194 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001195 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1196 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001197 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02001198 TEST_ASSERT( psa_mac_verify_finish( &operation,
1199 expected_mac->x,
1200 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001201
1202exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001203 psa_destroy_key( key_slot );
1204 mbedtls_psa_crypto_free( );
1205}
1206/* END_CASE */
1207
1208/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001209void cipher_setup( int key_type_arg,
1210 data_t *key,
1211 int alg_arg,
1212 int expected_status_arg )
1213{
1214 int key_slot = 1;
1215 psa_key_type_t key_type = key_type_arg;
1216 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001217 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001218 psa_cipher_operation_t operation;
1219 psa_key_policy_t policy;
1220 psa_status_t status;
1221
1222 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1223
1224 psa_key_policy_init( &policy );
1225 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1226 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1227
1228 TEST_ASSERT( psa_import_key( key_slot, key_type,
1229 key->x, key->len ) == PSA_SUCCESS );
1230
Gilles Peskinefe119512018-07-08 21:39:34 +02001231 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001232 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001233 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001234
1235exit:
1236 psa_destroy_key( key_slot );
1237 mbedtls_psa_crypto_free( );
1238}
1239/* END_CASE */
1240
1241/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001242void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001243 data_t *key,
1244 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001245 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001246{
1247 int key_slot = 1;
1248 psa_status_t status;
1249 psa_key_type_t key_type = key_type_arg;
1250 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001251 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001252 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001253 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001254 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001255 size_t output_buffer_size = 0;
1256 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001257 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001258 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001259 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001260
Gilles Peskine50e586b2018-06-08 14:28:46 +02001261 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001262 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001263 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001264 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1265 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1266 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001267
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001268 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1269 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001270
1271 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1272
Moran Pekered346952018-07-05 15:22:45 +03001273 psa_key_policy_init( &policy );
1274 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1275 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1276
Gilles Peskine50e586b2018-06-08 14:28:46 +02001277 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001278 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001279
Gilles Peskinefe119512018-07-08 21:39:34 +02001280 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1281 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001282
Gilles Peskinefe119512018-07-08 21:39:34 +02001283 TEST_ASSERT( psa_cipher_set_iv( &operation,
1284 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001285 output_buffer_size = (size_t) input->len +
1286 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001287 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001288 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001289
Gilles Peskine4abf7412018-06-18 16:35:34 +02001290 TEST_ASSERT( psa_cipher_update( &operation,
1291 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001292 output, output_buffer_size,
1293 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001294 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001295 status = psa_cipher_finish( &operation,
1296 output + function_output_length,
1297 output_buffer_size,
1298 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001299 total_output_length += function_output_length;
1300
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001301 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001302 if( expected_status == PSA_SUCCESS )
1303 {
1304 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001305 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001306 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001307 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001308 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001309
Gilles Peskine50e586b2018-06-08 14:28:46 +02001310exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001311 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001312 psa_destroy_key( key_slot );
1313 mbedtls_psa_crypto_free( );
1314}
1315/* END_CASE */
1316
1317/* BEGIN_CASE */
1318void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001319 data_t *key,
1320 data_t *input,
1321 int first_part_size,
1322 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001323{
1324 int key_slot = 1;
1325 psa_key_type_t key_type = key_type_arg;
1326 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001327 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001328 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001329 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001330 size_t output_buffer_size = 0;
1331 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001332 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001333 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001334 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001335
Gilles Peskine50e586b2018-06-08 14:28:46 +02001336 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001337 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001338 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001339 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1340 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1341 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001342
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001343 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1344 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001345
1346 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1347
Moran Pekered346952018-07-05 15:22:45 +03001348 psa_key_policy_init( &policy );
1349 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1350 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1351
Gilles Peskine50e586b2018-06-08 14:28:46 +02001352 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001353 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001354
Gilles Peskinefe119512018-07-08 21:39:34 +02001355 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1356 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001357
Gilles Peskinefe119512018-07-08 21:39:34 +02001358 TEST_ASSERT( psa_cipher_set_iv( &operation,
1359 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001360 output_buffer_size = (size_t) input->len +
1361 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001362 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001363 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001364
Gilles Peskine4abf7412018-06-18 16:35:34 +02001365 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001366 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001367 output, output_buffer_size,
1368 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001369 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001370 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001371 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001372 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001373 output, output_buffer_size,
1374 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001375 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001376 TEST_ASSERT( psa_cipher_finish( &operation,
1377 output + function_output_length,
1378 output_buffer_size,
1379 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001380 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001381 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1382
Gilles Peskine4abf7412018-06-18 16:35:34 +02001383 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001384 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001385 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001386
1387exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001388 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001389 psa_destroy_key( key_slot );
1390 mbedtls_psa_crypto_free( );
1391}
1392/* END_CASE */
1393
1394/* BEGIN_CASE */
1395void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001396 data_t *key,
1397 data_t *input,
1398 int first_part_size,
1399 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001400{
1401 int key_slot = 1;
1402
1403 psa_key_type_t key_type = key_type_arg;
1404 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001405 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001406 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001407 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001408 size_t output_buffer_size = 0;
1409 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001410 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001411 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001412 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001413
Gilles Peskine50e586b2018-06-08 14:28:46 +02001414 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001415 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001416 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001417 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1418 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1419 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001420
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001421 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1422 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001423
1424 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1425
Moran Pekered346952018-07-05 15:22:45 +03001426 psa_key_policy_init( &policy );
1427 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1428 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1429
Gilles Peskine50e586b2018-06-08 14:28:46 +02001430 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001431 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001432
Gilles Peskinefe119512018-07-08 21:39:34 +02001433 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1434 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001435
Gilles Peskinefe119512018-07-08 21:39:34 +02001436 TEST_ASSERT( psa_cipher_set_iv( &operation,
1437 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001438
mohammad16033d91abe2018-07-03 13:15:54 +03001439 output_buffer_size = (size_t) input->len +
1440 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001441 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001442 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001443
Gilles Peskine4abf7412018-06-18 16:35:34 +02001444 TEST_ASSERT( (unsigned int) first_part_size < input->len );
1445 TEST_ASSERT( psa_cipher_update( &operation,
1446 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001447 output, output_buffer_size,
1448 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001449 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001450 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001451 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001452 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001453 output, output_buffer_size,
1454 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001455 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001456 TEST_ASSERT( psa_cipher_finish( &operation,
1457 output + function_output_length,
1458 output_buffer_size,
1459 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001460 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001461 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1462
Gilles Peskine4abf7412018-06-18 16:35:34 +02001463 TEST_ASSERT( total_output_length == expected_output->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001464 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001465 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001466
1467exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001468 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001469 psa_destroy_key( key_slot );
1470 mbedtls_psa_crypto_free( );
1471}
1472/* END_CASE */
1473
Gilles Peskine50e586b2018-06-08 14:28:46 +02001474/* BEGIN_CASE */
1475void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001476 data_t *key,
1477 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001478 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001479{
1480 int key_slot = 1;
1481 psa_status_t status;
1482 psa_key_type_t key_type = key_type_arg;
1483 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001484 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001485 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001486 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001487 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001488 size_t output_buffer_size = 0;
1489 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001490 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001491 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001492 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001493
Gilles Peskine50e586b2018-06-08 14:28:46 +02001494 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001495 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001496 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001497 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1498 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1499 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001500
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001501 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1502 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001503
1504 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1505
Moran Pekered346952018-07-05 15:22:45 +03001506 psa_key_policy_init( &policy );
1507 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1508 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1509
Gilles Peskine50e586b2018-06-08 14:28:46 +02001510 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001511 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001512
Gilles Peskinefe119512018-07-08 21:39:34 +02001513 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1514 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001515
Gilles Peskinefe119512018-07-08 21:39:34 +02001516 TEST_ASSERT( psa_cipher_set_iv( &operation,
1517 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001518
mohammad16033d91abe2018-07-03 13:15:54 +03001519 output_buffer_size = (size_t) input->len +
1520 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001521 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001522 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001523
Gilles Peskine4abf7412018-06-18 16:35:34 +02001524 TEST_ASSERT( psa_cipher_update( &operation,
1525 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001526 output, output_buffer_size,
1527 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001528 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001529 status = psa_cipher_finish( &operation,
1530 output + function_output_length,
1531 output_buffer_size,
1532 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001533 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001534 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001535
1536 if( expected_status == PSA_SUCCESS )
1537 {
1538 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001539 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001540 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001541 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001542 }
1543
Gilles Peskine50e586b2018-06-08 14:28:46 +02001544exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001545 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001546 psa_destroy_key( key_slot );
1547 mbedtls_psa_crypto_free( );
1548}
1549/* END_CASE */
1550
Gilles Peskine50e586b2018-06-08 14:28:46 +02001551/* BEGIN_CASE */
1552void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001553 data_t *key,
1554 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02001555{
1556 int key_slot = 1;
1557 psa_key_type_t key_type = key_type_arg;
1558 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07001559 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02001560 size_t iv_size = 16;
1561 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001562 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001563 size_t output1_size = 0;
1564 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001565 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001566 size_t output2_size = 0;
1567 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001568 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001569 psa_cipher_operation_t operation1;
1570 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03001571 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001572
mohammad1603d7d7ba52018-03-12 18:51:53 +02001573 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001574 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001575 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1576 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001577
mohammad1603d7d7ba52018-03-12 18:51:53 +02001578 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1579
Moran Pekered346952018-07-05 15:22:45 +03001580 psa_key_policy_init( &policy );
1581 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
1582 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1583
mohammad1603d7d7ba52018-03-12 18:51:53 +02001584 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001585 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001586
Gilles Peskinefe119512018-07-08 21:39:34 +02001587 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
1588 key_slot, alg ) == PSA_SUCCESS );
1589 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
1590 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001591
Gilles Peskinefe119512018-07-08 21:39:34 +02001592 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
1593 iv, iv_size,
1594 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001595 output1_size = (size_t) input->len +
1596 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001597 output1 = mbedtls_calloc( 1, output1_size );
1598 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001599
Gilles Peskine4abf7412018-06-18 16:35:34 +02001600 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001601 output1, output1_size,
1602 &output1_length ) == PSA_SUCCESS );
1603 TEST_ASSERT( psa_cipher_finish( &operation1,
1604 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001605 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001606
Gilles Peskine048b7f02018-06-08 14:20:49 +02001607 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001608
1609 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1610
1611 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001612 output2 = mbedtls_calloc( 1, output2_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001613 TEST_ASSERT( output2 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001614
Gilles Peskinefe119512018-07-08 21:39:34 +02001615 TEST_ASSERT( psa_cipher_set_iv( &operation2,
1616 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001617 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
1618 output2, output2_size,
1619 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001620 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001621 TEST_ASSERT( psa_cipher_finish( &operation2,
1622 output2 + output2_length,
1623 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001624 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001625
Gilles Peskine048b7f02018-06-08 14:20:49 +02001626 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001627
Janos Follath25c4fa82018-07-06 16:23:25 +01001628 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001629
Gilles Peskine4abf7412018-06-18 16:35:34 +02001630 TEST_ASSERT( input->len == output2_length );
1631 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +03001632
1633exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001634 mbedtls_free( output1 );
1635 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03001636 psa_destroy_key( key_slot );
1637 mbedtls_psa_crypto_free( );
1638}
1639/* END_CASE */
1640
1641/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001642void cipher_verify_output_multipart( int alg_arg,
1643 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001644 data_t *key,
1645 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001646 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03001647{
1648 int key_slot = 1;
1649 psa_key_type_t key_type = key_type_arg;
1650 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03001651 unsigned char iv[16] = {0};
1652 size_t iv_size = 16;
1653 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001654 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001655 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001656 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001657 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001658 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001659 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001660 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001661 psa_cipher_operation_t operation1;
1662 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03001663 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03001664
Moran Pekerded84402018-06-06 16:36:50 +03001665 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001666 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001667 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1668 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001669
Moran Pekerded84402018-06-06 16:36:50 +03001670 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1671
Moran Pekered346952018-07-05 15:22:45 +03001672 psa_key_policy_init( &policy );
1673 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
1674 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1675
Moran Pekerded84402018-06-06 16:36:50 +03001676 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001677 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001678
Gilles Peskinefe119512018-07-08 21:39:34 +02001679 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
1680 key_slot, alg ) == PSA_SUCCESS );
1681 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
1682 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001683
Gilles Peskinefe119512018-07-08 21:39:34 +02001684 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
1685 iv, iv_size,
1686 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001687 output1_buffer_size = (size_t) input->len +
1688 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001689 output1 = mbedtls_calloc( 1, output1_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001690 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001691
Gilles Peskine4abf7412018-06-18 16:35:34 +02001692 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001693
itayzafrir3e02b3b2018-06-12 17:06:52 +03001694 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001695 output1, output1_buffer_size,
1696 &function_output_length ) == PSA_SUCCESS );
1697 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001698
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001699 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001700 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001701 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001702 output1, output1_buffer_size,
1703 &function_output_length ) == PSA_SUCCESS );
1704 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001705
Gilles Peskine048b7f02018-06-08 14:20:49 +02001706 TEST_ASSERT( psa_cipher_finish( &operation1,
1707 output1 + output1_length,
1708 output1_buffer_size - output1_length,
1709 &function_output_length ) == PSA_SUCCESS );
1710 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001711
1712 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1713
Gilles Peskine048b7f02018-06-08 14:20:49 +02001714 output2_buffer_size = output1_length;
1715 output2 = mbedtls_calloc( 1, output2_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001716 TEST_ASSERT( output2 != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001717
Gilles Peskinefe119512018-07-08 21:39:34 +02001718 TEST_ASSERT( psa_cipher_set_iv( &operation2,
1719 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001720
1721 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001722 output2, output2_buffer_size,
1723 &function_output_length ) == PSA_SUCCESS );
1724 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001725
Gilles Peskine048b7f02018-06-08 14:20:49 +02001726 TEST_ASSERT( psa_cipher_update( &operation2,
1727 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001728 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001729 output2, output2_buffer_size,
1730 &function_output_length ) == PSA_SUCCESS );
1731 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001732
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001733 TEST_ASSERT( psa_cipher_finish( &operation2,
1734 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001735 output2_buffer_size - output2_length,
1736 &function_output_length ) == PSA_SUCCESS );
1737 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001738
Janos Follath25c4fa82018-07-06 16:23:25 +01001739 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001740
Gilles Peskine4abf7412018-06-18 16:35:34 +02001741 TEST_ASSERT( input->len == output2_length );
1742 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001743
1744exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001745 mbedtls_free( output1 );
1746 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001747 psa_destroy_key( key_slot );
1748 mbedtls_psa_crypto_free( );
1749}
1750/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02001751
Gilles Peskine20035e32018-02-03 22:44:14 +01001752/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001753void aead_encrypt_decrypt( int key_type_arg,
1754 data_t * key_data,
1755 int alg_arg,
1756 data_t * input_data,
1757 data_t * nonce,
1758 data_t * additional_data,
1759 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001760{
1761 int slot = 1;
1762 psa_key_type_t key_type = key_type_arg;
1763 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001764 unsigned char *output_data = NULL;
1765 size_t output_size = 0;
1766 size_t output_length = 0;
1767 unsigned char *output_data2 = NULL;
1768 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001769 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001770 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001771 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001772
Gilles Peskinea1cac842018-06-11 19:33:02 +02001773 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001774 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001775 TEST_ASSERT( nonce != NULL );
1776 TEST_ASSERT( additional_data != NULL );
1777 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1778 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1779 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1780 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1781
Gilles Peskine4abf7412018-06-18 16:35:34 +02001782 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001783 output_data = mbedtls_calloc( 1, output_size );
1784 TEST_ASSERT( output_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001785
1786 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1787
1788 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001789 psa_key_policy_set_usage( &policy,
1790 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
1791 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001792 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1793
1794 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001795 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001796
1797 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001798 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001799 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001800 additional_data->len,
1801 input_data->x, input_data->len,
1802 output_data, output_size,
1803 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001804
1805 if( PSA_SUCCESS == expected_result )
1806 {
1807 output_data2 = mbedtls_calloc( 1, output_length );
1808 TEST_ASSERT( output_data2 != NULL );
1809
1810 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001811 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001812 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001813 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001814 output_data, output_length,
1815 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001816 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02001817
itayzafrir3e02b3b2018-06-12 17:06:52 +03001818 TEST_ASSERT( memcmp( input_data->x, output_data2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001819 input_data->len ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001820 }
Gilles Peskine2d277862018-06-18 15:41:12 +02001821
Gilles Peskinea1cac842018-06-11 19:33:02 +02001822exit:
1823 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001824 mbedtls_free( output_data );
1825 mbedtls_free( output_data2 );
1826 mbedtls_psa_crypto_free( );
1827}
1828/* END_CASE */
1829
1830/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001831void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001832 int alg_arg, data_t * input_data,
1833 data_t * additional_data, data_t * nonce,
1834 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001835{
1836 int slot = 1;
1837 psa_key_type_t key_type = key_type_arg;
1838 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001839 unsigned char *output_data = NULL;
1840 size_t output_size = 0;
1841 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001842 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001843 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001844
Gilles Peskinea1cac842018-06-11 19:33:02 +02001845 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001846 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001847 TEST_ASSERT( additional_data != NULL );
1848 TEST_ASSERT( nonce != NULL );
1849 TEST_ASSERT( expected_result != NULL );
1850 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1851 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1852 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1853 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1854 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
1855
Gilles Peskine4abf7412018-06-18 16:35:34 +02001856 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001857 output_data = mbedtls_calloc( 1, output_size );
1858 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001859
Gilles Peskinea1cac842018-06-11 19:33:02 +02001860 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1861
1862 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001863 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001864 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1865
1866 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001867 key_data->x,
1868 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001869
1870 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001871 nonce->x, nonce->len,
1872 additional_data->x, additional_data->len,
1873 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001874 output_data, output_size,
1875 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001876
itayzafrir3e02b3b2018-06-12 17:06:52 +03001877 TEST_ASSERT( memcmp( output_data, expected_result->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001878 output_length ) == 0 );
1879
Gilles Peskinea1cac842018-06-11 19:33:02 +02001880exit:
1881 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001882 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001883 mbedtls_psa_crypto_free( );
1884}
1885/* END_CASE */
1886
1887/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001888void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001889 int alg_arg, data_t * input_data,
1890 data_t * additional_data, data_t * nonce,
1891 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001892{
1893 int slot = 1;
1894 psa_key_type_t key_type = key_type_arg;
1895 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001896 unsigned char *output_data = NULL;
1897 size_t output_size = 0;
1898 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001899 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001900 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001901 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001902
Gilles Peskinea1cac842018-06-11 19:33:02 +02001903 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001904 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001905 TEST_ASSERT( additional_data != NULL );
1906 TEST_ASSERT( nonce != NULL );
1907 TEST_ASSERT( expected_data != NULL );
1908 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1909 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1910 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1911 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1912 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
1913
Gilles Peskine4abf7412018-06-18 16:35:34 +02001914 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001915 output_data = mbedtls_calloc( 1, output_size );
1916 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001917
Gilles Peskinea1cac842018-06-11 19:33:02 +02001918 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1919
1920 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001921 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001922 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1923
1924 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001925 key_data->x,
1926 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001927
1928 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001929 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001930 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001931 additional_data->len,
1932 input_data->x, input_data->len,
1933 output_data, output_size,
1934 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001935
Gilles Peskine2d277862018-06-18 15:41:12 +02001936 if( expected_result == PSA_SUCCESS )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001937 {
itayzafrir3e02b3b2018-06-12 17:06:52 +03001938 TEST_ASSERT( memcmp( output_data, expected_data->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001939 output_length ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001940 }
1941
Gilles Peskinea1cac842018-06-11 19:33:02 +02001942exit:
1943 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001944 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001945 mbedtls_psa_crypto_free( );
1946}
1947/* END_CASE */
1948
1949/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001950void signature_size( int type_arg,
1951 int bits,
1952 int alg_arg,
1953 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001954{
1955 psa_key_type_t type = type_arg;
1956 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02001957 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01001958 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
1959exit:
1960 ;
1961}
1962/* END_CASE */
1963
1964/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001965void sign_deterministic( int key_type_arg, data_t *key_data,
1966 int alg_arg, data_t *input_data,
1967 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001968{
1969 int slot = 1;
1970 psa_key_type_t key_type = key_type_arg;
1971 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01001972 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01001973 unsigned char *signature = NULL;
1974 size_t signature_size;
1975 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02001976 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01001977
Gilles Peskine20035e32018-02-03 22:44:14 +01001978 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001979 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001980 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001981 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1982 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1983 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01001984
1985 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1986
mohammad1603a97cb8c2018-03-28 03:46:26 -07001987 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001988 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001989 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1990
Gilles Peskine20035e32018-02-03 22:44:14 +01001991 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001992 key_data->x,
1993 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01001994 TEST_ASSERT( psa_get_key_information( slot,
1995 NULL,
1996 &key_bits ) == PSA_SUCCESS );
1997
Gilles Peskine860ce9d2018-06-28 12:23:00 +02001998 /* Allocate a buffer which has the size advertized by the
1999 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002000 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2001 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002002 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002003 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine20035e32018-02-03 22:44:14 +01002004 signature = mbedtls_calloc( 1, signature_size );
2005 TEST_ASSERT( signature != NULL );
2006
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002007 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002008 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002009 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002010 signature, signature_size,
2011 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002012 /* Verify that the signature is what is expected. */
Gilles Peskine4abf7412018-06-18 16:35:34 +02002013 TEST_ASSERT( signature_length == output_data->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02002014 TEST_ASSERT( memcmp( signature, output_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002015 output_data->len ) == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01002016
2017exit:
2018 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002019 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002020 mbedtls_psa_crypto_free( );
2021}
2022/* END_CASE */
2023
2024/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002025void sign_fail( int key_type_arg, data_t *key_data,
2026 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002027 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002028{
2029 int slot = 1;
2030 psa_key_type_t key_type = key_type_arg;
2031 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002032 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002033 psa_status_t actual_status;
2034 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002035 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002036 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002037 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002038
Gilles Peskine20035e32018-02-03 22:44:14 +01002039 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002040 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002041 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2042 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2043
Gilles Peskine20035e32018-02-03 22:44:14 +01002044 signature = mbedtls_calloc( 1, signature_size );
2045 TEST_ASSERT( signature != NULL );
2046
2047 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2048
mohammad1603a97cb8c2018-03-28 03:46:26 -07002049 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002050 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002051 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2052
Gilles Peskine20035e32018-02-03 22:44:14 +01002053 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002054 key_data->x,
2055 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002056
2057 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002058 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002059 signature, signature_size,
2060 &signature_length );
2061 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002062 /* The value of *signature_length is unspecified on error, but
2063 * whatever it is, it should be less than signature_size, so that
2064 * if the caller tries to read *signature_length bytes without
2065 * checking the error code then they don't overflow a buffer. */
2066 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002067
2068exit:
2069 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002070 mbedtls_free( signature );
2071 mbedtls_psa_crypto_free( );
2072}
2073/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002074
2075/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002076void sign_verify( int key_type_arg, data_t *key_data,
2077 int alg_arg, data_t *input_data )
2078{
2079 int slot = 1;
2080 psa_key_type_t key_type = key_type_arg;
2081 psa_algorithm_t alg = alg_arg;
2082 size_t key_bits;
2083 unsigned char *signature = NULL;
2084 size_t signature_size;
2085 size_t signature_length = 0xdeadbeef;
2086 psa_key_policy_t policy;
2087
2088 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2089
2090 psa_key_policy_init( &policy );
2091 psa_key_policy_set_usage( &policy,
2092 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2093 alg );
2094 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2095
2096 TEST_ASSERT( psa_import_key( slot, key_type,
2097 key_data->x,
2098 key_data->len ) == PSA_SUCCESS );
2099 TEST_ASSERT( psa_get_key_information( slot,
2100 NULL,
2101 &key_bits ) == PSA_SUCCESS );
2102
2103 /* Allocate a buffer which has the size advertized by the
2104 * library. */
2105 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2106 key_bits, alg );
2107 TEST_ASSERT( signature_size != 0 );
2108 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2109 signature = mbedtls_calloc( 1, signature_size );
2110 TEST_ASSERT( signature != NULL );
2111
2112 /* Perform the signature. */
2113 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2114 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002115 signature, signature_size,
2116 &signature_length ) == PSA_SUCCESS );
2117 /* Check that the signature length looks sensible. */
2118 TEST_ASSERT( signature_length <= signature_size );
2119 TEST_ASSERT( signature_length > 0 );
2120
2121 /* Use the library to verify that the signature is correct. */
2122 TEST_ASSERT( psa_asymmetric_verify(
2123 slot, alg,
2124 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002125 signature, signature_length ) == PSA_SUCCESS );
2126
2127 if( input_data->len != 0 )
2128 {
2129 /* Flip a bit in the input and verify that the signature is now
2130 * detected as invalid. Flip a bit at the beginning, not at the end,
2131 * because ECDSA may ignore the last few bits of the input. */
2132 input_data->x[0] ^= 1;
2133 TEST_ASSERT( psa_asymmetric_verify(
2134 slot, alg,
2135 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002136 signature,
2137 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2138 }
2139
2140exit:
2141 psa_destroy_key( slot );
2142 mbedtls_free( signature );
2143 mbedtls_psa_crypto_free( );
2144}
2145/* END_CASE */
2146
2147/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002148void asymmetric_verify( int key_type_arg, data_t *key_data,
2149 int alg_arg, data_t *hash_data,
2150 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002151{
2152 int slot = 1;
2153 psa_key_type_t key_type = key_type_arg;
2154 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002155 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002156
Gilles Peskine69c12672018-06-28 00:07:19 +02002157 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2158
itayzafrir5c753392018-05-08 11:18:38 +03002159 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002160 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002161 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002162 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2163 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2164 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002165
2166 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2167
2168 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002169 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002170 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2171
2172 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002173 key_data->x,
2174 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002175
2176 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002177 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002178 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002179 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002180exit:
2181 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002182 mbedtls_psa_crypto_free( );
2183}
2184/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002185
2186/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002187void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2188 int alg_arg, data_t *hash_data,
2189 data_t *signature_data,
2190 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002191{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002192 int slot = 1;
2193 psa_key_type_t key_type = key_type_arg;
2194 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002195 psa_status_t actual_status;
2196 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002197 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002198
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002199 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002200 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002201 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002202 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2203 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2204 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002205
2206 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2207
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002208 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002209 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002210 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2211
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002212 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002213 key_data->x,
2214 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002215
2216 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002217 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002218 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002219 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002220
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002221 TEST_ASSERT( actual_status == expected_status );
2222
2223exit:
2224 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002225 mbedtls_psa_crypto_free( );
2226}
2227/* END_CASE */
2228
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002229/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002230void asymmetric_encrypt( int key_type_arg,
2231 data_t *key_data,
2232 int alg_arg,
2233 data_t *input_data,
2234 int expected_output_length_arg,
2235 int expected_status_arg )
2236{
2237 int slot = 1;
2238 psa_key_type_t key_type = key_type_arg;
2239 psa_algorithm_t alg = alg_arg;
2240 size_t expected_output_length = expected_output_length_arg;
2241 size_t key_bits;
2242 unsigned char *output = NULL;
2243 size_t output_size;
2244 size_t output_length = ~0;
2245 psa_status_t actual_status;
2246 psa_status_t expected_status = expected_status_arg;
2247 psa_key_policy_t policy;
2248
2249 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2250
2251 /* Import the key */
2252 psa_key_policy_init( &policy );
2253 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2254 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2255 TEST_ASSERT( psa_import_key( slot, key_type,
2256 key_data->x,
2257 key_data->len ) == PSA_SUCCESS );
2258
2259 /* Determine the maximum output length */
2260 TEST_ASSERT( psa_get_key_information( slot,
2261 NULL,
2262 &key_bits ) == PSA_SUCCESS );
2263 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
2264 output = mbedtls_calloc( 1, output_size );
2265 TEST_ASSERT( output != NULL );
2266
2267 /* Encrypt the input */
2268 actual_status = psa_asymmetric_encrypt( slot, alg,
2269 input_data->x, input_data->len,
2270 NULL, 0,
2271 output, output_size,
2272 &output_length );
2273 TEST_ASSERT( actual_status == expected_status );
2274 TEST_ASSERT( output_length == expected_output_length );
2275
2276exit:
2277 psa_destroy_key( slot );
2278 mbedtls_free( output );
2279 mbedtls_psa_crypto_free( );
2280}
2281/* END_CASE */
2282
2283/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002284void asymmetric_encrypt_decrypt( int key_type_arg, data_t *key_data,
2285 int alg_arg, data_t *input_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002286{
2287 int slot = 1;
2288 psa_key_type_t key_type = key_type_arg;
2289 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002290 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002291 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002292 size_t output_length = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002293 unsigned char *output2 = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002294 size_t output2_size = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002295 size_t output2_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002296 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002297
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002298 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002299 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002300 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2301 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2302
Gilles Peskine4abf7412018-06-18 16:35:34 +02002303 output_size = key_data->len;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002304 output2_size = output_size;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002305 output = mbedtls_calloc( 1, output_size );
2306 TEST_ASSERT( output != NULL );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002307 output2 = mbedtls_calloc( 1, output2_size );
2308 TEST_ASSERT( output2 != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002309
2310 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2311
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002312 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002313 psa_key_policy_set_usage( &policy,
2314 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002315 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002316 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2317
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002318 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002319 key_data->x,
2320 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002321
Gilles Peskineeebd7382018-06-08 18:11:54 +02002322 /* We test encryption by checking that encrypt-then-decrypt gives back
2323 * the original plaintext because of the non-optional random
2324 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02002325 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002326 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002327 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002328 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002329 &output_length ) == PSA_SUCCESS );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002330
Gilles Peskine2d277862018-06-18 15:41:12 +02002331 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002332 output, output_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02002333 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002334 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002335 &output2_length ) == PSA_SUCCESS );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002336 TEST_ASSERT( memcmp( input_data->x, output2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002337 input_data->len ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002338
2339exit:
2340 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002341 mbedtls_free( output );
2342 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002343 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002344}
2345/* END_CASE */
2346
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002347/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002348void asymmetric_decrypt( int key_type_arg, data_t *key_data,
2349 int alg_arg, data_t *input_data,
Gilles Peskine66763a02018-06-29 21:54:10 +02002350 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002351{
2352 int slot = 1;
2353 psa_key_type_t key_type = key_type_arg;
2354 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002355 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002356 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002357 size_t output_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002358 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002359
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002360 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002361 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002362 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002363 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2364 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2365 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2366
Gilles Peskine4abf7412018-06-18 16:35:34 +02002367 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002368 output = mbedtls_calloc( 1, output_size );
2369 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002370
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002371 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2372
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002373 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002374 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002375 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2376
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002377 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002378 key_data->x,
2379 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002380
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002381 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002382 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002383 NULL, 0,
2384 output,
2385 output_size,
2386 &output_length ) == PSA_SUCCESS );
Gilles Peskine66763a02018-06-29 21:54:10 +02002387 TEST_ASSERT( expected_data->len == output_length );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002388 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002389
2390exit:
2391 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002392 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002393 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002394}
2395/* END_CASE */
2396
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002397/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002398void asymmetric_decrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002399 int alg_arg, data_t *input_data,
2400 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002401{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002402 int slot = 1;
2403 psa_key_type_t key_type = key_type_arg;
2404 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002405 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002406 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002407 size_t output_length = 0;
2408 psa_status_t actual_status;
2409 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002410 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002411
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002412 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002413 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002414 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2415 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2416
Gilles Peskine4abf7412018-06-18 16:35:34 +02002417 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002418 output = mbedtls_calloc( 1, output_size );
2419 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002420
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002421 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2422
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002423 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002424 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002425 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2426
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002427 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002428 key_data->x,
2429 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002430
Gilles Peskine2d277862018-06-18 15:41:12 +02002431 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002432 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002433 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002434 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002435 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002436 TEST_ASSERT( actual_status == expected_status );
2437
2438exit:
2439 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02002440 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002441 mbedtls_psa_crypto_free( );
2442}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002443/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02002444
2445/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02002446void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02002447{
Gilles Peskinea50d7392018-06-21 10:22:13 +02002448 size_t bytes = bytes_arg;
2449 const unsigned char trail[] = "don't overwrite me";
2450 unsigned char *output = mbedtls_calloc( 1, bytes + sizeof( trail ) );
2451 unsigned char *changed = mbedtls_calloc( 1, bytes );
2452 size_t i;
2453 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02002454
Gilles Peskinea50d7392018-06-21 10:22:13 +02002455 TEST_ASSERT( output != NULL );
2456 TEST_ASSERT( changed != NULL );
2457 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02002458
2459 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2460
Gilles Peskinea50d7392018-06-21 10:22:13 +02002461 /* Run several times, to ensure that every output byte will be
2462 * nonzero at least once with overwhelming probability
2463 * (2^(-8*number_of_runs)). */
2464 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02002465 {
Gilles Peskinea50d7392018-06-21 10:22:13 +02002466 memset( output, 0, bytes );
2467 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
2468
2469 /* Check that no more than bytes have been overwritten */
2470 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
2471
2472 for( i = 0; i < bytes; i++ )
2473 {
2474 if( output[i] != 0 )
2475 ++changed[i];
2476 }
Gilles Peskine05d69892018-06-19 22:00:52 +02002477 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02002478
2479 /* Check that every byte was changed to nonzero at least once. This
2480 * validates that psa_generate_random is overwriting every byte of
2481 * the output buffer. */
2482 for( i = 0; i < bytes; i++ )
2483 {
2484 TEST_ASSERT( changed[i] != 0 );
2485 }
Gilles Peskine05d69892018-06-19 22:00:52 +02002486
2487exit:
2488 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02002489 mbedtls_free( output );
2490 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02002491}
2492/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02002493
2494/* BEGIN_CASE */
2495void generate_key( int type_arg,
2496 int bits_arg,
2497 int usage_arg,
2498 int alg_arg,
2499 int expected_status_arg )
2500{
2501 int slot = 1;
2502 psa_key_type_t type = type_arg;
2503 psa_key_usage_t usage = usage_arg;
2504 size_t bits = bits_arg;
2505 psa_algorithm_t alg = alg_arg;
2506 psa_status_t expected_status = expected_status_arg;
2507 psa_key_type_t got_type;
2508 size_t got_bits;
2509 unsigned char exported[616] = {0}; /* enough for a 1024-bit RSA key */
2510 size_t exported_length;
2511 psa_status_t expected_export_status =
2512 usage & PSA_KEY_USAGE_EXPORT ? PSA_SUCCESS : PSA_ERROR_NOT_PERMITTED;
2513 psa_status_t expected_info_status =
2514 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
2515 psa_key_policy_t policy;
2516
2517 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2518
2519 psa_key_policy_init( &policy );
2520 psa_key_policy_set_usage( &policy, usage, alg );
2521 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2522
2523 /* Generate a key */
2524 TEST_ASSERT( psa_generate_key( slot, type, bits,
2525 NULL, 0 ) == expected_status );
2526
2527 /* Test the key information */
2528 TEST_ASSERT( psa_get_key_information( slot,
2529 &got_type,
2530 &got_bits ) == expected_info_status );
2531 if( expected_info_status != PSA_SUCCESS )
2532 goto exit;
2533 TEST_ASSERT( got_type == type );
2534 TEST_ASSERT( got_bits == bits );
2535
2536 /* Export the key */
2537 TEST_ASSERT( psa_export_key( slot,
2538 exported, sizeof( exported ),
2539 &exported_length ) == expected_export_status );
2540 if( expected_export_status == PSA_SUCCESS )
2541 {
Gilles Peskine48c0ea12018-06-21 14:15:31 +02002542 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02002543 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
2544#if defined(MBEDTLS_DES_C)
2545 if( type == PSA_KEY_TYPE_DES )
2546 {
2547 /* Check the parity bits. */
2548 unsigned i;
2549 for( i = 0; i < bits / 8; i++ )
2550 {
2551 unsigned bit_count = 0;
2552 unsigned m;
2553 for( m = 1; m <= 0x100; m <<= 1 )
2554 {
2555 if( exported[i] & m )
2556 ++bit_count;
2557 }
2558 TEST_ASSERT( bit_count % 2 != 0 );
2559 }
2560 }
2561#endif
2562#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
2563 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
2564 {
2565 /* Sanity check: does this look like the beginning of a PKCS#8
2566 * RSA key pair? Assumes bits is a multiple of 8. */
2567 size_t n_bytes = bits / 8 + 1;
2568 size_t n_encoded_bytes;
2569 unsigned char *n_end;
2570 TEST_ASSERT( exported_length >= 7 + ( n_bytes + 3 ) * 9 / 2 );
2571 TEST_ASSERT( exported[0] == 0x30 );
2572 TEST_ASSERT( exported[1] == 0x82 ); // assumes >=416-bit key
2573 TEST_ASSERT( exported[4] == 0x02 );
2574 TEST_ASSERT( exported[5] == 0x01 );
2575 TEST_ASSERT( exported[6] == 0x00 );
2576 TEST_ASSERT( exported[7] == 0x02 );
2577 n_encoded_bytes = exported[8];
2578 n_end = exported + 9 + n_encoded_bytes;
2579 if( n_encoded_bytes & 0x80 )
2580 {
2581 n_encoded_bytes = ( n_encoded_bytes & 0x7f ) << 7;
2582 n_encoded_bytes |= exported[9] & 0x7f;
2583 n_end += 1;
2584 }
2585 /* The encoding of n should start with a 0 byte since it should
2586 * have its high bit set. However Mbed TLS is not compliant and
2587 * generates an invalid, but widely tolerated, encoding of
2588 * positive INTEGERs with a bit size that is a multiple of 8
2589 * with no leading 0 byte. Accept this here. */
2590 TEST_ASSERT( n_bytes == n_encoded_bytes ||
2591 n_bytes == n_encoded_bytes + 1 );
2592 if( n_bytes == n_encoded_bytes )
2593 TEST_ASSERT( exported[n_encoded_bytes <= 127 ? 9 : 10] == 0x00 );
2594 /* Sanity check: e must be 3 */
2595 TEST_ASSERT( n_end[0] == 0x02 );
2596 TEST_ASSERT( n_end[1] == 0x03 );
2597 TEST_ASSERT( n_end[2] == 0x01 );
2598 TEST_ASSERT( n_end[3] == 0x00 );
2599 TEST_ASSERT( n_end[4] == 0x01 );
2600 TEST_ASSERT( n_end[5] == 0x02 );
2601 }
2602#endif /* MBEDTLS_RSA_C */
2603#if defined(MBEDTLS_ECP_C)
2604 if( PSA_KEY_TYPE_IS_ECC( type ) )
2605 {
2606 /* Sanity check: does this look like the beginning of a PKCS#8
2607 * elliptic curve key pair? */
2608 TEST_ASSERT( exported_length >= bits * 3 / 8 + 10 );
2609 TEST_ASSERT( exported[0] == 0x30 );
2610 }
2611#endif /* MBEDTLS_ECP_C */
2612 }
2613
Gilles Peskine818ca122018-06-20 18:16:48 +02002614 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02002615 if( ! exercise_key( slot, usage, alg ) )
2616 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02002617
2618exit:
2619 psa_destroy_key( slot );
2620 mbedtls_psa_crypto_free( );
2621}
2622/* END_CASE */