blob: d2875ae341036e5544337f72532ef7e78cdae7d5 [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 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100634 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200635 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 */
Moran Peker28a38e62018-11-07 16:18:24 +02001007void import_key_nonempty_slot( )
1008{
1009 int slot = 1;
1010 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1011 psa_status_t status;
1012 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
1013 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1014
1015 /* Import the key */
1016 TEST_ASSERT( psa_import_key( slot, type,
1017 data, sizeof( data ) ) == PSA_SUCCESS );
1018
1019 /* Import the key again */
1020 status = psa_import_key( slot, type, data, sizeof( data ) );
1021 TEST_ASSERT( status == PSA_ERROR_OCCUPIED_SLOT );
1022
1023exit:
1024 mbedtls_psa_crypto_free( );
1025}
1026/* END_CASE */
1027
1028/* BEGIN_CASE */
1029void export_invalid_slot( int slot, int expected_export_status_arg )
1030{
1031 psa_status_t status;
1032 unsigned char *exported = NULL;
1033 size_t export_size = 0;
1034 size_t exported_length = INVALID_EXPORT_LENGTH;
1035 psa_status_t expected_export_status = expected_export_status_arg;
1036
1037 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1038
1039 /* Export the key */
1040 status = psa_export_key( slot,
1041 exported, export_size,
1042 &exported_length );
1043 TEST_ASSERT( status == expected_export_status );
1044
1045exit:
1046 mbedtls_psa_crypto_free( );
1047}
1048/* END_CASE */
1049
1050/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001051void export_with_no_key_activity( )
1052{
1053 int slot = 1;
1054 psa_algorithm_t alg = PSA_ALG_CTR;
1055 psa_status_t status;
1056 psa_key_policy_t policy;
1057 unsigned char *exported = NULL;
1058 size_t export_size = 0;
1059 size_t exported_length = INVALID_EXPORT_LENGTH;
1060
1061 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1062
1063 psa_key_policy_init( &policy );
1064 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
1065 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1066
1067 /* Export the key */
1068 status = psa_export_key( slot,
1069 exported, export_size,
1070 &exported_length );
1071 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1072
1073exit:
1074 mbedtls_psa_crypto_free( );
1075}
1076/* END_CASE */
1077
1078/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001079void cipher_with_no_key_activity( )
1080{
1081 int slot = 1;
1082 psa_status_t status;
1083 psa_key_policy_t policy;
1084 psa_cipher_operation_t operation;
1085 int exercise_alg = PSA_ALG_CTR;
1086
1087 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1088
1089 psa_key_policy_init( &policy );
1090 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
1091 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1092
1093 status = psa_cipher_encrypt_setup( &operation, slot, exercise_alg );
1094 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1095
1096exit:
1097 psa_cipher_abort( &operation );
1098 mbedtls_psa_crypto_free( );
1099}
1100/* END_CASE */
1101
1102/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001103void export_after_import_failure( data_t *data, int type_arg,
1104 int expected_import_status_arg )
1105{
1106 int slot = 1;
1107 psa_key_type_t type = type_arg;
1108 psa_status_t status;
1109 unsigned char *exported = NULL;
1110 size_t export_size = 0;
1111 psa_status_t expected_import_status = expected_import_status_arg;
1112 size_t exported_length = INVALID_EXPORT_LENGTH;
1113
1114 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1115
1116 /* Import the key - expect failure */
1117 status = psa_import_key( slot, type,
1118 data->x, data->len );
1119 TEST_ASSERT( status == expected_import_status );
1120
1121 /* Export the key */
1122 status = psa_export_key( slot,
1123 exported, export_size,
1124 &exported_length );
1125 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1126
1127exit:
1128 mbedtls_psa_crypto_free( );
1129}
1130/* END_CASE */
1131
1132/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001133void cipher_after_import_failure( data_t *data, int type_arg,
1134 int expected_import_status_arg )
1135{
1136 int slot = 1;
1137 psa_cipher_operation_t operation;
1138 psa_key_type_t type = type_arg;
1139 psa_status_t status;
1140 psa_status_t expected_import_status = expected_import_status_arg;
1141 int exercise_alg = PSA_ALG_CTR;
1142
1143 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1144
1145 /* Import the key - expect failure */
1146 status = psa_import_key( slot, type,
1147 data->x, data->len );
1148 TEST_ASSERT( status == expected_import_status );
1149
1150 status = psa_cipher_encrypt_setup( &operation, slot, exercise_alg );
1151 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1152
1153exit:
1154 psa_cipher_abort( &operation );
1155 mbedtls_psa_crypto_free( );
1156}
1157/* END_CASE */
1158
1159/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001160void export_after_destroy_key( data_t *data, int type_arg )
1161{
1162 int slot = 1;
1163 psa_key_type_t type = type_arg;
1164 psa_status_t status;
1165 psa_key_policy_t policy;
1166 psa_algorithm_t alg = PSA_ALG_CTR;
1167 unsigned char *exported = NULL;
1168 size_t export_size = 0;
1169 size_t exported_length = INVALID_EXPORT_LENGTH;
1170
1171 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1172
1173 psa_key_policy_init( &policy );
1174 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
1175 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1176 export_size = (ptrdiff_t) data->len;
1177 ASSERT_ALLOC( exported, export_size );
1178
1179 /* Import the key */
1180 TEST_ASSERT( psa_import_key( slot, type,
1181 data->x, data->len ) == PSA_SUCCESS );
1182
1183 TEST_ASSERT( psa_export_key( slot, exported, export_size,
1184 &exported_length ) == PSA_SUCCESS );
1185
1186 /* Destroy the key */
1187 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
1188
1189 /* Export the key */
1190 status = psa_export_key( slot, exported, export_size,
1191 &exported_length );
1192 TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT );
1193
1194exit:
1195 mbedtls_free( exported );
1196 mbedtls_psa_crypto_free( );
1197}
1198/* END_CASE */
1199
1200/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001201void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001202 int type_arg,
1203 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001204 int export_size_delta,
1205 int expected_export_status_arg,
1206 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001207{
1208 int slot = 1;
1209 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001210 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001211 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001212 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001213 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001214 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001215 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskinedec72612018-06-18 18:12:37 +02001216 psa_key_policy_t policy;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001217
Moran Pekerf709f4a2018-06-06 17:26:04 +03001218 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1219
1220 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001221 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001222 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1223
1224 /* Import the key */
1225 TEST_ASSERT( psa_import_key( slot, type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001226 data->x, data->len ) == PSA_SUCCESS );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001227
Gilles Peskine49c25912018-10-29 15:15:31 +01001228 /* Export the public key */
1229 ASSERT_ALLOC( exported, export_size );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001230 status = psa_export_public_key( slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001231 exported, export_size,
1232 &exported_length );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001233 TEST_ASSERT( status == expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001234 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001235 {
1236 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1237 size_t bits;
1238 TEST_ASSERT( psa_get_key_information( slot, NULL, &bits ) ==
1239 PSA_SUCCESS );
1240 TEST_ASSERT( expected_public_key->len <=
1241 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001242 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1243 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001244 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001245
1246exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001247 mbedtls_free( exported );
Gilles Peskine49c25912018-10-29 15:15:31 +01001248 psa_destroy_key( slot );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001249 mbedtls_psa_crypto_free( );
1250}
1251/* END_CASE */
1252
Gilles Peskine20035e32018-02-03 22:44:14 +01001253/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001254void import_and_exercise_key( data_t *data,
1255 int type_arg,
1256 int bits_arg,
1257 int alg_arg )
1258{
1259 int slot = 1;
1260 psa_key_type_t type = type_arg;
1261 size_t bits = bits_arg;
1262 psa_algorithm_t alg = alg_arg;
1263 psa_key_usage_t usage =
1264 ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ?
1265 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1266 PSA_KEY_USAGE_VERIFY :
1267 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) :
1268 PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1269 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ?
1270 ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1271 PSA_KEY_USAGE_ENCRYPT :
1272 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) :
Gilles Peskineea0fb492018-07-12 17:17:20 +02001273 PSA_ALG_IS_KEY_DERIVATION( alg ) ? PSA_KEY_USAGE_DERIVE :
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001274 0 );
1275 psa_key_policy_t policy;
1276 psa_key_type_t got_type;
1277 size_t got_bits;
1278 psa_status_t status;
1279
1280 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1281
1282 psa_key_policy_init( &policy );
1283 psa_key_policy_set_usage( &policy, usage, alg );
1284 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
1285
1286 /* Import the key */
1287 status = psa_import_key( slot, type, data->x, data->len );
1288 TEST_ASSERT( status == PSA_SUCCESS );
1289
1290 /* Test the key information */
1291 TEST_ASSERT( psa_get_key_information( slot,
1292 &got_type,
1293 &got_bits ) == PSA_SUCCESS );
1294 TEST_ASSERT( got_type == type );
1295 TEST_ASSERT( got_bits == bits );
1296
1297 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02001298 if( ! exercise_key( slot, usage, alg ) )
1299 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001300
1301exit:
1302 psa_destroy_key( slot );
1303 mbedtls_psa_crypto_free( );
1304}
1305/* END_CASE */
1306
1307/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001308void key_policy( int usage_arg, int alg_arg )
1309{
1310 int key_slot = 1;
1311 psa_algorithm_t alg = alg_arg;
1312 psa_key_usage_t usage = usage_arg;
1313 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1314 unsigned char key[32] = {0};
1315 psa_key_policy_t policy_set;
1316 psa_key_policy_t policy_get;
1317
1318 memset( key, 0x2a, sizeof( key ) );
1319
1320 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1321
1322 psa_key_policy_init( &policy_set );
1323 psa_key_policy_init( &policy_get );
1324
1325 psa_key_policy_set_usage( &policy_set, usage, alg );
1326
1327 TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage );
1328 TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg );
1329 TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS );
1330
1331 TEST_ASSERT( psa_import_key( key_slot, key_type,
1332 key, sizeof( key ) ) == PSA_SUCCESS );
1333
1334 TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS );
1335
1336 TEST_ASSERT( policy_get.usage == policy_set.usage );
1337 TEST_ASSERT( policy_get.alg == policy_set.alg );
1338
1339exit:
1340 psa_destroy_key( key_slot );
1341 mbedtls_psa_crypto_free( );
1342}
1343/* END_CASE */
1344
1345/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001346void mac_key_policy( int policy_usage,
1347 int policy_alg,
1348 int key_type,
1349 data_t *key_data,
1350 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001351{
1352 int key_slot = 1;
Gilles Peskined5b33222018-06-18 22:20:03 +02001353 psa_key_policy_t policy;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001354 psa_mac_operation_t operation;
1355 psa_status_t status;
1356 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001357
1358 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1359
1360 psa_key_policy_init( &policy );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001361 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001362 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1363
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001364 TEST_ASSERT( psa_import_key( key_slot, key_type,
1365 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskined5b33222018-06-18 22:20:03 +02001366
Gilles Peskine89167cb2018-07-08 20:12:23 +02001367 status = psa_mac_sign_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001368 if( policy_alg == exercise_alg &&
1369 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1370 TEST_ASSERT( status == PSA_SUCCESS );
1371 else
1372 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1373 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001374
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001375 memset( mac, 0, sizeof( mac ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001376 status = psa_mac_verify_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001377 if( policy_alg == exercise_alg &&
1378 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine89167cb2018-07-08 20:12:23 +02001379 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001380 else
1381 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1382
1383exit:
1384 psa_mac_abort( &operation );
1385 psa_destroy_key( key_slot );
1386 mbedtls_psa_crypto_free( );
1387}
1388/* END_CASE */
1389
1390/* BEGIN_CASE */
1391void cipher_key_policy( int policy_usage,
1392 int policy_alg,
1393 int key_type,
1394 data_t *key_data,
1395 int exercise_alg )
1396{
1397 int key_slot = 1;
1398 psa_key_policy_t policy;
1399 psa_cipher_operation_t operation;
1400 psa_status_t status;
1401
1402 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1403
1404 psa_key_policy_init( &policy );
1405 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1406 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1407
1408 TEST_ASSERT( psa_import_key( key_slot, key_type,
1409 key_data->x, key_data->len ) == PSA_SUCCESS );
1410
Gilles Peskinefe119512018-07-08 21:39:34 +02001411 status = psa_cipher_encrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001412 if( policy_alg == exercise_alg &&
1413 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1414 TEST_ASSERT( status == PSA_SUCCESS );
1415 else
1416 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1417 psa_cipher_abort( &operation );
1418
Gilles Peskinefe119512018-07-08 21:39:34 +02001419 status = psa_cipher_decrypt_setup( &operation, key_slot, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001420 if( policy_alg == exercise_alg &&
1421 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1422 TEST_ASSERT( status == PSA_SUCCESS );
1423 else
1424 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1425
1426exit:
1427 psa_cipher_abort( &operation );
1428 psa_destroy_key( key_slot );
1429 mbedtls_psa_crypto_free( );
1430}
1431/* END_CASE */
1432
1433/* BEGIN_CASE */
1434void aead_key_policy( int policy_usage,
1435 int policy_alg,
1436 int key_type,
1437 data_t *key_data,
1438 int nonce_length_arg,
1439 int tag_length_arg,
1440 int exercise_alg )
1441{
1442 int key_slot = 1;
1443 psa_key_policy_t policy;
1444 psa_status_t status;
1445 unsigned char nonce[16] = {0};
1446 size_t nonce_length = nonce_length_arg;
1447 unsigned char tag[16];
1448 size_t tag_length = tag_length_arg;
1449 size_t output_length;
1450
1451 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1452 TEST_ASSERT( tag_length <= sizeof( tag ) );
1453
1454 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1455
1456 psa_key_policy_init( &policy );
1457 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1458 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1459
1460 TEST_ASSERT( psa_import_key( key_slot, key_type,
1461 key_data->x, key_data->len ) == PSA_SUCCESS );
1462
1463 status = psa_aead_encrypt( key_slot, exercise_alg,
1464 nonce, nonce_length,
1465 NULL, 0,
1466 NULL, 0,
1467 tag, tag_length,
1468 &output_length );
1469 if( policy_alg == exercise_alg &&
1470 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1471 TEST_ASSERT( status == PSA_SUCCESS );
1472 else
1473 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1474
1475 memset( tag, 0, sizeof( tag ) );
1476 status = psa_aead_decrypt( key_slot, exercise_alg,
1477 nonce, nonce_length,
1478 NULL, 0,
1479 tag, tag_length,
1480 NULL, 0,
1481 &output_length );
1482 if( policy_alg == exercise_alg &&
1483 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1484 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1485 else
1486 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1487
1488exit:
1489 psa_destroy_key( key_slot );
1490 mbedtls_psa_crypto_free( );
1491}
1492/* END_CASE */
1493
1494/* BEGIN_CASE */
1495void asymmetric_encryption_key_policy( int policy_usage,
1496 int policy_alg,
1497 int key_type,
1498 data_t *key_data,
1499 int exercise_alg )
1500{
1501 int key_slot = 1;
1502 psa_key_policy_t policy;
1503 psa_status_t status;
1504 size_t key_bits;
1505 size_t buffer_length;
1506 unsigned char *buffer = NULL;
1507 size_t output_length;
1508
1509 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1510
1511 psa_key_policy_init( &policy );
1512 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1513 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1514
1515 TEST_ASSERT( psa_import_key( key_slot, key_type,
1516 key_data->x, key_data->len ) == PSA_SUCCESS );
1517
1518 TEST_ASSERT( psa_get_key_information( key_slot,
1519 NULL,
1520 &key_bits ) == PSA_SUCCESS );
1521 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1522 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001523 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001524
1525 status = psa_asymmetric_encrypt( key_slot, exercise_alg,
1526 NULL, 0,
1527 NULL, 0,
1528 buffer, buffer_length,
1529 &output_length );
1530 if( policy_alg == exercise_alg &&
1531 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
1532 TEST_ASSERT( status == PSA_SUCCESS );
1533 else
1534 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1535
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001536 if( buffer_length != 0 )
1537 memset( buffer, 0, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001538 status = psa_asymmetric_decrypt( key_slot, exercise_alg,
1539 buffer, buffer_length,
1540 NULL, 0,
1541 buffer, buffer_length,
1542 &output_length );
1543 if( policy_alg == exercise_alg &&
1544 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
1545 TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING );
1546 else
1547 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1548
1549exit:
1550 psa_destroy_key( key_slot );
1551 mbedtls_psa_crypto_free( );
1552 mbedtls_free( buffer );
1553}
1554/* END_CASE */
1555
1556/* BEGIN_CASE */
1557void asymmetric_signature_key_policy( int policy_usage,
1558 int policy_alg,
1559 int key_type,
1560 data_t *key_data,
1561 int exercise_alg )
1562{
1563 int key_slot = 1;
1564 psa_key_policy_t policy;
1565 psa_status_t status;
1566 unsigned char payload[16] = {1};
1567 size_t payload_length = sizeof( payload );
1568 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1569 size_t signature_length;
1570
1571 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1572
1573 psa_key_policy_init( &policy );
1574 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1575 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1576
1577 TEST_ASSERT( psa_import_key( key_slot, key_type,
1578 key_data->x, key_data->len ) == PSA_SUCCESS );
1579
1580 status = psa_asymmetric_sign( key_slot, exercise_alg,
1581 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001582 signature, sizeof( signature ),
1583 &signature_length );
1584 if( policy_alg == exercise_alg &&
1585 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
1586 TEST_ASSERT( status == PSA_SUCCESS );
1587 else
1588 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1589
1590 memset( signature, 0, sizeof( signature ) );
1591 status = psa_asymmetric_verify( key_slot, exercise_alg,
1592 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001593 signature, sizeof( signature ) );
1594 if( policy_alg == exercise_alg &&
1595 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
1596 TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE );
1597 else
1598 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001599
1600exit:
1601 psa_destroy_key( key_slot );
1602 mbedtls_psa_crypto_free( );
1603}
1604/* END_CASE */
1605
1606/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001607void derive_key_policy( int policy_usage,
1608 int policy_alg,
1609 int key_type,
1610 data_t *key_data,
1611 int exercise_alg )
1612{
1613 int key_slot = 1;
1614 psa_key_policy_t policy;
1615 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1616 psa_status_t status;
1617
1618 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1619
1620 psa_key_policy_init( &policy );
1621 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
1622 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1623
1624 TEST_ASSERT( psa_import_key( key_slot, key_type,
1625 key_data->x, key_data->len ) == PSA_SUCCESS );
1626
1627 status = psa_key_derivation( &generator, key_slot,
1628 exercise_alg,
1629 NULL, 0,
1630 NULL, 0,
1631 1 );
1632 if( policy_alg == exercise_alg &&
1633 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1634 TEST_ASSERT( status == PSA_SUCCESS );
1635 else
1636 TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED );
1637
1638exit:
1639 psa_generator_abort( &generator );
1640 psa_destroy_key( key_slot );
1641 mbedtls_psa_crypto_free( );
1642}
1643/* END_CASE */
1644
1645/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001646void key_lifetime( int lifetime_arg )
1647{
1648 int key_slot = 1;
Gilles Peskinec32f0302018-08-10 16:02:11 +02001649 psa_key_type_t key_type = PSA_KEY_TYPE_RAW_DATA;
Gilles Peskined5b33222018-06-18 22:20:03 +02001650 unsigned char key[32] = {0};
1651 psa_key_lifetime_t lifetime_set = lifetime_arg;
1652 psa_key_lifetime_t lifetime_get;
1653
1654 memset( key, 0x2a, sizeof( key ) );
1655
1656 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1657
1658 TEST_ASSERT( psa_set_key_lifetime( key_slot,
1659 lifetime_set ) == PSA_SUCCESS );
1660
1661 TEST_ASSERT( psa_import_key( key_slot, key_type,
1662 key, sizeof( key ) ) == PSA_SUCCESS );
1663
1664 TEST_ASSERT( psa_get_key_lifetime( key_slot,
1665 &lifetime_get ) == PSA_SUCCESS );
1666
1667 TEST_ASSERT( lifetime_get == lifetime_set );
1668
1669exit:
1670 psa_destroy_key( key_slot );
1671 mbedtls_psa_crypto_free( );
1672}
1673/* END_CASE */
1674
1675/* BEGIN_CASE */
1676void key_lifetime_set_fail( int key_slot_arg,
1677 int lifetime_arg,
1678 int expected_status_arg )
1679{
1680 psa_key_slot_t key_slot = key_slot_arg;
1681 psa_key_lifetime_t lifetime_set = lifetime_arg;
1682 psa_status_t actual_status;
1683 psa_status_t expected_status = expected_status_arg;
1684
1685 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1686
1687 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1688
1689 if( actual_status == PSA_SUCCESS )
1690 actual_status = psa_set_key_lifetime( key_slot, lifetime_set );
1691
1692 TEST_ASSERT( expected_status == actual_status );
1693
1694exit:
1695 psa_destroy_key( key_slot );
1696 mbedtls_psa_crypto_free( );
1697}
1698/* END_CASE */
1699
1700/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001701void hash_setup( int alg_arg,
1702 int expected_status_arg )
1703{
1704 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001705 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001706 psa_hash_operation_t operation;
1707 psa_status_t status;
1708
1709 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1710
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001711 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001712 psa_hash_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001713 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001714
1715exit:
1716 mbedtls_psa_crypto_free( );
1717}
1718/* END_CASE */
1719
1720/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02001721void hash_bad_order( )
1722{
1723 unsigned char input[] = "";
1724 /* SHA-256 hash of an empty string */
1725 unsigned char hash[] = {
1726 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1727 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1728 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
1729 size_t hash_len;
1730 psa_hash_operation_t operation;
1731
1732 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1733
1734 /* psa_hash_update without calling psa_hash_setup beforehand */
1735 memset( &operation, 0, sizeof( operation ) );
1736 TEST_ASSERT( psa_hash_update( &operation,
1737 input, sizeof( input ) ) ==
1738 PSA_ERROR_INVALID_ARGUMENT );
1739
1740 /* psa_hash_verify without calling psa_hash_setup beforehand */
1741 memset( &operation, 0, sizeof( operation ) );
1742 TEST_ASSERT( psa_hash_verify( &operation,
1743 hash, sizeof( hash ) ) ==
1744 PSA_ERROR_INVALID_ARGUMENT );
1745
1746 /* psa_hash_finish without calling psa_hash_setup beforehand */
1747 memset( &operation, 0, sizeof( operation ) );
1748 TEST_ASSERT( psa_hash_finish( &operation,
1749 hash, sizeof( hash ), &hash_len ) ==
1750 PSA_ERROR_INVALID_ARGUMENT );
1751
1752exit:
1753 mbedtls_psa_crypto_free( );
1754}
1755/* END_CASE */
1756
itayzafrir27e69452018-11-01 14:26:34 +02001757/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1758void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001759{
1760 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001761 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1762 * appended to it */
1763 unsigned char hash[] = {
1764 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1765 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1766 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03001767 size_t expected_size = PSA_HASH_SIZE( alg );
itayzafrirec93d302018-10-18 18:01:10 +03001768 psa_hash_operation_t operation;
itayzafrirec93d302018-10-18 18:01:10 +03001769
1770 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1771
itayzafrir27e69452018-11-01 14:26:34 +02001772 /* psa_hash_verify with a smaller hash than expected */
itayzafrirec93d302018-10-18 18:01:10 +03001773 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
1774 TEST_ASSERT( psa_hash_verify( &operation,
1775 hash, expected_size - 1 ) ==
1776 PSA_ERROR_INVALID_SIGNATURE );
1777
itayzafrir27e69452018-11-01 14:26:34 +02001778 /* psa_hash_verify with a non-matching hash */
itayzafrirec93d302018-10-18 18:01:10 +03001779 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
itayzafrirec93d302018-10-18 18:01:10 +03001780 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir27e69452018-11-01 14:26:34 +02001781 hash + 1, expected_size ) ==
itayzafrirec93d302018-10-18 18:01:10 +03001782 PSA_ERROR_INVALID_SIGNATURE );
1783
itayzafrir27e69452018-11-01 14:26:34 +02001784 /* psa_hash_verify with a hash longer than expected */
itayzafrir4271df92018-10-24 18:16:19 +03001785 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
itayzafrir4271df92018-10-24 18:16:19 +03001786 TEST_ASSERT( psa_hash_verify( &operation,
itayzafrir27e69452018-11-01 14:26:34 +02001787 hash, sizeof( hash ) ) ==
itayzafrir4271df92018-10-24 18:16:19 +03001788 PSA_ERROR_INVALID_SIGNATURE );
1789
itayzafrirec93d302018-10-18 18:01:10 +03001790exit:
1791 mbedtls_psa_crypto_free( );
1792}
1793/* END_CASE */
1794
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001795/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1796void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001797{
1798 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001799 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03001800 size_t expected_size = PSA_HASH_SIZE( alg );
1801 psa_hash_operation_t operation;
1802 size_t hash_len;
1803
1804 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1805
itayzafrir58028322018-10-25 10:22:01 +03001806 /* psa_hash_finish with a smaller hash buffer than expected */
1807 TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS );
1808 TEST_ASSERT( psa_hash_finish( &operation,
1809 hash, expected_size - 1,
1810 &hash_len ) == PSA_ERROR_BUFFER_TOO_SMALL );
1811
1812exit:
1813 mbedtls_psa_crypto_free( );
1814}
1815/* END_CASE */
1816
1817/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001818void mac_setup( int key_type_arg,
1819 data_t *key,
1820 int alg_arg,
1821 int expected_status_arg )
1822{
1823 int key_slot = 1;
1824 psa_key_type_t key_type = key_type_arg;
1825 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001826 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001827 psa_mac_operation_t operation;
1828 psa_key_policy_t policy;
1829 psa_status_t status;
1830
1831 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1832
1833 psa_key_policy_init( &policy );
1834 psa_key_policy_set_usage( &policy,
1835 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1836 alg );
1837 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1838
1839 TEST_ASSERT( psa_import_key( key_slot, key_type,
1840 key->x, key->len ) == PSA_SUCCESS );
1841
Gilles Peskine89167cb2018-07-08 20:12:23 +02001842 status = psa_mac_sign_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001843 psa_mac_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001844 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001845
1846exit:
1847 psa_destroy_key( key_slot );
1848 mbedtls_psa_crypto_free( );
1849}
1850/* END_CASE */
1851
1852/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001853void mac_sign( int key_type_arg,
1854 data_t *key,
1855 int alg_arg,
1856 data_t *input,
1857 data_t *expected_mac )
1858{
1859 int key_slot = 1;
1860 psa_key_type_t key_type = key_type_arg;
1861 psa_algorithm_t alg = alg_arg;
1862 psa_mac_operation_t operation;
1863 psa_key_policy_t policy;
1864 /* Leave a little extra room in the output buffer. At the end of the
1865 * test, we'll check that the implementation didn't overwrite onto
1866 * this extra room. */
1867 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
1868 size_t mac_buffer_size =
1869 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
1870 size_t mac_length = 0;
1871
1872 memset( actual_mac, '+', sizeof( actual_mac ) );
1873 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
1874 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
1875
1876 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1877
1878 psa_key_policy_init( &policy );
1879 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
1880 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1881
1882 TEST_ASSERT( psa_import_key( key_slot, key_type,
1883 key->x, key->len ) == PSA_SUCCESS );
1884
1885 /* Calculate the MAC. */
1886 TEST_ASSERT( psa_mac_sign_setup( &operation,
1887 key_slot, alg ) == PSA_SUCCESS );
1888 TEST_ASSERT( psa_mac_update( &operation,
1889 input->x, input->len ) == PSA_SUCCESS );
1890 TEST_ASSERT( psa_mac_sign_finish( &operation,
1891 actual_mac, mac_buffer_size,
1892 &mac_length ) == PSA_SUCCESS );
1893
1894 /* Compare with the expected value. */
1895 TEST_ASSERT( mac_length == expected_mac->len );
1896 TEST_ASSERT( memcmp( actual_mac, expected_mac->x, mac_length ) == 0 );
1897
1898 /* Verify that the end of the buffer is untouched. */
1899 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
1900 sizeof( actual_mac ) - mac_length ) );
1901
1902exit:
1903 psa_destroy_key( key_slot );
1904 mbedtls_psa_crypto_free( );
1905}
1906/* END_CASE */
1907
1908/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001909void mac_verify( int key_type_arg,
1910 data_t *key,
1911 int alg_arg,
1912 data_t *input,
1913 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001914{
1915 int key_slot = 1;
1916 psa_key_type_t key_type = key_type_arg;
1917 psa_algorithm_t alg = alg_arg;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001918 psa_mac_operation_t operation;
mohammad16036df908f2018-04-02 08:34:15 -07001919 psa_key_policy_t policy;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001920
Gilles Peskine69c12672018-06-28 00:07:19 +02001921 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
1922
Gilles Peskine8c9def32018-02-08 10:02:12 +01001923 TEST_ASSERT( key != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001924 TEST_ASSERT( input != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001925 TEST_ASSERT( expected_mac != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001926 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03001927 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
1928 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001929
1930 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1931
mohammad16036df908f2018-04-02 08:34:15 -07001932 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001933 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
mohammad16036df908f2018-04-02 08:34:15 -07001934 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1935
Gilles Peskine8c9def32018-02-08 10:02:12 +01001936 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001937 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02001938
Gilles Peskine89167cb2018-07-08 20:12:23 +02001939 TEST_ASSERT( psa_mac_verify_setup( &operation,
1940 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001941 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
1942 TEST_ASSERT( psa_mac_update( &operation,
Gilles Peskine4abf7412018-06-18 16:35:34 +02001943 input->x, input->len ) == PSA_SUCCESS );
Gilles Peskineacd4be32018-07-08 19:56:25 +02001944 TEST_ASSERT( psa_mac_verify_finish( &operation,
1945 expected_mac->x,
1946 expected_mac->len ) == PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001947
1948exit:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001949 psa_destroy_key( key_slot );
1950 mbedtls_psa_crypto_free( );
1951}
1952/* END_CASE */
1953
1954/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001955void cipher_setup( int key_type_arg,
1956 data_t *key,
1957 int alg_arg,
1958 int expected_status_arg )
1959{
1960 int key_slot = 1;
1961 psa_key_type_t key_type = key_type_arg;
1962 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001963 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001964 psa_cipher_operation_t operation;
1965 psa_key_policy_t policy;
1966 psa_status_t status;
1967
1968 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
1969
1970 psa_key_policy_init( &policy );
1971 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
1972 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
1973
1974 TEST_ASSERT( psa_import_key( key_slot, key_type,
1975 key->x, key->len ) == PSA_SUCCESS );
1976
Gilles Peskinefe119512018-07-08 21:39:34 +02001977 status = psa_cipher_encrypt_setup( &operation, key_slot, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001978 psa_cipher_abort( &operation );
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001979 TEST_ASSERT( status == expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001980
1981exit:
1982 psa_destroy_key( key_slot );
1983 mbedtls_psa_crypto_free( );
1984}
1985/* END_CASE */
1986
1987/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02001988void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03001989 data_t *key,
1990 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001991 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02001992{
1993 int key_slot = 1;
1994 psa_status_t status;
1995 psa_key_type_t key_type = key_type_arg;
1996 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001997 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02001998 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001999 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002000 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002001 size_t output_buffer_size = 0;
2002 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002003 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002004 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002005 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002006
Gilles Peskine50e586b2018-06-08 14:28:46 +02002007 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002008 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002009 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002010 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2011 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2012 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002013
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002014 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2015 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002016
2017 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2018
Moran Pekered346952018-07-05 15:22:45 +03002019 psa_key_policy_init( &policy );
2020 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2021 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2022
Gilles Peskine50e586b2018-06-08 14:28:46 +02002023 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002024 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002025
Gilles Peskinefe119512018-07-08 21:39:34 +02002026 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
2027 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002028
Gilles Peskinefe119512018-07-08 21:39:34 +02002029 TEST_ASSERT( psa_cipher_set_iv( &operation,
2030 iv, iv_size ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002031 output_buffer_size = (size_t) input->len +
2032 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002033 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002034
Gilles Peskine4abf7412018-06-18 16:35:34 +02002035 TEST_ASSERT( psa_cipher_update( &operation,
2036 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002037 output, output_buffer_size,
2038 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002039 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002040 status = psa_cipher_finish( &operation,
2041 output + function_output_length,
2042 output_buffer_size,
2043 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002044 total_output_length += function_output_length;
2045
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002046 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002047 if( expected_status == PSA_SUCCESS )
2048 {
2049 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002050 ASSERT_COMPARE( expected_output->x, expected_output->len,
2051 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002052 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002053
Gilles Peskine50e586b2018-06-08 14:28:46 +02002054exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002055 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002056 psa_destroy_key( key_slot );
2057 mbedtls_psa_crypto_free( );
2058}
2059/* END_CASE */
2060
2061/* BEGIN_CASE */
2062void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002063 data_t *key,
2064 data_t *input,
2065 int first_part_size,
2066 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002067{
2068 int key_slot = 1;
2069 psa_key_type_t key_type = key_type_arg;
2070 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002071 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002072 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002073 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002074 size_t output_buffer_size = 0;
2075 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002076 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002077 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002078 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002079
Gilles Peskine50e586b2018-06-08 14:28:46 +02002080 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002081 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002082 TEST_ASSERT( expected_output != 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 ) );
2085 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002086
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002087 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2088 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002089
2090 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2091
Moran Pekered346952018-07-05 15:22:45 +03002092 psa_key_policy_init( &policy );
2093 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2094 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2095
Gilles Peskine50e586b2018-06-08 14:28:46 +02002096 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002097 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002098
Gilles Peskinefe119512018-07-08 21:39:34 +02002099 TEST_ASSERT( psa_cipher_encrypt_setup( &operation,
2100 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002101
Gilles Peskinefe119512018-07-08 21:39:34 +02002102 TEST_ASSERT( psa_cipher_set_iv( &operation,
2103 iv, sizeof( iv ) ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002104 output_buffer_size = (size_t) input->len +
2105 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002106 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002107
Gilles Peskine4abf7412018-06-18 16:35:34 +02002108 TEST_ASSERT( (unsigned int) first_part_size < input->len );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002109 TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002110 output, output_buffer_size,
2111 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002112 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002113 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002114 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002115 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002116 output, output_buffer_size,
2117 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002118 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002119 TEST_ASSERT( psa_cipher_finish( &operation,
2120 output + function_output_length,
2121 output_buffer_size,
2122 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002123 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002124 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
2125
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002126 ASSERT_COMPARE( expected_output->x, expected_output->len,
2127 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002128
2129exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002130 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002131 psa_destroy_key( key_slot );
2132 mbedtls_psa_crypto_free( );
2133}
2134/* END_CASE */
2135
2136/* BEGIN_CASE */
2137void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002138 data_t *key,
2139 data_t *input,
2140 int first_part_size,
2141 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002142{
2143 int key_slot = 1;
2144
2145 psa_key_type_t key_type = key_type_arg;
2146 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002147 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002148 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002149 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002150 size_t output_buffer_size = 0;
2151 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002152 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002153 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002154 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002155
Gilles Peskine50e586b2018-06-08 14:28:46 +02002156 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002157 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002158 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002159 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2160 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2161 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002162
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002163 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2164 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002165
2166 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2167
Moran Pekered346952018-07-05 15:22:45 +03002168 psa_key_policy_init( &policy );
2169 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2170 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2171
Gilles Peskine50e586b2018-06-08 14:28:46 +02002172 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002173 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002174
Gilles Peskinefe119512018-07-08 21:39:34 +02002175 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2176 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002177
Gilles Peskinefe119512018-07-08 21:39:34 +02002178 TEST_ASSERT( psa_cipher_set_iv( &operation,
2179 iv, sizeof( iv ) ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002180
mohammad16033d91abe2018-07-03 13:15:54 +03002181 output_buffer_size = (size_t) input->len +
2182 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002183 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002184
Gilles Peskine4abf7412018-06-18 16:35:34 +02002185 TEST_ASSERT( (unsigned int) first_part_size < input->len );
2186 TEST_ASSERT( psa_cipher_update( &operation,
2187 input->x, first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002188 output, output_buffer_size,
2189 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002190 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002191 TEST_ASSERT( psa_cipher_update( &operation,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002192 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002193 input->len - first_part_size,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002194 output, output_buffer_size,
2195 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002196 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002197 TEST_ASSERT( psa_cipher_finish( &operation,
2198 output + function_output_length,
2199 output_buffer_size,
2200 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002201 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002202 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
2203
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002204 ASSERT_COMPARE( expected_output->x, expected_output->len,
2205 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002206
2207exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002208 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002209 psa_destroy_key( key_slot );
2210 mbedtls_psa_crypto_free( );
2211}
2212/* END_CASE */
2213
Gilles Peskine50e586b2018-06-08 14:28:46 +02002214/* BEGIN_CASE */
2215void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002216 data_t *key,
2217 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002218 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002219{
2220 int key_slot = 1;
2221 psa_status_t status;
2222 psa_key_type_t key_type = key_type_arg;
2223 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002224 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002225 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002226 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002227 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002228 size_t output_buffer_size = 0;
2229 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002230 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002231 psa_cipher_operation_t operation;
Moran Pekered346952018-07-05 15:22:45 +03002232 psa_key_policy_t policy;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002233
Gilles Peskine50e586b2018-06-08 14:28:46 +02002234 TEST_ASSERT( key != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002235 TEST_ASSERT( input != NULL );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002236 TEST_ASSERT( expected_output != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002237 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2238 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
2239 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002240
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002241 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2242 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002243
2244 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2245
Moran Pekered346952018-07-05 15:22:45 +03002246 psa_key_policy_init( &policy );
2247 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
2248 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2249
Gilles Peskine50e586b2018-06-08 14:28:46 +02002250 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002251 key->x, key->len ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002252
Gilles Peskinefe119512018-07-08 21:39:34 +02002253 TEST_ASSERT( psa_cipher_decrypt_setup( &operation,
2254 key_slot, alg ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002255
Gilles Peskinefe119512018-07-08 21:39:34 +02002256 TEST_ASSERT( psa_cipher_set_iv( &operation,
2257 iv, iv_size ) == PSA_SUCCESS );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002258
mohammad16033d91abe2018-07-03 13:15:54 +03002259 output_buffer_size = (size_t) input->len +
2260 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002261 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002262
Gilles Peskine4abf7412018-06-18 16:35:34 +02002263 TEST_ASSERT( psa_cipher_update( &operation,
2264 input->x, input->len,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002265 output, output_buffer_size,
2266 &function_output_length ) == PSA_SUCCESS );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002267 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002268 status = psa_cipher_finish( &operation,
2269 output + function_output_length,
2270 output_buffer_size,
2271 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002272 total_output_length += function_output_length;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002273 TEST_ASSERT( status == expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002274
2275 if( expected_status == PSA_SUCCESS )
2276 {
2277 TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002278 ASSERT_COMPARE( expected_output->x, expected_output->len,
2279 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002280 }
2281
Gilles Peskine50e586b2018-06-08 14:28:46 +02002282exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002283 mbedtls_free( output );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002284 psa_destroy_key( key_slot );
2285 mbedtls_psa_crypto_free( );
2286}
2287/* END_CASE */
2288
Gilles Peskine50e586b2018-06-08 14:28:46 +02002289/* BEGIN_CASE */
2290void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002291 data_t *key,
2292 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002293{
2294 int key_slot = 1;
2295 psa_key_type_t key_type = key_type_arg;
2296 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002297 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002298 size_t iv_size = 16;
2299 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002300 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002301 size_t output1_size = 0;
2302 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002303 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002304 size_t output2_size = 0;
2305 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002306 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002307 psa_cipher_operation_t operation1;
2308 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002309 psa_key_policy_t policy;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002310
mohammad1603d7d7ba52018-03-12 18:51:53 +02002311 TEST_ASSERT( key != NULL );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002312 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002313 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2314 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002315
mohammad1603d7d7ba52018-03-12 18:51:53 +02002316 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2317
Moran Pekered346952018-07-05 15:22:45 +03002318 psa_key_policy_init( &policy );
2319 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2320 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2321
mohammad1603d7d7ba52018-03-12 18:51:53 +02002322 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002323 key->x, key->len ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002324
Gilles Peskinefe119512018-07-08 21:39:34 +02002325 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2326 key_slot, alg ) == PSA_SUCCESS );
2327 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2328 key_slot, alg ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002329
Gilles Peskinefe119512018-07-08 21:39:34 +02002330 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2331 iv, iv_size,
2332 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002333 output1_size = (size_t) input->len +
2334 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002335 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002336
Gilles Peskine4abf7412018-06-18 16:35:34 +02002337 TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002338 output1, output1_size,
2339 &output1_length ) == PSA_SUCCESS );
2340 TEST_ASSERT( psa_cipher_finish( &operation1,
2341 output1 + output1_length, output1_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002342 &function_output_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002343
Gilles Peskine048b7f02018-06-08 14:20:49 +02002344 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002345
2346 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2347
2348 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002349 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002350
Gilles Peskinefe119512018-07-08 21:39:34 +02002351 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2352 iv, iv_length ) == PSA_SUCCESS );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002353 TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2354 output2, output2_size,
2355 &output2_length ) == PSA_SUCCESS );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002356 function_output_length = 0;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002357 TEST_ASSERT( psa_cipher_finish( &operation2,
2358 output2 + output2_length,
2359 output2_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002360 &function_output_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002361
Gilles Peskine048b7f02018-06-08 14:20:49 +02002362 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002363
Janos Follath25c4fa82018-07-06 16:23:25 +01002364 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002365
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002366 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002367
2368exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002369 mbedtls_free( output1 );
2370 mbedtls_free( output2 );
Moran Pekerded84402018-06-06 16:36:50 +03002371 psa_destroy_key( key_slot );
2372 mbedtls_psa_crypto_free( );
2373}
2374/* END_CASE */
2375
2376/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002377void cipher_verify_output_multipart( int alg_arg,
2378 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002379 data_t *key,
2380 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002381 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002382{
2383 int key_slot = 1;
2384 psa_key_type_t key_type = key_type_arg;
2385 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002386 unsigned char iv[16] = {0};
2387 size_t iv_size = 16;
2388 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002389 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002390 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002391 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002392 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002393 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002394 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002395 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002396 psa_cipher_operation_t operation1;
2397 psa_cipher_operation_t operation2;
Moran Pekered346952018-07-05 15:22:45 +03002398 psa_key_policy_t policy;
Moran Pekerded84402018-06-06 16:36:50 +03002399
Moran Pekerded84402018-06-06 16:36:50 +03002400 TEST_ASSERT( key != NULL );
Moran Pekerded84402018-06-06 16:36:50 +03002401 TEST_ASSERT( input != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002402 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) );
2403 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002404
Moran Pekerded84402018-06-06 16:36:50 +03002405 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2406
Moran Pekered346952018-07-05 15:22:45 +03002407 psa_key_policy_init( &policy );
2408 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
2409 TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS );
2410
Moran Pekerded84402018-06-06 16:36:50 +03002411 TEST_ASSERT( psa_import_key( key_slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002412 key->x, key->len ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002413
Gilles Peskinefe119512018-07-08 21:39:34 +02002414 TEST_ASSERT( psa_cipher_encrypt_setup( &operation1,
2415 key_slot, alg ) == PSA_SUCCESS );
2416 TEST_ASSERT( psa_cipher_decrypt_setup( &operation2,
2417 key_slot, alg ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002418
Gilles Peskinefe119512018-07-08 21:39:34 +02002419 TEST_ASSERT( psa_cipher_generate_iv( &operation1,
2420 iv, iv_size,
2421 &iv_length ) == PSA_SUCCESS );
mohammad16033d91abe2018-07-03 13:15:54 +03002422 output1_buffer_size = (size_t) input->len +
2423 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002424 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002425
Gilles Peskine4abf7412018-06-18 16:35:34 +02002426 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002427
itayzafrir3e02b3b2018-06-12 17:06:52 +03002428 TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002429 output1, output1_buffer_size,
2430 &function_output_length ) == PSA_SUCCESS );
2431 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002432
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002433 TEST_ASSERT( psa_cipher_update( &operation1,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002434 input->x + first_part_size,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002435 input->len - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002436 output1, output1_buffer_size,
2437 &function_output_length ) == PSA_SUCCESS );
2438 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002439
Gilles Peskine048b7f02018-06-08 14:20:49 +02002440 TEST_ASSERT( psa_cipher_finish( &operation1,
2441 output1 + output1_length,
2442 output1_buffer_size - output1_length,
2443 &function_output_length ) == PSA_SUCCESS );
2444 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002445
2446 TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS );
2447
Gilles Peskine048b7f02018-06-08 14:20:49 +02002448 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002449 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002450
Gilles Peskinefe119512018-07-08 21:39:34 +02002451 TEST_ASSERT( psa_cipher_set_iv( &operation2,
2452 iv, iv_length ) == PSA_SUCCESS );
Moran Pekerded84402018-06-06 16:36:50 +03002453
2454 TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002455 output2, output2_buffer_size,
2456 &function_output_length ) == PSA_SUCCESS );
2457 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002458
Gilles Peskine048b7f02018-06-08 14:20:49 +02002459 TEST_ASSERT( psa_cipher_update( &operation2,
2460 output1 + first_part_size,
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002461 output1_length - first_part_size,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002462 output2, output2_buffer_size,
2463 &function_output_length ) == PSA_SUCCESS );
2464 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002465
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002466 TEST_ASSERT( psa_cipher_finish( &operation2,
2467 output2 + output2_length,
Gilles Peskine048b7f02018-06-08 14:20:49 +02002468 output2_buffer_size - output2_length,
2469 &function_output_length ) == PSA_SUCCESS );
2470 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002471
Janos Follath25c4fa82018-07-06 16:23:25 +01002472 TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002473
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002474 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002475
2476exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002477 mbedtls_free( output1 );
2478 mbedtls_free( output2 );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002479 psa_destroy_key( key_slot );
2480 mbedtls_psa_crypto_free( );
2481}
2482/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002483
Gilles Peskine20035e32018-02-03 22:44:14 +01002484/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002485void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002486 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002487 data_t *nonce,
2488 data_t *additional_data,
2489 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002490 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002491{
2492 int slot = 1;
2493 psa_key_type_t key_type = key_type_arg;
2494 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002495 unsigned char *output_data = NULL;
2496 size_t output_size = 0;
2497 size_t output_length = 0;
2498 unsigned char *output_data2 = NULL;
2499 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002500 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002501 psa_status_t expected_result = expected_result_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002502 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002503
Gilles Peskinea1cac842018-06-11 19:33:02 +02002504 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002505 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002506 TEST_ASSERT( nonce != NULL );
2507 TEST_ASSERT( additional_data != NULL );
2508 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2509 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2510 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2511 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2512
Gilles Peskine4abf7412018-06-18 16:35:34 +02002513 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002514 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002515
2516 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2517
2518 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002519 psa_key_policy_set_usage( &policy,
2520 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2521 alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002522 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2523
2524 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002525 key_data->x, key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002526
2527 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002528 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002529 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002530 additional_data->len,
2531 input_data->x, input_data->len,
2532 output_data, output_size,
2533 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002534
2535 if( PSA_SUCCESS == expected_result )
2536 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002537 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002538
2539 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002540 nonce->x, nonce->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002541 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002542 additional_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002543 output_data, output_length,
2544 output_data2, output_length,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002545 &output_length2 ) == expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002546
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002547 ASSERT_COMPARE( input_data->x, input_data->len,
2548 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002549 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002550
Gilles Peskinea1cac842018-06-11 19:33:02 +02002551exit:
2552 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002553 mbedtls_free( output_data );
2554 mbedtls_free( output_data2 );
2555 mbedtls_psa_crypto_free( );
2556}
2557/* END_CASE */
2558
2559/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002560void aead_encrypt( int key_type_arg, data_t *key_data,
2561 int alg_arg,
2562 data_t *nonce,
2563 data_t *additional_data,
2564 data_t *input_data,
2565 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002566{
2567 int slot = 1;
2568 psa_key_type_t key_type = key_type_arg;
2569 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002570 unsigned char *output_data = NULL;
2571 size_t output_size = 0;
2572 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002573 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002574 psa_key_policy_t policy;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002575
Gilles Peskinea1cac842018-06-11 19:33:02 +02002576 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002577 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002578 TEST_ASSERT( additional_data != NULL );
2579 TEST_ASSERT( nonce != NULL );
2580 TEST_ASSERT( expected_result != NULL );
2581 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2582 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2583 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2584 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2585 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) );
2586
Gilles Peskine4abf7412018-06-18 16:35:34 +02002587 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002588 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002589
Gilles Peskinea1cac842018-06-11 19:33:02 +02002590 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2591
2592 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002593 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002594 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2595
2596 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002597 key_data->x,
2598 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002599
2600 TEST_ASSERT( psa_aead_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002601 nonce->x, nonce->len,
2602 additional_data->x, additional_data->len,
2603 input_data->x, input_data->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002604 output_data, output_size,
2605 &output_length ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002606
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002607 ASSERT_COMPARE( expected_result->x, expected_result->len,
2608 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002609
Gilles Peskinea1cac842018-06-11 19:33:02 +02002610exit:
2611 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002612 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002613 mbedtls_psa_crypto_free( );
2614}
2615/* END_CASE */
2616
2617/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002618void aead_decrypt( int key_type_arg, data_t *key_data,
2619 int alg_arg,
2620 data_t *nonce,
2621 data_t *additional_data,
2622 data_t *input_data,
2623 data_t *expected_data,
2624 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002625{
2626 int slot = 1;
2627 psa_key_type_t key_type = key_type_arg;
2628 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002629 unsigned char *output_data = NULL;
2630 size_t output_size = 0;
2631 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002632 size_t tag_length = 16;
Gilles Peskinedec72612018-06-18 18:12:37 +02002633 psa_key_policy_t policy;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002634 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002635
Gilles Peskinea1cac842018-06-11 19:33:02 +02002636 TEST_ASSERT( key_data != NULL );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002637 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002638 TEST_ASSERT( additional_data != NULL );
2639 TEST_ASSERT( nonce != NULL );
2640 TEST_ASSERT( expected_data != NULL );
2641 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2642 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2643 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) );
2644 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) );
2645 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
2646
Gilles Peskine4abf7412018-06-18 16:35:34 +02002647 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002648 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002649
Gilles Peskinea1cac842018-06-11 19:33:02 +02002650 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2651
2652 psa_key_policy_init( &policy );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002653 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002654 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2655
2656 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002657 key_data->x,
2658 key_data->len ) == PSA_SUCCESS );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002659
2660 TEST_ASSERT( psa_aead_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002661 nonce->x, nonce->len,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002662 additional_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002663 additional_data->len,
2664 input_data->x, input_data->len,
2665 output_data, output_size,
2666 &output_length ) == expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002667
Gilles Peskine2d277862018-06-18 15:41:12 +02002668 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002669 ASSERT_COMPARE( expected_data->x, expected_data->len,
2670 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002671
Gilles Peskinea1cac842018-06-11 19:33:02 +02002672exit:
2673 psa_destroy_key( slot );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002674 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002675 mbedtls_psa_crypto_free( );
2676}
2677/* END_CASE */
2678
2679/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002680void signature_size( int type_arg,
2681 int bits,
2682 int alg_arg,
2683 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002684{
2685 psa_key_type_t type = type_arg;
2686 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002687 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002688 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
2689exit:
2690 ;
2691}
2692/* END_CASE */
2693
2694/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002695void sign_deterministic( int key_type_arg, data_t *key_data,
2696 int alg_arg, data_t *input_data,
2697 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002698{
2699 int slot = 1;
2700 psa_key_type_t key_type = key_type_arg;
2701 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002702 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002703 unsigned char *signature = NULL;
2704 size_t signature_size;
2705 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002706 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002707
Gilles Peskine20035e32018-02-03 22:44:14 +01002708 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002709 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002710 TEST_ASSERT( output_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002711 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2712 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2713 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002714
2715 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2716
mohammad1603a97cb8c2018-03-28 03:46:26 -07002717 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002718 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002719 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2720
Gilles Peskine20035e32018-02-03 22:44:14 +01002721 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002722 key_data->x,
2723 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002724 TEST_ASSERT( psa_get_key_information( slot,
2725 NULL,
2726 &key_bits ) == PSA_SUCCESS );
2727
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002728 /* Allocate a buffer which has the size advertized by the
2729 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002730 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2731 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002732 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002733 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002734 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002735
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002736 /* Perform the signature. */
Gilles Peskine20035e32018-02-03 22:44:14 +01002737 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002738 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002739 signature, signature_size,
2740 &signature_length ) == PSA_SUCCESS );
Gilles Peskine9911b022018-06-29 17:30:48 +02002741 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002742 ASSERT_COMPARE( output_data->x, output_data->len,
2743 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002744
2745exit:
2746 psa_destroy_key( slot );
Gilles Peskine0189e752018-02-03 23:57:22 +01002747 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002748 mbedtls_psa_crypto_free( );
2749}
2750/* END_CASE */
2751
2752/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002753void sign_fail( int key_type_arg, data_t *key_data,
2754 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002755 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002756{
2757 int slot = 1;
2758 psa_key_type_t key_type = key_type_arg;
2759 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002760 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002761 psa_status_t actual_status;
2762 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002763 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002764 size_t signature_length = 0xdeadbeef;
Gilles Peskinedec72612018-06-18 18:12:37 +02002765 psa_key_policy_t policy;
Gilles Peskine20035e32018-02-03 22:44:14 +01002766
Gilles Peskine20035e32018-02-03 22:44:14 +01002767 TEST_ASSERT( key_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +01002768 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002769 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2770 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
2771
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002772 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002773
2774 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2775
mohammad1603a97cb8c2018-03-28 03:46:26 -07002776 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002777 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002778 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2779
Gilles Peskine20035e32018-02-03 22:44:14 +01002780 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002781 key_data->x,
2782 key_data->len ) == PSA_SUCCESS );
Gilles Peskine20035e32018-02-03 22:44:14 +01002783
2784 actual_status = psa_asymmetric_sign( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002785 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002786 signature, signature_size,
2787 &signature_length );
2788 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002789 /* The value of *signature_length is unspecified on error, but
2790 * whatever it is, it should be less than signature_size, so that
2791 * if the caller tries to read *signature_length bytes without
2792 * checking the error code then they don't overflow a buffer. */
2793 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002794
2795exit:
2796 psa_destroy_key( slot );
Gilles Peskine20035e32018-02-03 22:44:14 +01002797 mbedtls_free( signature );
2798 mbedtls_psa_crypto_free( );
2799}
2800/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002801
2802/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002803void sign_verify( int key_type_arg, data_t *key_data,
2804 int alg_arg, data_t *input_data )
2805{
2806 int slot = 1;
2807 psa_key_type_t key_type = key_type_arg;
2808 psa_algorithm_t alg = alg_arg;
2809 size_t key_bits;
2810 unsigned char *signature = NULL;
2811 size_t signature_size;
2812 size_t signature_length = 0xdeadbeef;
2813 psa_key_policy_t policy;
2814
2815 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2816
2817 psa_key_policy_init( &policy );
2818 psa_key_policy_set_usage( &policy,
2819 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2820 alg );
2821 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2822
2823 TEST_ASSERT( psa_import_key( slot, key_type,
2824 key_data->x,
2825 key_data->len ) == PSA_SUCCESS );
2826 TEST_ASSERT( psa_get_key_information( slot,
2827 NULL,
2828 &key_bits ) == PSA_SUCCESS );
2829
2830 /* Allocate a buffer which has the size advertized by the
2831 * library. */
2832 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2833 key_bits, alg );
2834 TEST_ASSERT( signature_size != 0 );
2835 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002836 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002837
2838 /* Perform the signature. */
2839 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
2840 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002841 signature, signature_size,
2842 &signature_length ) == PSA_SUCCESS );
2843 /* Check that the signature length looks sensible. */
2844 TEST_ASSERT( signature_length <= signature_size );
2845 TEST_ASSERT( signature_length > 0 );
2846
2847 /* Use the library to verify that the signature is correct. */
2848 TEST_ASSERT( psa_asymmetric_verify(
2849 slot, alg,
2850 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002851 signature, signature_length ) == PSA_SUCCESS );
2852
2853 if( input_data->len != 0 )
2854 {
2855 /* Flip a bit in the input and verify that the signature is now
2856 * detected as invalid. Flip a bit at the beginning, not at the end,
2857 * because ECDSA may ignore the last few bits of the input. */
2858 input_data->x[0] ^= 1;
2859 TEST_ASSERT( psa_asymmetric_verify(
2860 slot, alg,
2861 input_data->x, input_data->len,
Gilles Peskine9911b022018-06-29 17:30:48 +02002862 signature,
2863 signature_length ) == PSA_ERROR_INVALID_SIGNATURE );
2864 }
2865
2866exit:
2867 psa_destroy_key( slot );
2868 mbedtls_free( signature );
2869 mbedtls_psa_crypto_free( );
2870}
2871/* END_CASE */
2872
2873/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002874void asymmetric_verify( int key_type_arg, data_t *key_data,
2875 int alg_arg, data_t *hash_data,
2876 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002877{
2878 int slot = 1;
2879 psa_key_type_t key_type = key_type_arg;
2880 psa_algorithm_t alg = alg_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002881 psa_key_policy_t policy;
itayzafrir5c753392018-05-08 11:18:38 +03002882
Gilles Peskine69c12672018-06-28 00:07:19 +02002883 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2884
itayzafrir5c753392018-05-08 11:18:38 +03002885 TEST_ASSERT( key_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002886 TEST_ASSERT( hash_data != NULL );
itayzafrir5c753392018-05-08 11:18:38 +03002887 TEST_ASSERT( signature_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( hash_data->len ) );
2890 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002891
2892 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2893
2894 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002895 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
itayzafrir5c753392018-05-08 11:18:38 +03002896 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2897
2898 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002899 key_data->x,
2900 key_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002901
2902 TEST_ASSERT( psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002903 hash_data->x, hash_data->len,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002904 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002905 signature_data->len ) == PSA_SUCCESS );
itayzafrir5c753392018-05-08 11:18:38 +03002906exit:
2907 psa_destroy_key( slot );
itayzafrir5c753392018-05-08 11:18:38 +03002908 mbedtls_psa_crypto_free( );
2909}
2910/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002911
2912/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002913void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2914 int alg_arg, data_t *hash_data,
2915 data_t *signature_data,
2916 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002917{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002918 int slot = 1;
2919 psa_key_type_t key_type = key_type_arg;
2920 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002921 psa_status_t actual_status;
2922 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02002923 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002924
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002925 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002926 TEST_ASSERT( hash_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002927 TEST_ASSERT( signature_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002928 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
2929 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) );
2930 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002931
2932 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2933
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002934 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002935 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002936 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2937
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002938 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002939 key_data->x,
2940 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002941
2942 actual_status = psa_asymmetric_verify( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002943 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002944 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002945 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002946
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002947 TEST_ASSERT( actual_status == expected_status );
2948
2949exit:
2950 psa_destroy_key( slot );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002951 mbedtls_psa_crypto_free( );
2952}
2953/* END_CASE */
2954
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002955/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002956void asymmetric_encrypt( int key_type_arg,
2957 data_t *key_data,
2958 int alg_arg,
2959 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02002960 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02002961 int expected_output_length_arg,
2962 int expected_status_arg )
2963{
2964 int slot = 1;
2965 psa_key_type_t key_type = key_type_arg;
2966 psa_algorithm_t alg = alg_arg;
2967 size_t expected_output_length = expected_output_length_arg;
2968 size_t key_bits;
2969 unsigned char *output = NULL;
2970 size_t output_size;
2971 size_t output_length = ~0;
2972 psa_status_t actual_status;
2973 psa_status_t expected_status = expected_status_arg;
2974 psa_key_policy_t policy;
2975
2976 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
2977
2978 /* Import the key */
2979 psa_key_policy_init( &policy );
2980 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
2981 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
2982 TEST_ASSERT( psa_import_key( slot, key_type,
2983 key_data->x,
2984 key_data->len ) == PSA_SUCCESS );
2985
2986 /* Determine the maximum output length */
2987 TEST_ASSERT( psa_get_key_information( slot,
2988 NULL,
2989 &key_bits ) == PSA_SUCCESS );
2990 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002991 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02002992
2993 /* Encrypt the input */
2994 actual_status = psa_asymmetric_encrypt( slot, alg,
2995 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02002996 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02002997 output, output_size,
2998 &output_length );
2999 TEST_ASSERT( actual_status == expected_status );
3000 TEST_ASSERT( output_length == expected_output_length );
3001
Gilles Peskine68428122018-06-30 18:42:41 +02003002 /* If the label is empty, the test framework puts a non-null pointer
3003 * in label->x. Test that a null pointer works as well. */
3004 if( label->len == 0 )
3005 {
3006 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003007 if( output_size != 0 )
3008 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003009 actual_status = psa_asymmetric_encrypt( slot, alg,
3010 input_data->x, input_data->len,
3011 NULL, label->len,
3012 output, output_size,
3013 &output_length );
3014 TEST_ASSERT( actual_status == expected_status );
3015 TEST_ASSERT( output_length == expected_output_length );
3016 }
3017
Gilles Peskine656896e2018-06-29 19:12:28 +02003018exit:
3019 psa_destroy_key( slot );
3020 mbedtls_free( output );
3021 mbedtls_psa_crypto_free( );
3022}
3023/* END_CASE */
3024
3025/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003026void asymmetric_encrypt_decrypt( int key_type_arg,
3027 data_t *key_data,
3028 int alg_arg,
3029 data_t *input_data,
3030 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003031{
3032 int slot = 1;
3033 psa_key_type_t key_type = key_type_arg;
3034 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003035 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003036 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003037 size_t output_size;
3038 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003039 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003040 size_t output2_size;
3041 size_t output2_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02003042 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003043
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003044 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003045 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003046 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3047 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3048
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003049 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3050
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003051 psa_key_policy_init( &policy );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003052 psa_key_policy_set_usage( &policy,
3053 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003054 alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003055 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3056
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003057 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003058 key_data->x,
3059 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003060
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003061
3062 /* Determine the maximum ciphertext length */
3063 TEST_ASSERT( psa_get_key_information( slot,
3064 NULL,
3065 &key_bits ) == PSA_SUCCESS );
3066 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003067 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003068 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003069 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003070
Gilles Peskineeebd7382018-06-08 18:11:54 +02003071 /* We test encryption by checking that encrypt-then-decrypt gives back
3072 * the original plaintext because of the non-optional random
3073 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine2d277862018-06-18 15:41:12 +02003074 TEST_ASSERT( psa_asymmetric_encrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003075 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003076 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003077 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003078 &output_length ) == PSA_SUCCESS );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003079 /* We don't know what ciphertext length to expect, but check that
3080 * it looks sensible. */
3081 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003082
Gilles Peskine2d277862018-06-18 15:41:12 +02003083 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003084 output, output_length,
Gilles Peskine68428122018-06-30 18:42:41 +02003085 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003086 output2, output2_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003087 &output2_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003088 ASSERT_COMPARE( input_data->x, input_data->len,
3089 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003090
3091exit:
3092 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003093 mbedtls_free( output );
3094 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003095 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003096}
3097/* END_CASE */
3098
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003099/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003100void asymmetric_decrypt( int key_type_arg,
3101 data_t *key_data,
3102 int alg_arg,
3103 data_t *input_data,
3104 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003105 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003106{
3107 int slot = 1;
3108 psa_key_type_t key_type = key_type_arg;
3109 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003110 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003111 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003112 size_t output_length = ~0;
Gilles Peskinedec72612018-06-18 18:12:37 +02003113 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003114
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003115 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003116 TEST_ASSERT( input_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003117 TEST_ASSERT( expected_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003118 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3119 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3120 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) );
3121
Gilles Peskine4abf7412018-06-18 16:35:34 +02003122 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003123 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003124
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003125 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3126
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003127 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003128 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003129 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3130
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003131 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003132 key_data->x,
3133 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003134
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003135 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003136 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003137 label->x, label->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003138 output,
3139 output_size,
3140 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003141 ASSERT_COMPARE( expected_data->x, expected_data->len,
3142 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003143
Gilles Peskine68428122018-06-30 18:42:41 +02003144 /* If the label is empty, the test framework puts a non-null pointer
3145 * in label->x. Test that a null pointer works as well. */
3146 if( label->len == 0 )
3147 {
3148 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003149 if( output_size != 0 )
3150 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003151 TEST_ASSERT( psa_asymmetric_decrypt( slot, alg,
3152 input_data->x, input_data->len,
3153 NULL, label->len,
3154 output,
3155 output_size,
3156 &output_length ) == PSA_SUCCESS );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003157 ASSERT_COMPARE( expected_data->x, expected_data->len,
3158 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003159 }
3160
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003161exit:
3162 psa_destroy_key( slot );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003163 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003164 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003165}
3166/* END_CASE */
3167
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003168/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003169void asymmetric_decrypt_fail( int key_type_arg,
3170 data_t *key_data,
3171 int alg_arg,
3172 data_t *input_data,
3173 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003174 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003175{
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003176 int slot = 1;
3177 psa_key_type_t key_type = key_type_arg;
3178 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003179 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003180 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003181 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003182 psa_status_t actual_status;
3183 psa_status_t expected_status = expected_status_arg;
Gilles Peskinedec72612018-06-18 18:12:37 +02003184 psa_key_policy_t policy;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003185
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003186 TEST_ASSERT( key_data != NULL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003187 TEST_ASSERT( input_data != NULL );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003188 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) );
3189 TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) );
3190
Gilles Peskine4abf7412018-06-18 16:35:34 +02003191 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003192 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003193
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003194 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3195
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003196 psa_key_policy_init( &policy );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003197 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003198 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3199
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003200 TEST_ASSERT( psa_import_key( slot, key_type,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003201 key_data->x,
3202 key_data->len ) == PSA_SUCCESS );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003203
Gilles Peskine2d277862018-06-18 15:41:12 +02003204 actual_status = psa_asymmetric_decrypt( slot, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003205 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003206 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003207 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003208 &output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003209 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003210 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003211
Gilles Peskine68428122018-06-30 18:42:41 +02003212 /* If the label is empty, the test framework puts a non-null pointer
3213 * in label->x. Test that a null pointer works as well. */
3214 if( label->len == 0 )
3215 {
3216 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003217 if( output_size != 0 )
3218 memset( output, 0, output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003219 actual_status = psa_asymmetric_decrypt( slot, alg,
3220 input_data->x, input_data->len,
3221 NULL, label->len,
3222 output, output_size,
3223 &output_length );
3224 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003225 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003226 }
3227
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003228exit:
3229 psa_destroy_key( slot );
Gilles Peskine2d277862018-06-18 15:41:12 +02003230 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003231 mbedtls_psa_crypto_free( );
3232}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003233/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003234
3235/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003236void derive_setup( int key_type_arg,
3237 data_t *key_data,
3238 int alg_arg,
3239 data_t *salt,
3240 data_t *label,
3241 int requested_capacity_arg,
3242 int expected_status_arg )
3243{
3244 psa_key_slot_t slot = 1;
3245 size_t key_type = key_type_arg;
3246 psa_algorithm_t alg = alg_arg;
3247 size_t requested_capacity = requested_capacity_arg;
3248 psa_status_t expected_status = expected_status_arg;
3249 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3250 psa_key_policy_t policy;
3251
3252 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3253
3254 psa_key_policy_init( &policy );
3255 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3256 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3257
3258 TEST_ASSERT( psa_import_key( slot, key_type,
3259 key_data->x,
3260 key_data->len ) == PSA_SUCCESS );
3261
3262 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3263 salt->x, salt->len,
3264 label->x, label->len,
3265 requested_capacity ) == expected_status );
3266
3267exit:
3268 psa_generator_abort( &generator );
3269 psa_destroy_key( slot );
3270 mbedtls_psa_crypto_free( );
3271}
3272/* END_CASE */
3273
3274/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003275void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003276{
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003277 psa_key_slot_t base_key = 1;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003278 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003279 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003280 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003281 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003282 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003283 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3284 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3285 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003286 psa_key_policy_t policy;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003287
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003288 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3289
3290 psa_key_policy_init( &policy );
3291 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3292 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3293
3294 TEST_ASSERT( psa_import_key( base_key, key_type,
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003295 key_data,
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003296 sizeof( key_data ) ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003297
3298 /* valid key derivation */
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003299 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003300 NULL, 0,
3301 NULL, 0,
3302 capacity ) == PSA_SUCCESS );
3303
3304 /* state of generator shouldn't allow additional generation */
3305 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3306 NULL, 0,
3307 NULL, 0,
3308 capacity ) == PSA_ERROR_BAD_STATE );
3309
3310 TEST_ASSERT( psa_generator_read( &generator, buffer, capacity )
3311 == PSA_SUCCESS );
3312
3313 TEST_ASSERT( psa_generator_read( &generator, buffer, capacity )
3314 == PSA_ERROR_INSUFFICIENT_CAPACITY );
3315
3316
3317exit:
3318 psa_generator_abort( &generator );
3319 psa_destroy_key( base_key );
3320 mbedtls_psa_crypto_free( );
3321}
3322/* END_CASE */
3323
3324
3325/* BEGIN_CASE */
3326void test_derive_invalid_generator_tests( )
3327{
3328 uint8_t output_buffer[16];
3329 size_t buffer_size = 16;
3330 size_t capacity = 0;
3331 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3332
Nir Sonnenschein50789302018-10-31 12:16:38 +02003333 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003334 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003335
3336 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003337 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003338
Nir Sonnenschein50789302018-10-31 12:16:38 +02003339 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003340
Nir Sonnenschein50789302018-10-31 12:16:38 +02003341 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003342 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003343
Nir Sonnenschein50789302018-10-31 12:16:38 +02003344 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003345 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003346
3347exit:
3348 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003349}
3350/* END_CASE */
3351
3352/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003353void derive_output( int alg_arg,
3354 data_t *key_data,
3355 data_t *salt,
3356 data_t *label,
3357 int requested_capacity_arg,
3358 data_t *expected_output1,
3359 data_t *expected_output2 )
3360{
3361 psa_key_slot_t slot = 1;
3362 psa_algorithm_t alg = alg_arg;
3363 size_t requested_capacity = requested_capacity_arg;
3364 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3365 uint8_t *expected_outputs[2] =
3366 {expected_output1->x, expected_output2->x};
3367 size_t output_sizes[2] =
3368 {expected_output1->len, expected_output2->len};
3369 size_t output_buffer_size = 0;
3370 uint8_t *output_buffer = NULL;
3371 size_t expected_capacity;
3372 size_t current_capacity;
3373 psa_key_policy_t policy;
3374 psa_status_t status;
3375 unsigned i;
3376
3377 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3378 {
3379 if( output_sizes[i] > output_buffer_size )
3380 output_buffer_size = output_sizes[i];
3381 if( output_sizes[i] == 0 )
3382 expected_outputs[i] = NULL;
3383 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003384 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003385 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3386
3387 psa_key_policy_init( &policy );
3388 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3389 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3390
3391 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3392 key_data->x,
3393 key_data->len ) == PSA_SUCCESS );
3394
3395 /* Extraction phase. */
3396 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3397 salt->x, salt->len,
3398 label->x, label->len,
3399 requested_capacity ) == PSA_SUCCESS );
3400 TEST_ASSERT( psa_get_generator_capacity( &generator,
3401 &current_capacity ) ==
3402 PSA_SUCCESS );
3403 TEST_ASSERT( current_capacity == requested_capacity );
3404 expected_capacity = requested_capacity;
3405
3406 /* Expansion phase. */
3407 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3408 {
3409 /* Read some bytes. */
3410 status = psa_generator_read( &generator,
3411 output_buffer, output_sizes[i] );
3412 if( expected_capacity == 0 && output_sizes[i] == 0 )
3413 {
3414 /* Reading 0 bytes when 0 bytes are available can go either way. */
3415 TEST_ASSERT( status == PSA_SUCCESS ||
3416 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3417 continue;
3418 }
3419 else if( expected_capacity == 0 ||
3420 output_sizes[i] > expected_capacity )
3421 {
3422 /* Capacity exceeded. */
3423 TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3424 expected_capacity = 0;
3425 continue;
3426 }
3427 /* Success. Check the read data. */
3428 TEST_ASSERT( status == PSA_SUCCESS );
3429 if( output_sizes[i] != 0 )
3430 TEST_ASSERT( memcmp( output_buffer, expected_outputs[i],
3431 output_sizes[i] ) == 0 );
3432 /* Check the generator status. */
3433 expected_capacity -= output_sizes[i];
3434 TEST_ASSERT( psa_get_generator_capacity( &generator,
3435 &current_capacity ) ==
3436 PSA_SUCCESS );
3437 TEST_ASSERT( expected_capacity == current_capacity );
3438 }
3439 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3440
3441exit:
3442 mbedtls_free( output_buffer );
3443 psa_generator_abort( &generator );
3444 psa_destroy_key( slot );
3445 mbedtls_psa_crypto_free( );
3446}
3447/* END_CASE */
3448
3449/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003450void derive_full( int alg_arg,
3451 data_t *key_data,
3452 data_t *salt,
3453 data_t *label,
3454 int requested_capacity_arg )
3455{
3456 psa_key_slot_t slot = 1;
3457 psa_algorithm_t alg = alg_arg;
3458 size_t requested_capacity = requested_capacity_arg;
3459 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3460 unsigned char output_buffer[16];
3461 size_t expected_capacity = requested_capacity;
3462 size_t current_capacity;
3463 psa_key_policy_t policy;
3464
3465 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3466
3467 psa_key_policy_init( &policy );
3468 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3469 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3470
3471 TEST_ASSERT( psa_import_key( slot, PSA_KEY_TYPE_DERIVE,
3472 key_data->x,
3473 key_data->len ) == PSA_SUCCESS );
3474
3475 /* Extraction phase. */
3476 TEST_ASSERT( psa_key_derivation( &generator, slot, alg,
3477 salt->x, salt->len,
3478 label->x, label->len,
3479 requested_capacity ) == PSA_SUCCESS );
3480 TEST_ASSERT( psa_get_generator_capacity( &generator,
3481 &current_capacity ) ==
3482 PSA_SUCCESS );
3483 TEST_ASSERT( current_capacity == expected_capacity );
3484
3485 /* Expansion phase. */
3486 while( current_capacity > 0 )
3487 {
3488 size_t read_size = sizeof( output_buffer );
3489 if( read_size > current_capacity )
3490 read_size = current_capacity;
3491 TEST_ASSERT( psa_generator_read( &generator,
3492 output_buffer,
3493 read_size ) == PSA_SUCCESS );
3494 expected_capacity -= read_size;
3495 TEST_ASSERT( psa_get_generator_capacity( &generator,
3496 &current_capacity ) ==
3497 PSA_SUCCESS );
3498 TEST_ASSERT( current_capacity == expected_capacity );
3499 }
3500
3501 /* Check that the generator refuses to go over capacity. */
3502 TEST_ASSERT( psa_generator_read( &generator,
3503 output_buffer,
3504 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY );
3505
3506 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3507
3508exit:
3509 psa_generator_abort( &generator );
3510 psa_destroy_key( slot );
3511 mbedtls_psa_crypto_free( );
3512}
3513/* END_CASE */
3514
3515/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003516void derive_key_exercise( int alg_arg,
3517 data_t *key_data,
3518 data_t *salt,
3519 data_t *label,
3520 int derived_type_arg,
3521 int derived_bits_arg,
3522 int derived_usage_arg,
3523 int derived_alg_arg )
3524{
3525 psa_key_slot_t base_key = 1;
3526 psa_key_slot_t derived_key = 2;
3527 psa_algorithm_t alg = alg_arg;
3528 psa_key_type_t derived_type = derived_type_arg;
3529 size_t derived_bits = derived_bits_arg;
3530 psa_key_usage_t derived_usage = derived_usage_arg;
3531 psa_algorithm_t derived_alg = derived_alg_arg;
3532 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3533 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3534 psa_key_policy_t policy;
3535 psa_key_type_t got_type;
3536 size_t got_bits;
3537
3538 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3539
3540 psa_key_policy_init( &policy );
3541 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3542 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3543 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3544 key_data->x,
3545 key_data->len ) == PSA_SUCCESS );
3546
3547 /* Derive a key. */
3548 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3549 salt->x, salt->len,
3550 label->x, label->len,
3551 capacity ) == PSA_SUCCESS );
3552 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
3553 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3554 TEST_ASSERT( psa_generator_import_key( derived_key,
3555 derived_type,
3556 derived_bits,
3557 &generator ) == PSA_SUCCESS );
3558
3559 /* Test the key information */
3560 TEST_ASSERT( psa_get_key_information( derived_key,
3561 &got_type,
3562 &got_bits ) == PSA_SUCCESS );
3563 TEST_ASSERT( got_type == derived_type );
3564 TEST_ASSERT( got_bits == derived_bits );
3565
3566 /* Exercise the derived key. */
3567 if( ! exercise_key( derived_key, derived_usage, derived_alg ) )
3568 goto exit;
3569
3570exit:
3571 psa_generator_abort( &generator );
3572 psa_destroy_key( base_key );
3573 psa_destroy_key( derived_key );
3574 mbedtls_psa_crypto_free( );
3575}
3576/* END_CASE */
3577
3578/* BEGIN_CASE */
3579void derive_key_export( int alg_arg,
3580 data_t *key_data,
3581 data_t *salt,
3582 data_t *label,
3583 int bytes1_arg,
3584 int bytes2_arg )
3585{
3586 psa_key_slot_t base_key = 1;
3587 psa_key_slot_t derived_key = 2;
3588 psa_algorithm_t alg = alg_arg;
3589 size_t bytes1 = bytes1_arg;
3590 size_t bytes2 = bytes2_arg;
3591 size_t capacity = bytes1 + bytes2;
3592 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003593 uint8_t *output_buffer = NULL;
3594 uint8_t *export_buffer = NULL;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003595 psa_key_policy_t policy;
3596 size_t length;
3597
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003598 ASSERT_ALLOC( output_buffer, capacity );
3599 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003600 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3601
3602 psa_key_policy_init( &policy );
3603 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
3604 TEST_ASSERT( psa_set_key_policy( base_key, &policy ) == PSA_SUCCESS );
3605 TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3606 key_data->x,
3607 key_data->len ) == PSA_SUCCESS );
3608
3609 /* Derive some material and output it. */
3610 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3611 salt->x, salt->len,
3612 label->x, label->len,
3613 capacity ) == PSA_SUCCESS );
3614 TEST_ASSERT( psa_generator_read( &generator,
3615 output_buffer,
3616 capacity ) == PSA_SUCCESS );
3617 TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS );
3618
3619 /* Derive the same output again, but this time store it in key objects. */
3620 TEST_ASSERT( psa_key_derivation( &generator, base_key, alg,
3621 salt->x, salt->len,
3622 label->x, label->len,
3623 capacity ) == PSA_SUCCESS );
3624 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
3625 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3626 TEST_ASSERT( psa_generator_import_key( derived_key,
3627 PSA_KEY_TYPE_RAW_DATA,
3628 PSA_BYTES_TO_BITS( bytes1 ),
3629 &generator ) == PSA_SUCCESS );
3630 TEST_ASSERT( psa_export_key( derived_key,
3631 export_buffer, bytes1,
3632 &length ) == PSA_SUCCESS );
3633 TEST_ASSERT( length == bytes1 );
3634 TEST_ASSERT( psa_destroy_key( derived_key ) == PSA_SUCCESS );
3635 TEST_ASSERT( psa_set_key_policy( derived_key, &policy ) == PSA_SUCCESS );
3636 TEST_ASSERT( psa_generator_import_key( derived_key,
3637 PSA_KEY_TYPE_RAW_DATA,
3638 PSA_BYTES_TO_BITS( bytes2 ),
3639 &generator ) == PSA_SUCCESS );
3640 TEST_ASSERT( psa_export_key( derived_key,
3641 export_buffer + bytes1, bytes2,
3642 &length ) == PSA_SUCCESS );
3643 TEST_ASSERT( length == bytes2 );
3644
3645 /* Compare the outputs from the two runs. */
3646 TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 );
3647
3648exit:
3649 mbedtls_free( output_buffer );
3650 mbedtls_free( export_buffer );
3651 psa_generator_abort( &generator );
3652 psa_destroy_key( base_key );
3653 psa_destroy_key( derived_key );
3654 mbedtls_psa_crypto_free( );
3655}
3656/* END_CASE */
3657
3658/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003659void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003660{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003661 size_t bytes = bytes_arg;
3662 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003663 unsigned char *output = NULL;
3664 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003665 size_t i;
3666 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003667
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003668 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3669 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003670 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003671
3672 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3673
Gilles Peskinea50d7392018-06-21 10:22:13 +02003674 /* Run several times, to ensure that every output byte will be
3675 * nonzero at least once with overwhelming probability
3676 * (2^(-8*number_of_runs)). */
3677 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003678 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003679 if( bytes != 0 )
3680 memset( output, 0, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003681 TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
3682
3683 /* Check that no more than bytes have been overwritten */
3684 TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
3685
3686 for( i = 0; i < bytes; i++ )
3687 {
3688 if( output[i] != 0 )
3689 ++changed[i];
3690 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003691 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003692
3693 /* Check that every byte was changed to nonzero at least once. This
3694 * validates that psa_generate_random is overwriting every byte of
3695 * the output buffer. */
3696 for( i = 0; i < bytes; i++ )
3697 {
3698 TEST_ASSERT( changed[i] != 0 );
3699 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003700
3701exit:
3702 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003703 mbedtls_free( output );
3704 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003705}
3706/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003707
3708/* BEGIN_CASE */
3709void generate_key( int type_arg,
3710 int bits_arg,
3711 int usage_arg,
3712 int alg_arg,
3713 int expected_status_arg )
3714{
3715 int slot = 1;
3716 psa_key_type_t type = type_arg;
3717 psa_key_usage_t usage = usage_arg;
3718 size_t bits = bits_arg;
3719 psa_algorithm_t alg = alg_arg;
3720 psa_status_t expected_status = expected_status_arg;
3721 psa_key_type_t got_type;
3722 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003723 psa_status_t expected_info_status =
3724 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
3725 psa_key_policy_t policy;
3726
3727 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
3728
3729 psa_key_policy_init( &policy );
3730 psa_key_policy_set_usage( &policy, usage, alg );
3731 TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS );
3732
3733 /* Generate a key */
3734 TEST_ASSERT( psa_generate_key( slot, type, bits,
3735 NULL, 0 ) == expected_status );
3736
3737 /* Test the key information */
3738 TEST_ASSERT( psa_get_key_information( slot,
3739 &got_type,
3740 &got_bits ) == expected_info_status );
3741 if( expected_info_status != PSA_SUCCESS )
3742 goto exit;
3743 TEST_ASSERT( got_type == type );
3744 TEST_ASSERT( got_bits == bits );
3745
Gilles Peskine818ca122018-06-20 18:16:48 +02003746 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine02b75072018-07-01 22:31:34 +02003747 if( ! exercise_key( slot, usage, alg ) )
3748 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003749
3750exit:
3751 psa_destroy_key( slot );
3752 mbedtls_psa_crypto_free( );
3753}
3754/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03003755
3756/* BEGIN_CASE */
3757void validate_module_init_generate_random( )
3758{
3759 psa_status_t status;
3760 uint8_t random[10] = { 0 };
3761 status = psa_generate_random( random, sizeof( random ) );
3762 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3763}
3764/* END_CASE */
itayzafrir90d8c7a2018-09-12 11:44:52 +03003765
3766/* BEGIN_CASE */
3767void validate_module_init_key_based( )
3768{
3769 psa_status_t status;
3770 uint8_t data[10] = { 0 };
3771 status = psa_import_key( 1, PSA_KEY_TYPE_RAW_DATA, data, sizeof( data ) );
3772 TEST_ASSERT( status == PSA_ERROR_BAD_STATE );
3773}
3774/* END_CASE */