blob: fcab07bc359a4cb4c2e007e1f49bd33913d9196e [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
4#if defined(MBEDTLS_PSA_CRYPTO_SPM)
5#include "spm/psa_defs.h"
6#endif
7
Gilles Peskine0b352bc2018-06-28 00:16:11 +02008#include "mbedtls/asn1write.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +01009#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030010
11#if(UINT32_MAX > SIZE_MAX)
Gilles Peskine2d277862018-06-18 15:41:12 +020012#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX )
itayzafrir3e02b3b2018-06-12 17:06:52 +030013#else
Gilles Peskine2d277862018-06-18 15:41:12 +020014#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1
itayzafrir3e02b3b2018-06-12 17:06:52 +030015#endif
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020016
Jaeden Amerof24c7f82018-06-27 17:20:43 +010017/** An invalid export length that will never be set by psa_export_key(). */
18static const size_t INVALID_EXPORT_LENGTH = ~0U;
19
Gilles Peskined35a1cc2018-06-26 21:26:10 +020020/** Test if a buffer is all-bits zero.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020021 *
22 * \param buffer Pointer to the beginning of the buffer.
23 * \param size Size of the buffer in bytes.
24 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020025 * \return 1 if the buffer is all-bits-zero.
26 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020027 */
Gilles Peskine3f669c32018-06-21 09:21:51 +020028static int mem_is_zero( void *buffer, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020029{
30 size_t i;
31 for( i = 0; i < size; i++ )
32 {
33 if( ( (unsigned char *) buffer )[i] != 0 )
Gilles Peskine3f669c32018-06-21 09:21:51 +020034 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020035 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020036 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037}
Gilles Peskine818ca122018-06-20 18:16:48 +020038
Gilles Peskine48c0ea12018-06-21 14:15:31 +020039static int key_type_is_raw_bytes( psa_key_type_t type )
40{
41 psa_key_type_t category = type & PSA_KEY_TYPE_CATEGORY_MASK;
42 return( category == PSA_KEY_TYPE_RAW_DATA ||
43 category == PSA_KEY_TYPE_CATEGORY_SYMMETRIC );
44}
45
Gilles Peskine0b352bc2018-06-28 00:16:11 +020046/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
47static int asn1_write_10x( unsigned char **p,
48 unsigned char *start,
49 size_t bits,
50 unsigned char x )
51{
52 int ret;
53 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020054 if( bits == 0 )
55 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
56 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020057 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
58 if( *p < start || *p - start < (ssize_t) len )
59 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
60 *p -= len;
61 ( *p )[len-1] = x;
62 if( bits % 8 == 0 )
63 ( *p )[1] |= 1;
64 else
65 ( *p )[0] |= 1 << ( bits % 8 );
66 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
67 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
68 MBEDTLS_ASN1_INTEGER ) );
69 return( len );
70}
71
72static int construct_fake_rsa_key( unsigned char *buffer,
73 size_t buffer_size,
74 unsigned char **p,
75 size_t bits,
76 int keypair )
77{
78 size_t half_bits = ( bits + 1 ) / 2;
79 int ret;
80 int len = 0;
81 /* Construct something that looks like a DER encoding of
82 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
83 * RSAPrivateKey ::= SEQUENCE {
84 * version Version,
85 * modulus INTEGER, -- n
86 * publicExponent INTEGER, -- e
87 * privateExponent INTEGER, -- d
88 * prime1 INTEGER, -- p
89 * prime2 INTEGER, -- q
90 * exponent1 INTEGER, -- d mod (p-1)
91 * exponent2 INTEGER, -- d mod (q-1)
92 * coefficient INTEGER, -- (inverse of q) mod p
93 * otherPrimeInfos OtherPrimeInfos OPTIONAL
94 * }
95 * Or, for a public key, the same structure with only
96 * version, modulus and publicExponent.
97 */
98 *p = buffer + buffer_size;
99 if( keypair )
100 {
101 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
102 asn1_write_10x( p, buffer, half_bits, 1 ) );
103 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
104 asn1_write_10x( p, buffer, half_bits, 1 ) );
105 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
106 asn1_write_10x( p, buffer, half_bits, 1 ) );
107 MBEDTLS_ASN1_CHK_ADD( len, /* q */
108 asn1_write_10x( p, buffer, half_bits, 1 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
110 asn1_write_10x( p, buffer, half_bits, 3 ) );
111 MBEDTLS_ASN1_CHK_ADD( len, /* d */
112 asn1_write_10x( p, buffer, bits, 1 ) );
113 }
114 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
115 asn1_write_10x( p, buffer, 17, 1 ) );
116 MBEDTLS_ASN1_CHK_ADD( len, /* n */
117 asn1_write_10x( p, buffer, bits, 1 ) );
118 if( keypair )
119 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
120 mbedtls_asn1_write_int( p, buffer, 0 ) );
121 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
122 {
123 const unsigned char tag =
124 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
125 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
126 }
127 return( len );
128}
129
Gilles Peskine818ca122018-06-20 18:16:48 +0200130static int exercise_mac_key( psa_key_slot_t key,
131 psa_key_usage_t usage,
132 psa_algorithm_t alg )
133{
134 psa_mac_operation_t operation;
135 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200136 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200137 size_t mac_length = sizeof( mac );
138
139 if( usage & PSA_KEY_USAGE_SIGN )
140 {
141 TEST_ASSERT( psa_mac_start( &operation, key, alg ) == PSA_SUCCESS );
142 TEST_ASSERT( psa_mac_update( &operation,
143 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200144 TEST_ASSERT( psa_mac_sign_finish( &operation,
145 mac, sizeof( input ),
146 &mac_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200147 }
148
149 if( usage & PSA_KEY_USAGE_VERIFY )
150 {
151 psa_status_t verify_status =
152 ( usage & PSA_KEY_USAGE_SIGN ?
153 PSA_SUCCESS :
154 PSA_ERROR_INVALID_SIGNATURE );
155 TEST_ASSERT( psa_mac_start( &operation, key, alg ) == PSA_SUCCESS );
156 TEST_ASSERT( psa_mac_update( &operation,
157 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200158 TEST_ASSERT( psa_mac_verify_finish( &operation,
159 mac,
160 mac_length ) == verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200161 }
162
163 return( 1 );
164
165exit:
166 psa_mac_abort( &operation );
167 return( 0 );
168}
169
170static int exercise_cipher_key( psa_key_slot_t key,
171 psa_key_usage_t usage,
172 psa_algorithm_t alg )
173{
174 psa_cipher_operation_t operation;
175 unsigned char iv[16] = {0};
176 size_t iv_length = sizeof( iv );
177 const unsigned char plaintext[16] = "Hello, world...";
178 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
179 size_t ciphertext_length = sizeof( ciphertext );
180 unsigned char decrypted[sizeof( ciphertext )];
181 size_t part_length;
182
183 if( usage & PSA_KEY_USAGE_ENCRYPT )
184 {
185 TEST_ASSERT( psa_encrypt_setup( &operation, key, alg ) == PSA_SUCCESS );
186 TEST_ASSERT( psa_encrypt_generate_iv( &operation,
187 iv, sizeof( iv ),
188 &iv_length ) == PSA_SUCCESS );
189 TEST_ASSERT( psa_cipher_update( &operation,
190 plaintext, sizeof( plaintext ),
191 ciphertext, sizeof( ciphertext ),
192 &ciphertext_length ) == PSA_SUCCESS );
193 TEST_ASSERT( psa_cipher_finish( &operation,
194 ciphertext + ciphertext_length,
195 sizeof( ciphertext ) - ciphertext_length,
196 &part_length ) == PSA_SUCCESS );
197 ciphertext_length += part_length;
198 }
199
200 if( usage & PSA_KEY_USAGE_DECRYPT )
201 {
202 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700203 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200204 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
205 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200206 size_t bits;
207 TEST_ASSERT( psa_get_key_information( key, &type, &bits ) );
208 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
209 }
210 TEST_ASSERT( psa_decrypt_setup( &operation, key, alg ) == PSA_SUCCESS );
211 TEST_ASSERT( psa_encrypt_set_iv( &operation,
212 iv, iv_length ) == PSA_SUCCESS );
213 TEST_ASSERT( psa_cipher_update( &operation,
214 ciphertext, ciphertext_length,
215 decrypted, sizeof( decrypted ),
216 &part_length ) == PSA_SUCCESS );
217 status = psa_cipher_finish( &operation,
218 decrypted + part_length,
219 sizeof( decrypted ) - part_length,
220 &part_length );
221 /* For a stream cipher, all inputs are valid. For a block cipher,
222 * if the input is some aribtrary data rather than an actual
223 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700224 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700225 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine818ca122018-06-20 18:16:48 +0200226 TEST_ASSERT( status == PSA_SUCCESS );
227 else
228 TEST_ASSERT( status == PSA_SUCCESS ||
229 status == PSA_ERROR_INVALID_PADDING );
230 }
231
232 return( 1 );
233
234exit:
235 psa_cipher_abort( &operation );
236 return( 0 );
237}
238
239static int exercise_aead_key( psa_key_slot_t key,
240 psa_key_usage_t usage,
241 psa_algorithm_t alg )
242{
243 unsigned char nonce[16] = {0};
244 size_t nonce_length = sizeof( nonce );
245 unsigned char plaintext[16] = "Hello, world...";
246 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
247 size_t ciphertext_length = sizeof( ciphertext );
248 size_t plaintext_length = sizeof( ciphertext );
249
250 if( usage & PSA_KEY_USAGE_ENCRYPT )
251 {
252 TEST_ASSERT( psa_aead_encrypt( key, alg,
253 nonce, nonce_length,
254 NULL, 0,
255 plaintext, sizeof( plaintext ),
256 ciphertext, sizeof( ciphertext ),
257 &ciphertext_length ) == PSA_SUCCESS );
258 }
259
260 if( usage & PSA_KEY_USAGE_DECRYPT )
261 {
262 psa_status_t verify_status =
263 ( usage & PSA_KEY_USAGE_ENCRYPT ?
264 PSA_SUCCESS :
265 PSA_ERROR_INVALID_SIGNATURE );
266 TEST_ASSERT( psa_aead_decrypt( key, alg,
267 nonce, nonce_length,
268 NULL, 0,
269 ciphertext, ciphertext_length,
270 plaintext, sizeof( plaintext ),
271 &plaintext_length ) == verify_status );
272 }
273
274 return( 1 );
275
276exit:
277 return( 0 );
278}
279
280static int exercise_signature_key( psa_key_slot_t key,
281 psa_key_usage_t usage,
282 psa_algorithm_t alg )
283{
Gilles Peskineca45c352018-06-26 16:13:09 +0200284 unsigned char payload[16] = {1};
Gilles Peskine818ca122018-06-20 18:16:48 +0200285 size_t payload_length = sizeof( payload );
Gilles Peskine69c12672018-06-28 00:07:19 +0200286 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200287 size_t signature_length = sizeof( signature );
288
289 if( usage & PSA_KEY_USAGE_SIGN )
290 {
291 TEST_ASSERT( psa_asymmetric_sign( key, alg,
292 payload, payload_length,
293 NULL, 0,
294 signature, sizeof( signature ),
295 &signature_length ) == PSA_SUCCESS );
296 }
297
298 if( usage & PSA_KEY_USAGE_VERIFY )
299 {
300 psa_status_t verify_status =
301 ( usage & PSA_KEY_USAGE_SIGN ?
302 PSA_SUCCESS :
303 PSA_ERROR_INVALID_SIGNATURE );
304 TEST_ASSERT( psa_asymmetric_verify( key, alg,
305 payload, payload_length,
306 NULL, 0,
307 signature, signature_length ) ==
308 verify_status );
309 }
310
311 return( 1 );
312
313exit:
314 return( 0 );
315}
316
317static int exercise_asymmetric_encryption_key( psa_key_slot_t key,
318 psa_key_usage_t usage,
319 psa_algorithm_t alg )
320{
321 unsigned char plaintext[256] = "Hello, world...";
322 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
323 size_t ciphertext_length = sizeof( ciphertext );
324 size_t plaintext_length = 16;
325
326 if( usage & PSA_KEY_USAGE_ENCRYPT )
327 {
328 TEST_ASSERT(
329 psa_asymmetric_encrypt( key, alg,
330 plaintext, plaintext_length,
331 NULL, 0,
332 ciphertext, sizeof( ciphertext ),
333 &ciphertext_length ) == PSA_SUCCESS );
334 }
335
336 if( usage & PSA_KEY_USAGE_DECRYPT )
337 {
338 psa_status_t status =
339 psa_asymmetric_decrypt( key, alg,
340 ciphertext, ciphertext_length,
341 NULL, 0,
342 plaintext, sizeof( plaintext ),
343 &plaintext_length );
344 TEST_ASSERT( status == PSA_SUCCESS ||
345 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
346 ( status == PSA_ERROR_INVALID_ARGUMENT ||
347 status == PSA_ERROR_INVALID_PADDING ) ) );
348 }
349
350 return( 1 );
351
352exit:
353 return( 0 );
354}
Gilles Peskine02b75072018-07-01 22:31:34 +0200355
356static int exercise_key( psa_key_slot_t slot,
357 psa_key_usage_t usage,
358 psa_algorithm_t alg )
359{
360 int ok;
361 if( alg == 0 )
362 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
363 else if( PSA_ALG_IS_MAC( alg ) )
364 ok = exercise_mac_key( slot, usage, alg );
365 else if( PSA_ALG_IS_CIPHER( alg ) )
366 ok = exercise_cipher_key( slot, usage, alg );
367 else if( PSA_ALG_IS_AEAD( alg ) )
368 ok = exercise_aead_key( slot, usage, alg );
369 else if( PSA_ALG_IS_SIGN( alg ) )
370 ok = exercise_signature_key( slot, usage, alg );
371 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
372 ok = exercise_asymmetric_encryption_key( slot, usage, alg );
373 else
374 {
375 char message[40];
376 mbedtls_snprintf( message, sizeof( message ),
377 "No code to exercise alg=0x%08lx",
378 (unsigned long) alg );
379 test_fail( message, __LINE__, __FILE__ );
380 ok = 0;
381 }
382 return( ok );
383}
384
Gilles Peskinee59236f2018-01-27 23:32:46 +0100385/* END_HEADER */
386
387/* BEGIN_DEPENDENCIES
388 * depends_on:MBEDTLS_PSA_CRYPTO_C
389 * END_DEPENDENCIES
390 */
391
392/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200393void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100394{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100395 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100396 int i;
397 for( i = 0; i <= 1; i++ )
398 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100399 status = psa_crypto_init( );
400 TEST_ASSERT( status == PSA_SUCCESS );
401 status = psa_crypto_init( );
402 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100403 mbedtls_psa_crypto_free( );
404 }
405}
406/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100407
408/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200409void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100410{
411 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200412 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100413 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100414
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100415 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300416 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100417 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
418
Gilles Peskine4abf7412018-06-18 16:35:34 +0200419 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200420 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100421 if( status == PSA_SUCCESS )
422 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
423
424exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100425 mbedtls_psa_crypto_free( );
426}
427/* END_CASE */
428
429/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200430void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
431{
432 int slot = 1;
433 size_t bits = bits_arg;
434 psa_status_t expected_status = expected_status_arg;
435 psa_status_t status;
436 psa_key_type_t type =
437 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
438 size_t buffer_size = /* Slight overapproximations */
439 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
440 unsigned char *buffer = mbedtls_calloc( 1, buffer_size );
441 unsigned char *p;
442 int ret;
443 size_t length;
444
445 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
446 TEST_ASSERT( buffer != NULL );
447
448 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
449 bits, keypair ) ) >= 0 );
450 length = ret;
451
452 /* Try importing the key */
453 status = psa_import_key( slot, type, p, length );
454 TEST_ASSERT( status == expected_status );
455 if( status == PSA_SUCCESS )
456 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
457
458exit:
459 mbedtls_free( buffer );
460 mbedtls_psa_crypto_free( );
461}
462/* END_CASE */
463
464/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300465void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300466 int type_arg,
467 int alg_arg,
468 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100469 int expected_bits,
470 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200471 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100472 int canonical_input )
473{
474 int slot = 1;
475 int slot2 = slot + 1;
476 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200477 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200478 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100479 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100480 unsigned char *exported = NULL;
481 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100482 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100483 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100484 size_t reexported_length;
485 psa_key_type_t got_type;
486 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200487 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100488
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100489 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300490 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
491 export_size = (ssize_t) data->len + export_size_delta;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100492 exported = mbedtls_calloc( 1, export_size );
493 TEST_ASSERT( exported != NULL );
494 if( ! canonical_input )
495 {
496 reexported = mbedtls_calloc( 1, export_size );
497 TEST_ASSERT( reexported != NULL );
498 }
499 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
500
mohammad1603a97cb8c2018-03-28 03:46:26 -0700501 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200502 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700503 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
504
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100505 /* Import the key */
506 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200507 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100508
509 /* Test the key information */
510 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200511 &got_type,
512 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100513 TEST_ASSERT( got_type == type );
514 TEST_ASSERT( got_bits == (size_t) expected_bits );
515
516 /* Export the key */
517 status = psa_export_key( slot,
518 exported, export_size,
519 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200520 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100521
522 /* The exported length must be set by psa_export_key() to a value between 0
523 * and export_size. On errors, the exported length must be 0. */
524 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
525 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
526 TEST_ASSERT( exported_length <= export_size );
527
Gilles Peskine3f669c32018-06-21 09:21:51 +0200528 TEST_ASSERT( mem_is_zero( exported + exported_length,
529 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100530 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200531 {
532 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100533 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200534 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100535
536 if( canonical_input )
537 {
Gilles Peskine4abf7412018-06-18 16:35:34 +0200538 TEST_ASSERT( exported_length == data->len );
539 TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100540 }
541 else
542 {
mohammad1603a97cb8c2018-03-28 03:46:26 -0700543 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
544
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100545 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200546 exported,
547 export_size ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100548 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200549 reexported,
550 export_size,
551 &reexported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100552 TEST_ASSERT( reexported_length == exported_length );
553 TEST_ASSERT( memcmp( reexported, exported,
554 exported_length ) == 0 );
555 }
556
557destroy:
558 /* Destroy the key */
559 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
560 TEST_ASSERT( psa_get_key_information(
561 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
562
563exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300564 mbedtls_free( exported );
565 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100566 mbedtls_psa_crypto_free( );
567}
568/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100569
Moran Pekerf709f4a2018-06-06 17:26:04 +0300570/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300571void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200572 int type_arg,
573 int alg_arg,
574 int expected_bits,
575 int public_key_expected_length,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200576 int expected_export_status_arg )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300577{
578 int slot = 1;
579 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200580 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200581 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300582 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300583 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300584 size_t export_size;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100585 size_t exported_length = INVALID_EXPORT_LENGTH;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300586 psa_key_type_t got_type;
587 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200588 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300589
Moran Pekerf709f4a2018-06-06 17:26:04 +0300590 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300591 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
592 export_size = (ssize_t) data->len;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300593 exported = mbedtls_calloc( 1, export_size );
594 TEST_ASSERT( exported != NULL );
595
596 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
597
598 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200599 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300600 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
601
602 /* Import the key */
603 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200604 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300605
606 /* Test the key information */
607 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200608 &got_type,
609 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300610 TEST_ASSERT( got_type == type );
611 TEST_ASSERT( got_bits == (size_t) expected_bits );
612
613 /* Export the key */
614 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200615 exported, export_size,
616 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200617 TEST_ASSERT( status == expected_export_status );
Jaeden Amero2a671e92018-06-27 17:47:40 +0100618 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
619 TEST_ASSERT( mem_is_zero( exported + exported_length,
620 export_size - exported_length ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300621 if( status != PSA_SUCCESS )
622 goto destroy;
623
Moran Pekerf709f4a2018-06-06 17:26:04 +0300624destroy:
625 /* Destroy the key */
626 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
627 TEST_ASSERT( psa_get_key_information(
628 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
629
630exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +0300631 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300632 mbedtls_psa_crypto_free( );
633}
634/* END_CASE */
635
Gilles Peskine20035e32018-02-03 22:44:14 +0100636/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200637void import_and_exercise_key( data_t *data,
638 int type_arg,
639 int bits_arg,
640 int alg_arg )
641{
642 int slot = 1;
643 psa_key_type_t type = type_arg;
644 size_t bits = bits_arg;
645 psa_algorithm_t alg = alg_arg;
646 psa_key_usage_t usage =
647 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
648 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
649 PSA_KEY_USAGE_VERIFY :
650 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
651 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
652 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
653 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
654 PSA_KEY_USAGE_ENCRYPT :
655 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
656 0 );
657 psa_key_policy_t policy;
658 psa_key_type_t got_type;
659 size_t got_bits;
660 psa_status_t status;
661
662 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
663
664 psa_key_policy_init( &policy );
665 psa_key_policy_set_usage( &policy, usage, alg );
666 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
667
668 /* Import the key */
669 status = psa_import_key( slot, type, data->x, data->len );
670 TEST_ASSERT( status == PSA_SUCCESS );
671
672 /* Test the key information */
673 TEST_ASSERT( psa_get_key_information( slot,
674 &got_type,
675 &got_bits ) == PSA_SUCCESS );
676 TEST_ASSERT( got_type == type );
677 TEST_ASSERT( got_bits == bits );
678
679 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +0200680 if( ! exercise_key( slot, usage, alg ) )
681 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200682
683exit:
684 psa_destroy_key( slot );
685 mbedtls_psa_crypto_free( );
686}
687/* END_CASE */
688
689/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +0200690void key_policy( int usage_arg, int alg_arg )
691{
692 int key_slot = 1;
693 psa_algorithm_t alg = alg_arg;
694 psa_key_usage_t usage = usage_arg;
695 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
696 unsigned char key[32] = {0};
697 psa_key_policy_t policy_set;
698 psa_key_policy_t policy_get;
699
700 memset( key, 0x2a, sizeof( key ) );
701
702 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
703
704 psa_key_policy_init( &policy_set );
705 psa_key_policy_init( &policy_get );
706
707 psa_key_policy_set_usage( &policy_set, usage, alg );
708
709 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
710 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
711 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
712
713 TEST_ASSERT( psa_import_key( key_slot, key_type,
714 key, sizeof( key ) ) == PSA_SUCCESS );
715
716 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
717
718 TEST_ASSERT( policy_get.usage == policy_set.usage );
719 TEST_ASSERT( policy_get.alg == policy_set.alg );
720
721exit:
722 psa_destroy_key( key_slot );
723 mbedtls_psa_crypto_free( );
724}
725/* END_CASE */
726
727/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200728void mac_key_policy( int policy_usage,
729 int policy_alg,
730 int key_type,
731 data_t *key_data,
732 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200733{
734 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +0200735 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200736 psa_mac_operation_t operation;
737 psa_status_t status;
738 unsigned char mac[PSA_MAC_MAX_SIZE];
739 size_t output_length;
Gilles Peskined5b33222018-06-18 22:20:03 +0200740
741 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
742
743 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200744 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200745 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
746
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200747 TEST_ASSERT( psa_import_key( key_slot, key_type,
748 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +0200749
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200750 status = psa_mac_start( &operation, key_slot, exercise_alg );
751 if( status == PSA_SUCCESS )
Gilles Peskineacd4be32018-07-08 19:56:25 +0200752 status = psa_mac_sign_finish( &operation,
753 mac, sizeof( mac ), &output_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200754 if( policy_alg == exercise_alg &&
755 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
756 TEST_ASSERT( status == PSA_SUCCESS );
757 else
758 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
759 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200760
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200761 memset( mac, 0, sizeof( mac ) );
762 status = psa_mac_start( &operation, key_slot, exercise_alg );
763 if( status == PSA_SUCCESS )
Gilles Peskineacd4be32018-07-08 19:56:25 +0200764 status = psa_mac_verify_finish( &operation, mac, sizeof( mac ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200765 if( policy_alg == exercise_alg &&
766 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
767 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
768 else
769 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
770
771exit:
772 psa_mac_abort( &operation );
773 psa_destroy_key( key_slot );
774 mbedtls_psa_crypto_free( );
775}
776/* END_CASE */
777
778/* BEGIN_CASE */
779void cipher_key_policy( int policy_usage,
780 int policy_alg,
781 int key_type,
782 data_t *key_data,
783 int exercise_alg )
784{
785 int key_slot = 1;
786 psa_key_policy_t policy;
787 psa_cipher_operation_t operation;
788 psa_status_t status;
789
790 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
791
792 psa_key_policy_init( &policy );
793 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
794 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
795
796 TEST_ASSERT( psa_import_key( key_slot, key_type,
797 key_data->x, key_data->len ) == PSA_SUCCESS );
798
799 status = psa_encrypt_setup( &operation, key_slot, exercise_alg );
800 if( policy_alg == exercise_alg &&
801 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
802 TEST_ASSERT( status == PSA_SUCCESS );
803 else
804 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
805 psa_cipher_abort( &operation );
806
807 status = psa_decrypt_setup( &operation, key_slot, exercise_alg );
808 if( policy_alg == exercise_alg &&
809 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
810 TEST_ASSERT( status == PSA_SUCCESS );
811 else
812 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
813
814exit:
815 psa_cipher_abort( &operation );
816 psa_destroy_key( key_slot );
817 mbedtls_psa_crypto_free( );
818}
819/* END_CASE */
820
821/* BEGIN_CASE */
822void aead_key_policy( int policy_usage,
823 int policy_alg,
824 int key_type,
825 data_t *key_data,
826 int nonce_length_arg,
827 int tag_length_arg,
828 int exercise_alg )
829{
830 int key_slot = 1;
831 psa_key_policy_t policy;
832 psa_status_t status;
833 unsigned char nonce[16] = {0};
834 size_t nonce_length = nonce_length_arg;
835 unsigned char tag[16];
836 size_t tag_length = tag_length_arg;
837 size_t output_length;
838
839 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
840 TEST_ASSERT( tag_length <= sizeof( tag ) );
841
842 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
843
844 psa_key_policy_init( &policy );
845 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
846 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
847
848 TEST_ASSERT( psa_import_key( key_slot, key_type,
849 key_data->x, key_data->len ) == PSA_SUCCESS );
850
851 status = psa_aead_encrypt( key_slot, exercise_alg,
852 nonce, nonce_length,
853 NULL, 0,
854 NULL, 0,
855 tag, tag_length,
856 &output_length );
857 if( policy_alg == exercise_alg &&
858 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
859 TEST_ASSERT( status == PSA_SUCCESS );
860 else
861 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
862
863 memset( tag, 0, sizeof( tag ) );
864 status = psa_aead_decrypt( key_slot, exercise_alg,
865 nonce, nonce_length,
866 NULL, 0,
867 tag, tag_length,
868 NULL, 0,
869 &output_length );
870 if( policy_alg == exercise_alg &&
871 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
872 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
873 else
874 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
875
876exit:
877 psa_destroy_key( key_slot );
878 mbedtls_psa_crypto_free( );
879}
880/* END_CASE */
881
882/* BEGIN_CASE */
883void asymmetric_encryption_key_policy( int policy_usage,
884 int policy_alg,
885 int key_type,
886 data_t *key_data,
887 int exercise_alg )
888{
889 int key_slot = 1;
890 psa_key_policy_t policy;
891 psa_status_t status;
892 size_t key_bits;
893 size_t buffer_length;
894 unsigned char *buffer = NULL;
895 size_t output_length;
896
897 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
898
899 psa_key_policy_init( &policy );
900 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
901 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
902
903 TEST_ASSERT( psa_import_key( key_slot, key_type,
904 key_data->x, key_data->len ) == PSA_SUCCESS );
905
906 TEST_ASSERT( psa_get_key_information( key_slot,
907 NULL,
908 &key_bits ) == PSA_SUCCESS );
909 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
910 exercise_alg );
911 buffer = mbedtls_calloc( 1, buffer_length );
912 TEST_ASSERT( buffer != NULL );
913
914 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
915 NULL, 0,
916 NULL, 0,
917 buffer, buffer_length,
918 &output_length );
919 if( policy_alg == exercise_alg &&
920 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
921 TEST_ASSERT( status == PSA_SUCCESS );
922 else
923 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
924
925 memset( buffer, 0, buffer_length );
926 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
927 buffer, buffer_length,
928 NULL, 0,
929 buffer, buffer_length,
930 &output_length );
931 if( policy_alg == exercise_alg &&
932 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
933 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
934 else
935 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
936
937exit:
938 psa_destroy_key( key_slot );
939 mbedtls_psa_crypto_free( );
940 mbedtls_free( buffer );
941}
942/* END_CASE */
943
944/* BEGIN_CASE */
945void asymmetric_signature_key_policy( int policy_usage,
946 int policy_alg,
947 int key_type,
948 data_t *key_data,
949 int exercise_alg )
950{
951 int key_slot = 1;
952 psa_key_policy_t policy;
953 psa_status_t status;
954 unsigned char payload[16] = {1};
955 size_t payload_length = sizeof( payload );
956 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
957 size_t signature_length;
958
959 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
960
961 psa_key_policy_init( &policy );
962 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
963 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
964
965 TEST_ASSERT( psa_import_key( key_slot, key_type,
966 key_data->x, key_data->len ) == PSA_SUCCESS );
967
968 status = psa_asymmetric_sign( key_slot, exercise_alg,
969 payload, payload_length,
970 NULL, 0,
971 signature, sizeof( signature ),
972 &signature_length );
973 if( policy_alg == exercise_alg &&
974 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
975 TEST_ASSERT( status == PSA_SUCCESS );
976 else
977 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
978
979 memset( signature, 0, sizeof( signature ) );
980 status = psa_asymmetric_verify( key_slot, exercise_alg,
981 payload, payload_length,
982 NULL, 0,
983 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
1158 status = psa_mac_start( &operation, key_slot, alg );
1159 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 Peskine8c9def32018-02-08 10:02:12 +01001199 TEST_ASSERT( psa_mac_start( &operation, key_slot, alg ) == PSA_SUCCESS );
1200 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1201 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001202 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02001203 TEST_ASSERT( psa_mac_verify_finish( &operation,
1204 expected_mac->x,
1205 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001206
1207exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001208 psa_destroy_key( key_slot );
1209 mbedtls_psa_crypto_free( );
1210}
1211/* END_CASE */
1212
1213/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001214void cipher_setup( int key_type_arg,
1215 data_t *key,
1216 int alg_arg,
1217 int expected_status_arg )
1218{
1219 int key_slot = 1;
1220 psa_key_type_t key_type = key_type_arg;
1221 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001222 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001223 psa_cipher_operation_t operation;
1224 psa_key_policy_t policy;
1225 psa_status_t status;
1226
1227 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1228
1229 psa_key_policy_init( &policy );
1230 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1231 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1232
1233 TEST_ASSERT( psa_import_key( key_slot, key_type,
1234 key->x, key->len ) == PSA_SUCCESS );
1235
1236 status = psa_encrypt_setup( &operation, key_slot, alg );
1237 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001238 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001239
1240exit:
1241 psa_destroy_key( key_slot );
1242 mbedtls_psa_crypto_free( );
1243}
1244/* END_CASE */
1245
1246/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001247void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001248 data_t *key,
1249 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001250 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001251{
1252 int key_slot = 1;
1253 psa_status_t status;
1254 psa_key_type_t key_type = key_type_arg;
1255 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001256 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001257 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001258 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001259 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001260 size_t output_buffer_size = 0;
1261 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001262 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001263 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001264 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001265
Gilles Peskine50e586b2018-06-08 14:28:46 +02001266 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001267 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001268 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001269 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1270 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1271 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001272
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001273 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1274 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001275
1276 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1277
Moran Pekered346952018-07-05 15:22:45 +03001278 psa_key_policy_init( &policy );
1279 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1280 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1281
Gilles Peskine50e586b2018-06-08 14:28:46 +02001282 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001283 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001284
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001285 TEST_ASSERT( psa_encrypt_setup( &operation,
1286 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001287
1288 TEST_ASSERT( psa_encrypt_set_iv( &operation,
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001289 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001290 output_buffer_size = (size_t) input->len +
1291 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001292 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001293 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001294
Gilles Peskine4abf7412018-06-18 16:35:34 +02001295 TEST_ASSERT( psa_cipher_update( &operation,
1296 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001297 output, output_buffer_size,
1298 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001299 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001300 status = psa_cipher_finish( &operation,
1301 output + function_output_length,
1302 output_buffer_size,
1303 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001304 total_output_length += function_output_length;
1305
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001306 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001307 if( expected_status == PSA_SUCCESS )
1308 {
1309 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001310 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001311 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001312 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001313 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001314
Gilles Peskine50e586b2018-06-08 14:28:46 +02001315exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001316 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001317 psa_destroy_key( key_slot );
1318 mbedtls_psa_crypto_free( );
1319}
1320/* END_CASE */
1321
1322/* BEGIN_CASE */
1323void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001324 data_t *key,
1325 data_t *input,
1326 int first_part_size,
1327 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001328{
1329 int key_slot = 1;
1330 psa_key_type_t key_type = key_type_arg;
1331 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001332 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001333 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001334 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001335 size_t output_buffer_size = 0;
1336 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001337 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001338 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001339 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001340
Gilles Peskine50e586b2018-06-08 14:28:46 +02001341 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001342 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001343 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001344 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1345 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1346 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001347
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001348 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1349 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001350
1351 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1352
Moran Pekered346952018-07-05 15:22:45 +03001353 psa_key_policy_init( &policy );
1354 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1355 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1356
Gilles Peskine50e586b2018-06-08 14:28:46 +02001357 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001358 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001359
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001360 TEST_ASSERT( psa_encrypt_setup( &operation,
1361 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001362
1363 TEST_ASSERT( psa_encrypt_set_iv( &operation,
1364 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001365 output_buffer_size = (size_t) input->len +
1366 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001367 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001368 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001369
Gilles Peskine4abf7412018-06-18 16:35:34 +02001370 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001371 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001372 output, output_buffer_size,
1373 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001374 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001375 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001376 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001377 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001378 output, output_buffer_size,
1379 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001380 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001381 TEST_ASSERT( psa_cipher_finish( &operation,
1382 output + function_output_length,
1383 output_buffer_size,
1384 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001385 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001386 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1387
Gilles Peskine4abf7412018-06-18 16:35:34 +02001388 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001389 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001390 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001391
1392exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001393 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001394 psa_destroy_key( key_slot );
1395 mbedtls_psa_crypto_free( );
1396}
1397/* END_CASE */
1398
1399/* BEGIN_CASE */
1400void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001401 data_t *key,
1402 data_t *input,
1403 int first_part_size,
1404 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001405{
1406 int key_slot = 1;
1407
1408 psa_key_type_t key_type = key_type_arg;
1409 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001410 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001411 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001412 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001413 size_t output_buffer_size = 0;
1414 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001415 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001416 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001417 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001418
Gilles Peskine50e586b2018-06-08 14:28:46 +02001419 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001420 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001421 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001422 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1423 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1424 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001425
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001426 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1427 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001428
1429 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1430
Moran Pekered346952018-07-05 15:22:45 +03001431 psa_key_policy_init( &policy );
1432 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1433 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1434
Gilles Peskine50e586b2018-06-08 14:28:46 +02001435 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001436 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001437
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001438 TEST_ASSERT( psa_decrypt_setup( &operation,
1439 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001440
1441 TEST_ASSERT( psa_encrypt_set_iv( &operation,
1442 iv, sizeof( iv ) ) == PSA_SUCCESS );
1443
mohammad16033d91abe2018-07-03 13:15:54 +03001444 output_buffer_size = (size_t) input->len +
1445 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001446 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001447 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001448
Gilles Peskine4abf7412018-06-18 16:35:34 +02001449 TEST_ASSERT( (unsigned int) first_part_size < input->len );
1450 TEST_ASSERT( psa_cipher_update( &operation,
1451 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001452 output, output_buffer_size,
1453 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001454 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001455 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001456 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001457 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001458 output, output_buffer_size,
1459 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001460 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001461 TEST_ASSERT( psa_cipher_finish( &operation,
1462 output + function_output_length,
1463 output_buffer_size,
1464 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001465 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001466 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1467
Gilles Peskine4abf7412018-06-18 16:35:34 +02001468 TEST_ASSERT( total_output_length == expected_output->len );
Gilles Peskine2d277862018-06-18 15:41:12 +02001469 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001470 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001471
1472exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001473 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001474 psa_destroy_key( key_slot );
1475 mbedtls_psa_crypto_free( );
1476}
1477/* END_CASE */
1478
Gilles Peskine50e586b2018-06-08 14:28:46 +02001479/* BEGIN_CASE */
1480void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001481 data_t *key,
1482 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001483 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001484{
1485 int key_slot = 1;
1486 psa_status_t status;
1487 psa_key_type_t key_type = key_type_arg;
1488 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001489 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001490 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001491 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001492 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001493 size_t output_buffer_size = 0;
1494 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001495 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001496 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001497 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001498
Gilles Peskine50e586b2018-06-08 14:28:46 +02001499 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001500 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001501 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001502 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1503 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1504 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001505
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001506 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1507 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001508
1509 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1510
Moran Pekered346952018-07-05 15:22:45 +03001511 psa_key_policy_init( &policy );
1512 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1513 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1514
Gilles Peskine50e586b2018-06-08 14:28:46 +02001515 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001516 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001517
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001518 TEST_ASSERT( psa_decrypt_setup( &operation,
1519 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001520
1521 TEST_ASSERT( psa_encrypt_set_iv( &operation,
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001522 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001523
mohammad16033d91abe2018-07-03 13:15:54 +03001524 output_buffer_size = (size_t) input->len +
1525 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001526 output = mbedtls_calloc( 1, output_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001527 TEST_ASSERT( output != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001528
Gilles Peskine4abf7412018-06-18 16:35:34 +02001529 TEST_ASSERT( psa_cipher_update( &operation,
1530 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001531 output, output_buffer_size,
1532 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001533 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001534 status = psa_cipher_finish( &operation,
1535 output + function_output_length,
1536 output_buffer_size,
1537 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001538 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001539 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001540
1541 if( expected_status == PSA_SUCCESS )
1542 {
1543 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001544 TEST_ASSERT( total_output_length == expected_output->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001545 TEST_ASSERT( memcmp( expected_output->x, output,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001546 expected_output->len ) == 0 );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001547 }
1548
Gilles Peskine50e586b2018-06-08 14:28:46 +02001549exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001550 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001551 psa_destroy_key( key_slot );
1552 mbedtls_psa_crypto_free( );
1553}
1554/* END_CASE */
1555
Gilles Peskine50e586b2018-06-08 14:28:46 +02001556/* BEGIN_CASE */
1557void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001558 data_t *key,
1559 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02001560{
1561 int key_slot = 1;
1562 psa_key_type_t key_type = key_type_arg;
1563 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07001564 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02001565 size_t iv_size = 16;
1566 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001567 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001568 size_t output1_size = 0;
1569 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001570 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001571 size_t output2_size = 0;
1572 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001573 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001574 psa_cipher_operation_t operation1;
1575 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03001576 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001577
mohammad1603d7d7ba52018-03-12 18:51:53 +02001578 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001579 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001580 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1581 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001582
mohammad1603d7d7ba52018-03-12 18:51:53 +02001583 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1584
Moran Pekered346952018-07-05 15:22:45 +03001585 psa_key_policy_init( &policy );
1586 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
1587 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1588
mohammad1603d7d7ba52018-03-12 18:51:53 +02001589 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001590 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001591
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001592 TEST_ASSERT( psa_encrypt_setup( &operation1,
1593 key_slot, alg ) == PSA_SUCCESS );
1594 TEST_ASSERT( psa_decrypt_setup( &operation2,
1595 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001596
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001597 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
1598 iv, iv_size,
1599 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001600 output1_size = (size_t) input->len +
1601 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001602 output1 = mbedtls_calloc( 1, output1_size );
1603 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001604
Gilles Peskine4abf7412018-06-18 16:35:34 +02001605 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001606 output1, output1_size,
1607 &output1_length ) == PSA_SUCCESS );
1608 TEST_ASSERT( psa_cipher_finish( &operation1,
1609 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001610 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001611
Gilles Peskine048b7f02018-06-08 14:20:49 +02001612 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001613
1614 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1615
1616 output2_size = output1_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001617 output2 = mbedtls_calloc( 1, output2_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001618 TEST_ASSERT( output2 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001619
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001620 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
1621 iv, iv_length ) == PSA_SUCCESS );
1622 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
1623 output2, output2_size,
1624 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001625 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001626 TEST_ASSERT( psa_cipher_finish( &operation2,
1627 output2 + output2_length,
1628 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001629 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001630
Gilles Peskine048b7f02018-06-08 14:20:49 +02001631 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001632
Janos Follath25c4fa82018-07-06 16:23:25 +01001633 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001634
Gilles Peskine4abf7412018-06-18 16:35:34 +02001635 TEST_ASSERT( input->len == output2_length );
1636 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
Moran Pekerded84402018-06-06 16:36:50 +03001637
1638exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001639 mbedtls_free( output1 );
1640 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03001641 psa_destroy_key( key_slot );
1642 mbedtls_psa_crypto_free( );
1643}
1644/* END_CASE */
1645
1646/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001647void cipher_verify_output_multipart( int alg_arg,
1648 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001649 data_t *key,
1650 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001651 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03001652{
1653 int key_slot = 1;
1654 psa_key_type_t key_type = key_type_arg;
1655 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03001656 unsigned char iv[16] = {0};
1657 size_t iv_size = 16;
1658 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001659 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001660 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001661 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001662 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001663 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03001664 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02001665 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001666 psa_cipher_operation_t operation1;
1667 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03001668 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03001669
Moran Pekerded84402018-06-06 16:36:50 +03001670 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001671 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001672 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1673 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001674
Moran Pekerded84402018-06-06 16:36:50 +03001675 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1676
Moran Pekered346952018-07-05 15:22:45 +03001677 psa_key_policy_init( &policy );
1678 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
1679 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1680
Moran Pekerded84402018-06-06 16:36:50 +03001681 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001682 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001683
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001684 TEST_ASSERT( psa_encrypt_setup( &operation1,
1685 key_slot, alg ) == PSA_SUCCESS );
1686 TEST_ASSERT( psa_decrypt_setup( &operation2,
1687 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001688
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001689 TEST_ASSERT( psa_encrypt_generate_iv( &operation1,
1690 iv, iv_size,
1691 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001692 output1_buffer_size = (size_t) input->len +
1693 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine048b7f02018-06-08 14:20:49 +02001694 output1 = mbedtls_calloc( 1, output1_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001695 TEST_ASSERT( output1 != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03001696
Gilles Peskine4abf7412018-06-18 16:35:34 +02001697 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001698
itayzafrir3e02b3b2018-06-12 17:06:52 +03001699 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001700 output1, output1_buffer_size,
1701 &function_output_length ) == PSA_SUCCESS );
1702 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001703
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001704 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001705 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001706 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001707 output1, output1_buffer_size,
1708 &function_output_length ) == PSA_SUCCESS );
1709 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001710
Gilles Peskine048b7f02018-06-08 14:20:49 +02001711 TEST_ASSERT( psa_cipher_finish( &operation1,
1712 output1 + output1_length,
1713 output1_buffer_size - output1_length,
1714 &function_output_length ) == PSA_SUCCESS );
1715 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02001716
1717 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
1718
Gilles Peskine048b7f02018-06-08 14:20:49 +02001719 output2_buffer_size = output1_length;
1720 output2 = mbedtls_calloc( 1, output2_buffer_size );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001721 TEST_ASSERT( output2 != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001722
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001723 TEST_ASSERT( psa_encrypt_set_iv( &operation2,
1724 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03001725
1726 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001727 output2, output2_buffer_size,
1728 &function_output_length ) == PSA_SUCCESS );
1729 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001730
Gilles Peskine048b7f02018-06-08 14:20:49 +02001731 TEST_ASSERT( psa_cipher_update( &operation2,
1732 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001733 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001734 output2, output2_buffer_size,
1735 &function_output_length ) == PSA_SUCCESS );
1736 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03001737
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001738 TEST_ASSERT( psa_cipher_finish( &operation2,
1739 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02001740 output2_buffer_size - output2_length,
1741 &function_output_length ) == PSA_SUCCESS );
1742 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02001743
Janos Follath25c4fa82018-07-06 16:23:25 +01001744 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001745
Gilles Peskine4abf7412018-06-18 16:35:34 +02001746 TEST_ASSERT( input->len == output2_length );
1747 TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001748
1749exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001750 mbedtls_free( output1 );
1751 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02001752 psa_destroy_key( key_slot );
1753 mbedtls_psa_crypto_free( );
1754}
1755/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02001756
Gilles Peskine20035e32018-02-03 22:44:14 +01001757/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001758void aead_encrypt_decrypt( int key_type_arg,
1759 data_t * key_data,
1760 int alg_arg,
1761 data_t * input_data,
1762 data_t * nonce,
1763 data_t * additional_data,
1764 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001765{
1766 int slot = 1;
1767 psa_key_type_t key_type = key_type_arg;
1768 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001769 unsigned char *output_data = NULL;
1770 size_t output_size = 0;
1771 size_t output_length = 0;
1772 unsigned char *output_data2 = NULL;
1773 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001774 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001775 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02001776 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001777
Gilles Peskinea1cac842018-06-11 19:33:02 +02001778 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001779 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001780 TEST_ASSERT( nonce != NULL );
1781 TEST_ASSERT( additional_data != NULL );
1782 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1783 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1784 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1785 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1786
Gilles Peskine4abf7412018-06-18 16:35:34 +02001787 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001788 output_data = mbedtls_calloc( 1, output_size );
1789 TEST_ASSERT( output_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001790
1791 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1792
1793 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001794 psa_key_policy_set_usage( &policy,
1795 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
1796 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001797 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1798
1799 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001800 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001801
1802 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001803 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001804 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001805 additional_data->len,
1806 input_data->x, input_data->len,
1807 output_data, output_size,
1808 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001809
1810 if( PSA_SUCCESS == expected_result )
1811 {
1812 output_data2 = mbedtls_calloc( 1, output_length );
1813 TEST_ASSERT( output_data2 != NULL );
1814
1815 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001816 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02001817 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001818 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001819 output_data, output_length,
1820 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001821 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02001822
itayzafrir3e02b3b2018-06-12 17:06:52 +03001823 TEST_ASSERT( memcmp( input_data->x, output_data2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001824 input_data->len ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001825 }
Gilles Peskine2d277862018-06-18 15:41:12 +02001826
Gilles Peskinea1cac842018-06-11 19:33:02 +02001827exit:
1828 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001829 mbedtls_free( output_data );
1830 mbedtls_free( output_data2 );
1831 mbedtls_psa_crypto_free( );
1832}
1833/* END_CASE */
1834
1835/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001836void aead_encrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001837 int alg_arg, data_t * input_data,
1838 data_t * additional_data, data_t * nonce,
1839 data_t * expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001840{
1841 int slot = 1;
1842 psa_key_type_t key_type = key_type_arg;
1843 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001844 unsigned char *output_data = NULL;
1845 size_t output_size = 0;
1846 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001847 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001848 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001849
Gilles Peskinea1cac842018-06-11 19:33:02 +02001850 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001851 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001852 TEST_ASSERT( additional_data != NULL );
1853 TEST_ASSERT( nonce != NULL );
1854 TEST_ASSERT( expected_result != NULL );
1855 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1856 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1857 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1858 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1859 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
1860
Gilles Peskine4abf7412018-06-18 16:35:34 +02001861 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001862 output_data = mbedtls_calloc( 1, output_size );
1863 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001864
Gilles Peskinea1cac842018-06-11 19:33:02 +02001865 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1866
1867 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001868 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001869 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1870
1871 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001872 key_data->x,
1873 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001874
1875 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001876 nonce->x, nonce->len,
1877 additional_data->x, additional_data->len,
1878 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001879 output_data, output_size,
1880 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001881
itayzafrir3e02b3b2018-06-12 17:06:52 +03001882 TEST_ASSERT( memcmp( output_data, expected_result->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001883 output_length ) == 0 );
1884
Gilles Peskinea1cac842018-06-11 19:33:02 +02001885exit:
1886 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001887 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001888 mbedtls_psa_crypto_free( );
1889}
1890/* END_CASE */
1891
1892/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001893void aead_decrypt( int key_type_arg, data_t * key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001894 int alg_arg, data_t * input_data,
1895 data_t * additional_data, data_t * nonce,
1896 data_t * expected_data, int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001897{
1898 int slot = 1;
1899 psa_key_type_t key_type = key_type_arg;
1900 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001901 unsigned char *output_data = NULL;
1902 size_t output_size = 0;
1903 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001904 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02001905 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001906 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001907
Gilles Peskinea1cac842018-06-11 19:33:02 +02001908 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001909 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001910 TEST_ASSERT( additional_data != NULL );
1911 TEST_ASSERT( nonce != NULL );
1912 TEST_ASSERT( expected_data != NULL );
1913 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1914 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1915 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
1916 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
1917 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
1918
Gilles Peskine4abf7412018-06-18 16:35:34 +02001919 output_size = input_data->len + tag_length;
Gilles Peskinea1cac842018-06-11 19:33:02 +02001920 output_data = mbedtls_calloc( 1, output_size );
1921 TEST_ASSERT( output_data != NULL );
Gilles Peskine2d277862018-06-18 15:41:12 +02001922
Gilles Peskinea1cac842018-06-11 19:33:02 +02001923 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1924
1925 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001926 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001927 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1928
1929 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001930 key_data->x,
1931 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001932
1933 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001934 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001935 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001936 additional_data->len,
1937 input_data->x, input_data->len,
1938 output_data, output_size,
1939 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001940
Gilles Peskine2d277862018-06-18 15:41:12 +02001941 if( expected_result == PSA_SUCCESS )
Gilles Peskinea1cac842018-06-11 19:33:02 +02001942 {
itayzafrir3e02b3b2018-06-12 17:06:52 +03001943 TEST_ASSERT( memcmp( output_data, expected_data->x,
Gilles Peskine2d277862018-06-18 15:41:12 +02001944 output_length ) == 0 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001945 }
1946
Gilles Peskinea1cac842018-06-11 19:33:02 +02001947exit:
1948 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001949 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02001950 mbedtls_psa_crypto_free( );
1951}
1952/* END_CASE */
1953
1954/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001955void signature_size( int type_arg,
1956 int bits,
1957 int alg_arg,
1958 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001959{
1960 psa_key_type_t type = type_arg;
1961 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02001962 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01001963 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
1964exit:
1965 ;
1966}
1967/* END_CASE */
1968
1969/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001970void sign_deterministic( int key_type_arg, data_t *key_data,
1971 int alg_arg, data_t *input_data,
1972 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01001973{
1974 int slot = 1;
1975 psa_key_type_t key_type = key_type_arg;
1976 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01001977 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01001978 unsigned char *signature = NULL;
1979 size_t signature_size;
1980 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02001981 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01001982
Gilles Peskine20035e32018-02-03 22:44:14 +01001983 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001984 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01001985 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001986 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
1987 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
1988 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01001989
1990 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1991
mohammad1603a97cb8c2018-03-28 03:46:26 -07001992 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001993 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001994 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1995
Gilles Peskine20035e32018-02-03 22:44:14 +01001996 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001997 key_data->x,
1998 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01001999 TEST_ASSERT( psa_get_key_information( slot,
2000 NULL,
2001 &key_bits ) == PSA_SUCCESS );
2002
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002003 /* Allocate a buffer which has the size advertized by the
2004 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002005 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2006 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002007 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002008 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine20035e32018-02-03 22:44:14 +01002009 signature = mbedtls_calloc( 1, signature_size );
2010 TEST_ASSERT( signature != NULL );
2011
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002012 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002013 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002014 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002015 NULL, 0,
2016 signature, signature_size,
2017 &signature_length ) == PSA_SUCCESS );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002018 /* Verify that the signature is correct. */
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 NULL, 0,
2066 signature, signature_size,
2067 &signature_length );
2068 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002069 /* The value of *signature_length is unspecified on error, but
2070 * whatever it is, it should be less than signature_size, so that
2071 * if the caller tries to read *signature_length bytes without
2072 * checking the error code then they don't overflow a buffer. */
2073 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002074
2075exit:
2076 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002077 mbedtls_free( signature );
2078 mbedtls_psa_crypto_free( );
2079}
2080/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002081
2082/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002083void asymmetric_verify( int key_type_arg, data_t *key_data,
2084 int alg_arg, data_t *hash_data,
2085 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002086{
2087 int slot = 1;
2088 psa_key_type_t key_type = key_type_arg;
2089 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002090 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002091
Gilles Peskine69c12672018-06-28 00:07:19 +02002092 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2093
itayzafrir5c753392018-05-08 11:18:38 +03002094 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002095 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002096 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002097 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2098 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2099 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002100
2101 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2102
2103 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002104 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002105 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2106
2107 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002108 key_data->x,
2109 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002110
2111 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002112 hash_data->x, hash_data->len,
itayzafrir5c753392018-05-08 11:18:38 +03002113 NULL, 0,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002114 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002115 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002116exit:
2117 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002118 mbedtls_psa_crypto_free( );
2119}
2120/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002121
2122/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002123void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2124 int alg_arg, data_t *hash_data,
2125 data_t *signature_data,
2126 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002127{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002128 int slot = 1;
2129 psa_key_type_t key_type = key_type_arg;
2130 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002131 psa_status_t actual_status;
2132 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002133 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002134
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002135 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002136 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002137 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002138 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2139 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2140 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002141
2142 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2143
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002144 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002145 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002146 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2147
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002148 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002149 key_data->x,
2150 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002151
2152 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002153 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002154 NULL, 0,
2155 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002156 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002157
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002158 TEST_ASSERT( actual_status == expected_status );
2159
2160exit:
2161 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002162 mbedtls_psa_crypto_free( );
2163}
2164/* END_CASE */
2165
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002166/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002167void asymmetric_encrypt_decrypt( int key_type_arg, data_t *key_data,
2168 int alg_arg, data_t *input_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002169{
2170 int slot = 1;
2171 psa_key_type_t key_type = key_type_arg;
2172 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002173 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002174 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002175 size_t output_length = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002176 unsigned char *output2 = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002177 size_t output2_size = 0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002178 size_t output2_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002179 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002180
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002181 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002182 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002183 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2184 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2185
Gilles Peskine4abf7412018-06-18 16:35:34 +02002186 output_size = key_data->len;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002187 output2_size = output_size;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002188 output = mbedtls_calloc( 1, output_size );
2189 TEST_ASSERT( output != NULL );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002190 output2 = mbedtls_calloc( 1, output2_size );
2191 TEST_ASSERT( output2 != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002192
2193 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2194
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002195 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002196 psa_key_policy_set_usage( &policy,
2197 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002198 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002199 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2200
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002201 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002202 key_data->x,
2203 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002204
Gilles Peskineeebd7382018-06-08 18:11:54 +02002205 /* We test encryption by checking that encrypt-then-decrypt gives back
2206 * the original plaintext because of the non-optional random
2207 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02002208 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002209 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002210 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002211 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002212 &output_length ) == PSA_SUCCESS );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002213
Gilles Peskine2d277862018-06-18 15:41:12 +02002214 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002215 output, output_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02002216 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002217 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002218 &output2_length ) == PSA_SUCCESS );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002219 TEST_ASSERT( memcmp( input_data->x, output2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002220 input_data->len ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002221
2222exit:
2223 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002224 mbedtls_free( output );
2225 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002226 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002227}
2228/* END_CASE */
2229
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002230/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002231void asymmetric_encrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002232 int alg_arg, data_t *input_data,
2233 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002234{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002235 int slot = 1;
2236 psa_key_type_t key_type = key_type_arg;
2237 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002238 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002239 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002240 size_t output_length = 0;
2241 psa_status_t actual_status;
2242 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002243 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002244
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002245 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002246 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002247 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2248 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2249
Gilles Peskine4abf7412018-06-18 16:35:34 +02002250 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002251 output = mbedtls_calloc( 1, output_size );
2252 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002253
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002254 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2255
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002256 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002257 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002258 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2259
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002260 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002261 key_data->x,
2262 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002263
Gilles Peskine2d277862018-06-18 15:41:12 +02002264 actual_status = psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002265 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002266 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002267 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002268 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002269 TEST_ASSERT( actual_status == expected_status );
2270
2271exit:
2272 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002273 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002274 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002275}
2276/* END_CASE */
2277
2278/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002279void asymmetric_decrypt( int key_type_arg, data_t *key_data,
2280 int alg_arg, data_t *input_data,
2281 data_t *expected_data, int expected_size )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002282{
2283 int slot = 1;
2284 psa_key_type_t key_type = key_type_arg;
2285 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002286 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002287 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002288 size_t output_length = 0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002289 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002290
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002291 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002292 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002293 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002294 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2295 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2296 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2297
Gilles Peskine4abf7412018-06-18 16:35:34 +02002298 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002299 output = mbedtls_calloc( 1, output_size );
2300 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002301
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002302 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2303
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002304 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002305 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002306 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2307
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002308 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002309 key_data->x,
2310 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002311
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002312 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002313 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002314 NULL, 0,
2315 output,
2316 output_size,
2317 &output_length ) == PSA_SUCCESS );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002318 TEST_ASSERT( (size_t) expected_size == output_length );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002319 TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002320
2321exit:
2322 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002323 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002324 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002325}
2326/* END_CASE */
2327
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002328/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002329void asymmetric_decrypt_fail( int key_type_arg, data_t *key_data,
Gilles Peskine2d277862018-06-18 15:41:12 +02002330 int alg_arg, data_t *input_data,
2331 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002332{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002333 int slot = 1;
2334 psa_key_type_t key_type = key_type_arg;
2335 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002336 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002337 size_t output_size = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002338 size_t output_length = 0;
2339 psa_status_t actual_status;
2340 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002341 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002342
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002343 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002344 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002345 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2346 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2347
Gilles Peskine4abf7412018-06-18 16:35:34 +02002348 output_size = key_data->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002349 output = mbedtls_calloc( 1, output_size );
2350 TEST_ASSERT( output != NULL );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002351
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002352 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2353
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002354 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002355 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002356 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2357
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002358 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002359 key_data->x,
2360 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002361
Gilles Peskine2d277862018-06-18 15:41:12 +02002362 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002363 input_data->x, input_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002364 NULL, 0,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002365 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002366 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002367 TEST_ASSERT( actual_status == expected_status );
2368
2369exit:
2370 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02002371 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002372 mbedtls_psa_crypto_free( );
2373}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002374/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02002375
2376/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02002377void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02002378{
Gilles Peskinea50d7392018-06-21 10:22:13 +02002379 size_t bytes = bytes_arg;
2380 const unsigned char trail[] = "don't overwrite me";
2381 unsigned char *output = mbedtls_calloc( 1, bytes + sizeof( trail ) );
2382 unsigned char *changed = mbedtls_calloc( 1, bytes );
2383 size_t i;
2384 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02002385
Gilles Peskinea50d7392018-06-21 10:22:13 +02002386 TEST_ASSERT( output != NULL );
2387 TEST_ASSERT( changed != NULL );
2388 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02002389
2390 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2391
Gilles Peskinea50d7392018-06-21 10:22:13 +02002392 /* Run several times, to ensure that every output byte will be
2393 * nonzero at least once with overwhelming probability
2394 * (2^(-8*number_of_runs)). */
2395 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02002396 {
Gilles Peskinea50d7392018-06-21 10:22:13 +02002397 memset( output, 0, bytes );
2398 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
2399
2400 /* Check that no more than bytes have been overwritten */
2401 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
2402
2403 for( i = 0; i < bytes; i++ )
2404 {
2405 if( output[i] != 0 )
2406 ++changed[i];
2407 }
Gilles Peskine05d69892018-06-19 22:00:52 +02002408 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02002409
2410 /* Check that every byte was changed to nonzero at least once. This
2411 * validates that psa_generate_random is overwriting every byte of
2412 * the output buffer. */
2413 for( i = 0; i < bytes; i++ )
2414 {
2415 TEST_ASSERT( changed[i] != 0 );
2416 }
Gilles Peskine05d69892018-06-19 22:00:52 +02002417
2418exit:
2419 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02002420 mbedtls_free( output );
2421 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02002422}
2423/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02002424
2425/* BEGIN_CASE */
2426void generate_key( int type_arg,
2427 int bits_arg,
2428 int usage_arg,
2429 int alg_arg,
2430 int expected_status_arg )
2431{
2432 int slot = 1;
2433 psa_key_type_t type = type_arg;
2434 psa_key_usage_t usage = usage_arg;
2435 size_t bits = bits_arg;
2436 psa_algorithm_t alg = alg_arg;
2437 psa_status_t expected_status = expected_status_arg;
2438 psa_key_type_t got_type;
2439 size_t got_bits;
2440 unsigned char exported[616] = {0}; /* enough for a 1024-bit RSA key */
2441 size_t exported_length;
2442 psa_status_t expected_export_status =
2443 usage & PSA_KEY_USAGE_EXPORT ? PSA_SUCCESS : PSA_ERROR_NOT_PERMITTED;
2444 psa_status_t expected_info_status =
2445 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
2446 psa_key_policy_t policy;
2447
2448 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2449
2450 psa_key_policy_init( &policy );
2451 psa_key_policy_set_usage( &policy, usage, alg );
2452 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2453
2454 /* Generate a key */
2455 TEST_ASSERT( psa_generate_key( slot, type, bits,
2456 NULL, 0 ) == expected_status );
2457
2458 /* Test the key information */
2459 TEST_ASSERT( psa_get_key_information( slot,
2460 &got_type,
2461 &got_bits ) == expected_info_status );
2462 if( expected_info_status != PSA_SUCCESS )
2463 goto exit;
2464 TEST_ASSERT( got_type == type );
2465 TEST_ASSERT( got_bits == bits );
2466
2467 /* Export the key */
2468 TEST_ASSERT( psa_export_key( slot,
2469 exported, sizeof( exported ),
2470 &exported_length ) == expected_export_status );
2471 if( expected_export_status == PSA_SUCCESS )
2472 {
Gilles Peskine48c0ea12018-06-21 14:15:31 +02002473 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02002474 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
2475#if defined(MBEDTLS_DES_C)
2476 if( type == PSA_KEY_TYPE_DES )
2477 {
2478 /* Check the parity bits. */
2479 unsigned i;
2480 for( i = 0; i < bits / 8; i++ )
2481 {
2482 unsigned bit_count = 0;
2483 unsigned m;
2484 for( m = 1; m <= 0x100; m <<= 1 )
2485 {
2486 if( exported[i] & m )
2487 ++bit_count;
2488 }
2489 TEST_ASSERT( bit_count % 2 != 0 );
2490 }
2491 }
2492#endif
2493#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
2494 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
2495 {
2496 /* Sanity check: does this look like the beginning of a PKCS#8
2497 * RSA key pair? Assumes bits is a multiple of 8. */
2498 size_t n_bytes = bits / 8 + 1;
2499 size_t n_encoded_bytes;
2500 unsigned char *n_end;
2501 TEST_ASSERT( exported_length >= 7 + ( n_bytes + 3 ) * 9 / 2 );
2502 TEST_ASSERT( exported[0] == 0x30 );
2503 TEST_ASSERT( exported[1] == 0x82 ); // assumes >=416-bit key
2504 TEST_ASSERT( exported[4] == 0x02 );
2505 TEST_ASSERT( exported[5] == 0x01 );
2506 TEST_ASSERT( exported[6] == 0x00 );
2507 TEST_ASSERT( exported[7] == 0x02 );
2508 n_encoded_bytes = exported[8];
2509 n_end = exported + 9 + n_encoded_bytes;
2510 if( n_encoded_bytes & 0x80 )
2511 {
2512 n_encoded_bytes = ( n_encoded_bytes & 0x7f ) << 7;
2513 n_encoded_bytes |= exported[9] & 0x7f;
2514 n_end += 1;
2515 }
2516 /* The encoding of n should start with a 0 byte since it should
2517 * have its high bit set. However Mbed TLS is not compliant and
2518 * generates an invalid, but widely tolerated, encoding of
2519 * positive INTEGERs with a bit size that is a multiple of 8
2520 * with no leading 0 byte. Accept this here. */
2521 TEST_ASSERT( n_bytes == n_encoded_bytes ||
2522 n_bytes == n_encoded_bytes + 1 );
2523 if( n_bytes == n_encoded_bytes )
2524 TEST_ASSERT( exported[n_encoded_bytes <= 127 ? 9 : 10] == 0x00 );
2525 /* Sanity check: e must be 3 */
2526 TEST_ASSERT( n_end[0] == 0x02 );
2527 TEST_ASSERT( n_end[1] == 0x03 );
2528 TEST_ASSERT( n_end[2] == 0x01 );
2529 TEST_ASSERT( n_end[3] == 0x00 );
2530 TEST_ASSERT( n_end[4] == 0x01 );
2531 TEST_ASSERT( n_end[5] == 0x02 );
2532 }
2533#endif /* MBEDTLS_RSA_C */
2534#if defined(MBEDTLS_ECP_C)
2535 if( PSA_KEY_TYPE_IS_ECC( type ) )
2536 {
2537 /* Sanity check: does this look like the beginning of a PKCS#8
2538 * elliptic curve key pair? */
2539 TEST_ASSERT( exported_length >= bits * 3 / 8 + 10 );
2540 TEST_ASSERT( exported[0] == 0x30 );
2541 }
2542#endif /* MBEDTLS_ECP_C */
2543 }
2544
Gilles Peskine818ca122018-06-20 18:16:48 +02002545 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02002546 if( ! exercise_key( slot, usage, alg ) )
2547 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02002548
2549exit:
2550 psa_destroy_key( slot );
2551 mbedtls_psa_crypto_free( );
2552}
2553/* END_CASE */