blob: 8ca7dcd100f1f9150ffeb05b903849f256a30c21 [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
Jaeden Amerof24c7f82018-06-27 17:20:43 +010014/** An invalid export length that will never be set by psa_export_key(). */
15static const size_t INVALID_EXPORT_LENGTH = ~0U;
16
Gilles Peskinea7aa4422018-08-14 15:17:54 +020017/** Test if a buffer contains a constant byte value.
18 *
19 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020020 *
21 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020022 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020023 * \param size Size of the buffer in bytes.
24 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020025 * \return 1 if the buffer is all-bits-zero.
26 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020027 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020028static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020029{
30 size_t i;
31 for( i = 0; i < size; i++ )
32 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020033 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020034 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020035 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020036 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037}
Gilles Peskine818ca122018-06-20 18:16:48 +020038
Gilles Peskine0b352bc2018-06-28 00:16:11 +020039/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
40static int asn1_write_10x( unsigned char **p,
41 unsigned char *start,
42 size_t bits,
43 unsigned char x )
44{
45 int ret;
46 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020047 if( bits == 0 )
48 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
49 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020050 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030051 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020052 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
53 *p -= len;
54 ( *p )[len-1] = x;
55 if( bits % 8 == 0 )
56 ( *p )[1] |= 1;
57 else
58 ( *p )[0] |= 1 << ( bits % 8 );
59 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
60 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
61 MBEDTLS_ASN1_INTEGER ) );
62 return( len );
63}
64
65static int construct_fake_rsa_key( unsigned char *buffer,
66 size_t buffer_size,
67 unsigned char **p,
68 size_t bits,
69 int keypair )
70{
71 size_t half_bits = ( bits + 1 ) / 2;
72 int ret;
73 int len = 0;
74 /* Construct something that looks like a DER encoding of
75 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
76 * RSAPrivateKey ::= SEQUENCE {
77 * version Version,
78 * modulus INTEGER, -- n
79 * publicExponent INTEGER, -- e
80 * privateExponent INTEGER, -- d
81 * prime1 INTEGER, -- p
82 * prime2 INTEGER, -- q
83 * exponent1 INTEGER, -- d mod (p-1)
84 * exponent2 INTEGER, -- d mod (q-1)
85 * coefficient INTEGER, -- (inverse of q) mod p
86 * otherPrimeInfos OtherPrimeInfos OPTIONAL
87 * }
88 * Or, for a public key, the same structure with only
89 * version, modulus and publicExponent.
90 */
91 *p = buffer + buffer_size;
92 if( keypair )
93 {
94 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
95 asn1_write_10x( p, buffer, half_bits, 1 ) );
96 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
97 asn1_write_10x( p, buffer, half_bits, 1 ) );
98 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
99 asn1_write_10x( p, buffer, half_bits, 1 ) );
100 MBEDTLS_ASN1_CHK_ADD( len, /* q */
101 asn1_write_10x( p, buffer, half_bits, 1 ) );
102 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
103 asn1_write_10x( p, buffer, half_bits, 3 ) );
104 MBEDTLS_ASN1_CHK_ADD( len, /* d */
105 asn1_write_10x( p, buffer, bits, 1 ) );
106 }
107 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
108 asn1_write_10x( p, buffer, 17, 1 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* n */
110 asn1_write_10x( p, buffer, bits, 1 ) );
111 if( keypair )
112 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
113 mbedtls_asn1_write_int( p, buffer, 0 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
115 {
116 const unsigned char tag =
117 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
118 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
119 }
120 return( len );
121}
122
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100123static int exercise_mac_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200124 psa_key_usage_t usage,
125 psa_algorithm_t alg )
126{
Jaeden Amero769ce272019-01-04 11:48:03 +0000127 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200128 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200129 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200130 size_t mac_length = sizeof( mac );
131
132 if( usage & PSA_KEY_USAGE_SIGN )
133 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100134 PSA_ASSERT( psa_mac_sign_setup( &operation,
135 handle, alg ) );
136 PSA_ASSERT( psa_mac_update( &operation,
137 input, sizeof( input ) ) );
138 PSA_ASSERT( psa_mac_sign_finish( &operation,
139 mac, sizeof( mac ),
140 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200141 }
142
143 if( usage & PSA_KEY_USAGE_VERIFY )
144 {
145 psa_status_t verify_status =
146 ( usage & PSA_KEY_USAGE_SIGN ?
147 PSA_SUCCESS :
148 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +0100149 PSA_ASSERT( psa_mac_verify_setup( &operation,
150 handle, alg ) );
151 PSA_ASSERT( psa_mac_update( &operation,
152 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100153 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
154 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200155 }
156
157 return( 1 );
158
159exit:
160 psa_mac_abort( &operation );
161 return( 0 );
162}
163
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100164static int exercise_cipher_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200165 psa_key_usage_t usage,
166 psa_algorithm_t alg )
167{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000168 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200169 unsigned char iv[16] = {0};
170 size_t iv_length = sizeof( iv );
171 const unsigned char plaintext[16] = "Hello, world...";
172 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
173 size_t ciphertext_length = sizeof( ciphertext );
174 unsigned char decrypted[sizeof( ciphertext )];
175 size_t part_length;
176
177 if( usage & PSA_KEY_USAGE_ENCRYPT )
178 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100179 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
180 handle, alg ) );
181 PSA_ASSERT( psa_cipher_generate_iv( &operation,
182 iv, sizeof( iv ),
183 &iv_length ) );
184 PSA_ASSERT( psa_cipher_update( &operation,
185 plaintext, sizeof( plaintext ),
186 ciphertext, sizeof( ciphertext ),
187 &ciphertext_length ) );
188 PSA_ASSERT( psa_cipher_finish( &operation,
189 ciphertext + ciphertext_length,
190 sizeof( ciphertext ) - ciphertext_length,
191 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200192 ciphertext_length += part_length;
193 }
194
195 if( usage & PSA_KEY_USAGE_DECRYPT )
196 {
197 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700198 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200199 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
200 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200201 size_t bits;
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100202 TEST_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200203 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
204 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100205 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
206 handle, alg ) );
207 PSA_ASSERT( psa_cipher_set_iv( &operation,
208 iv, iv_length ) );
209 PSA_ASSERT( psa_cipher_update( &operation,
210 ciphertext, ciphertext_length,
211 decrypted, sizeof( decrypted ),
212 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200213 status = psa_cipher_finish( &operation,
214 decrypted + part_length,
215 sizeof( decrypted ) - part_length,
216 &part_length );
217 /* For a stream cipher, all inputs are valid. For a block cipher,
218 * if the input is some aribtrary data rather than an actual
219 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700220 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700221 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100222 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200223 else
224 TEST_ASSERT( status == PSA_SUCCESS ||
225 status == PSA_ERROR_INVALID_PADDING );
226 }
227
228 return( 1 );
229
230exit:
231 psa_cipher_abort( &operation );
232 return( 0 );
233}
234
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100235static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200236 psa_key_usage_t usage,
237 psa_algorithm_t alg )
238{
239 unsigned char nonce[16] = {0};
240 size_t nonce_length = sizeof( nonce );
241 unsigned char plaintext[16] = "Hello, world...";
242 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
243 size_t ciphertext_length = sizeof( ciphertext );
244 size_t plaintext_length = sizeof( ciphertext );
245
246 if( usage & PSA_KEY_USAGE_ENCRYPT )
247 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100248 PSA_ASSERT( psa_aead_encrypt( handle, alg,
249 nonce, nonce_length,
250 NULL, 0,
251 plaintext, sizeof( plaintext ),
252 ciphertext, sizeof( ciphertext ),
253 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200254 }
255
256 if( usage & PSA_KEY_USAGE_DECRYPT )
257 {
258 psa_status_t verify_status =
259 ( usage & PSA_KEY_USAGE_ENCRYPT ?
260 PSA_SUCCESS :
261 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100262 TEST_EQUAL( psa_aead_decrypt( handle, alg,
263 nonce, nonce_length,
264 NULL, 0,
265 ciphertext, ciphertext_length,
266 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100267 &plaintext_length ),
268 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200269 }
270
271 return( 1 );
272
273exit:
274 return( 0 );
275}
276
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100277static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200278 psa_key_usage_t usage,
279 psa_algorithm_t alg )
280{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200281 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
282 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200283 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200284 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100285 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
286
287 /* If the policy allows signing with any hash, just pick one. */
288 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
289 {
290#if defined(MBEDTLS_MD2_C)
291 hash_alg = PSA_ALG_MD2;
292#elif defined(MBEDTLS_MD4_C)
293 hash_alg = PSA_ALG_MD4;
294#elif defined(MBEDTLS_MD5_C)
295 hash_alg = PSA_ALG_MD5;
Gilles Peskine6b156df2019-01-28 15:43:19 +0100296 /* MBEDTLS_RIPEMD160_C omitted because Mbed TLS doesn't
297 * support it in RSA PKCS#1v1.5 signatures. */
Gilles Peskine57ab7212019-01-28 13:03:09 +0100298#elif defined(MBEDTLS_SHA1_C)
299 hash_alg = PSA_ALG_SHA_1;
300#elif defined(MBEDTLS_SHA256_C)
301 hash_alg = PSA_ALG_SHA_256;
302#elif defined(MBEDTLS_SHA512_C)
303 hash_alg = PSA_ALG_SHA_384;
304#elif defined(MBEDTLS_SHA3_C)
305 hash_alg = PSA_ALG_SHA3_256;
306#else
307 test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ );
308#endif
309 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
310 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200311
312 if( usage & PSA_KEY_USAGE_SIGN )
313 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200314 /* Some algorithms require the payload to have the size of
315 * the hash encoded in the algorithm. Use this input size
316 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200317 if( hash_alg != 0 )
318 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100319 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
320 payload, payload_length,
321 signature, sizeof( signature ),
322 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200323 }
324
325 if( usage & PSA_KEY_USAGE_VERIFY )
326 {
327 psa_status_t verify_status =
328 ( usage & PSA_KEY_USAGE_SIGN ?
329 PSA_SUCCESS :
330 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100331 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
332 payload, payload_length,
333 signature, signature_length ),
334 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200335 }
336
337 return( 1 );
338
339exit:
340 return( 0 );
341}
342
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100343static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200344 psa_key_usage_t usage,
345 psa_algorithm_t alg )
346{
347 unsigned char plaintext[256] = "Hello, world...";
348 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
349 size_t ciphertext_length = sizeof( ciphertext );
350 size_t plaintext_length = 16;
351
352 if( usage & PSA_KEY_USAGE_ENCRYPT )
353 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100354 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
355 plaintext, plaintext_length,
356 NULL, 0,
357 ciphertext, sizeof( ciphertext ),
358 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200359 }
360
361 if( usage & PSA_KEY_USAGE_DECRYPT )
362 {
363 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100364 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200365 ciphertext, ciphertext_length,
366 NULL, 0,
367 plaintext, sizeof( plaintext ),
368 &plaintext_length );
369 TEST_ASSERT( status == PSA_SUCCESS ||
370 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
371 ( status == PSA_ERROR_INVALID_ARGUMENT ||
372 status == PSA_ERROR_INVALID_PADDING ) ) );
373 }
374
375 return( 1 );
376
377exit:
378 return( 0 );
379}
Gilles Peskine02b75072018-07-01 22:31:34 +0200380
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100381static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200382 psa_key_usage_t usage,
383 psa_algorithm_t alg )
384{
385 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
386 unsigned char label[16] = "This is a label.";
387 size_t label_length = sizeof( label );
388 unsigned char seed[16] = "abcdefghijklmnop";
389 size_t seed_length = sizeof( seed );
390 unsigned char output[1];
391
392 if( usage & PSA_KEY_USAGE_DERIVE )
393 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100394 PSA_ASSERT( psa_key_derivation( &generator,
395 handle, alg,
396 label, label_length,
397 seed, seed_length,
398 sizeof( output ) ) );
399 PSA_ASSERT( psa_generator_read( &generator,
400 output,
401 sizeof( output ) ) );
402 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200403 }
404
405 return( 1 );
406
407exit:
408 return( 0 );
409}
410
Gilles Peskinec7998b72018-11-07 18:45:02 +0100411/* We need two keys to exercise key agreement. Exercise the
412 * private key against its own public key. */
413static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator,
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100414 psa_key_handle_t handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100415 psa_algorithm_t alg )
416{
417 psa_key_type_t private_key_type;
418 psa_key_type_t public_key_type;
419 size_t key_bits;
420 uint8_t *public_key = NULL;
421 size_t public_key_length;
422 /* Return UNKNOWN_ERROR if something other than the final call to
423 * psa_key_agreement fails. This isn't fully satisfactory, but it's
424 * good enough: callers will report it as a failed test anyway. */
425 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
426
Gilles Peskine8817f612018-12-18 00:18:46 +0100427 PSA_ASSERT( psa_get_key_information( handle,
428 &private_key_type,
429 &key_bits ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100430 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
431 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
432 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100433 PSA_ASSERT( psa_export_public_key( handle,
434 public_key, public_key_length,
435 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100436
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100437 status = psa_key_agreement( generator, handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100438 public_key, public_key_length,
439 alg );
440exit:
441 mbedtls_free( public_key );
442 return( status );
443}
444
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100445static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200446 psa_key_usage_t usage,
447 psa_algorithm_t alg )
448{
449 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200450 unsigned char output[1];
451 int ok = 0;
452
453 if( usage & PSA_KEY_USAGE_DERIVE )
454 {
455 /* We need two keys to exercise key agreement. Exercise the
456 * private key against its own public key. */
Gilles Peskine8817f612018-12-18 00:18:46 +0100457 PSA_ASSERT( key_agreement_with_self( &generator, handle, alg ) );
458 PSA_ASSERT( psa_generator_read( &generator,
459 output,
460 sizeof( output ) ) );
461 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200462 }
463 ok = 1;
464
465exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200466 return( ok );
467}
468
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200469static int is_oid_of_key_type( psa_key_type_t type,
470 const uint8_t *oid, size_t oid_length )
471{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200472 const uint8_t *expected_oid = NULL;
473 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200474#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200475 if( PSA_KEY_TYPE_IS_RSA( type ) )
476 {
477 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
478 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
479 }
480 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200481#endif /* MBEDTLS_RSA_C */
482#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200483 if( PSA_KEY_TYPE_IS_ECC( type ) )
484 {
485 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
486 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
487 }
488 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200489#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200490 {
491 char message[40];
492 mbedtls_snprintf( message, sizeof( message ),
493 "OID not known for key type=0x%08lx",
494 (unsigned long) type );
495 test_fail( message, __LINE__, __FILE__ );
496 return( 0 );
497 }
498
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200499 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200500 return( 1 );
501
502exit:
503 return( 0 );
504}
505
506static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
507 size_t min_bits, size_t max_bits,
508 int must_be_odd )
509{
510 size_t len;
511 size_t actual_bits;
512 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100513 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100514 MBEDTLS_ASN1_INTEGER ),
515 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200516 /* Tolerate a slight departure from DER encoding:
517 * - 0 may be represented by an empty string or a 1-byte string.
518 * - The sign bit may be used as a value bit. */
519 if( ( len == 1 && ( *p )[0] == 0 ) ||
520 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
521 {
522 ++( *p );
523 --len;
524 }
525 if( min_bits == 0 && len == 0 )
526 return( 1 );
527 msb = ( *p )[0];
528 TEST_ASSERT( msb != 0 );
529 actual_bits = 8 * ( len - 1 );
530 while( msb != 0 )
531 {
532 msb >>= 1;
533 ++actual_bits;
534 }
535 TEST_ASSERT( actual_bits >= min_bits );
536 TEST_ASSERT( actual_bits <= max_bits );
537 if( must_be_odd )
538 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
539 *p += len;
540 return( 1 );
541exit:
542 return( 0 );
543}
544
545static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
546 size_t *len,
547 unsigned char n, unsigned char tag )
548{
549 int ret;
550 ret = mbedtls_asn1_get_tag( p, end, len,
551 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
552 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
553 if( ret != 0 )
554 return( ret );
555 end = *p + *len;
556 ret = mbedtls_asn1_get_tag( p, end, len, tag );
557 if( ret != 0 )
558 return( ret );
559 if( *p + *len != end )
560 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
561 return( 0 );
562}
563
564static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
565 uint8_t *exported, size_t exported_length )
566{
567 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100568 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200569 else
570 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200571
572#if defined(MBEDTLS_DES_C)
573 if( type == PSA_KEY_TYPE_DES )
574 {
575 /* Check the parity bits. */
576 unsigned i;
577 for( i = 0; i < bits / 8; i++ )
578 {
579 unsigned bit_count = 0;
580 unsigned m;
581 for( m = 1; m <= 0x100; m <<= 1 )
582 {
583 if( exported[i] & m )
584 ++bit_count;
585 }
586 TEST_ASSERT( bit_count % 2 != 0 );
587 }
588 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200589 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200590#endif
591
592#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
593 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
594 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200595 uint8_t *p = exported;
596 uint8_t *end = exported + exported_length;
597 size_t len;
598 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200599 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200600 * modulus INTEGER, -- n
601 * publicExponent INTEGER, -- e
602 * privateExponent INTEGER, -- d
603 * prime1 INTEGER, -- p
604 * prime2 INTEGER, -- q
605 * exponent1 INTEGER, -- d mod (p-1)
606 * exponent2 INTEGER, -- d mod (q-1)
607 * coefficient INTEGER, -- (inverse of q) mod p
608 * }
609 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100610 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
611 MBEDTLS_ASN1_SEQUENCE |
612 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
613 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200614 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
615 goto exit;
616 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
617 goto exit;
618 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
619 goto exit;
620 /* Require d to be at least half the size of n. */
621 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
622 goto exit;
623 /* Require p and q to be at most half the size of n, rounded up. */
624 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
625 goto exit;
626 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
627 goto exit;
628 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
629 goto exit;
630 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
631 goto exit;
632 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
633 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100634 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100635 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200636 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200637#endif /* MBEDTLS_RSA_C */
638
639#if defined(MBEDTLS_ECP_C)
640 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
641 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100642 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100643 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100644 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200645 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200646#endif /* MBEDTLS_ECP_C */
647
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200648 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
649 {
650 uint8_t *p = exported;
651 uint8_t *end = exported + exported_length;
652 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200653#if defined(MBEDTLS_RSA_C)
654 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
655 {
656 /* RSAPublicKey ::= SEQUENCE {
657 * modulus INTEGER, -- n
658 * publicExponent INTEGER } -- e
659 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100660 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
661 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100662 MBEDTLS_ASN1_CONSTRUCTED ),
663 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100664 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200665 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
666 goto exit;
667 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
668 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100669 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200670 }
671 else
672#endif /* MBEDTLS_RSA_C */
673#if defined(MBEDTLS_ECP_C)
674 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
675 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000676 /* The representation of an ECC public key is:
677 * - The byte 0x04;
678 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
679 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
680 * - where m is the bit size associated with the curve.
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200681 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100682 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
683 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200684 }
685 else
686#endif /* MBEDTLS_ECP_C */
687 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100688 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200689 mbedtls_snprintf( message, sizeof( message ),
690 "No sanity check for public key type=0x%08lx",
691 (unsigned long) type );
692 test_fail( message, __LINE__, __FILE__ );
693 return( 0 );
694 }
695 }
696 else
697
698 {
699 /* No sanity checks for other types */
700 }
701
702 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200703
704exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200705 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200706}
707
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100708static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200709 psa_key_usage_t usage )
710{
711 psa_key_type_t type;
712 size_t bits;
713 uint8_t *exported = NULL;
714 size_t exported_size = 0;
715 size_t exported_length = 0;
716 int ok = 0;
717
Gilles Peskine8817f612018-12-18 00:18:46 +0100718 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200719
720 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
721 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200722 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100723 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
724 PSA_ERROR_NOT_PERMITTED );
Gilles Peskined14664a2018-08-10 19:07:32 +0200725 return( 1 );
726 }
727
Gilles Peskined14664a2018-08-10 19:07:32 +0200728 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200729 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200730
Gilles Peskine8817f612018-12-18 00:18:46 +0100731 PSA_ASSERT( psa_export_key( handle,
732 exported, exported_size,
733 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200734 ok = exported_key_sanity_check( type, bits, exported, exported_length );
735
736exit:
737 mbedtls_free( exported );
738 return( ok );
739}
740
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100741static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200742{
743 psa_key_type_t type;
744 psa_key_type_t public_type;
745 size_t bits;
746 uint8_t *exported = NULL;
747 size_t exported_size = 0;
748 size_t exported_length = 0;
749 int ok = 0;
750
Gilles Peskine8817f612018-12-18 00:18:46 +0100751 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200752 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
753 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100754 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100755 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200756 return( 1 );
757 }
758
759 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
760 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200761 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200762
Gilles Peskine8817f612018-12-18 00:18:46 +0100763 PSA_ASSERT( psa_export_public_key( handle,
764 exported, exported_size,
765 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200766 ok = exported_key_sanity_check( public_type, bits,
767 exported, exported_length );
768
769exit:
770 mbedtls_free( exported );
771 return( ok );
772}
773
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100774static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +0200775 psa_key_usage_t usage,
776 psa_algorithm_t alg )
777{
778 int ok;
779 if( alg == 0 )
780 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
781 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100782 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200783 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100784 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200785 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100786 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200787 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100788 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200789 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100790 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200791 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100792 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200793 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100794 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200795 else
796 {
797 char message[40];
798 mbedtls_snprintf( message, sizeof( message ),
799 "No code to exercise alg=0x%08lx",
800 (unsigned long) alg );
801 test_fail( message, __LINE__, __FILE__ );
802 ok = 0;
803 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200804
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100805 ok = ok && exercise_export_key( handle, usage );
806 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +0200807
Gilles Peskine02b75072018-07-01 22:31:34 +0200808 return( ok );
809}
810
Gilles Peskine10df3412018-10-25 22:35:43 +0200811static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
812 psa_algorithm_t alg )
813{
814 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
815 {
816 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
817 PSA_KEY_USAGE_VERIFY :
818 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
819 }
820 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
821 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
822 {
823 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
824 PSA_KEY_USAGE_ENCRYPT :
825 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
826 }
827 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
828 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
829 {
830 return( PSA_KEY_USAGE_DERIVE );
831 }
832 else
833 {
834 return( 0 );
835 }
836
837}
Darryl Green0c6575a2018-11-07 16:05:30 +0000838
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100839/* An overapproximation of the amount of storage needed for a key of the
840 * given type and with the given content. The API doesn't make it easy
841 * to find a good value for the size. The current implementation doesn't
842 * care about the value anyway. */
843#define KEY_BITS_FROM_DATA( type, data ) \
844 ( data )->len
845
Darryl Green0c6575a2018-11-07 16:05:30 +0000846typedef enum {
847 IMPORT_KEY = 0,
848 GENERATE_KEY = 1,
849 DERIVE_KEY = 2
850} generate_method;
851
Gilles Peskinee59236f2018-01-27 23:32:46 +0100852/* END_HEADER */
853
854/* BEGIN_DEPENDENCIES
855 * depends_on:MBEDTLS_PSA_CRYPTO_C
856 * END_DEPENDENCIES
857 */
858
859/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200860void static_checks( )
861{
862 size_t max_truncated_mac_size =
863 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
864
865 /* Check that the length for a truncated MAC always fits in the algorithm
866 * encoding. The shifted mask is the maximum truncated value. The
867 * untruncated algorithm may be one byte larger. */
868 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
869}
870/* END_CASE */
871
872/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200873void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100874{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100875 psa_key_handle_t handle = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200876 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100877 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100878
Gilles Peskine8817f612018-12-18 00:18:46 +0100879 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100880
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100881 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100882 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100883 TEST_EQUAL( status, expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100884 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100885 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100886
887exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100888 mbedtls_psa_crypto_free( );
889}
890/* END_CASE */
891
892/* BEGIN_CASE */
Gilles Peskinea4261682018-12-03 11:34:01 +0100893void import_twice( int alg_arg, int usage_arg,
894 int type1_arg, data_t *data1,
895 int expected_import1_status_arg,
896 int type2_arg, data_t *data2,
897 int expected_import2_status_arg )
898{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100899 psa_key_handle_t handle = 0;
Gilles Peskinea4261682018-12-03 11:34:01 +0100900 psa_algorithm_t alg = alg_arg;
901 psa_key_usage_t usage = usage_arg;
902 psa_key_type_t type1 = type1_arg;
903 psa_status_t expected_import1_status = expected_import1_status_arg;
904 psa_key_type_t type2 = type2_arg;
905 psa_status_t expected_import2_status = expected_import2_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +0000906 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea4261682018-12-03 11:34:01 +0100907 psa_status_t status;
908
Gilles Peskine8817f612018-12-18 00:18:46 +0100909 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100910
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100911 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100912 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100913 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100914
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100915 status = psa_import_key( handle, type1, data1->x, data1->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100916 TEST_EQUAL( status, expected_import1_status );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100917 status = psa_import_key( handle, type2, data2->x, data2->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100918 TEST_EQUAL( status, expected_import2_status );
Gilles Peskinea4261682018-12-03 11:34:01 +0100919
920 if( expected_import1_status == PSA_SUCCESS ||
921 expected_import2_status == PSA_SUCCESS )
922 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100923 TEST_ASSERT( exercise_key( handle, usage, alg ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100924 }
925
926exit:
927 mbedtls_psa_crypto_free( );
928}
929/* END_CASE */
930
931/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200932void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
933{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100934 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200935 size_t bits = bits_arg;
936 psa_status_t expected_status = expected_status_arg;
937 psa_status_t status;
938 psa_key_type_t type =
939 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
940 size_t buffer_size = /* Slight overapproximations */
941 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200942 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200943 unsigned char *p;
944 int ret;
945 size_t length;
946
Gilles Peskine8817f612018-12-18 00:18:46 +0100947 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200948 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200949
950 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
951 bits, keypair ) ) >= 0 );
952 length = ret;
953
954 /* Try importing the key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100955 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100956 status = psa_import_key( handle, type, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100957 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200958 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100959 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200960
961exit:
962 mbedtls_free( buffer );
963 mbedtls_psa_crypto_free( );
964}
965/* END_CASE */
966
967/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300968void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300969 int type_arg,
970 int alg_arg,
971 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100972 int expected_bits,
973 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200974 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100975 int canonical_input )
976{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100977 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100978 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200979 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200980 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100981 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100982 unsigned char *exported = NULL;
983 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100984 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100985 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100986 size_t reexported_length;
987 psa_key_type_t got_type;
988 size_t got_bits;
Jaeden Amero70261c52019-01-04 11:47:20 +0000989 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100990
Moran Pekercb088e72018-07-17 17:36:59 +0300991 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200992 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100993 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200994 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100995 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100996
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100997 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200998 psa_key_policy_set_usage( &policy, usage_arg, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100999 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001000
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001001 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1002 PSA_ERROR_EMPTY_SLOT );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001003
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001004 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001005 PSA_ASSERT( psa_import_key( handle, type,
1006 data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001007
1008 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001009 PSA_ASSERT( psa_get_key_information( handle,
1010 &got_type,
1011 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001012 TEST_EQUAL( got_type, type );
1013 TEST_EQUAL( got_bits, (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001014
1015 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001016 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001017 exported, export_size,
1018 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001019 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001020
1021 /* The exported length must be set by psa_export_key() to a value between 0
1022 * and export_size. On errors, the exported length must be 0. */
1023 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1024 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1025 TEST_ASSERT( exported_length <= export_size );
1026
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001027 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001028 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001029 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001030 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001031 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001032 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001033 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001034
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001035 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001036 goto exit;
1037
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001038 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001039 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001040 else
1041 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001042 psa_key_handle_t handle2;
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001043 PSA_ASSERT( psa_allocate_key( &handle2 ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001044 PSA_ASSERT( psa_set_key_policy( handle2, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001045
Gilles Peskine8817f612018-12-18 00:18:46 +01001046 PSA_ASSERT( psa_import_key( handle2, type,
1047 exported,
1048 exported_length ) );
1049 PSA_ASSERT( psa_export_key( handle2,
1050 reexported,
1051 export_size,
1052 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001053 ASSERT_COMPARE( exported, exported_length,
1054 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001055 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001056 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001057 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001058
1059destroy:
1060 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001061 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001062 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1063 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001064
1065exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001066 mbedtls_free( exported );
1067 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001068 mbedtls_psa_crypto_free( );
1069}
1070/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001071
Moran Pekerf709f4a2018-06-06 17:26:04 +03001072/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001073void import_key_nonempty_slot( )
1074{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001075 psa_key_handle_t handle = 0;
Moran Peker28a38e62018-11-07 16:18:24 +02001076 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1077 psa_status_t status;
1078 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
Gilles Peskine8817f612018-12-18 00:18:46 +01001079 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001080
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001081 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001082
Moran Peker28a38e62018-11-07 16:18:24 +02001083 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001084 PSA_ASSERT( psa_import_key( handle, type,
1085 data, sizeof( data ) ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001086
1087 /* Import the key again */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001088 status = psa_import_key( handle, type, data, sizeof( data ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001089 TEST_EQUAL( status, PSA_ERROR_OCCUPIED_SLOT );
Moran Peker28a38e62018-11-07 16:18:24 +02001090
1091exit:
1092 mbedtls_psa_crypto_free( );
1093}
1094/* END_CASE */
1095
1096/* BEGIN_CASE */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001097void export_invalid_handle( int handle, int expected_export_status_arg )
Moran Peker28a38e62018-11-07 16:18:24 +02001098{
1099 psa_status_t status;
1100 unsigned char *exported = NULL;
1101 size_t export_size = 0;
1102 size_t exported_length = INVALID_EXPORT_LENGTH;
1103 psa_status_t expected_export_status = expected_export_status_arg;
1104
Gilles Peskine8817f612018-12-18 00:18:46 +01001105 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001106
1107 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001108 status = psa_export_key( (psa_key_handle_t) handle,
Moran Peker28a38e62018-11-07 16:18:24 +02001109 exported, export_size,
1110 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001111 TEST_EQUAL( status, expected_export_status );
Moran Peker28a38e62018-11-07 16:18:24 +02001112
1113exit:
1114 mbedtls_psa_crypto_free( );
1115}
1116/* END_CASE */
1117
1118/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001119void export_with_no_key_activity( )
1120{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001121 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001122 psa_algorithm_t alg = PSA_ALG_CTR;
1123 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001124 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001125 unsigned char *exported = NULL;
1126 size_t export_size = 0;
1127 size_t exported_length = INVALID_EXPORT_LENGTH;
1128
Gilles Peskine8817f612018-12-18 00:18:46 +01001129 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001130
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001131 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001132 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001133 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001134
1135 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001136 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001137 exported, export_size,
1138 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001139 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001140
1141exit:
1142 mbedtls_psa_crypto_free( );
1143}
1144/* END_CASE */
1145
1146/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001147void cipher_with_no_key_activity( )
1148{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001149 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001150 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001151 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001152 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001153 int exercise_alg = PSA_ALG_CTR;
1154
Gilles Peskine8817f612018-12-18 00:18:46 +01001155 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001156
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001157 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekerce500072018-11-07 16:20:07 +02001158 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001159 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerce500072018-11-07 16:20:07 +02001160
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001161 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001162 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001163
1164exit:
1165 psa_cipher_abort( &operation );
1166 mbedtls_psa_crypto_free( );
1167}
1168/* END_CASE */
1169
1170/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001171void export_after_import_failure( data_t *data, int type_arg,
1172 int expected_import_status_arg )
1173{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001174 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001175 psa_key_type_t type = type_arg;
1176 psa_status_t status;
1177 unsigned char *exported = NULL;
1178 size_t export_size = 0;
1179 psa_status_t expected_import_status = expected_import_status_arg;
1180 size_t exported_length = INVALID_EXPORT_LENGTH;
1181
Gilles Peskine8817f612018-12-18 00:18:46 +01001182 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001183
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001184 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001185
Moran Peker34550092018-11-07 16:19:34 +02001186 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001187 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001188 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001189 TEST_EQUAL( status, expected_import_status );
Moran Peker34550092018-11-07 16:19:34 +02001190
1191 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001192 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001193 exported, export_size,
1194 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001195 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001196
1197exit:
1198 mbedtls_psa_crypto_free( );
1199}
1200/* END_CASE */
1201
1202/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001203void cipher_after_import_failure( data_t *data, int type_arg,
1204 int expected_import_status_arg )
1205{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001206 psa_key_handle_t handle = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001207 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001208 psa_key_type_t type = type_arg;
1209 psa_status_t status;
1210 psa_status_t expected_import_status = expected_import_status_arg;
1211 int exercise_alg = PSA_ALG_CTR;
1212
Gilles Peskine8817f612018-12-18 00:18:46 +01001213 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001214
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001215 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001216
Moran Pekerce500072018-11-07 16:20:07 +02001217 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001218 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001219 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001220 TEST_EQUAL( status, expected_import_status );
Moran Pekerce500072018-11-07 16:20:07 +02001221
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001222 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001223 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001224
1225exit:
1226 psa_cipher_abort( &operation );
1227 mbedtls_psa_crypto_free( );
1228}
1229/* END_CASE */
1230
1231/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001232void export_after_destroy_key( data_t *data, int type_arg )
1233{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001234 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001235 psa_key_type_t type = type_arg;
1236 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001237 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001238 psa_algorithm_t alg = PSA_ALG_CTR;
1239 unsigned char *exported = NULL;
1240 size_t export_size = 0;
1241 size_t exported_length = INVALID_EXPORT_LENGTH;
1242
Gilles Peskine8817f612018-12-18 00:18:46 +01001243 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001244
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001245 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001246 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001247 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001248 export_size = (ptrdiff_t) data->len;
1249 ASSERT_ALLOC( exported, export_size );
1250
1251 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001252 PSA_ASSERT( psa_import_key( handle, type,
1253 data->x, data->len ) );
Moran Peker34550092018-11-07 16:19:34 +02001254
Gilles Peskine8817f612018-12-18 00:18:46 +01001255 PSA_ASSERT( psa_export_key( handle, exported, export_size,
1256 &exported_length ) );
Moran Peker34550092018-11-07 16:19:34 +02001257
1258 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001259 PSA_ASSERT( psa_destroy_key( handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001260
1261 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001262 status = psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001263 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001264 TEST_EQUAL( status, PSA_ERROR_INVALID_HANDLE );
Moran Peker34550092018-11-07 16:19:34 +02001265
1266exit:
1267 mbedtls_free( exported );
1268 mbedtls_psa_crypto_free( );
1269}
1270/* END_CASE */
1271
1272/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001273void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001274 int type_arg,
1275 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001276 int export_size_delta,
1277 int expected_export_status_arg,
1278 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001279{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001280 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001281 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001282 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001283 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001284 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001285 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001286 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001287 size_t exported_length = INVALID_EXPORT_LENGTH;
Jaeden Amero70261c52019-01-04 11:47:20 +00001288 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001289
Gilles Peskine8817f612018-12-18 00:18:46 +01001290 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001291
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001292 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001293 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001294 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001295
1296 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001297 PSA_ASSERT( psa_import_key( handle, type,
1298 data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001299
Gilles Peskine49c25912018-10-29 15:15:31 +01001300 /* Export the public key */
1301 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001302 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001303 exported, export_size,
1304 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001305 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001306 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001307 {
1308 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1309 size_t bits;
Gilles Peskine8817f612018-12-18 00:18:46 +01001310 PSA_ASSERT( psa_get_key_information( handle, NULL, &bits ) );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001311 TEST_ASSERT( expected_public_key->len <=
1312 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001313 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1314 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001315 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001316
1317exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001318 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001319 psa_destroy_key( handle );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001320 mbedtls_psa_crypto_free( );
1321}
1322/* END_CASE */
1323
Gilles Peskine20035e32018-02-03 22:44:14 +01001324/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001325void import_and_exercise_key( data_t *data,
1326 int type_arg,
1327 int bits_arg,
1328 int alg_arg )
1329{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001330 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001331 psa_key_type_t type = type_arg;
1332 size_t bits = bits_arg;
1333 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001334 psa_key_usage_t usage = usage_to_exercise( type, alg );
Jaeden Amero70261c52019-01-04 11:47:20 +00001335 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001336 psa_key_type_t got_type;
1337 size_t got_bits;
1338 psa_status_t status;
1339
Gilles Peskine8817f612018-12-18 00:18:46 +01001340 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001341
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001342 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001343 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001344 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001345
1346 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001347 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01001348 PSA_ASSERT( status );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001349
1350 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001351 PSA_ASSERT( psa_get_key_information( handle,
1352 &got_type,
1353 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001354 TEST_EQUAL( got_type, type );
1355 TEST_EQUAL( got_bits, bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001356
1357 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001358 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001359 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001360
1361exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001362 psa_destroy_key( handle );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001363 mbedtls_psa_crypto_free( );
1364}
1365/* END_CASE */
1366
1367/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001368void key_policy( int usage_arg, int alg_arg )
1369{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001370 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001371 psa_algorithm_t alg = alg_arg;
1372 psa_key_usage_t usage = usage_arg;
1373 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1374 unsigned char key[32] = {0};
Jaeden Amero70261c52019-01-04 11:47:20 +00001375 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
1376 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001377
1378 memset( key, 0x2a, sizeof( key ) );
1379
Gilles Peskine8817f612018-12-18 00:18:46 +01001380 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001381
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001382 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001383 psa_key_policy_set_usage( &policy_set, usage, alg );
1384
Gilles Peskinefe11b722018-12-18 00:24:04 +01001385 TEST_EQUAL( psa_key_policy_get_usage( &policy_set ), usage );
1386 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_set ), alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001387 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001388
Gilles Peskine8817f612018-12-18 00:18:46 +01001389 PSA_ASSERT( psa_import_key( handle, key_type,
1390 key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001391
Gilles Peskine8817f612018-12-18 00:18:46 +01001392 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001393
Gilles Peskinefe11b722018-12-18 00:24:04 +01001394 TEST_EQUAL( policy_get.usage, policy_set.usage );
1395 TEST_EQUAL( policy_get.alg, policy_set.alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001396
1397exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001398 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001399 mbedtls_psa_crypto_free( );
1400}
1401/* END_CASE */
1402
1403/* BEGIN_CASE */
Jaeden Amero70261c52019-01-04 11:47:20 +00001404void key_policy_init( )
1405{
1406 /* Test each valid way of initializing the object, except for `= {0}`, as
1407 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1408 * though it's OK by the C standard. We could test for this, but we'd need
1409 * to supress the Clang warning for the test. */
1410 psa_key_policy_t func = psa_key_policy_init( );
1411 psa_key_policy_t init = PSA_KEY_POLICY_INIT;
1412 psa_key_policy_t zero;
1413
1414 memset( &zero, 0, sizeof( zero ) );
1415
1416 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1417 * specification, we test that all valid ways of initializing the object
1418 * have the same bit pattern. This is a stronger requirement that may not
1419 * be valid on all platforms or PSA Crypto implementations, but implies the
1420 * weaker actual requirement is met: that a freshly initialized object, no
1421 * matter how it was initialized, acts the same as any other valid
1422 * initialization. */
1423 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1424 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1425}
1426/* END_CASE */
1427
1428/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001429void mac_key_policy( int policy_usage,
1430 int policy_alg,
1431 int key_type,
1432 data_t *key_data,
1433 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001434{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001435 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001436 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001437 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001438 psa_status_t status;
1439 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001440
Gilles Peskine8817f612018-12-18 00:18:46 +01001441 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001442
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001443 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001444 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001445 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001446
Gilles Peskine8817f612018-12-18 00:18:46 +01001447 PSA_ASSERT( psa_import_key( handle, key_type,
1448 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001449
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001450 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001451 if( policy_alg == exercise_alg &&
1452 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001453 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001454 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001455 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001456 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001457
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001458 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001459 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001460 if( policy_alg == exercise_alg &&
1461 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001462 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001463 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001464 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001465
1466exit:
1467 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001468 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001469 mbedtls_psa_crypto_free( );
1470}
1471/* END_CASE */
1472
1473/* BEGIN_CASE */
1474void cipher_key_policy( int policy_usage,
1475 int policy_alg,
1476 int key_type,
1477 data_t *key_data,
1478 int exercise_alg )
1479{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001480 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001481 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001482 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001483 psa_status_t status;
1484
Gilles Peskine8817f612018-12-18 00:18:46 +01001485 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001486
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001487 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001488 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001489 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001490
Gilles Peskine8817f612018-12-18 00:18:46 +01001491 PSA_ASSERT( psa_import_key( handle, key_type,
1492 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001493
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001494 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001495 if( policy_alg == exercise_alg &&
1496 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001497 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001498 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001499 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001500 psa_cipher_abort( &operation );
1501
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001502 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001503 if( policy_alg == exercise_alg &&
1504 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001505 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001506 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001507 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001508
1509exit:
1510 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001511 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001512 mbedtls_psa_crypto_free( );
1513}
1514/* END_CASE */
1515
1516/* BEGIN_CASE */
1517void aead_key_policy( int policy_usage,
1518 int policy_alg,
1519 int key_type,
1520 data_t *key_data,
1521 int nonce_length_arg,
1522 int tag_length_arg,
1523 int exercise_alg )
1524{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001525 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001526 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001527 psa_status_t status;
1528 unsigned char nonce[16] = {0};
1529 size_t nonce_length = nonce_length_arg;
1530 unsigned char tag[16];
1531 size_t tag_length = tag_length_arg;
1532 size_t output_length;
1533
1534 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1535 TEST_ASSERT( tag_length <= sizeof( tag ) );
1536
Gilles Peskine8817f612018-12-18 00:18:46 +01001537 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001538
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001539 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001540 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001541 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001542
Gilles Peskine8817f612018-12-18 00:18:46 +01001543 PSA_ASSERT( psa_import_key( handle, key_type,
1544 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001545
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001546 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001547 nonce, nonce_length,
1548 NULL, 0,
1549 NULL, 0,
1550 tag, tag_length,
1551 &output_length );
1552 if( policy_alg == exercise_alg &&
1553 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001554 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001555 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001556 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001557
1558 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001559 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001560 nonce, nonce_length,
1561 NULL, 0,
1562 tag, tag_length,
1563 NULL, 0,
1564 &output_length );
1565 if( policy_alg == exercise_alg &&
1566 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001567 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001568 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001569 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001570
1571exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001572 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001573 mbedtls_psa_crypto_free( );
1574}
1575/* END_CASE */
1576
1577/* BEGIN_CASE */
1578void asymmetric_encryption_key_policy( int policy_usage,
1579 int policy_alg,
1580 int key_type,
1581 data_t *key_data,
1582 int exercise_alg )
1583{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001584 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001585 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001586 psa_status_t status;
1587 size_t key_bits;
1588 size_t buffer_length;
1589 unsigned char *buffer = NULL;
1590 size_t output_length;
1591
Gilles Peskine8817f612018-12-18 00:18:46 +01001592 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001593
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001594 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001595 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001596 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001597
Gilles Peskine8817f612018-12-18 00:18:46 +01001598 PSA_ASSERT( psa_import_key( handle, key_type,
1599 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001600
Gilles Peskine8817f612018-12-18 00:18:46 +01001601 PSA_ASSERT( psa_get_key_information( handle,
1602 NULL,
1603 &key_bits ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001604 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1605 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001606 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001607
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001608 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001609 NULL, 0,
1610 NULL, 0,
1611 buffer, buffer_length,
1612 &output_length );
1613 if( policy_alg == exercise_alg &&
1614 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001615 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001616 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001617 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001618
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001619 if( buffer_length != 0 )
1620 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001621 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001622 buffer, buffer_length,
1623 NULL, 0,
1624 buffer, buffer_length,
1625 &output_length );
1626 if( policy_alg == exercise_alg &&
1627 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001628 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001629 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001630 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001631
1632exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001633 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001634 mbedtls_psa_crypto_free( );
1635 mbedtls_free( buffer );
1636}
1637/* END_CASE */
1638
1639/* BEGIN_CASE */
1640void asymmetric_signature_key_policy( int policy_usage,
1641 int policy_alg,
1642 int key_type,
1643 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001644 int exercise_alg,
1645 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001646{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001647 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001648 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001649 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001650 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1651 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1652 * compatible with the policy and `payload_length_arg` is supposed to be
1653 * a valid input length to sign. If `payload_length_arg <= 0`,
1654 * `exercise_alg` is supposed to be forbidden by the policy. */
1655 int compatible_alg = payload_length_arg > 0;
1656 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001657 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1658 size_t signature_length;
1659
Gilles Peskine8817f612018-12-18 00:18:46 +01001660 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001661
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001662 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001663 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001664 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001665
Gilles Peskine8817f612018-12-18 00:18:46 +01001666 PSA_ASSERT( psa_import_key( handle, key_type,
1667 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001668
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001669 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001670 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001671 signature, sizeof( signature ),
1672 &signature_length );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001673 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001674 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001675 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001676 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001677
1678 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001679 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001680 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001681 signature, sizeof( signature ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001682 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001683 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001684 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001685 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001686
1687exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001688 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001689 mbedtls_psa_crypto_free( );
1690}
1691/* END_CASE */
1692
1693/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001694void derive_key_policy( int policy_usage,
1695 int policy_alg,
1696 int key_type,
1697 data_t *key_data,
1698 int exercise_alg )
1699{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001700 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001701 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001702 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1703 psa_status_t status;
1704
Gilles Peskine8817f612018-12-18 00:18:46 +01001705 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001706
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001707 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001708 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001709 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001710
Gilles Peskine8817f612018-12-18 00:18:46 +01001711 PSA_ASSERT( psa_import_key( handle, key_type,
1712 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001713
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001714 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001715 exercise_alg,
1716 NULL, 0,
1717 NULL, 0,
1718 1 );
1719 if( policy_alg == exercise_alg &&
1720 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001721 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001722 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001723 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001724
1725exit:
1726 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001727 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001728 mbedtls_psa_crypto_free( );
1729}
1730/* END_CASE */
1731
1732/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001733void agreement_key_policy( int policy_usage,
1734 int policy_alg,
1735 int key_type_arg,
1736 data_t *key_data,
1737 int exercise_alg )
1738{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001739 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001740 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001741 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001742 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1743 psa_status_t status;
1744
Gilles Peskine8817f612018-12-18 00:18:46 +01001745 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001746
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001747 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001748 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001749 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001750
Gilles Peskine8817f612018-12-18 00:18:46 +01001751 PSA_ASSERT( psa_import_key( handle, key_type,
1752 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001753
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001754 status = key_agreement_with_self( &generator, handle, exercise_alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001755
Gilles Peskine01d718c2018-09-18 12:01:02 +02001756 if( policy_alg == exercise_alg &&
1757 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001758 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001759 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001760 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001761
1762exit:
1763 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001764 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001765 mbedtls_psa_crypto_free( );
1766}
1767/* END_CASE */
1768
1769/* BEGIN_CASE */
Gilles Peskine57ab7212019-01-28 13:03:09 +01001770void copy_key_policy( int source_usage_arg, int source_alg_arg,
1771 int type_arg, data_t *material,
1772 int target_usage_arg, int target_alg_arg,
1773 int constraint_usage_arg, int constraint_alg_arg,
1774 int expected_usage_arg, int expected_alg_arg )
1775{
1776 psa_key_usage_t source_usage = source_usage_arg;
1777 psa_algorithm_t source_alg = source_alg_arg;
1778 psa_key_handle_t source_handle = 0;
1779 psa_key_policy_t source_policy = PSA_KEY_POLICY_INIT;
1780 psa_key_type_t source_type = type_arg;
1781 size_t source_bits;
1782 psa_key_usage_t target_usage = target_usage_arg;
1783 psa_algorithm_t target_alg = target_alg_arg;
1784 psa_key_handle_t target_handle = 0;
1785 psa_key_policy_t target_policy = PSA_KEY_POLICY_INIT;
1786 psa_key_type_t target_type;
1787 size_t target_bits;
1788 psa_key_usage_t constraint_usage = constraint_usage_arg;
1789 psa_algorithm_t constraint_alg = constraint_alg_arg;
1790 psa_key_policy_t constraint = PSA_KEY_POLICY_INIT;
1791 psa_key_policy_t *p_constraint = NULL;
1792 psa_key_usage_t expected_usage = expected_usage_arg;
1793 psa_algorithm_t expected_alg = expected_alg_arg;
1794 uint8_t *export_buffer = NULL;
1795
1796 if( constraint_usage_arg != -1 )
1797 {
1798 p_constraint = &constraint;
1799 psa_key_policy_set_usage( p_constraint,
1800 constraint_usage, constraint_alg );
1801 }
1802
1803 PSA_ASSERT( psa_crypto_init( ) );
1804
1805 /* Populate the source slot. */
1806 PSA_ASSERT( psa_allocate_key( &source_handle ) );
1807 psa_key_policy_set_usage( &source_policy, source_usage, source_alg );
1808 PSA_ASSERT( psa_set_key_policy( source_handle, &source_policy ) );
1809 PSA_ASSERT( psa_import_key( source_handle, source_type,
1810 material->x, material->len ) );
1811 PSA_ASSERT( psa_get_key_information( source_handle, NULL, &source_bits ) );
1812
1813 /* Prepare the target slot. */
1814 PSA_ASSERT( psa_allocate_key( &target_handle ) );
1815 psa_key_policy_set_usage( &target_policy, target_usage, target_alg );
1816 PSA_ASSERT( psa_set_key_policy( target_handle, &target_policy ) );
1817 target_policy = psa_key_policy_init();
1818
1819 /* Copy the key. */
1820 PSA_ASSERT( psa_copy_key( source_handle, target_handle, p_constraint ) );
1821
1822 /* Destroy the source to ensure that this doesn't affect the target. */
1823 PSA_ASSERT( psa_destroy_key( source_handle ) );
1824
1825 /* Test that the target slot has the expected content and policy. */
1826 PSA_ASSERT( psa_get_key_information( target_handle,
1827 &target_type, &target_bits ) );
1828 TEST_EQUAL( source_type, target_type );
1829 TEST_EQUAL( source_bits, target_bits );
1830 PSA_ASSERT( psa_get_key_policy( target_handle, &target_policy ) );
1831 TEST_EQUAL( expected_usage, psa_key_policy_get_usage( &target_policy ) );
1832 TEST_EQUAL( expected_alg, psa_key_policy_get_algorithm( &target_policy ) );
1833 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1834 {
1835 size_t length;
1836 ASSERT_ALLOC( export_buffer, material->len );
1837 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
1838 material->len, &length ) );
1839 ASSERT_COMPARE( material->x, material->len,
1840 export_buffer, length );
1841 }
1842 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
1843 goto exit;
1844
1845 PSA_ASSERT( psa_close_key( target_handle ) );
1846
1847exit:
1848 mbedtls_psa_crypto_free( );
1849 mbedtls_free( export_buffer );
1850}
1851/* END_CASE */
1852
1853/* BEGIN_CASE */
1854void copy_fail( int source_usage_arg, int source_alg_arg,
1855 int type_arg, data_t *material,
1856 int target_usage_arg, int target_alg_arg,
1857 int constraint_usage_arg, int constraint_alg_arg,
1858 int expected_status_arg )
1859{
1860 /* Test copy failure into an empty slot. There is a test for copy failure
1861 * into an occupied slot in
1862 * test_suite_psa_crypto_slot_management.function. */
1863
1864 psa_key_usage_t source_usage = source_usage_arg;
1865 psa_algorithm_t source_alg = source_alg_arg;
1866 psa_key_handle_t source_handle = 0;
1867 psa_key_policy_t source_policy = PSA_KEY_POLICY_INIT;
1868 psa_key_type_t source_type = type_arg;
1869 size_t source_bits;
1870 psa_key_usage_t target_usage = target_usage_arg;
1871 psa_algorithm_t target_alg = target_alg_arg;
1872 psa_key_handle_t target_handle = 0;
1873 psa_key_policy_t target_policy = PSA_KEY_POLICY_INIT;
1874 psa_key_type_t target_type;
1875 size_t target_bits;
1876 psa_key_usage_t constraint_usage = constraint_usage_arg;
1877 psa_algorithm_t constraint_alg = constraint_alg_arg;
1878 psa_key_policy_t constraint = PSA_KEY_POLICY_INIT;
1879 psa_key_policy_t *p_constraint = NULL;
1880 psa_status_t expected_status = expected_status_arg;
1881
1882 if( constraint_usage_arg != -1 )
1883 {
1884 p_constraint = &constraint;
1885 psa_key_policy_set_usage( p_constraint,
1886 constraint_usage, constraint_alg );
1887 }
1888
1889 PSA_ASSERT( psa_crypto_init( ) );
1890
1891 /* Populate the source slot. */
1892 PSA_ASSERT( psa_allocate_key( &source_handle ) );
1893 psa_key_policy_set_usage( &source_policy, source_usage, source_alg );
1894 PSA_ASSERT( psa_set_key_policy( source_handle, &source_policy ) );
1895 PSA_ASSERT( psa_import_key( source_handle, source_type,
1896 material->x, material->len ) );
1897 PSA_ASSERT( psa_get_key_information( source_handle, NULL, &source_bits ) );
1898
1899 /* Prepare the target slot. */
1900 PSA_ASSERT( psa_allocate_key( &target_handle ) );
1901 psa_key_policy_set_usage( &target_policy, target_usage, target_alg );
1902 PSA_ASSERT( psa_set_key_policy( target_handle, &target_policy ) );
1903 target_policy = psa_key_policy_init();
1904
1905 /* Copy the key. */
1906 TEST_EQUAL( psa_copy_key( source_handle, target_handle, p_constraint ),
1907 expected_status );
1908
1909 /* Test that the target slot is unaffected. */
1910 TEST_EQUAL( psa_get_key_information( target_handle,
1911 &target_type, &target_bits ),
1912 PSA_ERROR_EMPTY_SLOT );
1913 PSA_ASSERT( psa_get_key_policy( target_handle, &target_policy ) );
1914 TEST_EQUAL( target_usage, psa_key_policy_get_usage( &target_policy ) );
1915 TEST_EQUAL( target_alg, psa_key_policy_get_algorithm( &target_policy ) );
1916
1917exit:
1918 mbedtls_psa_crypto_free( );
1919}
1920/* END_CASE */
1921
1922/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001923void hash_operation_init( )
1924{
1925 /* Test each valid way of initializing the object, except for `= {0}`, as
1926 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1927 * though it's OK by the C standard. We could test for this, but we'd need
1928 * to supress the Clang warning for the test. */
1929 psa_hash_operation_t func = psa_hash_operation_init( );
1930 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1931 psa_hash_operation_t zero;
1932
1933 memset( &zero, 0, sizeof( zero ) );
1934
1935 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1936 * specification, we test that all valid ways of initializing the object
1937 * have the same bit pattern. This is a stronger requirement that may not
1938 * be valid on all platforms or PSA Crypto implementations, but implies the
1939 * weaker actual requirement is met: that a freshly initialized object, no
1940 * matter how it was initialized, acts the same as any other valid
1941 * initialization. */
1942 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1943 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1944}
1945/* END_CASE */
1946
1947/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001948void hash_setup( int alg_arg,
1949 int expected_status_arg )
1950{
1951 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001952 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001953 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001954 psa_status_t status;
1955
Gilles Peskine8817f612018-12-18 00:18:46 +01001956 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001957
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001958 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001959 psa_hash_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001960 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001961
1962exit:
1963 mbedtls_psa_crypto_free( );
1964}
1965/* END_CASE */
1966
1967/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02001968void hash_bad_order( )
1969{
1970 unsigned char input[] = "";
1971 /* SHA-256 hash of an empty string */
1972 unsigned char hash[] = {
1973 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1974 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1975 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
1976 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001977 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001978
Gilles Peskine8817f612018-12-18 00:18:46 +01001979 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001980
1981 /* psa_hash_update without calling psa_hash_setup beforehand */
1982 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001983 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001984 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001985
1986 /* psa_hash_verify without calling psa_hash_setup beforehand */
1987 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001988 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001989 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001990
1991 /* psa_hash_finish without calling psa_hash_setup beforehand */
1992 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001993 TEST_EQUAL( psa_hash_finish( &operation,
1994 hash, sizeof( hash ), &hash_len ),
1995 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001996
1997exit:
1998 mbedtls_psa_crypto_free( );
1999}
2000/* END_CASE */
2001
itayzafrir27e69452018-11-01 14:26:34 +02002002/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2003void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002004{
2005 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002006 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2007 * appended to it */
2008 unsigned char hash[] = {
2009 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2010 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2011 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002012 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002013 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002014
Gilles Peskine8817f612018-12-18 00:18:46 +01002015 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002016
itayzafrir27e69452018-11-01 14:26:34 +02002017 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002018 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002019 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002020 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002021
itayzafrir27e69452018-11-01 14:26:34 +02002022 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002023 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002024 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002025 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002026
itayzafrir27e69452018-11-01 14:26:34 +02002027 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002028 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002029 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002030 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002031
itayzafrirec93d302018-10-18 18:01:10 +03002032exit:
2033 mbedtls_psa_crypto_free( );
2034}
2035/* END_CASE */
2036
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002037/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2038void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002039{
2040 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002041 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002042 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002043 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002044 size_t hash_len;
2045
Gilles Peskine8817f612018-12-18 00:18:46 +01002046 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002047
itayzafrir58028322018-10-25 10:22:01 +03002048 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002049 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002050 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002051 hash, expected_size - 1, &hash_len ),
2052 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002053
2054exit:
2055 mbedtls_psa_crypto_free( );
2056}
2057/* END_CASE */
2058
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002059/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2060void hash_clone_source_state( )
2061{
2062 psa_algorithm_t alg = PSA_ALG_SHA_256;
2063 unsigned char hash[PSA_HASH_MAX_SIZE];
2064 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2065 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2066 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2067 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2068 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2069 size_t hash_len;
2070
2071 PSA_ASSERT( psa_crypto_init( ) );
2072 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2073
2074 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2075 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2076 PSA_ASSERT( psa_hash_finish( &op_finished,
2077 hash, sizeof( hash ), &hash_len ) );
2078 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2079 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2080
2081 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2082 PSA_ERROR_BAD_STATE );
2083
2084 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2085 PSA_ASSERT( psa_hash_finish( &op_init,
2086 hash, sizeof( hash ), &hash_len ) );
2087 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2088 PSA_ASSERT( psa_hash_finish( &op_finished,
2089 hash, sizeof( hash ), &hash_len ) );
2090 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2091 PSA_ASSERT( psa_hash_finish( &op_aborted,
2092 hash, sizeof( hash ), &hash_len ) );
2093
2094exit:
2095 psa_hash_abort( &op_source );
2096 psa_hash_abort( &op_init );
2097 psa_hash_abort( &op_setup );
2098 psa_hash_abort( &op_finished );
2099 psa_hash_abort( &op_aborted );
2100 mbedtls_psa_crypto_free( );
2101}
2102/* END_CASE */
2103
2104/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2105void hash_clone_target_state( )
2106{
2107 psa_algorithm_t alg = PSA_ALG_SHA_256;
2108 unsigned char hash[PSA_HASH_MAX_SIZE];
2109 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2110 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2111 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2112 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2113 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2114 size_t hash_len;
2115
2116 PSA_ASSERT( psa_crypto_init( ) );
2117
2118 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2119 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2120 PSA_ASSERT( psa_hash_finish( &op_finished,
2121 hash, sizeof( hash ), &hash_len ) );
2122 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2123 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2124
2125 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2126 PSA_ASSERT( psa_hash_finish( &op_target,
2127 hash, sizeof( hash ), &hash_len ) );
2128
2129 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2130 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2131 PSA_ERROR_BAD_STATE );
2132 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2133 PSA_ERROR_BAD_STATE );
2134
2135exit:
2136 psa_hash_abort( &op_target );
2137 psa_hash_abort( &op_init );
2138 psa_hash_abort( &op_setup );
2139 psa_hash_abort( &op_finished );
2140 psa_hash_abort( &op_aborted );
2141 mbedtls_psa_crypto_free( );
2142}
2143/* END_CASE */
2144
itayzafrir58028322018-10-25 10:22:01 +03002145/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002146void mac_operation_init( )
2147{
2148 /* Test each valid way of initializing the object, except for `= {0}`, as
2149 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2150 * though it's OK by the C standard. We could test for this, but we'd need
2151 * to supress the Clang warning for the test. */
2152 psa_mac_operation_t func = psa_mac_operation_init( );
2153 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2154 psa_mac_operation_t zero;
2155
2156 memset( &zero, 0, sizeof( zero ) );
2157
2158 /* Although not technically guaranteed by the C standard nor the PSA Crypto
2159 * specification, we test that all valid ways of initializing the object
2160 * have the same bit pattern. This is a stronger requirement that may not
2161 * be valid on all platforms or PSA Crypto implementations, but implies the
2162 * weaker actual requirement is met: that a freshly initialized object, no
2163 * matter how it was initialized, acts the same as any other valid
2164 * initialization. */
2165 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
2166 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
2167}
2168/* END_CASE */
2169
2170/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002171void mac_setup( int key_type_arg,
2172 data_t *key,
2173 int alg_arg,
2174 int expected_status_arg )
2175{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002176 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002177 psa_key_type_t key_type = key_type_arg;
2178 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002179 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002180 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002181 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002182 psa_status_t status;
2183
Gilles Peskine8817f612018-12-18 00:18:46 +01002184 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002185
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002186 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002187 psa_key_policy_set_usage( &policy,
2188 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2189 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002190 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002191
Gilles Peskine8817f612018-12-18 00:18:46 +01002192 PSA_ASSERT( psa_import_key( handle, key_type,
2193 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002194
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002195 status = psa_mac_sign_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002196 psa_mac_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002197 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002198
2199exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002200 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002201 mbedtls_psa_crypto_free( );
2202}
2203/* END_CASE */
2204
2205/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002206void mac_sign( int key_type_arg,
2207 data_t *key,
2208 int alg_arg,
2209 data_t *input,
2210 data_t *expected_mac )
2211{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002212 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002213 psa_key_type_t key_type = key_type_arg;
2214 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002215 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002216 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002217 /* Leave a little extra room in the output buffer. At the end of the
2218 * test, we'll check that the implementation didn't overwrite onto
2219 * this extra room. */
2220 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2221 size_t mac_buffer_size =
2222 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2223 size_t mac_length = 0;
2224
2225 memset( actual_mac, '+', sizeof( actual_mac ) );
2226 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2227 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2228
Gilles Peskine8817f612018-12-18 00:18:46 +01002229 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002230
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002231 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002232 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002233 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002234
Gilles Peskine8817f612018-12-18 00:18:46 +01002235 PSA_ASSERT( psa_import_key( handle, key_type,
2236 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002237
2238 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002239 PSA_ASSERT( psa_mac_sign_setup( &operation,
2240 handle, alg ) );
2241 PSA_ASSERT( psa_mac_update( &operation,
2242 input->x, input->len ) );
2243 PSA_ASSERT( psa_mac_sign_finish( &operation,
2244 actual_mac, mac_buffer_size,
2245 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002246
2247 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002248 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2249 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002250
2251 /* Verify that the end of the buffer is untouched. */
2252 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2253 sizeof( actual_mac ) - mac_length ) );
2254
2255exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002256 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002257 mbedtls_psa_crypto_free( );
2258}
2259/* END_CASE */
2260
2261/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002262void mac_verify( int key_type_arg,
2263 data_t *key,
2264 int alg_arg,
2265 data_t *input,
2266 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002267{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002268 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002269 psa_key_type_t key_type = key_type_arg;
2270 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002271 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002272 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002273
Gilles Peskine69c12672018-06-28 00:07:19 +02002274 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2275
Gilles Peskine8817f612018-12-18 00:18:46 +01002276 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002277
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002278 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002279 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002280 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad16036df908f2018-04-02 08:34:15 -07002281
Gilles Peskine8817f612018-12-18 00:18:46 +01002282 PSA_ASSERT( psa_import_key( handle, key_type,
2283 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002284
Gilles Peskine8817f612018-12-18 00:18:46 +01002285 PSA_ASSERT( psa_mac_verify_setup( &operation,
2286 handle, alg ) );
2287 PSA_ASSERT( psa_destroy_key( handle ) );
2288 PSA_ASSERT( psa_mac_update( &operation,
2289 input->x, input->len ) );
2290 PSA_ASSERT( psa_mac_verify_finish( &operation,
2291 expected_mac->x,
2292 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002293
2294exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002295 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002296 mbedtls_psa_crypto_free( );
2297}
2298/* END_CASE */
2299
2300/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002301void cipher_operation_init( )
2302{
2303 /* Test each valid way of initializing the object, except for `= {0}`, as
2304 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2305 * though it's OK by the C standard. We could test for this, but we'd need
2306 * to supress the Clang warning for the test. */
2307 psa_cipher_operation_t func = psa_cipher_operation_init( );
2308 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2309 psa_cipher_operation_t zero;
2310
2311 memset( &zero, 0, sizeof( zero ) );
2312
2313 /* Although not technically guaranteed by the C standard nor the PSA Crypto
2314 * specification, we test that all valid ways of initializing the object
2315 * have the same bit pattern. This is a stronger requirement that may not
2316 * be valid on all platforms or PSA Crypto implementations, but implies the
2317 * weaker actual requirement is met: that a freshly initialized object, no
2318 * matter how it was initialized, acts the same as any other valid
2319 * initialization. */
2320 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
2321 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
2322}
2323/* END_CASE */
2324
2325/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002326void cipher_setup( int key_type_arg,
2327 data_t *key,
2328 int alg_arg,
2329 int expected_status_arg )
2330{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002331 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002332 psa_key_type_t key_type = key_type_arg;
2333 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002334 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002335 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002336 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002337 psa_status_t status;
2338
Gilles Peskine8817f612018-12-18 00:18:46 +01002339 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002340
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002341 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002342 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002343 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002344
Gilles Peskine8817f612018-12-18 00:18:46 +01002345 PSA_ASSERT( psa_import_key( handle, key_type,
2346 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002347
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002348 status = psa_cipher_encrypt_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002349 psa_cipher_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002350 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002351
2352exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002353 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002354 mbedtls_psa_crypto_free( );
2355}
2356/* END_CASE */
2357
2358/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002359void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002360 data_t *key,
2361 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002362 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002363{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002364 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002365 psa_status_t status;
2366 psa_key_type_t key_type = key_type_arg;
2367 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002368 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002369 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002370 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002371 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002372 size_t output_buffer_size = 0;
2373 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002374 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002375 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002376 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002377
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002378 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2379 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002380
Gilles Peskine8817f612018-12-18 00:18:46 +01002381 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002382
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002383 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002384 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002385 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002386
Gilles Peskine8817f612018-12-18 00:18:46 +01002387 PSA_ASSERT( psa_import_key( handle, key_type,
2388 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002389
Gilles Peskine8817f612018-12-18 00:18:46 +01002390 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2391 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002392
Gilles Peskine8817f612018-12-18 00:18:46 +01002393 PSA_ASSERT( psa_cipher_set_iv( &operation,
2394 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002395 output_buffer_size = ( (size_t) input->len +
2396 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002397 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002398
Gilles Peskine8817f612018-12-18 00:18:46 +01002399 PSA_ASSERT( psa_cipher_update( &operation,
2400 input->x, input->len,
2401 output, output_buffer_size,
2402 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002403 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002404 status = psa_cipher_finish( &operation,
2405 output + function_output_length,
2406 output_buffer_size,
2407 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002408 total_output_length += function_output_length;
2409
Gilles Peskinefe11b722018-12-18 00:24:04 +01002410 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002411 if( expected_status == PSA_SUCCESS )
2412 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002413 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002414 ASSERT_COMPARE( expected_output->x, expected_output->len,
2415 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002416 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002417
Gilles Peskine50e586b2018-06-08 14:28:46 +02002418exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002419 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002420 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002421 mbedtls_psa_crypto_free( );
2422}
2423/* END_CASE */
2424
2425/* BEGIN_CASE */
2426void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002427 data_t *key,
2428 data_t *input,
2429 int first_part_size,
2430 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002431{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002432 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002433 psa_key_type_t key_type = key_type_arg;
2434 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002435 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002436 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002437 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002438 size_t output_buffer_size = 0;
2439 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002440 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002441 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002442 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002443
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002444 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2445 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002446
Gilles Peskine8817f612018-12-18 00:18:46 +01002447 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002448
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002449 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002450 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002451 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002452
Gilles Peskine8817f612018-12-18 00:18:46 +01002453 PSA_ASSERT( psa_import_key( handle, key_type,
2454 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002455
Gilles Peskine8817f612018-12-18 00:18:46 +01002456 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2457 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002458
Gilles Peskine8817f612018-12-18 00:18:46 +01002459 PSA_ASSERT( psa_cipher_set_iv( &operation,
2460 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002461 output_buffer_size = ( (size_t) input->len +
2462 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002463 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002464
Gilles Peskine4abf7412018-06-18 16:35:34 +02002465 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002466 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2467 output, output_buffer_size,
2468 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002469 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002470 PSA_ASSERT( psa_cipher_update( &operation,
2471 input->x + first_part_size,
2472 input->len - first_part_size,
2473 output, output_buffer_size,
2474 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002475 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002476 PSA_ASSERT( psa_cipher_finish( &operation,
2477 output + function_output_length,
2478 output_buffer_size,
2479 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002480 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002481 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002482
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002483 ASSERT_COMPARE( expected_output->x, expected_output->len,
2484 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002485
2486exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002487 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002488 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002489 mbedtls_psa_crypto_free( );
2490}
2491/* END_CASE */
2492
2493/* BEGIN_CASE */
2494void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002495 data_t *key,
2496 data_t *input,
2497 int first_part_size,
2498 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002499{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002500 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002501
2502 psa_key_type_t key_type = key_type_arg;
2503 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002504 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002505 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002506 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002507 size_t output_buffer_size = 0;
2508 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002509 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002510 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002511 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002512
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002513 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2514 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002515
Gilles Peskine8817f612018-12-18 00:18:46 +01002516 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002517
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002518 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002519 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002520 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002521
Gilles Peskine8817f612018-12-18 00:18:46 +01002522 PSA_ASSERT( psa_import_key( handle, key_type,
2523 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002524
Gilles Peskine8817f612018-12-18 00:18:46 +01002525 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2526 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002527
Gilles Peskine8817f612018-12-18 00:18:46 +01002528 PSA_ASSERT( psa_cipher_set_iv( &operation,
2529 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002530
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002531 output_buffer_size = ( (size_t) input->len +
2532 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002533 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002534
Gilles Peskine4abf7412018-06-18 16:35:34 +02002535 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002536 PSA_ASSERT( psa_cipher_update( &operation,
2537 input->x, first_part_size,
2538 output, output_buffer_size,
2539 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002540 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002541 PSA_ASSERT( psa_cipher_update( &operation,
2542 input->x + first_part_size,
2543 input->len - first_part_size,
2544 output, output_buffer_size,
2545 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002546 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002547 PSA_ASSERT( psa_cipher_finish( &operation,
2548 output + function_output_length,
2549 output_buffer_size,
2550 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002551 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002552 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002553
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002554 ASSERT_COMPARE( expected_output->x, expected_output->len,
2555 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002556
2557exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002558 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002559 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002560 mbedtls_psa_crypto_free( );
2561}
2562/* END_CASE */
2563
Gilles Peskine50e586b2018-06-08 14:28:46 +02002564/* BEGIN_CASE */
2565void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002566 data_t *key,
2567 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002568 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002569{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002570 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002571 psa_status_t status;
2572 psa_key_type_t key_type = key_type_arg;
2573 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002574 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002575 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002576 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002577 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002578 size_t output_buffer_size = 0;
2579 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002580 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002581 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002582 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002583
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002584 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2585 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002586
Gilles Peskine8817f612018-12-18 00:18:46 +01002587 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002588
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002589 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002590 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002591 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002592
Gilles Peskine8817f612018-12-18 00:18:46 +01002593 PSA_ASSERT( psa_import_key( handle, key_type,
2594 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002595
Gilles Peskine8817f612018-12-18 00:18:46 +01002596 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2597 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002598
Gilles Peskine8817f612018-12-18 00:18:46 +01002599 PSA_ASSERT( psa_cipher_set_iv( &operation,
2600 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002601
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002602 output_buffer_size = ( (size_t) input->len +
2603 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002604 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002605
Gilles Peskine8817f612018-12-18 00:18:46 +01002606 PSA_ASSERT( psa_cipher_update( &operation,
2607 input->x, input->len,
2608 output, output_buffer_size,
2609 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002610 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002611 status = psa_cipher_finish( &operation,
2612 output + function_output_length,
2613 output_buffer_size,
2614 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002615 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002616 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002617
2618 if( expected_status == PSA_SUCCESS )
2619 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002620 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002621 ASSERT_COMPARE( expected_output->x, expected_output->len,
2622 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002623 }
2624
Gilles Peskine50e586b2018-06-08 14:28:46 +02002625exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002626 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002627 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002628 mbedtls_psa_crypto_free( );
2629}
2630/* END_CASE */
2631
Gilles Peskine50e586b2018-06-08 14:28:46 +02002632/* BEGIN_CASE */
2633void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002634 data_t *key,
2635 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002636{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002637 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002638 psa_key_type_t key_type = key_type_arg;
2639 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002640 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002641 size_t iv_size = 16;
2642 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002643 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002644 size_t output1_size = 0;
2645 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002646 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002647 size_t output2_size = 0;
2648 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002649 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002650 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2651 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002652 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002653
Gilles Peskine8817f612018-12-18 00:18:46 +01002654 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002655
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002656 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002657 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002658 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002659
Gilles Peskine8817f612018-12-18 00:18:46 +01002660 PSA_ASSERT( psa_import_key( handle, key_type,
2661 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002662
Gilles Peskine8817f612018-12-18 00:18:46 +01002663 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2664 handle, alg ) );
2665 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2666 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002667
Gilles Peskine8817f612018-12-18 00:18:46 +01002668 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2669 iv, iv_size,
2670 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002671 output1_size = ( (size_t) input->len +
2672 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002673 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002674
Gilles Peskine8817f612018-12-18 00:18:46 +01002675 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2676 output1, output1_size,
2677 &output1_length ) );
2678 PSA_ASSERT( psa_cipher_finish( &operation1,
2679 output1 + output1_length, output1_size,
2680 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002681
Gilles Peskine048b7f02018-06-08 14:20:49 +02002682 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002683
Gilles Peskine8817f612018-12-18 00:18:46 +01002684 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002685
2686 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002687 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002688
Gilles Peskine8817f612018-12-18 00:18:46 +01002689 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2690 iv, iv_length ) );
2691 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2692 output2, output2_size,
2693 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002694 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002695 PSA_ASSERT( psa_cipher_finish( &operation2,
2696 output2 + output2_length,
2697 output2_size,
2698 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002699
Gilles Peskine048b7f02018-06-08 14:20:49 +02002700 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002701
Gilles Peskine8817f612018-12-18 00:18:46 +01002702 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002703
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002704 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002705
2706exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002707 mbedtls_free( output1 );
2708 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002709 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03002710 mbedtls_psa_crypto_free( );
2711}
2712/* END_CASE */
2713
2714/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002715void cipher_verify_output_multipart( int alg_arg,
2716 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002717 data_t *key,
2718 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002719 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002720{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002721 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002722 psa_key_type_t key_type = key_type_arg;
2723 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002724 unsigned char iv[16] = {0};
2725 size_t iv_size = 16;
2726 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002727 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002728 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002729 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002730 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002731 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002732 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002733 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002734 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2735 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002736 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002737
Gilles Peskine8817f612018-12-18 00:18:46 +01002738 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002739
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002740 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002741 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002742 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002743
Gilles Peskine8817f612018-12-18 00:18:46 +01002744 PSA_ASSERT( psa_import_key( handle, key_type,
2745 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03002746
Gilles Peskine8817f612018-12-18 00:18:46 +01002747 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2748 handle, alg ) );
2749 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2750 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002751
Gilles Peskine8817f612018-12-18 00:18:46 +01002752 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2753 iv, iv_size,
2754 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002755 output1_buffer_size = ( (size_t) input->len +
2756 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002757 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002758
Gilles Peskine4abf7412018-06-18 16:35:34 +02002759 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002760
Gilles Peskine8817f612018-12-18 00:18:46 +01002761 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2762 output1, output1_buffer_size,
2763 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002764 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002765
Gilles Peskine8817f612018-12-18 00:18:46 +01002766 PSA_ASSERT( psa_cipher_update( &operation1,
2767 input->x + first_part_size,
2768 input->len - first_part_size,
2769 output1, output1_buffer_size,
2770 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002771 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002772
Gilles Peskine8817f612018-12-18 00:18:46 +01002773 PSA_ASSERT( psa_cipher_finish( &operation1,
2774 output1 + output1_length,
2775 output1_buffer_size - output1_length,
2776 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002777 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002778
Gilles Peskine8817f612018-12-18 00:18:46 +01002779 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002780
Gilles Peskine048b7f02018-06-08 14:20:49 +02002781 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002782 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002783
Gilles Peskine8817f612018-12-18 00:18:46 +01002784 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2785 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002786
Gilles Peskine8817f612018-12-18 00:18:46 +01002787 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2788 output2, output2_buffer_size,
2789 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002790 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002791
Gilles Peskine8817f612018-12-18 00:18:46 +01002792 PSA_ASSERT( psa_cipher_update( &operation2,
2793 output1 + first_part_size,
2794 output1_length - first_part_size,
2795 output2, output2_buffer_size,
2796 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002797 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002798
Gilles Peskine8817f612018-12-18 00:18:46 +01002799 PSA_ASSERT( psa_cipher_finish( &operation2,
2800 output2 + output2_length,
2801 output2_buffer_size - output2_length,
2802 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002803 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002804
Gilles Peskine8817f612018-12-18 00:18:46 +01002805 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002806
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002807 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002808
2809exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002810 mbedtls_free( output1 );
2811 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002812 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002813 mbedtls_psa_crypto_free( );
2814}
2815/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002816
Gilles Peskine20035e32018-02-03 22:44:14 +01002817/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002818void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002819 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002820 data_t *nonce,
2821 data_t *additional_data,
2822 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002823 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002824{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002825 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002826 psa_key_type_t key_type = key_type_arg;
2827 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002828 unsigned char *output_data = NULL;
2829 size_t output_size = 0;
2830 size_t output_length = 0;
2831 unsigned char *output_data2 = NULL;
2832 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002833 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002834 psa_status_t expected_result = expected_result_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002835 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002836
Gilles Peskine4abf7412018-06-18 16:35:34 +02002837 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002838 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002839
Gilles Peskine8817f612018-12-18 00:18:46 +01002840 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002841
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002842 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002843 psa_key_policy_set_usage( &policy,
2844 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2845 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002846 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002847
Gilles Peskine8817f612018-12-18 00:18:46 +01002848 PSA_ASSERT( psa_import_key( handle, key_type,
2849 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002850
Gilles Peskinefe11b722018-12-18 00:24:04 +01002851 TEST_EQUAL( psa_aead_encrypt( handle, alg,
2852 nonce->x, nonce->len,
2853 additional_data->x,
2854 additional_data->len,
2855 input_data->x, input_data->len,
2856 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002857 &output_length ),
2858 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002859
2860 if( PSA_SUCCESS == expected_result )
2861 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002862 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002863
Gilles Peskinefe11b722018-12-18 00:24:04 +01002864 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2865 nonce->x, nonce->len,
2866 additional_data->x,
2867 additional_data->len,
2868 output_data, output_length,
2869 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002870 &output_length2 ),
2871 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002872
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002873 ASSERT_COMPARE( input_data->x, input_data->len,
2874 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002875 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002876
Gilles Peskinea1cac842018-06-11 19:33:02 +02002877exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002878 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002879 mbedtls_free( output_data );
2880 mbedtls_free( output_data2 );
2881 mbedtls_psa_crypto_free( );
2882}
2883/* END_CASE */
2884
2885/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002886void aead_encrypt( int key_type_arg, data_t *key_data,
2887 int alg_arg,
2888 data_t *nonce,
2889 data_t *additional_data,
2890 data_t *input_data,
2891 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002892{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002893 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002894 psa_key_type_t key_type = key_type_arg;
2895 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002896 unsigned char *output_data = NULL;
2897 size_t output_size = 0;
2898 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002899 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002900 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002901
Gilles Peskine4abf7412018-06-18 16:35:34 +02002902 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002903 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002904
Gilles Peskine8817f612018-12-18 00:18:46 +01002905 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002906
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002907 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002908 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002909 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002910
Gilles Peskine8817f612018-12-18 00:18:46 +01002911 PSA_ASSERT( psa_import_key( handle, key_type,
2912 key_data->x,
2913 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002914
Gilles Peskine8817f612018-12-18 00:18:46 +01002915 PSA_ASSERT( psa_aead_encrypt( handle, alg,
2916 nonce->x, nonce->len,
2917 additional_data->x, additional_data->len,
2918 input_data->x, input_data->len,
2919 output_data, output_size,
2920 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002921
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002922 ASSERT_COMPARE( expected_result->x, expected_result->len,
2923 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002924
Gilles Peskinea1cac842018-06-11 19:33:02 +02002925exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002926 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002927 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002928 mbedtls_psa_crypto_free( );
2929}
2930/* END_CASE */
2931
2932/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002933void aead_decrypt( int key_type_arg, data_t *key_data,
2934 int alg_arg,
2935 data_t *nonce,
2936 data_t *additional_data,
2937 data_t *input_data,
2938 data_t *expected_data,
2939 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002940{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002941 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002942 psa_key_type_t key_type = key_type_arg;
2943 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002944 unsigned char *output_data = NULL;
2945 size_t output_size = 0;
2946 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002947 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002948 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002949 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002950
Gilles Peskine4abf7412018-06-18 16:35:34 +02002951 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002952 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002953
Gilles Peskine8817f612018-12-18 00:18:46 +01002954 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002955
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002956 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002957 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002958 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002959
Gilles Peskine8817f612018-12-18 00:18:46 +01002960 PSA_ASSERT( psa_import_key( handle, key_type,
2961 key_data->x,
2962 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002963
Gilles Peskinefe11b722018-12-18 00:24:04 +01002964 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2965 nonce->x, nonce->len,
2966 additional_data->x,
2967 additional_data->len,
2968 input_data->x, input_data->len,
2969 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002970 &output_length ),
2971 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002972
Gilles Peskine2d277862018-06-18 15:41:12 +02002973 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002974 ASSERT_COMPARE( expected_data->x, expected_data->len,
2975 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002976
Gilles Peskinea1cac842018-06-11 19:33:02 +02002977exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002978 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002979 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002980 mbedtls_psa_crypto_free( );
2981}
2982/* END_CASE */
2983
2984/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002985void signature_size( int type_arg,
2986 int bits,
2987 int alg_arg,
2988 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002989{
2990 psa_key_type_t type = type_arg;
2991 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002992 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002993 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002994exit:
2995 ;
2996}
2997/* END_CASE */
2998
2999/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003000void sign_deterministic( int key_type_arg, data_t *key_data,
3001 int alg_arg, data_t *input_data,
3002 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003003{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003004 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003005 psa_key_type_t key_type = key_type_arg;
3006 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003007 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003008 unsigned char *signature = NULL;
3009 size_t signature_size;
3010 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00003011 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003012
Gilles Peskine8817f612018-12-18 00:18:46 +01003013 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003014
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003015 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003016 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003017 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003018
Gilles Peskine8817f612018-12-18 00:18:46 +01003019 PSA_ASSERT( psa_import_key( handle, key_type,
3020 key_data->x,
3021 key_data->len ) );
3022 PSA_ASSERT( psa_get_key_information( handle,
3023 NULL,
3024 &key_bits ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003025
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003026 /* Allocate a buffer which has the size advertized by the
3027 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003028 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3029 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003030 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02003031 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003032 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003033
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003034 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003035 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3036 input_data->x, input_data->len,
3037 signature, signature_size,
3038 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003039 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003040 ASSERT_COMPARE( output_data->x, output_data->len,
3041 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003042
3043exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003044 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01003045 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01003046 mbedtls_psa_crypto_free( );
3047}
3048/* END_CASE */
3049
3050/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003051void sign_fail( int key_type_arg, data_t *key_data,
3052 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003053 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003054{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003055 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003056 psa_key_type_t key_type = key_type_arg;
3057 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003058 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003059 psa_status_t actual_status;
3060 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003061 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003062 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00003063 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003064
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003065 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003066
Gilles Peskine8817f612018-12-18 00:18:46 +01003067 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003068
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003069 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003070 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003071 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003072
Gilles Peskine8817f612018-12-18 00:18:46 +01003073 PSA_ASSERT( psa_import_key( handle, key_type,
3074 key_data->x,
3075 key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003076
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003077 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003078 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01003079 signature, signature_size,
3080 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003081 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003082 /* The value of *signature_length is unspecified on error, but
3083 * whatever it is, it should be less than signature_size, so that
3084 * if the caller tries to read *signature_length bytes without
3085 * checking the error code then they don't overflow a buffer. */
3086 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003087
3088exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003089 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01003090 mbedtls_free( signature );
3091 mbedtls_psa_crypto_free( );
3092}
3093/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003094
3095/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003096void sign_verify( int key_type_arg, data_t *key_data,
3097 int alg_arg, data_t *input_data )
3098{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003099 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02003100 psa_key_type_t key_type = key_type_arg;
3101 psa_algorithm_t alg = alg_arg;
3102 size_t key_bits;
3103 unsigned char *signature = NULL;
3104 size_t signature_size;
3105 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00003106 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003107
Gilles Peskine8817f612018-12-18 00:18:46 +01003108 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003109
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003110 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003111 psa_key_policy_set_usage( &policy,
3112 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
3113 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003114 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003115
Gilles Peskine8817f612018-12-18 00:18:46 +01003116 PSA_ASSERT( psa_import_key( handle, key_type,
3117 key_data->x,
3118 key_data->len ) );
3119 PSA_ASSERT( psa_get_key_information( handle,
3120 NULL,
3121 &key_bits ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003122
3123 /* Allocate a buffer which has the size advertized by the
3124 * library. */
3125 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3126 key_bits, alg );
3127 TEST_ASSERT( signature_size != 0 );
3128 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003129 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003130
3131 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003132 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3133 input_data->x, input_data->len,
3134 signature, signature_size,
3135 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003136 /* Check that the signature length looks sensible. */
3137 TEST_ASSERT( signature_length <= signature_size );
3138 TEST_ASSERT( signature_length > 0 );
3139
3140 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003141 PSA_ASSERT( psa_asymmetric_verify(
3142 handle, alg,
3143 input_data->x, input_data->len,
3144 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003145
3146 if( input_data->len != 0 )
3147 {
3148 /* Flip a bit in the input and verify that the signature is now
3149 * detected as invalid. Flip a bit at the beginning, not at the end,
3150 * because ECDSA may ignore the last few bits of the input. */
3151 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003152 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
3153 input_data->x, input_data->len,
3154 signature, signature_length ),
3155 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003156 }
3157
3158exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003159 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003160 mbedtls_free( signature );
3161 mbedtls_psa_crypto_free( );
3162}
3163/* END_CASE */
3164
3165/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003166void asymmetric_verify( int key_type_arg, data_t *key_data,
3167 int alg_arg, data_t *hash_data,
3168 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003169{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003170 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003171 psa_key_type_t key_type = key_type_arg;
3172 psa_algorithm_t alg = alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003173 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003174
Gilles Peskine69c12672018-06-28 00:07:19 +02003175 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3176
Gilles Peskine8817f612018-12-18 00:18:46 +01003177 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003178
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003179 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003180 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003181 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
itayzafrir5c753392018-05-08 11:18:38 +03003182
Gilles Peskine8817f612018-12-18 00:18:46 +01003183 PSA_ASSERT( psa_import_key( handle, key_type,
3184 key_data->x,
3185 key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003186
Gilles Peskine8817f612018-12-18 00:18:46 +01003187 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3188 hash_data->x, hash_data->len,
3189 signature_data->x,
3190 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003191exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003192 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03003193 mbedtls_psa_crypto_free( );
3194}
3195/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003196
3197/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003198void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3199 int alg_arg, data_t *hash_data,
3200 data_t *signature_data,
3201 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003202{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003203 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003204 psa_key_type_t key_type = key_type_arg;
3205 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003206 psa_status_t actual_status;
3207 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003208 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003209
Gilles Peskine8817f612018-12-18 00:18:46 +01003210 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003211
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003212 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003213 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003214 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003215
Gilles Peskine8817f612018-12-18 00:18:46 +01003216 PSA_ASSERT( psa_import_key( handle, key_type,
3217 key_data->x,
3218 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003219
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003220 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003221 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003222 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003223 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003224
Gilles Peskinefe11b722018-12-18 00:24:04 +01003225 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003226
3227exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003228 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003229 mbedtls_psa_crypto_free( );
3230}
3231/* END_CASE */
3232
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003233/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003234void asymmetric_encrypt( int key_type_arg,
3235 data_t *key_data,
3236 int alg_arg,
3237 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003238 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003239 int expected_output_length_arg,
3240 int expected_status_arg )
3241{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003242 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003243 psa_key_type_t key_type = key_type_arg;
3244 psa_algorithm_t alg = alg_arg;
3245 size_t expected_output_length = expected_output_length_arg;
3246 size_t key_bits;
3247 unsigned char *output = NULL;
3248 size_t output_size;
3249 size_t output_length = ~0;
3250 psa_status_t actual_status;
3251 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003252 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003253
Gilles Peskine8817f612018-12-18 00:18:46 +01003254 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003255
Gilles Peskine656896e2018-06-29 19:12:28 +02003256 /* Import the key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003257 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003258 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003259 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
3260 PSA_ASSERT( psa_import_key( handle, key_type,
3261 key_data->x,
3262 key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003263
3264 /* Determine the maximum output length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003265 PSA_ASSERT( psa_get_key_information( handle,
3266 NULL,
3267 &key_bits ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003268 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003269 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003270
3271 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003272 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003273 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003274 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003275 output, output_size,
3276 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003277 TEST_EQUAL( actual_status, expected_status );
3278 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003279
Gilles Peskine68428122018-06-30 18:42:41 +02003280 /* If the label is empty, the test framework puts a non-null pointer
3281 * in label->x. Test that a null pointer works as well. */
3282 if( label->len == 0 )
3283 {
3284 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003285 if( output_size != 0 )
3286 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003287 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003288 input_data->x, input_data->len,
3289 NULL, label->len,
3290 output, output_size,
3291 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003292 TEST_EQUAL( actual_status, expected_status );
3293 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003294 }
3295
Gilles Peskine656896e2018-06-29 19:12:28 +02003296exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003297 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003298 mbedtls_free( output );
3299 mbedtls_psa_crypto_free( );
3300}
3301/* END_CASE */
3302
3303/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003304void asymmetric_encrypt_decrypt( int key_type_arg,
3305 data_t *key_data,
3306 int alg_arg,
3307 data_t *input_data,
3308 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003309{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003310 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003311 psa_key_type_t key_type = key_type_arg;
3312 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003313 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003314 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003315 size_t output_size;
3316 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003317 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003318 size_t output2_size;
3319 size_t output2_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003320 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003321
Gilles Peskine8817f612018-12-18 00:18:46 +01003322 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003323
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003324 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003325 psa_key_policy_set_usage( &policy,
3326 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003327 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003328 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003329
Gilles Peskine8817f612018-12-18 00:18:46 +01003330 PSA_ASSERT( psa_import_key( handle, key_type,
3331 key_data->x,
3332 key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003333
3334 /* Determine the maximum ciphertext length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003335 PSA_ASSERT( psa_get_key_information( handle,
3336 NULL,
3337 &key_bits ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003338 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003339 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003340 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003341 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003342
Gilles Peskineeebd7382018-06-08 18:11:54 +02003343 /* We test encryption by checking that encrypt-then-decrypt gives back
3344 * the original plaintext because of the non-optional random
3345 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003346 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3347 input_data->x, input_data->len,
3348 label->x, label->len,
3349 output, output_size,
3350 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003351 /* We don't know what ciphertext length to expect, but check that
3352 * it looks sensible. */
3353 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003354
Gilles Peskine8817f612018-12-18 00:18:46 +01003355 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3356 output, output_length,
3357 label->x, label->len,
3358 output2, output2_size,
3359 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003360 ASSERT_COMPARE( input_data->x, input_data->len,
3361 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003362
3363exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003364 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003365 mbedtls_free( output );
3366 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003367 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003368}
3369/* END_CASE */
3370
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003371/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003372void asymmetric_decrypt( int key_type_arg,
3373 data_t *key_data,
3374 int alg_arg,
3375 data_t *input_data,
3376 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003377 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003378{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003379 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003380 psa_key_type_t key_type = key_type_arg;
3381 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003382 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003383 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003384 size_t output_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003385 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003386
Gilles Peskine4abf7412018-06-18 16:35:34 +02003387 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003388 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003389
Gilles Peskine8817f612018-12-18 00:18:46 +01003390 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003391
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003392 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003393 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003394 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003395
Gilles Peskine8817f612018-12-18 00:18:46 +01003396 PSA_ASSERT( psa_import_key( handle, key_type,
3397 key_data->x,
3398 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003399
Gilles Peskine8817f612018-12-18 00:18:46 +01003400 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3401 input_data->x, input_data->len,
3402 label->x, label->len,
3403 output,
3404 output_size,
3405 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003406 ASSERT_COMPARE( expected_data->x, expected_data->len,
3407 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003408
Gilles Peskine68428122018-06-30 18:42:41 +02003409 /* If the label is empty, the test framework puts a non-null pointer
3410 * in label->x. Test that a null pointer works as well. */
3411 if( label->len == 0 )
3412 {
3413 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003414 if( output_size != 0 )
3415 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003416 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3417 input_data->x, input_data->len,
3418 NULL, label->len,
3419 output,
3420 output_size,
3421 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003422 ASSERT_COMPARE( expected_data->x, expected_data->len,
3423 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003424 }
3425
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003426exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003427 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003428 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003429 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003430}
3431/* END_CASE */
3432
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003433/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003434void asymmetric_decrypt_fail( int key_type_arg,
3435 data_t *key_data,
3436 int alg_arg,
3437 data_t *input_data,
3438 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003439 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003440{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003441 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003442 psa_key_type_t key_type = key_type_arg;
3443 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003444 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003445 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003446 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003447 psa_status_t actual_status;
3448 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003449 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003450
Gilles Peskine4abf7412018-06-18 16:35:34 +02003451 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003452 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003453
Gilles Peskine8817f612018-12-18 00:18:46 +01003454 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003455
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003456 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003457 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003458 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003459
Gilles Peskine8817f612018-12-18 00:18:46 +01003460 PSA_ASSERT( psa_import_key( handle, key_type,
3461 key_data->x,
3462 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003463
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003464 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003465 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003466 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003467 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003468 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003469 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003470 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003471
Gilles Peskine68428122018-06-30 18:42:41 +02003472 /* If the label is empty, the test framework puts a non-null pointer
3473 * in label->x. Test that a null pointer works as well. */
3474 if( label->len == 0 )
3475 {
3476 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003477 if( output_size != 0 )
3478 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003479 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003480 input_data->x, input_data->len,
3481 NULL, label->len,
3482 output, output_size,
3483 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003484 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003485 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003486 }
3487
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003488exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003489 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02003490 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003491 mbedtls_psa_crypto_free( );
3492}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003493/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003494
3495/* BEGIN_CASE */
Jaeden Amerod94d6712019-01-04 14:11:48 +00003496void crypto_generator_init( )
3497{
3498 /* Test each valid way of initializing the object, except for `= {0}`, as
3499 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3500 * though it's OK by the C standard. We could test for this, but we'd need
3501 * to supress the Clang warning for the test. */
3502 psa_crypto_generator_t func = psa_crypto_generator_init( );
3503 psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT;
3504 psa_crypto_generator_t zero;
3505
3506 memset( &zero, 0, sizeof( zero ) );
3507
3508 /* Although not technically guaranteed by the C standard nor the PSA Crypto
3509 * specification, we test that all valid ways of initializing the object
3510 * have the same bit pattern. This is a stronger requirement that may not
3511 * be valid on all platforms or PSA Crypto implementations, but implies the
3512 * weaker actual requirement is met: that a freshly initialized object, no
3513 * matter how it was initialized, acts the same as any other valid
3514 * initialization. */
3515 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
3516 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
3517}
3518/* END_CASE */
3519
3520/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003521void derive_setup( int key_type_arg,
3522 data_t *key_data,
3523 int alg_arg,
3524 data_t *salt,
3525 data_t *label,
3526 int requested_capacity_arg,
3527 int expected_status_arg )
3528{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003529 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003530 size_t key_type = key_type_arg;
3531 psa_algorithm_t alg = alg_arg;
3532 size_t requested_capacity = requested_capacity_arg;
3533 psa_status_t expected_status = expected_status_arg;
3534 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003535 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003536
Gilles Peskine8817f612018-12-18 00:18:46 +01003537 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003538
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003539 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003540 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003541 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003542
Gilles Peskine8817f612018-12-18 00:18:46 +01003543 PSA_ASSERT( psa_import_key( handle, key_type,
3544 key_data->x,
3545 key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003546
Gilles Peskinefe11b722018-12-18 00:24:04 +01003547 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3548 salt->x, salt->len,
3549 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003550 requested_capacity ),
3551 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003552
3553exit:
3554 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003555 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003556 mbedtls_psa_crypto_free( );
3557}
3558/* END_CASE */
3559
3560/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003561void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003562{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003563 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003564 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003565 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003566 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003567 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003568 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003569 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3570 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3571 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Jaeden Amero70261c52019-01-04 11:47:20 +00003572 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003573
Gilles Peskine8817f612018-12-18 00:18:46 +01003574 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003575
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003576 PSA_ASSERT( psa_allocate_key( &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003577 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003578 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003579
Gilles Peskine8817f612018-12-18 00:18:46 +01003580 PSA_ASSERT( psa_import_key( handle, key_type,
3581 key_data,
3582 sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003583
3584 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01003585 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3586 NULL, 0,
3587 NULL, 0,
3588 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003589
3590 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003591 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3592 NULL, 0,
3593 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003594 capacity ),
3595 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003596
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003597 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003598
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003599 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
3600 PSA_ERROR_INSUFFICIENT_CAPACITY );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003601
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003602exit:
3603 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003604 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003605 mbedtls_psa_crypto_free( );
3606}
3607/* END_CASE */
3608
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003609/* BEGIN_CASE */
3610void test_derive_invalid_generator_tests( )
3611{
3612 uint8_t output_buffer[16];
3613 size_t buffer_size = 16;
3614 size_t capacity = 0;
3615 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3616
Nir Sonnenschein50789302018-10-31 12:16:38 +02003617 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003618 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003619
3620 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003621 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003622
Gilles Peskine8817f612018-12-18 00:18:46 +01003623 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003624
Nir Sonnenschein50789302018-10-31 12:16:38 +02003625 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003626 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003627
Nir Sonnenschein50789302018-10-31 12:16:38 +02003628 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003629 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003630
3631exit:
3632 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003633}
3634/* END_CASE */
3635
3636/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003637void derive_output( int alg_arg,
3638 data_t *key_data,
3639 data_t *salt,
3640 data_t *label,
3641 int requested_capacity_arg,
3642 data_t *expected_output1,
3643 data_t *expected_output2 )
3644{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003645 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003646 psa_algorithm_t alg = alg_arg;
3647 size_t requested_capacity = requested_capacity_arg;
3648 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3649 uint8_t *expected_outputs[2] =
3650 {expected_output1->x, expected_output2->x};
3651 size_t output_sizes[2] =
3652 {expected_output1->len, expected_output2->len};
3653 size_t output_buffer_size = 0;
3654 uint8_t *output_buffer = NULL;
3655 size_t expected_capacity;
3656 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003657 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003658 psa_status_t status;
3659 unsigned i;
3660
3661 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3662 {
3663 if( output_sizes[i] > output_buffer_size )
3664 output_buffer_size = output_sizes[i];
3665 if( output_sizes[i] == 0 )
3666 expected_outputs[i] = NULL;
3667 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003668 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003669 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003670
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003671 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003672 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003673 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003674
Gilles Peskine8817f612018-12-18 00:18:46 +01003675 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3676 key_data->x,
3677 key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003678
3679 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003680 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3681 salt->x, salt->len,
3682 label->x, label->len,
3683 requested_capacity ) );
3684 PSA_ASSERT( psa_get_generator_capacity( &generator,
3685 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003686 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003687 expected_capacity = requested_capacity;
3688
3689 /* Expansion phase. */
3690 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3691 {
3692 /* Read some bytes. */
3693 status = psa_generator_read( &generator,
3694 output_buffer, output_sizes[i] );
3695 if( expected_capacity == 0 && output_sizes[i] == 0 )
3696 {
3697 /* Reading 0 bytes when 0 bytes are available can go either way. */
3698 TEST_ASSERT( status == PSA_SUCCESS ||
3699 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3700 continue;
3701 }
3702 else if( expected_capacity == 0 ||
3703 output_sizes[i] > expected_capacity )
3704 {
3705 /* Capacity exceeded. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003706 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003707 expected_capacity = 0;
3708 continue;
3709 }
3710 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003711 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003712 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003713 ASSERT_COMPARE( output_buffer, output_sizes[i],
3714 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003715 /* Check the generator status. */
3716 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01003717 PSA_ASSERT( psa_get_generator_capacity( &generator,
3718 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003719 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003720 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003721 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003722
3723exit:
3724 mbedtls_free( output_buffer );
3725 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003726 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003727 mbedtls_psa_crypto_free( );
3728}
3729/* END_CASE */
3730
3731/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003732void derive_full( int alg_arg,
3733 data_t *key_data,
3734 data_t *salt,
3735 data_t *label,
3736 int requested_capacity_arg )
3737{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003738 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02003739 psa_algorithm_t alg = alg_arg;
3740 size_t requested_capacity = requested_capacity_arg;
3741 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3742 unsigned char output_buffer[16];
3743 size_t expected_capacity = requested_capacity;
3744 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003745 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02003746
Gilles Peskine8817f612018-12-18 00:18:46 +01003747 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003748
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003749 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003750 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003751 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003752
Gilles Peskine8817f612018-12-18 00:18:46 +01003753 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3754 key_data->x,
3755 key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003756
3757 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003758 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3759 salt->x, salt->len,
3760 label->x, label->len,
3761 requested_capacity ) );
3762 PSA_ASSERT( psa_get_generator_capacity( &generator,
3763 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003764 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003765
3766 /* Expansion phase. */
3767 while( current_capacity > 0 )
3768 {
3769 size_t read_size = sizeof( output_buffer );
3770 if( read_size > current_capacity )
3771 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01003772 PSA_ASSERT( psa_generator_read( &generator,
3773 output_buffer,
3774 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003775 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01003776 PSA_ASSERT( psa_get_generator_capacity( &generator,
3777 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003778 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003779 }
3780
3781 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003782 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
3783 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskined54931c2018-07-17 21:06:59 +02003784
Gilles Peskine8817f612018-12-18 00:18:46 +01003785 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003786
3787exit:
3788 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003789 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02003790 mbedtls_psa_crypto_free( );
3791}
3792/* END_CASE */
3793
3794/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003795void derive_key_exercise( int alg_arg,
3796 data_t *key_data,
3797 data_t *salt,
3798 data_t *label,
3799 int derived_type_arg,
3800 int derived_bits_arg,
3801 int derived_usage_arg,
3802 int derived_alg_arg )
3803{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003804 psa_key_handle_t base_handle = 0;
3805 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003806 psa_algorithm_t alg = alg_arg;
3807 psa_key_type_t derived_type = derived_type_arg;
3808 size_t derived_bits = derived_bits_arg;
3809 psa_key_usage_t derived_usage = derived_usage_arg;
3810 psa_algorithm_t derived_alg = derived_alg_arg;
3811 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3812 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003813 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003814 psa_key_type_t got_type;
3815 size_t got_bits;
3816
Gilles Peskine8817f612018-12-18 00:18:46 +01003817 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003818
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003819 PSA_ASSERT( psa_allocate_key( &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003820 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003821 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3822 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3823 key_data->x,
3824 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003825
3826 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003827 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3828 salt->x, salt->len,
3829 label->x, label->len,
3830 capacity ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003831 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003832 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003833 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3834 PSA_ASSERT( psa_generator_import_key( derived_handle,
3835 derived_type,
3836 derived_bits,
3837 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003838
3839 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01003840 PSA_ASSERT( psa_get_key_information( derived_handle,
3841 &got_type,
3842 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003843 TEST_EQUAL( got_type, derived_type );
3844 TEST_EQUAL( got_bits, derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003845
3846 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003847 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02003848 goto exit;
3849
3850exit:
3851 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003852 psa_destroy_key( base_handle );
3853 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003854 mbedtls_psa_crypto_free( );
3855}
3856/* END_CASE */
3857
3858/* BEGIN_CASE */
3859void derive_key_export( int alg_arg,
3860 data_t *key_data,
3861 data_t *salt,
3862 data_t *label,
3863 int bytes1_arg,
3864 int bytes2_arg )
3865{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003866 psa_key_handle_t base_handle = 0;
3867 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003868 psa_algorithm_t alg = alg_arg;
3869 size_t bytes1 = bytes1_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003870 size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003871 size_t bytes2 = bytes2_arg;
3872 size_t capacity = bytes1 + bytes2;
3873 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003874 uint8_t *output_buffer = NULL;
3875 uint8_t *export_buffer = NULL;
Jaeden Amero70261c52019-01-04 11:47:20 +00003876 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003877 size_t length;
3878
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003879 ASSERT_ALLOC( output_buffer, capacity );
3880 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01003881 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003882
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003883 PSA_ASSERT( psa_allocate_key( &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003884 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003885 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3886 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3887 key_data->x,
3888 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003889
3890 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003891 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3892 salt->x, salt->len,
3893 label->x, label->len,
3894 capacity ) );
3895 PSA_ASSERT( psa_generator_read( &generator,
3896 output_buffer,
3897 capacity ) );
3898 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003899
3900 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003901 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3902 salt->x, salt->len,
3903 label->x, label->len,
3904 capacity ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003905 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003906 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003907 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3908 PSA_ASSERT( psa_generator_import_key( derived_handle,
3909 PSA_KEY_TYPE_RAW_DATA,
3910 derived_bits,
3911 &generator ) );
3912 PSA_ASSERT( psa_export_key( derived_handle,
3913 export_buffer, bytes1,
3914 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003915 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003916 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003917 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003918 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3919 PSA_ASSERT( psa_generator_import_key( derived_handle,
3920 PSA_KEY_TYPE_RAW_DATA,
3921 PSA_BYTES_TO_BITS( bytes2 ),
3922 &generator ) );
3923 PSA_ASSERT( psa_export_key( derived_handle,
3924 export_buffer + bytes1, bytes2,
3925 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003926 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003927
3928 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003929 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
3930 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003931
3932exit:
3933 mbedtls_free( output_buffer );
3934 mbedtls_free( export_buffer );
3935 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003936 psa_destroy_key( base_handle );
3937 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003938 mbedtls_psa_crypto_free( );
3939}
3940/* END_CASE */
3941
3942/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02003943void key_agreement_setup( int alg_arg,
3944 int our_key_type_arg, data_t *our_key_data,
3945 data_t *peer_key_data,
3946 int expected_status_arg )
3947{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003948 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003949 psa_algorithm_t alg = alg_arg;
3950 psa_key_type_t our_key_type = our_key_type_arg;
3951 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003952 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003953
Gilles Peskine8817f612018-12-18 00:18:46 +01003954 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003955
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003956 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003957 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003958 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3959 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3960 our_key_data->x,
3961 our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003962
Gilles Peskinefe11b722018-12-18 00:24:04 +01003963 TEST_EQUAL( psa_key_agreement( &generator,
3964 our_key,
3965 peer_key_data->x, peer_key_data->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003966 alg ),
3967 expected_status_arg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003968
3969exit:
3970 psa_generator_abort( &generator );
3971 psa_destroy_key( our_key );
3972 mbedtls_psa_crypto_free( );
3973}
3974/* END_CASE */
3975
3976/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02003977void key_agreement_capacity( int alg_arg,
3978 int our_key_type_arg, data_t *our_key_data,
3979 data_t *peer_key_data,
3980 int expected_capacity_arg )
3981{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003982 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003983 psa_algorithm_t alg = alg_arg;
3984 psa_key_type_t our_key_type = our_key_type_arg;
3985 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003986 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02003987 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02003988 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02003989
Gilles Peskine8817f612018-12-18 00:18:46 +01003990 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003991
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003992 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003993 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003994 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3995 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3996 our_key_data->x,
3997 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003998
Gilles Peskine8817f612018-12-18 00:18:46 +01003999 PSA_ASSERT( psa_key_agreement( &generator,
4000 our_key,
4001 peer_key_data->x, peer_key_data->len,
4002 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004003
Gilles Peskinebf491972018-10-25 22:36:12 +02004004 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004005 PSA_ASSERT( psa_get_generator_capacity(
4006 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004007 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004008
Gilles Peskinebf491972018-10-25 22:36:12 +02004009 /* Test the actual capacity by reading the output. */
4010 while( actual_capacity > sizeof( output ) )
4011 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004012 PSA_ASSERT( psa_generator_read( &generator,
4013 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004014 actual_capacity -= sizeof( output );
4015 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004016 PSA_ASSERT( psa_generator_read( &generator,
4017 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004018 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
4019 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskinebf491972018-10-25 22:36:12 +02004020
Gilles Peskine59685592018-09-18 12:11:34 +02004021exit:
4022 psa_generator_abort( &generator );
4023 psa_destroy_key( our_key );
4024 mbedtls_psa_crypto_free( );
4025}
4026/* END_CASE */
4027
4028/* BEGIN_CASE */
4029void key_agreement_output( int alg_arg,
4030 int our_key_type_arg, data_t *our_key_data,
4031 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004032 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004033{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004034 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004035 psa_algorithm_t alg = alg_arg;
4036 psa_key_type_t our_key_type = our_key_type_arg;
4037 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004038 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004039 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004040
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004041 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4042 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004043
Gilles Peskine8817f612018-12-18 00:18:46 +01004044 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004045
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004046 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004047 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004048 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
4049 PSA_ASSERT( psa_import_key( our_key, our_key_type,
4050 our_key_data->x,
4051 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004052
Gilles Peskine8817f612018-12-18 00:18:46 +01004053 PSA_ASSERT( psa_key_agreement( &generator,
4054 our_key,
4055 peer_key_data->x, peer_key_data->len,
4056 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004057
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004058 PSA_ASSERT( psa_generator_read( &generator,
4059 actual_output,
4060 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004061 ASSERT_COMPARE( actual_output, expected_output1->len,
4062 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004063 if( expected_output2->len != 0 )
4064 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004065 PSA_ASSERT( psa_generator_read( &generator,
4066 actual_output,
4067 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004068 ASSERT_COMPARE( actual_output, expected_output2->len,
4069 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004070 }
Gilles Peskine59685592018-09-18 12:11:34 +02004071
4072exit:
4073 psa_generator_abort( &generator );
4074 psa_destroy_key( our_key );
4075 mbedtls_psa_crypto_free( );
4076 mbedtls_free( actual_output );
4077}
4078/* END_CASE */
4079
4080/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004081void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004082{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004083 size_t bytes = bytes_arg;
4084 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004085 unsigned char *output = NULL;
4086 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004087 size_t i;
4088 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004089
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004090 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
4091 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004092 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004093
Gilles Peskine8817f612018-12-18 00:18:46 +01004094 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004095
Gilles Peskinea50d7392018-06-21 10:22:13 +02004096 /* Run several times, to ensure that every output byte will be
4097 * nonzero at least once with overwhelming probability
4098 * (2^(-8*number_of_runs)). */
4099 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004100 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004101 if( bytes != 0 )
4102 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004103 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004104
4105 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004106 ASSERT_COMPARE( output + bytes, sizeof( trail ),
4107 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004108
4109 for( i = 0; i < bytes; i++ )
4110 {
4111 if( output[i] != 0 )
4112 ++changed[i];
4113 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004114 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004115
4116 /* Check that every byte was changed to nonzero at least once. This
4117 * validates that psa_generate_random is overwriting every byte of
4118 * the output buffer. */
4119 for( i = 0; i < bytes; i++ )
4120 {
4121 TEST_ASSERT( changed[i] != 0 );
4122 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004123
4124exit:
4125 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004126 mbedtls_free( output );
4127 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004128}
4129/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004130
4131/* BEGIN_CASE */
4132void generate_key( int type_arg,
4133 int bits_arg,
4134 int usage_arg,
4135 int alg_arg,
4136 int expected_status_arg )
4137{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004138 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004139 psa_key_type_t type = type_arg;
4140 psa_key_usage_t usage = usage_arg;
4141 size_t bits = bits_arg;
4142 psa_algorithm_t alg = alg_arg;
4143 psa_status_t expected_status = expected_status_arg;
4144 psa_key_type_t got_type;
4145 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004146 psa_status_t expected_info_status =
4147 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004148 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004149
Gilles Peskine8817f612018-12-18 00:18:46 +01004150 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004151
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004152 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004153 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004154 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004155
4156 /* Generate a key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004157 TEST_EQUAL( psa_generate_key( handle, type, bits, NULL, 0 ),
4158 expected_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004159
4160 /* Test the key information */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004161 TEST_EQUAL( psa_get_key_information( handle, &got_type, &got_bits ),
4162 expected_info_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004163 if( expected_info_status != PSA_SUCCESS )
4164 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004165 TEST_EQUAL( got_type, type );
4166 TEST_EQUAL( got_bits, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004167
Gilles Peskine818ca122018-06-20 18:16:48 +02004168 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004169 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004170 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004171
4172exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004173 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004174 mbedtls_psa_crypto_free( );
4175}
4176/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004177
Darryl Greend49a4992018-06-18 17:27:26 +01004178/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
4179void persistent_key_load_key_from_storage( data_t *data, int type_arg,
4180 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00004181 int alg_arg, int generation_method,
4182 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01004183{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004184 psa_key_handle_t handle = 0;
4185 psa_key_handle_t base_key;
Darryl Greend49a4992018-06-18 17:27:26 +01004186 psa_key_type_t type = (psa_key_type_t) type_arg;
4187 psa_key_type_t type_get;
4188 size_t bits_get;
Jaeden Amero70261c52019-01-04 11:47:20 +00004189 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
4190 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004191 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
4192 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00004193 psa_key_policy_t base_policy_set = PSA_KEY_POLICY_INIT;
Darryl Green0c6575a2018-11-07 16:05:30 +00004194 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
4195 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004196 unsigned char *first_export = NULL;
4197 unsigned char *second_export = NULL;
4198 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4199 size_t first_exported_length;
4200 size_t second_exported_length;
4201
4202 ASSERT_ALLOC( first_export, export_size );
4203 ASSERT_ALLOC( second_export, export_size );
4204
Gilles Peskine8817f612018-12-18 00:18:46 +01004205 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004206
Gilles Peskine8817f612018-12-18 00:18:46 +01004207 PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
Gilles Peskine8817f612018-12-18 00:18:46 +01004208 &handle ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004209 psa_key_policy_set_usage( &policy_set, policy_usage,
4210 policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004211 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004212
Darryl Green0c6575a2018-11-07 16:05:30 +00004213 switch( generation_method )
4214 {
4215 case IMPORT_KEY:
4216 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004217 PSA_ASSERT( psa_import_key( handle, type,
4218 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004219 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004220
Darryl Green0c6575a2018-11-07 16:05:30 +00004221 case GENERATE_KEY:
4222 /* Generate a key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004223 PSA_ASSERT( psa_generate_key( handle, type, bits,
4224 NULL, 0 ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004225 break;
4226
4227 case DERIVE_KEY:
4228 /* Create base key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004229 PSA_ASSERT( psa_allocate_key( &base_key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004230 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
4231 base_policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004232 PSA_ASSERT( psa_set_key_policy(
4233 base_key, &base_policy_set ) );
4234 PSA_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
4235 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004236 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004237 PSA_ASSERT( psa_key_derivation( &generator, base_key,
4238 base_policy_alg,
4239 NULL, 0, NULL, 0,
4240 export_size ) );
4241 PSA_ASSERT( psa_generator_import_key(
4242 handle, PSA_KEY_TYPE_RAW_DATA,
4243 bits, &generator ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004244 break;
4245 }
Darryl Greend49a4992018-06-18 17:27:26 +01004246
4247 /* Export the key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004248 TEST_EQUAL( psa_export_key( handle,
4249 first_export, export_size,
4250 &first_exported_length ),
4251 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004252
4253 /* Shutdown and restart */
4254 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004255 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004256
Darryl Greend49a4992018-06-18 17:27:26 +01004257 /* Check key slot still contains key data */
Gilles Peskine8817f612018-12-18 00:18:46 +01004258 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4259 &handle ) );
4260 PSA_ASSERT( psa_get_key_information(
4261 handle, &type_get, &bits_get ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004262 TEST_EQUAL( type_get, type );
4263 TEST_EQUAL( bits_get, (size_t) bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004264
Gilles Peskine8817f612018-12-18 00:18:46 +01004265 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004266 TEST_EQUAL( psa_key_policy_get_usage( &policy_get ), policy_usage );
4267 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_get ), policy_alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004268
4269 /* Export the key again */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004270 TEST_EQUAL( psa_export_key( handle,
4271 second_export, export_size,
4272 &second_exported_length ),
4273 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004274
Darryl Green0c6575a2018-11-07 16:05:30 +00004275 if( export_status == PSA_SUCCESS )
4276 {
4277 ASSERT_COMPARE( first_export, first_exported_length,
4278 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01004279
Darryl Green0c6575a2018-11-07 16:05:30 +00004280 switch( generation_method )
4281 {
4282 case IMPORT_KEY:
4283 ASSERT_COMPARE( data->x, data->len,
4284 first_export, first_exported_length );
4285 break;
4286 default:
4287 break;
4288 }
4289 }
4290
4291 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004292 if( ! exercise_key( handle, policy_usage, policy_alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004293 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004294
4295exit:
4296 mbedtls_free( first_export );
4297 mbedtls_free( second_export );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004298 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004299 mbedtls_psa_crypto_free();
4300}
4301/* END_CASE */