blob: 73f03b5c341a6d019122002aad075216fb0835ee [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 Peskine5b802a32018-10-29 19:21:41 +0100567 /* Just the secret value */
568 TEST_ASSERT( exported_length == PSA_BITS_TO_BYTES( bits ) );
569 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200570 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200571#endif /* MBEDTLS_ECP_C */
572
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200573 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
574 {
575 uint8_t *p = exported;
576 uint8_t *end = exported + exported_length;
577 size_t len;
578 mbedtls_asn1_buf alg;
579 mbedtls_asn1_buf params;
580 mbedtls_asn1_bitstring bitstring;
581 /* SubjectPublicKeyInfo ::= SEQUENCE {
582 * algorithm AlgorithmIdentifier,
583 * subjectPublicKey BIT STRING }
584 * AlgorithmIdentifier ::= SEQUENCE {
585 * algorithm OBJECT IDENTIFIER,
586 * parameters ANY DEFINED BY algorithm OPTIONAL }
587 */
588 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
589 MBEDTLS_ASN1_SEQUENCE |
590 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
591 TEST_ASSERT( p + len == end );
592 TEST_ASSERT( mbedtls_asn1_get_alg( &p, end, &alg, &params ) == 0 );
593 if( ! is_oid_of_key_type( type, alg.p, alg.len ) )
594 goto exit;
595 TEST_ASSERT( mbedtls_asn1_get_bitstring( &p, end, &bitstring ) == 0 );
596 TEST_ASSERT( p == end );
597 p = bitstring.p;
598#if defined(MBEDTLS_RSA_C)
599 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
600 {
601 /* RSAPublicKey ::= SEQUENCE {
602 * modulus INTEGER, -- n
603 * publicExponent INTEGER } -- e
604 */
605 TEST_ASSERT( bitstring.unused_bits == 0 );
606 TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len,
607 MBEDTLS_ASN1_SEQUENCE |
608 MBEDTLS_ASN1_CONSTRUCTED ) == 0 );
609 TEST_ASSERT( p + len == end );
610 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
611 goto exit;
612 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
613 goto exit;
614 TEST_ASSERT( p == end );
615 }
616 else
617#endif /* MBEDTLS_RSA_C */
618#if defined(MBEDTLS_ECP_C)
619 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
620 {
621 /* ECPoint ::= ...
Gilles Peskinec6290c02018-08-13 17:24:59 +0200622 * -- first 8 bits: 0x04 (uncompressed representation);
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200623 * -- then x_P as an n-bit string, big endian;
624 * -- then y_P as a n-bit string, big endian,
625 * -- where n is the order of the curve.
626 */
627 TEST_ASSERT( bitstring.unused_bits == 0 );
628 TEST_ASSERT( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ) == end );
629 TEST_ASSERT( p[0] == 4 );
630 }
631 else
632#endif /* MBEDTLS_ECP_C */
633 {
634 char message[40];
635 mbedtls_snprintf( message, sizeof( message ),
636 "No sanity check for public key type=0x%08lx",
637 (unsigned long) type );
638 test_fail( message, __LINE__, __FILE__ );
639 return( 0 );
640 }
641 }
642 else
643
644 {
645 /* No sanity checks for other types */
646 }
647
648 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200649
650exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200651 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200652}
653
654static int exercise_export_key( psa_key_slot_t slot,
655 psa_key_usage_t usage )
656{
657 psa_key_type_t type;
658 size_t bits;
659 uint8_t *exported = NULL;
660 size_t exported_size = 0;
661 size_t exported_length = 0;
662 int ok = 0;
663
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200664 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
665
666 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
667 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200668 {
669 TEST_ASSERT( psa_export_key( slot, NULL, 0, &exported_length ) ==
670 PSA_ERROR_NOT_PERMITTED );
671 return( 1 );
672 }
673
Gilles Peskined14664a2018-08-10 19:07:32 +0200674 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200675 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200676
677 TEST_ASSERT( psa_export_key( slot,
678 exported, exported_size,
679 &exported_length ) == PSA_SUCCESS );
680 ok = exported_key_sanity_check( type, bits, exported, exported_length );
681
682exit:
683 mbedtls_free( exported );
684 return( ok );
685}
686
687static int exercise_export_public_key( psa_key_slot_t slot )
688{
689 psa_key_type_t type;
690 psa_key_type_t public_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
697 TEST_ASSERT( psa_get_key_information( slot, &type, &bits ) == PSA_SUCCESS );
698 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
699 {
700 TEST_ASSERT( psa_export_public_key( slot,
701 NULL, 0, &exported_length ) ==
702 PSA_ERROR_INVALID_ARGUMENT );
703 return( 1 );
704 }
705
706 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
707 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_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_public_key( slot,
711 exported, exported_size,
712 &exported_length ) == PSA_SUCCESS );
713 ok = exported_key_sanity_check( public_type, bits,
714 exported, exported_length );
715
716exit:
717 mbedtls_free( exported );
718 return( ok );
719}
720
Gilles Peskine02b75072018-07-01 22:31:34 +0200721static int exercise_key( psa_key_slot_t slot,
722 psa_key_usage_t usage,
723 psa_algorithm_t alg )
724{
725 int ok;
726 if( alg == 0 )
727 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
728 else if( PSA_ALG_IS_MAC( alg ) )
729 ok = exercise_mac_key( slot, usage, alg );
730 else if( PSA_ALG_IS_CIPHER( alg ) )
731 ok = exercise_cipher_key( slot, usage, alg );
732 else if( PSA_ALG_IS_AEAD( alg ) )
733 ok = exercise_aead_key( slot, usage, alg );
734 else if( PSA_ALG_IS_SIGN( alg ) )
735 ok = exercise_signature_key( slot, usage, alg );
736 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
737 ok = exercise_asymmetric_encryption_key( slot, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200738 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
739 ok = exercise_key_derivation_key( slot, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200740 else
741 {
742 char message[40];
743 mbedtls_snprintf( message, sizeof( message ),
744 "No code to exercise alg=0x%08lx",
745 (unsigned long) alg );
746 test_fail( message, __LINE__, __FILE__ );
747 ok = 0;
748 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200749
750 ok = ok && exercise_export_key( slot, usage );
751 ok = ok && exercise_export_public_key( slot );
752
Gilles Peskine02b75072018-07-01 22:31:34 +0200753 return( ok );
754}
755
Gilles Peskinee59236f2018-01-27 23:32:46 +0100756/* END_HEADER */
757
758/* BEGIN_DEPENDENCIES
759 * depends_on:MBEDTLS_PSA_CRYPTO_C
760 * END_DEPENDENCIES
761 */
762
763/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200764void static_checks( )
765{
766 size_t max_truncated_mac_size =
767 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
768
769 /* Check that the length for a truncated MAC always fits in the algorithm
770 * encoding. The shifted mask is the maximum truncated value. The
771 * untruncated algorithm may be one byte larger. */
772 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
773}
774/* END_CASE */
775
776/* BEGIN_CASE */
Gilles Peskine2d277862018-06-18 15:41:12 +0200777void init_deinit( )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100778{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100779 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100780 int i;
781 for( i = 0; i <= 1; i++ )
782 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100783 status = psa_crypto_init( );
784 TEST_ASSERT( status == PSA_SUCCESS );
785 status = psa_crypto_init( );
786 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100787 mbedtls_psa_crypto_free( );
788 }
789}
790/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100791
792/* BEGIN_CASE */
Gilles Peskine996deb12018-08-01 15:45:45 +0200793void fill_slots( int max_arg )
794{
795 /* Fill all the slots until we run out of memory or out of slots,
796 * or until some limit specified in the test data for the sake of
797 * implementations with an essentially unlimited number of slots.
798 * This test assumes that available slots are numbered from 1. */
799
800 psa_key_slot_t slot;
801 psa_key_slot_t max = 0;
802 psa_key_policy_t policy;
803 uint8_t exported[sizeof( max )];
804 size_t exported_size;
805 psa_status_t status;
806
807 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
808
809 psa_key_policy_init( &policy );
810 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
811
812 for( max = 1; max <= (size_t) max_arg; max++ )
813 {
814 status = psa_set_key_policy( max, &policy );
815 /* Stop filling slots if we run out of memory or out of
816 * available slots. */
817 TEST_ASSERT( status == PSA_SUCCESS ||
818 status == PSA_ERROR_INSUFFICIENT_MEMORY ||
819 status == PSA_ERROR_INVALID_ARGUMENT );
820 if( status != PSA_SUCCESS )
821 break;
822 status = psa_import_key( max, PSA_KEY_TYPE_RAW_DATA,
823 (uint8_t*) &max, sizeof( max ) );
824 /* Since psa_set_key_policy succeeded, we know that the slot
825 * number is valid. But we may legitimately run out of memory. */
826 TEST_ASSERT( status == PSA_SUCCESS ||
827 status == PSA_ERROR_INSUFFICIENT_MEMORY );
828 if( status != PSA_SUCCESS )
829 break;
830 }
831 /* `max` is now the first slot number that wasn't filled. */
832 max -= 1;
833
834 for( slot = 1; slot <= max; slot++ )
835 {
836 TEST_ASSERT( psa_export_key( slot,
837 exported, sizeof( exported ),
838 &exported_size ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200839 ASSERT_COMPARE( &slot, sizeof( slot ), exported, exported_size );
Gilles Peskine996deb12018-08-01 15:45:45 +0200840 }
841
842exit:
Gilles Peskine9a056342018-08-01 15:46:54 +0200843 /* Do not destroy the keys. mbedtls_psa_crypto_free() should do it. */
Gilles Peskine996deb12018-08-01 15:45:45 +0200844 mbedtls_psa_crypto_free( );
845}
846/* END_CASE */
847
848/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200849void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100850{
851 int slot = 1;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200852 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100853 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100854
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100855 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300856 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100857 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
858
Gilles Peskine4abf7412018-06-18 16:35:34 +0200859 status = psa_import_key( slot, type, data->x, data->len );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200860 TEST_ASSERT( status == expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100861 if( status == PSA_SUCCESS )
862 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
863
864exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100865 mbedtls_psa_crypto_free( );
866}
867/* END_CASE */
868
869/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200870void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
871{
872 int slot = 1;
873 size_t bits = bits_arg;
874 psa_status_t expected_status = expected_status_arg;
875 psa_status_t status;
876 psa_key_type_t type =
877 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
878 size_t buffer_size = /* Slight overapproximations */
879 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200880 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200881 unsigned char *p;
882 int ret;
883 size_t length;
884
885 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200886 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200887
888 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
889 bits, keypair ) ) >= 0 );
890 length = ret;
891
892 /* Try importing the key */
893 status = psa_import_key( slot, type, p, length );
894 TEST_ASSERT( status == expected_status );
895 if( status == PSA_SUCCESS )
896 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
897
898exit:
899 mbedtls_free( buffer );
900 mbedtls_psa_crypto_free( );
901}
902/* END_CASE */
903
904/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300905void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300906 int type_arg,
907 int alg_arg,
908 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100909 int expected_bits,
910 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200911 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100912 int canonical_input )
913{
914 int slot = 1;
915 int slot2 = slot + 1;
916 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200917 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200918 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100919 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100920 unsigned char *exported = NULL;
921 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100922 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100923 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100924 size_t reexported_length;
925 psa_key_type_t got_type;
926 size_t got_bits;
Gilles Peskinedec72612018-06-18 18:12:37 +0200927 psa_key_policy_t policy;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100928
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100929 TEST_ASSERT( data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +0300930 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) );
Moran Pekercb088e72018-07-17 17:36:59 +0300931 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200932 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100933 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200934 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100935 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
936
mohammad1603a97cb8c2018-03-28 03:46:26 -0700937 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200938 psa_key_policy_set_usage( &policy, usage_arg, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700939 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
940
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100941 /* Import the key */
942 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200943 data->x, data->len ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100944
945 /* Test the key information */
946 TEST_ASSERT( psa_get_key_information( slot,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200947 &got_type,
948 &got_bits ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100949 TEST_ASSERT( got_type == type );
950 TEST_ASSERT( got_bits == (size_t) expected_bits );
951
952 /* Export the key */
953 status = psa_export_key( slot,
954 exported, export_size,
955 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200956 TEST_ASSERT( status == expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100957
958 /* The exported length must be set by psa_export_key() to a value between 0
959 * and export_size. On errors, the exported length must be 0. */
960 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
961 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
962 TEST_ASSERT( exported_length <= export_size );
963
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200964 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200965 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100966 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200967 {
968 TEST_ASSERT( exported_length == 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100969 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200970 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100971
Gilles Peskine8f609232018-08-11 01:24:55 +0200972 if( ! exercise_export_key( slot, usage_arg ) )
973 goto exit;
974
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100975 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200976 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100977 else
978 {
mohammad1603a97cb8c2018-03-28 03:46:26 -0700979 TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS );
980
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100981 TEST_ASSERT( psa_import_key( slot2, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200982 exported,
Gilles Peskineb67f3082018-08-07 15:33:10 +0200983 exported_length ) == PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100984 TEST_ASSERT( psa_export_key( slot2,
Gilles Peskine4abf7412018-06-18 16:35:34 +0200985 reexported,
986 export_size,
987 &reexported_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200988 ASSERT_COMPARE( exported, exported_length,
989 reexported, reexported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100990 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100991 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100992
993destroy:
994 /* Destroy the key */
995 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
996 TEST_ASSERT( psa_get_key_information(
997 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
998
999exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001000 mbedtls_free( exported );
1001 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001002 mbedtls_psa_crypto_free( );
1003}
1004/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001005
Moran Pekerf709f4a2018-06-06 17:26:04 +03001006/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001007void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001008 int type_arg,
1009 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001010 int export_size_delta,
1011 int expected_export_status_arg,
1012 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001013{
1014 int slot = 1;
1015 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001016 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001017 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001018 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001019 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001020 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001021 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskinedec72612018-06-18 18:12:37 +02001022 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001023
Moran Pekerf709f4a2018-06-06 17:26:04 +03001024 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1025
1026 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001027 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001028 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1029
1030 /* Import the key */
1031 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001032 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001033
Gilles Peskine49c25912018-10-29 15:15:31 +01001034 /* Export the public key */
1035 ASSERT_ALLOC( exported, export_size );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001036 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001037 exported, export_size,
1038 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001039 TEST_ASSERT( status == expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001040 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001041 {
1042 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1043 size_t bits;
1044 TEST_ASSERT( psa_get_key_information( slot, NULL, &bits ) ==
1045 PSA_SUCCESS );
1046 TEST_ASSERT( expected_public_key->len <=
1047 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001048 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1049 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001050 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001051
1052exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001053 mbedtls_free( exported );
Gilles Peskine49c25912018-10-29 15:15:31 +01001054 psa_destroy_key( slot );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001055 mbedtls_psa_crypto_free( );
1056}
1057/* END_CASE */
1058
Gilles Peskine20035e32018-02-03 22:44:14 +01001059/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001060void import_and_exercise_key( data_t *data,
1061 int type_arg,
1062 int bits_arg,
1063 int alg_arg )
1064{
1065 int slot = 1;
1066 psa_key_type_t type = type_arg;
1067 size_t bits = bits_arg;
1068 psa_algorithm_t alg = alg_arg;
1069 psa_key_usage_t usage =
1070 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
1071 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1072 PSA_KEY_USAGE_VERIFY :
1073 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
1074 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1075 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
1076 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1077 PSA_KEY_USAGE_ENCRYPT :
1078 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
Gilles Peskineea0fb492018-07-12 17:17:20 +02001079 PSA_ALG_IS_KEY_DERIVATION( alg ) ? PSA_KEY_USAGE_DERIVE :
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001080 0 );
1081 psa_key_policy_t policy;
1082 psa_key_type_t got_type;
1083 size_t got_bits;
1084 psa_status_t status;
1085
1086 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1087
1088 psa_key_policy_init( &policy );
1089 psa_key_policy_set_usage( &policy, usage, alg );
1090 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1091
1092 /* Import the key */
1093 status = psa_import_key( slot, type, data->x, data->len );
1094 TEST_ASSERT( status == PSA_SUCCESS );
1095
1096 /* Test the key information */
1097 TEST_ASSERT( psa_get_key_information( slot,
1098 &got_type,
1099 &got_bits ) == PSA_SUCCESS );
1100 TEST_ASSERT( got_type == type );
1101 TEST_ASSERT( got_bits == bits );
1102
1103 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02001104 if( ! exercise_key( slot, usage, alg ) )
1105 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001106
1107exit:
1108 psa_destroy_key( slot );
1109 mbedtls_psa_crypto_free( );
1110}
1111/* END_CASE */
1112
1113/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001114void key_policy( int usage_arg, int alg_arg )
1115{
1116 int key_slot = 1;
1117 psa_algorithm_t alg = alg_arg;
1118 psa_key_usage_t usage = usage_arg;
1119 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1120 unsigned char key[32] = {0};
1121 psa_key_policy_t policy_set;
1122 psa_key_policy_t policy_get;
1123
1124 memset( key, 0x2a, sizeof( key ) );
1125
1126 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1127
1128 psa_key_policy_init( &policy_set );
1129 psa_key_policy_init( &policy_get );
1130
1131 psa_key_policy_set_usage( &policy_set, usage, alg );
1132
1133 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
1134 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
1135 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
1136
1137 TEST_ASSERT( psa_import_key( key_slot, key_type,
1138 key, sizeof( key ) ) == PSA_SUCCESS );
1139
1140 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
1141
1142 TEST_ASSERT( policy_get.usage == policy_set.usage );
1143 TEST_ASSERT( policy_get.alg == policy_set.alg );
1144
1145exit:
1146 psa_destroy_key( key_slot );
1147 mbedtls_psa_crypto_free( );
1148}
1149/* END_CASE */
1150
1151/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001152void mac_key_policy( int policy_usage,
1153 int policy_alg,
1154 int key_type,
1155 data_t *key_data,
1156 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001157{
1158 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +02001159 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001160 psa_mac_operation_t operation;
1161 psa_status_t status;
1162 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001163
1164 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1165
1166 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001167 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001168 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1169
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001170 TEST_ASSERT( psa_import_key( key_slot, key_type,
1171 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001172
Gilles Peskine89167cb2018-07-08 20:12:23 +02001173 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001174 if( policy_alg == exercise_alg &&
1175 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1176 TEST_ASSERT( status == PSA_SUCCESS );
1177 else
1178 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1179 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001180
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001181 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001182 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001183 if( policy_alg == exercise_alg &&
1184 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +02001185 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001186 else
1187 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1188
1189exit:
1190 psa_mac_abort( &operation );
1191 psa_destroy_key( key_slot );
1192 mbedtls_psa_crypto_free( );
1193}
1194/* END_CASE */
1195
1196/* BEGIN_CASE */
1197void cipher_key_policy( int policy_usage,
1198 int policy_alg,
1199 int key_type,
1200 data_t *key_data,
1201 int exercise_alg )
1202{
1203 int key_slot = 1;
1204 psa_key_policy_t policy;
1205 psa_cipher_operation_t operation;
1206 psa_status_t status;
1207
1208 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1209
1210 psa_key_policy_init( &policy );
1211 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1212 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1213
1214 TEST_ASSERT( psa_import_key( key_slot, key_type,
1215 key_data->x, key_data->len ) == PSA_SUCCESS );
1216
Gilles Peskinefe119512018-07-08 21:39:34 +02001217 status = psa_cipher_encrypt_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_ENCRYPT ) != 0 )
1220 TEST_ASSERT( status == PSA_SUCCESS );
1221 else
1222 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1223 psa_cipher_abort( &operation );
1224
Gilles Peskinefe119512018-07-08 21:39:34 +02001225 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001226 if( policy_alg == exercise_alg &&
1227 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1228 TEST_ASSERT( status == PSA_SUCCESS );
1229 else
1230 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1231
1232exit:
1233 psa_cipher_abort( &operation );
1234 psa_destroy_key( key_slot );
1235 mbedtls_psa_crypto_free( );
1236}
1237/* END_CASE */
1238
1239/* BEGIN_CASE */
1240void aead_key_policy( int policy_usage,
1241 int policy_alg,
1242 int key_type,
1243 data_t *key_data,
1244 int nonce_length_arg,
1245 int tag_length_arg,
1246 int exercise_alg )
1247{
1248 int key_slot = 1;
1249 psa_key_policy_t policy;
1250 psa_status_t status;
1251 unsigned char nonce[16] = {0};
1252 size_t nonce_length = nonce_length_arg;
1253 unsigned char tag[16];
1254 size_t tag_length = tag_length_arg;
1255 size_t output_length;
1256
1257 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1258 TEST_ASSERT( tag_length <= sizeof( tag ) );
1259
1260 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1261
1262 psa_key_policy_init( &policy );
1263 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1264 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1265
1266 TEST_ASSERT( psa_import_key( key_slot, key_type,
1267 key_data->x, key_data->len ) == PSA_SUCCESS );
1268
1269 status = psa_aead_encrypt( key_slot, exercise_alg,
1270 nonce, nonce_length,
1271 NULL, 0,
1272 NULL, 0,
1273 tag, tag_length,
1274 &output_length );
1275 if( policy_alg == exercise_alg &&
1276 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1277 TEST_ASSERT( status == PSA_SUCCESS );
1278 else
1279 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1280
1281 memset( tag, 0, sizeof( tag ) );
1282 status = psa_aead_decrypt( key_slot, exercise_alg,
1283 nonce, nonce_length,
1284 NULL, 0,
1285 tag, tag_length,
1286 NULL, 0,
1287 &output_length );
1288 if( policy_alg == exercise_alg &&
1289 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1290 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1291 else
1292 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1293
1294exit:
1295 psa_destroy_key( key_slot );
1296 mbedtls_psa_crypto_free( );
1297}
1298/* END_CASE */
1299
1300/* BEGIN_CASE */
1301void asymmetric_encryption_key_policy( int policy_usage,
1302 int policy_alg,
1303 int key_type,
1304 data_t *key_data,
1305 int exercise_alg )
1306{
1307 int key_slot = 1;
1308 psa_key_policy_t policy;
1309 psa_status_t status;
1310 size_t key_bits;
1311 size_t buffer_length;
1312 unsigned char *buffer = NULL;
1313 size_t output_length;
1314
1315 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1316
1317 psa_key_policy_init( &policy );
1318 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1319 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1320
1321 TEST_ASSERT( psa_import_key( key_slot, key_type,
1322 key_data->x, key_data->len ) == PSA_SUCCESS );
1323
1324 TEST_ASSERT( psa_get_key_information( key_slot,
1325 NULL,
1326 &key_bits ) == PSA_SUCCESS );
1327 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1328 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001329 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001330
1331 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
1332 NULL, 0,
1333 NULL, 0,
1334 buffer, buffer_length,
1335 &output_length );
1336 if( policy_alg == exercise_alg &&
1337 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1338 TEST_ASSERT( status == PSA_SUCCESS );
1339 else
1340 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1341
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001342 if( buffer_length != 0 )
1343 memset( buffer, 0, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001344 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
1345 buffer, buffer_length,
1346 NULL, 0,
1347 buffer, buffer_length,
1348 &output_length );
1349 if( policy_alg == exercise_alg &&
1350 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1351 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
1352 else
1353 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1354
1355exit:
1356 psa_destroy_key( key_slot );
1357 mbedtls_psa_crypto_free( );
1358 mbedtls_free( buffer );
1359}
1360/* END_CASE */
1361
1362/* BEGIN_CASE */
1363void asymmetric_signature_key_policy( int policy_usage,
1364 int policy_alg,
1365 int key_type,
1366 data_t *key_data,
1367 int exercise_alg )
1368{
1369 int key_slot = 1;
1370 psa_key_policy_t policy;
1371 psa_status_t status;
1372 unsigned char payload[16] = {1};
1373 size_t payload_length = sizeof( payload );
1374 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1375 size_t signature_length;
1376
1377 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1378
1379 psa_key_policy_init( &policy );
1380 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1381 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1382
1383 TEST_ASSERT( psa_import_key( key_slot, key_type,
1384 key_data->x, key_data->len ) == PSA_SUCCESS );
1385
1386 status = psa_asymmetric_sign( key_slot, exercise_alg,
1387 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001388 signature, sizeof( signature ),
1389 &signature_length );
1390 if( policy_alg == exercise_alg &&
1391 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1392 TEST_ASSERT( status == PSA_SUCCESS );
1393 else
1394 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1395
1396 memset( signature, 0, sizeof( signature ) );
1397 status = psa_asymmetric_verify( key_slot, exercise_alg,
1398 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001399 signature, sizeof( signature ) );
1400 if( policy_alg == exercise_alg &&
1401 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1402 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1403 else
1404 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001405
1406exit:
1407 psa_destroy_key( key_slot );
1408 mbedtls_psa_crypto_free( );
1409}
1410/* END_CASE */
1411
1412/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001413void derive_key_policy( int policy_usage,
1414 int policy_alg,
1415 int key_type,
1416 data_t *key_data,
1417 int exercise_alg )
1418{
1419 int key_slot = 1;
1420 psa_key_policy_t policy;
1421 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1422 psa_status_t status;
1423
1424 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1425
1426 psa_key_policy_init( &policy );
1427 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1428 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1429
1430 TEST_ASSERT( psa_import_key( key_slot, key_type,
1431 key_data->x, key_data->len ) == PSA_SUCCESS );
1432
1433 status = psa_key_derivation( &generator, key_slot,
1434 exercise_alg,
1435 NULL, 0,
1436 NULL, 0,
1437 1 );
1438 if( policy_alg == exercise_alg &&
1439 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1440 TEST_ASSERT( status == PSA_SUCCESS );
1441 else
1442 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1443
1444exit:
1445 psa_generator_abort( &generator );
1446 psa_destroy_key( key_slot );
1447 mbedtls_psa_crypto_free( );
1448}
1449/* END_CASE */
1450
1451/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001452void key_lifetime( int lifetime_arg )
1453{
1454 int key_slot = 1;
Gilles Peskinec32f0302018-08-10 16:02:11 +02001455 psa_key_type_t key_type = PSA_KEY_TYPE_RAW_DATA;
Gilles Peskined5b33222018-06-18 22:20:03 +02001456 unsigned char key[32] = {0};
1457 psa_key_lifetime_t lifetime_set = lifetime_arg;
1458 psa_key_lifetime_t lifetime_get;
1459
1460 memset( key, 0x2a, sizeof( key ) );
1461
1462 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1463
1464 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1465 lifetime_set ) == PSA_SUCCESS );
1466
1467 TEST_ASSERT( psa_import_key( key_slot, key_type,
1468 key, sizeof( key ) ) == PSA_SUCCESS );
1469
1470 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1471 &lifetime_get ) == PSA_SUCCESS );
1472
1473 TEST_ASSERT( lifetime_get == lifetime_set );
1474
1475exit:
1476 psa_destroy_key( key_slot );
1477 mbedtls_psa_crypto_free( );
1478}
1479/* END_CASE */
1480
1481/* BEGIN_CASE */
1482void key_lifetime_set_fail( int key_slot_arg,
1483 int lifetime_arg,
1484 int expected_status_arg )
1485{
1486 psa_key_slot_t key_slot = key_slot_arg;
1487 psa_key_lifetime_t lifetime_set = lifetime_arg;
1488 psa_status_t actual_status;
1489 psa_status_t expected_status = expected_status_arg;
1490
1491 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1492
1493 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1494
1495 if( actual_status == PSA_SUCCESS )
1496 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1497
1498 TEST_ASSERT( expected_status == actual_status );
1499
1500exit:
1501 psa_destroy_key( key_slot );
1502 mbedtls_psa_crypto_free( );
1503}
1504/* END_CASE */
1505
1506/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001507void hash_setup( int alg_arg,
1508 int expected_status_arg )
1509{
1510 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001511 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001512 psa_hash_operation_t operation;
1513 psa_status_t status;
1514
1515 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1516
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001517 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001518 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001519 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001520
1521exit:
1522 mbedtls_psa_crypto_free( );
1523}
1524/* END_CASE */
1525
1526/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001527void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001528{
1529 psa_algorithm_t alg = alg_arg;
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001530 unsigned char actual_hash[PSA_HASH_MAX_SIZE];
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001531 size_t actual_hash_length;
1532 psa_hash_operation_t operation;
1533
Gilles Peskine69c12672018-06-28 00:07:19 +02001534 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1535 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1536
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001537 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001538 TEST_ASSERT( expected_hash != NULL );
1539 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1540 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001541
1542 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1543
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001544 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001545 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001546 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001547 TEST_ASSERT( psa_hash_finish( &operation,
1548 actual_hash, sizeof( actual_hash ),
1549 &actual_hash_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001550 ASSERT_COMPARE( expected_hash->x, expected_hash->len,
1551 actual_hash, actual_hash_length );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001552
1553exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001554 mbedtls_psa_crypto_free( );
1555}
1556/* END_CASE */
1557
1558/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001559void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001560{
1561 psa_algorithm_t alg = alg_arg;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001562 psa_hash_operation_t operation;
1563
Gilles Peskine69c12672018-06-28 00:07:19 +02001564 TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) );
1565 TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE );
1566
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001567 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001568 TEST_ASSERT( expected_hash != NULL );
1569 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1570 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001571
1572 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1573
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001574 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001575 TEST_ASSERT( psa_hash_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001576 input->x,
1577 input->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001578 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001579 expected_hash->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001580 expected_hash->len ) == PSA_SUCCESS );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001581
1582exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001583 mbedtls_psa_crypto_free( );
1584}
1585/* END_CASE */
1586
1587/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001588void mac_setup( int key_type_arg,
1589 data_t *key,
1590 int alg_arg,
1591 int expected_status_arg )
1592{
1593 int key_slot = 1;
1594 psa_key_type_t key_type = key_type_arg;
1595 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001596 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001597 psa_mac_operation_t operation;
1598 psa_key_policy_t policy;
1599 psa_status_t status;
1600
1601 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1602
1603 psa_key_policy_init( &policy );
1604 psa_key_policy_set_usage( &policy,
1605 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1606 alg );
1607 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1608
1609 TEST_ASSERT( psa_import_key( key_slot, key_type,
1610 key->x, key->len ) == PSA_SUCCESS );
1611
Gilles Peskine89167cb2018-07-08 20:12:23 +02001612 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001613 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001614 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001615
1616exit:
1617 psa_destroy_key( key_slot );
1618 mbedtls_psa_crypto_free( );
1619}
1620/* END_CASE */
1621
1622/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001623void mac_sign( int key_type_arg,
1624 data_t *key,
1625 int alg_arg,
1626 data_t *input,
1627 data_t *expected_mac )
1628{
1629 int key_slot = 1;
1630 psa_key_type_t key_type = key_type_arg;
1631 psa_algorithm_t alg = alg_arg;
1632 psa_mac_operation_t operation;
1633 psa_key_policy_t policy;
1634 /* Leave a little extra room in the output buffer. At the end of the
1635 * test, we'll check that the implementation didn't overwrite onto
1636 * this extra room. */
1637 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
1638 size_t mac_buffer_size =
1639 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
1640 size_t mac_length = 0;
1641
1642 memset( actual_mac, '+', sizeof( actual_mac ) );
1643 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
1644 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
1645
1646 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1647
1648 psa_key_policy_init( &policy );
1649 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
1650 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1651
1652 TEST_ASSERT( psa_import_key( key_slot, key_type,
1653 key->x, key->len ) == PSA_SUCCESS );
1654
1655 /* Calculate the MAC. */
1656 TEST_ASSERT( psa_mac_sign_setup( &operation,
1657 key_slot, alg ) == PSA_SUCCESS );
1658 TEST_ASSERT( psa_mac_update( &operation,
1659 input->x, input->len ) == PSA_SUCCESS );
1660 TEST_ASSERT( psa_mac_sign_finish( &operation,
1661 actual_mac, mac_buffer_size,
1662 &mac_length ) == PSA_SUCCESS );
1663
1664 /* Compare with the expected value. */
1665 TEST_ASSERT( mac_length == expected_mac->len );
1666 TEST_ASSERT( memcmp( actual_mac, expected_mac->x, mac_length ) == 0 );
1667
1668 /* Verify that the end of the buffer is untouched. */
1669 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
1670 sizeof( actual_mac ) - mac_length ) );
1671
1672exit:
1673 psa_destroy_key( key_slot );
1674 mbedtls_psa_crypto_free( );
1675}
1676/* END_CASE */
1677
1678/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001679void mac_verify( int key_type_arg,
1680 data_t *key,
1681 int alg_arg,
1682 data_t *input,
1683 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001684{
1685 int key_slot = 1;
1686 psa_key_type_t key_type = key_type_arg;
1687 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001688 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001689 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001690
Gilles Peskine69c12672018-06-28 00:07:19 +02001691 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1692
Gilles Peskine8c9def32018-02-08 10:02:12 +01001693 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001694 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001695 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001696 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001697 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1698 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001699
1700 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1701
mohammad16036df908f2018-04-02 08:34:15 -07001702 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001703 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07001704 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1705
Gilles Peskine8c9def32018-02-08 10:02:12 +01001706 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001707 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001708
Gilles Peskine89167cb2018-07-08 20:12:23 +02001709 TEST_ASSERT( psa_mac_verify_setup( &operation,
1710 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001711 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1712 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001713 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02001714 TEST_ASSERT( psa_mac_verify_finish( &operation,
1715 expected_mac->x,
1716 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001717
1718exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001719 psa_destroy_key( key_slot );
1720 mbedtls_psa_crypto_free( );
1721}
1722/* END_CASE */
1723
1724/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001725void cipher_setup( int key_type_arg,
1726 data_t *key,
1727 int alg_arg,
1728 int expected_status_arg )
1729{
1730 int key_slot = 1;
1731 psa_key_type_t key_type = key_type_arg;
1732 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001733 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001734 psa_cipher_operation_t operation;
1735 psa_key_policy_t policy;
1736 psa_status_t status;
1737
1738 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1739
1740 psa_key_policy_init( &policy );
1741 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1742 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1743
1744 TEST_ASSERT( psa_import_key( key_slot, key_type,
1745 key->x, key->len ) == PSA_SUCCESS );
1746
Gilles Peskinefe119512018-07-08 21:39:34 +02001747 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001748 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001749 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001750
1751exit:
1752 psa_destroy_key( key_slot );
1753 mbedtls_psa_crypto_free( );
1754}
1755/* END_CASE */
1756
1757/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001758void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001759 data_t *key,
1760 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001761 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001762{
1763 int key_slot = 1;
1764 psa_status_t status;
1765 psa_key_type_t key_type = key_type_arg;
1766 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001767 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001768 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001769 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001770 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001771 size_t output_buffer_size = 0;
1772 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001773 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001774 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001775 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001776
Gilles Peskine50e586b2018-06-08 14:28:46 +02001777 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001778 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001779 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001780 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1781 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1782 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001783
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001784 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1785 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001786
1787 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1788
Moran Pekered346952018-07-05 15:22:45 +03001789 psa_key_policy_init( &policy );
1790 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1791 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1792
Gilles Peskine50e586b2018-06-08 14:28:46 +02001793 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001794 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001795
Gilles Peskinefe119512018-07-08 21:39:34 +02001796 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1797 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001798
Gilles Peskinefe119512018-07-08 21:39:34 +02001799 TEST_ASSERT( psa_cipher_set_iv( &operation,
1800 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001801 output_buffer_size = (size_t) input->len +
1802 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001803 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001804
Gilles Peskine4abf7412018-06-18 16:35:34 +02001805 TEST_ASSERT( psa_cipher_update( &operation,
1806 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001807 output, output_buffer_size,
1808 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001809 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001810 status = psa_cipher_finish( &operation,
1811 output + function_output_length,
1812 output_buffer_size,
1813 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001814 total_output_length += function_output_length;
1815
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001816 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001817 if( expected_status == PSA_SUCCESS )
1818 {
1819 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001820 ASSERT_COMPARE( expected_output->x, expected_output->len,
1821 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001822 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001823
Gilles Peskine50e586b2018-06-08 14:28:46 +02001824exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001825 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001826 psa_destroy_key( key_slot );
1827 mbedtls_psa_crypto_free( );
1828}
1829/* END_CASE */
1830
1831/* BEGIN_CASE */
1832void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001833 data_t *key,
1834 data_t *input,
1835 int first_part_size,
1836 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001837{
1838 int key_slot = 1;
1839 psa_key_type_t key_type = key_type_arg;
1840 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001841 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001842 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001843 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001844 size_t output_buffer_size = 0;
1845 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001846 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001847 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001848 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001849
Gilles Peskine50e586b2018-06-08 14:28:46 +02001850 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001851 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001852 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001853 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1854 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1855 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001856
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001857 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1858 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001859
1860 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1861
Moran Pekered346952018-07-05 15:22:45 +03001862 psa_key_policy_init( &policy );
1863 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1864 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1865
Gilles Peskine50e586b2018-06-08 14:28:46 +02001866 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001867 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001868
Gilles Peskinefe119512018-07-08 21:39:34 +02001869 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
1870 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001871
Gilles Peskinefe119512018-07-08 21:39:34 +02001872 TEST_ASSERT( psa_cipher_set_iv( &operation,
1873 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03001874 output_buffer_size = (size_t) input->len +
1875 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001876 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001877
Gilles Peskine4abf7412018-06-18 16:35:34 +02001878 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001879 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001880 output, output_buffer_size,
1881 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001882 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001883 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001884 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001885 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001886 output, output_buffer_size,
1887 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001888 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001889 TEST_ASSERT( psa_cipher_finish( &operation,
1890 output + function_output_length,
1891 output_buffer_size,
1892 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001893 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001894 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1895
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001896 ASSERT_COMPARE( expected_output->x, expected_output->len,
1897 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001898
1899exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001900 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001901 psa_destroy_key( key_slot );
1902 mbedtls_psa_crypto_free( );
1903}
1904/* END_CASE */
1905
1906/* BEGIN_CASE */
1907void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001908 data_t *key,
1909 data_t *input,
1910 int first_part_size,
1911 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001912{
1913 int key_slot = 1;
1914
1915 psa_key_type_t key_type = key_type_arg;
1916 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001917 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001918 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001919 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001920 size_t output_buffer_size = 0;
1921 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001922 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001923 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03001924 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001925
Gilles Peskine50e586b2018-06-08 14:28:46 +02001926 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001927 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001928 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001929 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
1930 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1931 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001932
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001933 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
1934 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001935
1936 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1937
Moran Pekered346952018-07-05 15:22:45 +03001938 psa_key_policy_init( &policy );
1939 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
1940 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1941
Gilles Peskine50e586b2018-06-08 14:28:46 +02001942 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001943 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001944
Gilles Peskinefe119512018-07-08 21:39:34 +02001945 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
1946 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001947
Gilles Peskinefe119512018-07-08 21:39:34 +02001948 TEST_ASSERT( psa_cipher_set_iv( &operation,
1949 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001950
mohammad16033d91abe2018-07-03 13:15:54 +03001951 output_buffer_size = (size_t) input->len +
1952 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001953 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001954
Gilles Peskine4abf7412018-06-18 16:35:34 +02001955 TEST_ASSERT( (unsigned int) first_part_size < input->len );
1956 TEST_ASSERT( psa_cipher_update( &operation,
1957 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001958 output, output_buffer_size,
1959 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001960 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001961 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001962 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001963 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02001964 output, output_buffer_size,
1965 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001966 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001967 TEST_ASSERT( psa_cipher_finish( &operation,
1968 output + function_output_length,
1969 output_buffer_size,
1970 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02001971 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001972 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
1973
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001974 ASSERT_COMPARE( expected_output->x, expected_output->len,
1975 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001976
1977exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001978 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02001979 psa_destroy_key( key_slot );
1980 mbedtls_psa_crypto_free( );
1981}
1982/* END_CASE */
1983
Gilles Peskine50e586b2018-06-08 14:28:46 +02001984/* BEGIN_CASE */
1985void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001986 data_t *key,
1987 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001988 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001989{
1990 int key_slot = 1;
1991 psa_status_t status;
1992 psa_key_type_t key_type = key_type_arg;
1993 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001994 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001995 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001996 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03001997 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001998 size_t output_buffer_size = 0;
1999 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002000 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002001 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002002 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002003
Gilles Peskine50e586b2018-06-08 14:28:46 +02002004 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002005 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002006 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002007 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2008 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2009 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002010
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002011 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2012 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002013
2014 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2015
Moran Pekered346952018-07-05 15:22:45 +03002016 psa_key_policy_init( &policy );
2017 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2018 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2019
Gilles Peskine50e586b2018-06-08 14:28:46 +02002020 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002021 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002022
Gilles Peskinefe119512018-07-08 21:39:34 +02002023 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2024 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002025
Gilles Peskinefe119512018-07-08 21:39:34 +02002026 TEST_ASSERT( psa_cipher_set_iv( &operation,
2027 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002028
mohammad16033d91abe2018-07-03 13:15:54 +03002029 output_buffer_size = (size_t) input->len +
2030 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002031 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002032
Gilles Peskine4abf7412018-06-18 16:35:34 +02002033 TEST_ASSERT( psa_cipher_update( &operation,
2034 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002035 output, output_buffer_size,
2036 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002037 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002038 status = psa_cipher_finish( &operation,
2039 output + function_output_length,
2040 output_buffer_size,
2041 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002042 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002043 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002044
2045 if( expected_status == PSA_SUCCESS )
2046 {
2047 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002048 ASSERT_COMPARE( expected_output->x, expected_output->len,
2049 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002050 }
2051
Gilles Peskine50e586b2018-06-08 14:28:46 +02002052exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002053 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002054 psa_destroy_key( key_slot );
2055 mbedtls_psa_crypto_free( );
2056}
2057/* END_CASE */
2058
Gilles Peskine50e586b2018-06-08 14:28:46 +02002059/* BEGIN_CASE */
2060void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002061 data_t *key,
2062 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002063{
2064 int key_slot = 1;
2065 psa_key_type_t key_type = key_type_arg;
2066 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002067 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002068 size_t iv_size = 16;
2069 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002070 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002071 size_t output1_size = 0;
2072 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002073 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002074 size_t output2_size = 0;
2075 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002076 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002077 psa_cipher_operation_t operation1;
2078 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002079 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002080
mohammad1603d7d7ba52018-03-12 18:51:53 +02002081 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002082 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002083 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2084 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002085
mohammad1603d7d7ba52018-03-12 18:51:53 +02002086 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2087
Moran Pekered346952018-07-05 15:22:45 +03002088 psa_key_policy_init( &policy );
2089 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2090 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2091
mohammad1603d7d7ba52018-03-12 18:51:53 +02002092 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002093 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002094
Gilles Peskinefe119512018-07-08 21:39:34 +02002095 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2096 key_slot, alg ) == PSA_SUCCESS );
2097 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2098 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002099
Gilles Peskinefe119512018-07-08 21:39:34 +02002100 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2101 iv, iv_size,
2102 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002103 output1_size = (size_t) input->len +
2104 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002105 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002106
Gilles Peskine4abf7412018-06-18 16:35:34 +02002107 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002108 output1, output1_size,
2109 &output1_length ) == PSA_SUCCESS );
2110 TEST_ASSERT( psa_cipher_finish( &operation1,
2111 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002112 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002113
Gilles Peskine048b7f02018-06-08 14:20:49 +02002114 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002115
2116 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2117
2118 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002119 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002120
Gilles Peskinefe119512018-07-08 21:39:34 +02002121 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2122 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002123 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2124 output2, output2_size,
2125 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002126 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002127 TEST_ASSERT( psa_cipher_finish( &operation2,
2128 output2 + output2_length,
2129 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002130 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002131
Gilles Peskine048b7f02018-06-08 14:20:49 +02002132 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002133
Janos Follath25c4fa82018-07-06 16:23:25 +01002134 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002135
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002136 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002137
2138exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002139 mbedtls_free( output1 );
2140 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03002141 psa_destroy_key( key_slot );
2142 mbedtls_psa_crypto_free( );
2143}
2144/* END_CASE */
2145
2146/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002147void cipher_verify_output_multipart( int alg_arg,
2148 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002149 data_t *key,
2150 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002151 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002152{
2153 int key_slot = 1;
2154 psa_key_type_t key_type = key_type_arg;
2155 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002156 unsigned char iv[16] = {0};
2157 size_t iv_size = 16;
2158 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002159 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002160 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002161 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002162 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002163 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002164 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002165 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002166 psa_cipher_operation_t operation1;
2167 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002168 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03002169
Moran Pekerded84402018-06-06 16:36:50 +03002170 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002171 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002172 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2173 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002174
Moran Pekerded84402018-06-06 16:36:50 +03002175 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2176
Moran Pekered346952018-07-05 15:22:45 +03002177 psa_key_policy_init( &policy );
2178 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2179 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2180
Moran Pekerded84402018-06-06 16:36:50 +03002181 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002182 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002183
Gilles Peskinefe119512018-07-08 21:39:34 +02002184 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2185 key_slot, alg ) == PSA_SUCCESS );
2186 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2187 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002188
Gilles Peskinefe119512018-07-08 21:39:34 +02002189 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2190 iv, iv_size,
2191 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002192 output1_buffer_size = (size_t) input->len +
2193 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002194 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002195
Gilles Peskine4abf7412018-06-18 16:35:34 +02002196 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002197
itayzafrir3e02b3b2018-06-12 17:06:52 +03002198 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002199 output1, output1_buffer_size,
2200 &function_output_length ) == PSA_SUCCESS );
2201 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002202
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002203 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002204 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002205 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002206 output1, output1_buffer_size,
2207 &function_output_length ) == PSA_SUCCESS );
2208 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002209
Gilles Peskine048b7f02018-06-08 14:20:49 +02002210 TEST_ASSERT( psa_cipher_finish( &operation1,
2211 output1 + output1_length,
2212 output1_buffer_size - output1_length,
2213 &function_output_length ) == PSA_SUCCESS );
2214 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002215
2216 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2217
Gilles Peskine048b7f02018-06-08 14:20:49 +02002218 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002219 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002220
Gilles Peskinefe119512018-07-08 21:39:34 +02002221 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2222 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002223
2224 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002225 output2, output2_buffer_size,
2226 &function_output_length ) == PSA_SUCCESS );
2227 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002228
Gilles Peskine048b7f02018-06-08 14:20:49 +02002229 TEST_ASSERT( psa_cipher_update( &operation2,
2230 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002231 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002232 output2, output2_buffer_size,
2233 &function_output_length ) == PSA_SUCCESS );
2234 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002235
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002236 TEST_ASSERT( psa_cipher_finish( &operation2,
2237 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002238 output2_buffer_size - output2_length,
2239 &function_output_length ) == PSA_SUCCESS );
2240 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002241
Janos Follath25c4fa82018-07-06 16:23:25 +01002242 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002243
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002244 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002245
2246exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002247 mbedtls_free( output1 );
2248 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002249 psa_destroy_key( key_slot );
2250 mbedtls_psa_crypto_free( );
2251}
2252/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002253
Gilles Peskine20035e32018-02-03 22:44:14 +01002254/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002255void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002256 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002257 data_t *nonce,
2258 data_t *additional_data,
2259 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002260 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002261{
2262 int slot = 1;
2263 psa_key_type_t key_type = key_type_arg;
2264 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002265 unsigned char *output_data = NULL;
2266 size_t output_size = 0;
2267 size_t output_length = 0;
2268 unsigned char *output_data2 = NULL;
2269 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002270 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002271 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002272 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002273
Gilles Peskinea1cac842018-06-11 19:33:02 +02002274 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002275 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002276 TEST_ASSERT( nonce != NULL );
2277 TEST_ASSERT( additional_data != NULL );
2278 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2279 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2280 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2281 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2282
Gilles Peskine4abf7412018-06-18 16:35:34 +02002283 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002284 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002285
2286 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2287
2288 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002289 psa_key_policy_set_usage( &policy,
2290 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2291 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002292 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2293
2294 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002295 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002296
2297 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002298 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002299 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002300 additional_data->len,
2301 input_data->x, input_data->len,
2302 output_data, output_size,
2303 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002304
2305 if( PSA_SUCCESS == expected_result )
2306 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002307 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002308
2309 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002310 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002311 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002312 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002313 output_data, output_length,
2314 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002315 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002316
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002317 ASSERT_COMPARE( input_data->x, input_data->len,
2318 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002319 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002320
Gilles Peskinea1cac842018-06-11 19:33:02 +02002321exit:
2322 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002323 mbedtls_free( output_data );
2324 mbedtls_free( output_data2 );
2325 mbedtls_psa_crypto_free( );
2326}
2327/* END_CASE */
2328
2329/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002330void aead_encrypt( int key_type_arg, data_t *key_data,
2331 int alg_arg,
2332 data_t *nonce,
2333 data_t *additional_data,
2334 data_t *input_data,
2335 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002336{
2337 int slot = 1;
2338 psa_key_type_t key_type = key_type_arg;
2339 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002340 unsigned char *output_data = NULL;
2341 size_t output_size = 0;
2342 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002343 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002344 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002345
Gilles Peskinea1cac842018-06-11 19:33:02 +02002346 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002347 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002348 TEST_ASSERT( additional_data != NULL );
2349 TEST_ASSERT( nonce != NULL );
2350 TEST_ASSERT( expected_result != NULL );
2351 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2352 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2353 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2354 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2355 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2356
Gilles Peskine4abf7412018-06-18 16:35:34 +02002357 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002358 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002359
Gilles Peskinea1cac842018-06-11 19:33:02 +02002360 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2361
2362 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002363 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002364 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2365
2366 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002367 key_data->x,
2368 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002369
2370 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002371 nonce->x, nonce->len,
2372 additional_data->x, additional_data->len,
2373 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002374 output_data, output_size,
2375 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002376
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002377 ASSERT_COMPARE( expected_result->x, expected_result->len,
2378 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002379
Gilles Peskinea1cac842018-06-11 19:33:02 +02002380exit:
2381 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002382 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002383 mbedtls_psa_crypto_free( );
2384}
2385/* END_CASE */
2386
2387/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002388void aead_decrypt( int key_type_arg, data_t *key_data,
2389 int alg_arg,
2390 data_t *nonce,
2391 data_t *additional_data,
2392 data_t *input_data,
2393 data_t *expected_data,
2394 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002395{
2396 int slot = 1;
2397 psa_key_type_t key_type = key_type_arg;
2398 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002399 unsigned char *output_data = NULL;
2400 size_t output_size = 0;
2401 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002402 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002403 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002404 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002405
Gilles Peskinea1cac842018-06-11 19:33:02 +02002406 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002407 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002408 TEST_ASSERT( additional_data != NULL );
2409 TEST_ASSERT( nonce != NULL );
2410 TEST_ASSERT( expected_data != NULL );
2411 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2412 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2413 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2414 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2415 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2416
Gilles Peskine4abf7412018-06-18 16:35:34 +02002417 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002418 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002419
Gilles Peskinea1cac842018-06-11 19:33:02 +02002420 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2421
2422 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002423 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002424 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2425
2426 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002427 key_data->x,
2428 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002429
2430 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002431 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002432 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002433 additional_data->len,
2434 input_data->x, input_data->len,
2435 output_data, output_size,
2436 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002437
Gilles Peskine2d277862018-06-18 15:41:12 +02002438 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002439 ASSERT_COMPARE( expected_data->x, expected_data->len,
2440 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002441
Gilles Peskinea1cac842018-06-11 19:33:02 +02002442exit:
2443 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002444 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002445 mbedtls_psa_crypto_free( );
2446}
2447/* END_CASE */
2448
2449/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002450void signature_size( int type_arg,
2451 int bits,
2452 int alg_arg,
2453 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002454{
2455 psa_key_type_t type = type_arg;
2456 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002457 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002458 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2459exit:
2460 ;
2461}
2462/* END_CASE */
2463
2464/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002465void sign_deterministic( int key_type_arg, data_t *key_data,
2466 int alg_arg, data_t *input_data,
2467 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002468{
2469 int slot = 1;
2470 psa_key_type_t key_type = key_type_arg;
2471 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002472 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002473 unsigned char *signature = NULL;
2474 size_t signature_size;
2475 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002476 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002477
Gilles Peskine20035e32018-02-03 22:44:14 +01002478 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002479 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002480 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002481 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2482 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2483 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002484
2485 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2486
mohammad1603a97cb8c2018-03-28 03:46:26 -07002487 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002488 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002489 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2490
Gilles Peskine20035e32018-02-03 22:44:14 +01002491 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002492 key_data->x,
2493 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002494 TEST_ASSERT( psa_get_key_information( slot,
2495 NULL,
2496 &key_bits ) == PSA_SUCCESS );
2497
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002498 /* Allocate a buffer which has the size advertized by the
2499 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002500 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2501 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002502 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002503 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002504 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002505
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002506 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002507 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002508 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002509 signature, signature_size,
2510 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002511 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002512 ASSERT_COMPARE( output_data->x, output_data->len,
2513 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002514
2515exit:
2516 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002517 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002518 mbedtls_psa_crypto_free( );
2519}
2520/* END_CASE */
2521
2522/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002523void sign_fail( int key_type_arg, data_t *key_data,
2524 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002525 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002526{
2527 int slot = 1;
2528 psa_key_type_t key_type = key_type_arg;
2529 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002530 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002531 psa_status_t actual_status;
2532 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002533 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002534 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002535 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002536
Gilles Peskine20035e32018-02-03 22:44:14 +01002537 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002538 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002539 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2540 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2541
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002542 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002543
2544 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2545
mohammad1603a97cb8c2018-03-28 03:46:26 -07002546 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002547 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002548 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2549
Gilles Peskine20035e32018-02-03 22:44:14 +01002550 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002551 key_data->x,
2552 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002553
2554 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002555 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002556 signature, signature_size,
2557 &signature_length );
2558 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002559 /* The value of *signature_length is unspecified on error, but
2560 * whatever it is, it should be less than signature_size, so that
2561 * if the caller tries to read *signature_length bytes without
2562 * checking the error code then they don't overflow a buffer. */
2563 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002564
2565exit:
2566 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002567 mbedtls_free( signature );
2568 mbedtls_psa_crypto_free( );
2569}
2570/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002571
2572/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002573void sign_verify( int key_type_arg, data_t *key_data,
2574 int alg_arg, data_t *input_data )
2575{
2576 int slot = 1;
2577 psa_key_type_t key_type = key_type_arg;
2578 psa_algorithm_t alg = alg_arg;
2579 size_t key_bits;
2580 unsigned char *signature = NULL;
2581 size_t signature_size;
2582 size_t signature_length = 0xdeadbeef;
2583 psa_key_policy_t policy;
2584
2585 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2586
2587 psa_key_policy_init( &policy );
2588 psa_key_policy_set_usage( &policy,
2589 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2590 alg );
2591 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2592
2593 TEST_ASSERT( psa_import_key( slot, key_type,
2594 key_data->x,
2595 key_data->len ) == PSA_SUCCESS );
2596 TEST_ASSERT( psa_get_key_information( slot,
2597 NULL,
2598 &key_bits ) == PSA_SUCCESS );
2599
2600 /* Allocate a buffer which has the size advertized by the
2601 * library. */
2602 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2603 key_bits, alg );
2604 TEST_ASSERT( signature_size != 0 );
2605 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002606 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002607
2608 /* Perform the signature. */
2609 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2610 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002611 signature, signature_size,
2612 &signature_length ) == PSA_SUCCESS );
2613 /* Check that the signature length looks sensible. */
2614 TEST_ASSERT( signature_length <= signature_size );
2615 TEST_ASSERT( signature_length > 0 );
2616
2617 /* Use the library to verify that the signature is correct. */
2618 TEST_ASSERT( psa_asymmetric_verify(
2619 slot, alg,
2620 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002621 signature, signature_length ) == PSA_SUCCESS );
2622
2623 if( input_data->len != 0 )
2624 {
2625 /* Flip a bit in the input and verify that the signature is now
2626 * detected as invalid. Flip a bit at the beginning, not at the end,
2627 * because ECDSA may ignore the last few bits of the input. */
2628 input_data->x[0] ^= 1;
2629 TEST_ASSERT( psa_asymmetric_verify(
2630 slot, alg,
2631 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002632 signature,
2633 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2634 }
2635
2636exit:
2637 psa_destroy_key( slot );
2638 mbedtls_free( signature );
2639 mbedtls_psa_crypto_free( );
2640}
2641/* END_CASE */
2642
2643/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002644void asymmetric_verify( int key_type_arg, data_t *key_data,
2645 int alg_arg, data_t *hash_data,
2646 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002647{
2648 int slot = 1;
2649 psa_key_type_t key_type = key_type_arg;
2650 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002651 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002652
Gilles Peskine69c12672018-06-28 00:07:19 +02002653 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2654
itayzafrir5c753392018-05-08 11:18:38 +03002655 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002656 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002657 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002658 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2659 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2660 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002661
2662 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2663
2664 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002665 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002666 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2667
2668 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002669 key_data->x,
2670 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002671
2672 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002673 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002674 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002675 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002676exit:
2677 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002678 mbedtls_psa_crypto_free( );
2679}
2680/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002681
2682/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002683void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2684 int alg_arg, data_t *hash_data,
2685 data_t *signature_data,
2686 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002687{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002688 int slot = 1;
2689 psa_key_type_t key_type = key_type_arg;
2690 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002691 psa_status_t actual_status;
2692 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002693 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002694
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002695 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002696 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002697 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002698 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2699 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2700 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002701
2702 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2703
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002704 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002705 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002706 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2707
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002708 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002709 key_data->x,
2710 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002711
2712 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002713 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002714 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002715 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002716
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002717 TEST_ASSERT( actual_status == expected_status );
2718
2719exit:
2720 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002721 mbedtls_psa_crypto_free( );
2722}
2723/* END_CASE */
2724
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002725/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002726void asymmetric_encrypt( int key_type_arg,
2727 data_t *key_data,
2728 int alg_arg,
2729 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02002730 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02002731 int expected_output_length_arg,
2732 int expected_status_arg )
2733{
2734 int slot = 1;
2735 psa_key_type_t key_type = key_type_arg;
2736 psa_algorithm_t alg = alg_arg;
2737 size_t expected_output_length = expected_output_length_arg;
2738 size_t key_bits;
2739 unsigned char *output = NULL;
2740 size_t output_size;
2741 size_t output_length = ~0;
2742 psa_status_t actual_status;
2743 psa_status_t expected_status = expected_status_arg;
2744 psa_key_policy_t policy;
2745
2746 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2747
2748 /* Import the key */
2749 psa_key_policy_init( &policy );
2750 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2751 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2752 TEST_ASSERT( psa_import_key( slot, key_type,
2753 key_data->x,
2754 key_data->len ) == PSA_SUCCESS );
2755
2756 /* Determine the maximum output length */
2757 TEST_ASSERT( psa_get_key_information( slot,
2758 NULL,
2759 &key_bits ) == PSA_SUCCESS );
2760 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002761 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02002762
2763 /* Encrypt the input */
2764 actual_status = psa_asymmetric_encrypt( slot, alg,
2765 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002766 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02002767 output, output_size,
2768 &output_length );
2769 TEST_ASSERT( actual_status == expected_status );
2770 TEST_ASSERT( output_length == expected_output_length );
2771
Gilles Peskine68428122018-06-30 18:42:41 +02002772 /* If the label is empty, the test framework puts a non-null pointer
2773 * in label->x. Test that a null pointer works as well. */
2774 if( label->len == 0 )
2775 {
2776 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002777 if( output_size != 0 )
2778 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02002779 actual_status = psa_asymmetric_encrypt( slot, alg,
2780 input_data->x, input_data->len,
2781 NULL, label->len,
2782 output, output_size,
2783 &output_length );
2784 TEST_ASSERT( actual_status == expected_status );
2785 TEST_ASSERT( output_length == expected_output_length );
2786 }
2787
Gilles Peskine656896e2018-06-29 19:12:28 +02002788exit:
2789 psa_destroy_key( slot );
2790 mbedtls_free( output );
2791 mbedtls_psa_crypto_free( );
2792}
2793/* END_CASE */
2794
2795/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002796void asymmetric_encrypt_decrypt( int key_type_arg,
2797 data_t *key_data,
2798 int alg_arg,
2799 data_t *input_data,
2800 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002801{
2802 int slot = 1;
2803 psa_key_type_t key_type = key_type_arg;
2804 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002805 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002806 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002807 size_t output_size;
2808 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002809 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002810 size_t output2_size;
2811 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002812 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002813
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002814 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002815 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002816 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2817 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2818
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002819 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2820
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002821 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002822 psa_key_policy_set_usage( &policy,
2823 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002824 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002825 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2826
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002827 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002828 key_data->x,
2829 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002830
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002831
2832 /* Determine the maximum ciphertext length */
2833 TEST_ASSERT( psa_get_key_information( slot,
2834 NULL,
2835 &key_bits ) == PSA_SUCCESS );
2836 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002837 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002838 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002839 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002840
Gilles Peskineeebd7382018-06-08 18:11:54 +02002841 /* We test encryption by checking that encrypt-then-decrypt gives back
2842 * the original plaintext because of the non-optional random
2843 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02002844 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002845 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002846 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002847 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002848 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002849 /* We don't know what ciphertext length to expect, but check that
2850 * it looks sensible. */
2851 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002852
Gilles Peskine2d277862018-06-18 15:41:12 +02002853 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002854 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02002855 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002856 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002857 &output2_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002858 ASSERT_COMPARE( input_data->x, input_data->len,
2859 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002860
2861exit:
2862 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002863 mbedtls_free( output );
2864 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002865 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002866}
2867/* END_CASE */
2868
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002869/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002870void asymmetric_decrypt( int key_type_arg,
2871 data_t *key_data,
2872 int alg_arg,
2873 data_t *input_data,
2874 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02002875 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002876{
2877 int slot = 1;
2878 psa_key_type_t key_type = key_type_arg;
2879 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002880 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002881 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002882 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02002883 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002884
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002885 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002886 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002887 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002888 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2889 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2890 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2891
Gilles Peskine4abf7412018-06-18 16:35:34 +02002892 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002893 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002894
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002895 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2896
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002897 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002898 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002899 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2900
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002901 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002902 key_data->x,
2903 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002904
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03002905 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002906 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002907 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002908 output,
2909 output_size,
2910 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002911 ASSERT_COMPARE( expected_data->x, expected_data->len,
2912 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002913
Gilles Peskine68428122018-06-30 18:42:41 +02002914 /* If the label is empty, the test framework puts a non-null pointer
2915 * in label->x. Test that a null pointer works as well. */
2916 if( label->len == 0 )
2917 {
2918 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002919 if( output_size != 0 )
2920 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02002921 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
2922 input_data->x, input_data->len,
2923 NULL, label->len,
2924 output,
2925 output_size,
2926 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002927 ASSERT_COMPARE( expected_data->x, expected_data->len,
2928 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02002929 }
2930
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002931exit:
2932 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002933 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002934 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002935}
2936/* END_CASE */
2937
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002938/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02002939void asymmetric_decrypt_fail( int key_type_arg,
2940 data_t *key_data,
2941 int alg_arg,
2942 data_t *input_data,
2943 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02002944 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002945{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002946 int slot = 1;
2947 psa_key_type_t key_type = key_type_arg;
2948 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002949 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03002950 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002951 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002952 psa_status_t actual_status;
2953 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002954 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002955
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002956 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002957 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002958 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2959 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2960
Gilles Peskine4abf7412018-06-18 16:35:34 +02002961 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002962 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002963
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002964 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2965
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002966 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002967 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002968 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2969
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002970 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002971 key_data->x,
2972 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002973
Gilles Peskine2d277862018-06-18 15:41:12 +02002974 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002975 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002976 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002977 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02002978 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002979 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002980 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002981
Gilles Peskine68428122018-06-30 18:42:41 +02002982 /* If the label is empty, the test framework puts a non-null pointer
2983 * in label->x. Test that a null pointer works as well. */
2984 if( label->len == 0 )
2985 {
2986 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002987 if( output_size != 0 )
2988 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02002989 actual_status = psa_asymmetric_decrypt( slot, alg,
2990 input_data->x, input_data->len,
2991 NULL, label->len,
2992 output, output_size,
2993 &output_length );
2994 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02002995 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02002996 }
2997
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002998exit:
2999 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02003000 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003001 mbedtls_psa_crypto_free( );
3002}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003003/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003004
3005/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003006void derive_setup( int key_type_arg,
3007 data_t *key_data,
3008 int alg_arg,
3009 data_t *salt,
3010 data_t *label,
3011 int requested_capacity_arg,
3012 int expected_status_arg )
3013{
3014 psa_key_slot_t slot = 1;
3015 size_t key_type = key_type_arg;
3016 psa_algorithm_t alg = alg_arg;
3017 size_t requested_capacity = requested_capacity_arg;
3018 psa_status_t expected_status = expected_status_arg;
3019 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3020 psa_key_policy_t policy;
3021
3022 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3023
3024 psa_key_policy_init( &policy );
3025 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3026 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3027
3028 TEST_ASSERT( psa_import_key( slot, key_type,
3029 key_data->x,
3030 key_data->len ) == PSA_SUCCESS );
3031
3032 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3033 salt->x, salt->len,
3034 label->x, label->len,
3035 requested_capacity ) == expected_status );
3036
3037exit:
3038 psa_generator_abort( &generator );
3039 psa_destroy_key( slot );
3040 mbedtls_psa_crypto_free( );
3041}
3042/* END_CASE */
3043
3044/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003045void derive_output( int alg_arg,
3046 data_t *key_data,
3047 data_t *salt,
3048 data_t *label,
3049 int requested_capacity_arg,
3050 data_t *expected_output1,
3051 data_t *expected_output2 )
3052{
3053 psa_key_slot_t slot = 1;
3054 psa_algorithm_t alg = alg_arg;
3055 size_t requested_capacity = requested_capacity_arg;
3056 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3057 uint8_t *expected_outputs[2] =
3058 {expected_output1->x, expected_output2->x};
3059 size_t output_sizes[2] =
3060 {expected_output1->len, expected_output2->len};
3061 size_t output_buffer_size = 0;
3062 uint8_t *output_buffer = NULL;
3063 size_t expected_capacity;
3064 size_t current_capacity;
3065 psa_key_policy_t policy;
3066 psa_status_t status;
3067 unsigned i;
3068
3069 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3070 {
3071 if( output_sizes[i] > output_buffer_size )
3072 output_buffer_size = output_sizes[i];
3073 if( output_sizes[i] == 0 )
3074 expected_outputs[i] = NULL;
3075 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003076 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003077 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3078
3079 psa_key_policy_init( &policy );
3080 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3081 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3082
3083 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3084 key_data->x,
3085 key_data->len ) == PSA_SUCCESS );
3086
3087 /* Extraction phase. */
3088 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3089 salt->x, salt->len,
3090 label->x, label->len,
3091 requested_capacity ) == PSA_SUCCESS );
3092 TEST_ASSERT( psa_get_generator_capacity( &generator,
3093 &current_capacity ) ==
3094 PSA_SUCCESS );
3095 TEST_ASSERT( current_capacity == requested_capacity );
3096 expected_capacity = requested_capacity;
3097
3098 /* Expansion phase. */
3099 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3100 {
3101 /* Read some bytes. */
3102 status = psa_generator_read( &generator,
3103 output_buffer, output_sizes[i] );
3104 if( expected_capacity == 0 && output_sizes[i] == 0 )
3105 {
3106 /* Reading 0 bytes when 0 bytes are available can go either way. */
3107 TEST_ASSERT( status == PSA_SUCCESS ||
3108 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3109 continue;
3110 }
3111 else if( expected_capacity == 0 ||
3112 output_sizes[i] > expected_capacity )
3113 {
3114 /* Capacity exceeded. */
3115 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3116 expected_capacity = 0;
3117 continue;
3118 }
3119 /* Success. Check the read data. */
3120 TEST_ASSERT( status == PSA_SUCCESS );
3121 if( output_sizes[i] != 0 )
3122 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
3123 output_sizes[i] ) == 0 );
3124 /* Check the generator status. */
3125 expected_capacity -= output_sizes[i];
3126 TEST_ASSERT( psa_get_generator_capacity( &generator,
3127 &current_capacity ) ==
3128 PSA_SUCCESS );
3129 TEST_ASSERT( expected_capacity == current_capacity );
3130 }
3131 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3132
3133exit:
3134 mbedtls_free( output_buffer );
3135 psa_generator_abort( &generator );
3136 psa_destroy_key( slot );
3137 mbedtls_psa_crypto_free( );
3138}
3139/* END_CASE */
3140
3141/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003142void derive_full( int alg_arg,
3143 data_t *key_data,
3144 data_t *salt,
3145 data_t *label,
3146 int requested_capacity_arg )
3147{
3148 psa_key_slot_t slot = 1;
3149 psa_algorithm_t alg = alg_arg;
3150 size_t requested_capacity = requested_capacity_arg;
3151 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3152 unsigned char output_buffer[16];
3153 size_t expected_capacity = requested_capacity;
3154 size_t current_capacity;
3155 psa_key_policy_t policy;
3156
3157 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3158
3159 psa_key_policy_init( &policy );
3160 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3161 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3162
3163 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3164 key_data->x,
3165 key_data->len ) == PSA_SUCCESS );
3166
3167 /* Extraction phase. */
3168 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3169 salt->x, salt->len,
3170 label->x, label->len,
3171 requested_capacity ) == PSA_SUCCESS );
3172 TEST_ASSERT( psa_get_generator_capacity( &generator,
3173 &current_capacity ) ==
3174 PSA_SUCCESS );
3175 TEST_ASSERT( current_capacity == expected_capacity );
3176
3177 /* Expansion phase. */
3178 while( current_capacity > 0 )
3179 {
3180 size_t read_size = sizeof( output_buffer );
3181 if( read_size > current_capacity )
3182 read_size = current_capacity;
3183 TEST_ASSERT( psa_generator_read( &generator,
3184 output_buffer,
3185 read_size ) == PSA_SUCCESS );
3186 expected_capacity -= read_size;
3187 TEST_ASSERT( psa_get_generator_capacity( &generator,
3188 &current_capacity ) ==
3189 PSA_SUCCESS );
3190 TEST_ASSERT( current_capacity == expected_capacity );
3191 }
3192
3193 /* Check that the generator refuses to go over capacity. */
3194 TEST_ASSERT( psa_generator_read( &generator,
3195 output_buffer,
3196 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
3197
3198 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3199
3200exit:
3201 psa_generator_abort( &generator );
3202 psa_destroy_key( slot );
3203 mbedtls_psa_crypto_free( );
3204}
3205/* END_CASE */
3206
3207/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003208void derive_key_exercise( int alg_arg,
3209 data_t *key_data,
3210 data_t *salt,
3211 data_t *label,
3212 int derived_type_arg,
3213 int derived_bits_arg,
3214 int derived_usage_arg,
3215 int derived_alg_arg )
3216{
3217 psa_key_slot_t base_key = 1;
3218 psa_key_slot_t derived_key = 2;
3219 psa_algorithm_t alg = alg_arg;
3220 psa_key_type_t derived_type = derived_type_arg;
3221 size_t derived_bits = derived_bits_arg;
3222 psa_key_usage_t derived_usage = derived_usage_arg;
3223 psa_algorithm_t derived_alg = derived_alg_arg;
3224 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3225 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3226 psa_key_policy_t policy;
3227 psa_key_type_t got_type;
3228 size_t got_bits;
3229
3230 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3231
3232 psa_key_policy_init( &policy );
3233 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3234 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3235 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3236 key_data->x,
3237 key_data->len ) == PSA_SUCCESS );
3238
3239 /* Derive a key. */
3240 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3241 salt->x, salt->len,
3242 label->x, label->len,
3243 capacity ) == PSA_SUCCESS );
3244 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
3245 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3246 TEST_ASSERT( psa_generator_import_key( derived_key,
3247 derived_type,
3248 derived_bits,
3249 &generator ) == PSA_SUCCESS );
3250
3251 /* Test the key information */
3252 TEST_ASSERT( psa_get_key_information( derived_key,
3253 &got_type,
3254 &got_bits ) == PSA_SUCCESS );
3255 TEST_ASSERT( got_type == derived_type );
3256 TEST_ASSERT( got_bits == derived_bits );
3257
3258 /* Exercise the derived key. */
3259 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
3260 goto exit;
3261
3262exit:
3263 psa_generator_abort( &generator );
3264 psa_destroy_key( base_key );
3265 psa_destroy_key( derived_key );
3266 mbedtls_psa_crypto_free( );
3267}
3268/* END_CASE */
3269
3270/* BEGIN_CASE */
3271void derive_key_export( int alg_arg,
3272 data_t *key_data,
3273 data_t *salt,
3274 data_t *label,
3275 int bytes1_arg,
3276 int bytes2_arg )
3277{
3278 psa_key_slot_t base_key = 1;
3279 psa_key_slot_t derived_key = 2;
3280 psa_algorithm_t alg = alg_arg;
3281 size_t bytes1 = bytes1_arg;
3282 size_t bytes2 = bytes2_arg;
3283 size_t capacity = bytes1 + bytes2;
3284 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003285 uint8_t *output_buffer = NULL;
3286 uint8_t *export_buffer = NULL;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003287 psa_key_policy_t policy;
3288 size_t length;
3289
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003290 ASSERT_ALLOC( output_buffer, capacity );
3291 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003292 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3293
3294 psa_key_policy_init( &policy );
3295 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3296 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3297 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3298 key_data->x,
3299 key_data->len ) == PSA_SUCCESS );
3300
3301 /* Derive some material and output it. */
3302 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3303 salt->x, salt->len,
3304 label->x, label->len,
3305 capacity ) == PSA_SUCCESS );
3306 TEST_ASSERT( psa_generator_read( &generator,
3307 output_buffer,
3308 capacity ) == PSA_SUCCESS );
3309 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3310
3311 /* Derive the same output again, but this time store it in key objects. */
3312 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3313 salt->x, salt->len,
3314 label->x, label->len,
3315 capacity ) == PSA_SUCCESS );
3316 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
3317 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3318 TEST_ASSERT( psa_generator_import_key( derived_key,
3319 PSA_KEY_TYPE_RAW_DATA,
3320 PSA_BYTES_TO_BITS( bytes1 ),
3321 &generator ) == PSA_SUCCESS );
3322 TEST_ASSERT( psa_export_key( derived_key,
3323 export_buffer, bytes1,
3324 &length ) == PSA_SUCCESS );
3325 TEST_ASSERT( length == bytes1 );
3326 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
3327 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3328 TEST_ASSERT( psa_generator_import_key( derived_key,
3329 PSA_KEY_TYPE_RAW_DATA,
3330 PSA_BYTES_TO_BITS( bytes2 ),
3331 &generator ) == PSA_SUCCESS );
3332 TEST_ASSERT( psa_export_key( derived_key,
3333 export_buffer + bytes1, bytes2,
3334 &length ) == PSA_SUCCESS );
3335 TEST_ASSERT( length == bytes2 );
3336
3337 /* Compare the outputs from the two runs. */
3338 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
3339
3340exit:
3341 mbedtls_free( output_buffer );
3342 mbedtls_free( export_buffer );
3343 psa_generator_abort( &generator );
3344 psa_destroy_key( base_key );
3345 psa_destroy_key( derived_key );
3346 mbedtls_psa_crypto_free( );
3347}
3348/* END_CASE */
3349
3350/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003351void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003352{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003353 size_t bytes = bytes_arg;
3354 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003355 unsigned char *output = NULL;
3356 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003357 size_t i;
3358 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003359
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003360 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3361 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003362 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003363
3364 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3365
Gilles Peskinea50d7392018-06-21 10:22:13 +02003366 /* Run several times, to ensure that every output byte will be
3367 * nonzero at least once with overwhelming probability
3368 * (2^(-8*number_of_runs)). */
3369 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003370 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003371 if( bytes != 0 )
3372 memset( output, 0, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003373 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
3374
3375 /* Check that no more than bytes have been overwritten */
3376 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
3377
3378 for( i = 0; i < bytes; i++ )
3379 {
3380 if( output[i] != 0 )
3381 ++changed[i];
3382 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003383 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003384
3385 /* Check that every byte was changed to nonzero at least once. This
3386 * validates that psa_generate_random is overwriting every byte of
3387 * the output buffer. */
3388 for( i = 0; i < bytes; i++ )
3389 {
3390 TEST_ASSERT( changed[i] != 0 );
3391 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003392
3393exit:
3394 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003395 mbedtls_free( output );
3396 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003397}
3398/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003399
3400/* BEGIN_CASE */
3401void generate_key( int type_arg,
3402 int bits_arg,
3403 int usage_arg,
3404 int alg_arg,
3405 int expected_status_arg )
3406{
3407 int slot = 1;
3408 psa_key_type_t type = type_arg;
3409 psa_key_usage_t usage = usage_arg;
3410 size_t bits = bits_arg;
3411 psa_algorithm_t alg = alg_arg;
3412 psa_status_t expected_status = expected_status_arg;
3413 psa_key_type_t got_type;
3414 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003415 psa_status_t expected_info_status =
3416 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3417 psa_key_policy_t policy;
3418
3419 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3420
3421 psa_key_policy_init( &policy );
3422 psa_key_policy_set_usage( &policy, usage, alg );
3423 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3424
3425 /* Generate a key */
3426 TEST_ASSERT( psa_generate_key( slot, type, bits,
3427 NULL, 0 ) == expected_status );
3428
3429 /* Test the key information */
3430 TEST_ASSERT( psa_get_key_information( slot,
3431 &got_type,
3432 &got_bits ) == expected_info_status );
3433 if( expected_info_status != PSA_SUCCESS )
3434 goto exit;
3435 TEST_ASSERT( got_type == type );
3436 TEST_ASSERT( got_bits == bits );
3437
Gilles Peskine818ca122018-06-20 18:16:48 +02003438 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02003439 if( ! exercise_key( slot, usage, alg ) )
3440 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003441
3442exit:
3443 psa_destroy_key( slot );
3444 mbedtls_psa_crypto_free( );
3445}
3446/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03003447
3448/* BEGIN_CASE */
3449void validate_module_init_generate_random( )
3450{
3451 psa_status_t status;
3452 uint8_t random[10] = { 0 };
3453 status = psa_generate_random( random, sizeof( random ) );
3454 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3455}
3456/* END_CASE */
itayzafrir90d8c7a2018-09-12 11:44:52 +03003457
3458/* BEGIN_CASE */
3459void validate_module_init_key_based( )
3460{
3461 psa_status_t status;
3462 uint8_t data[10] = { 0 };
3463 status = psa_import_key( 1, PSA_KEY_TYPE_RAW_DATA, data, sizeof( data ) );
3464 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3465}
3466/* END_CASE */