blob: 528857b8ef1f33a436bc88235dc74d17e591432a [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 Peskinedd2f95b2018-08-11 01:22:42 +02008#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02009#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +020010#include "mbedtls/oid.h"
11
Gilles Peskinee59236f2018-01-27 23:32:46 +010012#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030013
Gilles Peskine96ee5c72018-07-12 17:24:54 +020014#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
15
itayzafrir3e02b3b2018-06-12 17:06:52 +030016#if(UINT32_MAX > SIZE_MAX)
Gilles Peskine2d277862018-06-18 15:41:12 +020017#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX )
itayzafrir3e02b3b2018-06-12 17:06:52 +030018#else
Gilles Peskine2d277862018-06-18 15:41:12 +020019#define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1
itayzafrir3e02b3b2018-06-12 17:06:52 +030020#endif
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020021
Jaeden Amerof24c7f82018-06-27 17:20:43 +010022/** An invalid export length that will never be set by psa_export_key(). */
23static const size_t INVALID_EXPORT_LENGTH = ~0U;
24
Gilles Peskinea7aa4422018-08-14 15:17:54 +020025/** Test if a buffer contains a constant byte value.
26 *
27 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020028 *
29 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020030 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020031 * \param size Size of the buffer in bytes.
32 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020033 * \return 1 if the buffer is all-bits-zero.
34 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020035 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020036static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037{
38 size_t i;
39 for( i = 0; i < size; i++ )
40 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020042 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020043 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020044 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020045}
Gilles Peskine818ca122018-06-20 18:16:48 +020046
Gilles Peskine0b352bc2018-06-28 00:16:11 +020047/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
48static int asn1_write_10x( unsigned char **p,
49 unsigned char *start,
50 size_t bits,
51 unsigned char x )
52{
53 int ret;
54 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020055 if( bits == 0 )
56 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
57 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020058 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030059 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020060 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
61 *p -= len;
62 ( *p )[len-1] = x;
63 if( bits % 8 == 0 )
64 ( *p )[1] |= 1;
65 else
66 ( *p )[0] |= 1 << ( bits % 8 );
67 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
68 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
69 MBEDTLS_ASN1_INTEGER ) );
70 return( len );
71}
72
73static int construct_fake_rsa_key( unsigned char *buffer,
74 size_t buffer_size,
75 unsigned char **p,
76 size_t bits,
77 int keypair )
78{
79 size_t half_bits = ( bits + 1 ) / 2;
80 int ret;
81 int len = 0;
82 /* Construct something that looks like a DER encoding of
83 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
84 * RSAPrivateKey ::= SEQUENCE {
85 * version Version,
86 * modulus INTEGER, -- n
87 * publicExponent INTEGER, -- e
88 * privateExponent INTEGER, -- d
89 * prime1 INTEGER, -- p
90 * prime2 INTEGER, -- q
91 * exponent1 INTEGER, -- d mod (p-1)
92 * exponent2 INTEGER, -- d mod (q-1)
93 * coefficient INTEGER, -- (inverse of q) mod p
94 * otherPrimeInfos OtherPrimeInfos OPTIONAL
95 * }
96 * Or, for a public key, the same structure with only
97 * version, modulus and publicExponent.
98 */
99 *p = buffer + buffer_size;
100 if( keypair )
101 {
102 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
103 asn1_write_10x( p, buffer, half_bits, 1 ) );
104 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
105 asn1_write_10x( p, buffer, half_bits, 1 ) );
106 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
107 asn1_write_10x( p, buffer, half_bits, 1 ) );
108 MBEDTLS_ASN1_CHK_ADD( len, /* q */
109 asn1_write_10x( p, buffer, half_bits, 1 ) );
110 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
111 asn1_write_10x( p, buffer, half_bits, 3 ) );
112 MBEDTLS_ASN1_CHK_ADD( len, /* d */
113 asn1_write_10x( p, buffer, bits, 1 ) );
114 }
115 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
116 asn1_write_10x( p, buffer, 17, 1 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* n */
118 asn1_write_10x( p, buffer, bits, 1 ) );
119 if( keypair )
120 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
121 mbedtls_asn1_write_int( p, buffer, 0 ) );
122 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
123 {
124 const unsigned char tag =
125 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
126 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
127 }
128 return( len );
129}
130
Gilles Peskine818ca122018-06-20 18:16:48 +0200131static int exercise_mac_key( psa_key_slot_t key,
132 psa_key_usage_t usage,
133 psa_algorithm_t alg )
134{
135 psa_mac_operation_t operation;
136 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200137 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200138 size_t mac_length = sizeof( mac );
139
140 if( usage & PSA_KEY_USAGE_SIGN )
141 {
Gilles Peskine89167cb2018-07-08 20:12:23 +0200142 TEST_ASSERT( psa_mac_sign_setup( &operation,
143 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200144 TEST_ASSERT( psa_mac_update( &operation,
145 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200146 TEST_ASSERT( psa_mac_sign_finish( &operation,
Gilles Peskineef0cb402018-07-12 16:55:59 +0200147 mac, sizeof( mac ),
Gilles Peskineacd4be32018-07-08 19:56:25 +0200148 &mac_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200149 }
150
151 if( usage & PSA_KEY_USAGE_VERIFY )
152 {
153 psa_status_t verify_status =
154 ( usage & PSA_KEY_USAGE_SIGN ?
155 PSA_SUCCESS :
156 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine89167cb2018-07-08 20:12:23 +0200157 TEST_ASSERT( psa_mac_verify_setup( &operation,
158 key, alg ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200159 TEST_ASSERT( psa_mac_update( &operation,
160 input, sizeof( input ) ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +0200161 TEST_ASSERT( psa_mac_verify_finish( &operation,
162 mac,
163 mac_length ) == verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200164 }
165
166 return( 1 );
167
168exit:
169 psa_mac_abort( &operation );
170 return( 0 );
171}
172
173static int exercise_cipher_key( psa_key_slot_t key,
174 psa_key_usage_t usage,
175 psa_algorithm_t alg )
176{
177 psa_cipher_operation_t operation;
178 unsigned char iv[16] = {0};
179 size_t iv_length = sizeof( iv );
180 const unsigned char plaintext[16] = "Hello, world...";
181 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
182 size_t ciphertext_length = sizeof( ciphertext );
183 unsigned char decrypted[sizeof( ciphertext )];
184 size_t part_length;
185
186 if( usage & PSA_KEY_USAGE_ENCRYPT )
187 {
Gilles Peskinefe119512018-07-08 21:39:34 +0200188 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
189 key, alg ) == PSA_SUCCESS );
190 TEST_ASSERT( psa_cipher_generate_iv( &operation,
191 iv, sizeof( iv ),
192 &iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200193 TEST_ASSERT( psa_cipher_update( &operation,
194 plaintext, sizeof( plaintext ),
195 ciphertext, sizeof( ciphertext ),
196 &ciphertext_length ) == PSA_SUCCESS );
197 TEST_ASSERT( psa_cipher_finish( &operation,
198 ciphertext + ciphertext_length,
199 sizeof( ciphertext ) - ciphertext_length,
200 &part_length ) == PSA_SUCCESS );
201 ciphertext_length += part_length;
202 }
203
204 if( usage & PSA_KEY_USAGE_DECRYPT )
205 {
206 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700207 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200208 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
209 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200210 size_t bits;
211 TEST_ASSERT( psa_get_key_information( key, &type, &bits ) );
212 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
213 }
Gilles Peskinefe119512018-07-08 21:39:34 +0200214 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
215 key, alg ) == PSA_SUCCESS );
216 TEST_ASSERT( psa_cipher_set_iv( &operation,
217 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine818ca122018-06-20 18:16:48 +0200218 TEST_ASSERT( psa_cipher_update( &operation,
219 ciphertext, ciphertext_length,
220 decrypted, sizeof( decrypted ),
221 &part_length ) == PSA_SUCCESS );
222 status = psa_cipher_finish( &operation,
223 decrypted + part_length,
224 sizeof( decrypted ) - part_length,
225 &part_length );
226 /* For a stream cipher, all inputs are valid. For a block cipher,
227 * if the input is some aribtrary data rather than an actual
228 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700229 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700230 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine818ca122018-06-20 18:16:48 +0200231 TEST_ASSERT( status == PSA_SUCCESS );
232 else
233 TEST_ASSERT( status == PSA_SUCCESS ||
234 status == PSA_ERROR_INVALID_PADDING );
235 }
236
237 return( 1 );
238
239exit:
240 psa_cipher_abort( &operation );
241 return( 0 );
242}
243
244static int exercise_aead_key( psa_key_slot_t key,
245 psa_key_usage_t usage,
246 psa_algorithm_t alg )
247{
248 unsigned char nonce[16] = {0};
249 size_t nonce_length = sizeof( nonce );
250 unsigned char plaintext[16] = "Hello, world...";
251 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
252 size_t ciphertext_length = sizeof( ciphertext );
253 size_t plaintext_length = sizeof( ciphertext );
254
255 if( usage & PSA_KEY_USAGE_ENCRYPT )
256 {
257 TEST_ASSERT( psa_aead_encrypt( key, alg,
258 nonce, nonce_length,
259 NULL, 0,
260 plaintext, sizeof( plaintext ),
261 ciphertext, sizeof( ciphertext ),
262 &ciphertext_length ) == PSA_SUCCESS );
263 }
264
265 if( usage & PSA_KEY_USAGE_DECRYPT )
266 {
267 psa_status_t verify_status =
268 ( usage & PSA_KEY_USAGE_ENCRYPT ?
269 PSA_SUCCESS :
270 PSA_ERROR_INVALID_SIGNATURE );
271 TEST_ASSERT( psa_aead_decrypt( key, alg,
272 nonce, nonce_length,
273 NULL, 0,
274 ciphertext, ciphertext_length,
275 plaintext, sizeof( plaintext ),
276 &plaintext_length ) == verify_status );
277 }
278
279 return( 1 );
280
281exit:
282 return( 0 );
283}
284
285static int exercise_signature_key( psa_key_slot_t key,
286 psa_key_usage_t usage,
287 psa_algorithm_t alg )
288{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200289 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
290 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200291 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200292 size_t signature_length = sizeof( signature );
293
294 if( usage & PSA_KEY_USAGE_SIGN )
295 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200296 /* Some algorithms require the payload to have the size of
297 * the hash encoded in the algorithm. Use this input size
298 * even for algorithms that allow other input sizes. */
299 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
300 if( hash_alg != 0 )
301 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200302 TEST_ASSERT( psa_asymmetric_sign( key, alg,
303 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200304 signature, sizeof( signature ),
305 &signature_length ) == PSA_SUCCESS );
306 }
307
308 if( usage & PSA_KEY_USAGE_VERIFY )
309 {
310 psa_status_t verify_status =
311 ( usage & PSA_KEY_USAGE_SIGN ?
312 PSA_SUCCESS :
313 PSA_ERROR_INVALID_SIGNATURE );
314 TEST_ASSERT( psa_asymmetric_verify( key, alg,
315 payload, payload_length,
Gilles Peskine818ca122018-06-20 18:16:48 +0200316 signature, signature_length ) ==
317 verify_status );
318 }
319
320 return( 1 );
321
322exit:
323 return( 0 );
324}
325
326static int exercise_asymmetric_encryption_key( psa_key_slot_t key,
327 psa_key_usage_t usage,
328 psa_algorithm_t alg )
329{
330 unsigned char plaintext[256] = "Hello, world...";
331 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
332 size_t ciphertext_length = sizeof( ciphertext );
333 size_t plaintext_length = 16;
334
335 if( usage & PSA_KEY_USAGE_ENCRYPT )
336 {
337 TEST_ASSERT(
338 psa_asymmetric_encrypt( key, alg,
339 plaintext, plaintext_length,
340 NULL, 0,
341 ciphertext, sizeof( ciphertext ),
342 &ciphertext_length ) == PSA_SUCCESS );
343 }
344
345 if( usage & PSA_KEY_USAGE_DECRYPT )
346 {
347 psa_status_t status =
348 psa_asymmetric_decrypt( key, alg,
349 ciphertext, ciphertext_length,
350 NULL, 0,
351 plaintext, sizeof( plaintext ),
352 &plaintext_length );
353 TEST_ASSERT( status == PSA_SUCCESS ||
354 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
355 ( status == PSA_ERROR_INVALID_ARGUMENT ||
356 status == PSA_ERROR_INVALID_PADDING ) ) );
357 }
358
359 return( 1 );
360
361exit:
362 return( 0 );
363}
Gilles Peskine02b75072018-07-01 22:31:34 +0200364
Gilles Peskineea0fb492018-07-12 17:17:20 +0200365static int exercise_key_derivation_key( psa_key_slot_t key,
366 psa_key_usage_t usage,
367 psa_algorithm_t alg )
368{
369 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
370 unsigned char label[16] = "This is a label.";
371 size_t label_length = sizeof( label );
372 unsigned char seed[16] = "abcdefghijklmnop";
373 size_t seed_length = sizeof( seed );
374 unsigned char output[1];
375
376 if( usage & PSA_KEY_USAGE_DERIVE )
377 {
378 TEST_ASSERT( psa_key_derivation( &generator,
379 key, alg,
380 label, label_length,
381 seed, seed_length,
382 sizeof( output ) ) == PSA_SUCCESS );
383 TEST_ASSERT( psa_generator_read( &generator,
384 output,
385 sizeof( output ) ) == PSA_SUCCESS );
386 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
387 }
388
389 return( 1 );
390
391exit:
392 return( 0 );
393}
394
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200395static int is_oid_of_key_type( psa_key_type_t type,
396 const uint8_t *oid, size_t oid_length )
397{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200398 const uint8_t *expected_oid = NULL;
399 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200400#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200401 if( PSA_KEY_TYPE_IS_RSA( type ) )
402 {
403 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
404 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
405 }
406 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200407#endif /* MBEDTLS_RSA_C */
408#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200409 if( PSA_KEY_TYPE_IS_ECC( type ) )
410 {
411 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
412 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
413 }
414 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200415#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200416 {
417 char message[40];
418 mbedtls_snprintf( message, sizeof( message ),
419 "OID not known for key type=0x%08lx",
420 (unsigned long) type );
421 test_fail( message, __LINE__, __FILE__ );
422 return( 0 );
423 }
424
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200425 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200426 return( 1 );
427
428exit:
429 return( 0 );
430}
431
432static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
433 size_t min_bits, size_t max_bits,
434 int must_be_odd )
435{
436 size_t len;
437 size_t actual_bits;
438 unsigned char msb;
439 TEST_ASSERT( mbedtls_asn1_get_tag( p, end, &len,
440 MBEDTLS_ASN1_INTEGER ) == 0 );
441 /* Tolerate a slight departure from DER encoding:
442 * - 0 may be represented by an empty string or a 1-byte string.
443 * - The sign bit may be used as a value bit. */
444 if( ( len == 1 && ( *p )[0] == 0 ) ||
445 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
446 {
447 ++( *p );
448 --len;
449 }
450 if( min_bits == 0 && len == 0 )
451 return( 1 );
452 msb = ( *p )[0];
453 TEST_ASSERT( msb != 0 );
454 actual_bits = 8 * ( len - 1 );
455 while( msb != 0 )
456 {
457 msb >>= 1;
458 ++actual_bits;
459 }
460 TEST_ASSERT( actual_bits >= min_bits );
461 TEST_ASSERT( actual_bits <= max_bits );
462 if( must_be_odd )
463 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
464 *p += len;
465 return( 1 );
466exit:
467 return( 0 );
468}
469
470static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
471 size_t *len,
472 unsigned char n, unsigned char tag )
473{
474 int ret;
475 ret = mbedtls_asn1_get_tag( p, end, len,
476 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
477 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
478 if( ret != 0 )
479 return( ret );
480 end = *p + *len;
481 ret = mbedtls_asn1_get_tag( p, end, len, tag );
482 if( ret != 0 )
483 return( ret );
484 if( *p + *len != end )
485 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
486 return( 0 );
487}
488
489static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
490 uint8_t *exported, size_t exported_length )
491{
492 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200493 TEST_ASSERT( exported_length == ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200494 else
495 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200496
497#if defined(MBEDTLS_DES_C)
498 if( type == PSA_KEY_TYPE_DES )
499 {
500 /* Check the parity bits. */
501 unsigned i;
502 for( i = 0; i < bits / 8; i++ )
503 {
504 unsigned bit_count = 0;
505 unsigned m;
506 for( m = 1; m <= 0x100; m <<= 1 )
507 {
508 if( exported[i] & m )
509 ++bit_count;
510 }
511 TEST_ASSERT( bit_count % 2 != 0 );
512 }
513 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200514 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200515#endif
516
517#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
518 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
519 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200520 uint8_t *p = exported;
521 uint8_t *end = exported + exported_length;
522 size_t len;
523 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200524 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200525 * modulus INTEGER, -- n
526 * publicExponent INTEGER, -- e
527 * privateExponent INTEGER, -- d
528 * prime1 INTEGER, -- p
529 * prime2 INTEGER, -- q
530 * exponent1 INTEGER, -- d mod (p-1)
531 * exponent2 INTEGER, -- d mod (q-1)
532 * coefficient INTEGER, -- (inverse of q) mod p
533 * }
534 */
535 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
536 MBEDTLS_ASN1_SEQUENCE |
537 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
538 TEST_ASSERT( p + len == end );
539 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
540 goto exit;
541 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
542 goto exit;
543 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
544 goto exit;
545 /* Require d to be at least half the size of n. */
546 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
547 goto exit;
548 /* Require p and q to be at most half the size of n, rounded up. */
549 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
550 goto exit;
551 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
552 goto exit;
553 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
554 goto exit;
555 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
556 goto exit;
557 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
558 goto exit;
559 TEST_ASSERT( p == end );
560 }
561 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200562#endif /* MBEDTLS_RSA_C */
563
564#if defined(MBEDTLS_ECP_C)
565 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
566 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200567 uint8_t *p = exported;
568 uint8_t *end = exported + exported_length;
569 size_t len;
570 int version;
571 /* ECPrivateKey ::= SEQUENCE {
572 * version INTEGER, -- must be 1
573 * privateKey OCTET STRING,
574 * -- `ceiling(log_{256}(n))`-byte string, big endian,
575 * -- where n is the order of the curve.
576 * parameters ECParameters {{ NamedCurve }}, -- mandatory
577 * publicKey BIT STRING -- mandatory
578 * }
579 */
580 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
581 MBEDTLS_ASN1_SEQUENCE |
582 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
583 TEST_ASSERT( p + len == end );
584 TEST_ASSERT( mbedtls_asn1_get_int( &p, end, &version ) == 0 );
585 TEST_ASSERT( version == 1 );
586 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
587 MBEDTLS_ASN1_OCTET_STRING ) == 0 );
588 /* Bug in Mbed TLS: the length of the octet string depends on the value */
589 // TEST_ASSERT( len == PSA_BITS_TO_BYTES( bits ) );
590 p += len;
591 TEST_ASSERT( asn1_get_implicit_tag( &p, end, &len, 0,
592 MBEDTLS_ASN1_OID ) == 0 );
593 p += len;
Gilles Peskinec6290c02018-08-13 17:24:59 +0200594 /* publicKey: ECPoint in uncompressed representation (as below) */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200595 TEST_ASSERT( asn1_get_implicit_tag( &p, end, &len, 1,
596 MBEDTLS_ASN1_BIT_STRING ) == 0 );
597 TEST_ASSERT( p + len == end );
598 TEST_ASSERT( p[0] == 0 ); /* 0 unused bits in the bit string */
599 ++p;
600 TEST_ASSERT( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ) == end );
601 TEST_ASSERT( p[0] == 4 );
602 }
603 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200604#endif /* MBEDTLS_ECP_C */
605
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200606 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
607 {
608 uint8_t *p = exported;
609 uint8_t *end = exported + exported_length;
610 size_t len;
611 mbedtls_asn1_buf alg;
612 mbedtls_asn1_buf params;
613 mbedtls_asn1_bitstring bitstring;
614 /* SubjectPublicKeyInfo ::= SEQUENCE {
615 * algorithm AlgorithmIdentifier,
616 * subjectPublicKey BIT STRING }
617 * AlgorithmIdentifier ::= SEQUENCE {
618 * algorithm OBJECT IDENTIFIER,
619 * parameters ANY DEFINED BY algorithm OPTIONAL }
620 */
621 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
622 MBEDTLS_ASN1_SEQUENCE |
623 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
624 TEST_ASSERT( p + len == end );
625 TEST_ASSERT( mbedtls_asn1_get_alg( &p, end, &alg, &params ) == 0 );
626 if( ! is_oid_of_key_type( type, alg.p, alg.len ) )
627 goto exit;
628 TEST_ASSERT( mbedtls_asn1_get_bitstring( &p, end, &bitstring ) == 0 );
629 TEST_ASSERT( p == end );
630 p = bitstring.p;
631#if defined(MBEDTLS_RSA_C)
632 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
633 {
634 /* RSAPublicKey ::= SEQUENCE {
635 * modulus INTEGER, -- n
636 * publicExponent INTEGER } -- e
637 */
638 TEST_ASSERT( bitstring.unused_bits == 0 );
639 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
640 MBEDTLS_ASN1_SEQUENCE |
641 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
642 TEST_ASSERT( p + len == end );
643 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
644 goto exit;
645 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
646 goto exit;
647 TEST_ASSERT( p == end );
648 }
649 else
650#endif /* MBEDTLS_RSA_C */
651#if defined(MBEDTLS_ECP_C)
652 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
653 {
654 /* ECPoint ::= ...
Gilles Peskinec6290c02018-08-13 17:24:59 +0200655 * -- first 8 bits: 0x04 (uncompressed representation);
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200656 * -- then x_P as an n-bit string, big endian;
657 * -- then y_P as a n-bit string, big endian,
658 * -- where n is the order of the curve.
659 */
660 TEST_ASSERT( bitstring.unused_bits == 0 );
661 TEST_ASSERT( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ) == end );
662 TEST_ASSERT( p[0] == 4 );
663 }
664 else
665#endif /* MBEDTLS_ECP_C */
666 {
667 char message[40];
668 mbedtls_snprintf( message, sizeof( message ),
669 "No sanity check for public key type=0x%08lx",
670 (unsigned long) type );
671 test_fail( message, __LINE__, __FILE__ );
672 return( 0 );
673 }
674 }
675 else
676
677 {
678 /* No sanity checks for other types */
679 }
680
681 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200682
683exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200684 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200685}
686
687static int exercise_export_key( psa_key_slot_t slot,
688 psa_key_usage_t usage )
689{
690 psa_key_type_t type;
691 size_t bits;
692 uint8_t *exported = NULL;
693 size_t exported_size = 0;
694 size_t exported_length = 0;
695 int ok = 0;
696
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200697 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
698
699 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
700 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200701 {
702 TEST_ASSERT( psa_export_key( slot, NULL, 0, &exported_length ) ==
703 PSA_ERROR_NOT_PERMITTED );
704 return( 1 );
705 }
706
Gilles Peskined14664a2018-08-10 19:07:32 +0200707 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200708 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200709
710 TEST_ASSERT( psa_export_key( slot,
711 exported, exported_size,
712 &exported_length ) == PSA_SUCCESS );
713 ok = exported_key_sanity_check( type, bits, exported, exported_length );
714
715exit:
716 mbedtls_free( exported );
717 return( ok );
718}
719
720static int exercise_export_public_key( psa_key_slot_t slot )
721{
722 psa_key_type_t type;
723 psa_key_type_t public_type;
724 size_t bits;
725 uint8_t *exported = NULL;
726 size_t exported_size = 0;
727 size_t exported_length = 0;
728 int ok = 0;
729
730 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
731 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
732 {
733 TEST_ASSERT( psa_export_public_key( slot,
734 NULL, 0, &exported_length ) ==
735 PSA_ERROR_INVALID_ARGUMENT );
736 return( 1 );
737 }
738
739 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
740 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200741 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200742
743 TEST_ASSERT( psa_export_public_key( slot,
744 exported, exported_size,
745 &exported_length ) == PSA_SUCCESS );
746 ok = exported_key_sanity_check( public_type, bits,
747 exported, exported_length );
748
749exit:
750 mbedtls_free( exported );
751 return( ok );
752}
753
Gilles Peskine02b75072018-07-01 22:31:34 +0200754static int exercise_key( psa_key_slot_t slot,
755 psa_key_usage_t usage,
756 psa_algorithm_t alg )
757{
758 int ok;
759 if( alg == 0 )
760 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
761 else if( PSA_ALG_IS_MAC( alg ) )
762 ok = exercise_mac_key( slot, usage, alg );
763 else if( PSA_ALG_IS_CIPHER( alg ) )
764 ok = exercise_cipher_key( slot, usage, alg );
765 else if( PSA_ALG_IS_AEAD( alg ) )
766 ok = exercise_aead_key( slot, usage, alg );
767 else if( PSA_ALG_IS_SIGN( alg ) )
768 ok = exercise_signature_key( slot, usage, alg );
769 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
770 ok = exercise_asymmetric_encryption_key( slot, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200771 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
772 ok = exercise_key_derivation_key( slot, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200773 else
774 {
775 char message[40];
776 mbedtls_snprintf( message, sizeof( message ),
777 "No code to exercise alg=0x%08lx",
778 (unsigned long) alg );
779 test_fail( message, __LINE__, __FILE__ );
780 ok = 0;
781 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200782
783 ok = ok && exercise_export_key( slot, usage );
784 ok = ok && exercise_export_public_key( slot );
785
Gilles Peskine02b75072018-07-01 22:31:34 +0200786 return( ok );
787}
788
Gilles Peskinee59236f2018-01-27 23:32:46 +0100789/* END_HEADER */
790
791/* BEGIN_DEPENDENCIES
792 * depends_on:MBEDTLS_PSA_CRYPTO_C
793 * END_DEPENDENCIES
794 */
795
796/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200797void static_checks( )
798{
799 size_t max_truncated_mac_size =
800 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
801
802 /* Check that the length for a truncated MAC always fits in the algorithm
803 * encoding. The shifted mask is the maximum truncated value. The
804 * untruncated algorithm may be one byte larger. */
805 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
806}
807/* END_CASE */
808
809/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200810void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100811{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100812 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100813 int i;
814 for( i = 0; i <= 1; i++ )
815 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100816 status = psa_crypto_init( );
817 TEST_ASSERT( status == PSA_SUCCESS );
818 status = psa_crypto_init( );
819 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100820 mbedtls_psa_crypto_free( );
821 }
822}
823/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100824
825/* BEGIN_CASE */
Gilles Peskine996deb12018-08-01 15:45:45 +0200826void fill_slots( int max_arg )
827{
828 /* Fill all the slots until we run out of memory or out of slots,
829 * or until some limit specified in the test data for the sake of
830 * implementations with an essentially unlimited number of slots.
831 * This test assumes that available slots are numbered from 1. */
832
833 psa_key_slot_t slot;
834 psa_key_slot_t max = 0;
835 psa_key_policy_t policy;
836 uint8_t exported[sizeof( max )];
837 size_t exported_size;
838 psa_status_t status;
839
840 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
841
842 psa_key_policy_init( &policy );
843 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
844
845 for( max = 1; max <= (size_t) max_arg; max++ )
846 {
847 status = psa_set_key_policy( max, &policy );
848 /* Stop filling slots if we run out of memory or out of
849 * available slots. */
850 TEST_ASSERT( status == PSA_SUCCESS ||
851 status == PSA_ERROR_INSUFFICIENT_MEMORY ||
852 status == PSA_ERROR_INVALID_ARGUMENT );
853 if( status != PSA_SUCCESS )
854 break;
855 status = psa_import_key( max, PSA_KEY_TYPE_RAW_DATA,
856 (uint8_t*) &max, sizeof( max ) );
857 /* Since psa_set_key_policy succeeded, we know that the slot
858 * number is valid. But we may legitimately run out of memory. */
859 TEST_ASSERT( status == PSA_SUCCESS ||
860 status == PSA_ERROR_INSUFFICIENT_MEMORY );
861 if( status != PSA_SUCCESS )
862 break;
863 }
864 /* `max` is now the first slot number that wasn't filled. */
865 max -= 1;
866
867 for( slot = 1; slot <= max; slot++ )
868 {
869 TEST_ASSERT( psa_export_key( slot,
870 exported, sizeof( exported ),
871 &exported_size ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200872 ASSERT_COMPARE( &slot, sizeof( slot ), exported, exported_size );
Gilles Peskine996deb12018-08-01 15:45:45 +0200873 }
874
875exit:
Gilles Peskine9a056342018-08-01 15:46:54 +0200876 /* Do not destroy the keys. mbedtls_psa_crypto_free() should do it. */
Gilles Peskine996deb12018-08-01 15:45:45 +0200877 mbedtls_psa_crypto_free( );
878}
879/* END_CASE */
880
881/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200882void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100883{
884 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200885 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100886 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100887
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100888 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300889 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100890 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
891
Gilles Peskine4abf7412018-06-18 16:35:34 +0200892 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200893 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100894 if( status == PSA_SUCCESS )
895 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
896
897exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100898 mbedtls_psa_crypto_free( );
899}
900/* END_CASE */
901
902/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200903void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
904{
905 int slot = 1;
906 size_t bits = bits_arg;
907 psa_status_t expected_status = expected_status_arg;
908 psa_status_t status;
909 psa_key_type_t type =
910 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
911 size_t buffer_size = /* Slight overapproximations */
912 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200913 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200914 unsigned char *p;
915 int ret;
916 size_t length;
917
918 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200919 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200920
921 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
922 bits, keypair ) ) >= 0 );
923 length = ret;
924
925 /* Try importing the key */
926 status = psa_import_key( slot, type, p, length );
927 TEST_ASSERT( status == expected_status );
928 if( status == PSA_SUCCESS )
929 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
930
931exit:
932 mbedtls_free( buffer );
933 mbedtls_psa_crypto_free( );
934}
935/* END_CASE */
936
937/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300938void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300939 int type_arg,
940 int alg_arg,
941 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100942 int expected_bits,
943 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200944 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100945 int canonical_input )
946{
947 int slot = 1;
948 int slot2 = slot + 1;
949 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200950 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200951 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100952 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100953 unsigned char *exported = NULL;
954 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100955 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100956 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100957 size_t reexported_length;
958 psa_key_type_t got_type;
959 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200960 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100961
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100962 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300963 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +0300964 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200965 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100966 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200967 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100968 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
969
mohammad1603a97cb8c2018-03-28 03:46:26 -0700970 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200971 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700972 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
973
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100974 /* Import the key */
975 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200976 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100977
978 /* Test the key information */
979 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200980 &got_type,
981 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100982 TEST_ASSERT( got_type == type );
983 TEST_ASSERT( got_bits == (size_t) expected_bits );
984
985 /* Export the key */
986 status = psa_export_key( slot,
987 exported, export_size,
988 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200989 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100990
991 /* The exported length must be set by psa_export_key() to a value between 0
992 * and export_size. On errors, the exported length must be 0. */
993 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
994 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
995 TEST_ASSERT( exported_length <= export_size );
996
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200997 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200998 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100999 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001000 {
1001 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001002 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001003 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001004
Gilles Peskine8f609232018-08-11 01:24:55 +02001005 if( ! exercise_export_key( slot, usage_arg ) )
1006 goto exit;
1007
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001008 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001009 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001010 else
1011 {
mohammad1603a97cb8c2018-03-28 03:46:26 -07001012 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
1013
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001014 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001015 exported,
Gilles Peskineb67f3082018-08-07 15:33:10 +02001016 exported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001017 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001018 reexported,
1019 export_size,
1020 &reexported_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001021 ASSERT_COMPARE( exported, exported_length,
1022 reexported, reexported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001023 }
1024
1025destroy:
1026 /* Destroy the key */
1027 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1028 TEST_ASSERT( psa_get_key_information(
1029 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1030
1031exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001032 mbedtls_free( exported );
1033 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001034 mbedtls_psa_crypto_free( );
1035}
1036/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001037
Moran Pekerf709f4a2018-06-06 17:26:04 +03001038/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001039void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001040 int type_arg,
1041 int alg_arg,
1042 int expected_bits,
1043 int public_key_expected_length,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001044 int expected_export_status_arg )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001045{
1046 int slot = 1;
1047 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001048 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001049 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001050 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001051 unsigned char *exported = NULL;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001052 size_t export_size;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001053 size_t exported_length = INVALID_EXPORT_LENGTH;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001054 psa_key_type_t got_type;
1055 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +02001056 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001057
Moran Pekerf709f4a2018-06-06 17:26:04 +03001058 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001059 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +03001060 export_size = (ptrdiff_t) data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001061 ASSERT_ALLOC( exported, export_size );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001062
1063 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1064
1065 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001066 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001067 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1068
1069 /* Import the key */
1070 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001071 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001072
1073 /* Test the key information */
1074 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001075 &got_type,
1076 &got_bits ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001077 TEST_ASSERT( got_type == type );
1078 TEST_ASSERT( got_bits == (size_t) expected_bits );
1079
1080 /* Export the key */
1081 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001082 exported, export_size,
1083 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001084 TEST_ASSERT( status == expected_export_status );
Jaeden Amero2a671e92018-06-27 17:47:40 +01001085 TEST_ASSERT( exported_length == (size_t) public_key_expected_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001086 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Jaeden Amero2a671e92018-06-27 17:47:40 +01001087 export_size - exported_length ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001088 if( status != PSA_SUCCESS )
1089 goto destroy;
1090
Moran Pekerf709f4a2018-06-06 17:26:04 +03001091destroy:
1092 /* Destroy the key */
1093 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1094 TEST_ASSERT( psa_get_key_information(
1095 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
1096
1097exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001098 mbedtls_free( exported );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001099 mbedtls_psa_crypto_free( );
1100}
1101/* END_CASE */
1102
Gilles Peskine20035e32018-02-03 22:44:14 +01001103/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001104void import_and_exercise_key( data_t *data,
1105 int type_arg,
1106 int bits_arg,
1107 int alg_arg )
1108{
1109 int slot = 1;
1110 psa_key_type_t type = type_arg;
1111 size_t bits = bits_arg;
1112 psa_algorithm_t alg = alg_arg;
1113 psa_key_usage_t usage =
1114 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
1115 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1116 PSA_KEY_USAGE_VERIFY :
1117 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
1118 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1119 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
1120 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1121 PSA_KEY_USAGE_ENCRYPT :
1122 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
Gilles Peskineea0fb492018-07-12 17:17:20 +02001123 PSA_ALG_IS_KEY_DERIVATION( alg ) ? PSA_KEY_USAGE_DERIVE :
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001124 0 );
1125 psa_key_policy_t policy;
1126 psa_key_type_t got_type;
1127 size_t got_bits;
1128 psa_status_t status;
1129
1130 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1131
1132 psa_key_policy_init( &policy );
1133 psa_key_policy_set_usage( &policy, usage, alg );
1134 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1135
1136 /* Import the key */
1137 status = psa_import_key( slot, type, data->x, data->len );
1138 TEST_ASSERT( status == PSA_SUCCESS );
1139
1140 /* Test the key information */
1141 TEST_ASSERT( psa_get_key_information( slot,
1142 &got_type,
1143 &got_bits ) == PSA_SUCCESS );
1144 TEST_ASSERT( got_type == type );
1145 TEST_ASSERT( got_bits == bits );
1146
1147 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02001148 if( ! exercise_key( slot, usage, alg ) )
1149 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001150
1151exit:
1152 psa_destroy_key( slot );
1153 mbedtls_psa_crypto_free( );
1154}
1155/* END_CASE */
1156
1157/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001158void key_policy( int usage_arg, int alg_arg )
1159{
1160 int key_slot = 1;
1161 psa_algorithm_t alg = alg_arg;
1162 psa_key_usage_t usage = usage_arg;
1163 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1164 unsigned char key[32] = {0};
1165 psa_key_policy_t policy_set;
1166 psa_key_policy_t policy_get;
1167
1168 memset( key, 0x2a, sizeof( key ) );
1169
1170 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1171
1172 psa_key_policy_init( &policy_set );
1173 psa_key_policy_init( &policy_get );
1174
1175 psa_key_policy_set_usage( &policy_set, usage, alg );
1176
1177 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
1178 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
1179 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
1180
1181 TEST_ASSERT( psa_import_key( key_slot, key_type,
1182 key, sizeof( key ) ) == PSA_SUCCESS );
1183
1184 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
1185
1186 TEST_ASSERT( policy_get.usage == policy_set.usage );
1187 TEST_ASSERT( policy_get.alg == policy_set.alg );
1188
1189exit:
1190 psa_destroy_key( key_slot );
1191 mbedtls_psa_crypto_free( );
1192}
1193/* END_CASE */
1194
1195/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001196void mac_key_policy( int policy_usage,
1197 int policy_alg,
1198 int key_type,
1199 data_t *key_data,
1200 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001201{
1202 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +02001203 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001204 psa_mac_operation_t operation;
1205 psa_status_t status;
1206 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001207
1208 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1209
1210 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001211 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001212 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1213
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001214 TEST_ASSERT( psa_import_key( key_slot, key_type,
1215 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001216
Gilles Peskine89167cb2018-07-08 20:12:23 +02001217 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001218 if( policy_alg == exercise_alg &&
1219 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1220 TEST_ASSERT( status == PSA_SUCCESS );
1221 else
1222 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1223 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001224
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001225 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001226 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001227 if( policy_alg == exercise_alg &&
1228 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +02001229 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001230 else
1231 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1232
1233exit:
1234 psa_mac_abort( &operation );
1235 psa_destroy_key( key_slot );
1236 mbedtls_psa_crypto_free( );
1237}
1238/* END_CASE */
1239
1240/* BEGIN_CASE */
1241void cipher_key_policy( int policy_usage,
1242 int policy_alg,
1243 int key_type,
1244 data_t *key_data,
1245 int exercise_alg )
1246{
1247 int key_slot = 1;
1248 psa_key_policy_t policy;
1249 psa_cipher_operation_t operation;
1250 psa_status_t status;
1251
1252 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1253
1254 psa_key_policy_init( &policy );
1255 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1256 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1257
1258 TEST_ASSERT( psa_import_key( key_slot, key_type,
1259 key_data->x, key_data->len ) == PSA_SUCCESS );
1260
Gilles Peskinefe119512018-07-08 21:39:34 +02001261 status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001262 if( policy_alg == exercise_alg &&
1263 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1264 TEST_ASSERT( status == PSA_SUCCESS );
1265 else
1266 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1267 psa_cipher_abort( &operation );
1268
Gilles Peskinefe119512018-07-08 21:39:34 +02001269 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001270 if( policy_alg == exercise_alg &&
1271 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1272 TEST_ASSERT( status == PSA_SUCCESS );
1273 else
1274 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1275
1276exit:
1277 psa_cipher_abort( &operation );
1278 psa_destroy_key( key_slot );
1279 mbedtls_psa_crypto_free( );
1280}
1281/* END_CASE */
1282
1283/* BEGIN_CASE */
1284void aead_key_policy( int policy_usage,
1285 int policy_alg,
1286 int key_type,
1287 data_t *key_data,
1288 int nonce_length_arg,
1289 int tag_length_arg,
1290 int exercise_alg )
1291{
1292 int key_slot = 1;
1293 psa_key_policy_t policy;
1294 psa_status_t status;
1295 unsigned char nonce[16] = {0};
1296 size_t nonce_length = nonce_length_arg;
1297 unsigned char tag[16];
1298 size_t tag_length = tag_length_arg;
1299 size_t output_length;
1300
1301 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1302 TEST_ASSERT( tag_length <= sizeof( tag ) );
1303
1304 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1305
1306 psa_key_policy_init( &policy );
1307 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1308 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1309
1310 TEST_ASSERT( psa_import_key( key_slot, key_type,
1311 key_data->x, key_data->len ) == PSA_SUCCESS );
1312
1313 status = psa_aead_encrypt( key_slot, exercise_alg,
1314 nonce, nonce_length,
1315 NULL, 0,
1316 NULL, 0,
1317 tag, tag_length,
1318 &output_length );
1319 if( policy_alg == exercise_alg &&
1320 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1321 TEST_ASSERT( status == PSA_SUCCESS );
1322 else
1323 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1324
1325 memset( tag, 0, sizeof( tag ) );
1326 status = psa_aead_decrypt( key_slot, exercise_alg,
1327 nonce, nonce_length,
1328 NULL, 0,
1329 tag, tag_length,
1330 NULL, 0,
1331 &output_length );
1332 if( policy_alg == exercise_alg &&
1333 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1334 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1335 else
1336 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1337
1338exit:
1339 psa_destroy_key( key_slot );
1340 mbedtls_psa_crypto_free( );
1341}
1342/* END_CASE */
1343
1344/* BEGIN_CASE */
1345void asymmetric_encryption_key_policy( int policy_usage,
1346 int policy_alg,
1347 int key_type,
1348 data_t *key_data,
1349 int exercise_alg )
1350{
1351 int key_slot = 1;
1352 psa_key_policy_t policy;
1353 psa_status_t status;
1354 size_t key_bits;
1355 size_t buffer_length;
1356 unsigned char *buffer = NULL;
1357 size_t output_length;
1358
1359 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1360
1361 psa_key_policy_init( &policy );
1362 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1363 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1364
1365 TEST_ASSERT( psa_import_key( key_slot, key_type,
1366 key_data->x, key_data->len ) == PSA_SUCCESS );
1367
1368 TEST_ASSERT( psa_get_key_information( key_slot,
1369 NULL,
1370 &key_bits ) == PSA_SUCCESS );
1371 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1372 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001373 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001374
1375 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
1376 NULL, 0,
1377 NULL, 0,
1378 buffer, buffer_length,
1379 &output_length );
1380 if( policy_alg == exercise_alg &&
1381 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1382 TEST_ASSERT( status == PSA_SUCCESS );
1383 else
1384 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1385
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001386 if( buffer_length != 0 )
1387 memset( buffer, 0, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001388 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
1389 buffer, buffer_length,
1390 NULL, 0,
1391 buffer, buffer_length,
1392 &output_length );
1393 if( policy_alg == exercise_alg &&
1394 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1395 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
1396 else
1397 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1398
1399exit:
1400 psa_destroy_key( key_slot );
1401 mbedtls_psa_crypto_free( );
1402 mbedtls_free( buffer );
1403}
1404/* END_CASE */
1405
1406/* BEGIN_CASE */
1407void asymmetric_signature_key_policy( int policy_usage,
1408 int policy_alg,
1409 int key_type,
1410 data_t *key_data,
1411 int exercise_alg )
1412{
1413 int key_slot = 1;
1414 psa_key_policy_t policy;
1415 psa_status_t status;
1416 unsigned char payload[16] = {1};
1417 size_t payload_length = sizeof( payload );
1418 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1419 size_t signature_length;
1420
1421 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1422
1423 psa_key_policy_init( &policy );
1424 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1425 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1426
1427 TEST_ASSERT( psa_import_key( key_slot, key_type,
1428 key_data->x, key_data->len ) == PSA_SUCCESS );
1429
1430 status = psa_asymmetric_sign( key_slot, exercise_alg,
1431 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001432 signature, sizeof( signature ),
1433 &signature_length );
1434 if( policy_alg == exercise_alg &&
1435 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1436 TEST_ASSERT( status == PSA_SUCCESS );
1437 else
1438 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1439
1440 memset( signature, 0, sizeof( signature ) );
1441 status = psa_asymmetric_verify( key_slot, exercise_alg,
1442 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001443 signature, sizeof( signature ) );
1444 if( policy_alg == exercise_alg &&
1445 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1446 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1447 else
1448 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001449
1450exit:
1451 psa_destroy_key( key_slot );
1452 mbedtls_psa_crypto_free( );
1453}
1454/* END_CASE */
1455
1456/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001457void derive_key_policy( int policy_usage,
1458 int policy_alg,
1459 int key_type,
1460 data_t *key_data,
1461 int exercise_alg )
1462{
1463 int key_slot = 1;
1464 psa_key_policy_t policy;
1465 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1466 psa_status_t status;
1467
1468 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1469
1470 psa_key_policy_init( &policy );
1471 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1472 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1473
1474 TEST_ASSERT( psa_import_key( key_slot, key_type,
1475 key_data->x, key_data->len ) == PSA_SUCCESS );
1476
1477 status = psa_key_derivation( &generator, key_slot,
1478 exercise_alg,
1479 NULL, 0,
1480 NULL, 0,
1481 1 );
1482 if( policy_alg == exercise_alg &&
1483 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1484 TEST_ASSERT( status == PSA_SUCCESS );
1485 else
1486 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1487
1488exit:
1489 psa_generator_abort( &generator );
1490 psa_destroy_key( key_slot );
1491 mbedtls_psa_crypto_free( );
1492}
1493/* END_CASE */
1494
1495/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001496void key_lifetime( int lifetime_arg )
1497{
1498 int key_slot = 1;
Gilles Peskinec32f0302018-08-10 16:02:11 +02001499 psa_key_type_t key_type = PSA_KEY_TYPE_RAW_DATA;
Gilles Peskined5b33222018-06-18 22:20:03 +02001500 unsigned char key[32] = {0};
1501 psa_key_lifetime_t lifetime_set = lifetime_arg;
1502 psa_key_lifetime_t lifetime_get;
1503
1504 memset( key, 0x2a, sizeof( key ) );
1505
1506 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1507
1508 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1509 lifetime_set ) == PSA_SUCCESS );
1510
1511 TEST_ASSERT( psa_import_key( key_slot, key_type,
1512 key, sizeof( key ) ) == PSA_SUCCESS );
1513
1514 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1515 &lifetime_get ) == PSA_SUCCESS );
1516
1517 TEST_ASSERT( lifetime_get == lifetime_set );
1518
1519exit:
1520 psa_destroy_key( key_slot );
1521 mbedtls_psa_crypto_free( );
1522}
1523/* END_CASE */
1524
1525/* BEGIN_CASE */
1526void key_lifetime_set_fail( int key_slot_arg,
1527 int lifetime_arg,
1528 int expected_status_arg )
1529{
1530 psa_key_slot_t key_slot = key_slot_arg;
1531 psa_key_lifetime_t lifetime_set = lifetime_arg;
1532 psa_status_t actual_status;
1533 psa_status_t expected_status = expected_status_arg;
1534
1535 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1536
1537 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1538
1539 if( actual_status == PSA_SUCCESS )
1540 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1541
1542 TEST_ASSERT( expected_status == actual_status );
1543
1544exit:
1545 psa_destroy_key( key_slot );
1546 mbedtls_psa_crypto_free( );
1547}
1548/* END_CASE */
1549
1550/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001551void hash_setup( int alg_arg,
1552 int expected_status_arg )
1553{
1554 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001555 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001556 psa_hash_operation_t operation;
1557 psa_status_t status;
1558
1559 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1560
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001561 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001562 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001563 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001564
1565exit:
1566 mbedtls_psa_crypto_free( );
1567}
1568/* END_CASE */
1569
1570/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001571void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001572{
1573 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001574 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001575 size_t actual_hash_length;
1576 psa_hash_operation_t operation;
1577
Gilles Peskine69c12672018-06-28 00:07:19 +02001578 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1579 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1580
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001581 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001582 TEST_ASSERT( expected_hash != NULL );
1583 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1584 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001585
1586 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1587
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001588 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001589 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001590 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001591 TEST_ASSERT( psa_hash_finish( &operation,
1592 actual_hash, sizeof( actual_hash ),
1593 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001594 ASSERT_COMPARE( expected_hash->x, expected_hash->len,
1595 actual_hash, actual_hash_length );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001596
1597exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001598 mbedtls_psa_crypto_free( );
1599}
1600/* END_CASE */
1601
1602/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001603void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001604{
1605 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001606 psa_hash_operation_t operation;
1607
Gilles Peskine69c12672018-06-28 00:07:19 +02001608 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1609 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1610
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001611 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001612 TEST_ASSERT( expected_hash != NULL );
1613 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1614 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001615
1616 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1617
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001618 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001619 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001620 input->x,
1621 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001622 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001623 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001624 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001625
1626exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001627 mbedtls_psa_crypto_free( );
1628}
1629/* END_CASE */
1630
1631/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001632void mac_setup( int key_type_arg,
1633 data_t *key,
1634 int alg_arg,
1635 int expected_status_arg )
1636{
1637 int key_slot = 1;
1638 psa_key_type_t key_type = key_type_arg;
1639 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001640 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001641 psa_mac_operation_t operation;
1642 psa_key_policy_t policy;
1643 psa_status_t status;
1644
1645 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1646
1647 psa_key_policy_init( &policy );
1648 psa_key_policy_set_usage( &policy,
1649 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1650 alg );
1651 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1652
1653 TEST_ASSERT( psa_import_key( key_slot, key_type,
1654 key->x, key->len ) == PSA_SUCCESS );
1655
Gilles Peskine89167cb2018-07-08 20:12:23 +02001656 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001657 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001658 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001659
1660exit:
1661 psa_destroy_key( key_slot );
1662 mbedtls_psa_crypto_free( );
1663}
1664/* END_CASE */
1665
1666/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001667void mac_sign( int key_type_arg,
1668 data_t *key,
1669 int alg_arg,
1670 data_t *input,
1671 data_t *expected_mac )
1672{
1673 int key_slot = 1;
1674 psa_key_type_t key_type = key_type_arg;
1675 psa_algorithm_t alg = alg_arg;
1676 psa_mac_operation_t operation;
1677 psa_key_policy_t policy;
1678 /* Leave a little extra room in the output buffer. At the end of the
1679 * test, we'll check that the implementation didn't overwrite onto
1680 * this extra room. */
1681 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
1682 size_t mac_buffer_size =
1683 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
1684 size_t mac_length = 0;
1685
1686 memset( actual_mac, '+', sizeof( actual_mac ) );
1687 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
1688 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
1689
1690 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1691
1692 psa_key_policy_init( &policy );
1693 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
1694 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1695
1696 TEST_ASSERT( psa_import_key( key_slot, key_type,
1697 key->x, key->len ) == PSA_SUCCESS );
1698
1699 /* Calculate the MAC. */
1700 TEST_ASSERT( psa_mac_sign_setup( &operation,
1701 key_slot, alg ) == PSA_SUCCESS );
1702 TEST_ASSERT( psa_mac_update( &operation,
1703 input->x, input->len ) == PSA_SUCCESS );
1704 TEST_ASSERT( psa_mac_sign_finish( &operation,
1705 actual_mac, mac_buffer_size,
1706 &mac_length ) == PSA_SUCCESS );
1707
1708 /* Compare with the expected value. */
1709 TEST_ASSERT( mac_length == expected_mac->len );
1710 TEST_ASSERT( memcmp( actual_mac, expected_mac->x, mac_length ) == 0 );
1711
1712 /* Verify that the end of the buffer is untouched. */
1713 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
1714 sizeof( actual_mac ) - mac_length ) );
1715
1716exit:
1717 psa_destroy_key( key_slot );
1718 mbedtls_psa_crypto_free( );
1719}
1720/* END_CASE */
1721
1722/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001723void mac_verify( int key_type_arg,
1724 data_t *key,
1725 int alg_arg,
1726 data_t *input,
1727 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001728{
1729 int key_slot = 1;
1730 psa_key_type_t key_type = key_type_arg;
1731 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001732 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001733 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001734
Gilles Peskine69c12672018-06-28 00:07:19 +02001735 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1736
Gilles Peskine8c9def32018-02-08 10:02:12 +01001737 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001738 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001739 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001740 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001741 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1742 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001743
1744 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1745
mohammad16036df908f2018-04-02 08:34:15 -07001746 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001747 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07001748 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1749
Gilles Peskine8c9def32018-02-08 10:02:12 +01001750 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001751 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001752
Gilles Peskine89167cb2018-07-08 20:12:23 +02001753 TEST_ASSERT( psa_mac_verify_setup( &operation,
1754 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001755 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1756 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001757 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02001758 TEST_ASSERT( psa_mac_verify_finish( &operation,
1759 expected_mac->x,
1760 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001761
1762exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001763 psa_destroy_key( key_slot );
1764 mbedtls_psa_crypto_free( );
1765}
1766/* END_CASE */
1767
1768/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001769void cipher_setup( int key_type_arg,
1770 data_t *key,
1771 int alg_arg,
1772 int expected_status_arg )
1773{
1774 int key_slot = 1;
1775 psa_key_type_t key_type = key_type_arg;
1776 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001777 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001778 psa_cipher_operation_t operation;
1779 psa_key_policy_t policy;
1780 psa_status_t status;
1781
1782 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1783
1784 psa_key_policy_init( &policy );
1785 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1786 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1787
1788 TEST_ASSERT( psa_import_key( key_slot, key_type,
1789 key->x, key->len ) == PSA_SUCCESS );
1790
Gilles Peskinefe119512018-07-08 21:39:34 +02001791 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001792 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001793 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001794
1795exit:
1796 psa_destroy_key( key_slot );
1797 mbedtls_psa_crypto_free( );
1798}
1799/* END_CASE */
1800
1801/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001802void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001803 data_t *key,
1804 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001805 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001806{
1807 int key_slot = 1;
1808 psa_status_t status;
1809 psa_key_type_t key_type = key_type_arg;
1810 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001811 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001812 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001813 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001814 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001815 size_t output_buffer_size = 0;
1816 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001817 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001818 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001819 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001820
Gilles Peskine50e586b2018-06-08 14:28:46 +02001821 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001822 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001823 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001824 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1825 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1826 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001827
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001828 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1829 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001830
1831 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1832
Moran Pekered346952018-07-05 15:22:45 +03001833 psa_key_policy_init( &policy );
1834 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1835 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1836
Gilles Peskine50e586b2018-06-08 14:28:46 +02001837 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001838 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001839
Gilles Peskinefe119512018-07-08 21:39:34 +02001840 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1841 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001842
Gilles Peskinefe119512018-07-08 21:39:34 +02001843 TEST_ASSERT( psa_cipher_set_iv( &operation,
1844 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001845 output_buffer_size = (size_t) input->len +
1846 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001847 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001848
Gilles Peskine4abf7412018-06-18 16:35:34 +02001849 TEST_ASSERT( psa_cipher_update( &operation,
1850 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001851 output, output_buffer_size,
1852 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001853 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001854 status = psa_cipher_finish( &operation,
1855 output + function_output_length,
1856 output_buffer_size,
1857 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001858 total_output_length += function_output_length;
1859
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001860 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001861 if( expected_status == PSA_SUCCESS )
1862 {
1863 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001864 ASSERT_COMPARE( expected_output->x, expected_output->len,
1865 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001866 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001867
Gilles Peskine50e586b2018-06-08 14:28:46 +02001868exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001869 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001870 psa_destroy_key( key_slot );
1871 mbedtls_psa_crypto_free( );
1872}
1873/* END_CASE */
1874
1875/* BEGIN_CASE */
1876void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001877 data_t *key,
1878 data_t *input,
1879 int first_part_size,
1880 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001881{
1882 int key_slot = 1;
1883 psa_key_type_t key_type = key_type_arg;
1884 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001885 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001886 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001887 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001888 size_t output_buffer_size = 0;
1889 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001890 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001891 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001892 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001893
Gilles Peskine50e586b2018-06-08 14:28:46 +02001894 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001895 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001896 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001897 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1898 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1899 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001900
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001901 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1902 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001903
1904 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1905
Moran Pekered346952018-07-05 15:22:45 +03001906 psa_key_policy_init( &policy );
1907 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1908 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1909
Gilles Peskine50e586b2018-06-08 14:28:46 +02001910 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001911 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001912
Gilles Peskinefe119512018-07-08 21:39:34 +02001913 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1914 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001915
Gilles Peskinefe119512018-07-08 21:39:34 +02001916 TEST_ASSERT( psa_cipher_set_iv( &operation,
1917 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001918 output_buffer_size = (size_t) input->len +
1919 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001920 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001921
Gilles Peskine4abf7412018-06-18 16:35:34 +02001922 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001923 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001924 output, output_buffer_size,
1925 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001926 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001927 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001928 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001929 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001930 output, output_buffer_size,
1931 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001932 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001933 TEST_ASSERT( psa_cipher_finish( &operation,
1934 output + function_output_length,
1935 output_buffer_size,
1936 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001937 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001938 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1939
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001940 ASSERT_COMPARE( expected_output->x, expected_output->len,
1941 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001942
1943exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001944 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001945 psa_destroy_key( key_slot );
1946 mbedtls_psa_crypto_free( );
1947}
1948/* END_CASE */
1949
1950/* BEGIN_CASE */
1951void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001952 data_t *key,
1953 data_t *input,
1954 int first_part_size,
1955 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001956{
1957 int key_slot = 1;
1958
1959 psa_key_type_t key_type = key_type_arg;
1960 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001961 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001962 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001963 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001964 size_t output_buffer_size = 0;
1965 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001966 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001967 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001968 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001969
Gilles Peskine50e586b2018-06-08 14:28:46 +02001970 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001971 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001972 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001973 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1974 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1975 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001976
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001977 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1978 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001979
1980 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1981
Moran Pekered346952018-07-05 15:22:45 +03001982 psa_key_policy_init( &policy );
1983 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1984 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1985
Gilles Peskine50e586b2018-06-08 14:28:46 +02001986 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001987 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001988
Gilles Peskinefe119512018-07-08 21:39:34 +02001989 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1990 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001991
Gilles Peskinefe119512018-07-08 21:39:34 +02001992 TEST_ASSERT( psa_cipher_set_iv( &operation,
1993 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001994
mohammad16033d91abe2018-07-03 13:15:54 +03001995 output_buffer_size = (size_t) input->len +
1996 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001997 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001998
Gilles Peskine4abf7412018-06-18 16:35:34 +02001999 TEST_ASSERT( (unsigned int) first_part_size < input->len );
2000 TEST_ASSERT( psa_cipher_update( &operation,
2001 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002002 output, output_buffer_size,
2003 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002004 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002005 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002006 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002007 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002008 output, output_buffer_size,
2009 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002010 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002011 TEST_ASSERT( psa_cipher_finish( &operation,
2012 output + function_output_length,
2013 output_buffer_size,
2014 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002015 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002016 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
2017
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002018 ASSERT_COMPARE( expected_output->x, expected_output->len,
2019 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002020
2021exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002022 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002023 psa_destroy_key( key_slot );
2024 mbedtls_psa_crypto_free( );
2025}
2026/* END_CASE */
2027
Gilles Peskine50e586b2018-06-08 14:28:46 +02002028/* BEGIN_CASE */
2029void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002030 data_t *key,
2031 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002032 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002033{
2034 int key_slot = 1;
2035 psa_status_t status;
2036 psa_key_type_t key_type = key_type_arg;
2037 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002038 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002039 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002040 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002041 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002042 size_t output_buffer_size = 0;
2043 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002044 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002045 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002046 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002047
Gilles Peskine50e586b2018-06-08 14:28:46 +02002048 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002049 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002050 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002051 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2052 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2053 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002054
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002055 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2056 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002057
2058 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2059
Moran Pekered346952018-07-05 15:22:45 +03002060 psa_key_policy_init( &policy );
2061 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2062 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2063
Gilles Peskine50e586b2018-06-08 14:28:46 +02002064 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002065 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002066
Gilles Peskinefe119512018-07-08 21:39:34 +02002067 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2068 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002069
Gilles Peskinefe119512018-07-08 21:39:34 +02002070 TEST_ASSERT( psa_cipher_set_iv( &operation,
2071 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002072
mohammad16033d91abe2018-07-03 13:15:54 +03002073 output_buffer_size = (size_t) input->len +
2074 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002075 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002076
Gilles Peskine4abf7412018-06-18 16:35:34 +02002077 TEST_ASSERT( psa_cipher_update( &operation,
2078 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002079 output, output_buffer_size,
2080 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002081 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002082 status = psa_cipher_finish( &operation,
2083 output + function_output_length,
2084 output_buffer_size,
2085 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002086 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002087 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002088
2089 if( expected_status == PSA_SUCCESS )
2090 {
2091 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002092 ASSERT_COMPARE( expected_output->x, expected_output->len,
2093 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002094 }
2095
Gilles Peskine50e586b2018-06-08 14:28:46 +02002096exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002097 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002098 psa_destroy_key( key_slot );
2099 mbedtls_psa_crypto_free( );
2100}
2101/* END_CASE */
2102
Gilles Peskine50e586b2018-06-08 14:28:46 +02002103/* BEGIN_CASE */
2104void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002105 data_t *key,
2106 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002107{
2108 int key_slot = 1;
2109 psa_key_type_t key_type = key_type_arg;
2110 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002111 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002112 size_t iv_size = 16;
2113 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002114 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002115 size_t output1_size = 0;
2116 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002117 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002118 size_t output2_size = 0;
2119 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002120 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002121 psa_cipher_operation_t operation1;
2122 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002123 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002124
mohammad1603d7d7ba52018-03-12 18:51:53 +02002125 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002126 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002127 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2128 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002129
mohammad1603d7d7ba52018-03-12 18:51:53 +02002130 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2131
Moran Pekered346952018-07-05 15:22:45 +03002132 psa_key_policy_init( &policy );
2133 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2134 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2135
mohammad1603d7d7ba52018-03-12 18:51:53 +02002136 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002137 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002138
Gilles Peskinefe119512018-07-08 21:39:34 +02002139 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2140 key_slot, alg ) == PSA_SUCCESS );
2141 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2142 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002143
Gilles Peskinefe119512018-07-08 21:39:34 +02002144 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2145 iv, iv_size,
2146 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002147 output1_size = (size_t) input->len +
2148 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002149 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002150
Gilles Peskine4abf7412018-06-18 16:35:34 +02002151 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002152 output1, output1_size,
2153 &output1_length ) == PSA_SUCCESS );
2154 TEST_ASSERT( psa_cipher_finish( &operation1,
2155 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002156 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002157
Gilles Peskine048b7f02018-06-08 14:20:49 +02002158 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002159
2160 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2161
2162 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002163 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002164
Gilles Peskinefe119512018-07-08 21:39:34 +02002165 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2166 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002167 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2168 output2, output2_size,
2169 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002170 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002171 TEST_ASSERT( psa_cipher_finish( &operation2,
2172 output2 + output2_length,
2173 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002174 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002175
Gilles Peskine048b7f02018-06-08 14:20:49 +02002176 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002177
Janos Follath25c4fa82018-07-06 16:23:25 +01002178 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002179
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002180 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002181
2182exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002183 mbedtls_free( output1 );
2184 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03002185 psa_destroy_key( key_slot );
2186 mbedtls_psa_crypto_free( );
2187}
2188/* END_CASE */
2189
2190/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002191void cipher_verify_output_multipart( int alg_arg,
2192 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002193 data_t *key,
2194 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002195 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002196{
2197 int key_slot = 1;
2198 psa_key_type_t key_type = key_type_arg;
2199 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002200 unsigned char iv[16] = {0};
2201 size_t iv_size = 16;
2202 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002203 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002204 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002205 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002206 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002207 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002208 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002209 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002210 psa_cipher_operation_t operation1;
2211 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002212 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03002213
Moran Pekerded84402018-06-06 16:36:50 +03002214 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002215 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002216 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2217 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002218
Moran Pekerded84402018-06-06 16:36:50 +03002219 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2220
Moran Pekered346952018-07-05 15:22:45 +03002221 psa_key_policy_init( &policy );
2222 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2223 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2224
Moran Pekerded84402018-06-06 16:36:50 +03002225 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002226 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002227
Gilles Peskinefe119512018-07-08 21:39:34 +02002228 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2229 key_slot, alg ) == PSA_SUCCESS );
2230 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2231 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002232
Gilles Peskinefe119512018-07-08 21:39:34 +02002233 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2234 iv, iv_size,
2235 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002236 output1_buffer_size = (size_t) input->len +
2237 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002238 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002239
Gilles Peskine4abf7412018-06-18 16:35:34 +02002240 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002241
itayzafrir3e02b3b2018-06-12 17:06:52 +03002242 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002243 output1, output1_buffer_size,
2244 &function_output_length ) == PSA_SUCCESS );
2245 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002246
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002247 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002248 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002249 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002250 output1, output1_buffer_size,
2251 &function_output_length ) == PSA_SUCCESS );
2252 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002253
Gilles Peskine048b7f02018-06-08 14:20:49 +02002254 TEST_ASSERT( psa_cipher_finish( &operation1,
2255 output1 + output1_length,
2256 output1_buffer_size - output1_length,
2257 &function_output_length ) == PSA_SUCCESS );
2258 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002259
2260 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2261
Gilles Peskine048b7f02018-06-08 14:20:49 +02002262 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002263 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002264
Gilles Peskinefe119512018-07-08 21:39:34 +02002265 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2266 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002267
2268 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002269 output2, output2_buffer_size,
2270 &function_output_length ) == PSA_SUCCESS );
2271 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002272
Gilles Peskine048b7f02018-06-08 14:20:49 +02002273 TEST_ASSERT( psa_cipher_update( &operation2,
2274 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002275 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002276 output2, output2_buffer_size,
2277 &function_output_length ) == PSA_SUCCESS );
2278 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002279
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002280 TEST_ASSERT( psa_cipher_finish( &operation2,
2281 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002282 output2_buffer_size - output2_length,
2283 &function_output_length ) == PSA_SUCCESS );
2284 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002285
Janos Follath25c4fa82018-07-06 16:23:25 +01002286 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002287
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002288 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002289
2290exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002291 mbedtls_free( output1 );
2292 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002293 psa_destroy_key( key_slot );
2294 mbedtls_psa_crypto_free( );
2295}
2296/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002297
Gilles Peskine20035e32018-02-03 22:44:14 +01002298/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002299void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002300 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002301 data_t *nonce,
2302 data_t *additional_data,
2303 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002304 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002305{
2306 int slot = 1;
2307 psa_key_type_t key_type = key_type_arg;
2308 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002309 unsigned char *output_data = NULL;
2310 size_t output_size = 0;
2311 size_t output_length = 0;
2312 unsigned char *output_data2 = NULL;
2313 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002314 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002315 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002316 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002317
Gilles Peskinea1cac842018-06-11 19:33:02 +02002318 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002319 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002320 TEST_ASSERT( nonce != NULL );
2321 TEST_ASSERT( additional_data != NULL );
2322 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2323 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2324 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2325 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2326
Gilles Peskine4abf7412018-06-18 16:35:34 +02002327 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002328 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002329
2330 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2331
2332 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002333 psa_key_policy_set_usage( &policy,
2334 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2335 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002336 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2337
2338 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002339 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002340
2341 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002342 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002343 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002344 additional_data->len,
2345 input_data->x, input_data->len,
2346 output_data, output_size,
2347 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002348
2349 if( PSA_SUCCESS == expected_result )
2350 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002351 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002352
2353 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002354 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002355 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002356 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002357 output_data, output_length,
2358 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002359 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002360
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002361 ASSERT_COMPARE( input_data->x, input_data->len,
2362 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002363 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002364
Gilles Peskinea1cac842018-06-11 19:33:02 +02002365exit:
2366 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002367 mbedtls_free( output_data );
2368 mbedtls_free( output_data2 );
2369 mbedtls_psa_crypto_free( );
2370}
2371/* END_CASE */
2372
2373/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002374void aead_encrypt( int key_type_arg, data_t *key_data,
2375 int alg_arg,
2376 data_t *nonce,
2377 data_t *additional_data,
2378 data_t *input_data,
2379 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002380{
2381 int slot = 1;
2382 psa_key_type_t key_type = key_type_arg;
2383 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002384 unsigned char *output_data = NULL;
2385 size_t output_size = 0;
2386 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002387 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002388 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002389
Gilles Peskinea1cac842018-06-11 19:33:02 +02002390 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002391 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002392 TEST_ASSERT( additional_data != NULL );
2393 TEST_ASSERT( nonce != NULL );
2394 TEST_ASSERT( expected_result != NULL );
2395 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2396 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2397 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2398 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2399 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2400
Gilles Peskine4abf7412018-06-18 16:35:34 +02002401 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002402 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002403
Gilles Peskinea1cac842018-06-11 19:33:02 +02002404 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2405
2406 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002407 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002408 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2409
2410 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002411 key_data->x,
2412 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002413
2414 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002415 nonce->x, nonce->len,
2416 additional_data->x, additional_data->len,
2417 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002418 output_data, output_size,
2419 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002420
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002421 ASSERT_COMPARE( expected_result->x, expected_result->len,
2422 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002423
Gilles Peskinea1cac842018-06-11 19:33:02 +02002424exit:
2425 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002426 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002427 mbedtls_psa_crypto_free( );
2428}
2429/* END_CASE */
2430
2431/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002432void aead_decrypt( int key_type_arg, data_t *key_data,
2433 int alg_arg,
2434 data_t *nonce,
2435 data_t *additional_data,
2436 data_t *input_data,
2437 data_t *expected_data,
2438 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002439{
2440 int slot = 1;
2441 psa_key_type_t key_type = key_type_arg;
2442 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002443 unsigned char *output_data = NULL;
2444 size_t output_size = 0;
2445 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002446 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002447 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002448 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002449
Gilles Peskinea1cac842018-06-11 19:33:02 +02002450 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002451 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002452 TEST_ASSERT( additional_data != NULL );
2453 TEST_ASSERT( nonce != NULL );
2454 TEST_ASSERT( expected_data != NULL );
2455 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2456 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2457 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2458 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2459 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2460
Gilles Peskine4abf7412018-06-18 16:35:34 +02002461 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002462 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002463
Gilles Peskinea1cac842018-06-11 19:33:02 +02002464 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2465
2466 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002467 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002468 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2469
2470 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002471 key_data->x,
2472 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002473
2474 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002475 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002476 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002477 additional_data->len,
2478 input_data->x, input_data->len,
2479 output_data, output_size,
2480 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002481
Gilles Peskine2d277862018-06-18 15:41:12 +02002482 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002483 ASSERT_COMPARE( expected_data->x, expected_data->len,
2484 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002485
Gilles Peskinea1cac842018-06-11 19:33:02 +02002486exit:
2487 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002488 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002489 mbedtls_psa_crypto_free( );
2490}
2491/* END_CASE */
2492
2493/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002494void signature_size( int type_arg,
2495 int bits,
2496 int alg_arg,
2497 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002498{
2499 psa_key_type_t type = type_arg;
2500 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002501 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002502 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2503exit:
2504 ;
2505}
2506/* END_CASE */
2507
2508/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002509void sign_deterministic( int key_type_arg, data_t *key_data,
2510 int alg_arg, data_t *input_data,
2511 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002512{
2513 int slot = 1;
2514 psa_key_type_t key_type = key_type_arg;
2515 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002516 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002517 unsigned char *signature = NULL;
2518 size_t signature_size;
2519 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002520 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002521
Gilles Peskine20035e32018-02-03 22:44:14 +01002522 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002523 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002524 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002525 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2526 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2527 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002528
2529 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2530
mohammad1603a97cb8c2018-03-28 03:46:26 -07002531 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002532 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002533 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2534
Gilles Peskine20035e32018-02-03 22:44:14 +01002535 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002536 key_data->x,
2537 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002538 TEST_ASSERT( psa_get_key_information( slot,
2539 NULL,
2540 &key_bits ) == PSA_SUCCESS );
2541
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002542 /* Allocate a buffer which has the size advertized by the
2543 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002544 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2545 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002546 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002547 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002548 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002549
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002550 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002551 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002552 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002553 signature, signature_size,
2554 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002555 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002556 ASSERT_COMPARE( output_data->x, output_data->len,
2557 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002558
2559exit:
2560 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002561 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002562 mbedtls_psa_crypto_free( );
2563}
2564/* END_CASE */
2565
2566/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002567void sign_fail( int key_type_arg, data_t *key_data,
2568 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002569 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002570{
2571 int slot = 1;
2572 psa_key_type_t key_type = key_type_arg;
2573 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002574 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002575 psa_status_t actual_status;
2576 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002577 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002578 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002579 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002580
Gilles Peskine20035e32018-02-03 22:44:14 +01002581 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002582 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002583 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2584 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2585
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002586 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002587
2588 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2589
mohammad1603a97cb8c2018-03-28 03:46:26 -07002590 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002591 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002592 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2593
Gilles Peskine20035e32018-02-03 22:44:14 +01002594 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002595 key_data->x,
2596 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002597
2598 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002599 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002600 signature, signature_size,
2601 &signature_length );
2602 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002603 /* The value of *signature_length is unspecified on error, but
2604 * whatever it is, it should be less than signature_size, so that
2605 * if the caller tries to read *signature_length bytes without
2606 * checking the error code then they don't overflow a buffer. */
2607 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002608
2609exit:
2610 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002611 mbedtls_free( signature );
2612 mbedtls_psa_crypto_free( );
2613}
2614/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002615
2616/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002617void sign_verify( int key_type_arg, data_t *key_data,
2618 int alg_arg, data_t *input_data )
2619{
2620 int slot = 1;
2621 psa_key_type_t key_type = key_type_arg;
2622 psa_algorithm_t alg = alg_arg;
2623 size_t key_bits;
2624 unsigned char *signature = NULL;
2625 size_t signature_size;
2626 size_t signature_length = 0xdeadbeef;
2627 psa_key_policy_t policy;
2628
2629 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2630
2631 psa_key_policy_init( &policy );
2632 psa_key_policy_set_usage( &policy,
2633 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2634 alg );
2635 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2636
2637 TEST_ASSERT( psa_import_key( slot, key_type,
2638 key_data->x,
2639 key_data->len ) == PSA_SUCCESS );
2640 TEST_ASSERT( psa_get_key_information( slot,
2641 NULL,
2642 &key_bits ) == PSA_SUCCESS );
2643
2644 /* Allocate a buffer which has the size advertized by the
2645 * library. */
2646 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2647 key_bits, alg );
2648 TEST_ASSERT( signature_size != 0 );
2649 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002650 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002651
2652 /* Perform the signature. */
2653 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2654 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002655 signature, signature_size,
2656 &signature_length ) == PSA_SUCCESS );
2657 /* Check that the signature length looks sensible. */
2658 TEST_ASSERT( signature_length <= signature_size );
2659 TEST_ASSERT( signature_length > 0 );
2660
2661 /* Use the library to verify that the signature is correct. */
2662 TEST_ASSERT( psa_asymmetric_verify(
2663 slot, alg,
2664 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002665 signature, signature_length ) == PSA_SUCCESS );
2666
2667 if( input_data->len != 0 )
2668 {
2669 /* Flip a bit in the input and verify that the signature is now
2670 * detected as invalid. Flip a bit at the beginning, not at the end,
2671 * because ECDSA may ignore the last few bits of the input. */
2672 input_data->x[0] ^= 1;
2673 TEST_ASSERT( psa_asymmetric_verify(
2674 slot, alg,
2675 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002676 signature,
2677 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2678 }
2679
2680exit:
2681 psa_destroy_key( slot );
2682 mbedtls_free( signature );
2683 mbedtls_psa_crypto_free( );
2684}
2685/* END_CASE */
2686
2687/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002688void asymmetric_verify( int key_type_arg, data_t *key_data,
2689 int alg_arg, data_t *hash_data,
2690 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002691{
2692 int slot = 1;
2693 psa_key_type_t key_type = key_type_arg;
2694 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002695 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002696
Gilles Peskine69c12672018-06-28 00:07:19 +02002697 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2698
itayzafrir5c753392018-05-08 11:18:38 +03002699 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002700 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002701 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002702 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2703 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2704 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002705
2706 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2707
2708 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002709 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002710 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2711
2712 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002713 key_data->x,
2714 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002715
2716 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002717 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002718 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002719 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002720exit:
2721 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002722 mbedtls_psa_crypto_free( );
2723}
2724/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002725
2726/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002727void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2728 int alg_arg, data_t *hash_data,
2729 data_t *signature_data,
2730 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002731{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002732 int slot = 1;
2733 psa_key_type_t key_type = key_type_arg;
2734 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002735 psa_status_t actual_status;
2736 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002737 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002738
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002739 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002740 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002741 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002742 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2743 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2744 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002745
2746 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2747
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002748 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002749 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002750 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2751
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002752 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002753 key_data->x,
2754 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002755
2756 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002757 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002758 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002759 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002760
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002761 TEST_ASSERT( actual_status == expected_status );
2762
2763exit:
2764 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002765 mbedtls_psa_crypto_free( );
2766}
2767/* END_CASE */
2768
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002769/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002770void asymmetric_encrypt( int key_type_arg,
2771 data_t *key_data,
2772 int alg_arg,
2773 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02002774 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02002775 int expected_output_length_arg,
2776 int expected_status_arg )
2777{
2778 int slot = 1;
2779 psa_key_type_t key_type = key_type_arg;
2780 psa_algorithm_t alg = alg_arg;
2781 size_t expected_output_length = expected_output_length_arg;
2782 size_t key_bits;
2783 unsigned char *output = NULL;
2784 size_t output_size;
2785 size_t output_length = ~0;
2786 psa_status_t actual_status;
2787 psa_status_t expected_status = expected_status_arg;
2788 psa_key_policy_t policy;
2789
2790 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2791
2792 /* Import the key */
2793 psa_key_policy_init( &policy );
2794 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2795 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2796 TEST_ASSERT( psa_import_key( slot, key_type,
2797 key_data->x,
2798 key_data->len ) == PSA_SUCCESS );
2799
2800 /* Determine the maximum output length */
2801 TEST_ASSERT( psa_get_key_information( slot,
2802 NULL,
2803 &key_bits ) == PSA_SUCCESS );
2804 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002805 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02002806
2807 /* Encrypt the input */
2808 actual_status = psa_asymmetric_encrypt( slot, alg,
2809 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002810 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02002811 output, output_size,
2812 &output_length );
2813 TEST_ASSERT( actual_status == expected_status );
2814 TEST_ASSERT( output_length == expected_output_length );
2815
Gilles Peskine68428122018-06-30 18:42:41 +02002816 /* If the label is empty, the test framework puts a non-null pointer
2817 * in label->x. Test that a null pointer works as well. */
2818 if( label->len == 0 )
2819 {
2820 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002821 if( output_size != 0 )
2822 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02002823 actual_status = psa_asymmetric_encrypt( slot, alg,
2824 input_data->x, input_data->len,
2825 NULL, label->len,
2826 output, output_size,
2827 &output_length );
2828 TEST_ASSERT( actual_status == expected_status );
2829 TEST_ASSERT( output_length == expected_output_length );
2830 }
2831
Gilles Peskine656896e2018-06-29 19:12:28 +02002832exit:
2833 psa_destroy_key( slot );
2834 mbedtls_free( output );
2835 mbedtls_psa_crypto_free( );
2836}
2837/* END_CASE */
2838
2839/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002840void asymmetric_encrypt_decrypt( int key_type_arg,
2841 data_t *key_data,
2842 int alg_arg,
2843 data_t *input_data,
2844 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002845{
2846 int slot = 1;
2847 psa_key_type_t key_type = key_type_arg;
2848 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002849 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002850 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002851 size_t output_size;
2852 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002853 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002854 size_t output2_size;
2855 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002856 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002857
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002858 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002859 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002860 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2861 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2862
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002863 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2864
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002865 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002866 psa_key_policy_set_usage( &policy,
2867 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002868 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002869 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2870
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002871 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002872 key_data->x,
2873 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002874
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002875
2876 /* Determine the maximum ciphertext length */
2877 TEST_ASSERT( psa_get_key_information( slot,
2878 NULL,
2879 &key_bits ) == PSA_SUCCESS );
2880 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002881 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002882 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002883 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002884
Gilles Peskineeebd7382018-06-08 18:11:54 +02002885 /* We test encryption by checking that encrypt-then-decrypt gives back
2886 * the original plaintext because of the non-optional random
2887 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02002888 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002889 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002890 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002891 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002892 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002893 /* We don't know what ciphertext length to expect, but check that
2894 * it looks sensible. */
2895 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002896
Gilles Peskine2d277862018-06-18 15:41:12 +02002897 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002898 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02002899 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002900 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002901 &output2_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002902 ASSERT_COMPARE( input_data->x, input_data->len,
2903 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002904
2905exit:
2906 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002907 mbedtls_free( output );
2908 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002909 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002910}
2911/* END_CASE */
2912
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002913/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002914void asymmetric_decrypt( int key_type_arg,
2915 data_t *key_data,
2916 int alg_arg,
2917 data_t *input_data,
2918 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02002919 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002920{
2921 int slot = 1;
2922 psa_key_type_t key_type = key_type_arg;
2923 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002924 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002925 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002926 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002927 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002928
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002929 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002930 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002931 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002932 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2933 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2934 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2935
Gilles Peskine4abf7412018-06-18 16:35:34 +02002936 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002937 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002938
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002939 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2940
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002941 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002942 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002943 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2944
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002945 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002946 key_data->x,
2947 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002948
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002949 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002950 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002951 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002952 output,
2953 output_size,
2954 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002955 ASSERT_COMPARE( expected_data->x, expected_data->len,
2956 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002957
Gilles Peskine68428122018-06-30 18:42:41 +02002958 /* If the label is empty, the test framework puts a non-null pointer
2959 * in label->x. Test that a null pointer works as well. */
2960 if( label->len == 0 )
2961 {
2962 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002963 if( output_size != 0 )
2964 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02002965 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
2966 input_data->x, input_data->len,
2967 NULL, label->len,
2968 output,
2969 output_size,
2970 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002971 ASSERT_COMPARE( expected_data->x, expected_data->len,
2972 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02002973 }
2974
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002975exit:
2976 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002977 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002978 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002979}
2980/* END_CASE */
2981
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002982/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002983void asymmetric_decrypt_fail( int key_type_arg,
2984 data_t *key_data,
2985 int alg_arg,
2986 data_t *input_data,
2987 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02002988 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002989{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002990 int slot = 1;
2991 psa_key_type_t key_type = key_type_arg;
2992 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002993 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002994 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002995 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002996 psa_status_t actual_status;
2997 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002998 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002999
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003000 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003001 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003002 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3003 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3004
Gilles Peskine4abf7412018-06-18 16:35:34 +02003005 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003006 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003007
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003008 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3009
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003010 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003011 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003012 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3013
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003014 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003015 key_data->x,
3016 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003017
Gilles Peskine2d277862018-06-18 15:41:12 +02003018 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003019 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003020 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003021 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003022 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003023 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003024 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003025
Gilles Peskine68428122018-06-30 18:42:41 +02003026 /* If the label is empty, the test framework puts a non-null pointer
3027 * in label->x. Test that a null pointer works as well. */
3028 if( label->len == 0 )
3029 {
3030 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003031 if( output_size != 0 )
3032 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003033 actual_status = psa_asymmetric_decrypt( slot, alg,
3034 input_data->x, input_data->len,
3035 NULL, label->len,
3036 output, output_size,
3037 &output_length );
3038 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003039 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003040 }
3041
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003042exit:
3043 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02003044 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003045 mbedtls_psa_crypto_free( );
3046}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003047/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003048
3049/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003050void derive_setup( int key_type_arg,
3051 data_t *key_data,
3052 int alg_arg,
3053 data_t *salt,
3054 data_t *label,
3055 int requested_capacity_arg,
3056 int expected_status_arg )
3057{
3058 psa_key_slot_t slot = 1;
3059 size_t key_type = key_type_arg;
3060 psa_algorithm_t alg = alg_arg;
3061 size_t requested_capacity = requested_capacity_arg;
3062 psa_status_t expected_status = expected_status_arg;
3063 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3064 psa_key_policy_t policy;
3065
3066 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3067
3068 psa_key_policy_init( &policy );
3069 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3070 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3071
3072 TEST_ASSERT( psa_import_key( slot, key_type,
3073 key_data->x,
3074 key_data->len ) == PSA_SUCCESS );
3075
3076 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3077 salt->x, salt->len,
3078 label->x, label->len,
3079 requested_capacity ) == expected_status );
3080
3081exit:
3082 psa_generator_abort( &generator );
3083 psa_destroy_key( slot );
3084 mbedtls_psa_crypto_free( );
3085}
3086/* END_CASE */
3087
3088/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003089void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003090{
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003091 psa_key_slot_t base_key = 1;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003092 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003093 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003094 psa_algorithm_t alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003095 size_t capacity = 42;
3096 uint8_t buffer[42];
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003097 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3098 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3099 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003100 psa_key_policy_t policy;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003101
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003102 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3103
3104 psa_key_policy_init( &policy );
3105 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3106 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3107
3108 TEST_ASSERT( psa_import_key( base_key, key_type,
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003109 key_data,
3110 sizeof(key_data) ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003111
3112 /* valid key derivation */
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003113 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003114 NULL, 0,
3115 NULL, 0,
3116 capacity ) == PSA_SUCCESS );
3117
3118 /* state of generator shouldn't allow additional generation */
3119 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3120 NULL, 0,
3121 NULL, 0,
3122 capacity ) == PSA_ERROR_BAD_STATE );
3123
3124 TEST_ASSERT( psa_generator_read( &generator, buffer, capacity )
3125 == PSA_SUCCESS );
3126
3127 TEST_ASSERT( psa_generator_read( &generator, buffer, capacity )
3128 == PSA_ERROR_INSUFFICIENT_CAPACITY );
3129
3130
3131exit:
3132 psa_generator_abort( &generator );
3133 psa_destroy_key( base_key );
3134 mbedtls_psa_crypto_free( );
3135}
3136/* END_CASE */
3137
3138
3139/* BEGIN_CASE */
3140void test_derive_invalid_generator_tests( )
3141{
3142 uint8_t output_buffer[16];
3143 size_t buffer_size = 16;
3144 size_t capacity = 0;
3145 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3146
Nir Sonnenschein50789302018-10-31 12:16:38 +02003147 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003148 == PSA_ERROR_INSUFFICIENT_CAPACITY );
3149
3150 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenscheinf8964b92018-10-31 18:06:14 +02003151 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:issue opened
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003152
Nir Sonnenschein50789302018-10-31 12:16:38 +02003153 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003154
Nir Sonnenschein50789302018-10-31 12:16:38 +02003155 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003156 == PSA_ERROR_INSUFFICIENT_CAPACITY );
3157
Nir Sonnenschein50789302018-10-31 12:16:38 +02003158 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenscheinf8964b92018-10-31 18:06:14 +02003159 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:issue opened
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003160
3161exit:
3162 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003163}
3164/* END_CASE */
3165
3166/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003167void derive_output( int alg_arg,
3168 data_t *key_data,
3169 data_t *salt,
3170 data_t *label,
3171 int requested_capacity_arg,
3172 data_t *expected_output1,
3173 data_t *expected_output2 )
3174{
3175 psa_key_slot_t slot = 1;
3176 psa_algorithm_t alg = alg_arg;
3177 size_t requested_capacity = requested_capacity_arg;
3178 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3179 uint8_t *expected_outputs[2] =
3180 {expected_output1->x, expected_output2->x};
3181 size_t output_sizes[2] =
3182 {expected_output1->len, expected_output2->len};
3183 size_t output_buffer_size = 0;
3184 uint8_t *output_buffer = NULL;
3185 size_t expected_capacity;
3186 size_t current_capacity;
3187 psa_key_policy_t policy;
3188 psa_status_t status;
3189 unsigned i;
3190
3191 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3192 {
3193 if( output_sizes[i] > output_buffer_size )
3194 output_buffer_size = output_sizes[i];
3195 if( output_sizes[i] == 0 )
3196 expected_outputs[i] = NULL;
3197 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003198 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003199 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3200
3201 psa_key_policy_init( &policy );
3202 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3203 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3204
3205 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3206 key_data->x,
3207 key_data->len ) == PSA_SUCCESS );
3208
3209 /* Extraction phase. */
3210 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3211 salt->x, salt->len,
3212 label->x, label->len,
3213 requested_capacity ) == PSA_SUCCESS );
3214 TEST_ASSERT( psa_get_generator_capacity( &generator,
3215 &current_capacity ) ==
3216 PSA_SUCCESS );
3217 TEST_ASSERT( current_capacity == requested_capacity );
3218 expected_capacity = requested_capacity;
3219
3220 /* Expansion phase. */
3221 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3222 {
3223 /* Read some bytes. */
3224 status = psa_generator_read( &generator,
3225 output_buffer, output_sizes[i] );
3226 if( expected_capacity == 0 && output_sizes[i] == 0 )
3227 {
3228 /* Reading 0 bytes when 0 bytes are available can go either way. */
3229 TEST_ASSERT( status == PSA_SUCCESS ||
3230 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3231 continue;
3232 }
3233 else if( expected_capacity == 0 ||
3234 output_sizes[i] > expected_capacity )
3235 {
3236 /* Capacity exceeded. */
3237 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3238 expected_capacity = 0;
3239 continue;
3240 }
3241 /* Success. Check the read data. */
3242 TEST_ASSERT( status == PSA_SUCCESS );
3243 if( output_sizes[i] != 0 )
3244 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
3245 output_sizes[i] ) == 0 );
3246 /* Check the generator status. */
3247 expected_capacity -= output_sizes[i];
3248 TEST_ASSERT( psa_get_generator_capacity( &generator,
3249 &current_capacity ) ==
3250 PSA_SUCCESS );
3251 TEST_ASSERT( expected_capacity == current_capacity );
3252 }
3253 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3254
3255exit:
3256 mbedtls_free( output_buffer );
3257 psa_generator_abort( &generator );
3258 psa_destroy_key( slot );
3259 mbedtls_psa_crypto_free( );
3260}
3261/* END_CASE */
3262
3263/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003264void derive_full( int alg_arg,
3265 data_t *key_data,
3266 data_t *salt,
3267 data_t *label,
3268 int requested_capacity_arg )
3269{
3270 psa_key_slot_t slot = 1;
3271 psa_algorithm_t alg = alg_arg;
3272 size_t requested_capacity = requested_capacity_arg;
3273 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3274 unsigned char output_buffer[16];
3275 size_t expected_capacity = requested_capacity;
3276 size_t current_capacity;
3277 psa_key_policy_t policy;
3278
3279 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3280
3281 psa_key_policy_init( &policy );
3282 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3283 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3284
3285 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3286 key_data->x,
3287 key_data->len ) == PSA_SUCCESS );
3288
3289 /* Extraction phase. */
3290 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3291 salt->x, salt->len,
3292 label->x, label->len,
3293 requested_capacity ) == PSA_SUCCESS );
3294 TEST_ASSERT( psa_get_generator_capacity( &generator,
3295 &current_capacity ) ==
3296 PSA_SUCCESS );
3297 TEST_ASSERT( current_capacity == expected_capacity );
3298
3299 /* Expansion phase. */
3300 while( current_capacity > 0 )
3301 {
3302 size_t read_size = sizeof( output_buffer );
3303 if( read_size > current_capacity )
3304 read_size = current_capacity;
3305 TEST_ASSERT( psa_generator_read( &generator,
3306 output_buffer,
3307 read_size ) == PSA_SUCCESS );
3308 expected_capacity -= read_size;
3309 TEST_ASSERT( psa_get_generator_capacity( &generator,
3310 &current_capacity ) ==
3311 PSA_SUCCESS );
3312 TEST_ASSERT( current_capacity == expected_capacity );
3313 }
3314
3315 /* Check that the generator refuses to go over capacity. */
3316 TEST_ASSERT( psa_generator_read( &generator,
3317 output_buffer,
3318 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
3319
3320 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3321
3322exit:
3323 psa_generator_abort( &generator );
3324 psa_destroy_key( slot );
3325 mbedtls_psa_crypto_free( );
3326}
3327/* END_CASE */
3328
3329/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003330void derive_key_exercise( int alg_arg,
3331 data_t *key_data,
3332 data_t *salt,
3333 data_t *label,
3334 int derived_type_arg,
3335 int derived_bits_arg,
3336 int derived_usage_arg,
3337 int derived_alg_arg )
3338{
3339 psa_key_slot_t base_key = 1;
3340 psa_key_slot_t derived_key = 2;
3341 psa_algorithm_t alg = alg_arg;
3342 psa_key_type_t derived_type = derived_type_arg;
3343 size_t derived_bits = derived_bits_arg;
3344 psa_key_usage_t derived_usage = derived_usage_arg;
3345 psa_algorithm_t derived_alg = derived_alg_arg;
3346 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3347 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3348 psa_key_policy_t policy;
3349 psa_key_type_t got_type;
3350 size_t got_bits;
3351
3352 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3353
3354 psa_key_policy_init( &policy );
3355 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3356 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3357 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3358 key_data->x,
3359 key_data->len ) == PSA_SUCCESS );
3360
3361 /* Derive a key. */
3362 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3363 salt->x, salt->len,
3364 label->x, label->len,
3365 capacity ) == PSA_SUCCESS );
3366 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
3367 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3368 TEST_ASSERT( psa_generator_import_key( derived_key,
3369 derived_type,
3370 derived_bits,
3371 &generator ) == PSA_SUCCESS );
3372
3373 /* Test the key information */
3374 TEST_ASSERT( psa_get_key_information( derived_key,
3375 &got_type,
3376 &got_bits ) == PSA_SUCCESS );
3377 TEST_ASSERT( got_type == derived_type );
3378 TEST_ASSERT( got_bits == derived_bits );
3379
3380 /* Exercise the derived key. */
3381 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
3382 goto exit;
3383
3384exit:
3385 psa_generator_abort( &generator );
3386 psa_destroy_key( base_key );
3387 psa_destroy_key( derived_key );
3388 mbedtls_psa_crypto_free( );
3389}
3390/* END_CASE */
3391
3392/* BEGIN_CASE */
3393void derive_key_export( int alg_arg,
3394 data_t *key_data,
3395 data_t *salt,
3396 data_t *label,
3397 int bytes1_arg,
3398 int bytes2_arg )
3399{
3400 psa_key_slot_t base_key = 1;
3401 psa_key_slot_t derived_key = 2;
3402 psa_algorithm_t alg = alg_arg;
3403 size_t bytes1 = bytes1_arg;
3404 size_t bytes2 = bytes2_arg;
3405 size_t capacity = bytes1 + bytes2;
3406 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003407 uint8_t *output_buffer = NULL;
3408 uint8_t *export_buffer = NULL;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003409 psa_key_policy_t policy;
3410 size_t length;
3411
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003412 ASSERT_ALLOC( output_buffer, capacity );
3413 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003414 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3415
3416 psa_key_policy_init( &policy );
3417 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3418 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3419 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3420 key_data->x,
3421 key_data->len ) == PSA_SUCCESS );
3422
3423 /* Derive some material and output it. */
3424 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3425 salt->x, salt->len,
3426 label->x, label->len,
3427 capacity ) == PSA_SUCCESS );
3428 TEST_ASSERT( psa_generator_read( &generator,
3429 output_buffer,
3430 capacity ) == PSA_SUCCESS );
3431 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3432
3433 /* Derive the same output again, but this time store it in key objects. */
3434 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3435 salt->x, salt->len,
3436 label->x, label->len,
3437 capacity ) == PSA_SUCCESS );
3438 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
3439 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3440 TEST_ASSERT( psa_generator_import_key( derived_key,
3441 PSA_KEY_TYPE_RAW_DATA,
3442 PSA_BYTES_TO_BITS( bytes1 ),
3443 &generator ) == PSA_SUCCESS );
3444 TEST_ASSERT( psa_export_key( derived_key,
3445 export_buffer, bytes1,
3446 &length ) == PSA_SUCCESS );
3447 TEST_ASSERT( length == bytes1 );
3448 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
3449 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3450 TEST_ASSERT( psa_generator_import_key( derived_key,
3451 PSA_KEY_TYPE_RAW_DATA,
3452 PSA_BYTES_TO_BITS( bytes2 ),
3453 &generator ) == PSA_SUCCESS );
3454 TEST_ASSERT( psa_export_key( derived_key,
3455 export_buffer + bytes1, bytes2,
3456 &length ) == PSA_SUCCESS );
3457 TEST_ASSERT( length == bytes2 );
3458
3459 /* Compare the outputs from the two runs. */
3460 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
3461
3462exit:
3463 mbedtls_free( output_buffer );
3464 mbedtls_free( export_buffer );
3465 psa_generator_abort( &generator );
3466 psa_destroy_key( base_key );
3467 psa_destroy_key( derived_key );
3468 mbedtls_psa_crypto_free( );
3469}
3470/* END_CASE */
3471
3472/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003473void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003474{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003475 size_t bytes = bytes_arg;
3476 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003477 unsigned char *output = NULL;
3478 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003479 size_t i;
3480 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003481
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003482 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3483 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003484 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003485
3486 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3487
Gilles Peskinea50d7392018-06-21 10:22:13 +02003488 /* Run several times, to ensure that every output byte will be
3489 * nonzero at least once with overwhelming probability
3490 * (2^(-8*number_of_runs)). */
3491 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003492 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003493 if( bytes != 0 )
3494 memset( output, 0, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003495 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
3496
3497 /* Check that no more than bytes have been overwritten */
3498 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
3499
3500 for( i = 0; i < bytes; i++ )
3501 {
3502 if( output[i] != 0 )
3503 ++changed[i];
3504 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003505 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003506
3507 /* Check that every byte was changed to nonzero at least once. This
3508 * validates that psa_generate_random is overwriting every byte of
3509 * the output buffer. */
3510 for( i = 0; i < bytes; i++ )
3511 {
3512 TEST_ASSERT( changed[i] != 0 );
3513 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003514
3515exit:
3516 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003517 mbedtls_free( output );
3518 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003519}
3520/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003521
3522/* BEGIN_CASE */
3523void generate_key( int type_arg,
3524 int bits_arg,
3525 int usage_arg,
3526 int alg_arg,
3527 int expected_status_arg )
3528{
3529 int slot = 1;
3530 psa_key_type_t type = type_arg;
3531 psa_key_usage_t usage = usage_arg;
3532 size_t bits = bits_arg;
3533 psa_algorithm_t alg = alg_arg;
3534 psa_status_t expected_status = expected_status_arg;
3535 psa_key_type_t got_type;
3536 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003537 psa_status_t expected_info_status =
3538 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3539 psa_key_policy_t policy;
3540
3541 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3542
3543 psa_key_policy_init( &policy );
3544 psa_key_policy_set_usage( &policy, usage, alg );
3545 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3546
3547 /* Generate a key */
3548 TEST_ASSERT( psa_generate_key( slot, type, bits,
3549 NULL, 0 ) == expected_status );
3550
3551 /* Test the key information */
3552 TEST_ASSERT( psa_get_key_information( slot,
3553 &got_type,
3554 &got_bits ) == expected_info_status );
3555 if( expected_info_status != PSA_SUCCESS )
3556 goto exit;
3557 TEST_ASSERT( got_type == type );
3558 TEST_ASSERT( got_bits == bits );
3559
Gilles Peskine818ca122018-06-20 18:16:48 +02003560 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02003561 if( ! exercise_key( slot, usage, alg ) )
3562 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003563
3564exit:
3565 psa_destroy_key( slot );
3566 mbedtls_psa_crypto_free( );
3567}
3568/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03003569
3570/* BEGIN_CASE */
3571void validate_module_init_generate_random( )
3572{
3573 psa_status_t status;
3574 uint8_t random[10] = { 0 };
3575 status = psa_generate_random( random, sizeof( random ) );
3576 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3577}
3578/* END_CASE */
itayzafrir90d8c7a2018-09-12 11:44:52 +03003579
3580/* BEGIN_CASE */
3581void validate_module_init_key_based( )
3582{
3583 psa_status_t status;
3584 uint8_t data[10] = { 0 };
3585 status = psa_import_key( 1, PSA_KEY_TYPE_RAW_DATA, data, sizeof( data ) );
3586 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3587}
3588/* END_CASE */