blob: 5c662d8f1eca9d93a8bee2b624adbbff12b00538 [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;
David Saadab4ecc272019-02-14 13:48:10 +0200422 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinec7998b72018-11-07 18:45:02 +0100423 * psa_key_agreement fails. This isn't fully satisfactory, but it's
424 * good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200425 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100426
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 Peskinec9516fb2019-02-05 20:32:06 +0100774/** Do smoke tests on a key.
775 *
776 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
777 * sign/verify, or derivation) that is permitted according to \p usage.
778 * \p usage and \p alg should correspond to the expected policy on the
779 * key.
780 *
781 * Export the key if permitted by \p usage, and check that the output
782 * looks sensible. If \p usage forbids export, check that
783 * \p psa_export_key correctly rejects the attempt. If the key is
784 * asymmetric, also check \p psa_export_public_key.
785 *
786 * If the key fails the tests, this function calls the test framework's
787 * `test_fail` function and returns false. Otherwise this function returns
788 * true. Therefore it should be used as follows:
789 * ```
790 * if( ! exercise_key( ... ) ) goto exit;
791 * ```
792 *
793 * \param handle The key to exercise. It should be capable of performing
794 * \p alg.
795 * \param usage The usage flags to assume.
796 * \param alg The algorithm to exercise.
797 *
798 * \retval 0 The key failed the smoke tests.
799 * \retval 1 The key passed the smoke tests.
800 */
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100801static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +0200802 psa_key_usage_t usage,
803 psa_algorithm_t alg )
804{
805 int ok;
806 if( alg == 0 )
807 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
808 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100809 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200810 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100811 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200812 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100813 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200814 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100815 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200816 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100817 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200818 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100819 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200820 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100821 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200822 else
823 {
824 char message[40];
825 mbedtls_snprintf( message, sizeof( message ),
826 "No code to exercise alg=0x%08lx",
827 (unsigned long) alg );
828 test_fail( message, __LINE__, __FILE__ );
829 ok = 0;
830 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200831
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100832 ok = ok && exercise_export_key( handle, usage );
833 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +0200834
Gilles Peskine02b75072018-07-01 22:31:34 +0200835 return( ok );
836}
837
Gilles Peskine10df3412018-10-25 22:35:43 +0200838static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
839 psa_algorithm_t alg )
840{
841 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
842 {
843 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
844 PSA_KEY_USAGE_VERIFY :
845 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
846 }
847 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
848 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
849 {
850 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
851 PSA_KEY_USAGE_ENCRYPT :
852 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
853 }
854 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
855 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
856 {
857 return( PSA_KEY_USAGE_DERIVE );
858 }
859 else
860 {
861 return( 0 );
862 }
863
864}
Darryl Green0c6575a2018-11-07 16:05:30 +0000865
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100866/* An overapproximation of the amount of storage needed for a key of the
867 * given type and with the given content. The API doesn't make it easy
868 * to find a good value for the size. The current implementation doesn't
869 * care about the value anyway. */
870#define KEY_BITS_FROM_DATA( type, data ) \
871 ( data )->len
872
Darryl Green0c6575a2018-11-07 16:05:30 +0000873typedef enum {
874 IMPORT_KEY = 0,
875 GENERATE_KEY = 1,
876 DERIVE_KEY = 2
877} generate_method;
878
Gilles Peskinee59236f2018-01-27 23:32:46 +0100879/* END_HEADER */
880
881/* BEGIN_DEPENDENCIES
882 * depends_on:MBEDTLS_PSA_CRYPTO_C
883 * END_DEPENDENCIES
884 */
885
886/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200887void static_checks( )
888{
889 size_t max_truncated_mac_size =
890 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
891
892 /* Check that the length for a truncated MAC always fits in the algorithm
893 * encoding. The shifted mask is the maximum truncated value. The
894 * untruncated algorithm may be one byte larger. */
895 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
896}
897/* END_CASE */
898
899/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200900void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100901{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100902 psa_key_handle_t handle = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200903 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100904 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100905
Gilles Peskine8817f612018-12-18 00:18:46 +0100906 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100907
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100908 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100909 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100910 TEST_EQUAL( status, expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100911 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100912 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100913
914exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100915 mbedtls_psa_crypto_free( );
916}
917/* END_CASE */
918
919/* BEGIN_CASE */
Gilles Peskinea4261682018-12-03 11:34:01 +0100920void import_twice( int alg_arg, int usage_arg,
921 int type1_arg, data_t *data1,
922 int expected_import1_status_arg,
923 int type2_arg, data_t *data2,
924 int expected_import2_status_arg )
925{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100926 psa_key_handle_t handle = 0;
Gilles Peskinea4261682018-12-03 11:34:01 +0100927 psa_algorithm_t alg = alg_arg;
928 psa_key_usage_t usage = usage_arg;
929 psa_key_type_t type1 = type1_arg;
930 psa_status_t expected_import1_status = expected_import1_status_arg;
931 psa_key_type_t type2 = type2_arg;
932 psa_status_t expected_import2_status = expected_import2_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +0000933 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea4261682018-12-03 11:34:01 +0100934 psa_status_t status;
935
Gilles Peskine8817f612018-12-18 00:18:46 +0100936 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100937
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100938 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100939 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100940 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100941
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100942 status = psa_import_key( handle, type1, data1->x, data1->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100943 TEST_EQUAL( status, expected_import1_status );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100944 status = psa_import_key( handle, type2, data2->x, data2->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100945 TEST_EQUAL( status, expected_import2_status );
Gilles Peskinea4261682018-12-03 11:34:01 +0100946
947 if( expected_import1_status == PSA_SUCCESS ||
948 expected_import2_status == PSA_SUCCESS )
949 {
Gilles Peskinec9516fb2019-02-05 20:32:06 +0100950 if( ! exercise_key( handle, usage, alg ) )
951 goto exit;
Gilles Peskinea4261682018-12-03 11:34:01 +0100952 }
953
954exit:
955 mbedtls_psa_crypto_free( );
956}
957/* END_CASE */
958
959/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200960void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
961{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100962 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200963 size_t bits = bits_arg;
964 psa_status_t expected_status = expected_status_arg;
965 psa_status_t status;
966 psa_key_type_t type =
967 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
968 size_t buffer_size = /* Slight overapproximations */
969 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200970 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200971 unsigned char *p;
972 int ret;
973 size_t length;
974
Gilles Peskine8817f612018-12-18 00:18:46 +0100975 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200976 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200977
978 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
979 bits, keypair ) ) >= 0 );
980 length = ret;
981
982 /* Try importing the key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100983 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100984 status = psa_import_key( handle, type, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100985 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200986 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100987 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200988
989exit:
990 mbedtls_free( buffer );
991 mbedtls_psa_crypto_free( );
992}
993/* END_CASE */
994
995/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300996void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300997 int type_arg,
998 int alg_arg,
999 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001000 int expected_bits,
1001 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001002 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001003 int canonical_input )
1004{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001005 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001006 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001007 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001008 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001009 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001010 unsigned char *exported = NULL;
1011 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001012 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001013 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001014 size_t reexported_length;
1015 psa_key_type_t got_type;
1016 size_t got_bits;
Jaeden Amero70261c52019-01-04 11:47:20 +00001017 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001018
Moran Pekercb088e72018-07-17 17:36:59 +03001019 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001020 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001021 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001022 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001023 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001024
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001025 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001026 psa_key_policy_set_usage( &policy, usage_arg, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001027 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001028
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001029 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
David Saadab4ecc272019-02-14 13:48:10 +02001030 PSA_ERROR_DOES_NOT_EXIST );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001031
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001032 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001033 PSA_ASSERT( psa_import_key( handle, type,
1034 data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001035
1036 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001037 PSA_ASSERT( psa_get_key_information( handle,
1038 &got_type,
1039 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001040 TEST_EQUAL( got_type, type );
1041 TEST_EQUAL( got_bits, (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001042
1043 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001044 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001045 exported, export_size,
1046 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001047 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001048
1049 /* The exported length must be set by psa_export_key() to a value between 0
1050 * and export_size. On errors, the exported length must be 0. */
1051 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1052 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1053 TEST_ASSERT( exported_length <= export_size );
1054
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001055 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001056 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001057 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001058 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001059 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001060 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001061 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001062
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001063 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001064 goto exit;
1065
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001066 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001067 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001068 else
1069 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001070 psa_key_handle_t handle2;
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001071 PSA_ASSERT( psa_allocate_key( &handle2 ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001072 PSA_ASSERT( psa_set_key_policy( handle2, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001073
Gilles Peskine8817f612018-12-18 00:18:46 +01001074 PSA_ASSERT( psa_import_key( handle2, type,
1075 exported,
1076 exported_length ) );
1077 PSA_ASSERT( psa_export_key( handle2,
1078 reexported,
1079 export_size,
1080 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001081 ASSERT_COMPARE( exported, exported_length,
1082 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001083 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001084 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001085 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001086
1087destroy:
1088 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001089 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001090 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1091 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001092
1093exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001094 mbedtls_free( exported );
1095 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001096 mbedtls_psa_crypto_free( );
1097}
1098/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001099
Moran Pekerf709f4a2018-06-06 17:26:04 +03001100/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001101void import_key_nonempty_slot( )
1102{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001103 psa_key_handle_t handle = 0;
Moran Peker28a38e62018-11-07 16:18:24 +02001104 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1105 psa_status_t status;
1106 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
Gilles Peskine8817f612018-12-18 00:18:46 +01001107 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001108
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001109 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001110
Moran Peker28a38e62018-11-07 16:18:24 +02001111 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001112 PSA_ASSERT( psa_import_key( handle, type,
1113 data, sizeof( data ) ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001114
1115 /* Import the key again */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001116 status = psa_import_key( handle, type, data, sizeof( data ) );
David Saadab4ecc272019-02-14 13:48:10 +02001117 TEST_EQUAL( status, PSA_ERROR_ALREADY_EXISTS );
Moran Peker28a38e62018-11-07 16:18:24 +02001118
1119exit:
1120 mbedtls_psa_crypto_free( );
1121}
1122/* END_CASE */
1123
1124/* BEGIN_CASE */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001125void export_invalid_handle( int handle, int expected_export_status_arg )
Moran Peker28a38e62018-11-07 16:18:24 +02001126{
1127 psa_status_t status;
1128 unsigned char *exported = NULL;
1129 size_t export_size = 0;
1130 size_t exported_length = INVALID_EXPORT_LENGTH;
1131 psa_status_t expected_export_status = expected_export_status_arg;
1132
Gilles Peskine8817f612018-12-18 00:18:46 +01001133 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001134
1135 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001136 status = psa_export_key( (psa_key_handle_t) handle,
Moran Peker28a38e62018-11-07 16:18:24 +02001137 exported, export_size,
1138 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001139 TEST_EQUAL( status, expected_export_status );
Moran Peker28a38e62018-11-07 16:18:24 +02001140
1141exit:
1142 mbedtls_psa_crypto_free( );
1143}
1144/* END_CASE */
1145
1146/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001147void export_with_no_key_activity( )
1148{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001149 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001150 psa_algorithm_t alg = PSA_ALG_CTR;
1151 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001152 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001153 unsigned char *exported = NULL;
1154 size_t export_size = 0;
1155 size_t exported_length = INVALID_EXPORT_LENGTH;
1156
Gilles Peskine8817f612018-12-18 00:18:46 +01001157 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001158
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001159 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001160 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001161 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001162
1163 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001164 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001165 exported, export_size,
1166 &exported_length );
David Saadab4ecc272019-02-14 13:48:10 +02001167 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Peker34550092018-11-07 16:19:34 +02001168
1169exit:
1170 mbedtls_psa_crypto_free( );
1171}
1172/* END_CASE */
1173
1174/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001175void cipher_with_no_key_activity( )
1176{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001177 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001178 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001179 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001180 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001181 int exercise_alg = PSA_ALG_CTR;
1182
Gilles Peskine8817f612018-12-18 00:18:46 +01001183 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001184
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001185 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekerce500072018-11-07 16:20:07 +02001186 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001187 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerce500072018-11-07 16:20:07 +02001188
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001189 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
David Saadab4ecc272019-02-14 13:48:10 +02001190 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Pekerce500072018-11-07 16:20:07 +02001191
1192exit:
1193 psa_cipher_abort( &operation );
1194 mbedtls_psa_crypto_free( );
1195}
1196/* END_CASE */
1197
1198/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001199void export_after_import_failure( data_t *data, int type_arg,
1200 int expected_import_status_arg )
1201{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001202 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001203 psa_key_type_t type = type_arg;
1204 psa_status_t status;
1205 unsigned char *exported = NULL;
1206 size_t export_size = 0;
1207 psa_status_t expected_import_status = expected_import_status_arg;
1208 size_t exported_length = INVALID_EXPORT_LENGTH;
1209
Gilles Peskine8817f612018-12-18 00:18:46 +01001210 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001211
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001212 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001213
Moran Peker34550092018-11-07 16:19:34 +02001214 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001215 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001216 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001217 TEST_EQUAL( status, expected_import_status );
Moran Peker34550092018-11-07 16:19:34 +02001218
1219 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001220 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001221 exported, export_size,
1222 &exported_length );
David Saadab4ecc272019-02-14 13:48:10 +02001223 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Peker34550092018-11-07 16:19:34 +02001224
1225exit:
1226 mbedtls_psa_crypto_free( );
1227}
1228/* END_CASE */
1229
1230/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001231void cipher_after_import_failure( data_t *data, int type_arg,
1232 int expected_import_status_arg )
1233{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001234 psa_key_handle_t handle = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001235 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001236 psa_key_type_t type = type_arg;
1237 psa_status_t status;
1238 psa_status_t expected_import_status = expected_import_status_arg;
1239 int exercise_alg = PSA_ALG_CTR;
1240
Gilles Peskine8817f612018-12-18 00:18:46 +01001241 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001242
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001243 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001244
Moran Pekerce500072018-11-07 16:20:07 +02001245 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001246 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001247 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001248 TEST_EQUAL( status, expected_import_status );
Moran Pekerce500072018-11-07 16:20:07 +02001249
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001250 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
David Saadab4ecc272019-02-14 13:48:10 +02001251 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Pekerce500072018-11-07 16:20:07 +02001252
1253exit:
1254 psa_cipher_abort( &operation );
1255 mbedtls_psa_crypto_free( );
1256}
1257/* END_CASE */
1258
1259/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001260void export_after_destroy_key( data_t *data, int type_arg )
1261{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001262 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001263 psa_key_type_t type = type_arg;
1264 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001265 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001266 psa_algorithm_t alg = PSA_ALG_CTR;
1267 unsigned char *exported = NULL;
1268 size_t export_size = 0;
1269 size_t exported_length = INVALID_EXPORT_LENGTH;
1270
Gilles Peskine8817f612018-12-18 00:18:46 +01001271 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001272
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001273 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001274 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001275 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001276 export_size = (ptrdiff_t) data->len;
1277 ASSERT_ALLOC( exported, export_size );
1278
1279 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001280 PSA_ASSERT( psa_import_key( handle, type,
1281 data->x, data->len ) );
Moran Peker34550092018-11-07 16:19:34 +02001282
Gilles Peskine8817f612018-12-18 00:18:46 +01001283 PSA_ASSERT( psa_export_key( handle, exported, export_size,
1284 &exported_length ) );
Moran Peker34550092018-11-07 16:19:34 +02001285
1286 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001287 PSA_ASSERT( psa_destroy_key( handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001288
1289 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001290 status = psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001291 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001292 TEST_EQUAL( status, PSA_ERROR_INVALID_HANDLE );
Moran Peker34550092018-11-07 16:19:34 +02001293
1294exit:
1295 mbedtls_free( exported );
1296 mbedtls_psa_crypto_free( );
1297}
1298/* END_CASE */
1299
1300/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001301void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001302 int type_arg,
1303 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001304 int export_size_delta,
1305 int expected_export_status_arg,
1306 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001307{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001308 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001309 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001310 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001311 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001312 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001313 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001314 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001315 size_t exported_length = INVALID_EXPORT_LENGTH;
Jaeden Amero70261c52019-01-04 11:47:20 +00001316 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001317
Gilles Peskine8817f612018-12-18 00:18:46 +01001318 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001319
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001320 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001321 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001322 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001323
1324 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001325 PSA_ASSERT( psa_import_key( handle, type,
1326 data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001327
Gilles Peskine49c25912018-10-29 15:15:31 +01001328 /* Export the public key */
1329 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001330 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001331 exported, export_size,
1332 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001333 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001334 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001335 {
1336 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1337 size_t bits;
Gilles Peskine8817f612018-12-18 00:18:46 +01001338 PSA_ASSERT( psa_get_key_information( handle, NULL, &bits ) );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001339 TEST_ASSERT( expected_public_key->len <=
1340 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001341 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1342 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001343 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001344
1345exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001346 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001347 psa_destroy_key( handle );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001348 mbedtls_psa_crypto_free( );
1349}
1350/* END_CASE */
1351
Gilles Peskine20035e32018-02-03 22:44:14 +01001352/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001353void import_and_exercise_key( data_t *data,
1354 int type_arg,
1355 int bits_arg,
1356 int alg_arg )
1357{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001358 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001359 psa_key_type_t type = type_arg;
1360 size_t bits = bits_arg;
1361 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001362 psa_key_usage_t usage = usage_to_exercise( type, alg );
Jaeden Amero70261c52019-01-04 11:47:20 +00001363 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001364 psa_key_type_t got_type;
1365 size_t got_bits;
1366 psa_status_t status;
1367
Gilles Peskine8817f612018-12-18 00:18:46 +01001368 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001369
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001370 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001371 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001372 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001373
1374 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001375 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01001376 PSA_ASSERT( status );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001377
1378 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001379 PSA_ASSERT( psa_get_key_information( handle,
1380 &got_type,
1381 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001382 TEST_EQUAL( got_type, type );
1383 TEST_EQUAL( got_bits, bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001384
1385 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001386 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001387 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001388
1389exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001390 psa_destroy_key( handle );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001391 mbedtls_psa_crypto_free( );
1392}
1393/* END_CASE */
1394
1395/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001396void key_policy( int usage_arg, int alg_arg )
1397{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001398 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001399 psa_algorithm_t alg = alg_arg;
1400 psa_key_usage_t usage = usage_arg;
1401 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1402 unsigned char key[32] = {0};
Jaeden Amero70261c52019-01-04 11:47:20 +00001403 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
1404 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001405
1406 memset( key, 0x2a, sizeof( key ) );
1407
Gilles Peskine8817f612018-12-18 00:18:46 +01001408 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001409
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001410 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001411 psa_key_policy_set_usage( &policy_set, usage, alg );
1412
Gilles Peskinefe11b722018-12-18 00:24:04 +01001413 TEST_EQUAL( psa_key_policy_get_usage( &policy_set ), usage );
1414 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_set ), alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001415 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001416
Gilles Peskine8817f612018-12-18 00:18:46 +01001417 PSA_ASSERT( psa_import_key( handle, key_type,
1418 key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001419
Gilles Peskine8817f612018-12-18 00:18:46 +01001420 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001421
Gilles Peskinefe11b722018-12-18 00:24:04 +01001422 TEST_EQUAL( policy_get.usage, policy_set.usage );
1423 TEST_EQUAL( policy_get.alg, policy_set.alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001424
1425exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001426 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001427 mbedtls_psa_crypto_free( );
1428}
1429/* END_CASE */
1430
1431/* BEGIN_CASE */
Jaeden Amero70261c52019-01-04 11:47:20 +00001432void key_policy_init( )
1433{
1434 /* Test each valid way of initializing the object, except for `= {0}`, as
1435 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1436 * though it's OK by the C standard. We could test for this, but we'd need
1437 * to supress the Clang warning for the test. */
1438 psa_key_policy_t func = psa_key_policy_init( );
1439 psa_key_policy_t init = PSA_KEY_POLICY_INIT;
1440 psa_key_policy_t zero;
1441
1442 memset( &zero, 0, sizeof( zero ) );
1443
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001444 /* A default key policy should not permit any usage. */
1445 TEST_EQUAL( psa_key_policy_get_usage( &func ), 0 );
1446 TEST_EQUAL( psa_key_policy_get_usage( &init ), 0 );
1447 TEST_EQUAL( psa_key_policy_get_usage( &zero ), 0 );
1448
1449 /* A default key policy should not permit any algorithm. */
1450 TEST_EQUAL( psa_key_policy_get_algorithm( &func ), 0 );
1451 TEST_EQUAL( psa_key_policy_get_algorithm( &init ), 0 );
1452 TEST_EQUAL( psa_key_policy_get_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001453}
1454/* END_CASE */
1455
1456/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001457void mac_key_policy( int policy_usage,
1458 int policy_alg,
1459 int key_type,
1460 data_t *key_data,
1461 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001462{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001463 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001464 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001465 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001466 psa_status_t status;
1467 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001468
Gilles Peskine8817f612018-12-18 00:18:46 +01001469 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001470
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001471 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001472 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001473 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001474
Gilles Peskine8817f612018-12-18 00:18:46 +01001475 PSA_ASSERT( psa_import_key( handle, key_type,
1476 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001477
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001478 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001479 if( policy_alg == exercise_alg &&
1480 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001481 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001482 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001483 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001484 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001485
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001486 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001487 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001488 if( policy_alg == exercise_alg &&
1489 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001490 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001491 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001492 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001493
1494exit:
1495 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001496 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001497 mbedtls_psa_crypto_free( );
1498}
1499/* END_CASE */
1500
1501/* BEGIN_CASE */
1502void cipher_key_policy( int policy_usage,
1503 int policy_alg,
1504 int key_type,
1505 data_t *key_data,
1506 int exercise_alg )
1507{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001508 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001509 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001510 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001511 psa_status_t status;
1512
Gilles Peskine8817f612018-12-18 00:18:46 +01001513 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001514
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001515 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001516 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001517 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001518
Gilles Peskine8817f612018-12-18 00:18:46 +01001519 PSA_ASSERT( psa_import_key( handle, key_type,
1520 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001521
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001522 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001523 if( policy_alg == exercise_alg &&
1524 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001525 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001526 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001527 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001528 psa_cipher_abort( &operation );
1529
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001530 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001531 if( policy_alg == exercise_alg &&
1532 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001533 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001534 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001535 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001536
1537exit:
1538 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001539 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001540 mbedtls_psa_crypto_free( );
1541}
1542/* END_CASE */
1543
1544/* BEGIN_CASE */
1545void aead_key_policy( int policy_usage,
1546 int policy_alg,
1547 int key_type,
1548 data_t *key_data,
1549 int nonce_length_arg,
1550 int tag_length_arg,
1551 int exercise_alg )
1552{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001553 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001554 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001555 psa_status_t status;
1556 unsigned char nonce[16] = {0};
1557 size_t nonce_length = nonce_length_arg;
1558 unsigned char tag[16];
1559 size_t tag_length = tag_length_arg;
1560 size_t output_length;
1561
1562 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1563 TEST_ASSERT( tag_length <= sizeof( tag ) );
1564
Gilles Peskine8817f612018-12-18 00:18:46 +01001565 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001566
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001567 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001568 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001569 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001570
Gilles Peskine8817f612018-12-18 00:18:46 +01001571 PSA_ASSERT( psa_import_key( handle, key_type,
1572 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001573
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001574 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001575 nonce, nonce_length,
1576 NULL, 0,
1577 NULL, 0,
1578 tag, tag_length,
1579 &output_length );
1580 if( policy_alg == exercise_alg &&
1581 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001582 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001583 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001584 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001585
1586 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001587 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001588 nonce, nonce_length,
1589 NULL, 0,
1590 tag, tag_length,
1591 NULL, 0,
1592 &output_length );
1593 if( policy_alg == exercise_alg &&
1594 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001595 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001596 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001597 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001598
1599exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001600 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001601 mbedtls_psa_crypto_free( );
1602}
1603/* END_CASE */
1604
1605/* BEGIN_CASE */
1606void asymmetric_encryption_key_policy( int policy_usage,
1607 int policy_alg,
1608 int key_type,
1609 data_t *key_data,
1610 int exercise_alg )
1611{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001612 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001613 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001614 psa_status_t status;
1615 size_t key_bits;
1616 size_t buffer_length;
1617 unsigned char *buffer = NULL;
1618 size_t output_length;
1619
Gilles Peskine8817f612018-12-18 00:18:46 +01001620 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001621
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001622 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001623 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001624 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001625
Gilles Peskine8817f612018-12-18 00:18:46 +01001626 PSA_ASSERT( psa_import_key( handle, key_type,
1627 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001628
Gilles Peskine8817f612018-12-18 00:18:46 +01001629 PSA_ASSERT( psa_get_key_information( handle,
1630 NULL,
1631 &key_bits ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001632 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1633 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001634 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001635
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001636 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001637 NULL, 0,
1638 NULL, 0,
1639 buffer, buffer_length,
1640 &output_length );
1641 if( policy_alg == exercise_alg &&
1642 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001643 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001644 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001645 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001646
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001647 if( buffer_length != 0 )
1648 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001649 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001650 buffer, buffer_length,
1651 NULL, 0,
1652 buffer, buffer_length,
1653 &output_length );
1654 if( policy_alg == exercise_alg &&
1655 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001656 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001657 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001658 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001659
1660exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001661 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001662 mbedtls_psa_crypto_free( );
1663 mbedtls_free( buffer );
1664}
1665/* END_CASE */
1666
1667/* BEGIN_CASE */
1668void asymmetric_signature_key_policy( int policy_usage,
1669 int policy_alg,
1670 int key_type,
1671 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001672 int exercise_alg,
1673 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001674{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001675 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001676 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001677 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001678 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1679 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1680 * compatible with the policy and `payload_length_arg` is supposed to be
1681 * a valid input length to sign. If `payload_length_arg <= 0`,
1682 * `exercise_alg` is supposed to be forbidden by the policy. */
1683 int compatible_alg = payload_length_arg > 0;
1684 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001685 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1686 size_t signature_length;
1687
Gilles Peskine8817f612018-12-18 00:18:46 +01001688 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001689
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001690 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001691 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001692 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001693
Gilles Peskine8817f612018-12-18 00:18:46 +01001694 PSA_ASSERT( psa_import_key( handle, key_type,
1695 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001696
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001697 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001698 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001699 signature, sizeof( signature ),
1700 &signature_length );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001701 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001702 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001703 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001704 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001705
1706 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001707 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001708 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001709 signature, sizeof( signature ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001710 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001711 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001712 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001713 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001714
1715exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001716 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001717 mbedtls_psa_crypto_free( );
1718}
1719/* END_CASE */
1720
1721/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001722void derive_key_policy( int policy_usage,
1723 int policy_alg,
1724 int key_type,
1725 data_t *key_data,
1726 int exercise_alg )
1727{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001728 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001729 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001730 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1731 psa_status_t status;
1732
Gilles Peskine8817f612018-12-18 00:18:46 +01001733 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001734
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001735 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001736 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001737 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001738
Gilles Peskine8817f612018-12-18 00:18:46 +01001739 PSA_ASSERT( psa_import_key( handle, key_type,
1740 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001741
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001742 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001743 exercise_alg,
1744 NULL, 0,
1745 NULL, 0,
1746 1 );
1747 if( policy_alg == exercise_alg &&
1748 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001749 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001750 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001751 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001752
1753exit:
1754 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001755 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001756 mbedtls_psa_crypto_free( );
1757}
1758/* END_CASE */
1759
1760/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001761void agreement_key_policy( int policy_usage,
1762 int policy_alg,
1763 int key_type_arg,
1764 data_t *key_data,
1765 int exercise_alg )
1766{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001767 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001768 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001769 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001770 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1771 psa_status_t status;
1772
Gilles Peskine8817f612018-12-18 00:18:46 +01001773 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001774
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001775 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001776 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001777 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001778
Gilles Peskine8817f612018-12-18 00:18:46 +01001779 PSA_ASSERT( psa_import_key( handle, key_type,
1780 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001781
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001782 status = key_agreement_with_self( &generator, handle, exercise_alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001783
Gilles Peskine01d718c2018-09-18 12:01:02 +02001784 if( policy_alg == exercise_alg &&
1785 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001786 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001787 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001788 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001789
1790exit:
1791 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001792 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001793 mbedtls_psa_crypto_free( );
1794}
1795/* END_CASE */
1796
1797/* BEGIN_CASE */
Gilles Peskine57ab7212019-01-28 13:03:09 +01001798void copy_key_policy( int source_usage_arg, int source_alg_arg,
1799 int type_arg, data_t *material,
1800 int target_usage_arg, int target_alg_arg,
1801 int constraint_usage_arg, int constraint_alg_arg,
1802 int expected_usage_arg, int expected_alg_arg )
1803{
1804 psa_key_usage_t source_usage = source_usage_arg;
1805 psa_algorithm_t source_alg = source_alg_arg;
1806 psa_key_handle_t source_handle = 0;
1807 psa_key_policy_t source_policy = PSA_KEY_POLICY_INIT;
1808 psa_key_type_t source_type = type_arg;
1809 size_t source_bits;
1810 psa_key_usage_t target_usage = target_usage_arg;
1811 psa_algorithm_t target_alg = target_alg_arg;
1812 psa_key_handle_t target_handle = 0;
1813 psa_key_policy_t target_policy = PSA_KEY_POLICY_INIT;
1814 psa_key_type_t target_type;
1815 size_t target_bits;
1816 psa_key_usage_t constraint_usage = constraint_usage_arg;
1817 psa_algorithm_t constraint_alg = constraint_alg_arg;
1818 psa_key_policy_t constraint = PSA_KEY_POLICY_INIT;
1819 psa_key_policy_t *p_constraint = NULL;
1820 psa_key_usage_t expected_usage = expected_usage_arg;
1821 psa_algorithm_t expected_alg = expected_alg_arg;
1822 uint8_t *export_buffer = NULL;
1823
1824 if( constraint_usage_arg != -1 )
1825 {
1826 p_constraint = &constraint;
1827 psa_key_policy_set_usage( p_constraint,
1828 constraint_usage, constraint_alg );
1829 }
1830
1831 PSA_ASSERT( psa_crypto_init( ) );
1832
1833 /* Populate the source slot. */
1834 PSA_ASSERT( psa_allocate_key( &source_handle ) );
1835 psa_key_policy_set_usage( &source_policy, source_usage, source_alg );
1836 PSA_ASSERT( psa_set_key_policy( source_handle, &source_policy ) );
1837 PSA_ASSERT( psa_import_key( source_handle, source_type,
1838 material->x, material->len ) );
1839 PSA_ASSERT( psa_get_key_information( source_handle, NULL, &source_bits ) );
1840
1841 /* Prepare the target slot. */
1842 PSA_ASSERT( psa_allocate_key( &target_handle ) );
1843 psa_key_policy_set_usage( &target_policy, target_usage, target_alg );
1844 PSA_ASSERT( psa_set_key_policy( target_handle, &target_policy ) );
1845 target_policy = psa_key_policy_init();
1846
1847 /* Copy the key. */
1848 PSA_ASSERT( psa_copy_key( source_handle, target_handle, p_constraint ) );
1849
1850 /* Destroy the source to ensure that this doesn't affect the target. */
1851 PSA_ASSERT( psa_destroy_key( source_handle ) );
1852
1853 /* Test that the target slot has the expected content and policy. */
1854 PSA_ASSERT( psa_get_key_information( target_handle,
1855 &target_type, &target_bits ) );
1856 TEST_EQUAL( source_type, target_type );
1857 TEST_EQUAL( source_bits, target_bits );
1858 PSA_ASSERT( psa_get_key_policy( target_handle, &target_policy ) );
1859 TEST_EQUAL( expected_usage, psa_key_policy_get_usage( &target_policy ) );
1860 TEST_EQUAL( expected_alg, psa_key_policy_get_algorithm( &target_policy ) );
1861 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1862 {
1863 size_t length;
1864 ASSERT_ALLOC( export_buffer, material->len );
1865 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
1866 material->len, &length ) );
1867 ASSERT_COMPARE( material->x, material->len,
1868 export_buffer, length );
1869 }
1870 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
1871 goto exit;
1872
1873 PSA_ASSERT( psa_close_key( target_handle ) );
1874
1875exit:
1876 mbedtls_psa_crypto_free( );
1877 mbedtls_free( export_buffer );
1878}
1879/* END_CASE */
1880
1881/* BEGIN_CASE */
1882void copy_fail( int source_usage_arg, int source_alg_arg,
1883 int type_arg, data_t *material,
1884 int target_usage_arg, int target_alg_arg,
1885 int constraint_usage_arg, int constraint_alg_arg,
1886 int expected_status_arg )
1887{
1888 /* Test copy failure into an empty slot. There is a test for copy failure
1889 * into an occupied slot in
1890 * test_suite_psa_crypto_slot_management.function. */
1891
1892 psa_key_usage_t source_usage = source_usage_arg;
1893 psa_algorithm_t source_alg = source_alg_arg;
1894 psa_key_handle_t source_handle = 0;
1895 psa_key_policy_t source_policy = PSA_KEY_POLICY_INIT;
1896 psa_key_type_t source_type = type_arg;
1897 size_t source_bits;
1898 psa_key_usage_t target_usage = target_usage_arg;
1899 psa_algorithm_t target_alg = target_alg_arg;
1900 psa_key_handle_t target_handle = 0;
1901 psa_key_policy_t target_policy = PSA_KEY_POLICY_INIT;
1902 psa_key_type_t target_type;
1903 size_t target_bits;
1904 psa_key_usage_t constraint_usage = constraint_usage_arg;
1905 psa_algorithm_t constraint_alg = constraint_alg_arg;
1906 psa_key_policy_t constraint = PSA_KEY_POLICY_INIT;
1907 psa_key_policy_t *p_constraint = NULL;
1908 psa_status_t expected_status = expected_status_arg;
1909
1910 if( constraint_usage_arg != -1 )
1911 {
1912 p_constraint = &constraint;
1913 psa_key_policy_set_usage( p_constraint,
1914 constraint_usage, constraint_alg );
1915 }
1916
1917 PSA_ASSERT( psa_crypto_init( ) );
1918
1919 /* Populate the source slot. */
1920 PSA_ASSERT( psa_allocate_key( &source_handle ) );
1921 psa_key_policy_set_usage( &source_policy, source_usage, source_alg );
1922 PSA_ASSERT( psa_set_key_policy( source_handle, &source_policy ) );
1923 PSA_ASSERT( psa_import_key( source_handle, source_type,
1924 material->x, material->len ) );
1925 PSA_ASSERT( psa_get_key_information( source_handle, NULL, &source_bits ) );
1926
1927 /* Prepare the target slot. */
1928 PSA_ASSERT( psa_allocate_key( &target_handle ) );
1929 psa_key_policy_set_usage( &target_policy, target_usage, target_alg );
1930 PSA_ASSERT( psa_set_key_policy( target_handle, &target_policy ) );
1931 target_policy = psa_key_policy_init();
1932
1933 /* Copy the key. */
1934 TEST_EQUAL( psa_copy_key( source_handle, target_handle, p_constraint ),
1935 expected_status );
1936
1937 /* Test that the target slot is unaffected. */
1938 TEST_EQUAL( psa_get_key_information( target_handle,
1939 &target_type, &target_bits ),
David Saadab4ecc272019-02-14 13:48:10 +02001940 PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001941 PSA_ASSERT( psa_get_key_policy( target_handle, &target_policy ) );
1942 TEST_EQUAL( target_usage, psa_key_policy_get_usage( &target_policy ) );
1943 TEST_EQUAL( target_alg, psa_key_policy_get_algorithm( &target_policy ) );
1944
1945exit:
1946 mbedtls_psa_crypto_free( );
1947}
1948/* END_CASE */
1949
1950/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001951void hash_operation_init( )
1952{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001953 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001954 /* Test each valid way of initializing the object, except for `= {0}`, as
1955 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1956 * though it's OK by the C standard. We could test for this, but we'd need
1957 * to supress the Clang warning for the test. */
1958 psa_hash_operation_t func = psa_hash_operation_init( );
1959 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1960 psa_hash_operation_t zero;
1961
1962 memset( &zero, 0, sizeof( zero ) );
1963
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001964 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001965 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1966 PSA_ERROR_BAD_STATE );
1967 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1968 PSA_ERROR_BAD_STATE );
1969 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1970 PSA_ERROR_BAD_STATE );
1971
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001972 /* A default hash operation should be abortable without error. */
1973 PSA_ASSERT( psa_hash_abort( &func ) );
1974 PSA_ASSERT( psa_hash_abort( &init ) );
1975 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001976}
1977/* END_CASE */
1978
1979/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001980void hash_setup( int alg_arg,
1981 int expected_status_arg )
1982{
1983 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001984 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001985 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001986 psa_status_t status;
1987
Gilles Peskine8817f612018-12-18 00:18:46 +01001988 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001989
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001990 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001991 psa_hash_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001992 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001993
1994exit:
1995 mbedtls_psa_crypto_free( );
1996}
1997/* END_CASE */
1998
1999/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002000void hash_bad_order( )
2001{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002002 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002003 unsigned char input[] = "";
2004 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002005 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002006 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2007 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2008 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002009 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002010 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002011 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002012
Gilles Peskine8817f612018-12-18 00:18:46 +01002013 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002014
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002015 /* Call setup twice in a row. */
2016 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2017 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2018 PSA_ERROR_BAD_STATE );
2019 PSA_ASSERT( psa_hash_abort( &operation ) );
2020
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002021 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002022 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002023 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002024 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002025
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002026 /* Call update after finish. */
2027 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2028 PSA_ASSERT( psa_hash_finish( &operation,
2029 hash, sizeof( hash ), &hash_len ) );
2030 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002031 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002032 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002033
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002034 /* Call verify without calling setup beforehand. */
2035 TEST_EQUAL( psa_hash_verify( &operation,
2036 valid_hash, sizeof( valid_hash ) ),
2037 PSA_ERROR_BAD_STATE );
2038 PSA_ASSERT( psa_hash_abort( &operation ) );
2039
2040 /* Call verify after finish. */
2041 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2042 PSA_ASSERT( psa_hash_finish( &operation,
2043 hash, sizeof( hash ), &hash_len ) );
2044 TEST_EQUAL( psa_hash_verify( &operation,
2045 valid_hash, sizeof( valid_hash ) ),
2046 PSA_ERROR_BAD_STATE );
2047 PSA_ASSERT( psa_hash_abort( &operation ) );
2048
2049 /* Call verify twice in a row. */
2050 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2051 PSA_ASSERT( psa_hash_verify( &operation,
2052 valid_hash, sizeof( valid_hash ) ) );
2053 TEST_EQUAL( psa_hash_verify( &operation,
2054 valid_hash, sizeof( valid_hash ) ),
2055 PSA_ERROR_BAD_STATE );
2056 PSA_ASSERT( psa_hash_abort( &operation ) );
2057
2058 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002059 TEST_EQUAL( psa_hash_finish( &operation,
2060 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002061 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002062 PSA_ASSERT( psa_hash_abort( &operation ) );
2063
2064 /* Call finish twice in a row. */
2065 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2066 PSA_ASSERT( psa_hash_finish( &operation,
2067 hash, sizeof( hash ), &hash_len ) );
2068 TEST_EQUAL( psa_hash_finish( &operation,
2069 hash, sizeof( hash ), &hash_len ),
2070 PSA_ERROR_BAD_STATE );
2071 PSA_ASSERT( psa_hash_abort( &operation ) );
2072
2073 /* Call finish after calling verify. */
2074 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2075 PSA_ASSERT( psa_hash_verify( &operation,
2076 valid_hash, sizeof( valid_hash ) ) );
2077 TEST_EQUAL( psa_hash_finish( &operation,
2078 hash, sizeof( hash ), &hash_len ),
2079 PSA_ERROR_BAD_STATE );
2080 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002081
2082exit:
2083 mbedtls_psa_crypto_free( );
2084}
2085/* END_CASE */
2086
itayzafrir27e69452018-11-01 14:26:34 +02002087/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2088void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002089{
2090 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002091 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2092 * appended to it */
2093 unsigned char hash[] = {
2094 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2095 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2096 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002097 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002098 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002099
Gilles Peskine8817f612018-12-18 00:18:46 +01002100 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002101
itayzafrir27e69452018-11-01 14:26:34 +02002102 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002103 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002104 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002105 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002106
itayzafrir27e69452018-11-01 14:26:34 +02002107 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002108 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002109 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002110 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002111
itayzafrir27e69452018-11-01 14:26:34 +02002112 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002113 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002114 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002115 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002116
itayzafrirec93d302018-10-18 18:01:10 +03002117exit:
2118 mbedtls_psa_crypto_free( );
2119}
2120/* END_CASE */
2121
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002122/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2123void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002124{
2125 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002126 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002127 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002128 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002129 size_t hash_len;
2130
Gilles Peskine8817f612018-12-18 00:18:46 +01002131 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002132
itayzafrir58028322018-10-25 10:22:01 +03002133 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002134 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002135 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002136 hash, expected_size - 1, &hash_len ),
2137 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002138
2139exit:
2140 mbedtls_psa_crypto_free( );
2141}
2142/* END_CASE */
2143
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002144/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2145void hash_clone_source_state( )
2146{
2147 psa_algorithm_t alg = PSA_ALG_SHA_256;
2148 unsigned char hash[PSA_HASH_MAX_SIZE];
2149 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2150 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2151 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2152 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2153 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2154 size_t hash_len;
2155
2156 PSA_ASSERT( psa_crypto_init( ) );
2157 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2158
2159 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2160 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2161 PSA_ASSERT( psa_hash_finish( &op_finished,
2162 hash, sizeof( hash ), &hash_len ) );
2163 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2164 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2165
2166 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2167 PSA_ERROR_BAD_STATE );
2168
2169 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2170 PSA_ASSERT( psa_hash_finish( &op_init,
2171 hash, sizeof( hash ), &hash_len ) );
2172 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2173 PSA_ASSERT( psa_hash_finish( &op_finished,
2174 hash, sizeof( hash ), &hash_len ) );
2175 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2176 PSA_ASSERT( psa_hash_finish( &op_aborted,
2177 hash, sizeof( hash ), &hash_len ) );
2178
2179exit:
2180 psa_hash_abort( &op_source );
2181 psa_hash_abort( &op_init );
2182 psa_hash_abort( &op_setup );
2183 psa_hash_abort( &op_finished );
2184 psa_hash_abort( &op_aborted );
2185 mbedtls_psa_crypto_free( );
2186}
2187/* END_CASE */
2188
2189/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2190void hash_clone_target_state( )
2191{
2192 psa_algorithm_t alg = PSA_ALG_SHA_256;
2193 unsigned char hash[PSA_HASH_MAX_SIZE];
2194 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2195 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2196 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2197 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2198 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2199 size_t hash_len;
2200
2201 PSA_ASSERT( psa_crypto_init( ) );
2202
2203 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2204 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2205 PSA_ASSERT( psa_hash_finish( &op_finished,
2206 hash, sizeof( hash ), &hash_len ) );
2207 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2208 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2209
2210 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2211 PSA_ASSERT( psa_hash_finish( &op_target,
2212 hash, sizeof( hash ), &hash_len ) );
2213
2214 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2215 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2216 PSA_ERROR_BAD_STATE );
2217 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2218 PSA_ERROR_BAD_STATE );
2219
2220exit:
2221 psa_hash_abort( &op_target );
2222 psa_hash_abort( &op_init );
2223 psa_hash_abort( &op_setup );
2224 psa_hash_abort( &op_finished );
2225 psa_hash_abort( &op_aborted );
2226 mbedtls_psa_crypto_free( );
2227}
2228/* END_CASE */
2229
itayzafrir58028322018-10-25 10:22:01 +03002230/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002231void mac_operation_init( )
2232{
Jaeden Amero252ef282019-02-15 14:05:35 +00002233 const uint8_t input[1] = { 0 };
2234
Jaeden Amero769ce272019-01-04 11:48:03 +00002235 /* Test each valid way of initializing the object, except for `= {0}`, as
2236 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2237 * though it's OK by the C standard. We could test for this, but we'd need
2238 * to supress the Clang warning for the test. */
2239 psa_mac_operation_t func = psa_mac_operation_init( );
2240 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2241 psa_mac_operation_t zero;
2242
2243 memset( &zero, 0, sizeof( zero ) );
2244
Jaeden Amero252ef282019-02-15 14:05:35 +00002245 /* A freshly-initialized MAC operation should not be usable. */
2246 TEST_EQUAL( psa_mac_update( &func,
2247 input, sizeof( input ) ),
2248 PSA_ERROR_BAD_STATE );
2249 TEST_EQUAL( psa_mac_update( &init,
2250 input, sizeof( input ) ),
2251 PSA_ERROR_BAD_STATE );
2252 TEST_EQUAL( psa_mac_update( &zero,
2253 input, sizeof( input ) ),
2254 PSA_ERROR_BAD_STATE );
2255
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002256 /* A default MAC operation should be abortable without error. */
2257 PSA_ASSERT( psa_mac_abort( &func ) );
2258 PSA_ASSERT( psa_mac_abort( &init ) );
2259 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002260}
2261/* END_CASE */
2262
2263/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002264void mac_setup( int key_type_arg,
2265 data_t *key,
2266 int alg_arg,
2267 int expected_status_arg )
2268{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002269 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002270 psa_key_type_t key_type = key_type_arg;
2271 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002272 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002273 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002274 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002275 psa_status_t status;
2276
Gilles Peskine8817f612018-12-18 00:18:46 +01002277 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002278
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002279 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002280 psa_key_policy_set_usage( &policy,
2281 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2282 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002283 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002284
Gilles Peskine8817f612018-12-18 00:18:46 +01002285 PSA_ASSERT( psa_import_key( handle, key_type,
2286 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002287
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002288 status = psa_mac_sign_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002289 psa_mac_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002290 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002291
2292exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002293 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002294 mbedtls_psa_crypto_free( );
2295}
2296/* END_CASE */
2297
2298/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002299void mac_bad_order( )
2300{
2301 psa_key_handle_t handle = 0;
2302 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2303 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2304 const uint8_t key[] = {
2305 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2306 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2307 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
2308 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
2309 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2310 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2311 size_t sign_mac_length = 0;
2312 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2313 const uint8_t verify_mac[] = {
2314 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2315 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2316 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2317
2318 PSA_ASSERT( psa_crypto_init( ) );
2319 PSA_ASSERT( psa_allocate_key( &handle ) );
2320 psa_key_policy_set_usage( &policy,
2321 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2322 alg );
2323 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
2324
2325 PSA_ASSERT( psa_import_key( handle, key_type,
2326 key, sizeof(key) ) );
2327
2328 /* Call update without calling setup beforehand. */
2329 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2330 PSA_ERROR_BAD_STATE );
2331 PSA_ASSERT( psa_mac_abort( &operation ) );
2332
2333 /* Call sign finish without calling setup beforehand. */
2334 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2335 &sign_mac_length),
2336 PSA_ERROR_BAD_STATE );
2337 PSA_ASSERT( psa_mac_abort( &operation ) );
2338
2339 /* Call verify finish without calling setup beforehand. */
2340 TEST_EQUAL( psa_mac_verify_finish( &operation,
2341 verify_mac, sizeof( verify_mac ) ),
2342 PSA_ERROR_BAD_STATE );
2343 PSA_ASSERT( psa_mac_abort( &operation ) );
2344
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002345 /* Call setup twice in a row. */
2346 PSA_ASSERT( psa_mac_sign_setup( &operation,
2347 handle, alg ) );
2348 TEST_EQUAL( psa_mac_sign_setup( &operation,
2349 handle, alg ),
2350 PSA_ERROR_BAD_STATE );
2351 PSA_ASSERT( psa_mac_abort( &operation ) );
2352
Jaeden Amero252ef282019-02-15 14:05:35 +00002353 /* Call update after sign finish. */
2354 PSA_ASSERT( psa_mac_sign_setup( &operation,
2355 handle, alg ) );
2356 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2357 PSA_ASSERT( psa_mac_sign_finish( &operation,
2358 sign_mac, sizeof( sign_mac ),
2359 &sign_mac_length ) );
2360 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2361 PSA_ERROR_BAD_STATE );
2362 PSA_ASSERT( psa_mac_abort( &operation ) );
2363
2364 /* Call update after verify finish. */
2365 PSA_ASSERT( psa_mac_verify_setup( &operation,
2366 handle, alg ) );
2367 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2368 PSA_ASSERT( psa_mac_verify_finish( &operation,
2369 verify_mac, sizeof( verify_mac ) ) );
2370 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2371 PSA_ERROR_BAD_STATE );
2372 PSA_ASSERT( psa_mac_abort( &operation ) );
2373
2374 /* Call sign finish twice in a row. */
2375 PSA_ASSERT( psa_mac_sign_setup( &operation,
2376 handle, alg ) );
2377 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2378 PSA_ASSERT( psa_mac_sign_finish( &operation,
2379 sign_mac, sizeof( sign_mac ),
2380 &sign_mac_length ) );
2381 TEST_EQUAL( psa_mac_sign_finish( &operation,
2382 sign_mac, sizeof( sign_mac ),
2383 &sign_mac_length ),
2384 PSA_ERROR_BAD_STATE );
2385 PSA_ASSERT( psa_mac_abort( &operation ) );
2386
2387 /* Call verify finish twice in a row. */
2388 PSA_ASSERT( psa_mac_verify_setup( &operation,
2389 handle, alg ) );
2390 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2391 PSA_ASSERT( psa_mac_verify_finish( &operation,
2392 verify_mac, sizeof( verify_mac ) ) );
2393 TEST_EQUAL( psa_mac_verify_finish( &operation,
2394 verify_mac, sizeof( verify_mac ) ),
2395 PSA_ERROR_BAD_STATE );
2396 PSA_ASSERT( psa_mac_abort( &operation ) );
2397
2398 /* Setup sign but try verify. */
2399 PSA_ASSERT( psa_mac_sign_setup( &operation,
2400 handle, alg ) );
2401 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2402 TEST_EQUAL( psa_mac_verify_finish( &operation,
2403 verify_mac, sizeof( verify_mac ) ),
2404 PSA_ERROR_BAD_STATE );
2405 PSA_ASSERT( psa_mac_abort( &operation ) );
2406
2407 /* Setup verify but try sign. */
2408 PSA_ASSERT( psa_mac_verify_setup( &operation,
2409 handle, alg ) );
2410 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2411 TEST_EQUAL( psa_mac_sign_finish( &operation,
2412 sign_mac, sizeof( sign_mac ),
2413 &sign_mac_length ),
2414 PSA_ERROR_BAD_STATE );
2415 PSA_ASSERT( psa_mac_abort( &operation ) );
2416
2417exit:
2418 mbedtls_psa_crypto_free( );
2419}
2420/* END_CASE */
2421
2422/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002423void mac_sign( int key_type_arg,
2424 data_t *key,
2425 int alg_arg,
2426 data_t *input,
2427 data_t *expected_mac )
2428{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002429 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002430 psa_key_type_t key_type = key_type_arg;
2431 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002432 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002433 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002434 /* Leave a little extra room in the output buffer. At the end of the
2435 * test, we'll check that the implementation didn't overwrite onto
2436 * this extra room. */
2437 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2438 size_t mac_buffer_size =
2439 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2440 size_t mac_length = 0;
2441
2442 memset( actual_mac, '+', sizeof( actual_mac ) );
2443 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2444 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2445
Gilles Peskine8817f612018-12-18 00:18:46 +01002446 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002447
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002448 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002449 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002450 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002451
Gilles Peskine8817f612018-12-18 00:18:46 +01002452 PSA_ASSERT( psa_import_key( handle, key_type,
2453 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002454
2455 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002456 PSA_ASSERT( psa_mac_sign_setup( &operation,
2457 handle, alg ) );
2458 PSA_ASSERT( psa_mac_update( &operation,
2459 input->x, input->len ) );
2460 PSA_ASSERT( psa_mac_sign_finish( &operation,
2461 actual_mac, mac_buffer_size,
2462 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002463
2464 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002465 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2466 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002467
2468 /* Verify that the end of the buffer is untouched. */
2469 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2470 sizeof( actual_mac ) - mac_length ) );
2471
2472exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002473 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002474 mbedtls_psa_crypto_free( );
2475}
2476/* END_CASE */
2477
2478/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002479void mac_verify( int key_type_arg,
2480 data_t *key,
2481 int alg_arg,
2482 data_t *input,
2483 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002484{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002485 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002486 psa_key_type_t key_type = key_type_arg;
2487 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002488 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002489 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002490
Gilles Peskine69c12672018-06-28 00:07:19 +02002491 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2492
Gilles Peskine8817f612018-12-18 00:18:46 +01002493 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002494
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002495 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002496 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002497 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad16036df908f2018-04-02 08:34:15 -07002498
Gilles Peskine8817f612018-12-18 00:18:46 +01002499 PSA_ASSERT( psa_import_key( handle, key_type,
2500 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002501
Gilles Peskine8817f612018-12-18 00:18:46 +01002502 PSA_ASSERT( psa_mac_verify_setup( &operation,
2503 handle, alg ) );
2504 PSA_ASSERT( psa_destroy_key( handle ) );
2505 PSA_ASSERT( psa_mac_update( &operation,
2506 input->x, input->len ) );
2507 PSA_ASSERT( psa_mac_verify_finish( &operation,
2508 expected_mac->x,
2509 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002510
2511exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002512 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002513 mbedtls_psa_crypto_free( );
2514}
2515/* END_CASE */
2516
2517/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002518void cipher_operation_init( )
2519{
Jaeden Ameroab439972019-02-15 14:12:05 +00002520 const uint8_t input[1] = { 0 };
2521 unsigned char output[1] = { 0 };
2522 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002523 /* Test each valid way of initializing the object, except for `= {0}`, as
2524 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2525 * though it's OK by the C standard. We could test for this, but we'd need
2526 * to supress the Clang warning for the test. */
2527 psa_cipher_operation_t func = psa_cipher_operation_init( );
2528 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2529 psa_cipher_operation_t zero;
2530
2531 memset( &zero, 0, sizeof( zero ) );
2532
Jaeden Ameroab439972019-02-15 14:12:05 +00002533 /* A freshly-initialized cipher operation should not be usable. */
2534 TEST_EQUAL( psa_cipher_update( &func,
2535 input, sizeof( input ),
2536 output, sizeof( output ),
2537 &output_length ),
2538 PSA_ERROR_BAD_STATE );
2539 TEST_EQUAL( psa_cipher_update( &init,
2540 input, sizeof( input ),
2541 output, sizeof( output ),
2542 &output_length ),
2543 PSA_ERROR_BAD_STATE );
2544 TEST_EQUAL( psa_cipher_update( &zero,
2545 input, sizeof( input ),
2546 output, sizeof( output ),
2547 &output_length ),
2548 PSA_ERROR_BAD_STATE );
2549
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002550 /* A default cipher operation should be abortable without error. */
2551 PSA_ASSERT( psa_cipher_abort( &func ) );
2552 PSA_ASSERT( psa_cipher_abort( &init ) );
2553 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002554}
2555/* END_CASE */
2556
2557/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002558void cipher_setup( int key_type_arg,
2559 data_t *key,
2560 int alg_arg,
2561 int expected_status_arg )
2562{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002563 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002564 psa_key_type_t key_type = key_type_arg;
2565 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002566 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002567 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002568 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002569 psa_status_t status;
2570
Gilles Peskine8817f612018-12-18 00:18:46 +01002571 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002572
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002573 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002574 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002575 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002576
Gilles Peskine8817f612018-12-18 00:18:46 +01002577 PSA_ASSERT( psa_import_key( handle, key_type,
2578 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002579
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002580 status = psa_cipher_encrypt_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002581 psa_cipher_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002582 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002583
2584exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002585 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002586 mbedtls_psa_crypto_free( );
2587}
2588/* END_CASE */
2589
2590/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00002591void cipher_bad_order( )
2592{
2593 psa_key_handle_t handle = 0;
2594 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2595 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
2596 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
2597 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2598 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2599 const uint8_t key[] = {
2600 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2601 0xaa, 0xaa, 0xaa, 0xaa };
2602 const uint8_t text[] = {
2603 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2604 0xbb, 0xbb, 0xbb, 0xbb };
2605 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2606 size_t length = 0;
2607
2608 PSA_ASSERT( psa_crypto_init( ) );
2609 PSA_ASSERT( psa_allocate_key( &handle ) );
2610 psa_key_policy_set_usage( &policy,
2611 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2612 alg );
2613 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
2614 PSA_ASSERT( psa_import_key( handle, key_type,
2615 key, sizeof(key) ) );
2616
2617
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002618 /* Call encrypt setup twice in a row. */
2619 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2620 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
2621 PSA_ERROR_BAD_STATE );
2622 PSA_ASSERT( psa_cipher_abort( &operation ) );
2623
2624 /* Call decrypt setup twice in a row. */
2625 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
2626 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
2627 PSA_ERROR_BAD_STATE );
2628 PSA_ASSERT( psa_cipher_abort( &operation ) );
2629
Jaeden Ameroab439972019-02-15 14:12:05 +00002630 /* Generate an IV without calling setup beforehand. */
2631 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2632 buffer, sizeof( buffer ),
2633 &length ),
2634 PSA_ERROR_BAD_STATE );
2635 PSA_ASSERT( psa_cipher_abort( &operation ) );
2636
2637 /* Generate an IV twice in a row. */
2638 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2639 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2640 buffer, sizeof( buffer ),
2641 &length ) );
2642 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2643 buffer, sizeof( buffer ),
2644 &length ),
2645 PSA_ERROR_BAD_STATE );
2646 PSA_ASSERT( psa_cipher_abort( &operation ) );
2647
2648 /* Generate an IV after it's already set. */
2649 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2650 PSA_ASSERT( psa_cipher_set_iv( &operation,
2651 iv, sizeof( iv ) ) );
2652 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2653 buffer, sizeof( buffer ),
2654 &length ),
2655 PSA_ERROR_BAD_STATE );
2656 PSA_ASSERT( psa_cipher_abort( &operation ) );
2657
2658 /* Set an IV without calling setup beforehand. */
2659 TEST_EQUAL( psa_cipher_set_iv( &operation,
2660 iv, sizeof( iv ) ),
2661 PSA_ERROR_BAD_STATE );
2662 PSA_ASSERT( psa_cipher_abort( &operation ) );
2663
2664 /* Set an IV after it's already set. */
2665 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2666 PSA_ASSERT( psa_cipher_set_iv( &operation,
2667 iv, sizeof( iv ) ) );
2668 TEST_EQUAL( psa_cipher_set_iv( &operation,
2669 iv, sizeof( iv ) ),
2670 PSA_ERROR_BAD_STATE );
2671 PSA_ASSERT( psa_cipher_abort( &operation ) );
2672
2673 /* Set an IV after it's already generated. */
2674 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2675 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2676 buffer, sizeof( buffer ),
2677 &length ) );
2678 TEST_EQUAL( psa_cipher_set_iv( &operation,
2679 iv, sizeof( iv ) ),
2680 PSA_ERROR_BAD_STATE );
2681 PSA_ASSERT( psa_cipher_abort( &operation ) );
2682
2683 /* Call update without calling setup beforehand. */
2684 TEST_EQUAL( psa_cipher_update( &operation,
2685 text, sizeof( text ),
2686 buffer, sizeof( buffer ),
2687 &length ),
2688 PSA_ERROR_BAD_STATE );
2689 PSA_ASSERT( psa_cipher_abort( &operation ) );
2690
2691 /* Call update without an IV where an IV is required. */
2692 TEST_EQUAL( psa_cipher_update( &operation,
2693 text, sizeof( text ),
2694 buffer, sizeof( buffer ),
2695 &length ),
2696 PSA_ERROR_BAD_STATE );
2697 PSA_ASSERT( psa_cipher_abort( &operation ) );
2698
2699 /* Call update after finish. */
2700 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2701 PSA_ASSERT( psa_cipher_set_iv( &operation,
2702 iv, sizeof( iv ) ) );
2703 PSA_ASSERT( psa_cipher_finish( &operation,
2704 buffer, sizeof( buffer ), &length ) );
2705 TEST_EQUAL( psa_cipher_update( &operation,
2706 text, sizeof( text ),
2707 buffer, sizeof( buffer ),
2708 &length ),
2709 PSA_ERROR_BAD_STATE );
2710 PSA_ASSERT( psa_cipher_abort( &operation ) );
2711
2712 /* Call finish without calling setup beforehand. */
2713 TEST_EQUAL( psa_cipher_finish( &operation,
2714 buffer, sizeof( buffer ), &length ),
2715 PSA_ERROR_BAD_STATE );
2716 PSA_ASSERT( psa_cipher_abort( &operation ) );
2717
2718 /* Call finish without an IV where an IV is required. */
2719 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2720 /* Not calling update means we are encrypting an empty buffer, which is OK
2721 * for cipher modes with padding. */
2722 TEST_EQUAL( psa_cipher_finish( &operation,
2723 buffer, sizeof( buffer ), &length ),
2724 PSA_ERROR_BAD_STATE );
2725 PSA_ASSERT( psa_cipher_abort( &operation ) );
2726
2727 /* Call finish twice in a row. */
2728 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2729 PSA_ASSERT( psa_cipher_set_iv( &operation,
2730 iv, sizeof( iv ) ) );
2731 PSA_ASSERT( psa_cipher_finish( &operation,
2732 buffer, sizeof( buffer ), &length ) );
2733 TEST_EQUAL( psa_cipher_finish( &operation,
2734 buffer, sizeof( buffer ), &length ),
2735 PSA_ERROR_BAD_STATE );
2736 PSA_ASSERT( psa_cipher_abort( &operation ) );
2737
2738exit:
2739 mbedtls_psa_crypto_free( );
2740}
2741/* END_CASE */
2742
2743/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002744void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002745 data_t *key,
2746 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002747 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002748{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002749 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002750 psa_status_t status;
2751 psa_key_type_t key_type = key_type_arg;
2752 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002753 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002754 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002755 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002756 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002757 size_t output_buffer_size = 0;
2758 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002759 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002760 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002761 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002762
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002763 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2764 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002765
Gilles Peskine8817f612018-12-18 00:18:46 +01002766 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002767
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002768 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002769 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002770 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002771
Gilles Peskine8817f612018-12-18 00:18:46 +01002772 PSA_ASSERT( psa_import_key( handle, key_type,
2773 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002774
Gilles Peskine8817f612018-12-18 00:18:46 +01002775 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2776 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002777
Gilles Peskine8817f612018-12-18 00:18:46 +01002778 PSA_ASSERT( psa_cipher_set_iv( &operation,
2779 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002780 output_buffer_size = ( (size_t) input->len +
2781 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002782 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002783
Gilles Peskine8817f612018-12-18 00:18:46 +01002784 PSA_ASSERT( psa_cipher_update( &operation,
2785 input->x, input->len,
2786 output, output_buffer_size,
2787 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002788 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002789 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002790 output + total_output_length,
2791 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002792 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002793 total_output_length += function_output_length;
2794
Gilles Peskinefe11b722018-12-18 00:24:04 +01002795 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002796 if( expected_status == PSA_SUCCESS )
2797 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002798 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002799 ASSERT_COMPARE( expected_output->x, expected_output->len,
2800 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002801 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002802
Gilles Peskine50e586b2018-06-08 14:28:46 +02002803exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002804 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002805 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002806 mbedtls_psa_crypto_free( );
2807}
2808/* END_CASE */
2809
2810/* BEGIN_CASE */
2811void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002812 data_t *key,
2813 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002814 int first_part_size_arg,
2815 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002816 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002817{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002818 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002819 psa_key_type_t key_type = key_type_arg;
2820 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002821 size_t first_part_size = first_part_size_arg;
2822 size_t output1_length = output1_length_arg;
2823 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002824 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002825 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002826 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002827 size_t output_buffer_size = 0;
2828 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002829 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002830 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002831 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002832
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002833 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2834 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002835
Gilles Peskine8817f612018-12-18 00:18:46 +01002836 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002837
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002838 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002839 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002840 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002841
Gilles Peskine8817f612018-12-18 00:18:46 +01002842 PSA_ASSERT( psa_import_key( handle, key_type,
2843 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002844
Gilles Peskine8817f612018-12-18 00:18:46 +01002845 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2846 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002847
Gilles Peskine8817f612018-12-18 00:18:46 +01002848 PSA_ASSERT( psa_cipher_set_iv( &operation,
2849 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002850 output_buffer_size = ( (size_t) input->len +
2851 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002852 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002853
Gilles Peskinee0866522019-02-19 19:44:00 +01002854 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002855 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2856 output, output_buffer_size,
2857 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002858 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002859 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002860 PSA_ASSERT( psa_cipher_update( &operation,
2861 input->x + first_part_size,
2862 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002863 output + total_output_length,
2864 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002865 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002866 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002867 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002868 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002869 output + total_output_length,
2870 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002871 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002872 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002873 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002874
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002875 ASSERT_COMPARE( expected_output->x, expected_output->len,
2876 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002877
2878exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002879 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002880 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002881 mbedtls_psa_crypto_free( );
2882}
2883/* END_CASE */
2884
2885/* BEGIN_CASE */
2886void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002887 data_t *key,
2888 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002889 int first_part_size_arg,
2890 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002891 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002892{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002893 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002894
2895 psa_key_type_t key_type = key_type_arg;
2896 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002897 size_t first_part_size = first_part_size_arg;
2898 size_t output1_length = output1_length_arg;
2899 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002900 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002901 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002902 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002903 size_t output_buffer_size = 0;
2904 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002905 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002906 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002907 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002908
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002909 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2910 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002911
Gilles Peskine8817f612018-12-18 00:18:46 +01002912 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002913
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002914 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002915 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002916 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002917
Gilles Peskine8817f612018-12-18 00:18:46 +01002918 PSA_ASSERT( psa_import_key( handle, key_type,
2919 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002920
Gilles Peskine8817f612018-12-18 00:18:46 +01002921 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2922 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002923
Gilles Peskine8817f612018-12-18 00:18:46 +01002924 PSA_ASSERT( psa_cipher_set_iv( &operation,
2925 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002926
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002927 output_buffer_size = ( (size_t) input->len +
2928 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002929 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002930
Gilles Peskinee0866522019-02-19 19:44:00 +01002931 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002932 PSA_ASSERT( psa_cipher_update( &operation,
2933 input->x, first_part_size,
2934 output, output_buffer_size,
2935 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002936 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002937 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002938 PSA_ASSERT( psa_cipher_update( &operation,
2939 input->x + first_part_size,
2940 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002941 output + total_output_length,
2942 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002943 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002944 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002945 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002946 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002947 output + total_output_length,
2948 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002949 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002950 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002951 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002952
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002953 ASSERT_COMPARE( expected_output->x, expected_output->len,
2954 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002955
2956exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002957 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002958 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002959 mbedtls_psa_crypto_free( );
2960}
2961/* END_CASE */
2962
Gilles Peskine50e586b2018-06-08 14:28:46 +02002963/* BEGIN_CASE */
2964void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002965 data_t *key,
2966 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002967 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002968{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002969 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002970 psa_status_t status;
2971 psa_key_type_t key_type = key_type_arg;
2972 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002973 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002974 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002975 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002976 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002977 size_t output_buffer_size = 0;
2978 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002979 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002980 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002981 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002982
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002983 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2984 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002985
Gilles Peskine8817f612018-12-18 00:18:46 +01002986 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002987
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002988 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002989 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002990 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002991
Gilles Peskine8817f612018-12-18 00:18:46 +01002992 PSA_ASSERT( psa_import_key( handle, key_type,
2993 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002994
Gilles Peskine8817f612018-12-18 00:18:46 +01002995 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2996 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002997
Gilles Peskine8817f612018-12-18 00:18:46 +01002998 PSA_ASSERT( psa_cipher_set_iv( &operation,
2999 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003000
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003001 output_buffer_size = ( (size_t) input->len +
3002 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003003 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003004
Gilles Peskine8817f612018-12-18 00:18:46 +01003005 PSA_ASSERT( psa_cipher_update( &operation,
3006 input->x, input->len,
3007 output, output_buffer_size,
3008 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003009 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003010 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003011 output + total_output_length,
3012 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003013 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003014 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003015 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003016
3017 if( expected_status == PSA_SUCCESS )
3018 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003019 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003020 ASSERT_COMPARE( expected_output->x, expected_output->len,
3021 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003022 }
3023
Gilles Peskine50e586b2018-06-08 14:28:46 +02003024exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003025 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003026 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003027 mbedtls_psa_crypto_free( );
3028}
3029/* END_CASE */
3030
Gilles Peskine50e586b2018-06-08 14:28:46 +02003031/* BEGIN_CASE */
3032void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003033 data_t *key,
3034 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003035{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003036 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003037 psa_key_type_t key_type = key_type_arg;
3038 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003039 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003040 size_t iv_size = 16;
3041 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003042 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003043 size_t output1_size = 0;
3044 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003045 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003046 size_t output2_size = 0;
3047 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003048 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003049 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3050 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003051 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003052
Gilles Peskine8817f612018-12-18 00:18:46 +01003053 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003054
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003055 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003056 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003057 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003058
Gilles Peskine8817f612018-12-18 00:18:46 +01003059 PSA_ASSERT( psa_import_key( handle, key_type,
3060 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003061
Gilles Peskine8817f612018-12-18 00:18:46 +01003062 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3063 handle, alg ) );
3064 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3065 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003066
Gilles Peskine8817f612018-12-18 00:18:46 +01003067 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3068 iv, iv_size,
3069 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003070 output1_size = ( (size_t) input->len +
3071 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003072 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003073
Gilles Peskine8817f612018-12-18 00:18:46 +01003074 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3075 output1, output1_size,
3076 &output1_length ) );
3077 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003078 output1 + output1_length,
3079 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003080 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003081
Gilles Peskine048b7f02018-06-08 14:20:49 +02003082 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003083
Gilles Peskine8817f612018-12-18 00:18:46 +01003084 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003085
3086 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003087 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003088
Gilles Peskine8817f612018-12-18 00:18:46 +01003089 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3090 iv, iv_length ) );
3091 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3092 output2, output2_size,
3093 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003094 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003095 PSA_ASSERT( psa_cipher_finish( &operation2,
3096 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003097 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003098 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003099
Gilles Peskine048b7f02018-06-08 14:20:49 +02003100 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003101
Gilles Peskine8817f612018-12-18 00:18:46 +01003102 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003103
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003104 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003105
3106exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003107 mbedtls_free( output1 );
3108 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003109 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03003110 mbedtls_psa_crypto_free( );
3111}
3112/* END_CASE */
3113
3114/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003115void cipher_verify_output_multipart( int alg_arg,
3116 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003117 data_t *key,
3118 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003119 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003120{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003121 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003122 psa_key_type_t key_type = key_type_arg;
3123 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003124 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003125 unsigned char iv[16] = {0};
3126 size_t iv_size = 16;
3127 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003128 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003129 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003130 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003131 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003132 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003133 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003134 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003135 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3136 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003137 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003138
Gilles Peskine8817f612018-12-18 00:18:46 +01003139 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003140
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003141 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003142 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003143 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003144
Gilles Peskine8817f612018-12-18 00:18:46 +01003145 PSA_ASSERT( psa_import_key( handle, key_type,
3146 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003147
Gilles Peskine8817f612018-12-18 00:18:46 +01003148 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3149 handle, alg ) );
3150 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3151 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003152
Gilles Peskine8817f612018-12-18 00:18:46 +01003153 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3154 iv, iv_size,
3155 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003156 output1_buffer_size = ( (size_t) input->len +
3157 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003158 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003159
Gilles Peskinee0866522019-02-19 19:44:00 +01003160 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003161
Gilles Peskine8817f612018-12-18 00:18:46 +01003162 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3163 output1, output1_buffer_size,
3164 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003165 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003166
Gilles Peskine8817f612018-12-18 00:18:46 +01003167 PSA_ASSERT( psa_cipher_update( &operation1,
3168 input->x + first_part_size,
3169 input->len - first_part_size,
3170 output1, output1_buffer_size,
3171 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003172 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003173
Gilles Peskine8817f612018-12-18 00:18:46 +01003174 PSA_ASSERT( psa_cipher_finish( &operation1,
3175 output1 + output1_length,
3176 output1_buffer_size - output1_length,
3177 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003178 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003179
Gilles Peskine8817f612018-12-18 00:18:46 +01003180 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003181
Gilles Peskine048b7f02018-06-08 14:20:49 +02003182 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003183 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003184
Gilles Peskine8817f612018-12-18 00:18:46 +01003185 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3186 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003187
Gilles Peskine8817f612018-12-18 00:18:46 +01003188 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3189 output2, output2_buffer_size,
3190 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003191 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003192
Gilles Peskine8817f612018-12-18 00:18:46 +01003193 PSA_ASSERT( psa_cipher_update( &operation2,
3194 output1 + first_part_size,
3195 output1_length - first_part_size,
3196 output2, output2_buffer_size,
3197 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003198 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003199
Gilles Peskine8817f612018-12-18 00:18:46 +01003200 PSA_ASSERT( psa_cipher_finish( &operation2,
3201 output2 + output2_length,
3202 output2_buffer_size - output2_length,
3203 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003204 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003205
Gilles Peskine8817f612018-12-18 00:18:46 +01003206 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003207
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003208 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003209
3210exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003211 mbedtls_free( output1 );
3212 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003213 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003214 mbedtls_psa_crypto_free( );
3215}
3216/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003217
Gilles Peskine20035e32018-02-03 22:44:14 +01003218/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003219void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003220 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003221 data_t *nonce,
3222 data_t *additional_data,
3223 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003224 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003225{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003226 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003227 psa_key_type_t key_type = key_type_arg;
3228 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003229 unsigned char *output_data = NULL;
3230 size_t output_size = 0;
3231 size_t output_length = 0;
3232 unsigned char *output_data2 = NULL;
3233 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003234 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003235 psa_status_t expected_result = expected_result_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003236 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003237
Gilles Peskine4abf7412018-06-18 16:35:34 +02003238 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003239 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003240
Gilles Peskine8817f612018-12-18 00:18:46 +01003241 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003242
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003243 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003244 psa_key_policy_set_usage( &policy,
3245 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
3246 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003247 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003248
Gilles Peskine8817f612018-12-18 00:18:46 +01003249 PSA_ASSERT( psa_import_key( handle, key_type,
3250 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003251
Gilles Peskinefe11b722018-12-18 00:24:04 +01003252 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3253 nonce->x, nonce->len,
3254 additional_data->x,
3255 additional_data->len,
3256 input_data->x, input_data->len,
3257 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003258 &output_length ),
3259 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003260
3261 if( PSA_SUCCESS == expected_result )
3262 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003263 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003264
Gilles Peskinefe11b722018-12-18 00:24:04 +01003265 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3266 nonce->x, nonce->len,
3267 additional_data->x,
3268 additional_data->len,
3269 output_data, output_length,
3270 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003271 &output_length2 ),
3272 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003273
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003274 ASSERT_COMPARE( input_data->x, input_data->len,
3275 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003276 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003277
Gilles Peskinea1cac842018-06-11 19:33:02 +02003278exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003279 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003280 mbedtls_free( output_data );
3281 mbedtls_free( output_data2 );
3282 mbedtls_psa_crypto_free( );
3283}
3284/* END_CASE */
3285
3286/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003287void aead_encrypt( int key_type_arg, data_t *key_data,
3288 int alg_arg,
3289 data_t *nonce,
3290 data_t *additional_data,
3291 data_t *input_data,
3292 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003293{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003294 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003295 psa_key_type_t key_type = key_type_arg;
3296 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003297 unsigned char *output_data = NULL;
3298 size_t output_size = 0;
3299 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003300 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00003301 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003302
Gilles Peskine4abf7412018-06-18 16:35:34 +02003303 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003304 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003305
Gilles Peskine8817f612018-12-18 00:18:46 +01003306 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003307
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003308 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003309 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003310 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003311
Gilles Peskine8817f612018-12-18 00:18:46 +01003312 PSA_ASSERT( psa_import_key( handle, key_type,
3313 key_data->x,
3314 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003315
Gilles Peskine8817f612018-12-18 00:18:46 +01003316 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3317 nonce->x, nonce->len,
3318 additional_data->x, additional_data->len,
3319 input_data->x, input_data->len,
3320 output_data, output_size,
3321 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003322
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003323 ASSERT_COMPARE( expected_result->x, expected_result->len,
3324 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003325
Gilles Peskinea1cac842018-06-11 19:33:02 +02003326exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003327 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003328 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003329 mbedtls_psa_crypto_free( );
3330}
3331/* END_CASE */
3332
3333/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003334void aead_decrypt( int key_type_arg, data_t *key_data,
3335 int alg_arg,
3336 data_t *nonce,
3337 data_t *additional_data,
3338 data_t *input_data,
3339 data_t *expected_data,
3340 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003341{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003342 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003343 psa_key_type_t key_type = key_type_arg;
3344 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003345 unsigned char *output_data = NULL;
3346 size_t output_size = 0;
3347 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003348 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00003349 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003350 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003351
Gilles Peskine4abf7412018-06-18 16:35:34 +02003352 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003353 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003354
Gilles Peskine8817f612018-12-18 00:18:46 +01003355 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003356
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003357 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003358 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003359 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003360
Gilles Peskine8817f612018-12-18 00:18:46 +01003361 PSA_ASSERT( psa_import_key( handle, key_type,
3362 key_data->x,
3363 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003364
Gilles Peskinefe11b722018-12-18 00:24:04 +01003365 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3366 nonce->x, nonce->len,
3367 additional_data->x,
3368 additional_data->len,
3369 input_data->x, input_data->len,
3370 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003371 &output_length ),
3372 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003373
Gilles Peskine2d277862018-06-18 15:41:12 +02003374 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003375 ASSERT_COMPARE( expected_data->x, expected_data->len,
3376 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003377
Gilles Peskinea1cac842018-06-11 19:33:02 +02003378exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003379 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003380 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003381 mbedtls_psa_crypto_free( );
3382}
3383/* END_CASE */
3384
3385/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003386void signature_size( int type_arg,
3387 int bits,
3388 int alg_arg,
3389 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003390{
3391 psa_key_type_t type = type_arg;
3392 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02003393 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003394 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003395exit:
3396 ;
3397}
3398/* END_CASE */
3399
3400/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003401void sign_deterministic( int key_type_arg, data_t *key_data,
3402 int alg_arg, data_t *input_data,
3403 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003404{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003405 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003406 psa_key_type_t key_type = key_type_arg;
3407 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003408 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003409 unsigned char *signature = NULL;
3410 size_t signature_size;
3411 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00003412 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003413
Gilles Peskine8817f612018-12-18 00:18:46 +01003414 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003415
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003416 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003417 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003418 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003419
Gilles Peskine8817f612018-12-18 00:18:46 +01003420 PSA_ASSERT( psa_import_key( handle, key_type,
3421 key_data->x,
3422 key_data->len ) );
3423 PSA_ASSERT( psa_get_key_information( handle,
3424 NULL,
3425 &key_bits ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003426
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003427 /* Allocate a buffer which has the size advertized by the
3428 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003429 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3430 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003431 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02003432 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003433 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003434
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003435 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003436 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3437 input_data->x, input_data->len,
3438 signature, signature_size,
3439 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003440 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003441 ASSERT_COMPARE( output_data->x, output_data->len,
3442 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003443
3444exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003445 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01003446 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01003447 mbedtls_psa_crypto_free( );
3448}
3449/* END_CASE */
3450
3451/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003452void sign_fail( int key_type_arg, data_t *key_data,
3453 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003454 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003455{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003456 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003457 psa_key_type_t key_type = key_type_arg;
3458 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003459 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003460 psa_status_t actual_status;
3461 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003462 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003463 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00003464 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003465
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003466 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003467
Gilles Peskine8817f612018-12-18 00:18:46 +01003468 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003469
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003470 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003471 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003472 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003473
Gilles Peskine8817f612018-12-18 00:18:46 +01003474 PSA_ASSERT( psa_import_key( handle, key_type,
3475 key_data->x,
3476 key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003477
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003478 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003479 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01003480 signature, signature_size,
3481 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003482 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003483 /* The value of *signature_length is unspecified on error, but
3484 * whatever it is, it should be less than signature_size, so that
3485 * if the caller tries to read *signature_length bytes without
3486 * checking the error code then they don't overflow a buffer. */
3487 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003488
3489exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003490 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01003491 mbedtls_free( signature );
3492 mbedtls_psa_crypto_free( );
3493}
3494/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003495
3496/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003497void sign_verify( int key_type_arg, data_t *key_data,
3498 int alg_arg, data_t *input_data )
3499{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003500 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02003501 psa_key_type_t key_type = key_type_arg;
3502 psa_algorithm_t alg = alg_arg;
3503 size_t key_bits;
3504 unsigned char *signature = NULL;
3505 size_t signature_size;
3506 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00003507 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003508
Gilles Peskine8817f612018-12-18 00:18:46 +01003509 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003510
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003511 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003512 psa_key_policy_set_usage( &policy,
3513 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
3514 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003515 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003516
Gilles Peskine8817f612018-12-18 00:18:46 +01003517 PSA_ASSERT( psa_import_key( handle, key_type,
3518 key_data->x,
3519 key_data->len ) );
3520 PSA_ASSERT( psa_get_key_information( handle,
3521 NULL,
3522 &key_bits ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003523
3524 /* Allocate a buffer which has the size advertized by the
3525 * library. */
3526 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3527 key_bits, alg );
3528 TEST_ASSERT( signature_size != 0 );
3529 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003530 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003531
3532 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003533 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3534 input_data->x, input_data->len,
3535 signature, signature_size,
3536 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003537 /* Check that the signature length looks sensible. */
3538 TEST_ASSERT( signature_length <= signature_size );
3539 TEST_ASSERT( signature_length > 0 );
3540
3541 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003542 PSA_ASSERT( psa_asymmetric_verify(
3543 handle, alg,
3544 input_data->x, input_data->len,
3545 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003546
3547 if( input_data->len != 0 )
3548 {
3549 /* Flip a bit in the input and verify that the signature is now
3550 * detected as invalid. Flip a bit at the beginning, not at the end,
3551 * because ECDSA may ignore the last few bits of the input. */
3552 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003553 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
3554 input_data->x, input_data->len,
3555 signature, signature_length ),
3556 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003557 }
3558
3559exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003560 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003561 mbedtls_free( signature );
3562 mbedtls_psa_crypto_free( );
3563}
3564/* END_CASE */
3565
3566/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003567void asymmetric_verify( int key_type_arg, data_t *key_data,
3568 int alg_arg, data_t *hash_data,
3569 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003570{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003571 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003572 psa_key_type_t key_type = key_type_arg;
3573 psa_algorithm_t alg = alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003574 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003575
Gilles Peskine69c12672018-06-28 00:07:19 +02003576 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3577
Gilles Peskine8817f612018-12-18 00:18:46 +01003578 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003579
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003580 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003581 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003582 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
itayzafrir5c753392018-05-08 11:18:38 +03003583
Gilles Peskine8817f612018-12-18 00:18:46 +01003584 PSA_ASSERT( psa_import_key( handle, key_type,
3585 key_data->x,
3586 key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003587
Gilles Peskine8817f612018-12-18 00:18:46 +01003588 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3589 hash_data->x, hash_data->len,
3590 signature_data->x,
3591 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003592exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003593 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03003594 mbedtls_psa_crypto_free( );
3595}
3596/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003597
3598/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003599void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3600 int alg_arg, data_t *hash_data,
3601 data_t *signature_data,
3602 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003603{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003604 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003605 psa_key_type_t key_type = key_type_arg;
3606 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003607 psa_status_t actual_status;
3608 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003609 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003610
Gilles Peskine8817f612018-12-18 00:18:46 +01003611 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003612
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003613 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003614 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003615 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003616
Gilles Peskine8817f612018-12-18 00:18:46 +01003617 PSA_ASSERT( psa_import_key( handle, key_type,
3618 key_data->x,
3619 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003620
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003621 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003622 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003623 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003624 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003625
Gilles Peskinefe11b722018-12-18 00:24:04 +01003626 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003627
3628exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003629 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003630 mbedtls_psa_crypto_free( );
3631}
3632/* END_CASE */
3633
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003634/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003635void asymmetric_encrypt( int key_type_arg,
3636 data_t *key_data,
3637 int alg_arg,
3638 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003639 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003640 int expected_output_length_arg,
3641 int expected_status_arg )
3642{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003643 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003644 psa_key_type_t key_type = key_type_arg;
3645 psa_algorithm_t alg = alg_arg;
3646 size_t expected_output_length = expected_output_length_arg;
3647 size_t key_bits;
3648 unsigned char *output = NULL;
3649 size_t output_size;
3650 size_t output_length = ~0;
3651 psa_status_t actual_status;
3652 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003653 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003654
Gilles Peskine8817f612018-12-18 00:18:46 +01003655 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003656
Gilles Peskine656896e2018-06-29 19:12:28 +02003657 /* Import the key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003658 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003659 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003660 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
3661 PSA_ASSERT( psa_import_key( handle, key_type,
3662 key_data->x,
3663 key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003664
3665 /* Determine the maximum output length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003666 PSA_ASSERT( psa_get_key_information( handle,
3667 NULL,
3668 &key_bits ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003669 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003670 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003671
3672 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003673 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003674 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003675 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003676 output, output_size,
3677 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003678 TEST_EQUAL( actual_status, expected_status );
3679 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003680
Gilles Peskine68428122018-06-30 18:42:41 +02003681 /* If the label is empty, the test framework puts a non-null pointer
3682 * in label->x. Test that a null pointer works as well. */
3683 if( label->len == 0 )
3684 {
3685 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003686 if( output_size != 0 )
3687 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003688 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003689 input_data->x, input_data->len,
3690 NULL, label->len,
3691 output, output_size,
3692 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003693 TEST_EQUAL( actual_status, expected_status );
3694 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003695 }
3696
Gilles Peskine656896e2018-06-29 19:12:28 +02003697exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003698 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003699 mbedtls_free( output );
3700 mbedtls_psa_crypto_free( );
3701}
3702/* END_CASE */
3703
3704/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003705void asymmetric_encrypt_decrypt( int key_type_arg,
3706 data_t *key_data,
3707 int alg_arg,
3708 data_t *input_data,
3709 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003710{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003711 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003712 psa_key_type_t key_type = key_type_arg;
3713 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003714 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003715 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003716 size_t output_size;
3717 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003718 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003719 size_t output2_size;
3720 size_t output2_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003721 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003722
Gilles Peskine8817f612018-12-18 00:18:46 +01003723 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003724
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003725 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003726 psa_key_policy_set_usage( &policy,
3727 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003728 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003729 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003730
Gilles Peskine8817f612018-12-18 00:18:46 +01003731 PSA_ASSERT( psa_import_key( handle, key_type,
3732 key_data->x,
3733 key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003734
3735 /* Determine the maximum ciphertext length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003736 PSA_ASSERT( psa_get_key_information( handle,
3737 NULL,
3738 &key_bits ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003739 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003740 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003741 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003742 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003743
Gilles Peskineeebd7382018-06-08 18:11:54 +02003744 /* We test encryption by checking that encrypt-then-decrypt gives back
3745 * the original plaintext because of the non-optional random
3746 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003747 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3748 input_data->x, input_data->len,
3749 label->x, label->len,
3750 output, output_size,
3751 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003752 /* We don't know what ciphertext length to expect, but check that
3753 * it looks sensible. */
3754 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003755
Gilles Peskine8817f612018-12-18 00:18:46 +01003756 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3757 output, output_length,
3758 label->x, label->len,
3759 output2, output2_size,
3760 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003761 ASSERT_COMPARE( input_data->x, input_data->len,
3762 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003763
3764exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003765 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003766 mbedtls_free( output );
3767 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003768 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003769}
3770/* END_CASE */
3771
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003772/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003773void asymmetric_decrypt( int key_type_arg,
3774 data_t *key_data,
3775 int alg_arg,
3776 data_t *input_data,
3777 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003778 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003779{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003780 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003781 psa_key_type_t key_type = key_type_arg;
3782 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003783 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003784 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003785 size_t output_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003786 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003787
Jaeden Amero412654a2019-02-06 12:57:46 +00003788 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003789 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003790
Gilles Peskine8817f612018-12-18 00:18:46 +01003791 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003792
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003793 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003794 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003795 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003796
Gilles Peskine8817f612018-12-18 00:18:46 +01003797 PSA_ASSERT( psa_import_key( handle, key_type,
3798 key_data->x,
3799 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003800
Gilles Peskine8817f612018-12-18 00:18:46 +01003801 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3802 input_data->x, input_data->len,
3803 label->x, label->len,
3804 output,
3805 output_size,
3806 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003807 ASSERT_COMPARE( expected_data->x, expected_data->len,
3808 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003809
Gilles Peskine68428122018-06-30 18:42:41 +02003810 /* If the label is empty, the test framework puts a non-null pointer
3811 * in label->x. Test that a null pointer works as well. */
3812 if( label->len == 0 )
3813 {
3814 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003815 if( output_size != 0 )
3816 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003817 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3818 input_data->x, input_data->len,
3819 NULL, label->len,
3820 output,
3821 output_size,
3822 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003823 ASSERT_COMPARE( expected_data->x, expected_data->len,
3824 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003825 }
3826
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003827exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003828 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003829 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003830 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003831}
3832/* END_CASE */
3833
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003834/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003835void asymmetric_decrypt_fail( int key_type_arg,
3836 data_t *key_data,
3837 int alg_arg,
3838 data_t *input_data,
3839 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00003840 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02003841 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003842{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003843 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003844 psa_key_type_t key_type = key_type_arg;
3845 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003846 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00003847 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003848 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003849 psa_status_t actual_status;
3850 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003851 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003852
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003853 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003854
Gilles Peskine8817f612018-12-18 00:18:46 +01003855 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003856
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003857 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003858 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003859 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003860
Gilles Peskine8817f612018-12-18 00:18:46 +01003861 PSA_ASSERT( psa_import_key( handle, key_type,
3862 key_data->x,
3863 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003864
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003865 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003866 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003867 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003868 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003869 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003870 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003871 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003872
Gilles Peskine68428122018-06-30 18:42:41 +02003873 /* If the label is empty, the test framework puts a non-null pointer
3874 * in label->x. Test that a null pointer works as well. */
3875 if( label->len == 0 )
3876 {
3877 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003878 if( output_size != 0 )
3879 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003880 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003881 input_data->x, input_data->len,
3882 NULL, label->len,
3883 output, output_size,
3884 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003885 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003886 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003887 }
3888
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003889exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003890 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02003891 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003892 mbedtls_psa_crypto_free( );
3893}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003894/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003895
3896/* BEGIN_CASE */
Jaeden Amerod94d6712019-01-04 14:11:48 +00003897void crypto_generator_init( )
3898{
3899 /* Test each valid way of initializing the object, except for `= {0}`, as
3900 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3901 * though it's OK by the C standard. We could test for this, but we'd need
3902 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003903 size_t capacity;
Jaeden Amerod94d6712019-01-04 14:11:48 +00003904 psa_crypto_generator_t func = psa_crypto_generator_init( );
3905 psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT;
3906 psa_crypto_generator_t zero;
3907
3908 memset( &zero, 0, sizeof( zero ) );
3909
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003910 /* A default generator should not be able to report its capacity. */
3911 TEST_EQUAL( psa_get_generator_capacity( &func, &capacity ),
3912 PSA_ERROR_BAD_STATE );
3913 TEST_EQUAL( psa_get_generator_capacity( &init, &capacity ),
3914 PSA_ERROR_BAD_STATE );
3915 TEST_EQUAL( psa_get_generator_capacity( &zero, &capacity ),
3916 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003917
3918 /* A default generator should be abortable without error. */
3919 PSA_ASSERT( psa_generator_abort(&func) );
3920 PSA_ASSERT( psa_generator_abort(&init) );
3921 PSA_ASSERT( psa_generator_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00003922}
3923/* END_CASE */
3924
3925/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003926void derive_setup( int key_type_arg,
3927 data_t *key_data,
3928 int alg_arg,
3929 data_t *salt,
3930 data_t *label,
3931 int requested_capacity_arg,
3932 int expected_status_arg )
3933{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003934 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003935 size_t key_type = key_type_arg;
3936 psa_algorithm_t alg = alg_arg;
3937 size_t requested_capacity = requested_capacity_arg;
3938 psa_status_t expected_status = expected_status_arg;
3939 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003940 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003941
Gilles Peskine8817f612018-12-18 00:18:46 +01003942 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003943
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003944 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003945 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003946 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003947
Gilles Peskine8817f612018-12-18 00:18:46 +01003948 PSA_ASSERT( psa_import_key( handle, key_type,
3949 key_data->x,
3950 key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003951
Gilles Peskinefe11b722018-12-18 00:24:04 +01003952 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3953 salt->x, salt->len,
3954 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003955 requested_capacity ),
3956 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003957
3958exit:
3959 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003960 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003961 mbedtls_psa_crypto_free( );
3962}
3963/* END_CASE */
3964
3965/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003966void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003967{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003968 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003969 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003970 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003971 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003972 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003973 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003974 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3975 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3976 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Jaeden Amero70261c52019-01-04 11:47:20 +00003977 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003978
Gilles Peskine8817f612018-12-18 00:18:46 +01003979 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003980
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003981 PSA_ASSERT( psa_allocate_key( &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003982 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003983 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003984
Gilles Peskine8817f612018-12-18 00:18:46 +01003985 PSA_ASSERT( psa_import_key( handle, key_type,
3986 key_data,
3987 sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003988
3989 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01003990 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3991 NULL, 0,
3992 NULL, 0,
3993 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003994
3995 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003996 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3997 NULL, 0,
3998 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003999 capacity ),
4000 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004001
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004002 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004003
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004004 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004005 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004006
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004007exit:
4008 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004009 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004010 mbedtls_psa_crypto_free( );
4011}
4012/* END_CASE */
4013
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004014/* BEGIN_CASE */
4015void test_derive_invalid_generator_tests( )
4016{
4017 uint8_t output_buffer[16];
4018 size_t buffer_size = 16;
4019 size_t capacity = 0;
4020 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4021
Nir Sonnenschein50789302018-10-31 12:16:38 +02004022 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004023 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004024
4025 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004026 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004027
Gilles Peskine8817f612018-12-18 00:18:46 +01004028 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004029
Nir Sonnenschein50789302018-10-31 12:16:38 +02004030 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004031 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004032
Nir Sonnenschein50789302018-10-31 12:16:38 +02004033 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004034 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004035
4036exit:
4037 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004038}
4039/* END_CASE */
4040
4041/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004042void derive_output( int alg_arg,
4043 data_t *key_data,
4044 data_t *salt,
4045 data_t *label,
4046 int requested_capacity_arg,
4047 data_t *expected_output1,
4048 data_t *expected_output2 )
4049{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004050 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004051 psa_algorithm_t alg = alg_arg;
4052 size_t requested_capacity = requested_capacity_arg;
4053 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4054 uint8_t *expected_outputs[2] =
4055 {expected_output1->x, expected_output2->x};
4056 size_t output_sizes[2] =
4057 {expected_output1->len, expected_output2->len};
4058 size_t output_buffer_size = 0;
4059 uint8_t *output_buffer = NULL;
4060 size_t expected_capacity;
4061 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00004062 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004063 psa_status_t status;
4064 unsigned i;
4065
4066 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4067 {
4068 if( output_sizes[i] > output_buffer_size )
4069 output_buffer_size = output_sizes[i];
4070 if( output_sizes[i] == 0 )
4071 expected_outputs[i] = NULL;
4072 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004073 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004074 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004075
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004076 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004077 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004078 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004079
Gilles Peskine8817f612018-12-18 00:18:46 +01004080 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
4081 key_data->x,
4082 key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004083
4084 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004085 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4086 salt->x, salt->len,
4087 label->x, label->len,
4088 requested_capacity ) );
4089 PSA_ASSERT( psa_get_generator_capacity( &generator,
4090 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004091 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004092 expected_capacity = requested_capacity;
4093
4094 /* Expansion phase. */
4095 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4096 {
4097 /* Read some bytes. */
4098 status = psa_generator_read( &generator,
4099 output_buffer, output_sizes[i] );
4100 if( expected_capacity == 0 && output_sizes[i] == 0 )
4101 {
4102 /* Reading 0 bytes when 0 bytes are available can go either way. */
4103 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004104 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004105 continue;
4106 }
4107 else if( expected_capacity == 0 ||
4108 output_sizes[i] > expected_capacity )
4109 {
4110 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004111 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004112 expected_capacity = 0;
4113 continue;
4114 }
4115 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004116 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004117 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004118 ASSERT_COMPARE( output_buffer, output_sizes[i],
4119 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004120 /* Check the generator status. */
4121 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01004122 PSA_ASSERT( psa_get_generator_capacity( &generator,
4123 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004124 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004125 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004126 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004127
4128exit:
4129 mbedtls_free( output_buffer );
4130 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004131 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004132 mbedtls_psa_crypto_free( );
4133}
4134/* END_CASE */
4135
4136/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004137void derive_full( int alg_arg,
4138 data_t *key_data,
4139 data_t *salt,
4140 data_t *label,
4141 int requested_capacity_arg )
4142{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004143 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004144 psa_algorithm_t alg = alg_arg;
4145 size_t requested_capacity = requested_capacity_arg;
4146 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4147 unsigned char output_buffer[16];
4148 size_t expected_capacity = requested_capacity;
4149 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00004150 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004151
Gilles Peskine8817f612018-12-18 00:18:46 +01004152 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004153
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004154 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004155 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004156 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004157
Gilles Peskine8817f612018-12-18 00:18:46 +01004158 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
4159 key_data->x,
4160 key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004161
4162 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004163 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4164 salt->x, salt->len,
4165 label->x, label->len,
4166 requested_capacity ) );
4167 PSA_ASSERT( psa_get_generator_capacity( &generator,
4168 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004169 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004170
4171 /* Expansion phase. */
4172 while( current_capacity > 0 )
4173 {
4174 size_t read_size = sizeof( output_buffer );
4175 if( read_size > current_capacity )
4176 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01004177 PSA_ASSERT( psa_generator_read( &generator,
4178 output_buffer,
4179 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004180 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01004181 PSA_ASSERT( psa_get_generator_capacity( &generator,
4182 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004183 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004184 }
4185
4186 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004187 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004188 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004189
Gilles Peskine8817f612018-12-18 00:18:46 +01004190 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004191
4192exit:
4193 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004194 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02004195 mbedtls_psa_crypto_free( );
4196}
4197/* END_CASE */
4198
4199/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004200void derive_key_exercise( int alg_arg,
4201 data_t *key_data,
4202 data_t *salt,
4203 data_t *label,
4204 int derived_type_arg,
4205 int derived_bits_arg,
4206 int derived_usage_arg,
4207 int derived_alg_arg )
4208{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004209 psa_key_handle_t base_handle = 0;
4210 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004211 psa_algorithm_t alg = alg_arg;
4212 psa_key_type_t derived_type = derived_type_arg;
4213 size_t derived_bits = derived_bits_arg;
4214 psa_key_usage_t derived_usage = derived_usage_arg;
4215 psa_algorithm_t derived_alg = derived_alg_arg;
4216 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
4217 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004218 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004219 psa_key_type_t got_type;
4220 size_t got_bits;
4221
Gilles Peskine8817f612018-12-18 00:18:46 +01004222 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004223
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004224 PSA_ASSERT( psa_allocate_key( &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004225 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004226 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
4227 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
4228 key_data->x,
4229 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004230
4231 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004232 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4233 salt->x, salt->len,
4234 label->x, label->len,
4235 capacity ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004236 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004237 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004238 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
4239 PSA_ASSERT( psa_generator_import_key( derived_handle,
4240 derived_type,
4241 derived_bits,
4242 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004243
4244 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01004245 PSA_ASSERT( psa_get_key_information( derived_handle,
4246 &got_type,
4247 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004248 TEST_EQUAL( got_type, derived_type );
4249 TEST_EQUAL( got_bits, derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004250
4251 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004252 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004253 goto exit;
4254
4255exit:
4256 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004257 psa_destroy_key( base_handle );
4258 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004259 mbedtls_psa_crypto_free( );
4260}
4261/* END_CASE */
4262
4263/* BEGIN_CASE */
4264void derive_key_export( int alg_arg,
4265 data_t *key_data,
4266 data_t *salt,
4267 data_t *label,
4268 int bytes1_arg,
4269 int bytes2_arg )
4270{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004271 psa_key_handle_t base_handle = 0;
4272 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004273 psa_algorithm_t alg = alg_arg;
4274 size_t bytes1 = bytes1_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004275 size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004276 size_t bytes2 = bytes2_arg;
4277 size_t capacity = bytes1 + bytes2;
4278 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004279 uint8_t *output_buffer = NULL;
4280 uint8_t *export_buffer = NULL;
Jaeden Amero70261c52019-01-04 11:47:20 +00004281 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004282 size_t length;
4283
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004284 ASSERT_ALLOC( output_buffer, capacity );
4285 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004286 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004287
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004288 PSA_ASSERT( psa_allocate_key( &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004289 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004290 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
4291 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
4292 key_data->x,
4293 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004294
4295 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004296 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4297 salt->x, salt->len,
4298 label->x, label->len,
4299 capacity ) );
4300 PSA_ASSERT( psa_generator_read( &generator,
4301 output_buffer,
4302 capacity ) );
4303 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004304
4305 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004306 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4307 salt->x, salt->len,
4308 label->x, label->len,
4309 capacity ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004310 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004311 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004312 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
4313 PSA_ASSERT( psa_generator_import_key( derived_handle,
4314 PSA_KEY_TYPE_RAW_DATA,
4315 derived_bits,
4316 &generator ) );
4317 PSA_ASSERT( psa_export_key( derived_handle,
4318 export_buffer, bytes1,
4319 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004320 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004321 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004322 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004323 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
4324 PSA_ASSERT( psa_generator_import_key( derived_handle,
4325 PSA_KEY_TYPE_RAW_DATA,
4326 PSA_BYTES_TO_BITS( bytes2 ),
4327 &generator ) );
4328 PSA_ASSERT( psa_export_key( derived_handle,
4329 export_buffer + bytes1, bytes2,
4330 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004331 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004332
4333 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004334 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4335 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004336
4337exit:
4338 mbedtls_free( output_buffer );
4339 mbedtls_free( export_buffer );
4340 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004341 psa_destroy_key( base_handle );
4342 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004343 mbedtls_psa_crypto_free( );
4344}
4345/* END_CASE */
4346
4347/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004348void key_agreement_setup( int alg_arg,
4349 int our_key_type_arg, data_t *our_key_data,
4350 data_t *peer_key_data,
4351 int expected_status_arg )
4352{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004353 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004354 psa_algorithm_t alg = alg_arg;
4355 psa_key_type_t our_key_type = our_key_type_arg;
4356 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004357 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004358
Gilles Peskine8817f612018-12-18 00:18:46 +01004359 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004360
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004361 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004362 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004363 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
4364 PSA_ASSERT( psa_import_key( our_key, our_key_type,
4365 our_key_data->x,
4366 our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004367
Gilles Peskinefe11b722018-12-18 00:24:04 +01004368 TEST_EQUAL( psa_key_agreement( &generator,
4369 our_key,
4370 peer_key_data->x, peer_key_data->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004371 alg ),
4372 expected_status_arg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004373
4374exit:
4375 psa_generator_abort( &generator );
4376 psa_destroy_key( our_key );
4377 mbedtls_psa_crypto_free( );
4378}
4379/* END_CASE */
4380
4381/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004382void key_agreement_capacity( int alg_arg,
4383 int our_key_type_arg, data_t *our_key_data,
4384 data_t *peer_key_data,
4385 int expected_capacity_arg )
4386{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004387 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004388 psa_algorithm_t alg = alg_arg;
4389 psa_key_type_t our_key_type = our_key_type_arg;
4390 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004391 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004392 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004393 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004394
Gilles Peskine8817f612018-12-18 00:18:46 +01004395 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004396
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004397 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004398 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004399 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
4400 PSA_ASSERT( psa_import_key( our_key, our_key_type,
4401 our_key_data->x,
4402 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004403
Gilles Peskine8817f612018-12-18 00:18:46 +01004404 PSA_ASSERT( psa_key_agreement( &generator,
4405 our_key,
4406 peer_key_data->x, peer_key_data->len,
4407 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004408
Gilles Peskinebf491972018-10-25 22:36:12 +02004409 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004410 PSA_ASSERT( psa_get_generator_capacity(
4411 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004412 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004413
Gilles Peskinebf491972018-10-25 22:36:12 +02004414 /* Test the actual capacity by reading the output. */
4415 while( actual_capacity > sizeof( output ) )
4416 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004417 PSA_ASSERT( psa_generator_read( &generator,
4418 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004419 actual_capacity -= sizeof( output );
4420 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004421 PSA_ASSERT( psa_generator_read( &generator,
4422 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004423 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004424 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004425
Gilles Peskine59685592018-09-18 12:11:34 +02004426exit:
4427 psa_generator_abort( &generator );
4428 psa_destroy_key( our_key );
4429 mbedtls_psa_crypto_free( );
4430}
4431/* END_CASE */
4432
4433/* BEGIN_CASE */
4434void key_agreement_output( int alg_arg,
4435 int our_key_type_arg, data_t *our_key_data,
4436 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004437 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004438{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004439 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004440 psa_algorithm_t alg = alg_arg;
4441 psa_key_type_t our_key_type = our_key_type_arg;
4442 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004443 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004444 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004445
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004446 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4447 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004448
Gilles Peskine8817f612018-12-18 00:18:46 +01004449 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004450
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004451 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004452 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004453 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
4454 PSA_ASSERT( psa_import_key( our_key, our_key_type,
4455 our_key_data->x,
4456 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004457
Gilles Peskine8817f612018-12-18 00:18:46 +01004458 PSA_ASSERT( psa_key_agreement( &generator,
4459 our_key,
4460 peer_key_data->x, peer_key_data->len,
4461 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004462
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004463 PSA_ASSERT( psa_generator_read( &generator,
4464 actual_output,
4465 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004466 ASSERT_COMPARE( actual_output, expected_output1->len,
4467 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004468 if( expected_output2->len != 0 )
4469 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004470 PSA_ASSERT( psa_generator_read( &generator,
4471 actual_output,
4472 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004473 ASSERT_COMPARE( actual_output, expected_output2->len,
4474 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004475 }
Gilles Peskine59685592018-09-18 12:11:34 +02004476
4477exit:
4478 psa_generator_abort( &generator );
4479 psa_destroy_key( our_key );
4480 mbedtls_psa_crypto_free( );
4481 mbedtls_free( actual_output );
4482}
4483/* END_CASE */
4484
4485/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004486void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004487{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004488 size_t bytes = bytes_arg;
4489 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004490 unsigned char *output = NULL;
4491 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004492 size_t i;
4493 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004494
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004495 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
4496 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004497 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004498
Gilles Peskine8817f612018-12-18 00:18:46 +01004499 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004500
Gilles Peskinea50d7392018-06-21 10:22:13 +02004501 /* Run several times, to ensure that every output byte will be
4502 * nonzero at least once with overwhelming probability
4503 * (2^(-8*number_of_runs)). */
4504 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004505 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004506 if( bytes != 0 )
4507 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004508 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004509
4510 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004511 ASSERT_COMPARE( output + bytes, sizeof( trail ),
4512 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004513
4514 for( i = 0; i < bytes; i++ )
4515 {
4516 if( output[i] != 0 )
4517 ++changed[i];
4518 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004519 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004520
4521 /* Check that every byte was changed to nonzero at least once. This
4522 * validates that psa_generate_random is overwriting every byte of
4523 * the output buffer. */
4524 for( i = 0; i < bytes; i++ )
4525 {
4526 TEST_ASSERT( changed[i] != 0 );
4527 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004528
4529exit:
4530 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004531 mbedtls_free( output );
4532 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004533}
4534/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004535
4536/* BEGIN_CASE */
4537void generate_key( int type_arg,
4538 int bits_arg,
4539 int usage_arg,
4540 int alg_arg,
4541 int expected_status_arg )
4542{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004543 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004544 psa_key_type_t type = type_arg;
4545 psa_key_usage_t usage = usage_arg;
4546 size_t bits = bits_arg;
4547 psa_algorithm_t alg = alg_arg;
4548 psa_status_t expected_status = expected_status_arg;
4549 psa_key_type_t got_type;
4550 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004551 psa_status_t expected_info_status =
David Saadab4ecc272019-02-14 13:48:10 +02004552 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Jaeden Amero70261c52019-01-04 11:47:20 +00004553 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004554
Gilles Peskine8817f612018-12-18 00:18:46 +01004555 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004556
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004557 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004558 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004559 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004560
4561 /* Generate a key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004562 TEST_EQUAL( psa_generate_key( handle, type, bits, NULL, 0 ),
4563 expected_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004564
4565 /* Test the key information */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004566 TEST_EQUAL( psa_get_key_information( handle, &got_type, &got_bits ),
4567 expected_info_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004568 if( expected_info_status != PSA_SUCCESS )
4569 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004570 TEST_EQUAL( got_type, type );
4571 TEST_EQUAL( got_bits, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004572
Gilles Peskine818ca122018-06-20 18:16:48 +02004573 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004574 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004575 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004576
4577exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004578 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004579 mbedtls_psa_crypto_free( );
4580}
4581/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004582
Darryl Greend49a4992018-06-18 17:27:26 +01004583/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
4584void persistent_key_load_key_from_storage( data_t *data, int type_arg,
4585 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00004586 int alg_arg, int generation_method,
4587 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01004588{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004589 psa_key_handle_t handle = 0;
4590 psa_key_handle_t base_key;
Darryl Greend49a4992018-06-18 17:27:26 +01004591 psa_key_type_t type = (psa_key_type_t) type_arg;
4592 psa_key_type_t type_get;
4593 size_t bits_get;
Jaeden Amero70261c52019-01-04 11:47:20 +00004594 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
4595 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004596 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
4597 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00004598 psa_key_policy_t base_policy_set = PSA_KEY_POLICY_INIT;
Darryl Green0c6575a2018-11-07 16:05:30 +00004599 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
4600 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004601 unsigned char *first_export = NULL;
4602 unsigned char *second_export = NULL;
4603 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4604 size_t first_exported_length;
4605 size_t second_exported_length;
4606
4607 ASSERT_ALLOC( first_export, export_size );
4608 ASSERT_ALLOC( second_export, export_size );
4609
Gilles Peskine8817f612018-12-18 00:18:46 +01004610 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004611
Gilles Peskine8817f612018-12-18 00:18:46 +01004612 PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
Gilles Peskine8817f612018-12-18 00:18:46 +01004613 &handle ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004614 psa_key_policy_set_usage( &policy_set, policy_usage,
4615 policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004616 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004617
Darryl Green0c6575a2018-11-07 16:05:30 +00004618 switch( generation_method )
4619 {
4620 case IMPORT_KEY:
4621 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004622 PSA_ASSERT( psa_import_key( handle, type,
4623 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004624 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004625
Darryl Green0c6575a2018-11-07 16:05:30 +00004626 case GENERATE_KEY:
4627 /* Generate a key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004628 PSA_ASSERT( psa_generate_key( handle, type, bits,
4629 NULL, 0 ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004630 break;
4631
4632 case DERIVE_KEY:
4633 /* Create base key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004634 PSA_ASSERT( psa_allocate_key( &base_key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004635 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
4636 base_policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004637 PSA_ASSERT( psa_set_key_policy(
4638 base_key, &base_policy_set ) );
4639 PSA_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
4640 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004641 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004642 PSA_ASSERT( psa_key_derivation( &generator, base_key,
4643 base_policy_alg,
4644 NULL, 0, NULL, 0,
4645 export_size ) );
4646 PSA_ASSERT( psa_generator_import_key(
4647 handle, PSA_KEY_TYPE_RAW_DATA,
4648 bits, &generator ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004649 break;
4650 }
Darryl Greend49a4992018-06-18 17:27:26 +01004651
4652 /* Export the key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004653 TEST_EQUAL( psa_export_key( handle,
4654 first_export, export_size,
4655 &first_exported_length ),
4656 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004657
4658 /* Shutdown and restart */
4659 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004660 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004661
Darryl Greend49a4992018-06-18 17:27:26 +01004662 /* Check key slot still contains key data */
Gilles Peskine8817f612018-12-18 00:18:46 +01004663 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4664 &handle ) );
4665 PSA_ASSERT( psa_get_key_information(
4666 handle, &type_get, &bits_get ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004667 TEST_EQUAL( type_get, type );
4668 TEST_EQUAL( bits_get, (size_t) bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004669
Gilles Peskine8817f612018-12-18 00:18:46 +01004670 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004671 TEST_EQUAL( psa_key_policy_get_usage( &policy_get ), policy_usage );
4672 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_get ), policy_alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004673
4674 /* Export the key again */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004675 TEST_EQUAL( psa_export_key( handle,
4676 second_export, export_size,
4677 &second_exported_length ),
4678 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004679
Darryl Green0c6575a2018-11-07 16:05:30 +00004680 if( export_status == PSA_SUCCESS )
4681 {
4682 ASSERT_COMPARE( first_export, first_exported_length,
4683 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01004684
Darryl Green0c6575a2018-11-07 16:05:30 +00004685 switch( generation_method )
4686 {
4687 case IMPORT_KEY:
4688 ASSERT_COMPARE( data->x, data->len,
4689 first_export, first_exported_length );
4690 break;
4691 default:
4692 break;
4693 }
4694 }
4695
4696 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004697 if( ! exercise_key( handle, policy_usage, policy_alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004698 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004699
4700exit:
4701 mbedtls_free( first_export );
4702 mbedtls_free( second_export );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004703 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004704 mbedtls_psa_crypto_free();
4705}
4706/* END_CASE */