blob: dbe306e047022af73d7cf0c7549a538e3a8401be [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 Peskinef969b3a2018-06-30 00:20:25 +0200288 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
289 size_t payload_length = 16;
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 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200295 /* Some algorithms require the payload to have the size of
296 * the hash encoded in the algorithm. Use this input size
297 * even for algorithms that allow other input sizes. */
298 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
299 if( hash_alg != 0 )
300 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200301 TEST_ASSERT( psa_asymmetric_sign( key, alg,
302 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200303 signature, sizeof( signature ),
304 &signature_length ) == PSA_SUCCESS );
305 }
306
307 if( usage & PSA_KEY_USAGE_VERIFY )
308 {
309 psa_status_t verify_status =
310 ( usage & PSA_KEY_USAGE_SIGN ?
311 PSA_SUCCESS :
312 PSA_ERROR_INVALID_SIGNATURE );
313 TEST_ASSERT( psa_asymmetric_verify( key, alg,
314 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200315 signature, signature_length ) ==
316 verify_status );
317 }
318
319 return( 1 );
320
321exit:
322 return( 0 );
323}
324
325static int exercise_asymmetric_encryption_key( psa_key_slot_t key,
326 psa_key_usage_t usage,
327 psa_algorithm_t alg )
328{
329 unsigned char plaintext[256] = "Hello, world...";
330 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
331 size_t ciphertext_length = sizeof( ciphertext );
332 size_t plaintext_length = 16;
333
334 if( usage & PSA_KEY_USAGE_ENCRYPT )
335 {
336 TEST_ASSERT(
337 psa_asymmetric_encrypt( key, alg,
338 plaintext, plaintext_length,
339 NULL, 0,
340 ciphertext, sizeof( ciphertext ),
341 &ciphertext_length ) == PSA_SUCCESS );
342 }
343
344 if( usage & PSA_KEY_USAGE_DECRYPT )
345 {
346 psa_status_t status =
347 psa_asymmetric_decrypt( key, alg,
348 ciphertext, ciphertext_length,
349 NULL, 0,
350 plaintext, sizeof( plaintext ),
351 &plaintext_length );
352 TEST_ASSERT( status == PSA_SUCCESS ||
353 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
354 ( status == PSA_ERROR_INVALID_ARGUMENT ||
355 status == PSA_ERROR_INVALID_PADDING ) ) );
356 }
357
358 return( 1 );
359
360exit:
361 return( 0 );
362}
Gilles Peskine02b75072018-07-01 22:31:34 +0200363
364static int exercise_key( psa_key_slot_t slot,
365 psa_key_usage_t usage,
366 psa_algorithm_t alg )
367{
368 int ok;
369 if( alg == 0 )
370 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
371 else if( PSA_ALG_IS_MAC( alg ) )
372 ok = exercise_mac_key( slot, usage, alg );
373 else if( PSA_ALG_IS_CIPHER( alg ) )
374 ok = exercise_cipher_key( slot, usage, alg );
375 else if( PSA_ALG_IS_AEAD( alg ) )
376 ok = exercise_aead_key( slot, usage, alg );
377 else if( PSA_ALG_IS_SIGN( alg ) )
378 ok = exercise_signature_key( slot, usage, alg );
379 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
380 ok = exercise_asymmetric_encryption_key( slot, usage, alg );
381 else
382 {
383 char message[40];
384 mbedtls_snprintf( message, sizeof( message ),
385 "No code to exercise alg=0x%08lx",
386 (unsigned long) alg );
387 test_fail( message, __LINE__, __FILE__ );
388 ok = 0;
389 }
390 return( ok );
391}
392
Gilles Peskinee59236f2018-01-27 23:32:46 +0100393/* END_HEADER */
394
395/* BEGIN_DEPENDENCIES
396 * depends_on:MBEDTLS_PSA_CRYPTO_C
397 * END_DEPENDENCIES
398 */
399
400/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200401void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100402{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100403 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100404 int i;
405 for( i = 0; i <= 1; i++ )
406 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100407 status = psa_crypto_init( );
408 TEST_ASSERT( status == PSA_SUCCESS );
409 status = psa_crypto_init( );
410 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100411 mbedtls_psa_crypto_free( );
412 }
413}
414/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100415
416/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200417void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100418{
419 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200420 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100421 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100422
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100423 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300424 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100425 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
426
Gilles Peskine4abf7412018-06-18 16:35:34 +0200427 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200428 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100429 if( status == PSA_SUCCESS )
430 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
431
432exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100433 mbedtls_psa_crypto_free( );
434}
435/* END_CASE */
436
437/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200438void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
439{
440 int slot = 1;
441 size_t bits = bits_arg;
442 psa_status_t expected_status = expected_status_arg;
443 psa_status_t status;
444 psa_key_type_t type =
445 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
446 size_t buffer_size = /* Slight overapproximations */
447 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
448 unsigned char *buffer = mbedtls_calloc( 1, buffer_size );
449 unsigned char *p;
450 int ret;
451 size_t length;
452
453 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
454 TEST_ASSERT( buffer != NULL );
455
456 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
457 bits, keypair ) ) >= 0 );
458 length = ret;
459
460 /* Try importing the key */
461 status = psa_import_key( slot, type, p, length );
462 TEST_ASSERT( status == expected_status );
463 if( status == PSA_SUCCESS )
464 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
465
466exit:
467 mbedtls_free( buffer );
468 mbedtls_psa_crypto_free( );
469}
470/* END_CASE */
471
472/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300473void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300474 int type_arg,
475 int alg_arg,
476 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100477 int expected_bits,
478 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200479 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100480 int canonical_input )
481{
482 int slot = 1;
483 int slot2 = slot + 1;
484 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200485 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200486 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100487 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100488 unsigned char *exported = NULL;
489 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100490 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100491 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100492 size_t reexported_length;
493 psa_key_type_t got_type;
494 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200495 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100496
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100497 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300498 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
499 export_size = (ssize_t) data->len + export_size_delta;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100500 exported = mbedtls_calloc( 1, export_size );
501 TEST_ASSERT( exported != NULL );
502 if( ! canonical_input )
503 {
504 reexported = mbedtls_calloc( 1, export_size );
505 TEST_ASSERT( reexported != NULL );
506 }
507 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
508
mohammad1603a97cb8c2018-03-28 03:46:26 -0700509 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200510 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700511 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
512
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100513 /* Import the key */
514 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200515 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100516
517 /* Test the key information */
518 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200519 &got_type,
520 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100521 TEST_ASSERT( got_type == type );
522 TEST_ASSERT( got_bits == (size_t) expected_bits );
523
524 /* Export the key */
525 status = psa_export_key( slot,
526 exported, export_size,
527 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200528 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100529
530 /* The exported length must be set by psa_export_key() to a value between 0
531 * and export_size. On errors, the exported length must be 0. */
532 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
533 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
534 TEST_ASSERT( exported_length <= export_size );
535
Gilles Peskine3f669c32018-06-21 09:21:51 +0200536 TEST_ASSERT( mem_is_zero( exported + exported_length,
537 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100538 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200539 {
540 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100541 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200542 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100543
544 if( canonical_input )
545 {
Gilles Peskine4abf7412018-06-18 16:35:34 +0200546 TEST_ASSERT( exported_length == data->len );
547 TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100548 }
549 else
550 {
mohammad1603a97cb8c2018-03-28 03:46:26 -0700551 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
552
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100553 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200554 exported,
555 export_size ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100556 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200557 reexported,
558 export_size,
559 &reexported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100560 TEST_ASSERT( reexported_length == exported_length );
561 TEST_ASSERT( memcmp( reexported, exported,
562 exported_length ) == 0 );
563 }
564
565destroy:
566 /* Destroy the key */
567 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
568 TEST_ASSERT( psa_get_key_information(
569 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
570
571exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300572 mbedtls_free( exported );
573 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100574 mbedtls_psa_crypto_free( );
575}
576/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100577
Moran Pekerf709f4a2018-06-06 17:26:04 +0300578/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300579void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200580 int type_arg,
581 int alg_arg,
582 int expected_bits,
583 int public_key_expected_length,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200584 int expected_export_status_arg )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300585{
586 int slot = 1;
587 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200588 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200589 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300590 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300591 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300592 size_t export_size;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100593 size_t exported_length = INVALID_EXPORT_LENGTH;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300594 psa_key_type_t got_type;
595 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200596 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300597
Moran Pekerf709f4a2018-06-06 17:26:04 +0300598 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300599 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
600 export_size = (ssize_t) data->len;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300601 exported = mbedtls_calloc( 1, export_size );
602 TEST_ASSERT( exported != NULL );
603
604 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
605
606 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200607 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300608 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
609
610 /* Import the key */
611 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200612 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300613
614 /* Test the key information */
615 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200616 &got_type,
617 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300618 TEST_ASSERT( got_type == type );
619 TEST_ASSERT( got_bits == (size_t) expected_bits );
620
621 /* Export the key */
622 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200623 exported, export_size,
624 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200625 TEST_ASSERT( status == expected_export_status );
Jaeden Amero2a671e92018-06-27 17:47:40 +0100626 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
627 TEST_ASSERT( mem_is_zero( exported + exported_length,
628 export_size - exported_length ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300629 if( status != PSA_SUCCESS )
630 goto destroy;
631
Moran Pekerf709f4a2018-06-06 17:26:04 +0300632destroy:
633 /* Destroy the key */
634 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
635 TEST_ASSERT( psa_get_key_information(
636 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
637
638exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300639 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300640 mbedtls_psa_crypto_free( );
641}
642/* END_CASE */
643
Gilles Peskine20035e32018-02-03 22:44:14 +0100644/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200645void import_and_exercise_key( data_t *data,
646 int type_arg,
647 int bits_arg,
648 int alg_arg )
649{
650 int slot = 1;
651 psa_key_type_t type = type_arg;
652 size_t bits = bits_arg;
653 psa_algorithm_t alg = alg_arg;
654 psa_key_usage_t usage =
655 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
656 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
657 PSA_KEY_USAGE_VERIFY :
658 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
659 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
660 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
661 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
662 PSA_KEY_USAGE_ENCRYPT :
663 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
664 0 );
665 psa_key_policy_t policy;
666 psa_key_type_t got_type;
667 size_t got_bits;
668 psa_status_t status;
669
670 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
671
672 psa_key_policy_init( &policy );
673 psa_key_policy_set_usage( &policy, usage, alg );
674 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
675
676 /* Import the key */
677 status = psa_import_key( slot, type, data->x, data->len );
678 TEST_ASSERT( status == PSA_SUCCESS );
679
680 /* Test the key information */
681 TEST_ASSERT( psa_get_key_information( slot,
682 &got_type,
683 &got_bits ) == PSA_SUCCESS );
684 TEST_ASSERT( got_type == type );
685 TEST_ASSERT( got_bits == bits );
686
687 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +0200688 if( ! exercise_key( slot, usage, alg ) )
689 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200690
691exit:
692 psa_destroy_key( slot );
693 mbedtls_psa_crypto_free( );
694}
695/* END_CASE */
696
697/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +0200698void key_policy( int usage_arg, int alg_arg )
699{
700 int key_slot = 1;
701 psa_algorithm_t alg = alg_arg;
702 psa_key_usage_t usage = usage_arg;
703 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
704 unsigned char key[32] = {0};
705 psa_key_policy_t policy_set;
706 psa_key_policy_t policy_get;
707
708 memset( key, 0x2a, sizeof( key ) );
709
710 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
711
712 psa_key_policy_init( &policy_set );
713 psa_key_policy_init( &policy_get );
714
715 psa_key_policy_set_usage( &policy_set, usage, alg );
716
717 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
718 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
719 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
720
721 TEST_ASSERT( psa_import_key( key_slot, key_type,
722 key, sizeof( key ) ) == PSA_SUCCESS );
723
724 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
725
726 TEST_ASSERT( policy_get.usage == policy_set.usage );
727 TEST_ASSERT( policy_get.alg == policy_set.alg );
728
729exit:
730 psa_destroy_key( key_slot );
731 mbedtls_psa_crypto_free( );
732}
733/* END_CASE */
734
735/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200736void mac_key_policy( int policy_usage,
737 int policy_alg,
738 int key_type,
739 data_t *key_data,
740 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200741{
742 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +0200743 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200744 psa_mac_operation_t operation;
745 psa_status_t status;
746 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200747
748 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
749
750 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200751 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200752 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
753
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200754 TEST_ASSERT( psa_import_key( key_slot, key_type,
755 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +0200756
Gilles Peskine89167cb2018-07-08 20:12:23 +0200757 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200758 if( policy_alg == exercise_alg &&
759 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
760 TEST_ASSERT( status == PSA_SUCCESS );
761 else
762 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
763 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200764
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200765 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +0200766 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200767 if( policy_alg == exercise_alg &&
768 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +0200769 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200770 else
771 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
772
773exit:
774 psa_mac_abort( &operation );
775 psa_destroy_key( key_slot );
776 mbedtls_psa_crypto_free( );
777}
778/* END_CASE */
779
780/* BEGIN_CASE */
781void cipher_key_policy( int policy_usage,
782 int policy_alg,
783 int key_type,
784 data_t *key_data,
785 int exercise_alg )
786{
787 int key_slot = 1;
788 psa_key_policy_t policy;
789 psa_cipher_operation_t operation;
790 psa_status_t status;
791
792 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
793
794 psa_key_policy_init( &policy );
795 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
796 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
797
798 TEST_ASSERT( psa_import_key( key_slot, key_type,
799 key_data->x, key_data->len ) == PSA_SUCCESS );
800
Gilles Peskinefe119512018-07-08 21:39:34 +0200801 status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200802 if( policy_alg == exercise_alg &&
803 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
804 TEST_ASSERT( status == PSA_SUCCESS );
805 else
806 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
807 psa_cipher_abort( &operation );
808
Gilles Peskinefe119512018-07-08 21:39:34 +0200809 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200810 if( policy_alg == exercise_alg &&
811 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
812 TEST_ASSERT( status == PSA_SUCCESS );
813 else
814 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
815
816exit:
817 psa_cipher_abort( &operation );
818 psa_destroy_key( key_slot );
819 mbedtls_psa_crypto_free( );
820}
821/* END_CASE */
822
823/* BEGIN_CASE */
824void aead_key_policy( int policy_usage,
825 int policy_alg,
826 int key_type,
827 data_t *key_data,
828 int nonce_length_arg,
829 int tag_length_arg,
830 int exercise_alg )
831{
832 int key_slot = 1;
833 psa_key_policy_t policy;
834 psa_status_t status;
835 unsigned char nonce[16] = {0};
836 size_t nonce_length = nonce_length_arg;
837 unsigned char tag[16];
838 size_t tag_length = tag_length_arg;
839 size_t output_length;
840
841 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
842 TEST_ASSERT( tag_length <= sizeof( tag ) );
843
844 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
845
846 psa_key_policy_init( &policy );
847 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
848 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
849
850 TEST_ASSERT( psa_import_key( key_slot, key_type,
851 key_data->x, key_data->len ) == PSA_SUCCESS );
852
853 status = psa_aead_encrypt( key_slot, exercise_alg,
854 nonce, nonce_length,
855 NULL, 0,
856 NULL, 0,
857 tag, tag_length,
858 &output_length );
859 if( policy_alg == exercise_alg &&
860 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
861 TEST_ASSERT( status == PSA_SUCCESS );
862 else
863 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
864
865 memset( tag, 0, sizeof( tag ) );
866 status = psa_aead_decrypt( key_slot, exercise_alg,
867 nonce, nonce_length,
868 NULL, 0,
869 tag, tag_length,
870 NULL, 0,
871 &output_length );
872 if( policy_alg == exercise_alg &&
873 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
874 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
875 else
876 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
877
878exit:
879 psa_destroy_key( key_slot );
880 mbedtls_psa_crypto_free( );
881}
882/* END_CASE */
883
884/* BEGIN_CASE */
885void asymmetric_encryption_key_policy( int policy_usage,
886 int policy_alg,
887 int key_type,
888 data_t *key_data,
889 int exercise_alg )
890{
891 int key_slot = 1;
892 psa_key_policy_t policy;
893 psa_status_t status;
894 size_t key_bits;
895 size_t buffer_length;
896 unsigned char *buffer = NULL;
897 size_t output_length;
898
899 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
900
901 psa_key_policy_init( &policy );
902 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
903 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
904
905 TEST_ASSERT( psa_import_key( key_slot, key_type,
906 key_data->x, key_data->len ) == PSA_SUCCESS );
907
908 TEST_ASSERT( psa_get_key_information( key_slot,
909 NULL,
910 &key_bits ) == PSA_SUCCESS );
911 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
912 exercise_alg );
913 buffer = mbedtls_calloc( 1, buffer_length );
914 TEST_ASSERT( buffer != NULL );
915
916 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
917 NULL, 0,
918 NULL, 0,
919 buffer, buffer_length,
920 &output_length );
921 if( policy_alg == exercise_alg &&
922 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
923 TEST_ASSERT( status == PSA_SUCCESS );
924 else
925 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
926
927 memset( buffer, 0, buffer_length );
928 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
929 buffer, buffer_length,
930 NULL, 0,
931 buffer, buffer_length,
932 &output_length );
933 if( policy_alg == exercise_alg &&
934 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
935 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
936 else
937 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
938
939exit:
940 psa_destroy_key( key_slot );
941 mbedtls_psa_crypto_free( );
942 mbedtls_free( buffer );
943}
944/* END_CASE */
945
946/* BEGIN_CASE */
947void asymmetric_signature_key_policy( int policy_usage,
948 int policy_alg,
949 int key_type,
950 data_t *key_data,
951 int exercise_alg )
952{
953 int key_slot = 1;
954 psa_key_policy_t policy;
955 psa_status_t status;
956 unsigned char payload[16] = {1};
957 size_t payload_length = sizeof( payload );
958 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
959 size_t signature_length;
960
961 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
962
963 psa_key_policy_init( &policy );
964 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
965 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
966
967 TEST_ASSERT( psa_import_key( key_slot, key_type,
968 key_data->x, key_data->len ) == PSA_SUCCESS );
969
970 status = psa_asymmetric_sign( key_slot, exercise_alg,
971 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200972 signature, sizeof( signature ),
973 &signature_length );
974 if( policy_alg == exercise_alg &&
975 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
976 TEST_ASSERT( status == PSA_SUCCESS );
977 else
978 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
979
980 memset( signature, 0, sizeof( signature ) );
981 status = psa_asymmetric_verify( key_slot, exercise_alg,
982 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200983 signature, sizeof( signature ) );
984 if( policy_alg == exercise_alg &&
985 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
986 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
987 else
988 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +0200989
990exit:
991 psa_destroy_key( key_slot );
992 mbedtls_psa_crypto_free( );
993}
994/* END_CASE */
995
996/* BEGIN_CASE */
997void key_lifetime( int lifetime_arg )
998{
999 int key_slot = 1;
1000 psa_key_type_t key_type = PSA_ALG_CBC_BASE;
1001 unsigned char key[32] = {0};
1002 psa_key_lifetime_t lifetime_set = lifetime_arg;
1003 psa_key_lifetime_t lifetime_get;
1004
1005 memset( key, 0x2a, sizeof( key ) );
1006
1007 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1008
1009 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1010 lifetime_set ) == PSA_SUCCESS );
1011
1012 TEST_ASSERT( psa_import_key( key_slot, key_type,
1013 key, sizeof( key ) ) == PSA_SUCCESS );
1014
1015 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1016 &lifetime_get ) == PSA_SUCCESS );
1017
1018 TEST_ASSERT( lifetime_get == lifetime_set );
1019
1020exit:
1021 psa_destroy_key( key_slot );
1022 mbedtls_psa_crypto_free( );
1023}
1024/* END_CASE */
1025
1026/* BEGIN_CASE */
1027void key_lifetime_set_fail( int key_slot_arg,
1028 int lifetime_arg,
1029 int expected_status_arg )
1030{
1031 psa_key_slot_t key_slot = key_slot_arg;
1032 psa_key_lifetime_t lifetime_set = lifetime_arg;
1033 psa_status_t actual_status;
1034 psa_status_t expected_status = expected_status_arg;
1035
1036 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1037
1038 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1039
1040 if( actual_status == PSA_SUCCESS )
1041 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1042
1043 TEST_ASSERT( expected_status == actual_status );
1044
1045exit:
1046 psa_destroy_key( key_slot );
1047 mbedtls_psa_crypto_free( );
1048}
1049/* END_CASE */
1050
1051/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001052void hash_setup( int alg_arg,
1053 int expected_status_arg )
1054{
1055 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001056 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001057 psa_hash_operation_t operation;
1058 psa_status_t status;
1059
1060 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1061
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001062 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001063 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001064 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001065
1066exit:
1067 mbedtls_psa_crypto_free( );
1068}
1069/* END_CASE */
1070
1071/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001072void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001073{
1074 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001075 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001076 size_t actual_hash_length;
1077 psa_hash_operation_t operation;
1078
Gilles Peskine69c12672018-06-28 00:07:19 +02001079 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1080 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1081
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001082 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001083 TEST_ASSERT( expected_hash != NULL );
1084 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1085 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001086
1087 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1088
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001089 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001090 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001091 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001092 TEST_ASSERT( psa_hash_finish( &operation,
1093 actual_hash, sizeof( actual_hash ),
1094 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001095 TEST_ASSERT( actual_hash_length == expected_hash->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001096 TEST_ASSERT( memcmp( expected_hash->x, actual_hash,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001097 expected_hash->len ) == 0 );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001098
1099exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001100 mbedtls_psa_crypto_free( );
1101}
1102/* END_CASE */
1103
1104/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001105void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001106{
1107 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001108 psa_hash_operation_t operation;
1109
Gilles Peskine69c12672018-06-28 00:07:19 +02001110 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1111 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1112
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001113 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001114 TEST_ASSERT( expected_hash != NULL );
1115 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1116 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001117
1118 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1119
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001120 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001121 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001122 input->x,
1123 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001124 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001125 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001126 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001127
1128exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001129 mbedtls_psa_crypto_free( );
1130}
1131/* END_CASE */
1132
1133/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001134void mac_setup( int key_type_arg,
1135 data_t *key,
1136 int alg_arg,
1137 int expected_status_arg )
1138{
1139 int key_slot = 1;
1140 psa_key_type_t key_type = key_type_arg;
1141 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001142 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001143 psa_mac_operation_t operation;
1144 psa_key_policy_t policy;
1145 psa_status_t status;
1146
1147 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1148
1149 psa_key_policy_init( &policy );
1150 psa_key_policy_set_usage( &policy,
1151 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1152 alg );
1153 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1154
1155 TEST_ASSERT( psa_import_key( key_slot, key_type,
1156 key->x, key->len ) == PSA_SUCCESS );
1157
Gilles Peskine89167cb2018-07-08 20:12:23 +02001158 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001159 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001160 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001161
1162exit:
1163 psa_destroy_key( key_slot );
1164 mbedtls_psa_crypto_free( );
1165}
1166/* END_CASE */
1167
1168/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001169void mac_verify( int key_type_arg,
1170 data_t *key,
1171 int alg_arg,
1172 data_t *input,
1173 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001174{
1175 int key_slot = 1;
1176 psa_key_type_t key_type = key_type_arg;
1177 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001178 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001179 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001180
Gilles Peskine69c12672018-06-28 00:07:19 +02001181 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1182
Gilles Peskine8c9def32018-02-08 10:02:12 +01001183 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001184 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001185 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001186 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001187 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1188 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001189
1190 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1191
mohammad16036df908f2018-04-02 08:34:15 -07001192 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001193 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07001194 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1195
Gilles Peskine8c9def32018-02-08 10:02:12 +01001196 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001197 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001198
Gilles Peskine89167cb2018-07-08 20:12:23 +02001199 TEST_ASSERT( psa_mac_verify_setup( &operation,
1200 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001201 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1202 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001203 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02001204 TEST_ASSERT( psa_mac_verify_finish( &operation,
1205 expected_mac->x,
1206 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001207
1208exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001209 psa_destroy_key( key_slot );
1210 mbedtls_psa_crypto_free( );
1211}
1212/* END_CASE */
1213
1214/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001215void cipher_setup( int key_type_arg,
1216 data_t *key,
1217 int alg_arg,
1218 int expected_status_arg )
1219{
1220 int key_slot = 1;
1221 psa_key_type_t key_type = key_type_arg;
1222 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001223 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001224 psa_cipher_operation_t operation;
1225 psa_key_policy_t policy;
1226 psa_status_t status;
1227
1228 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1229
1230 psa_key_policy_init( &policy );
1231 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1232 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1233
1234 TEST_ASSERT( psa_import_key( key_slot, key_type,
1235 key->x, key->len ) == PSA_SUCCESS );
1236
Gilles Peskinefe119512018-07-08 21:39:34 +02001237 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001238 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001239 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001240
1241exit:
1242 psa_destroy_key( key_slot );
1243 mbedtls_psa_crypto_free( );
1244}
1245/* END_CASE */
1246
1247/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001248void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001249 data_t *key,
1250 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001251 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001252{
1253 int key_slot = 1;
1254 psa_status_t status;
1255 psa_key_type_t key_type = key_type_arg;
1256 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001257 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001258 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001259 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001260 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001261 size_t output_buffer_size = 0;
1262 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001263 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001264 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001265 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001266
Gilles Peskine50e586b2018-06-08 14:28:46 +02001267 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001268 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001269 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001270 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1271 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1272 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001273
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001274 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1275 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001276
1277 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1278
Moran Pekered346952018-07-05 15:22:45 +03001279 psa_key_policy_init( &policy );
1280 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1281 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1282
Gilles Peskine50e586b2018-06-08 14:28:46 +02001283 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001284 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001285
Gilles Peskinefe119512018-07-08 21:39:34 +02001286 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1287 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001288
Gilles Peskinefe119512018-07-08 21:39:34 +02001289 TEST_ASSERT( psa_cipher_set_iv( &operation,
1290 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001291 output_buffer_size = (size_t) input->len +
1292 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001293 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001294 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001295
Gilles Peskine4abf7412018-06-18 16:35:34 +02001296 TEST_ASSERT( psa_cipher_update( &operation,
1297 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001298 output, output_buffer_size,
1299 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001300 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001301 status = psa_cipher_finish( &operation,
1302 output + function_output_length,
1303 output_buffer_size,
1304 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001305 total_output_length += function_output_length;
1306
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001307 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001308 if( expected_status == PSA_SUCCESS )
1309 {
1310 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001311 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001312 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001313 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001314 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001315
Gilles Peskine50e586b2018-06-08 14:28:46 +02001316exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001317 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001318 psa_destroy_key( key_slot );
1319 mbedtls_psa_crypto_free( );
1320}
1321/* END_CASE */
1322
1323/* BEGIN_CASE */
1324void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001325 data_t *key,
1326 data_t *input,
1327 int first_part_size,
1328 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001329{
1330 int key_slot = 1;
1331 psa_key_type_t key_type = key_type_arg;
1332 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001333 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001334 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001335 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001336 size_t output_buffer_size = 0;
1337 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001338 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001339 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001340 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001341
Gilles Peskine50e586b2018-06-08 14:28:46 +02001342 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001343 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001344 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001345 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1346 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1347 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001348
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001349 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1350 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001351
1352 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1353
Moran Pekered346952018-07-05 15:22:45 +03001354 psa_key_policy_init( &policy );
1355 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1356 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1357
Gilles Peskine50e586b2018-06-08 14:28:46 +02001358 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001359 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001360
Gilles Peskinefe119512018-07-08 21:39:34 +02001361 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1362 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001363
Gilles Peskinefe119512018-07-08 21:39:34 +02001364 TEST_ASSERT( psa_cipher_set_iv( &operation,
1365 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001366 output_buffer_size = (size_t) input->len +
1367 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001368 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001369 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001370
Gilles Peskine4abf7412018-06-18 16:35:34 +02001371 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001372 TEST_ASSERT( psa_cipher_update( &operation, input->x, 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_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001377 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001378 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001379 output, output_buffer_size,
1380 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001381 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001382 TEST_ASSERT( psa_cipher_finish( &operation,
1383 output + function_output_length,
1384 output_buffer_size,
1385 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001386 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001387 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1388
Gilles Peskine4abf7412018-06-18 16:35:34 +02001389 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001390 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001391 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001392
1393exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001394 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001395 psa_destroy_key( key_slot );
1396 mbedtls_psa_crypto_free( );
1397}
1398/* END_CASE */
1399
1400/* BEGIN_CASE */
1401void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001402 data_t *key,
1403 data_t *input,
1404 int first_part_size,
1405 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001406{
1407 int key_slot = 1;
1408
1409 psa_key_type_t key_type = key_type_arg;
1410 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001411 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001412 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001413 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001414 size_t output_buffer_size = 0;
1415 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001416 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001417 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001418 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001419
Gilles Peskine50e586b2018-06-08 14:28:46 +02001420 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001421 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001422 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001423 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1424 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1425 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001426
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001427 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1428 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001429
1430 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1431
Moran Pekered346952018-07-05 15:22:45 +03001432 psa_key_policy_init( &policy );
1433 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1434 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1435
Gilles Peskine50e586b2018-06-08 14:28:46 +02001436 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001437 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001438
Gilles Peskinefe119512018-07-08 21:39:34 +02001439 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1440 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001441
Gilles Peskinefe119512018-07-08 21:39:34 +02001442 TEST_ASSERT( psa_cipher_set_iv( &operation,
1443 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001444
mohammad16033d91abe2018-07-03 13:15:54 +03001445 output_buffer_size = (size_t) input->len +
1446 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001447 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001448 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001449
Gilles Peskine4abf7412018-06-18 16:35:34 +02001450 TEST_ASSERT( (unsigned int) first_part_size < input->len );
1451 TEST_ASSERT( psa_cipher_update( &operation,
1452 input->x, 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_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001457 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001458 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001459 output, output_buffer_size,
1460 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001461 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001462 TEST_ASSERT( psa_cipher_finish( &operation,
1463 output + function_output_length,
1464 output_buffer_size,
1465 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001466 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001467 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1468
Gilles Peskine4abf7412018-06-18 16:35:34 +02001469 TEST_ASSERT( total_output_length == expected_output->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001470 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001471 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001472
1473exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001474 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001475 psa_destroy_key( key_slot );
1476 mbedtls_psa_crypto_free( );
1477}
1478/* END_CASE */
1479
Gilles Peskine50e586b2018-06-08 14:28:46 +02001480/* BEGIN_CASE */
1481void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001482 data_t *key,
1483 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001484 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001485{
1486 int key_slot = 1;
1487 psa_status_t status;
1488 psa_key_type_t key_type = key_type_arg;
1489 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001490 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001491 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001492 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001493 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001494 size_t output_buffer_size = 0;
1495 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001496 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001497 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001498 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001499
Gilles Peskine50e586b2018-06-08 14:28:46 +02001500 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001501 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001502 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001503 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1504 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1505 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001506
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001507 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1508 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001509
1510 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1511
Moran Pekered346952018-07-05 15:22:45 +03001512 psa_key_policy_init( &policy );
1513 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1514 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1515
Gilles Peskine50e586b2018-06-08 14:28:46 +02001516 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001517 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001518
Gilles Peskinefe119512018-07-08 21:39:34 +02001519 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1520 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001521
Gilles Peskinefe119512018-07-08 21:39:34 +02001522 TEST_ASSERT( psa_cipher_set_iv( &operation,
1523 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001524
mohammad16033d91abe2018-07-03 13:15:54 +03001525 output_buffer_size = (size_t) input->len +
1526 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001527 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001528 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001529
Gilles Peskine4abf7412018-06-18 16:35:34 +02001530 TEST_ASSERT( psa_cipher_update( &operation,
1531 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001532 output, output_buffer_size,
1533 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001534 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001535 status = psa_cipher_finish( &operation,
1536 output + function_output_length,
1537 output_buffer_size,
1538 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001539 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001540 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001541
1542 if( expected_status == PSA_SUCCESS )
1543 {
1544 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001545 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001546 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001547 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001548 }
1549
Gilles Peskine50e586b2018-06-08 14:28:46 +02001550exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001551 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001552 psa_destroy_key( key_slot );
1553 mbedtls_psa_crypto_free( );
1554}
1555/* END_CASE */
1556
Gilles Peskine50e586b2018-06-08 14:28:46 +02001557/* BEGIN_CASE */
1558void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001559 data_t *key,
1560 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02001561{
1562 int key_slot = 1;
1563 psa_key_type_t key_type = key_type_arg;
1564 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07001565 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02001566 size_t iv_size = 16;
1567 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001568 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001569 size_t output1_size = 0;
1570 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001571 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001572 size_t output2_size = 0;
1573 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001574 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001575 psa_cipher_operation_t operation1;
1576 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03001577 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001578
mohammad1603d7d7ba52018-03-12 18:51:53 +02001579 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001580 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001581 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1582 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001583
mohammad1603d7d7ba52018-03-12 18:51:53 +02001584 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1585
Moran Pekered346952018-07-05 15:22:45 +03001586 psa_key_policy_init( &policy );
1587 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
1588 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1589
mohammad1603d7d7ba52018-03-12 18:51:53 +02001590 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001591 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001592
Gilles Peskinefe119512018-07-08 21:39:34 +02001593 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
1594 key_slot, alg ) == PSA_SUCCESS );
1595 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
1596 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001597
Gilles Peskinefe119512018-07-08 21:39:34 +02001598 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
1599 iv, iv_size,
1600 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001601 output1_size = (size_t) input->len +
1602 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001603 output1 = mbedtls_calloc( 1, output1_size );
1604 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001605
Gilles Peskine4abf7412018-06-18 16:35:34 +02001606 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001607 output1, output1_size,
1608 &output1_length ) == PSA_SUCCESS );
1609 TEST_ASSERT( psa_cipher_finish( &operation1,
1610 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001611 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001612
Gilles Peskine048b7f02018-06-08 14:20:49 +02001613 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001614
1615 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1616
1617 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001618 output2 = mbedtls_calloc( 1, output2_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001619 TEST_ASSERT( output2 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001620
Gilles Peskinefe119512018-07-08 21:39:34 +02001621 TEST_ASSERT( psa_cipher_set_iv( &operation2,
1622 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001623 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
1624 output2, output2_size,
1625 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001626 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001627 TEST_ASSERT( psa_cipher_finish( &operation2,
1628 output2 + output2_length,
1629 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001630 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001631
Gilles Peskine048b7f02018-06-08 14:20:49 +02001632 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001633
Janos Follath25c4fa82018-07-06 16:23:25 +01001634 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001635
Gilles Peskine4abf7412018-06-18 16:35:34 +02001636 TEST_ASSERT( input->len == output2_length );
1637 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +03001638
1639exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001640 mbedtls_free( output1 );
1641 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03001642 psa_destroy_key( key_slot );
1643 mbedtls_psa_crypto_free( );
1644}
1645/* END_CASE */
1646
1647/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001648void cipher_verify_output_multipart( int alg_arg,
1649 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001650 data_t *key,
1651 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001652 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03001653{
1654 int key_slot = 1;
1655 psa_key_type_t key_type = key_type_arg;
1656 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03001657 unsigned char iv[16] = {0};
1658 size_t iv_size = 16;
1659 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001660 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001661 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001662 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001663 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001664 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001665 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001666 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001667 psa_cipher_operation_t operation1;
1668 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03001669 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03001670
Moran Pekerded84402018-06-06 16:36:50 +03001671 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001672 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001673 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1674 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001675
Moran Pekerded84402018-06-06 16:36:50 +03001676 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1677
Moran Pekered346952018-07-05 15:22:45 +03001678 psa_key_policy_init( &policy );
1679 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
1680 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1681
Moran Pekerded84402018-06-06 16:36:50 +03001682 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001683 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001684
Gilles Peskinefe119512018-07-08 21:39:34 +02001685 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
1686 key_slot, alg ) == PSA_SUCCESS );
1687 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
1688 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001689
Gilles Peskinefe119512018-07-08 21:39:34 +02001690 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
1691 iv, iv_size,
1692 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001693 output1_buffer_size = (size_t) input->len +
1694 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001695 output1 = mbedtls_calloc( 1, output1_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001696 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001697
Gilles Peskine4abf7412018-06-18 16:35:34 +02001698 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001699
itayzafrir3e02b3b2018-06-12 17:06:52 +03001700 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001701 output1, output1_buffer_size,
1702 &function_output_length ) == PSA_SUCCESS );
1703 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001704
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001705 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001706 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001707 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001708 output1, output1_buffer_size,
1709 &function_output_length ) == PSA_SUCCESS );
1710 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001711
Gilles Peskine048b7f02018-06-08 14:20:49 +02001712 TEST_ASSERT( psa_cipher_finish( &operation1,
1713 output1 + output1_length,
1714 output1_buffer_size - output1_length,
1715 &function_output_length ) == PSA_SUCCESS );
1716 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001717
1718 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1719
Gilles Peskine048b7f02018-06-08 14:20:49 +02001720 output2_buffer_size = output1_length;
1721 output2 = mbedtls_calloc( 1, output2_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001722 TEST_ASSERT( output2 != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001723
Gilles Peskinefe119512018-07-08 21:39:34 +02001724 TEST_ASSERT( psa_cipher_set_iv( &operation2,
1725 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001726
1727 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001728 output2, output2_buffer_size,
1729 &function_output_length ) == PSA_SUCCESS );
1730 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001731
Gilles Peskine048b7f02018-06-08 14:20:49 +02001732 TEST_ASSERT( psa_cipher_update( &operation2,
1733 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001734 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001735 output2, output2_buffer_size,
1736 &function_output_length ) == PSA_SUCCESS );
1737 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001738
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001739 TEST_ASSERT( psa_cipher_finish( &operation2,
1740 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001741 output2_buffer_size - output2_length,
1742 &function_output_length ) == PSA_SUCCESS );
1743 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001744
Janos Follath25c4fa82018-07-06 16:23:25 +01001745 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001746
Gilles Peskine4abf7412018-06-18 16:35:34 +02001747 TEST_ASSERT( input->len == output2_length );
1748 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001749
1750exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001751 mbedtls_free( output1 );
1752 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001753 psa_destroy_key( key_slot );
1754 mbedtls_psa_crypto_free( );
1755}
1756/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02001757
Gilles Peskine20035e32018-02-03 22:44:14 +01001758/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001759void aead_encrypt_decrypt( int key_type_arg,
1760 data_t * key_data,
1761 int alg_arg,
1762 data_t * input_data,
1763 data_t * nonce,
1764 data_t * additional_data,
1765 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001766{
1767 int slot = 1;
1768 psa_key_type_t key_type = key_type_arg;
1769 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001770 unsigned char *output_data = NULL;
1771 size_t output_size = 0;
1772 size_t output_length = 0;
1773 unsigned char *output_data2 = NULL;
1774 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001775 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001776 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001777 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001778
Gilles Peskinea1cac842018-06-11 19:33:02 +02001779 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001780 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001781 TEST_ASSERT( nonce != NULL );
1782 TEST_ASSERT( additional_data != NULL );
1783 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1784 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1785 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1786 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1787
Gilles Peskine4abf7412018-06-18 16:35:34 +02001788 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001789 output_data = mbedtls_calloc( 1, output_size );
1790 TEST_ASSERT( output_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001791
1792 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1793
1794 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001795 psa_key_policy_set_usage( &policy,
1796 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
1797 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001798 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1799
1800 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001801 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001802
1803 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001804 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001805 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001806 additional_data->len,
1807 input_data->x, input_data->len,
1808 output_data, output_size,
1809 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001810
1811 if( PSA_SUCCESS == expected_result )
1812 {
1813 output_data2 = mbedtls_calloc( 1, output_length );
1814 TEST_ASSERT( output_data2 != NULL );
1815
1816 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001817 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001818 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001819 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001820 output_data, output_length,
1821 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001822 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02001823
itayzafrir3e02b3b2018-06-12 17:06:52 +03001824 TEST_ASSERT( memcmp( input_data->x, output_data2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001825 input_data->len ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001826 }
Gilles Peskine2d277862018-06-18 15:41:12 +02001827
Gilles Peskinea1cac842018-06-11 19:33:02 +02001828exit:
1829 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001830 mbedtls_free( output_data );
1831 mbedtls_free( output_data2 );
1832 mbedtls_psa_crypto_free( );
1833}
1834/* END_CASE */
1835
1836/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001837void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001838 int alg_arg, data_t * input_data,
1839 data_t * additional_data, data_t * nonce,
1840 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001841{
1842 int slot = 1;
1843 psa_key_type_t key_type = key_type_arg;
1844 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001845 unsigned char *output_data = NULL;
1846 size_t output_size = 0;
1847 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001848 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001849 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001850
Gilles Peskinea1cac842018-06-11 19:33:02 +02001851 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001852 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001853 TEST_ASSERT( additional_data != NULL );
1854 TEST_ASSERT( nonce != NULL );
1855 TEST_ASSERT( expected_result != NULL );
1856 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1857 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1858 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1859 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1860 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
1861
Gilles Peskine4abf7412018-06-18 16:35:34 +02001862 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001863 output_data = mbedtls_calloc( 1, output_size );
1864 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001865
Gilles Peskinea1cac842018-06-11 19:33:02 +02001866 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1867
1868 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001869 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001870 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1871
1872 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001873 key_data->x,
1874 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001875
1876 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001877 nonce->x, nonce->len,
1878 additional_data->x, additional_data->len,
1879 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001880 output_data, output_size,
1881 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001882
itayzafrir3e02b3b2018-06-12 17:06:52 +03001883 TEST_ASSERT( memcmp( output_data, expected_result->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001884 output_length ) == 0 );
1885
Gilles Peskinea1cac842018-06-11 19:33:02 +02001886exit:
1887 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001888 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001889 mbedtls_psa_crypto_free( );
1890}
1891/* END_CASE */
1892
1893/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001894void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001895 int alg_arg, data_t * input_data,
1896 data_t * additional_data, data_t * nonce,
1897 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001898{
1899 int slot = 1;
1900 psa_key_type_t key_type = key_type_arg;
1901 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001902 unsigned char *output_data = NULL;
1903 size_t output_size = 0;
1904 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001905 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001906 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001907 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001908
Gilles Peskinea1cac842018-06-11 19:33:02 +02001909 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001910 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001911 TEST_ASSERT( additional_data != NULL );
1912 TEST_ASSERT( nonce != NULL );
1913 TEST_ASSERT( expected_data != NULL );
1914 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1915 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1916 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1917 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1918 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
1919
Gilles Peskine4abf7412018-06-18 16:35:34 +02001920 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001921 output_data = mbedtls_calloc( 1, output_size );
1922 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001923
Gilles Peskinea1cac842018-06-11 19:33:02 +02001924 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1925
1926 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001927 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001928 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1929
1930 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001931 key_data->x,
1932 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001933
1934 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001935 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001936 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001937 additional_data->len,
1938 input_data->x, input_data->len,
1939 output_data, output_size,
1940 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001941
Gilles Peskine2d277862018-06-18 15:41:12 +02001942 if( expected_result == PSA_SUCCESS )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001943 {
itayzafrir3e02b3b2018-06-12 17:06:52 +03001944 TEST_ASSERT( memcmp( output_data, expected_data->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001945 output_length ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001946 }
1947
Gilles Peskinea1cac842018-06-11 19:33:02 +02001948exit:
1949 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001950 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001951 mbedtls_psa_crypto_free( );
1952}
1953/* END_CASE */
1954
1955/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001956void signature_size( int type_arg,
1957 int bits,
1958 int alg_arg,
1959 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001960{
1961 psa_key_type_t type = type_arg;
1962 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02001963 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01001964 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
1965exit:
1966 ;
1967}
1968/* END_CASE */
1969
1970/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001971void sign_deterministic( int key_type_arg, data_t *key_data,
1972 int alg_arg, data_t *input_data,
1973 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001974{
1975 int slot = 1;
1976 psa_key_type_t key_type = key_type_arg;
1977 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01001978 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01001979 unsigned char *signature = NULL;
1980 size_t signature_size;
1981 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02001982 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01001983
Gilles Peskine20035e32018-02-03 22:44:14 +01001984 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001985 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001986 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001987 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1988 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1989 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01001990
1991 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1992
mohammad1603a97cb8c2018-03-28 03:46:26 -07001993 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001994 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001995 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1996
Gilles Peskine20035e32018-02-03 22:44:14 +01001997 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001998 key_data->x,
1999 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002000 TEST_ASSERT( psa_get_key_information( slot,
2001 NULL,
2002 &key_bits ) == PSA_SUCCESS );
2003
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002004 /* Allocate a buffer which has the size advertized by the
2005 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002006 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2007 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002008 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002009 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine20035e32018-02-03 22:44:14 +01002010 signature = mbedtls_calloc( 1, signature_size );
2011 TEST_ASSERT( signature != NULL );
2012
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002013 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002014 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002015 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002016 signature, signature_size,
2017 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002018 /* Verify that the signature is what is expected. */
Gilles Peskine4abf7412018-06-18 16:35:34 +02002019 TEST_ASSERT( signature_length == output_data->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02002020 TEST_ASSERT( memcmp( signature, output_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002021 output_data->len ) == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +01002022
2023exit:
2024 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002025 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002026 mbedtls_psa_crypto_free( );
2027}
2028/* END_CASE */
2029
2030/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002031void sign_fail( int key_type_arg, data_t *key_data,
2032 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002033 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002034{
2035 int slot = 1;
2036 psa_key_type_t key_type = key_type_arg;
2037 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002038 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002039 psa_status_t actual_status;
2040 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002041 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002042 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002043 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002044
Gilles Peskine20035e32018-02-03 22:44:14 +01002045 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002046 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002047 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2048 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2049
Gilles Peskine20035e32018-02-03 22:44:14 +01002050 signature = mbedtls_calloc( 1, signature_size );
2051 TEST_ASSERT( signature != NULL );
2052
2053 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2054
mohammad1603a97cb8c2018-03-28 03:46:26 -07002055 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002056 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002057 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2058
Gilles Peskine20035e32018-02-03 22:44:14 +01002059 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002060 key_data->x,
2061 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002062
2063 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002064 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002065 signature, signature_size,
2066 &signature_length );
2067 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002068 /* The value of *signature_length is unspecified on error, but
2069 * whatever it is, it should be less than signature_size, so that
2070 * if the caller tries to read *signature_length bytes without
2071 * checking the error code then they don't overflow a buffer. */
2072 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002073
2074exit:
2075 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002076 mbedtls_free( signature );
2077 mbedtls_psa_crypto_free( );
2078}
2079/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002080
2081/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002082void sign_verify( int key_type_arg, data_t *key_data,
2083 int alg_arg, data_t *input_data )
2084{
2085 int slot = 1;
2086 psa_key_type_t key_type = key_type_arg;
2087 psa_algorithm_t alg = alg_arg;
2088 size_t key_bits;
2089 unsigned char *signature = NULL;
2090 size_t signature_size;
2091 size_t signature_length = 0xdeadbeef;
2092 psa_key_policy_t policy;
2093
2094 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2095
2096 psa_key_policy_init( &policy );
2097 psa_key_policy_set_usage( &policy,
2098 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2099 alg );
2100 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2101
2102 TEST_ASSERT( psa_import_key( slot, key_type,
2103 key_data->x,
2104 key_data->len ) == PSA_SUCCESS );
2105 TEST_ASSERT( psa_get_key_information( slot,
2106 NULL,
2107 &key_bits ) == PSA_SUCCESS );
2108
2109 /* Allocate a buffer which has the size advertized by the
2110 * library. */
2111 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2112 key_bits, alg );
2113 TEST_ASSERT( signature_size != 0 );
2114 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2115 signature = mbedtls_calloc( 1, signature_size );
2116 TEST_ASSERT( signature != NULL );
2117
2118 /* Perform the signature. */
2119 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2120 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002121 signature, signature_size,
2122 &signature_length ) == PSA_SUCCESS );
2123 /* Check that the signature length looks sensible. */
2124 TEST_ASSERT( signature_length <= signature_size );
2125 TEST_ASSERT( signature_length > 0 );
2126
2127 /* Use the library to verify that the signature is correct. */
2128 TEST_ASSERT( psa_asymmetric_verify(
2129 slot, alg,
2130 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002131 signature, signature_length ) == PSA_SUCCESS );
2132
2133 if( input_data->len != 0 )
2134 {
2135 /* Flip a bit in the input and verify that the signature is now
2136 * detected as invalid. Flip a bit at the beginning, not at the end,
2137 * because ECDSA may ignore the last few bits of the input. */
2138 input_data->x[0] ^= 1;
2139 TEST_ASSERT( psa_asymmetric_verify(
2140 slot, alg,
2141 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002142 signature,
2143 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2144 }
2145
2146exit:
2147 psa_destroy_key( slot );
2148 mbedtls_free( signature );
2149 mbedtls_psa_crypto_free( );
2150}
2151/* END_CASE */
2152
2153/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002154void asymmetric_verify( int key_type_arg, data_t *key_data,
2155 int alg_arg, data_t *hash_data,
2156 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002157{
2158 int slot = 1;
2159 psa_key_type_t key_type = key_type_arg;
2160 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002161 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002162
Gilles Peskine69c12672018-06-28 00:07:19 +02002163 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2164
itayzafrir5c753392018-05-08 11:18:38 +03002165 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002166 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002167 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002168 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2169 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2170 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002171
2172 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2173
2174 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002175 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002176 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2177
2178 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002179 key_data->x,
2180 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002181
2182 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002183 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002184 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002185 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002186exit:
2187 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002188 mbedtls_psa_crypto_free( );
2189}
2190/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002191
2192/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002193void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2194 int alg_arg, data_t *hash_data,
2195 data_t *signature_data,
2196 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002197{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002198 int slot = 1;
2199 psa_key_type_t key_type = key_type_arg;
2200 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002201 psa_status_t actual_status;
2202 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002203 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002204
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002205 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002206 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002207 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002208 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2209 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2210 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002211
2212 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2213
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002214 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002215 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002216 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2217
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002218 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002219 key_data->x,
2220 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002221
2222 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002223 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002224 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002225 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002226
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002227 TEST_ASSERT( actual_status == expected_status );
2228
2229exit:
2230 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002231 mbedtls_psa_crypto_free( );
2232}
2233/* END_CASE */
2234
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002235/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002236void asymmetric_encrypt( int key_type_arg,
2237 data_t *key_data,
2238 int alg_arg,
2239 data_t *input_data,
2240 int expected_output_length_arg,
2241 int expected_status_arg )
2242{
2243 int slot = 1;
2244 psa_key_type_t key_type = key_type_arg;
2245 psa_algorithm_t alg = alg_arg;
2246 size_t expected_output_length = expected_output_length_arg;
2247 size_t key_bits;
2248 unsigned char *output = NULL;
2249 size_t output_size;
2250 size_t output_length = ~0;
2251 psa_status_t actual_status;
2252 psa_status_t expected_status = expected_status_arg;
2253 psa_key_policy_t policy;
2254
2255 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2256
2257 /* Import the key */
2258 psa_key_policy_init( &policy );
2259 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2260 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2261 TEST_ASSERT( psa_import_key( slot, key_type,
2262 key_data->x,
2263 key_data->len ) == PSA_SUCCESS );
2264
2265 /* Determine the maximum output length */
2266 TEST_ASSERT( psa_get_key_information( slot,
2267 NULL,
2268 &key_bits ) == PSA_SUCCESS );
2269 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
2270 output = mbedtls_calloc( 1, output_size );
2271 TEST_ASSERT( output != NULL );
2272
2273 /* Encrypt the input */
2274 actual_status = psa_asymmetric_encrypt( slot, alg,
2275 input_data->x, input_data->len,
2276 NULL, 0,
2277 output, output_size,
2278 &output_length );
2279 TEST_ASSERT( actual_status == expected_status );
2280 TEST_ASSERT( output_length == expected_output_length );
2281
2282exit:
2283 psa_destroy_key( slot );
2284 mbedtls_free( output );
2285 mbedtls_psa_crypto_free( );
2286}
2287/* END_CASE */
2288
2289/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002290void asymmetric_encrypt_decrypt( int key_type_arg, data_t *key_data,
2291 int alg_arg, data_t *input_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002292{
2293 int slot = 1;
2294 psa_key_type_t key_type = key_type_arg;
2295 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002296 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002297 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002298 size_t output_length = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002299 unsigned char *output2 = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002300 size_t output2_size = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002301 size_t output2_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002302 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002303
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002304 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002305 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002306 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2307 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2308
Gilles Peskine4abf7412018-06-18 16:35:34 +02002309 output_size = key_data->len;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002310 output2_size = output_size;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002311 output = mbedtls_calloc( 1, output_size );
2312 TEST_ASSERT( output != NULL );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002313 output2 = mbedtls_calloc( 1, output2_size );
2314 TEST_ASSERT( output2 != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002315
2316 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2317
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002318 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002319 psa_key_policy_set_usage( &policy,
2320 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002321 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002322 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2323
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002324 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002325 key_data->x,
2326 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002327
Gilles Peskineeebd7382018-06-08 18:11:54 +02002328 /* We test encryption by checking that encrypt-then-decrypt gives back
2329 * the original plaintext because of the non-optional random
2330 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02002331 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002332 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002333 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002334 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002335 &output_length ) == PSA_SUCCESS );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002336
Gilles Peskine2d277862018-06-18 15:41:12 +02002337 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002338 output, output_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02002339 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002340 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002341 &output2_length ) == PSA_SUCCESS );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002342 TEST_ASSERT( memcmp( input_data->x, output2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002343 input_data->len ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002344
2345exit:
2346 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002347 mbedtls_free( output );
2348 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002349 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002350}
2351/* END_CASE */
2352
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002353/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002354void asymmetric_decrypt( int key_type_arg, data_t *key_data,
2355 int alg_arg, data_t *input_data,
Gilles Peskine66763a02018-06-29 21:54:10 +02002356 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002357{
2358 int slot = 1;
2359 psa_key_type_t key_type = key_type_arg;
2360 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002361 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002362 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002363 size_t output_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002364 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002365
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002366 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002367 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002368 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002369 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2370 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2371 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2372
Gilles Peskine4abf7412018-06-18 16:35:34 +02002373 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002374 output = mbedtls_calloc( 1, output_size );
2375 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002376
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002377 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2378
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002379 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002380 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002381 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2382
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002383 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002384 key_data->x,
2385 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002386
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002387 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002388 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002389 NULL, 0,
2390 output,
2391 output_size,
2392 &output_length ) == PSA_SUCCESS );
Gilles Peskine66763a02018-06-29 21:54:10 +02002393 TEST_ASSERT( expected_data->len == output_length );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002394 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002395
2396exit:
2397 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002398 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002399 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002400}
2401/* END_CASE */
2402
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002403/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002404void asymmetric_decrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002405 int alg_arg, data_t *input_data,
2406 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002407{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002408 int slot = 1;
2409 psa_key_type_t key_type = key_type_arg;
2410 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002411 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002412 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002413 size_t output_length = 0;
2414 psa_status_t actual_status;
2415 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002416 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002417
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002418 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002419 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002420 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2421 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2422
Gilles Peskine4abf7412018-06-18 16:35:34 +02002423 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002424 output = mbedtls_calloc( 1, output_size );
2425 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002426
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002427 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2428
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002429 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002430 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002431 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2432
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002433 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002434 key_data->x,
2435 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002436
Gilles Peskine2d277862018-06-18 15:41:12 +02002437 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002438 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002439 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002440 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002441 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002442 TEST_ASSERT( actual_status == expected_status );
2443
2444exit:
2445 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02002446 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002447 mbedtls_psa_crypto_free( );
2448}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002449/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02002450
2451/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02002452void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02002453{
Gilles Peskinea50d7392018-06-21 10:22:13 +02002454 size_t bytes = bytes_arg;
2455 const unsigned char trail[] = "don't overwrite me";
2456 unsigned char *output = mbedtls_calloc( 1, bytes + sizeof( trail ) );
2457 unsigned char *changed = mbedtls_calloc( 1, bytes );
2458 size_t i;
2459 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02002460
Gilles Peskinea50d7392018-06-21 10:22:13 +02002461 TEST_ASSERT( output != NULL );
2462 TEST_ASSERT( changed != NULL );
2463 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02002464
2465 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2466
Gilles Peskinea50d7392018-06-21 10:22:13 +02002467 /* Run several times, to ensure that every output byte will be
2468 * nonzero at least once with overwhelming probability
2469 * (2^(-8*number_of_runs)). */
2470 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02002471 {
Gilles Peskinea50d7392018-06-21 10:22:13 +02002472 memset( output, 0, bytes );
2473 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
2474
2475 /* Check that no more than bytes have been overwritten */
2476 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
2477
2478 for( i = 0; i < bytes; i++ )
2479 {
2480 if( output[i] != 0 )
2481 ++changed[i];
2482 }
Gilles Peskine05d69892018-06-19 22:00:52 +02002483 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02002484
2485 /* Check that every byte was changed to nonzero at least once. This
2486 * validates that psa_generate_random is overwriting every byte of
2487 * the output buffer. */
2488 for( i = 0; i < bytes; i++ )
2489 {
2490 TEST_ASSERT( changed[i] != 0 );
2491 }
Gilles Peskine05d69892018-06-19 22:00:52 +02002492
2493exit:
2494 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02002495 mbedtls_free( output );
2496 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02002497}
2498/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02002499
2500/* BEGIN_CASE */
2501void generate_key( int type_arg,
2502 int bits_arg,
2503 int usage_arg,
2504 int alg_arg,
2505 int expected_status_arg )
2506{
2507 int slot = 1;
2508 psa_key_type_t type = type_arg;
2509 psa_key_usage_t usage = usage_arg;
2510 size_t bits = bits_arg;
2511 psa_algorithm_t alg = alg_arg;
2512 psa_status_t expected_status = expected_status_arg;
2513 psa_key_type_t got_type;
2514 size_t got_bits;
2515 unsigned char exported[616] = {0}; /* enough for a 1024-bit RSA key */
2516 size_t exported_length;
2517 psa_status_t expected_export_status =
2518 usage & PSA_KEY_USAGE_EXPORT ? PSA_SUCCESS : PSA_ERROR_NOT_PERMITTED;
2519 psa_status_t expected_info_status =
2520 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
2521 psa_key_policy_t policy;
2522
2523 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2524
2525 psa_key_policy_init( &policy );
2526 psa_key_policy_set_usage( &policy, usage, alg );
2527 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2528
2529 /* Generate a key */
2530 TEST_ASSERT( psa_generate_key( slot, type, bits,
2531 NULL, 0 ) == expected_status );
2532
2533 /* Test the key information */
2534 TEST_ASSERT( psa_get_key_information( slot,
2535 &got_type,
2536 &got_bits ) == expected_info_status );
2537 if( expected_info_status != PSA_SUCCESS )
2538 goto exit;
2539 TEST_ASSERT( got_type == type );
2540 TEST_ASSERT( got_bits == bits );
2541
2542 /* Export the key */
2543 TEST_ASSERT( psa_export_key( slot,
2544 exported, sizeof( exported ),
2545 &exported_length ) == expected_export_status );
2546 if( expected_export_status == PSA_SUCCESS )
2547 {
Gilles Peskine48c0ea12018-06-21 14:15:31 +02002548 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02002549 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
2550#if defined(MBEDTLS_DES_C)
2551 if( type == PSA_KEY_TYPE_DES )
2552 {
2553 /* Check the parity bits. */
2554 unsigned i;
2555 for( i = 0; i < bits / 8; i++ )
2556 {
2557 unsigned bit_count = 0;
2558 unsigned m;
2559 for( m = 1; m <= 0x100; m <<= 1 )
2560 {
2561 if( exported[i] & m )
2562 ++bit_count;
2563 }
2564 TEST_ASSERT( bit_count % 2 != 0 );
2565 }
2566 }
2567#endif
2568#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
2569 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
2570 {
2571 /* Sanity check: does this look like the beginning of a PKCS#8
2572 * RSA key pair? Assumes bits is a multiple of 8. */
2573 size_t n_bytes = bits / 8 + 1;
2574 size_t n_encoded_bytes;
2575 unsigned char *n_end;
2576 TEST_ASSERT( exported_length >= 7 + ( n_bytes + 3 ) * 9 / 2 );
2577 TEST_ASSERT( exported[0] == 0x30 );
2578 TEST_ASSERT( exported[1] == 0x82 ); // assumes >=416-bit key
2579 TEST_ASSERT( exported[4] == 0x02 );
2580 TEST_ASSERT( exported[5] == 0x01 );
2581 TEST_ASSERT( exported[6] == 0x00 );
2582 TEST_ASSERT( exported[7] == 0x02 );
2583 n_encoded_bytes = exported[8];
2584 n_end = exported + 9 + n_encoded_bytes;
2585 if( n_encoded_bytes & 0x80 )
2586 {
2587 n_encoded_bytes = ( n_encoded_bytes & 0x7f ) << 7;
2588 n_encoded_bytes |= exported[9] & 0x7f;
2589 n_end += 1;
2590 }
2591 /* The encoding of n should start with a 0 byte since it should
2592 * have its high bit set. However Mbed TLS is not compliant and
2593 * generates an invalid, but widely tolerated, encoding of
2594 * positive INTEGERs with a bit size that is a multiple of 8
2595 * with no leading 0 byte. Accept this here. */
2596 TEST_ASSERT( n_bytes == n_encoded_bytes ||
2597 n_bytes == n_encoded_bytes + 1 );
2598 if( n_bytes == n_encoded_bytes )
2599 TEST_ASSERT( exported[n_encoded_bytes <= 127 ? 9 : 10] == 0x00 );
2600 /* Sanity check: e must be 3 */
2601 TEST_ASSERT( n_end[0] == 0x02 );
2602 TEST_ASSERT( n_end[1] == 0x03 );
2603 TEST_ASSERT( n_end[2] == 0x01 );
2604 TEST_ASSERT( n_end[3] == 0x00 );
2605 TEST_ASSERT( n_end[4] == 0x01 );
2606 TEST_ASSERT( n_end[5] == 0x02 );
2607 }
2608#endif /* MBEDTLS_RSA_C */
2609#if defined(MBEDTLS_ECP_C)
2610 if( PSA_KEY_TYPE_IS_ECC( type ) )
2611 {
2612 /* Sanity check: does this look like the beginning of a PKCS#8
2613 * elliptic curve key pair? */
2614 TEST_ASSERT( exported_length >= bits * 3 / 8 + 10 );
2615 TEST_ASSERT( exported[0] == 0x30 );
2616 }
2617#endif /* MBEDTLS_ECP_C */
2618 }
2619
Gilles Peskine818ca122018-06-20 18:16:48 +02002620 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02002621 if( ! exercise_key( slot, usage, alg ) )
2622 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02002623
2624exit:
2625 psa_destroy_key( slot );
2626 mbedtls_psa_crypto_free( );
2627}
2628/* END_CASE */