blob: 28761bd3397aafb307eb4df579cb55d205f66adc [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 );
285
286 if( usage & PSA_KEY_USAGE_SIGN )
287 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200288 /* Some algorithms require the payload to have the size of
289 * the hash encoded in the algorithm. Use this input size
290 * even for algorithms that allow other input sizes. */
291 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
292 if( hash_alg != 0 )
293 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100294 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
295 payload, payload_length,
296 signature, sizeof( signature ),
297 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200298 }
299
300 if( usage & PSA_KEY_USAGE_VERIFY )
301 {
302 psa_status_t verify_status =
303 ( usage & PSA_KEY_USAGE_SIGN ?
304 PSA_SUCCESS :
305 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100306 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
307 payload, payload_length,
308 signature, signature_length ),
309 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200310 }
311
312 return( 1 );
313
314exit:
315 return( 0 );
316}
317
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100318static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200319 psa_key_usage_t usage,
320 psa_algorithm_t alg )
321{
322 unsigned char plaintext[256] = "Hello, world...";
323 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
324 size_t ciphertext_length = sizeof( ciphertext );
325 size_t plaintext_length = 16;
326
327 if( usage & PSA_KEY_USAGE_ENCRYPT )
328 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100329 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
330 plaintext, plaintext_length,
331 NULL, 0,
332 ciphertext, sizeof( ciphertext ),
333 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200334 }
335
336 if( usage & PSA_KEY_USAGE_DECRYPT )
337 {
338 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100339 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200340 ciphertext, ciphertext_length,
341 NULL, 0,
342 plaintext, sizeof( plaintext ),
343 &plaintext_length );
344 TEST_ASSERT( status == PSA_SUCCESS ||
345 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
346 ( status == PSA_ERROR_INVALID_ARGUMENT ||
347 status == PSA_ERROR_INVALID_PADDING ) ) );
348 }
349
350 return( 1 );
351
352exit:
353 return( 0 );
354}
Gilles Peskine02b75072018-07-01 22:31:34 +0200355
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100356static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200357 psa_key_usage_t usage,
358 psa_algorithm_t alg )
359{
360 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
361 unsigned char label[16] = "This is a label.";
362 size_t label_length = sizeof( label );
363 unsigned char seed[16] = "abcdefghijklmnop";
364 size_t seed_length = sizeof( seed );
365 unsigned char output[1];
366
367 if( usage & PSA_KEY_USAGE_DERIVE )
368 {
Gilles Peskineb70a0fd2019-01-07 22:59:38 +0100369 if( PSA_ALG_IS_HKDF( alg ) )
370 {
371 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
372 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
373 PSA_KDF_STEP_SALT,
374 label,
375 label_length ) );
376 PSA_ASSERT( psa_key_derivation_input_key( &generator,
377 PSA_KDF_STEP_SECRET,
378 handle ) );
379 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
380 PSA_KDF_STEP_INFO,
381 seed,
382 seed_length ) );
383 }
384 else
385 {
386 // legacy
387 PSA_ASSERT( psa_key_derivation( &generator,
388 handle, alg,
389 label, label_length,
390 seed, seed_length,
391 sizeof( output ) ) );
392 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100393 PSA_ASSERT( psa_generator_read( &generator,
394 output,
395 sizeof( output ) ) );
396 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200397 }
398
399 return( 1 );
400
401exit:
402 return( 0 );
403}
404
Gilles Peskinec7998b72018-11-07 18:45:02 +0100405/* We need two keys to exercise key agreement. Exercise the
406 * private key against its own public key. */
407static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +0100408 psa_key_handle_t handle )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100409{
410 psa_key_type_t private_key_type;
411 psa_key_type_t public_key_type;
412 size_t key_bits;
413 uint8_t *public_key = NULL;
414 size_t public_key_length;
415 /* Return UNKNOWN_ERROR if something other than the final call to
416 * psa_key_agreement fails. This isn't fully satisfactory, but it's
417 * good enough: callers will report it as a failed test anyway. */
418 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
419
Gilles Peskine8817f612018-12-18 00:18:46 +0100420 PSA_ASSERT( psa_get_key_information( handle,
421 &private_key_type,
422 &key_bits ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100423 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
424 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
425 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100426 PSA_ASSERT( psa_export_public_key( handle,
427 public_key, public_key_length,
428 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100429
Gilles Peskine969c5d62019-01-16 15:53:06 +0100430 status = psa_key_agreement( generator, PSA_KDF_STEP_SECRET, handle,
431 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100432exit:
433 mbedtls_free( public_key );
434 return( status );
435}
436
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100437static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200438 psa_key_usage_t usage,
439 psa_algorithm_t alg )
440{
441 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200442 unsigned char output[1];
443 int ok = 0;
444
445 if( usage & PSA_KEY_USAGE_DERIVE )
446 {
447 /* We need two keys to exercise key agreement. Exercise the
448 * private key against its own public key. */
Gilles Peskine969c5d62019-01-16 15:53:06 +0100449 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
450 PSA_ASSERT( key_agreement_with_self( &generator, handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100451 PSA_ASSERT( psa_generator_read( &generator,
452 output,
453 sizeof( output ) ) );
454 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200455 }
456 ok = 1;
457
458exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200459 return( ok );
460}
461
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200462static int is_oid_of_key_type( psa_key_type_t type,
463 const uint8_t *oid, size_t oid_length )
464{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200465 const uint8_t *expected_oid = NULL;
466 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200467#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200468 if( PSA_KEY_TYPE_IS_RSA( type ) )
469 {
470 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
471 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
472 }
473 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200474#endif /* MBEDTLS_RSA_C */
475#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200476 if( PSA_KEY_TYPE_IS_ECC( type ) )
477 {
478 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
479 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
480 }
481 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200482#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200483 {
484 char message[40];
485 mbedtls_snprintf( message, sizeof( message ),
486 "OID not known for key type=0x%08lx",
487 (unsigned long) type );
488 test_fail( message, __LINE__, __FILE__ );
489 return( 0 );
490 }
491
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200492 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200493 return( 1 );
494
495exit:
496 return( 0 );
497}
498
499static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
500 size_t min_bits, size_t max_bits,
501 int must_be_odd )
502{
503 size_t len;
504 size_t actual_bits;
505 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100506 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100507 MBEDTLS_ASN1_INTEGER ),
508 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200509 /* Tolerate a slight departure from DER encoding:
510 * - 0 may be represented by an empty string or a 1-byte string.
511 * - The sign bit may be used as a value bit. */
512 if( ( len == 1 && ( *p )[0] == 0 ) ||
513 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
514 {
515 ++( *p );
516 --len;
517 }
518 if( min_bits == 0 && len == 0 )
519 return( 1 );
520 msb = ( *p )[0];
521 TEST_ASSERT( msb != 0 );
522 actual_bits = 8 * ( len - 1 );
523 while( msb != 0 )
524 {
525 msb >>= 1;
526 ++actual_bits;
527 }
528 TEST_ASSERT( actual_bits >= min_bits );
529 TEST_ASSERT( actual_bits <= max_bits );
530 if( must_be_odd )
531 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
532 *p += len;
533 return( 1 );
534exit:
535 return( 0 );
536}
537
538static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
539 size_t *len,
540 unsigned char n, unsigned char tag )
541{
542 int ret;
543 ret = mbedtls_asn1_get_tag( p, end, len,
544 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
545 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
546 if( ret != 0 )
547 return( ret );
548 end = *p + *len;
549 ret = mbedtls_asn1_get_tag( p, end, len, tag );
550 if( ret != 0 )
551 return( ret );
552 if( *p + *len != end )
553 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
554 return( 0 );
555}
556
557static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
558 uint8_t *exported, size_t exported_length )
559{
560 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100561 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200562 else
563 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200564
565#if defined(MBEDTLS_DES_C)
566 if( type == PSA_KEY_TYPE_DES )
567 {
568 /* Check the parity bits. */
569 unsigned i;
570 for( i = 0; i < bits / 8; i++ )
571 {
572 unsigned bit_count = 0;
573 unsigned m;
574 for( m = 1; m <= 0x100; m <<= 1 )
575 {
576 if( exported[i] & m )
577 ++bit_count;
578 }
579 TEST_ASSERT( bit_count % 2 != 0 );
580 }
581 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200582 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200583#endif
584
585#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
586 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
587 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200588 uint8_t *p = exported;
589 uint8_t *end = exported + exported_length;
590 size_t len;
591 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200592 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200593 * modulus INTEGER, -- n
594 * publicExponent INTEGER, -- e
595 * privateExponent INTEGER, -- d
596 * prime1 INTEGER, -- p
597 * prime2 INTEGER, -- q
598 * exponent1 INTEGER, -- d mod (p-1)
599 * exponent2 INTEGER, -- d mod (q-1)
600 * coefficient INTEGER, -- (inverse of q) mod p
601 * }
602 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100603 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
604 MBEDTLS_ASN1_SEQUENCE |
605 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
606 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200607 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
608 goto exit;
609 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
610 goto exit;
611 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
612 goto exit;
613 /* Require d to be at least half the size of n. */
614 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
615 goto exit;
616 /* Require p and q to be at most half the size of n, rounded up. */
617 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
618 goto exit;
619 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
620 goto exit;
621 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
622 goto exit;
623 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
624 goto exit;
625 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
626 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100627 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100628 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200629 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200630#endif /* MBEDTLS_RSA_C */
631
632#if defined(MBEDTLS_ECP_C)
633 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
634 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100635 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100636 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100637 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200638 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200639#endif /* MBEDTLS_ECP_C */
640
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200641 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
642 {
643 uint8_t *p = exported;
644 uint8_t *end = exported + exported_length;
645 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200646#if defined(MBEDTLS_RSA_C)
647 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
648 {
649 /* RSAPublicKey ::= SEQUENCE {
650 * modulus INTEGER, -- n
651 * publicExponent INTEGER } -- e
652 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100653 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
654 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100655 MBEDTLS_ASN1_CONSTRUCTED ),
656 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100657 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200658 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
659 goto exit;
660 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
661 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100662 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200663 }
664 else
665#endif /* MBEDTLS_RSA_C */
666#if defined(MBEDTLS_ECP_C)
667 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
668 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000669 /* The representation of an ECC public key is:
670 * - The byte 0x04;
671 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
672 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
673 * - where m is the bit size associated with the curve.
Jaeden Amero6b196002019-01-10 10:23:21 +0000674 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100675 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
676 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200677 }
678 else
679#endif /* MBEDTLS_ECP_C */
680 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100681 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200682 mbedtls_snprintf( message, sizeof( message ),
683 "No sanity check for public key type=0x%08lx",
684 (unsigned long) type );
685 test_fail( message, __LINE__, __FILE__ );
686 return( 0 );
687 }
688 }
689 else
690
691 {
692 /* No sanity checks for other types */
693 }
694
695 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200696
697exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200698 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200699}
700
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100701static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200702 psa_key_usage_t usage )
703{
704 psa_key_type_t type;
705 size_t bits;
706 uint8_t *exported = NULL;
707 size_t exported_size = 0;
708 size_t exported_length = 0;
709 int ok = 0;
710
Gilles Peskine8817f612018-12-18 00:18:46 +0100711 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200712
713 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
714 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200715 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100716 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
717 PSA_ERROR_NOT_PERMITTED );
Gilles Peskined14664a2018-08-10 19:07:32 +0200718 return( 1 );
719 }
720
Gilles Peskined14664a2018-08-10 19:07:32 +0200721 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200722 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200723
Gilles Peskine8817f612018-12-18 00:18:46 +0100724 PSA_ASSERT( psa_export_key( handle,
725 exported, exported_size,
726 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200727 ok = exported_key_sanity_check( type, bits, exported, exported_length );
728
729exit:
730 mbedtls_free( exported );
731 return( ok );
732}
733
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100734static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200735{
736 psa_key_type_t type;
737 psa_key_type_t public_type;
738 size_t bits;
739 uint8_t *exported = NULL;
740 size_t exported_size = 0;
741 size_t exported_length = 0;
742 int ok = 0;
743
Gilles Peskine8817f612018-12-18 00:18:46 +0100744 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200745 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
746 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100747 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100748 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200749 return( 1 );
750 }
751
752 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
753 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200754 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200755
Gilles Peskine8817f612018-12-18 00:18:46 +0100756 PSA_ASSERT( psa_export_public_key( handle,
757 exported, exported_size,
758 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200759 ok = exported_key_sanity_check( public_type, bits,
760 exported, exported_length );
761
762exit:
763 mbedtls_free( exported );
764 return( ok );
765}
766
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100767static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +0200768 psa_key_usage_t usage,
769 psa_algorithm_t alg )
770{
771 int ok;
772 if( alg == 0 )
773 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
774 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100775 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200776 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100777 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200778 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100779 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200780 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100781 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200782 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100783 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200784 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100785 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200786 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100787 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200788 else
789 {
790 char message[40];
791 mbedtls_snprintf( message, sizeof( message ),
792 "No code to exercise alg=0x%08lx",
793 (unsigned long) alg );
794 test_fail( message, __LINE__, __FILE__ );
795 ok = 0;
796 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200797
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100798 ok = ok && exercise_export_key( handle, usage );
799 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +0200800
Gilles Peskine02b75072018-07-01 22:31:34 +0200801 return( ok );
802}
803
Gilles Peskine10df3412018-10-25 22:35:43 +0200804static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
805 psa_algorithm_t alg )
806{
807 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
808 {
809 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
810 PSA_KEY_USAGE_VERIFY :
811 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
812 }
813 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
814 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
815 {
816 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
817 PSA_KEY_USAGE_ENCRYPT :
818 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
819 }
820 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
821 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
822 {
823 return( PSA_KEY_USAGE_DERIVE );
824 }
825 else
826 {
827 return( 0 );
828 }
829
830}
Darryl Green0c6575a2018-11-07 16:05:30 +0000831
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100832/* An overapproximation of the amount of storage needed for a key of the
833 * given type and with the given content. The API doesn't make it easy
834 * to find a good value for the size. The current implementation doesn't
835 * care about the value anyway. */
836#define KEY_BITS_FROM_DATA( type, data ) \
837 ( data )->len
838
Darryl Green0c6575a2018-11-07 16:05:30 +0000839typedef enum {
840 IMPORT_KEY = 0,
841 GENERATE_KEY = 1,
842 DERIVE_KEY = 2
843} generate_method;
844
Gilles Peskinee59236f2018-01-27 23:32:46 +0100845/* END_HEADER */
846
847/* BEGIN_DEPENDENCIES
848 * depends_on:MBEDTLS_PSA_CRYPTO_C
849 * END_DEPENDENCIES
850 */
851
852/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200853void static_checks( )
854{
855 size_t max_truncated_mac_size =
856 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
857
858 /* Check that the length for a truncated MAC always fits in the algorithm
859 * encoding. The shifted mask is the maximum truncated value. The
860 * untruncated algorithm may be one byte larger. */
861 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
862}
863/* END_CASE */
864
865/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200866void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100867{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100868 psa_key_handle_t handle = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200869 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100870 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100871
Gilles Peskine8817f612018-12-18 00:18:46 +0100872 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100873
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100874 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100875 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100876 TEST_EQUAL( status, expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100877 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100878 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100879
880exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100881 mbedtls_psa_crypto_free( );
882}
883/* END_CASE */
884
885/* BEGIN_CASE */
Gilles Peskinea4261682018-12-03 11:34:01 +0100886void import_twice( int alg_arg, int usage_arg,
887 int type1_arg, data_t *data1,
888 int expected_import1_status_arg,
889 int type2_arg, data_t *data2,
890 int expected_import2_status_arg )
891{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100892 psa_key_handle_t handle = 0;
Gilles Peskinea4261682018-12-03 11:34:01 +0100893 psa_algorithm_t alg = alg_arg;
894 psa_key_usage_t usage = usage_arg;
895 psa_key_type_t type1 = type1_arg;
896 psa_status_t expected_import1_status = expected_import1_status_arg;
897 psa_key_type_t type2 = type2_arg;
898 psa_status_t expected_import2_status = expected_import2_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +0000899 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea4261682018-12-03 11:34:01 +0100900 psa_status_t status;
901
Gilles Peskine8817f612018-12-18 00:18:46 +0100902 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100903
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100904 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100905 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100906 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100907
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100908 status = psa_import_key( handle, type1, data1->x, data1->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100909 TEST_EQUAL( status, expected_import1_status );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100910 status = psa_import_key( handle, type2, data2->x, data2->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100911 TEST_EQUAL( status, expected_import2_status );
Gilles Peskinea4261682018-12-03 11:34:01 +0100912
913 if( expected_import1_status == PSA_SUCCESS ||
914 expected_import2_status == PSA_SUCCESS )
915 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100916 TEST_ASSERT( exercise_key( handle, usage, alg ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100917 }
918
919exit:
920 mbedtls_psa_crypto_free( );
921}
922/* END_CASE */
923
924/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200925void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
926{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100927 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200928 size_t bits = bits_arg;
929 psa_status_t expected_status = expected_status_arg;
930 psa_status_t status;
931 psa_key_type_t type =
932 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
933 size_t buffer_size = /* Slight overapproximations */
934 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200935 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200936 unsigned char *p;
937 int ret;
938 size_t length;
939
Gilles Peskine8817f612018-12-18 00:18:46 +0100940 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200941 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200942
943 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
944 bits, keypair ) ) >= 0 );
945 length = ret;
946
947 /* Try importing the key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100948 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100949 status = psa_import_key( handle, type, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100950 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200951 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100952 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200953
954exit:
955 mbedtls_free( buffer );
956 mbedtls_psa_crypto_free( );
957}
958/* END_CASE */
959
960/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300961void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300962 int type_arg,
963 int alg_arg,
964 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100965 int expected_bits,
966 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200967 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100968 int canonical_input )
969{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100970 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100971 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200972 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200973 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100974 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100975 unsigned char *exported = NULL;
976 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100977 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100978 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100979 size_t reexported_length;
980 psa_key_type_t got_type;
981 size_t got_bits;
Jaeden Amero70261c52019-01-04 11:47:20 +0000982 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100983
Moran Pekercb088e72018-07-17 17:36:59 +0300984 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200985 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100986 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200987 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100988 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100989
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100990 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200991 psa_key_policy_set_usage( &policy, usage_arg, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100992 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100993
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100994 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
995 PSA_ERROR_EMPTY_SLOT );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700996
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100997 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +0100998 PSA_ASSERT( psa_import_key( handle, type,
999 data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001000
1001 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001002 PSA_ASSERT( psa_get_key_information( handle,
1003 &got_type,
1004 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001005 TEST_EQUAL( got_type, type );
1006 TEST_EQUAL( got_bits, (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001007
1008 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001009 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001010 exported, export_size,
1011 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001012 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001013
1014 /* The exported length must be set by psa_export_key() to a value between 0
1015 * and export_size. On errors, the exported length must be 0. */
1016 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1017 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1018 TEST_ASSERT( exported_length <= export_size );
1019
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001020 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001021 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001022 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001023 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001024 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001025 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001026 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001027
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001028 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001029 goto exit;
1030
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001031 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001032 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001033 else
1034 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001035 psa_key_handle_t handle2;
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001036 PSA_ASSERT( psa_allocate_key( &handle2 ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001037 PSA_ASSERT( psa_set_key_policy( handle2, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001038
Gilles Peskine8817f612018-12-18 00:18:46 +01001039 PSA_ASSERT( psa_import_key( handle2, type,
1040 exported,
1041 exported_length ) );
1042 PSA_ASSERT( psa_export_key( handle2,
1043 reexported,
1044 export_size,
1045 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001046 ASSERT_COMPARE( exported, exported_length,
1047 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001048 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001049 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001050 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001051
1052destroy:
1053 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001054 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001055 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1056 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001057
1058exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001059 mbedtls_free( exported );
1060 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001061 mbedtls_psa_crypto_free( );
1062}
1063/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001064
Moran Pekerf709f4a2018-06-06 17:26:04 +03001065/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001066void import_key_nonempty_slot( )
1067{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001068 psa_key_handle_t handle = 0;
Moran Peker28a38e62018-11-07 16:18:24 +02001069 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1070 psa_status_t status;
1071 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
Gilles Peskine8817f612018-12-18 00:18:46 +01001072 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001073
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001074 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001075
Moran Peker28a38e62018-11-07 16:18:24 +02001076 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001077 PSA_ASSERT( psa_import_key( handle, type,
1078 data, sizeof( data ) ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001079
1080 /* Import the key again */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001081 status = psa_import_key( handle, type, data, sizeof( data ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001082 TEST_EQUAL( status, PSA_ERROR_OCCUPIED_SLOT );
Moran Peker28a38e62018-11-07 16:18:24 +02001083
1084exit:
1085 mbedtls_psa_crypto_free( );
1086}
1087/* END_CASE */
1088
1089/* BEGIN_CASE */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001090void export_invalid_handle( int handle, int expected_export_status_arg )
Moran Peker28a38e62018-11-07 16:18:24 +02001091{
1092 psa_status_t status;
1093 unsigned char *exported = NULL;
1094 size_t export_size = 0;
1095 size_t exported_length = INVALID_EXPORT_LENGTH;
1096 psa_status_t expected_export_status = expected_export_status_arg;
1097
Gilles Peskine8817f612018-12-18 00:18:46 +01001098 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001099
1100 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001101 status = psa_export_key( (psa_key_handle_t) handle,
Moran Peker28a38e62018-11-07 16:18:24 +02001102 exported, export_size,
1103 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001104 TEST_EQUAL( status, expected_export_status );
Moran Peker28a38e62018-11-07 16:18:24 +02001105
1106exit:
1107 mbedtls_psa_crypto_free( );
1108}
1109/* END_CASE */
1110
1111/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001112void export_with_no_key_activity( )
1113{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001114 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001115 psa_algorithm_t alg = PSA_ALG_CTR;
1116 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001117 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001118 unsigned char *exported = NULL;
1119 size_t export_size = 0;
1120 size_t exported_length = INVALID_EXPORT_LENGTH;
1121
Gilles Peskine8817f612018-12-18 00:18:46 +01001122 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001123
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001124 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001125 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001126 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001127
1128 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001129 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001130 exported, export_size,
1131 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001132 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001133
1134exit:
1135 mbedtls_psa_crypto_free( );
1136}
1137/* END_CASE */
1138
1139/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001140void cipher_with_no_key_activity( )
1141{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001142 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001143 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001144 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001145 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001146 int exercise_alg = PSA_ALG_CTR;
1147
Gilles Peskine8817f612018-12-18 00:18:46 +01001148 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001149
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001150 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekerce500072018-11-07 16:20:07 +02001151 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001152 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerce500072018-11-07 16:20:07 +02001153
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001154 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001155 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001156
1157exit:
1158 psa_cipher_abort( &operation );
1159 mbedtls_psa_crypto_free( );
1160}
1161/* END_CASE */
1162
1163/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001164void export_after_import_failure( data_t *data, int type_arg,
1165 int expected_import_status_arg )
1166{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001167 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001168 psa_key_type_t type = type_arg;
1169 psa_status_t status;
1170 unsigned char *exported = NULL;
1171 size_t export_size = 0;
1172 psa_status_t expected_import_status = expected_import_status_arg;
1173 size_t exported_length = INVALID_EXPORT_LENGTH;
1174
Gilles Peskine8817f612018-12-18 00:18:46 +01001175 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001176
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001177 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001178
Moran Peker34550092018-11-07 16:19:34 +02001179 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001180 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001181 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001182 TEST_EQUAL( status, expected_import_status );
Moran Peker34550092018-11-07 16:19:34 +02001183
1184 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001185 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001186 exported, export_size,
1187 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001188 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001189
1190exit:
1191 mbedtls_psa_crypto_free( );
1192}
1193/* END_CASE */
1194
1195/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001196void cipher_after_import_failure( data_t *data, int type_arg,
1197 int expected_import_status_arg )
1198{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001199 psa_key_handle_t handle = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001200 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001201 psa_key_type_t type = type_arg;
1202 psa_status_t status;
1203 psa_status_t expected_import_status = expected_import_status_arg;
1204 int exercise_alg = PSA_ALG_CTR;
1205
Gilles Peskine8817f612018-12-18 00:18:46 +01001206 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001207
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001208 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001209
Moran Pekerce500072018-11-07 16:20:07 +02001210 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001211 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001212 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001213 TEST_EQUAL( status, expected_import_status );
Moran Pekerce500072018-11-07 16:20:07 +02001214
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001215 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001216 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001217
1218exit:
1219 psa_cipher_abort( &operation );
1220 mbedtls_psa_crypto_free( );
1221}
1222/* END_CASE */
1223
1224/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001225void export_after_destroy_key( data_t *data, int type_arg )
1226{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001227 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001228 psa_key_type_t type = type_arg;
1229 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001230 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001231 psa_algorithm_t alg = PSA_ALG_CTR;
1232 unsigned char *exported = NULL;
1233 size_t export_size = 0;
1234 size_t exported_length = INVALID_EXPORT_LENGTH;
1235
Gilles Peskine8817f612018-12-18 00:18:46 +01001236 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001237
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001238 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001239 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001240 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001241 export_size = (ptrdiff_t) data->len;
1242 ASSERT_ALLOC( exported, export_size );
1243
1244 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001245 PSA_ASSERT( psa_import_key( handle, type,
1246 data->x, data->len ) );
Moran Peker34550092018-11-07 16:19:34 +02001247
Gilles Peskine8817f612018-12-18 00:18:46 +01001248 PSA_ASSERT( psa_export_key( handle, exported, export_size,
1249 &exported_length ) );
Moran Peker34550092018-11-07 16:19:34 +02001250
1251 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001252 PSA_ASSERT( psa_destroy_key( handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001253
1254 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001255 status = psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001256 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001257 TEST_EQUAL( status, PSA_ERROR_INVALID_HANDLE );
Moran Peker34550092018-11-07 16:19:34 +02001258
1259exit:
1260 mbedtls_free( exported );
1261 mbedtls_psa_crypto_free( );
1262}
1263/* END_CASE */
1264
1265/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001266void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001267 int type_arg,
1268 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001269 int export_size_delta,
1270 int expected_export_status_arg,
1271 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001272{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001273 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001274 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001275 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001276 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001277 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001278 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001279 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001280 size_t exported_length = INVALID_EXPORT_LENGTH;
Jaeden Amero70261c52019-01-04 11:47:20 +00001281 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001282
Gilles Peskine8817f612018-12-18 00:18:46 +01001283 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001284
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001285 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001286 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001287 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001288
1289 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001290 PSA_ASSERT( psa_import_key( handle, type,
1291 data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001292
Gilles Peskine49c25912018-10-29 15:15:31 +01001293 /* Export the public key */
1294 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001295 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001296 exported, export_size,
1297 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001298 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001299 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001300 {
1301 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1302 size_t bits;
Gilles Peskine8817f612018-12-18 00:18:46 +01001303 PSA_ASSERT( psa_get_key_information( handle, NULL, &bits ) );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001304 TEST_ASSERT( expected_public_key->len <=
1305 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001306 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1307 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001308 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001309
1310exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001311 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001312 psa_destroy_key( handle );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001313 mbedtls_psa_crypto_free( );
1314}
1315/* END_CASE */
1316
Gilles Peskine20035e32018-02-03 22:44:14 +01001317/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001318void import_and_exercise_key( data_t *data,
1319 int type_arg,
1320 int bits_arg,
1321 int alg_arg )
1322{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001323 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001324 psa_key_type_t type = type_arg;
1325 size_t bits = bits_arg;
1326 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001327 psa_key_usage_t usage = usage_to_exercise( type, alg );
Jaeden Amero70261c52019-01-04 11:47:20 +00001328 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001329 psa_key_type_t got_type;
1330 size_t got_bits;
1331 psa_status_t status;
1332
Gilles Peskine8817f612018-12-18 00:18:46 +01001333 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001334
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001335 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001336 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001337 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001338
1339 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001340 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01001341 PSA_ASSERT( status );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001342
1343 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001344 PSA_ASSERT( psa_get_key_information( handle,
1345 &got_type,
1346 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001347 TEST_EQUAL( got_type, type );
1348 TEST_EQUAL( got_bits, bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001349
1350 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001351 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001352 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001353
1354exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001355 psa_destroy_key( handle );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001356 mbedtls_psa_crypto_free( );
1357}
1358/* END_CASE */
1359
1360/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001361void key_policy( int usage_arg, int alg_arg )
1362{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001363 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001364 psa_algorithm_t alg = alg_arg;
1365 psa_key_usage_t usage = usage_arg;
1366 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1367 unsigned char key[32] = {0};
Jaeden Amero70261c52019-01-04 11:47:20 +00001368 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
1369 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001370
1371 memset( key, 0x2a, sizeof( key ) );
1372
Gilles Peskine8817f612018-12-18 00:18:46 +01001373 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001374
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001375 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001376 psa_key_policy_set_usage( &policy_set, usage, alg );
1377
Gilles Peskinefe11b722018-12-18 00:24:04 +01001378 TEST_EQUAL( psa_key_policy_get_usage( &policy_set ), usage );
1379 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_set ), alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001380 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001381
Gilles Peskine8817f612018-12-18 00:18:46 +01001382 PSA_ASSERT( psa_import_key( handle, key_type,
1383 key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001384
Gilles Peskine8817f612018-12-18 00:18:46 +01001385 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001386
Gilles Peskinefe11b722018-12-18 00:24:04 +01001387 TEST_EQUAL( policy_get.usage, policy_set.usage );
1388 TEST_EQUAL( policy_get.alg, policy_set.alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001389
1390exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001391 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001392 mbedtls_psa_crypto_free( );
1393}
1394/* END_CASE */
1395
1396/* BEGIN_CASE */
Jaeden Amero70261c52019-01-04 11:47:20 +00001397void key_policy_init( )
1398{
1399 /* Test each valid way of initializing the object, except for `= {0}`, as
1400 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1401 * though it's OK by the C standard. We could test for this, but we'd need
1402 * to supress the Clang warning for the test. */
1403 psa_key_policy_t func = psa_key_policy_init( );
1404 psa_key_policy_t init = PSA_KEY_POLICY_INIT;
1405 psa_key_policy_t zero;
1406
1407 memset( &zero, 0, sizeof( zero ) );
1408
1409 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1410 * specification, we test that all valid ways of initializing the object
1411 * have the same bit pattern. This is a stronger requirement that may not
1412 * be valid on all platforms or PSA Crypto implementations, but implies the
1413 * weaker actual requirement is met: that a freshly initialized object, no
1414 * matter how it was initialized, acts the same as any other valid
1415 * initialization. */
1416 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1417 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1418}
1419/* END_CASE */
1420
1421/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001422void mac_key_policy( int policy_usage,
1423 int policy_alg,
1424 int key_type,
1425 data_t *key_data,
1426 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001427{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001428 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001429 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001430 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001431 psa_status_t status;
1432 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001433
Gilles Peskine8817f612018-12-18 00:18:46 +01001434 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001435
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001436 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001437 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001438 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001439
Gilles Peskine8817f612018-12-18 00:18:46 +01001440 PSA_ASSERT( psa_import_key( handle, key_type,
1441 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001442
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001443 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001444 if( policy_alg == exercise_alg &&
1445 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001446 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001447 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001448 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001449 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001450
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001451 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001452 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001453 if( policy_alg == exercise_alg &&
1454 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001455 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001456 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001457 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001458
1459exit:
1460 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001461 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001462 mbedtls_psa_crypto_free( );
1463}
1464/* END_CASE */
1465
1466/* BEGIN_CASE */
1467void cipher_key_policy( int policy_usage,
1468 int policy_alg,
1469 int key_type,
1470 data_t *key_data,
1471 int exercise_alg )
1472{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001473 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001474 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001475 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001476 psa_status_t status;
1477
Gilles Peskine8817f612018-12-18 00:18:46 +01001478 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001479
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001480 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001481 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001482 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001483
Gilles Peskine8817f612018-12-18 00:18:46 +01001484 PSA_ASSERT( psa_import_key( handle, key_type,
1485 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001486
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001487 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001488 if( policy_alg == exercise_alg &&
1489 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 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 psa_cipher_abort( &operation );
1494
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001495 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001496 if( policy_alg == exercise_alg &&
1497 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001498 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001499 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001500 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001501
1502exit:
1503 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001504 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001505 mbedtls_psa_crypto_free( );
1506}
1507/* END_CASE */
1508
1509/* BEGIN_CASE */
1510void aead_key_policy( int policy_usage,
1511 int policy_alg,
1512 int key_type,
1513 data_t *key_data,
1514 int nonce_length_arg,
1515 int tag_length_arg,
1516 int exercise_alg )
1517{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001518 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001519 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001520 psa_status_t status;
1521 unsigned char nonce[16] = {0};
1522 size_t nonce_length = nonce_length_arg;
1523 unsigned char tag[16];
1524 size_t tag_length = tag_length_arg;
1525 size_t output_length;
1526
1527 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1528 TEST_ASSERT( tag_length <= sizeof( tag ) );
1529
Gilles Peskine8817f612018-12-18 00:18:46 +01001530 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001531
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001532 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001533 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001534 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001535
Gilles Peskine8817f612018-12-18 00:18:46 +01001536 PSA_ASSERT( psa_import_key( handle, key_type,
1537 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001538
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001539 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001540 nonce, nonce_length,
1541 NULL, 0,
1542 NULL, 0,
1543 tag, tag_length,
1544 &output_length );
1545 if( policy_alg == exercise_alg &&
1546 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001547 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001548 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001549 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001550
1551 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001552 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001553 nonce, nonce_length,
1554 NULL, 0,
1555 tag, tag_length,
1556 NULL, 0,
1557 &output_length );
1558 if( policy_alg == exercise_alg &&
1559 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001560 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001561 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001562 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001563
1564exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001565 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001566 mbedtls_psa_crypto_free( );
1567}
1568/* END_CASE */
1569
1570/* BEGIN_CASE */
1571void asymmetric_encryption_key_policy( int policy_usage,
1572 int policy_alg,
1573 int key_type,
1574 data_t *key_data,
1575 int exercise_alg )
1576{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001577 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001578 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001579 psa_status_t status;
1580 size_t key_bits;
1581 size_t buffer_length;
1582 unsigned char *buffer = NULL;
1583 size_t output_length;
1584
Gilles Peskine8817f612018-12-18 00:18:46 +01001585 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001586
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001587 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001588 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001589 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001590
Gilles Peskine8817f612018-12-18 00:18:46 +01001591 PSA_ASSERT( psa_import_key( handle, key_type,
1592 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001593
Gilles Peskine8817f612018-12-18 00:18:46 +01001594 PSA_ASSERT( psa_get_key_information( handle,
1595 NULL,
1596 &key_bits ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001597 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1598 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001599 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001600
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001601 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001602 NULL, 0,
1603 NULL, 0,
1604 buffer, buffer_length,
1605 &output_length );
1606 if( policy_alg == exercise_alg &&
1607 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001608 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001609 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001610 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001611
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001612 if( buffer_length != 0 )
1613 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001614 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001615 buffer, buffer_length,
1616 NULL, 0,
1617 buffer, buffer_length,
1618 &output_length );
1619 if( policy_alg == exercise_alg &&
1620 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001621 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001622 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001623 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001624
1625exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001626 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001627 mbedtls_psa_crypto_free( );
1628 mbedtls_free( buffer );
1629}
1630/* END_CASE */
1631
1632/* BEGIN_CASE */
1633void asymmetric_signature_key_policy( int policy_usage,
1634 int policy_alg,
1635 int key_type,
1636 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001637 int exercise_alg,
1638 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001639{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001640 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001641 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001642 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001643 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1644 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1645 * compatible with the policy and `payload_length_arg` is supposed to be
1646 * a valid input length to sign. If `payload_length_arg <= 0`,
1647 * `exercise_alg` is supposed to be forbidden by the policy. */
1648 int compatible_alg = payload_length_arg > 0;
1649 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001650 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1651 size_t signature_length;
1652
Gilles Peskine8817f612018-12-18 00:18:46 +01001653 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001654
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001655 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001656 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001657 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001658
Gilles Peskine8817f612018-12-18 00:18:46 +01001659 PSA_ASSERT( psa_import_key( handle, key_type,
1660 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001661
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001662 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001663 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001664 signature, sizeof( signature ),
1665 &signature_length );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001666 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001667 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001668 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001669 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001670
1671 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001672 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001673 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001674 signature, sizeof( signature ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001675 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001676 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001677 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001678 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001679
1680exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001681 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001682 mbedtls_psa_crypto_free( );
1683}
1684/* END_CASE */
1685
1686/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001687void derive_key_policy( int policy_usage,
1688 int policy_alg,
1689 int key_type,
1690 data_t *key_data,
1691 int exercise_alg )
1692{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001693 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001694 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001695 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1696 psa_status_t status;
1697
Gilles Peskine8817f612018-12-18 00:18:46 +01001698 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001699
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001700 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001701 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001702 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001703
Gilles Peskine8817f612018-12-18 00:18:46 +01001704 PSA_ASSERT( psa_import_key( handle, key_type,
1705 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001706
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001707 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001708 exercise_alg,
1709 NULL, 0,
1710 NULL, 0,
1711 1 );
1712 if( policy_alg == exercise_alg &&
1713 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001714 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001715 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001716 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001717
1718exit:
1719 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001720 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001721 mbedtls_psa_crypto_free( );
1722}
1723/* END_CASE */
1724
1725/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001726void agreement_key_policy( int policy_usage,
1727 int policy_alg,
1728 int key_type_arg,
1729 data_t *key_data,
1730 int exercise_alg )
1731{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001732 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001733 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001734 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001735 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1736 psa_status_t status;
1737
Gilles Peskine8817f612018-12-18 00:18:46 +01001738 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001739
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001740 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001741 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001742 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001743
Gilles Peskine8817f612018-12-18 00:18:46 +01001744 PSA_ASSERT( psa_import_key( handle, key_type,
1745 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001746
Gilles Peskine969c5d62019-01-16 15:53:06 +01001747 PSA_ASSERT( psa_key_derivation_setup( &generator, exercise_alg ) );
1748 status = key_agreement_with_self( &generator, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001749
Gilles Peskine01d718c2018-09-18 12:01:02 +02001750 if( policy_alg == exercise_alg &&
1751 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001752 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001753 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001754 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001755
1756exit:
1757 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001758 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001759 mbedtls_psa_crypto_free( );
1760}
1761/* END_CASE */
1762
1763/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001764void hash_operation_init( )
1765{
1766 /* Test each valid way of initializing the object, except for `= {0}`, as
1767 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1768 * though it's OK by the C standard. We could test for this, but we'd need
1769 * to supress the Clang warning for the test. */
1770 psa_hash_operation_t func = psa_hash_operation_init( );
1771 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1772 psa_hash_operation_t zero;
1773
1774 memset( &zero, 0, sizeof( zero ) );
1775
1776 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1777 * specification, we test that all valid ways of initializing the object
1778 * have the same bit pattern. This is a stronger requirement that may not
1779 * be valid on all platforms or PSA Crypto implementations, but implies the
1780 * weaker actual requirement is met: that a freshly initialized object, no
1781 * matter how it was initialized, acts the same as any other valid
1782 * initialization. */
1783 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1784 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1785}
1786/* END_CASE */
1787
1788/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001789void hash_setup( int alg_arg,
1790 int expected_status_arg )
1791{
1792 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001793 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001794 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001795 psa_status_t status;
1796
Gilles Peskine8817f612018-12-18 00:18:46 +01001797 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001798
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001799 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001800 psa_hash_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001801 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001802
1803exit:
1804 mbedtls_psa_crypto_free( );
1805}
1806/* END_CASE */
1807
1808/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02001809void hash_bad_order( )
1810{
1811 unsigned char input[] = "";
1812 /* SHA-256 hash of an empty string */
1813 unsigned char hash[] = {
1814 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1815 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1816 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
1817 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001818 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001819
Gilles Peskine8817f612018-12-18 00:18:46 +01001820 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001821
1822 /* psa_hash_update without calling psa_hash_setup beforehand */
1823 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001824 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001825 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001826
1827 /* psa_hash_verify without calling psa_hash_setup beforehand */
1828 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001829 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001830 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001831
1832 /* psa_hash_finish without calling psa_hash_setup beforehand */
1833 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001834 TEST_EQUAL( psa_hash_finish( &operation,
1835 hash, sizeof( hash ), &hash_len ),
1836 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001837
1838exit:
1839 mbedtls_psa_crypto_free( );
1840}
1841/* END_CASE */
1842
itayzafrir27e69452018-11-01 14:26:34 +02001843/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1844void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001845{
1846 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001847 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1848 * appended to it */
1849 unsigned char hash[] = {
1850 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1851 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1852 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03001853 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001854 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001855
Gilles Peskine8817f612018-12-18 00:18:46 +01001856 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001857
itayzafrir27e69452018-11-01 14:26:34 +02001858 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001859 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001860 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001861 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001862
itayzafrir27e69452018-11-01 14:26:34 +02001863 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001864 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001865 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001866 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001867
itayzafrir27e69452018-11-01 14:26:34 +02001868 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001869 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001870 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001871 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001872
itayzafrirec93d302018-10-18 18:01:10 +03001873exit:
1874 mbedtls_psa_crypto_free( );
1875}
1876/* END_CASE */
1877
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001878/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1879void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001880{
1881 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001882 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03001883 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001884 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001885 size_t hash_len;
1886
Gilles Peskine8817f612018-12-18 00:18:46 +01001887 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001888
itayzafrir58028322018-10-25 10:22:01 +03001889 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001890 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001891 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001892 hash, expected_size - 1, &hash_len ),
1893 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001894
1895exit:
1896 mbedtls_psa_crypto_free( );
1897}
1898/* END_CASE */
1899
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001900/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1901void hash_clone_source_state( )
1902{
1903 psa_algorithm_t alg = PSA_ALG_SHA_256;
1904 unsigned char hash[PSA_HASH_MAX_SIZE];
1905 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1906 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1907 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1908 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1909 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1910 size_t hash_len;
1911
1912 PSA_ASSERT( psa_crypto_init( ) );
1913 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1914
1915 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1916 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1917 PSA_ASSERT( psa_hash_finish( &op_finished,
1918 hash, sizeof( hash ), &hash_len ) );
1919 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1920 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1921
1922 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1923 PSA_ERROR_BAD_STATE );
1924
1925 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1926 PSA_ASSERT( psa_hash_finish( &op_init,
1927 hash, sizeof( hash ), &hash_len ) );
1928 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1929 PSA_ASSERT( psa_hash_finish( &op_finished,
1930 hash, sizeof( hash ), &hash_len ) );
1931 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1932 PSA_ASSERT( psa_hash_finish( &op_aborted,
1933 hash, sizeof( hash ), &hash_len ) );
1934
1935exit:
1936 psa_hash_abort( &op_source );
1937 psa_hash_abort( &op_init );
1938 psa_hash_abort( &op_setup );
1939 psa_hash_abort( &op_finished );
1940 psa_hash_abort( &op_aborted );
1941 mbedtls_psa_crypto_free( );
1942}
1943/* END_CASE */
1944
1945/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1946void hash_clone_target_state( )
1947{
1948 psa_algorithm_t alg = PSA_ALG_SHA_256;
1949 unsigned char hash[PSA_HASH_MAX_SIZE];
1950 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1951 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1952 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1953 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1954 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1955 size_t hash_len;
1956
1957 PSA_ASSERT( psa_crypto_init( ) );
1958
1959 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1960 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1961 PSA_ASSERT( psa_hash_finish( &op_finished,
1962 hash, sizeof( hash ), &hash_len ) );
1963 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1964 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1965
1966 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1967 PSA_ASSERT( psa_hash_finish( &op_target,
1968 hash, sizeof( hash ), &hash_len ) );
1969
1970 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1971 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1972 PSA_ERROR_BAD_STATE );
1973 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1974 PSA_ERROR_BAD_STATE );
1975
1976exit:
1977 psa_hash_abort( &op_target );
1978 psa_hash_abort( &op_init );
1979 psa_hash_abort( &op_setup );
1980 psa_hash_abort( &op_finished );
1981 psa_hash_abort( &op_aborted );
1982 mbedtls_psa_crypto_free( );
1983}
1984/* END_CASE */
1985
itayzafrir58028322018-10-25 10:22:01 +03001986/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001987void mac_operation_init( )
1988{
1989 /* Test each valid way of initializing the object, except for `= {0}`, as
1990 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1991 * though it's OK by the C standard. We could test for this, but we'd need
1992 * to supress the Clang warning for the test. */
1993 psa_mac_operation_t func = psa_mac_operation_init( );
1994 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1995 psa_mac_operation_t zero;
1996
1997 memset( &zero, 0, sizeof( zero ) );
1998
1999 /* Although not technically guaranteed by the C standard nor the PSA Crypto
2000 * specification, we test that all valid ways of initializing the object
2001 * have the same bit pattern. This is a stronger requirement that may not
2002 * be valid on all platforms or PSA Crypto implementations, but implies the
2003 * weaker actual requirement is met: that a freshly initialized object, no
2004 * matter how it was initialized, acts the same as any other valid
2005 * initialization. */
2006 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
2007 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
2008}
2009/* END_CASE */
2010
2011/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002012void mac_setup( int key_type_arg,
2013 data_t *key,
2014 int alg_arg,
2015 int expected_status_arg )
2016{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002017 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002018 psa_key_type_t key_type = key_type_arg;
2019 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002020 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002021 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002022 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002023 psa_status_t status;
2024
Gilles Peskine8817f612018-12-18 00:18:46 +01002025 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002026
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002027 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002028 psa_key_policy_set_usage( &policy,
2029 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2030 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002031 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002032
Gilles Peskine8817f612018-12-18 00:18:46 +01002033 PSA_ASSERT( psa_import_key( handle, key_type,
2034 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002035
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002036 status = psa_mac_sign_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002037 psa_mac_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002038 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002039
2040exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002041 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002042 mbedtls_psa_crypto_free( );
2043}
2044/* END_CASE */
2045
2046/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002047void mac_sign( int key_type_arg,
2048 data_t *key,
2049 int alg_arg,
2050 data_t *input,
2051 data_t *expected_mac )
2052{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002053 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002054 psa_key_type_t key_type = key_type_arg;
2055 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002056 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002057 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002058 /* Leave a little extra room in the output buffer. At the end of the
2059 * test, we'll check that the implementation didn't overwrite onto
2060 * this extra room. */
2061 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2062 size_t mac_buffer_size =
2063 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2064 size_t mac_length = 0;
2065
2066 memset( actual_mac, '+', sizeof( actual_mac ) );
2067 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2068 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2069
Gilles Peskine8817f612018-12-18 00:18:46 +01002070 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002071
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002072 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002073 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002074 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002075
Gilles Peskine8817f612018-12-18 00:18:46 +01002076 PSA_ASSERT( psa_import_key( handle, key_type,
2077 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002078
2079 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002080 PSA_ASSERT( psa_mac_sign_setup( &operation,
2081 handle, alg ) );
2082 PSA_ASSERT( psa_mac_update( &operation,
2083 input->x, input->len ) );
2084 PSA_ASSERT( psa_mac_sign_finish( &operation,
2085 actual_mac, mac_buffer_size,
2086 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002087
2088 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002089 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2090 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002091
2092 /* Verify that the end of the buffer is untouched. */
2093 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2094 sizeof( actual_mac ) - mac_length ) );
2095
2096exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002097 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002098 mbedtls_psa_crypto_free( );
2099}
2100/* END_CASE */
2101
2102/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002103void mac_verify( int key_type_arg,
2104 data_t *key,
2105 int alg_arg,
2106 data_t *input,
2107 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002108{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002109 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002110 psa_key_type_t key_type = key_type_arg;
2111 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002112 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002113 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002114
Gilles Peskine69c12672018-06-28 00:07:19 +02002115 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2116
Gilles Peskine8817f612018-12-18 00:18:46 +01002117 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002118
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002119 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002120 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002121 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad16036df908f2018-04-02 08:34:15 -07002122
Gilles Peskine8817f612018-12-18 00:18:46 +01002123 PSA_ASSERT( psa_import_key( handle, key_type,
2124 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002125
Gilles Peskine8817f612018-12-18 00:18:46 +01002126 PSA_ASSERT( psa_mac_verify_setup( &operation,
2127 handle, alg ) );
2128 PSA_ASSERT( psa_destroy_key( handle ) );
2129 PSA_ASSERT( psa_mac_update( &operation,
2130 input->x, input->len ) );
2131 PSA_ASSERT( psa_mac_verify_finish( &operation,
2132 expected_mac->x,
2133 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002134
2135exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002136 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002137 mbedtls_psa_crypto_free( );
2138}
2139/* END_CASE */
2140
2141/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002142void cipher_operation_init( )
2143{
2144 /* Test each valid way of initializing the object, except for `= {0}`, as
2145 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2146 * though it's OK by the C standard. We could test for this, but we'd need
2147 * to supress the Clang warning for the test. */
2148 psa_cipher_operation_t func = psa_cipher_operation_init( );
2149 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2150 psa_cipher_operation_t zero;
2151
2152 memset( &zero, 0, sizeof( zero ) );
2153
2154 /* Although not technically guaranteed by the C standard nor the PSA Crypto
2155 * specification, we test that all valid ways of initializing the object
2156 * have the same bit pattern. This is a stronger requirement that may not
2157 * be valid on all platforms or PSA Crypto implementations, but implies the
2158 * weaker actual requirement is met: that a freshly initialized object, no
2159 * matter how it was initialized, acts the same as any other valid
2160 * initialization. */
2161 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
2162 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
2163}
2164/* END_CASE */
2165
2166/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002167void cipher_setup( int key_type_arg,
2168 data_t *key,
2169 int alg_arg,
2170 int expected_status_arg )
2171{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002172 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002173 psa_key_type_t key_type = key_type_arg;
2174 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002175 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002176 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002177 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002178 psa_status_t status;
2179
Gilles Peskine8817f612018-12-18 00:18:46 +01002180 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002181
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002182 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002183 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002184 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002185
Gilles Peskine8817f612018-12-18 00:18:46 +01002186 PSA_ASSERT( psa_import_key( handle, key_type,
2187 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002188
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002189 status = psa_cipher_encrypt_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002190 psa_cipher_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002191 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002192
2193exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002194 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002195 mbedtls_psa_crypto_free( );
2196}
2197/* END_CASE */
2198
2199/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002200void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002201 data_t *key,
2202 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002203 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002204{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002205 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002206 psa_status_t status;
2207 psa_key_type_t key_type = key_type_arg;
2208 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002209 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002210 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002211 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002212 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002213 size_t output_buffer_size = 0;
2214 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002215 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002216 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002217 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002218
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002219 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2220 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002221
Gilles Peskine8817f612018-12-18 00:18:46 +01002222 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002223
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002224 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002225 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002226 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002227
Gilles Peskine8817f612018-12-18 00:18:46 +01002228 PSA_ASSERT( psa_import_key( handle, key_type,
2229 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002230
Gilles Peskine8817f612018-12-18 00:18:46 +01002231 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2232 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002233
Gilles Peskine8817f612018-12-18 00:18:46 +01002234 PSA_ASSERT( psa_cipher_set_iv( &operation,
2235 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002236 output_buffer_size = ( (size_t) input->len +
2237 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002238 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002239
Gilles Peskine8817f612018-12-18 00:18:46 +01002240 PSA_ASSERT( psa_cipher_update( &operation,
2241 input->x, input->len,
2242 output, output_buffer_size,
2243 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002244 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002245 status = psa_cipher_finish( &operation,
2246 output + function_output_length,
2247 output_buffer_size,
2248 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002249 total_output_length += function_output_length;
2250
Gilles Peskinefe11b722018-12-18 00:24:04 +01002251 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002252 if( expected_status == PSA_SUCCESS )
2253 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002254 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002255 ASSERT_COMPARE( expected_output->x, expected_output->len,
2256 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002257 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002258
Gilles Peskine50e586b2018-06-08 14:28:46 +02002259exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002260 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002261 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002262 mbedtls_psa_crypto_free( );
2263}
2264/* END_CASE */
2265
2266/* BEGIN_CASE */
2267void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002268 data_t *key,
2269 data_t *input,
2270 int first_part_size,
2271 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002272{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002273 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002274 psa_key_type_t key_type = key_type_arg;
2275 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002276 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002277 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002278 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002279 size_t output_buffer_size = 0;
2280 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002281 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002282 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002283 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002284
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002285 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2286 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002287
Gilles Peskine8817f612018-12-18 00:18:46 +01002288 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002289
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002290 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002291 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002292 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002293
Gilles Peskine8817f612018-12-18 00:18:46 +01002294 PSA_ASSERT( psa_import_key( handle, key_type,
2295 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002296
Gilles Peskine8817f612018-12-18 00:18:46 +01002297 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2298 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002299
Gilles Peskine8817f612018-12-18 00:18:46 +01002300 PSA_ASSERT( psa_cipher_set_iv( &operation,
2301 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002302 output_buffer_size = ( (size_t) input->len +
2303 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002304 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002305
Gilles Peskine4abf7412018-06-18 16:35:34 +02002306 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002307 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2308 output, output_buffer_size,
2309 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002310 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002311 PSA_ASSERT( psa_cipher_update( &operation,
2312 input->x + first_part_size,
2313 input->len - first_part_size,
2314 output, output_buffer_size,
2315 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002316 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002317 PSA_ASSERT( psa_cipher_finish( &operation,
2318 output + function_output_length,
2319 output_buffer_size,
2320 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002321 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002322 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002323
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002324 ASSERT_COMPARE( expected_output->x, expected_output->len,
2325 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002326
2327exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002328 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002329 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002330 mbedtls_psa_crypto_free( );
2331}
2332/* END_CASE */
2333
2334/* BEGIN_CASE */
2335void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002336 data_t *key,
2337 data_t *input,
2338 int first_part_size,
2339 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002340{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002341 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002342
2343 psa_key_type_t key_type = key_type_arg;
2344 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002345 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002346 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002347 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002348 size_t output_buffer_size = 0;
2349 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002350 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002351 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002352 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002353
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002354 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2355 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002356
Gilles Peskine8817f612018-12-18 00:18:46 +01002357 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002358
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002359 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002360 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002361 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002362
Gilles Peskine8817f612018-12-18 00:18:46 +01002363 PSA_ASSERT( psa_import_key( handle, key_type,
2364 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002365
Gilles Peskine8817f612018-12-18 00:18:46 +01002366 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2367 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002368
Gilles Peskine8817f612018-12-18 00:18:46 +01002369 PSA_ASSERT( psa_cipher_set_iv( &operation,
2370 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002371
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002372 output_buffer_size = ( (size_t) input->len +
2373 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002374 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002375
Gilles Peskine4abf7412018-06-18 16:35:34 +02002376 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002377 PSA_ASSERT( psa_cipher_update( &operation,
2378 input->x, first_part_size,
2379 output, output_buffer_size,
2380 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002381 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002382 PSA_ASSERT( psa_cipher_update( &operation,
2383 input->x + first_part_size,
2384 input->len - first_part_size,
2385 output, output_buffer_size,
2386 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002387 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002388 PSA_ASSERT( psa_cipher_finish( &operation,
2389 output + function_output_length,
2390 output_buffer_size,
2391 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002392 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002393 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002394
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002395 ASSERT_COMPARE( expected_output->x, expected_output->len,
2396 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002397
2398exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002399 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002400 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002401 mbedtls_psa_crypto_free( );
2402}
2403/* END_CASE */
2404
Gilles Peskine50e586b2018-06-08 14:28:46 +02002405/* BEGIN_CASE */
2406void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002407 data_t *key,
2408 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002409 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002410{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002411 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002412 psa_status_t status;
2413 psa_key_type_t key_type = key_type_arg;
2414 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002415 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002416 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002417 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002418 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002419 size_t output_buffer_size = 0;
2420 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002421 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002422 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002423 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002424
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002425 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2426 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002427
Gilles Peskine8817f612018-12-18 00:18:46 +01002428 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002429
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002430 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002431 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002432 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002433
Gilles Peskine8817f612018-12-18 00:18:46 +01002434 PSA_ASSERT( psa_import_key( handle, key_type,
2435 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002436
Gilles Peskine8817f612018-12-18 00:18:46 +01002437 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2438 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002439
Gilles Peskine8817f612018-12-18 00:18:46 +01002440 PSA_ASSERT( psa_cipher_set_iv( &operation,
2441 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002442
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002443 output_buffer_size = ( (size_t) input->len +
2444 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002445 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002446
Gilles Peskine8817f612018-12-18 00:18:46 +01002447 PSA_ASSERT( psa_cipher_update( &operation,
2448 input->x, input->len,
2449 output, output_buffer_size,
2450 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002451 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002452 status = psa_cipher_finish( &operation,
2453 output + function_output_length,
2454 output_buffer_size,
2455 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002456 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002457 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002458
2459 if( expected_status == PSA_SUCCESS )
2460 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002461 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002462 ASSERT_COMPARE( expected_output->x, expected_output->len,
2463 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002464 }
2465
Gilles Peskine50e586b2018-06-08 14:28:46 +02002466exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002467 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002468 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002469 mbedtls_psa_crypto_free( );
2470}
2471/* END_CASE */
2472
Gilles Peskine50e586b2018-06-08 14:28:46 +02002473/* BEGIN_CASE */
2474void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002475 data_t *key,
2476 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002477{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002478 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002479 psa_key_type_t key_type = key_type_arg;
2480 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002481 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002482 size_t iv_size = 16;
2483 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002484 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002485 size_t output1_size = 0;
2486 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002487 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002488 size_t output2_size = 0;
2489 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002490 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002491 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2492 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002493 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002494
Gilles Peskine8817f612018-12-18 00:18:46 +01002495 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002496
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002497 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002498 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002499 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002500
Gilles Peskine8817f612018-12-18 00:18:46 +01002501 PSA_ASSERT( psa_import_key( handle, key_type,
2502 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002503
Gilles Peskine8817f612018-12-18 00:18:46 +01002504 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2505 handle, alg ) );
2506 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2507 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002508
Gilles Peskine8817f612018-12-18 00:18:46 +01002509 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2510 iv, iv_size,
2511 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002512 output1_size = ( (size_t) input->len +
2513 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002514 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002515
Gilles Peskine8817f612018-12-18 00:18:46 +01002516 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2517 output1, output1_size,
2518 &output1_length ) );
2519 PSA_ASSERT( psa_cipher_finish( &operation1,
2520 output1 + output1_length, output1_size,
2521 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002522
Gilles Peskine048b7f02018-06-08 14:20:49 +02002523 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002524
Gilles Peskine8817f612018-12-18 00:18:46 +01002525 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002526
2527 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002528 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002529
Gilles Peskine8817f612018-12-18 00:18:46 +01002530 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2531 iv, iv_length ) );
2532 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2533 output2, output2_size,
2534 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002535 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002536 PSA_ASSERT( psa_cipher_finish( &operation2,
2537 output2 + output2_length,
2538 output2_size,
2539 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002540
Gilles Peskine048b7f02018-06-08 14:20:49 +02002541 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002542
Gilles Peskine8817f612018-12-18 00:18:46 +01002543 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002544
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002545 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002546
2547exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002548 mbedtls_free( output1 );
2549 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002550 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03002551 mbedtls_psa_crypto_free( );
2552}
2553/* END_CASE */
2554
2555/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002556void cipher_verify_output_multipart( int alg_arg,
2557 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002558 data_t *key,
2559 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002560 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002561{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002562 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002563 psa_key_type_t key_type = key_type_arg;
2564 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002565 unsigned char iv[16] = {0};
2566 size_t iv_size = 16;
2567 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002568 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002569 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002570 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002571 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002572 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002573 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002574 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002575 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2576 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002577 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002578
Gilles Peskine8817f612018-12-18 00:18:46 +01002579 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002580
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002581 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002582 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002583 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002584
Gilles Peskine8817f612018-12-18 00:18:46 +01002585 PSA_ASSERT( psa_import_key( handle, key_type,
2586 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03002587
Gilles Peskine8817f612018-12-18 00:18:46 +01002588 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2589 handle, alg ) );
2590 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2591 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002592
Gilles Peskine8817f612018-12-18 00:18:46 +01002593 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2594 iv, iv_size,
2595 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002596 output1_buffer_size = ( (size_t) input->len +
2597 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002598 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002599
Gilles Peskine4abf7412018-06-18 16:35:34 +02002600 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002601
Gilles Peskine8817f612018-12-18 00:18:46 +01002602 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2603 output1, output1_buffer_size,
2604 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002605 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002606
Gilles Peskine8817f612018-12-18 00:18:46 +01002607 PSA_ASSERT( psa_cipher_update( &operation1,
2608 input->x + first_part_size,
2609 input->len - first_part_size,
2610 output1, output1_buffer_size,
2611 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002612 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002613
Gilles Peskine8817f612018-12-18 00:18:46 +01002614 PSA_ASSERT( psa_cipher_finish( &operation1,
2615 output1 + output1_length,
2616 output1_buffer_size - output1_length,
2617 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002618 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002619
Gilles Peskine8817f612018-12-18 00:18:46 +01002620 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002621
Gilles Peskine048b7f02018-06-08 14:20:49 +02002622 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002623 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002624
Gilles Peskine8817f612018-12-18 00:18:46 +01002625 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2626 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002627
Gilles Peskine8817f612018-12-18 00:18:46 +01002628 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2629 output2, output2_buffer_size,
2630 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002631 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002632
Gilles Peskine8817f612018-12-18 00:18:46 +01002633 PSA_ASSERT( psa_cipher_update( &operation2,
2634 output1 + first_part_size,
2635 output1_length - first_part_size,
2636 output2, output2_buffer_size,
2637 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002638 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002639
Gilles Peskine8817f612018-12-18 00:18:46 +01002640 PSA_ASSERT( psa_cipher_finish( &operation2,
2641 output2 + output2_length,
2642 output2_buffer_size - output2_length,
2643 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002644 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002645
Gilles Peskine8817f612018-12-18 00:18:46 +01002646 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002647
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002648 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002649
2650exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002651 mbedtls_free( output1 );
2652 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002653 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002654 mbedtls_psa_crypto_free( );
2655}
2656/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002657
Gilles Peskine20035e32018-02-03 22:44:14 +01002658/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002659void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002660 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002661 data_t *nonce,
2662 data_t *additional_data,
2663 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002664 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002665{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002666 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002667 psa_key_type_t key_type = key_type_arg;
2668 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002669 unsigned char *output_data = NULL;
2670 size_t output_size = 0;
2671 size_t output_length = 0;
2672 unsigned char *output_data2 = NULL;
2673 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002674 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002675 psa_status_t expected_result = expected_result_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002676 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002677
Gilles Peskine4abf7412018-06-18 16:35:34 +02002678 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002679 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002680
Gilles Peskine8817f612018-12-18 00:18:46 +01002681 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002682
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002683 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002684 psa_key_policy_set_usage( &policy,
2685 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2686 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002687 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002688
Gilles Peskine8817f612018-12-18 00:18:46 +01002689 PSA_ASSERT( psa_import_key( handle, key_type,
2690 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002691
Gilles Peskinefe11b722018-12-18 00:24:04 +01002692 TEST_EQUAL( psa_aead_encrypt( handle, alg,
2693 nonce->x, nonce->len,
2694 additional_data->x,
2695 additional_data->len,
2696 input_data->x, input_data->len,
2697 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002698 &output_length ),
2699 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002700
2701 if( PSA_SUCCESS == expected_result )
2702 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002703 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002704
Gilles Peskinefe11b722018-12-18 00:24:04 +01002705 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2706 nonce->x, nonce->len,
2707 additional_data->x,
2708 additional_data->len,
2709 output_data, output_length,
2710 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002711 &output_length2 ),
2712 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002713
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002714 ASSERT_COMPARE( input_data->x, input_data->len,
2715 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002716 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002717
Gilles Peskinea1cac842018-06-11 19:33:02 +02002718exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002719 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002720 mbedtls_free( output_data );
2721 mbedtls_free( output_data2 );
2722 mbedtls_psa_crypto_free( );
2723}
2724/* END_CASE */
2725
2726/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002727void aead_encrypt( int key_type_arg, data_t *key_data,
2728 int alg_arg,
2729 data_t *nonce,
2730 data_t *additional_data,
2731 data_t *input_data,
2732 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002733{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002734 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002735 psa_key_type_t key_type = key_type_arg;
2736 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002737 unsigned char *output_data = NULL;
2738 size_t output_size = 0;
2739 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002740 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002741 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002742
Gilles Peskine4abf7412018-06-18 16:35:34 +02002743 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002744 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002745
Gilles Peskine8817f612018-12-18 00:18:46 +01002746 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002747
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002748 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002749 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002750 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002751
Gilles Peskine8817f612018-12-18 00:18:46 +01002752 PSA_ASSERT( psa_import_key( handle, key_type,
2753 key_data->x,
2754 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002755
Gilles Peskine8817f612018-12-18 00:18:46 +01002756 PSA_ASSERT( psa_aead_encrypt( handle, alg,
2757 nonce->x, nonce->len,
2758 additional_data->x, additional_data->len,
2759 input_data->x, input_data->len,
2760 output_data, output_size,
2761 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002762
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002763 ASSERT_COMPARE( expected_result->x, expected_result->len,
2764 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002765
Gilles Peskinea1cac842018-06-11 19:33:02 +02002766exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002767 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002768 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002769 mbedtls_psa_crypto_free( );
2770}
2771/* END_CASE */
2772
2773/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002774void aead_decrypt( int key_type_arg, data_t *key_data,
2775 int alg_arg,
2776 data_t *nonce,
2777 data_t *additional_data,
2778 data_t *input_data,
2779 data_t *expected_data,
2780 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002781{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002782 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002783 psa_key_type_t key_type = key_type_arg;
2784 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002785 unsigned char *output_data = NULL;
2786 size_t output_size = 0;
2787 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002788 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002789 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002790 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002791
Gilles Peskine4abf7412018-06-18 16:35:34 +02002792 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002793 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002794
Gilles Peskine8817f612018-12-18 00:18:46 +01002795 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002796
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002797 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002798 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002799 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002800
Gilles Peskine8817f612018-12-18 00:18:46 +01002801 PSA_ASSERT( psa_import_key( handle, key_type,
2802 key_data->x,
2803 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002804
Gilles Peskinefe11b722018-12-18 00:24:04 +01002805 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2806 nonce->x, nonce->len,
2807 additional_data->x,
2808 additional_data->len,
2809 input_data->x, input_data->len,
2810 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002811 &output_length ),
2812 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002813
Gilles Peskine2d277862018-06-18 15:41:12 +02002814 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002815 ASSERT_COMPARE( expected_data->x, expected_data->len,
2816 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002817
Gilles Peskinea1cac842018-06-11 19:33:02 +02002818exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002819 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002820 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002821 mbedtls_psa_crypto_free( );
2822}
2823/* END_CASE */
2824
2825/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002826void signature_size( int type_arg,
2827 int bits,
2828 int alg_arg,
2829 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002830{
2831 psa_key_type_t type = type_arg;
2832 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002833 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002834 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002835exit:
2836 ;
2837}
2838/* END_CASE */
2839
2840/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002841void sign_deterministic( int key_type_arg, data_t *key_data,
2842 int alg_arg, data_t *input_data,
2843 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002844{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002845 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01002846 psa_key_type_t key_type = key_type_arg;
2847 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002848 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002849 unsigned char *signature = NULL;
2850 size_t signature_size;
2851 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002852 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01002853
Gilles Peskine8817f612018-12-18 00:18:46 +01002854 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002855
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002856 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002857 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002858 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002859
Gilles Peskine8817f612018-12-18 00:18:46 +01002860 PSA_ASSERT( psa_import_key( handle, key_type,
2861 key_data->x,
2862 key_data->len ) );
2863 PSA_ASSERT( psa_get_key_information( handle,
2864 NULL,
2865 &key_bits ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002866
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002867 /* Allocate a buffer which has the size advertized by the
2868 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002869 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2870 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002871 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002872 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002873 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002874
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002875 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002876 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2877 input_data->x, input_data->len,
2878 signature, signature_size,
2879 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002880 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002881 ASSERT_COMPARE( output_data->x, output_data->len,
2882 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002883
2884exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002885 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01002886 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002887 mbedtls_psa_crypto_free( );
2888}
2889/* END_CASE */
2890
2891/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002892void sign_fail( int key_type_arg, data_t *key_data,
2893 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002894 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002895{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002896 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01002897 psa_key_type_t key_type = key_type_arg;
2898 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002899 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002900 psa_status_t actual_status;
2901 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002902 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002903 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002904 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01002905
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002906 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002907
Gilles Peskine8817f612018-12-18 00:18:46 +01002908 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002909
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002910 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002911 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002912 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002913
Gilles Peskine8817f612018-12-18 00:18:46 +01002914 PSA_ASSERT( psa_import_key( handle, key_type,
2915 key_data->x,
2916 key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002917
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002918 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002919 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002920 signature, signature_size,
2921 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002922 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002923 /* The value of *signature_length is unspecified on error, but
2924 * whatever it is, it should be less than signature_size, so that
2925 * if the caller tries to read *signature_length bytes without
2926 * checking the error code then they don't overflow a buffer. */
2927 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002928
2929exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002930 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01002931 mbedtls_free( signature );
2932 mbedtls_psa_crypto_free( );
2933}
2934/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002935
2936/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002937void sign_verify( int key_type_arg, data_t *key_data,
2938 int alg_arg, data_t *input_data )
2939{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002940 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02002941 psa_key_type_t key_type = key_type_arg;
2942 psa_algorithm_t alg = alg_arg;
2943 size_t key_bits;
2944 unsigned char *signature = NULL;
2945 size_t signature_size;
2946 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002947 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02002948
Gilles Peskine8817f612018-12-18 00:18:46 +01002949 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002950
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002951 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002952 psa_key_policy_set_usage( &policy,
2953 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2954 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002955 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002956
Gilles Peskine8817f612018-12-18 00:18:46 +01002957 PSA_ASSERT( psa_import_key( handle, key_type,
2958 key_data->x,
2959 key_data->len ) );
2960 PSA_ASSERT( psa_get_key_information( handle,
2961 NULL,
2962 &key_bits ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002963
2964 /* Allocate a buffer which has the size advertized by the
2965 * library. */
2966 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2967 key_bits, alg );
2968 TEST_ASSERT( signature_size != 0 );
2969 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002970 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002971
2972 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002973 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2974 input_data->x, input_data->len,
2975 signature, signature_size,
2976 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002977 /* Check that the signature length looks sensible. */
2978 TEST_ASSERT( signature_length <= signature_size );
2979 TEST_ASSERT( signature_length > 0 );
2980
2981 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002982 PSA_ASSERT( psa_asymmetric_verify(
2983 handle, alg,
2984 input_data->x, input_data->len,
2985 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002986
2987 if( input_data->len != 0 )
2988 {
2989 /* Flip a bit in the input and verify that the signature is now
2990 * detected as invalid. Flip a bit at the beginning, not at the end,
2991 * because ECDSA may ignore the last few bits of the input. */
2992 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002993 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
2994 input_data->x, input_data->len,
2995 signature, signature_length ),
2996 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02002997 }
2998
2999exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003000 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003001 mbedtls_free( signature );
3002 mbedtls_psa_crypto_free( );
3003}
3004/* END_CASE */
3005
3006/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003007void asymmetric_verify( int key_type_arg, data_t *key_data,
3008 int alg_arg, data_t *hash_data,
3009 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003010{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003011 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003012 psa_key_type_t key_type = key_type_arg;
3013 psa_algorithm_t alg = alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003014 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003015
Gilles Peskine69c12672018-06-28 00:07:19 +02003016 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3017
Gilles Peskine8817f612018-12-18 00:18:46 +01003018 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003019
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003020 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003021 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003022 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
itayzafrir5c753392018-05-08 11:18:38 +03003023
Gilles Peskine8817f612018-12-18 00:18:46 +01003024 PSA_ASSERT( psa_import_key( handle, key_type,
3025 key_data->x,
3026 key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003027
Gilles Peskine8817f612018-12-18 00:18:46 +01003028 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3029 hash_data->x, hash_data->len,
3030 signature_data->x,
3031 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003032exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003033 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03003034 mbedtls_psa_crypto_free( );
3035}
3036/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003037
3038/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003039void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3040 int alg_arg, data_t *hash_data,
3041 data_t *signature_data,
3042 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003043{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003044 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003045 psa_key_type_t key_type = key_type_arg;
3046 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003047 psa_status_t actual_status;
3048 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003049 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003050
Gilles Peskine8817f612018-12-18 00:18:46 +01003051 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003052
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003053 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003054 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003055 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003056
Gilles Peskine8817f612018-12-18 00:18:46 +01003057 PSA_ASSERT( psa_import_key( handle, key_type,
3058 key_data->x,
3059 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003060
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003061 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003062 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003063 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003064 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003065
Gilles Peskinefe11b722018-12-18 00:24:04 +01003066 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003067
3068exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003069 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003070 mbedtls_psa_crypto_free( );
3071}
3072/* END_CASE */
3073
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003074/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003075void asymmetric_encrypt( int key_type_arg,
3076 data_t *key_data,
3077 int alg_arg,
3078 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003079 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003080 int expected_output_length_arg,
3081 int expected_status_arg )
3082{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003083 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003084 psa_key_type_t key_type = key_type_arg;
3085 psa_algorithm_t alg = alg_arg;
3086 size_t expected_output_length = expected_output_length_arg;
3087 size_t key_bits;
3088 unsigned char *output = NULL;
3089 size_t output_size;
3090 size_t output_length = ~0;
3091 psa_status_t actual_status;
3092 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003093 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003094
Gilles Peskine8817f612018-12-18 00:18:46 +01003095 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003096
Gilles Peskine656896e2018-06-29 19:12:28 +02003097 /* Import the key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003098 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003099 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003100 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
3101 PSA_ASSERT( psa_import_key( handle, key_type,
3102 key_data->x,
3103 key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003104
3105 /* Determine the maximum output length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003106 PSA_ASSERT( psa_get_key_information( handle,
3107 NULL,
3108 &key_bits ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003109 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003110 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003111
3112 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003113 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003114 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003115 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003116 output, output_size,
3117 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003118 TEST_EQUAL( actual_status, expected_status );
3119 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003120
Gilles Peskine68428122018-06-30 18:42:41 +02003121 /* If the label is empty, the test framework puts a non-null pointer
3122 * in label->x. Test that a null pointer works as well. */
3123 if( label->len == 0 )
3124 {
3125 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003126 if( output_size != 0 )
3127 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003128 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003129 input_data->x, input_data->len,
3130 NULL, label->len,
3131 output, output_size,
3132 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003133 TEST_EQUAL( actual_status, expected_status );
3134 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003135 }
3136
Gilles Peskine656896e2018-06-29 19:12:28 +02003137exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003138 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003139 mbedtls_free( output );
3140 mbedtls_psa_crypto_free( );
3141}
3142/* END_CASE */
3143
3144/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003145void asymmetric_encrypt_decrypt( int key_type_arg,
3146 data_t *key_data,
3147 int alg_arg,
3148 data_t *input_data,
3149 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003150{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003151 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003152 psa_key_type_t key_type = key_type_arg;
3153 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003154 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003155 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003156 size_t output_size;
3157 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003158 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003159 size_t output2_size;
3160 size_t output2_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003161 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003162
Gilles Peskine8817f612018-12-18 00:18:46 +01003163 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003164
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003165 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003166 psa_key_policy_set_usage( &policy,
3167 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003168 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003169 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003170
Gilles Peskine8817f612018-12-18 00:18:46 +01003171 PSA_ASSERT( psa_import_key( handle, key_type,
3172 key_data->x,
3173 key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003174
3175 /* Determine the maximum ciphertext length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003176 PSA_ASSERT( psa_get_key_information( handle,
3177 NULL,
3178 &key_bits ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003179 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003180 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003181 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003182 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003183
Gilles Peskineeebd7382018-06-08 18:11:54 +02003184 /* We test encryption by checking that encrypt-then-decrypt gives back
3185 * the original plaintext because of the non-optional random
3186 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003187 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3188 input_data->x, input_data->len,
3189 label->x, label->len,
3190 output, output_size,
3191 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003192 /* We don't know what ciphertext length to expect, but check that
3193 * it looks sensible. */
3194 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003195
Gilles Peskine8817f612018-12-18 00:18:46 +01003196 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3197 output, output_length,
3198 label->x, label->len,
3199 output2, output2_size,
3200 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003201 ASSERT_COMPARE( input_data->x, input_data->len,
3202 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003203
3204exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003205 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003206 mbedtls_free( output );
3207 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003208 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003209}
3210/* END_CASE */
3211
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003212/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003213void asymmetric_decrypt( int key_type_arg,
3214 data_t *key_data,
3215 int alg_arg,
3216 data_t *input_data,
3217 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003218 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003219{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003220 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003221 psa_key_type_t key_type = key_type_arg;
3222 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003223 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003224 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003225 size_t output_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003226 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003227
Gilles Peskine4abf7412018-06-18 16:35:34 +02003228 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003229 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003230
Gilles Peskine8817f612018-12-18 00:18:46 +01003231 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003232
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003233 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003234 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003235 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003236
Gilles Peskine8817f612018-12-18 00:18:46 +01003237 PSA_ASSERT( psa_import_key( handle, key_type,
3238 key_data->x,
3239 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003240
Gilles Peskine8817f612018-12-18 00:18:46 +01003241 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3242 input_data->x, input_data->len,
3243 label->x, label->len,
3244 output,
3245 output_size,
3246 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003247 ASSERT_COMPARE( expected_data->x, expected_data->len,
3248 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003249
Gilles Peskine68428122018-06-30 18:42:41 +02003250 /* If the label is empty, the test framework puts a non-null pointer
3251 * in label->x. Test that a null pointer works as well. */
3252 if( label->len == 0 )
3253 {
3254 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003255 if( output_size != 0 )
3256 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003257 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3258 input_data->x, input_data->len,
3259 NULL, label->len,
3260 output,
3261 output_size,
3262 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003263 ASSERT_COMPARE( expected_data->x, expected_data->len,
3264 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003265 }
3266
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003267exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003268 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003269 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003270 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003271}
3272/* END_CASE */
3273
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003274/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003275void asymmetric_decrypt_fail( int key_type_arg,
3276 data_t *key_data,
3277 int alg_arg,
3278 data_t *input_data,
3279 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003280 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003281{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003282 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003283 psa_key_type_t key_type = key_type_arg;
3284 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003285 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003286 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003287 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003288 psa_status_t actual_status;
3289 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003290 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003291
Gilles Peskine4abf7412018-06-18 16:35:34 +02003292 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003293 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003294
Gilles Peskine8817f612018-12-18 00:18:46 +01003295 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003296
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003297 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003298 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003299 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003300
Gilles Peskine8817f612018-12-18 00:18:46 +01003301 PSA_ASSERT( psa_import_key( handle, key_type,
3302 key_data->x,
3303 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003304
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003305 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003306 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003307 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003308 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003309 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003310 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003311 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003312
Gilles Peskine68428122018-06-30 18:42:41 +02003313 /* If the label is empty, the test framework puts a non-null pointer
3314 * in label->x. Test that a null pointer works as well. */
3315 if( label->len == 0 )
3316 {
3317 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003318 if( output_size != 0 )
3319 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003320 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003321 input_data->x, input_data->len,
3322 NULL, label->len,
3323 output, output_size,
3324 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003325 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003326 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003327 }
3328
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003329exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003330 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02003331 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003332 mbedtls_psa_crypto_free( );
3333}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003334/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003335
3336/* BEGIN_CASE */
Jaeden Amerod94d6712019-01-04 14:11:48 +00003337void crypto_generator_init( )
3338{
3339 /* Test each valid way of initializing the object, except for `= {0}`, as
3340 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3341 * though it's OK by the C standard. We could test for this, but we'd need
3342 * to supress the Clang warning for the test. */
3343 psa_crypto_generator_t func = psa_crypto_generator_init( );
3344 psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT;
3345 psa_crypto_generator_t zero;
3346
3347 memset( &zero, 0, sizeof( zero ) );
3348
3349 /* Although not technically guaranteed by the C standard nor the PSA Crypto
3350 * specification, we test that all valid ways of initializing the object
3351 * have the same bit pattern. This is a stronger requirement that may not
3352 * be valid on all platforms or PSA Crypto implementations, but implies the
3353 * weaker actual requirement is met: that a freshly initialized object, no
3354 * matter how it was initialized, acts the same as any other valid
3355 * initialization. */
3356 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
3357 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
3358}
3359/* END_CASE */
3360
3361/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003362void derive_setup( int key_type_arg,
3363 data_t *key_data,
3364 int alg_arg,
3365 data_t *salt,
3366 data_t *label,
3367 int requested_capacity_arg,
3368 int expected_status_arg )
3369{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003370 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003371 size_t key_type = key_type_arg;
3372 psa_algorithm_t alg = alg_arg;
3373 size_t requested_capacity = requested_capacity_arg;
3374 psa_status_t expected_status = expected_status_arg;
3375 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003376 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003377
Gilles Peskine8817f612018-12-18 00:18:46 +01003378 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003379
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003380 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003381 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003382 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003383
Gilles Peskine8817f612018-12-18 00:18:46 +01003384 PSA_ASSERT( psa_import_key( handle, key_type,
3385 key_data->x,
3386 key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003387
Gilles Peskinefe11b722018-12-18 00:24:04 +01003388 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3389 salt->x, salt->len,
3390 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003391 requested_capacity ),
3392 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003393
3394exit:
3395 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003396 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003397 mbedtls_psa_crypto_free( );
3398}
3399/* END_CASE */
3400
3401/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003402void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003403{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003404 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003405 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003406 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003407 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003408 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003409 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003410 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3411 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3412 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Jaeden Amero70261c52019-01-04 11:47:20 +00003413 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003414
Gilles Peskine8817f612018-12-18 00:18:46 +01003415 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003416
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003417 PSA_ASSERT( psa_allocate_key( &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003418 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003419 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003420
Gilles Peskine8817f612018-12-18 00:18:46 +01003421 PSA_ASSERT( psa_import_key( handle, key_type,
3422 key_data,
3423 sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003424
3425 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01003426 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3427 NULL, 0,
3428 NULL, 0,
3429 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003430
3431 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003432 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3433 NULL, 0,
3434 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003435 capacity ),
3436 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003437
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003438 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003439
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003440 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
3441 PSA_ERROR_INSUFFICIENT_CAPACITY );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003442
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003443exit:
3444 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003445 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003446 mbedtls_psa_crypto_free( );
3447}
3448/* END_CASE */
3449
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003450/* BEGIN_CASE */
3451void test_derive_invalid_generator_tests( )
3452{
3453 uint8_t output_buffer[16];
3454 size_t buffer_size = 16;
3455 size_t capacity = 0;
3456 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3457
Nir Sonnenschein50789302018-10-31 12:16:38 +02003458 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003459 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003460
3461 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003462 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003463
Gilles Peskine8817f612018-12-18 00:18:46 +01003464 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003465
Nir Sonnenschein50789302018-10-31 12:16:38 +02003466 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003467 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003468
Nir Sonnenschein50789302018-10-31 12:16:38 +02003469 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003470 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003471
3472exit:
3473 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003474}
3475/* END_CASE */
3476
3477/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003478void derive_output( int alg_arg,
3479 data_t *key_data,
3480 data_t *salt,
3481 data_t *label,
3482 int requested_capacity_arg,
3483 data_t *expected_output1,
3484 data_t *expected_output2 )
3485{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003486 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003487 psa_algorithm_t alg = alg_arg;
3488 size_t requested_capacity = requested_capacity_arg;
3489 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3490 uint8_t *expected_outputs[2] =
3491 {expected_output1->x, expected_output2->x};
3492 size_t output_sizes[2] =
3493 {expected_output1->len, expected_output2->len};
3494 size_t output_buffer_size = 0;
3495 uint8_t *output_buffer = NULL;
3496 size_t expected_capacity;
3497 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003498 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003499 psa_status_t status;
3500 unsigned i;
3501
3502 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3503 {
3504 if( output_sizes[i] > output_buffer_size )
3505 output_buffer_size = output_sizes[i];
3506 if( output_sizes[i] == 0 )
3507 expected_outputs[i] = NULL;
3508 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003509 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003510 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003511
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003512 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003513 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003514 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003515
Gilles Peskine8817f612018-12-18 00:18:46 +01003516 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3517 key_data->x,
3518 key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003519
3520 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003521 if( PSA_ALG_IS_HKDF( alg ) )
3522 {
3523 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
3524 PSA_ASSERT( psa_set_generator_capacity( &generator,
3525 requested_capacity ) );
3526 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
3527 PSA_KDF_STEP_SALT,
3528 salt->x, salt->len ) );
3529 PSA_ASSERT( psa_key_derivation_input_key( &generator,
3530 PSA_KDF_STEP_SECRET,
3531 handle ) );
3532 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
3533 PSA_KDF_STEP_INFO,
3534 label->x, label->len ) );
3535 }
3536 else
3537 {
3538 // legacy
3539 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3540 salt->x, salt->len,
3541 label->x, label->len,
3542 requested_capacity ) );
3543 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003544 PSA_ASSERT( psa_get_generator_capacity( &generator,
3545 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003546 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003547 expected_capacity = requested_capacity;
3548
3549 /* Expansion phase. */
3550 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3551 {
3552 /* Read some bytes. */
3553 status = psa_generator_read( &generator,
3554 output_buffer, output_sizes[i] );
3555 if( expected_capacity == 0 && output_sizes[i] == 0 )
3556 {
3557 /* Reading 0 bytes when 0 bytes are available can go either way. */
3558 TEST_ASSERT( status == PSA_SUCCESS ||
3559 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3560 continue;
3561 }
3562 else if( expected_capacity == 0 ||
3563 output_sizes[i] > expected_capacity )
3564 {
3565 /* Capacity exceeded. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003566 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003567 expected_capacity = 0;
3568 continue;
3569 }
3570 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003571 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003572 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003573 ASSERT_COMPARE( output_buffer, output_sizes[i],
3574 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003575 /* Check the generator status. */
3576 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01003577 PSA_ASSERT( psa_get_generator_capacity( &generator,
3578 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003579 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003580 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003581 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003582
3583exit:
3584 mbedtls_free( output_buffer );
3585 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003586 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003587 mbedtls_psa_crypto_free( );
3588}
3589/* END_CASE */
3590
3591/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003592void derive_full( int alg_arg,
3593 data_t *key_data,
3594 data_t *salt,
3595 data_t *label,
3596 int requested_capacity_arg )
3597{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003598 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02003599 psa_algorithm_t alg = alg_arg;
3600 size_t requested_capacity = requested_capacity_arg;
3601 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3602 unsigned char output_buffer[16];
3603 size_t expected_capacity = requested_capacity;
3604 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003605 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02003606
Gilles Peskine8817f612018-12-18 00:18:46 +01003607 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003608
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003609 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003610 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003611 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003612
Gilles Peskine8817f612018-12-18 00:18:46 +01003613 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3614 key_data->x,
3615 key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003616
3617 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003618 if( PSA_ALG_IS_HKDF( alg ) )
3619 {
3620 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
3621 PSA_ASSERT( psa_set_generator_capacity( &generator,
3622 requested_capacity ) );
3623 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
3624 PSA_KDF_STEP_SALT,
3625 salt->x, salt->len ) );
3626 PSA_ASSERT( psa_key_derivation_input_key( &generator,
3627 PSA_KDF_STEP_SECRET,
3628 handle ) );
3629 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
3630 PSA_KDF_STEP_INFO,
3631 label->x, label->len ) );
3632 }
3633 else
3634 {
3635 // legacy
3636 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3637 salt->x, salt->len,
3638 label->x, label->len,
3639 requested_capacity ) );
3640 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003641 PSA_ASSERT( psa_get_generator_capacity( &generator,
3642 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003643 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003644
3645 /* Expansion phase. */
3646 while( current_capacity > 0 )
3647 {
3648 size_t read_size = sizeof( output_buffer );
3649 if( read_size > current_capacity )
3650 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01003651 PSA_ASSERT( psa_generator_read( &generator,
3652 output_buffer,
3653 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003654 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01003655 PSA_ASSERT( psa_get_generator_capacity( &generator,
3656 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003657 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003658 }
3659
3660 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003661 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
3662 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskined54931c2018-07-17 21:06:59 +02003663
Gilles Peskine8817f612018-12-18 00:18:46 +01003664 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003665
3666exit:
3667 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003668 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02003669 mbedtls_psa_crypto_free( );
3670}
3671/* END_CASE */
3672
3673/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003674void derive_key_exercise( int alg_arg,
3675 data_t *key_data,
3676 data_t *salt,
3677 data_t *label,
3678 int derived_type_arg,
3679 int derived_bits_arg,
3680 int derived_usage_arg,
3681 int derived_alg_arg )
3682{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003683 psa_key_handle_t base_handle = 0;
3684 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003685 psa_algorithm_t alg = alg_arg;
3686 psa_key_type_t derived_type = derived_type_arg;
3687 size_t derived_bits = derived_bits_arg;
3688 psa_key_usage_t derived_usage = derived_usage_arg;
3689 psa_algorithm_t derived_alg = derived_alg_arg;
3690 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3691 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003692 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003693 psa_key_type_t got_type;
3694 size_t got_bits;
3695
Gilles Peskine8817f612018-12-18 00:18:46 +01003696 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003697
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003698 PSA_ASSERT( psa_allocate_key( &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003699 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003700 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3701 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3702 key_data->x,
3703 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003704
3705 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003706 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3707 salt->x, salt->len,
3708 label->x, label->len,
3709 capacity ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003710 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003711 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003712 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3713 PSA_ASSERT( psa_generator_import_key( derived_handle,
3714 derived_type,
3715 derived_bits,
3716 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003717
3718 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01003719 PSA_ASSERT( psa_get_key_information( derived_handle,
3720 &got_type,
3721 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003722 TEST_EQUAL( got_type, derived_type );
3723 TEST_EQUAL( got_bits, derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003724
3725 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003726 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02003727 goto exit;
3728
3729exit:
3730 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003731 psa_destroy_key( base_handle );
3732 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003733 mbedtls_psa_crypto_free( );
3734}
3735/* END_CASE */
3736
3737/* BEGIN_CASE */
3738void derive_key_export( int alg_arg,
3739 data_t *key_data,
3740 data_t *salt,
3741 data_t *label,
3742 int bytes1_arg,
3743 int bytes2_arg )
3744{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003745 psa_key_handle_t base_handle = 0;
3746 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003747 psa_algorithm_t alg = alg_arg;
3748 size_t bytes1 = bytes1_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003749 size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003750 size_t bytes2 = bytes2_arg;
3751 size_t capacity = bytes1 + bytes2;
3752 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003753 uint8_t *output_buffer = NULL;
3754 uint8_t *export_buffer = NULL;
Jaeden Amero70261c52019-01-04 11:47:20 +00003755 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003756 size_t length;
3757
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003758 ASSERT_ALLOC( output_buffer, capacity );
3759 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01003760 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003761
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003762 PSA_ASSERT( psa_allocate_key( &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003763 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003764 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3765 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3766 key_data->x,
3767 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003768
3769 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003770 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3771 salt->x, salt->len,
3772 label->x, label->len,
3773 capacity ) );
3774 PSA_ASSERT( psa_generator_read( &generator,
3775 output_buffer,
3776 capacity ) );
3777 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003778
3779 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003780 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3781 salt->x, salt->len,
3782 label->x, label->len,
3783 capacity ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003784 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003785 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003786 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3787 PSA_ASSERT( psa_generator_import_key( derived_handle,
3788 PSA_KEY_TYPE_RAW_DATA,
3789 derived_bits,
3790 &generator ) );
3791 PSA_ASSERT( psa_export_key( derived_handle,
3792 export_buffer, bytes1,
3793 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003794 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003795 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003796 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003797 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3798 PSA_ASSERT( psa_generator_import_key( derived_handle,
3799 PSA_KEY_TYPE_RAW_DATA,
3800 PSA_BYTES_TO_BITS( bytes2 ),
3801 &generator ) );
3802 PSA_ASSERT( psa_export_key( derived_handle,
3803 export_buffer + bytes1, bytes2,
3804 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003805 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003806
3807 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003808 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
3809 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003810
3811exit:
3812 mbedtls_free( output_buffer );
3813 mbedtls_free( export_buffer );
3814 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003815 psa_destroy_key( base_handle );
3816 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003817 mbedtls_psa_crypto_free( );
3818}
3819/* END_CASE */
3820
3821/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02003822void key_agreement_setup( int alg_arg,
3823 int our_key_type_arg, data_t *our_key_data,
3824 data_t *peer_key_data,
3825 int expected_status_arg )
3826{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003827 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003828 psa_algorithm_t alg = alg_arg;
3829 psa_key_type_t our_key_type = our_key_type_arg;
3830 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003831 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003832
Gilles Peskine8817f612018-12-18 00:18:46 +01003833 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003834
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003835 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003836 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003837 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3838 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3839 our_key_data->x,
3840 our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003841
Gilles Peskine969c5d62019-01-16 15:53:06 +01003842 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
3843 TEST_EQUAL( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003844 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003845 peer_key_data->x, peer_key_data->len ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003846 expected_status_arg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003847
3848exit:
3849 psa_generator_abort( &generator );
3850 psa_destroy_key( our_key );
3851 mbedtls_psa_crypto_free( );
3852}
3853/* END_CASE */
3854
3855/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02003856void key_agreement_capacity( int alg_arg,
3857 int our_key_type_arg, data_t *our_key_data,
3858 data_t *peer_key_data,
3859 int expected_capacity_arg )
3860{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003861 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003862 psa_algorithm_t alg = alg_arg;
3863 psa_key_type_t our_key_type = our_key_type_arg;
3864 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003865 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02003866 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02003867 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02003868
Gilles Peskine8817f612018-12-18 00:18:46 +01003869 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003870
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003871 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003872 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003873 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3874 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3875 our_key_data->x,
3876 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003877
Gilles Peskine969c5d62019-01-16 15:53:06 +01003878 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
3879 PSA_ASSERT( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskine8817f612018-12-18 00:18:46 +01003880 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003881 peer_key_data->x, peer_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003882
Gilles Peskinebf491972018-10-25 22:36:12 +02003883 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003884 PSA_ASSERT( psa_get_generator_capacity(
3885 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003886 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02003887
Gilles Peskinebf491972018-10-25 22:36:12 +02003888 /* Test the actual capacity by reading the output. */
3889 while( actual_capacity > sizeof( output ) )
3890 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003891 PSA_ASSERT( psa_generator_read( &generator,
3892 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02003893 actual_capacity -= sizeof( output );
3894 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003895 PSA_ASSERT( psa_generator_read( &generator,
3896 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003897 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
3898 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskinebf491972018-10-25 22:36:12 +02003899
Gilles Peskine59685592018-09-18 12:11:34 +02003900exit:
3901 psa_generator_abort( &generator );
3902 psa_destroy_key( our_key );
3903 mbedtls_psa_crypto_free( );
3904}
3905/* END_CASE */
3906
3907/* BEGIN_CASE */
3908void key_agreement_output( int alg_arg,
3909 int our_key_type_arg, data_t *our_key_data,
3910 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003911 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02003912{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003913 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003914 psa_algorithm_t alg = alg_arg;
3915 psa_key_type_t our_key_type = our_key_type_arg;
3916 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003917 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003918 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02003919
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003920 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
3921 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003922
Gilles Peskine8817f612018-12-18 00:18:46 +01003923 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003924
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003925 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003926 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003927 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3928 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3929 our_key_data->x,
3930 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003931
Gilles Peskine969c5d62019-01-16 15:53:06 +01003932 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
3933 PSA_ASSERT( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskine8817f612018-12-18 00:18:46 +01003934 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003935 peer_key_data->x, peer_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003936
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003937 PSA_ASSERT( psa_generator_read( &generator,
3938 actual_output,
3939 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003940 ASSERT_COMPARE( actual_output, expected_output1->len,
3941 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003942 if( expected_output2->len != 0 )
3943 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003944 PSA_ASSERT( psa_generator_read( &generator,
3945 actual_output,
3946 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003947 ASSERT_COMPARE( actual_output, expected_output2->len,
3948 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003949 }
Gilles Peskine59685592018-09-18 12:11:34 +02003950
3951exit:
3952 psa_generator_abort( &generator );
3953 psa_destroy_key( our_key );
3954 mbedtls_psa_crypto_free( );
3955 mbedtls_free( actual_output );
3956}
3957/* END_CASE */
3958
3959/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003960void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003961{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003962 size_t bytes = bytes_arg;
3963 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003964 unsigned char *output = NULL;
3965 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003966 size_t i;
3967 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003968
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003969 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3970 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003971 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003972
Gilles Peskine8817f612018-12-18 00:18:46 +01003973 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003974
Gilles Peskinea50d7392018-06-21 10:22:13 +02003975 /* Run several times, to ensure that every output byte will be
3976 * nonzero at least once with overwhelming probability
3977 * (2^(-8*number_of_runs)). */
3978 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003979 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003980 if( bytes != 0 )
3981 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01003982 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003983
3984 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003985 ASSERT_COMPARE( output + bytes, sizeof( trail ),
3986 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003987
3988 for( i = 0; i < bytes; i++ )
3989 {
3990 if( output[i] != 0 )
3991 ++changed[i];
3992 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003993 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003994
3995 /* Check that every byte was changed to nonzero at least once. This
3996 * validates that psa_generate_random is overwriting every byte of
3997 * the output buffer. */
3998 for( i = 0; i < bytes; i++ )
3999 {
4000 TEST_ASSERT( changed[i] != 0 );
4001 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004002
4003exit:
4004 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004005 mbedtls_free( output );
4006 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004007}
4008/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004009
4010/* BEGIN_CASE */
4011void generate_key( int type_arg,
4012 int bits_arg,
4013 int usage_arg,
4014 int alg_arg,
4015 int expected_status_arg )
4016{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004017 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004018 psa_key_type_t type = type_arg;
4019 psa_key_usage_t usage = usage_arg;
4020 size_t bits = bits_arg;
4021 psa_algorithm_t alg = alg_arg;
4022 psa_status_t expected_status = expected_status_arg;
4023 psa_key_type_t got_type;
4024 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004025 psa_status_t expected_info_status =
4026 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004027 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004028
Gilles Peskine8817f612018-12-18 00:18:46 +01004029 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004030
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004031 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004032 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004033 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004034
4035 /* Generate a key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004036 TEST_EQUAL( psa_generate_key( handle, type, bits, NULL, 0 ),
4037 expected_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004038
4039 /* Test the key information */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004040 TEST_EQUAL( psa_get_key_information( handle, &got_type, &got_bits ),
4041 expected_info_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004042 if( expected_info_status != PSA_SUCCESS )
4043 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004044 TEST_EQUAL( got_type, type );
4045 TEST_EQUAL( got_bits, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004046
Gilles Peskine818ca122018-06-20 18:16:48 +02004047 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004048 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004049 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004050
4051exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004052 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004053 mbedtls_psa_crypto_free( );
4054}
4055/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004056
Darryl Greend49a4992018-06-18 17:27:26 +01004057/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
4058void persistent_key_load_key_from_storage( data_t *data, int type_arg,
4059 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00004060 int alg_arg, int generation_method,
4061 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01004062{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004063 psa_key_handle_t handle = 0;
4064 psa_key_handle_t base_key;
Darryl Greend49a4992018-06-18 17:27:26 +01004065 psa_key_type_t type = (psa_key_type_t) type_arg;
4066 psa_key_type_t type_get;
4067 size_t bits_get;
Jaeden Amero70261c52019-01-04 11:47:20 +00004068 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
4069 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004070 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
4071 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00004072 psa_key_policy_t base_policy_set = PSA_KEY_POLICY_INIT;
Darryl Green0c6575a2018-11-07 16:05:30 +00004073 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
4074 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004075 unsigned char *first_export = NULL;
4076 unsigned char *second_export = NULL;
4077 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4078 size_t first_exported_length;
4079 size_t second_exported_length;
4080
4081 ASSERT_ALLOC( first_export, export_size );
4082 ASSERT_ALLOC( second_export, export_size );
4083
Gilles Peskine8817f612018-12-18 00:18:46 +01004084 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004085
Gilles Peskine8817f612018-12-18 00:18:46 +01004086 PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
Gilles Peskine8817f612018-12-18 00:18:46 +01004087 &handle ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004088 psa_key_policy_set_usage( &policy_set, policy_usage,
4089 policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004090 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004091
Darryl Green0c6575a2018-11-07 16:05:30 +00004092 switch( generation_method )
4093 {
4094 case IMPORT_KEY:
4095 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004096 PSA_ASSERT( psa_import_key( handle, type,
4097 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004098 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004099
Darryl Green0c6575a2018-11-07 16:05:30 +00004100 case GENERATE_KEY:
4101 /* Generate a key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004102 PSA_ASSERT( psa_generate_key( handle, type, bits,
4103 NULL, 0 ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004104 break;
4105
4106 case DERIVE_KEY:
4107 /* Create base key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004108 PSA_ASSERT( psa_allocate_key( &base_key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004109 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
4110 base_policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004111 PSA_ASSERT( psa_set_key_policy(
4112 base_key, &base_policy_set ) );
4113 PSA_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
4114 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004115 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004116 PSA_ASSERT( psa_key_derivation( &generator, base_key,
4117 base_policy_alg,
4118 NULL, 0, NULL, 0,
4119 export_size ) );
4120 PSA_ASSERT( psa_generator_import_key(
4121 handle, PSA_KEY_TYPE_RAW_DATA,
4122 bits, &generator ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004123 break;
4124 }
Darryl Greend49a4992018-06-18 17:27:26 +01004125
4126 /* Export the key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004127 TEST_EQUAL( psa_export_key( handle,
4128 first_export, export_size,
4129 &first_exported_length ),
4130 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004131
4132 /* Shutdown and restart */
4133 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004134 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004135
Darryl Greend49a4992018-06-18 17:27:26 +01004136 /* Check key slot still contains key data */
Gilles Peskine8817f612018-12-18 00:18:46 +01004137 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4138 &handle ) );
4139 PSA_ASSERT( psa_get_key_information(
4140 handle, &type_get, &bits_get ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004141 TEST_EQUAL( type_get, type );
4142 TEST_EQUAL( bits_get, (size_t) bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004143
Gilles Peskine8817f612018-12-18 00:18:46 +01004144 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004145 TEST_EQUAL( psa_key_policy_get_usage( &policy_get ), policy_usage );
4146 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_get ), policy_alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004147
4148 /* Export the key again */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004149 TEST_EQUAL( psa_export_key( handle,
4150 second_export, export_size,
4151 &second_exported_length ),
4152 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004153
Darryl Green0c6575a2018-11-07 16:05:30 +00004154 if( export_status == PSA_SUCCESS )
4155 {
4156 ASSERT_COMPARE( first_export, first_exported_length,
4157 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01004158
Darryl Green0c6575a2018-11-07 16:05:30 +00004159 switch( generation_method )
4160 {
4161 case IMPORT_KEY:
4162 ASSERT_COMPARE( data->x, data->len,
4163 first_export, first_exported_length );
4164 break;
4165 default:
4166 break;
4167 }
4168 }
4169
4170 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004171 if( ! exercise_key( handle, policy_usage, policy_alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004172 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004173
4174exit:
4175 mbedtls_free( first_export );
4176 mbedtls_free( second_export );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004177 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004178 mbedtls_psa_crypto_free();
4179}
4180/* END_CASE */