blob: 4891064f959716e38d4c0df8886fa450e881b8ac [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 Peskine8817f612018-12-18 00:18:46 +0100369 PSA_ASSERT( psa_key_derivation( &generator,
370 handle, alg,
371 label, label_length,
372 seed, seed_length,
373 sizeof( output ) ) );
374 PSA_ASSERT( psa_generator_read( &generator,
375 output,
376 sizeof( output ) ) );
377 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200378 }
379
380 return( 1 );
381
382exit:
383 return( 0 );
384}
385
Gilles Peskinec7998b72018-11-07 18:45:02 +0100386/* We need two keys to exercise key agreement. Exercise the
387 * private key against its own public key. */
388static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator,
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100389 psa_key_handle_t handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100390 psa_algorithm_t alg )
391{
392 psa_key_type_t private_key_type;
393 psa_key_type_t public_key_type;
394 size_t key_bits;
395 uint8_t *public_key = NULL;
396 size_t public_key_length;
397 /* Return UNKNOWN_ERROR if something other than the final call to
398 * psa_key_agreement fails. This isn't fully satisfactory, but it's
399 * good enough: callers will report it as a failed test anyway. */
400 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
401
Gilles Peskine8817f612018-12-18 00:18:46 +0100402 PSA_ASSERT( psa_get_key_information( handle,
403 &private_key_type,
404 &key_bits ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100405 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
406 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
407 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100408 PSA_ASSERT( psa_export_public_key( handle,
409 public_key, public_key_length,
410 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100411
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100412 status = psa_key_agreement( generator, handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100413 public_key, public_key_length,
414 alg );
415exit:
416 mbedtls_free( public_key );
417 return( status );
418}
419
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100420static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200421 psa_key_usage_t usage,
422 psa_algorithm_t alg )
423{
424 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200425 unsigned char output[1];
426 int ok = 0;
427
428 if( usage & PSA_KEY_USAGE_DERIVE )
429 {
430 /* We need two keys to exercise key agreement. Exercise the
431 * private key against its own public key. */
Gilles Peskine8817f612018-12-18 00:18:46 +0100432 PSA_ASSERT( key_agreement_with_self( &generator, handle, alg ) );
433 PSA_ASSERT( psa_generator_read( &generator,
434 output,
435 sizeof( output ) ) );
436 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200437 }
438 ok = 1;
439
440exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200441 return( ok );
442}
443
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200444static int is_oid_of_key_type( psa_key_type_t type,
445 const uint8_t *oid, size_t oid_length )
446{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200447 const uint8_t *expected_oid = NULL;
448 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200449#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200450 if( PSA_KEY_TYPE_IS_RSA( type ) )
451 {
452 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
453 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
454 }
455 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200456#endif /* MBEDTLS_RSA_C */
457#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200458 if( PSA_KEY_TYPE_IS_ECC( type ) )
459 {
460 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
461 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
462 }
463 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200464#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200465 {
466 char message[40];
467 mbedtls_snprintf( message, sizeof( message ),
468 "OID not known for key type=0x%08lx",
469 (unsigned long) type );
470 test_fail( message, __LINE__, __FILE__ );
471 return( 0 );
472 }
473
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200474 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200475 return( 1 );
476
477exit:
478 return( 0 );
479}
480
481static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
482 size_t min_bits, size_t max_bits,
483 int must_be_odd )
484{
485 size_t len;
486 size_t actual_bits;
487 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100488 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100489 MBEDTLS_ASN1_INTEGER ),
490 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200491 /* Tolerate a slight departure from DER encoding:
492 * - 0 may be represented by an empty string or a 1-byte string.
493 * - The sign bit may be used as a value bit. */
494 if( ( len == 1 && ( *p )[0] == 0 ) ||
495 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
496 {
497 ++( *p );
498 --len;
499 }
500 if( min_bits == 0 && len == 0 )
501 return( 1 );
502 msb = ( *p )[0];
503 TEST_ASSERT( msb != 0 );
504 actual_bits = 8 * ( len - 1 );
505 while( msb != 0 )
506 {
507 msb >>= 1;
508 ++actual_bits;
509 }
510 TEST_ASSERT( actual_bits >= min_bits );
511 TEST_ASSERT( actual_bits <= max_bits );
512 if( must_be_odd )
513 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
514 *p += len;
515 return( 1 );
516exit:
517 return( 0 );
518}
519
520static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
521 size_t *len,
522 unsigned char n, unsigned char tag )
523{
524 int ret;
525 ret = mbedtls_asn1_get_tag( p, end, len,
526 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
527 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
528 if( ret != 0 )
529 return( ret );
530 end = *p + *len;
531 ret = mbedtls_asn1_get_tag( p, end, len, tag );
532 if( ret != 0 )
533 return( ret );
534 if( *p + *len != end )
535 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
536 return( 0 );
537}
538
539static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
540 uint8_t *exported, size_t exported_length )
541{
542 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100543 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200544 else
545 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200546
547#if defined(MBEDTLS_DES_C)
548 if( type == PSA_KEY_TYPE_DES )
549 {
550 /* Check the parity bits. */
551 unsigned i;
552 for( i = 0; i < bits / 8; i++ )
553 {
554 unsigned bit_count = 0;
555 unsigned m;
556 for( m = 1; m <= 0x100; m <<= 1 )
557 {
558 if( exported[i] & m )
559 ++bit_count;
560 }
561 TEST_ASSERT( bit_count % 2 != 0 );
562 }
563 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200564 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200565#endif
566
567#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
568 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
569 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200570 uint8_t *p = exported;
571 uint8_t *end = exported + exported_length;
572 size_t len;
573 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200574 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200575 * modulus INTEGER, -- n
576 * publicExponent INTEGER, -- e
577 * privateExponent INTEGER, -- d
578 * prime1 INTEGER, -- p
579 * prime2 INTEGER, -- q
580 * exponent1 INTEGER, -- d mod (p-1)
581 * exponent2 INTEGER, -- d mod (q-1)
582 * coefficient INTEGER, -- (inverse of q) mod p
583 * }
584 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100585 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
586 MBEDTLS_ASN1_SEQUENCE |
587 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
588 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200589 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
590 goto exit;
591 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
592 goto exit;
593 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
594 goto exit;
595 /* Require d to be at least half the size of n. */
596 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
597 goto exit;
598 /* Require p and q to be at most half the size of n, rounded up. */
599 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
600 goto exit;
601 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
602 goto exit;
603 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
604 goto exit;
605 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
606 goto exit;
607 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
608 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100609 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100610 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200611 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200612#endif /* MBEDTLS_RSA_C */
613
614#if defined(MBEDTLS_ECP_C)
615 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
616 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100617 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100618 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100619 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200620 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200621#endif /* MBEDTLS_ECP_C */
622
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200623 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
624 {
625 uint8_t *p = exported;
626 uint8_t *end = exported + exported_length;
627 size_t len;
628 mbedtls_asn1_buf alg;
629 mbedtls_asn1_buf params;
630 mbedtls_asn1_bitstring bitstring;
631 /* SubjectPublicKeyInfo ::= SEQUENCE {
632 * algorithm AlgorithmIdentifier,
633 * subjectPublicKey BIT STRING }
634 * AlgorithmIdentifier ::= SEQUENCE {
635 * algorithm OBJECT IDENTIFIER,
636 * parameters ANY DEFINED BY algorithm OPTIONAL }
637 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100638 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
639 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100640 MBEDTLS_ASN1_CONSTRUCTED ),
641 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100642 TEST_EQUAL( p + len, end );
643 TEST_EQUAL( mbedtls_asn1_get_alg( &p, end, &alg, &params ), 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200644 if( ! is_oid_of_key_type( type, alg.p, alg.len ) )
645 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100646 TEST_EQUAL( mbedtls_asn1_get_bitstring( &p, end, &bitstring ), 0 );
647 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200648 p = bitstring.p;
649#if defined(MBEDTLS_RSA_C)
650 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
651 {
652 /* RSAPublicKey ::= SEQUENCE {
653 * modulus INTEGER, -- n
654 * publicExponent INTEGER } -- e
655 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100656 TEST_EQUAL( bitstring.unused_bits, 0 );
657 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
658 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100659 MBEDTLS_ASN1_CONSTRUCTED ),
660 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100661 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200662 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
663 goto exit;
664 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
665 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100666 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200667 }
668 else
669#endif /* MBEDTLS_RSA_C */
670#if defined(MBEDTLS_ECP_C)
671 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
672 {
673 /* ECPoint ::= ...
Gilles Peskinec6290c02018-08-13 17:24:59 +0200674 * -- first 8 bits: 0x04 (uncompressed representation);
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200675 * -- then x_P as an n-bit string, big endian;
676 * -- then y_P as a n-bit string, big endian,
677 * -- where n is the order of the curve.
678 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100679 TEST_EQUAL( bitstring.unused_bits, 0 );
680 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
681 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200682 }
683 else
684#endif /* MBEDTLS_ECP_C */
685 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100686 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200687 mbedtls_snprintf( message, sizeof( message ),
688 "No sanity check for public key type=0x%08lx",
689 (unsigned long) type );
690 test_fail( message, __LINE__, __FILE__ );
691 return( 0 );
692 }
693 }
694 else
695
696 {
697 /* No sanity checks for other types */
698 }
699
700 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200701
702exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200703 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200704}
705
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100706static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200707 psa_key_usage_t usage )
708{
709 psa_key_type_t type;
710 size_t bits;
711 uint8_t *exported = NULL;
712 size_t exported_size = 0;
713 size_t exported_length = 0;
714 int ok = 0;
715
Gilles Peskine8817f612018-12-18 00:18:46 +0100716 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200717
718 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
719 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200720 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100721 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
722 PSA_ERROR_NOT_PERMITTED );
Gilles Peskined14664a2018-08-10 19:07:32 +0200723 return( 1 );
724 }
725
Gilles Peskined14664a2018-08-10 19:07:32 +0200726 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200727 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200728
Gilles Peskine8817f612018-12-18 00:18:46 +0100729 PSA_ASSERT( psa_export_key( handle,
730 exported, exported_size,
731 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200732 ok = exported_key_sanity_check( type, bits, exported, exported_length );
733
734exit:
735 mbedtls_free( exported );
736 return( ok );
737}
738
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100739static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200740{
741 psa_key_type_t type;
742 psa_key_type_t public_type;
743 size_t bits;
744 uint8_t *exported = NULL;
745 size_t exported_size = 0;
746 size_t exported_length = 0;
747 int ok = 0;
748
Gilles Peskine8817f612018-12-18 00:18:46 +0100749 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200750 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
751 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100752 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100753 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200754 return( 1 );
755 }
756
757 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
758 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200759 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200760
Gilles Peskine8817f612018-12-18 00:18:46 +0100761 PSA_ASSERT( psa_export_public_key( handle,
762 exported, exported_size,
763 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200764 ok = exported_key_sanity_check( public_type, bits,
765 exported, exported_length );
766
767exit:
768 mbedtls_free( exported );
769 return( ok );
770}
771
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100772static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +0200773 psa_key_usage_t usage,
774 psa_algorithm_t alg )
775{
776 int ok;
777 if( alg == 0 )
778 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
779 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100780 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200781 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100782 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200783 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100784 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200785 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100786 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200787 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100788 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200789 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100790 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200791 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100792 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200793 else
794 {
795 char message[40];
796 mbedtls_snprintf( message, sizeof( message ),
797 "No code to exercise alg=0x%08lx",
798 (unsigned long) alg );
799 test_fail( message, __LINE__, __FILE__ );
800 ok = 0;
801 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200802
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100803 ok = ok && exercise_export_key( handle, usage );
804 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +0200805
Gilles Peskine02b75072018-07-01 22:31:34 +0200806 return( ok );
807}
808
Gilles Peskine10df3412018-10-25 22:35:43 +0200809static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
810 psa_algorithm_t alg )
811{
812 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
813 {
814 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
815 PSA_KEY_USAGE_VERIFY :
816 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
817 }
818 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
819 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
820 {
821 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
822 PSA_KEY_USAGE_ENCRYPT :
823 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
824 }
825 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
826 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
827 {
828 return( PSA_KEY_USAGE_DERIVE );
829 }
830 else
831 {
832 return( 0 );
833 }
834
835}
Darryl Green0c6575a2018-11-07 16:05:30 +0000836
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100837/* An overapproximation of the amount of storage needed for a key of the
838 * given type and with the given content. The API doesn't make it easy
839 * to find a good value for the size. The current implementation doesn't
840 * care about the value anyway. */
841#define KEY_BITS_FROM_DATA( type, data ) \
842 ( data )->len
843
Darryl Green0c6575a2018-11-07 16:05:30 +0000844typedef enum {
845 IMPORT_KEY = 0,
846 GENERATE_KEY = 1,
847 DERIVE_KEY = 2
848} generate_method;
849
Gilles Peskinee59236f2018-01-27 23:32:46 +0100850/* END_HEADER */
851
852/* BEGIN_DEPENDENCIES
853 * depends_on:MBEDTLS_PSA_CRYPTO_C
854 * END_DEPENDENCIES
855 */
856
857/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200858void static_checks( )
859{
860 size_t max_truncated_mac_size =
861 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
862
863 /* Check that the length for a truncated MAC always fits in the algorithm
864 * encoding. The shifted mask is the maximum truncated value. The
865 * untruncated algorithm may be one byte larger. */
866 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
867}
868/* END_CASE */
869
870/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200871void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100873 psa_key_handle_t handle = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200874 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100875 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100876
Gilles Peskine8817f612018-12-18 00:18:46 +0100877 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100878
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100879 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100880 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100881 TEST_EQUAL( status, expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100882 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100883 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100884
885exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100886 mbedtls_psa_crypto_free( );
887}
888/* END_CASE */
889
890/* BEGIN_CASE */
Gilles Peskinea4261682018-12-03 11:34:01 +0100891void import_twice( int alg_arg, int usage_arg,
892 int type1_arg, data_t *data1,
893 int expected_import1_status_arg,
894 int type2_arg, data_t *data2,
895 int expected_import2_status_arg )
896{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100897 psa_key_handle_t handle = 0;
Gilles Peskinea4261682018-12-03 11:34:01 +0100898 psa_algorithm_t alg = alg_arg;
899 psa_key_usage_t usage = usage_arg;
900 psa_key_type_t type1 = type1_arg;
901 psa_status_t expected_import1_status = expected_import1_status_arg;
902 psa_key_type_t type2 = type2_arg;
903 psa_status_t expected_import2_status = expected_import2_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +0000904 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea4261682018-12-03 11:34:01 +0100905 psa_status_t status;
906
Gilles Peskine8817f612018-12-18 00:18:46 +0100907 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100908
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100909 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100910 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100911 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100912
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100913 status = psa_import_key( handle, type1, data1->x, data1->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100914 TEST_EQUAL( status, expected_import1_status );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100915 status = psa_import_key( handle, type2, data2->x, data2->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100916 TEST_EQUAL( status, expected_import2_status );
Gilles Peskinea4261682018-12-03 11:34:01 +0100917
918 if( expected_import1_status == PSA_SUCCESS ||
919 expected_import2_status == PSA_SUCCESS )
920 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100921 TEST_ASSERT( exercise_key( handle, usage, alg ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100922 }
923
924exit:
925 mbedtls_psa_crypto_free( );
926}
927/* END_CASE */
928
929/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200930void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
931{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100932 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200933 size_t bits = bits_arg;
934 psa_status_t expected_status = expected_status_arg;
935 psa_status_t status;
936 psa_key_type_t type =
937 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
938 size_t buffer_size = /* Slight overapproximations */
939 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200940 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200941 unsigned char *p;
942 int ret;
943 size_t length;
944
Gilles Peskine8817f612018-12-18 00:18:46 +0100945 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200946 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200947
948 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
949 bits, keypair ) ) >= 0 );
950 length = ret;
951
952 /* Try importing the key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100953 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100954 status = psa_import_key( handle, type, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100955 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200956 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100957 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200958
959exit:
960 mbedtls_free( buffer );
961 mbedtls_psa_crypto_free( );
962}
963/* END_CASE */
964
965/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300966void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300967 int type_arg,
968 int alg_arg,
969 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100970 int expected_bits,
971 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200972 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100973 int canonical_input )
974{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100975 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100976 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200977 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200978 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100979 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100980 unsigned char *exported = NULL;
981 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100982 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100983 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100984 size_t reexported_length;
985 psa_key_type_t got_type;
986 size_t got_bits;
Jaeden Amero70261c52019-01-04 11:47:20 +0000987 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100988
Moran Pekercb088e72018-07-17 17:36:59 +0300989 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200990 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100991 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200992 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100993 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100994
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100995 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200996 psa_key_policy_set_usage( &policy, usage_arg, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100997 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100998
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100999 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1000 PSA_ERROR_EMPTY_SLOT );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001001
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001002 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001003 PSA_ASSERT( psa_import_key( handle, type,
1004 data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001005
1006 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001007 PSA_ASSERT( psa_get_key_information( handle,
1008 &got_type,
1009 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001010 TEST_EQUAL( got_type, type );
1011 TEST_EQUAL( got_bits, (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001012
1013 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001014 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001015 exported, export_size,
1016 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001017 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001018
1019 /* The exported length must be set by psa_export_key() to a value between 0
1020 * and export_size. On errors, the exported length must be 0. */
1021 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1022 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1023 TEST_ASSERT( exported_length <= export_size );
1024
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001025 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001026 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001027 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001028 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001029 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001030 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001031 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001032
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001033 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001034 goto exit;
1035
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001036 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001037 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001038 else
1039 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001040 psa_key_handle_t handle2;
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001041 PSA_ASSERT( psa_allocate_key( &handle2 ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001042 PSA_ASSERT( psa_set_key_policy( handle2, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001043
Gilles Peskine8817f612018-12-18 00:18:46 +01001044 PSA_ASSERT( psa_import_key( handle2, type,
1045 exported,
1046 exported_length ) );
1047 PSA_ASSERT( psa_export_key( handle2,
1048 reexported,
1049 export_size,
1050 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001051 ASSERT_COMPARE( exported, exported_length,
1052 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001053 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001054 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001055 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001056
1057destroy:
1058 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001059 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001060 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1061 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001062
1063exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001064 mbedtls_free( exported );
1065 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001066 mbedtls_psa_crypto_free( );
1067}
1068/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001069
Moran Pekerf709f4a2018-06-06 17:26:04 +03001070/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001071void import_key_nonempty_slot( )
1072{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001073 psa_key_handle_t handle = 0;
Moran Peker28a38e62018-11-07 16:18:24 +02001074 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1075 psa_status_t status;
1076 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
Gilles Peskine8817f612018-12-18 00:18:46 +01001077 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001078
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001079 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001080
Moran Peker28a38e62018-11-07 16:18:24 +02001081 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001082 PSA_ASSERT( psa_import_key( handle, type,
1083 data, sizeof( data ) ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001084
1085 /* Import the key again */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001086 status = psa_import_key( handle, type, data, sizeof( data ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001087 TEST_EQUAL( status, PSA_ERROR_OCCUPIED_SLOT );
Moran Peker28a38e62018-11-07 16:18:24 +02001088
1089exit:
1090 mbedtls_psa_crypto_free( );
1091}
1092/* END_CASE */
1093
1094/* BEGIN_CASE */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001095void export_invalid_handle( int handle, int expected_export_status_arg )
Moran Peker28a38e62018-11-07 16:18:24 +02001096{
1097 psa_status_t status;
1098 unsigned char *exported = NULL;
1099 size_t export_size = 0;
1100 size_t exported_length = INVALID_EXPORT_LENGTH;
1101 psa_status_t expected_export_status = expected_export_status_arg;
1102
Gilles Peskine8817f612018-12-18 00:18:46 +01001103 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001104
1105 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001106 status = psa_export_key( (psa_key_handle_t) handle,
Moran Peker28a38e62018-11-07 16:18:24 +02001107 exported, export_size,
1108 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001109 TEST_EQUAL( status, expected_export_status );
Moran Peker28a38e62018-11-07 16:18:24 +02001110
1111exit:
1112 mbedtls_psa_crypto_free( );
1113}
1114/* END_CASE */
1115
1116/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001117void export_with_no_key_activity( )
1118{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001119 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001120 psa_algorithm_t alg = PSA_ALG_CTR;
1121 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001122 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001123 unsigned char *exported = NULL;
1124 size_t export_size = 0;
1125 size_t exported_length = INVALID_EXPORT_LENGTH;
1126
Gilles Peskine8817f612018-12-18 00:18:46 +01001127 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001128
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001129 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001130 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001131 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001132
1133 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001134 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001135 exported, export_size,
1136 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001137 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001138
1139exit:
1140 mbedtls_psa_crypto_free( );
1141}
1142/* END_CASE */
1143
1144/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001145void cipher_with_no_key_activity( )
1146{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001147 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001148 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001149 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001150 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001151 int exercise_alg = PSA_ALG_CTR;
1152
Gilles Peskine8817f612018-12-18 00:18:46 +01001153 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001154
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001155 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekerce500072018-11-07 16:20:07 +02001156 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001157 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerce500072018-11-07 16:20:07 +02001158
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001159 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001160 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001161
1162exit:
1163 psa_cipher_abort( &operation );
1164 mbedtls_psa_crypto_free( );
1165}
1166/* END_CASE */
1167
1168/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001169void export_after_import_failure( data_t *data, int type_arg,
1170 int expected_import_status_arg )
1171{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001172 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001173 psa_key_type_t type = type_arg;
1174 psa_status_t status;
1175 unsigned char *exported = NULL;
1176 size_t export_size = 0;
1177 psa_status_t expected_import_status = expected_import_status_arg;
1178 size_t exported_length = INVALID_EXPORT_LENGTH;
1179
Gilles Peskine8817f612018-12-18 00:18:46 +01001180 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001181
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001182 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001183
Moran Peker34550092018-11-07 16:19:34 +02001184 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001185 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001186 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001187 TEST_EQUAL( status, expected_import_status );
Moran Peker34550092018-11-07 16:19:34 +02001188
1189 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001190 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001191 exported, export_size,
1192 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001193 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001194
1195exit:
1196 mbedtls_psa_crypto_free( );
1197}
1198/* END_CASE */
1199
1200/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001201void cipher_after_import_failure( data_t *data, int type_arg,
1202 int expected_import_status_arg )
1203{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001204 psa_key_handle_t handle = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001205 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001206 psa_key_type_t type = type_arg;
1207 psa_status_t status;
1208 psa_status_t expected_import_status = expected_import_status_arg;
1209 int exercise_alg = PSA_ALG_CTR;
1210
Gilles Peskine8817f612018-12-18 00:18:46 +01001211 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001212
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001213 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001214
Moran Pekerce500072018-11-07 16:20:07 +02001215 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001216 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001217 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001218 TEST_EQUAL( status, expected_import_status );
Moran Pekerce500072018-11-07 16:20:07 +02001219
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001220 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001221 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001222
1223exit:
1224 psa_cipher_abort( &operation );
1225 mbedtls_psa_crypto_free( );
1226}
1227/* END_CASE */
1228
1229/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001230void export_after_destroy_key( data_t *data, int type_arg )
1231{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001232 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001233 psa_key_type_t type = type_arg;
1234 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001235 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001236 psa_algorithm_t alg = PSA_ALG_CTR;
1237 unsigned char *exported = NULL;
1238 size_t export_size = 0;
1239 size_t exported_length = INVALID_EXPORT_LENGTH;
1240
Gilles Peskine8817f612018-12-18 00:18:46 +01001241 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001242
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001243 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001244 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001245 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001246 export_size = (ptrdiff_t) data->len;
1247 ASSERT_ALLOC( exported, export_size );
1248
1249 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001250 PSA_ASSERT( psa_import_key( handle, type,
1251 data->x, data->len ) );
Moran Peker34550092018-11-07 16:19:34 +02001252
Gilles Peskine8817f612018-12-18 00:18:46 +01001253 PSA_ASSERT( psa_export_key( handle, exported, export_size,
1254 &exported_length ) );
Moran Peker34550092018-11-07 16:19:34 +02001255
1256 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001257 PSA_ASSERT( psa_destroy_key( handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001258
1259 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001260 status = psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001261 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001262 TEST_EQUAL( status, PSA_ERROR_INVALID_HANDLE );
Moran Peker34550092018-11-07 16:19:34 +02001263
1264exit:
1265 mbedtls_free( exported );
1266 mbedtls_psa_crypto_free( );
1267}
1268/* END_CASE */
1269
1270/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001271void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001272 int type_arg,
1273 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001274 int export_size_delta,
1275 int expected_export_status_arg,
1276 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001277{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001278 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001279 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001280 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001281 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001282 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001283 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001284 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001285 size_t exported_length = INVALID_EXPORT_LENGTH;
Jaeden Amero70261c52019-01-04 11:47:20 +00001286 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001287
Gilles Peskine8817f612018-12-18 00:18:46 +01001288 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001289
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001290 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001291 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001292 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001293
1294 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001295 PSA_ASSERT( psa_import_key( handle, type,
1296 data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001297
Gilles Peskine49c25912018-10-29 15:15:31 +01001298 /* Export the public key */
1299 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001300 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001301 exported, export_size,
1302 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001303 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001304 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001305 {
1306 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1307 size_t bits;
Gilles Peskine8817f612018-12-18 00:18:46 +01001308 PSA_ASSERT( psa_get_key_information( handle, NULL, &bits ) );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001309 TEST_ASSERT( expected_public_key->len <=
1310 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001311 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1312 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001313 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001314
1315exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001316 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001317 psa_destroy_key( handle );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001318 mbedtls_psa_crypto_free( );
1319}
1320/* END_CASE */
1321
Gilles Peskine20035e32018-02-03 22:44:14 +01001322/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001323void import_and_exercise_key( data_t *data,
1324 int type_arg,
1325 int bits_arg,
1326 int alg_arg )
1327{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001328 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001329 psa_key_type_t type = type_arg;
1330 size_t bits = bits_arg;
1331 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001332 psa_key_usage_t usage = usage_to_exercise( type, alg );
Jaeden Amero70261c52019-01-04 11:47:20 +00001333 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001334 psa_key_type_t got_type;
1335 size_t got_bits;
1336 psa_status_t status;
1337
Gilles Peskine8817f612018-12-18 00:18:46 +01001338 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001339
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001340 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001341 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001342 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001343
1344 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001345 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01001346 PSA_ASSERT( status );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001347
1348 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001349 PSA_ASSERT( psa_get_key_information( handle,
1350 &got_type,
1351 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001352 TEST_EQUAL( got_type, type );
1353 TEST_EQUAL( got_bits, bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001354
1355 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001356 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001357 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001358
1359exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001360 psa_destroy_key( handle );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001361 mbedtls_psa_crypto_free( );
1362}
1363/* END_CASE */
1364
1365/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001366void key_policy( int usage_arg, int alg_arg )
1367{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001368 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001369 psa_algorithm_t alg = alg_arg;
1370 psa_key_usage_t usage = usage_arg;
1371 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1372 unsigned char key[32] = {0};
Jaeden Amero70261c52019-01-04 11:47:20 +00001373 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
1374 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001375
1376 memset( key, 0x2a, sizeof( key ) );
1377
Gilles Peskine8817f612018-12-18 00:18:46 +01001378 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001379
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001380 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001381 psa_key_policy_set_usage( &policy_set, usage, alg );
1382
Gilles Peskinefe11b722018-12-18 00:24:04 +01001383 TEST_EQUAL( psa_key_policy_get_usage( &policy_set ), usage );
1384 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_set ), alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001385 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001386
Gilles Peskine8817f612018-12-18 00:18:46 +01001387 PSA_ASSERT( psa_import_key( handle, key_type,
1388 key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001389
Gilles Peskine8817f612018-12-18 00:18:46 +01001390 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001391
Gilles Peskinefe11b722018-12-18 00:24:04 +01001392 TEST_EQUAL( policy_get.usage, policy_set.usage );
1393 TEST_EQUAL( policy_get.alg, policy_set.alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001394
1395exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001396 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001397 mbedtls_psa_crypto_free( );
1398}
1399/* END_CASE */
1400
1401/* BEGIN_CASE */
Jaeden Amero70261c52019-01-04 11:47:20 +00001402void key_policy_init( )
1403{
1404 /* Test each valid way of initializing the object, except for `= {0}`, as
1405 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1406 * though it's OK by the C standard. We could test for this, but we'd need
1407 * to supress the Clang warning for the test. */
1408 psa_key_policy_t func = psa_key_policy_init( );
1409 psa_key_policy_t init = PSA_KEY_POLICY_INIT;
1410 psa_key_policy_t zero;
1411
1412 memset( &zero, 0, sizeof( zero ) );
1413
1414 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1415 * specification, we test that all valid ways of initializing the object
1416 * have the same bit pattern. This is a stronger requirement that may not
1417 * be valid on all platforms or PSA Crypto implementations, but implies the
1418 * weaker actual requirement is met: that a freshly initialized object, no
1419 * matter how it was initialized, acts the same as any other valid
1420 * initialization. */
1421 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1422 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1423}
1424/* END_CASE */
1425
1426/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001427void mac_key_policy( int policy_usage,
1428 int policy_alg,
1429 int key_type,
1430 data_t *key_data,
1431 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001432{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001433 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001434 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001435 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001436 psa_status_t status;
1437 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001438
Gilles Peskine8817f612018-12-18 00:18:46 +01001439 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001440
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001441 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001442 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001443 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001444
Gilles Peskine8817f612018-12-18 00:18:46 +01001445 PSA_ASSERT( psa_import_key( handle, key_type,
1446 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001447
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001448 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001449 if( policy_alg == exercise_alg &&
1450 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001451 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001452 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001453 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001454 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001455
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001456 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001457 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001458 if( policy_alg == exercise_alg &&
1459 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001460 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001461 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001462 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001463
1464exit:
1465 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001466 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001467 mbedtls_psa_crypto_free( );
1468}
1469/* END_CASE */
1470
1471/* BEGIN_CASE */
1472void cipher_key_policy( int policy_usage,
1473 int policy_alg,
1474 int key_type,
1475 data_t *key_data,
1476 int exercise_alg )
1477{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001478 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001479 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001480 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001481 psa_status_t status;
1482
Gilles Peskine8817f612018-12-18 00:18:46 +01001483 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001484
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001485 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001486 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001487 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001488
Gilles Peskine8817f612018-12-18 00:18:46 +01001489 PSA_ASSERT( psa_import_key( handle, key_type,
1490 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001491
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001492 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001493 if( policy_alg == exercise_alg &&
1494 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001495 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001496 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001497 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001498 psa_cipher_abort( &operation );
1499
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001500 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001501 if( policy_alg == exercise_alg &&
1502 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001503 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001504 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001505 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001506
1507exit:
1508 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001509 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001510 mbedtls_psa_crypto_free( );
1511}
1512/* END_CASE */
1513
1514/* BEGIN_CASE */
1515void aead_key_policy( int policy_usage,
1516 int policy_alg,
1517 int key_type,
1518 data_t *key_data,
1519 int nonce_length_arg,
1520 int tag_length_arg,
1521 int exercise_alg )
1522{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001523 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001524 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001525 psa_status_t status;
1526 unsigned char nonce[16] = {0};
1527 size_t nonce_length = nonce_length_arg;
1528 unsigned char tag[16];
1529 size_t tag_length = tag_length_arg;
1530 size_t output_length;
1531
1532 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1533 TEST_ASSERT( tag_length <= sizeof( tag ) );
1534
Gilles Peskine8817f612018-12-18 00:18:46 +01001535 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001536
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001537 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001538 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001539 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001540
Gilles Peskine8817f612018-12-18 00:18:46 +01001541 PSA_ASSERT( psa_import_key( handle, key_type,
1542 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001543
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001544 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001545 nonce, nonce_length,
1546 NULL, 0,
1547 NULL, 0,
1548 tag, tag_length,
1549 &output_length );
1550 if( policy_alg == exercise_alg &&
1551 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001552 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001553 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001554 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001555
1556 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001557 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001558 nonce, nonce_length,
1559 NULL, 0,
1560 tag, tag_length,
1561 NULL, 0,
1562 &output_length );
1563 if( policy_alg == exercise_alg &&
1564 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001565 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001566 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001567 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001568
1569exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001570 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001571 mbedtls_psa_crypto_free( );
1572}
1573/* END_CASE */
1574
1575/* BEGIN_CASE */
1576void asymmetric_encryption_key_policy( int policy_usage,
1577 int policy_alg,
1578 int key_type,
1579 data_t *key_data,
1580 int exercise_alg )
1581{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001582 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001583 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001584 psa_status_t status;
1585 size_t key_bits;
1586 size_t buffer_length;
1587 unsigned char *buffer = NULL;
1588 size_t output_length;
1589
Gilles Peskine8817f612018-12-18 00:18:46 +01001590 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001591
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001592 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001593 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001594 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001595
Gilles Peskine8817f612018-12-18 00:18:46 +01001596 PSA_ASSERT( psa_import_key( handle, key_type,
1597 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001598
Gilles Peskine8817f612018-12-18 00:18:46 +01001599 PSA_ASSERT( psa_get_key_information( handle,
1600 NULL,
1601 &key_bits ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001602 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1603 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001604 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001605
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001606 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001607 NULL, 0,
1608 NULL, 0,
1609 buffer, buffer_length,
1610 &output_length );
1611 if( policy_alg == exercise_alg &&
1612 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001613 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001614 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001615 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001616
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001617 if( buffer_length != 0 )
1618 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001619 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001620 buffer, buffer_length,
1621 NULL, 0,
1622 buffer, buffer_length,
1623 &output_length );
1624 if( policy_alg == exercise_alg &&
1625 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001626 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001627 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001628 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001629
1630exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001631 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001632 mbedtls_psa_crypto_free( );
1633 mbedtls_free( buffer );
1634}
1635/* END_CASE */
1636
1637/* BEGIN_CASE */
1638void asymmetric_signature_key_policy( int policy_usage,
1639 int policy_alg,
1640 int key_type,
1641 data_t *key_data,
1642 int exercise_alg )
1643{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001644 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001645 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001646 psa_status_t status;
1647 unsigned char payload[16] = {1};
1648 size_t payload_length = sizeof( payload );
1649 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1650 size_t signature_length;
1651
Gilles Peskine8817f612018-12-18 00:18:46 +01001652 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001653
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001654 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001655 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001656 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001657
Gilles Peskine8817f612018-12-18 00:18:46 +01001658 PSA_ASSERT( psa_import_key( handle, key_type,
1659 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001660
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001661 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001662 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001663 signature, sizeof( signature ),
1664 &signature_length );
1665 if( policy_alg == exercise_alg &&
1666 ( 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 ) );
1675 if( policy_alg == exercise_alg &&
1676 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001677 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001678 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001679 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001680
1681exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001682 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001683 mbedtls_psa_crypto_free( );
1684}
1685/* END_CASE */
1686
1687/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001688void derive_key_policy( int policy_usage,
1689 int policy_alg,
1690 int key_type,
1691 data_t *key_data,
1692 int exercise_alg )
1693{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001694 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001695 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001696 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1697 psa_status_t status;
1698
Gilles Peskine8817f612018-12-18 00:18:46 +01001699 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001700
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001701 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001702 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001703 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001704
Gilles Peskine8817f612018-12-18 00:18:46 +01001705 PSA_ASSERT( psa_import_key( handle, key_type,
1706 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001707
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001708 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001709 exercise_alg,
1710 NULL, 0,
1711 NULL, 0,
1712 1 );
1713 if( policy_alg == exercise_alg &&
1714 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001715 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001716 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001717 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001718
1719exit:
1720 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001721 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001722 mbedtls_psa_crypto_free( );
1723}
1724/* END_CASE */
1725
1726/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001727void agreement_key_policy( int policy_usage,
1728 int policy_alg,
1729 int key_type_arg,
1730 data_t *key_data,
1731 int exercise_alg )
1732{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001733 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001734 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001735 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001736 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1737 psa_status_t status;
1738
Gilles Peskine8817f612018-12-18 00:18:46 +01001739 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001740
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001741 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001742 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001743 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001744
Gilles Peskine8817f612018-12-18 00:18:46 +01001745 PSA_ASSERT( psa_import_key( handle, key_type,
1746 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001747
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001748 status = key_agreement_with_self( &generator, handle, exercise_alg );
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
1900/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001901void mac_operation_init( )
1902{
1903 /* Test each valid way of initializing the object, except for `= {0}`, as
1904 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1905 * though it's OK by the C standard. We could test for this, but we'd need
1906 * to supress the Clang warning for the test. */
1907 psa_mac_operation_t func = psa_mac_operation_init( );
1908 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1909 psa_mac_operation_t zero;
1910
1911 memset( &zero, 0, sizeof( zero ) );
1912
1913 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1914 * specification, we test that all valid ways of initializing the object
1915 * have the same bit pattern. This is a stronger requirement that may not
1916 * be valid on all platforms or PSA Crypto implementations, but implies the
1917 * weaker actual requirement is met: that a freshly initialized object, no
1918 * matter how it was initialized, acts the same as any other valid
1919 * initialization. */
1920 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1921 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1922}
1923/* END_CASE */
1924
1925/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001926void mac_setup( int key_type_arg,
1927 data_t *key,
1928 int alg_arg,
1929 int expected_status_arg )
1930{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001931 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001932 psa_key_type_t key_type = key_type_arg;
1933 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001934 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001935 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00001936 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001937 psa_status_t status;
1938
Gilles Peskine8817f612018-12-18 00:18:46 +01001939 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001940
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001941 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001942 psa_key_policy_set_usage( &policy,
1943 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1944 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001945 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001946
Gilles Peskine8817f612018-12-18 00:18:46 +01001947 PSA_ASSERT( psa_import_key( handle, key_type,
1948 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001949
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001950 status = psa_mac_sign_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001951 psa_mac_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001952 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001953
1954exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001955 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001956 mbedtls_psa_crypto_free( );
1957}
1958/* END_CASE */
1959
1960/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001961void mac_sign( int key_type_arg,
1962 data_t *key,
1963 int alg_arg,
1964 data_t *input,
1965 data_t *expected_mac )
1966{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001967 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001968 psa_key_type_t key_type = key_type_arg;
1969 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001970 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00001971 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001972 /* Leave a little extra room in the output buffer. At the end of the
1973 * test, we'll check that the implementation didn't overwrite onto
1974 * this extra room. */
1975 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
1976 size_t mac_buffer_size =
1977 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
1978 size_t mac_length = 0;
1979
1980 memset( actual_mac, '+', sizeof( actual_mac ) );
1981 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
1982 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
1983
Gilles Peskine8817f612018-12-18 00:18:46 +01001984 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001985
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001986 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001987 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001988 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001989
Gilles Peskine8817f612018-12-18 00:18:46 +01001990 PSA_ASSERT( psa_import_key( handle, key_type,
1991 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001992
1993 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01001994 PSA_ASSERT( psa_mac_sign_setup( &operation,
1995 handle, alg ) );
1996 PSA_ASSERT( psa_mac_update( &operation,
1997 input->x, input->len ) );
1998 PSA_ASSERT( psa_mac_sign_finish( &operation,
1999 actual_mac, mac_buffer_size,
2000 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002001
2002 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002003 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2004 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002005
2006 /* Verify that the end of the buffer is untouched. */
2007 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2008 sizeof( actual_mac ) - mac_length ) );
2009
2010exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002011 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002012 mbedtls_psa_crypto_free( );
2013}
2014/* END_CASE */
2015
2016/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002017void mac_verify( int key_type_arg,
2018 data_t *key,
2019 int alg_arg,
2020 data_t *input,
2021 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002022{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002023 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002024 psa_key_type_t key_type = key_type_arg;
2025 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002026 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002027 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002028
Gilles Peskine69c12672018-06-28 00:07:19 +02002029 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2030
Gilles Peskine8817f612018-12-18 00:18:46 +01002031 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002032
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002033 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002034 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002035 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad16036df908f2018-04-02 08:34:15 -07002036
Gilles Peskine8817f612018-12-18 00:18:46 +01002037 PSA_ASSERT( psa_import_key( handle, key_type,
2038 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002039
Gilles Peskine8817f612018-12-18 00:18:46 +01002040 PSA_ASSERT( psa_mac_verify_setup( &operation,
2041 handle, alg ) );
2042 PSA_ASSERT( psa_destroy_key( handle ) );
2043 PSA_ASSERT( psa_mac_update( &operation,
2044 input->x, input->len ) );
2045 PSA_ASSERT( psa_mac_verify_finish( &operation,
2046 expected_mac->x,
2047 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002048
2049exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002050 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002051 mbedtls_psa_crypto_free( );
2052}
2053/* END_CASE */
2054
2055/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002056void cipher_operation_init( )
2057{
2058 /* Test each valid way of initializing the object, except for `= {0}`, as
2059 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2060 * though it's OK by the C standard. We could test for this, but we'd need
2061 * to supress the Clang warning for the test. */
2062 psa_cipher_operation_t func = psa_cipher_operation_init( );
2063 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2064 psa_cipher_operation_t zero;
2065
2066 memset( &zero, 0, sizeof( zero ) );
2067
2068 /* Although not technically guaranteed by the C standard nor the PSA Crypto
2069 * specification, we test that all valid ways of initializing the object
2070 * have the same bit pattern. This is a stronger requirement that may not
2071 * be valid on all platforms or PSA Crypto implementations, but implies the
2072 * weaker actual requirement is met: that a freshly initialized object, no
2073 * matter how it was initialized, acts the same as any other valid
2074 * initialization. */
2075 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
2076 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
2077}
2078/* END_CASE */
2079
2080/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002081void cipher_setup( int key_type_arg,
2082 data_t *key,
2083 int alg_arg,
2084 int expected_status_arg )
2085{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002086 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002087 psa_key_type_t key_type = key_type_arg;
2088 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002089 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002090 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002091 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002092 psa_status_t status;
2093
Gilles Peskine8817f612018-12-18 00:18:46 +01002094 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002095
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002096 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002097 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002098 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002099
Gilles Peskine8817f612018-12-18 00:18:46 +01002100 PSA_ASSERT( psa_import_key( handle, key_type,
2101 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002102
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002103 status = psa_cipher_encrypt_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002104 psa_cipher_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002105 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002106
2107exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002108 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002109 mbedtls_psa_crypto_free( );
2110}
2111/* END_CASE */
2112
2113/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002114void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002115 data_t *key,
2116 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002117 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002118{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002119 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002120 psa_status_t status;
2121 psa_key_type_t key_type = key_type_arg;
2122 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002123 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002124 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002125 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002126 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002127 size_t output_buffer_size = 0;
2128 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002129 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002130 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002131 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002132
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002133 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2134 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002135
Gilles Peskine8817f612018-12-18 00:18:46 +01002136 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002137
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002138 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002139 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002140 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002141
Gilles Peskine8817f612018-12-18 00:18:46 +01002142 PSA_ASSERT( psa_import_key( handle, key_type,
2143 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002144
Gilles Peskine8817f612018-12-18 00:18:46 +01002145 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2146 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002147
Gilles Peskine8817f612018-12-18 00:18:46 +01002148 PSA_ASSERT( psa_cipher_set_iv( &operation,
2149 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002150 output_buffer_size = ( (size_t) input->len +
2151 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002152 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002153
Gilles Peskine8817f612018-12-18 00:18:46 +01002154 PSA_ASSERT( psa_cipher_update( &operation,
2155 input->x, input->len,
2156 output, output_buffer_size,
2157 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002158 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002159 status = psa_cipher_finish( &operation,
2160 output + function_output_length,
2161 output_buffer_size,
2162 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002163 total_output_length += function_output_length;
2164
Gilles Peskinefe11b722018-12-18 00:24:04 +01002165 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002166 if( expected_status == PSA_SUCCESS )
2167 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002168 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002169 ASSERT_COMPARE( expected_output->x, expected_output->len,
2170 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002171 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002172
Gilles Peskine50e586b2018-06-08 14:28:46 +02002173exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002174 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002175 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002176 mbedtls_psa_crypto_free( );
2177}
2178/* END_CASE */
2179
2180/* BEGIN_CASE */
2181void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002182 data_t *key,
2183 data_t *input,
2184 int first_part_size,
2185 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002186{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002187 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002188 psa_key_type_t key_type = key_type_arg;
2189 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002190 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002191 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002192 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002193 size_t output_buffer_size = 0;
2194 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002195 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002196 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002197 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002198
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002199 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2200 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002201
Gilles Peskine8817f612018-12-18 00:18:46 +01002202 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002203
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002204 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002205 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002206 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002207
Gilles Peskine8817f612018-12-18 00:18:46 +01002208 PSA_ASSERT( psa_import_key( handle, key_type,
2209 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002210
Gilles Peskine8817f612018-12-18 00:18:46 +01002211 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2212 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002213
Gilles Peskine8817f612018-12-18 00:18:46 +01002214 PSA_ASSERT( psa_cipher_set_iv( &operation,
2215 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002216 output_buffer_size = ( (size_t) input->len +
2217 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002218 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002219
Gilles Peskine4abf7412018-06-18 16:35:34 +02002220 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002221 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2222 output, output_buffer_size,
2223 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002224 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002225 PSA_ASSERT( psa_cipher_update( &operation,
2226 input->x + first_part_size,
2227 input->len - first_part_size,
2228 output, output_buffer_size,
2229 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002230 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002231 PSA_ASSERT( psa_cipher_finish( &operation,
2232 output + function_output_length,
2233 output_buffer_size,
2234 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002235 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002236 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002237
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002238 ASSERT_COMPARE( expected_output->x, expected_output->len,
2239 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002240
2241exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002242 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002243 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002244 mbedtls_psa_crypto_free( );
2245}
2246/* END_CASE */
2247
2248/* BEGIN_CASE */
2249void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002250 data_t *key,
2251 data_t *input,
2252 int first_part_size,
2253 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002254{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002255 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002256
2257 psa_key_type_t key_type = key_type_arg;
2258 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002259 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002260 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002261 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002262 size_t output_buffer_size = 0;
2263 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002264 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002265 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002266 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002267
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002268 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2269 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002270
Gilles Peskine8817f612018-12-18 00:18:46 +01002271 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002272
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002273 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002274 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002275 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002276
Gilles Peskine8817f612018-12-18 00:18:46 +01002277 PSA_ASSERT( psa_import_key( handle, key_type,
2278 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002279
Gilles Peskine8817f612018-12-18 00:18:46 +01002280 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2281 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002282
Gilles Peskine8817f612018-12-18 00:18:46 +01002283 PSA_ASSERT( psa_cipher_set_iv( &operation,
2284 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002285
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002286 output_buffer_size = ( (size_t) input->len +
2287 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002288 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002289
Gilles Peskine4abf7412018-06-18 16:35:34 +02002290 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002291 PSA_ASSERT( psa_cipher_update( &operation,
2292 input->x, first_part_size,
2293 output, output_buffer_size,
2294 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002295 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002296 PSA_ASSERT( psa_cipher_update( &operation,
2297 input->x + first_part_size,
2298 input->len - first_part_size,
2299 output, output_buffer_size,
2300 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002301 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002302 PSA_ASSERT( psa_cipher_finish( &operation,
2303 output + function_output_length,
2304 output_buffer_size,
2305 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002306 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002307 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002308
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002309 ASSERT_COMPARE( expected_output->x, expected_output->len,
2310 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002311
2312exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002313 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002314 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002315 mbedtls_psa_crypto_free( );
2316}
2317/* END_CASE */
2318
Gilles Peskine50e586b2018-06-08 14:28:46 +02002319/* BEGIN_CASE */
2320void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002321 data_t *key,
2322 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002323 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002324{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002325 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002326 psa_status_t status;
2327 psa_key_type_t key_type = key_type_arg;
2328 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002329 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002330 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002331 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002332 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002333 size_t output_buffer_size = 0;
2334 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002335 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002336 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002337 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002338
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002339 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2340 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002341
Gilles Peskine8817f612018-12-18 00:18:46 +01002342 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002343
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002344 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002345 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002346 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002347
Gilles Peskine8817f612018-12-18 00:18:46 +01002348 PSA_ASSERT( psa_import_key( handle, key_type,
2349 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002350
Gilles Peskine8817f612018-12-18 00:18:46 +01002351 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2352 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002353
Gilles Peskine8817f612018-12-18 00:18:46 +01002354 PSA_ASSERT( psa_cipher_set_iv( &operation,
2355 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002356
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002357 output_buffer_size = ( (size_t) input->len +
2358 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002359 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002360
Gilles Peskine8817f612018-12-18 00:18:46 +01002361 PSA_ASSERT( psa_cipher_update( &operation,
2362 input->x, input->len,
2363 output, output_buffer_size,
2364 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002365 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002366 status = psa_cipher_finish( &operation,
2367 output + function_output_length,
2368 output_buffer_size,
2369 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002370 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002371 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002372
2373 if( expected_status == PSA_SUCCESS )
2374 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002375 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002376 ASSERT_COMPARE( expected_output->x, expected_output->len,
2377 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002378 }
2379
Gilles Peskine50e586b2018-06-08 14:28:46 +02002380exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002381 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002382 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002383 mbedtls_psa_crypto_free( );
2384}
2385/* END_CASE */
2386
Gilles Peskine50e586b2018-06-08 14:28:46 +02002387/* BEGIN_CASE */
2388void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002389 data_t *key,
2390 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002391{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002392 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002393 psa_key_type_t key_type = key_type_arg;
2394 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002395 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002396 size_t iv_size = 16;
2397 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002398 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002399 size_t output1_size = 0;
2400 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002401 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002402 size_t output2_size = 0;
2403 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002404 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002405 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2406 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002407 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002408
Gilles Peskine8817f612018-12-18 00:18:46 +01002409 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002410
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002411 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002412 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002413 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002414
Gilles Peskine8817f612018-12-18 00:18:46 +01002415 PSA_ASSERT( psa_import_key( handle, key_type,
2416 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002417
Gilles Peskine8817f612018-12-18 00:18:46 +01002418 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2419 handle, alg ) );
2420 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2421 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002422
Gilles Peskine8817f612018-12-18 00:18:46 +01002423 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2424 iv, iv_size,
2425 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002426 output1_size = ( (size_t) input->len +
2427 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002428 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002429
Gilles Peskine8817f612018-12-18 00:18:46 +01002430 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2431 output1, output1_size,
2432 &output1_length ) );
2433 PSA_ASSERT( psa_cipher_finish( &operation1,
2434 output1 + output1_length, output1_size,
2435 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002436
Gilles Peskine048b7f02018-06-08 14:20:49 +02002437 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002438
Gilles Peskine8817f612018-12-18 00:18:46 +01002439 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002440
2441 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002442 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002443
Gilles Peskine8817f612018-12-18 00:18:46 +01002444 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2445 iv, iv_length ) );
2446 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2447 output2, output2_size,
2448 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002449 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002450 PSA_ASSERT( psa_cipher_finish( &operation2,
2451 output2 + output2_length,
2452 output2_size,
2453 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002454
Gilles Peskine048b7f02018-06-08 14:20:49 +02002455 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002456
Gilles Peskine8817f612018-12-18 00:18:46 +01002457 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002458
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002459 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002460
2461exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002462 mbedtls_free( output1 );
2463 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002464 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03002465 mbedtls_psa_crypto_free( );
2466}
2467/* END_CASE */
2468
2469/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002470void cipher_verify_output_multipart( int alg_arg,
2471 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002472 data_t *key,
2473 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002474 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002475{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002476 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002477 psa_key_type_t key_type = key_type_arg;
2478 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002479 unsigned char iv[16] = {0};
2480 size_t iv_size = 16;
2481 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002482 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002483 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002484 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002485 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002486 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002487 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002488 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002489 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2490 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002491 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002492
Gilles Peskine8817f612018-12-18 00:18:46 +01002493 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002494
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002495 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002496 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002497 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002498
Gilles Peskine8817f612018-12-18 00:18:46 +01002499 PSA_ASSERT( psa_import_key( handle, key_type,
2500 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03002501
Gilles Peskine8817f612018-12-18 00:18:46 +01002502 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2503 handle, alg ) );
2504 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2505 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002506
Gilles Peskine8817f612018-12-18 00:18:46 +01002507 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2508 iv, iv_size,
2509 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002510 output1_buffer_size = ( (size_t) input->len +
2511 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002512 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002513
Gilles Peskine4abf7412018-06-18 16:35:34 +02002514 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002515
Gilles Peskine8817f612018-12-18 00:18:46 +01002516 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2517 output1, output1_buffer_size,
2518 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002519 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002520
Gilles Peskine8817f612018-12-18 00:18:46 +01002521 PSA_ASSERT( psa_cipher_update( &operation1,
2522 input->x + first_part_size,
2523 input->len - first_part_size,
2524 output1, output1_buffer_size,
2525 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002526 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002527
Gilles Peskine8817f612018-12-18 00:18:46 +01002528 PSA_ASSERT( psa_cipher_finish( &operation1,
2529 output1 + output1_length,
2530 output1_buffer_size - output1_length,
2531 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002532 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002533
Gilles Peskine8817f612018-12-18 00:18:46 +01002534 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002535
Gilles Peskine048b7f02018-06-08 14:20:49 +02002536 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002537 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002538
Gilles Peskine8817f612018-12-18 00:18:46 +01002539 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2540 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002541
Gilles Peskine8817f612018-12-18 00:18:46 +01002542 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2543 output2, output2_buffer_size,
2544 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002545 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002546
Gilles Peskine8817f612018-12-18 00:18:46 +01002547 PSA_ASSERT( psa_cipher_update( &operation2,
2548 output1 + first_part_size,
2549 output1_length - first_part_size,
2550 output2, output2_buffer_size,
2551 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002552 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002553
Gilles Peskine8817f612018-12-18 00:18:46 +01002554 PSA_ASSERT( psa_cipher_finish( &operation2,
2555 output2 + output2_length,
2556 output2_buffer_size - output2_length,
2557 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002558 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002559
Gilles Peskine8817f612018-12-18 00:18:46 +01002560 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002561
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002562 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002563
2564exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002565 mbedtls_free( output1 );
2566 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002567 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002568 mbedtls_psa_crypto_free( );
2569}
2570/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002571
Gilles Peskine20035e32018-02-03 22:44:14 +01002572/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002573void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002574 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002575 data_t *nonce,
2576 data_t *additional_data,
2577 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002578 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002579{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002580 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002581 psa_key_type_t key_type = key_type_arg;
2582 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002583 unsigned char *output_data = NULL;
2584 size_t output_size = 0;
2585 size_t output_length = 0;
2586 unsigned char *output_data2 = NULL;
2587 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002588 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002589 psa_status_t expected_result = expected_result_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002590 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002591
Gilles Peskine4abf7412018-06-18 16:35:34 +02002592 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002593 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002594
Gilles Peskine8817f612018-12-18 00:18:46 +01002595 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002596
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002597 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002598 psa_key_policy_set_usage( &policy,
2599 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2600 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002601 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002602
Gilles Peskine8817f612018-12-18 00:18:46 +01002603 PSA_ASSERT( psa_import_key( handle, key_type,
2604 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002605
Gilles Peskinefe11b722018-12-18 00:24:04 +01002606 TEST_EQUAL( psa_aead_encrypt( handle, alg,
2607 nonce->x, nonce->len,
2608 additional_data->x,
2609 additional_data->len,
2610 input_data->x, input_data->len,
2611 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002612 &output_length ),
2613 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002614
2615 if( PSA_SUCCESS == expected_result )
2616 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002617 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002618
Gilles Peskinefe11b722018-12-18 00:24:04 +01002619 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2620 nonce->x, nonce->len,
2621 additional_data->x,
2622 additional_data->len,
2623 output_data, output_length,
2624 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002625 &output_length2 ),
2626 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002627
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002628 ASSERT_COMPARE( input_data->x, input_data->len,
2629 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002630 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002631
Gilles Peskinea1cac842018-06-11 19:33:02 +02002632exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002633 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002634 mbedtls_free( output_data );
2635 mbedtls_free( output_data2 );
2636 mbedtls_psa_crypto_free( );
2637}
2638/* END_CASE */
2639
2640/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002641void aead_encrypt( int key_type_arg, data_t *key_data,
2642 int alg_arg,
2643 data_t *nonce,
2644 data_t *additional_data,
2645 data_t *input_data,
2646 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002647{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002648 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002649 psa_key_type_t key_type = key_type_arg;
2650 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002651 unsigned char *output_data = NULL;
2652 size_t output_size = 0;
2653 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002654 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002655 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002656
Gilles Peskine4abf7412018-06-18 16:35:34 +02002657 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002658 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002659
Gilles Peskine8817f612018-12-18 00:18:46 +01002660 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002661
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002662 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002663 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002664 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002665
Gilles Peskine8817f612018-12-18 00:18:46 +01002666 PSA_ASSERT( psa_import_key( handle, key_type,
2667 key_data->x,
2668 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002669
Gilles Peskine8817f612018-12-18 00:18:46 +01002670 PSA_ASSERT( psa_aead_encrypt( handle, alg,
2671 nonce->x, nonce->len,
2672 additional_data->x, additional_data->len,
2673 input_data->x, input_data->len,
2674 output_data, output_size,
2675 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002676
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002677 ASSERT_COMPARE( expected_result->x, expected_result->len,
2678 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002679
Gilles Peskinea1cac842018-06-11 19:33:02 +02002680exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002681 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002682 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002683 mbedtls_psa_crypto_free( );
2684}
2685/* END_CASE */
2686
2687/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002688void aead_decrypt( int key_type_arg, data_t *key_data,
2689 int alg_arg,
2690 data_t *nonce,
2691 data_t *additional_data,
2692 data_t *input_data,
2693 data_t *expected_data,
2694 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002695{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002696 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002697 psa_key_type_t key_type = key_type_arg;
2698 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002699 unsigned char *output_data = NULL;
2700 size_t output_size = 0;
2701 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002702 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002703 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002704 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002705
Gilles Peskine4abf7412018-06-18 16:35:34 +02002706 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002707 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002708
Gilles Peskine8817f612018-12-18 00:18:46 +01002709 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002710
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002711 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002712 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002713 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002714
Gilles Peskine8817f612018-12-18 00:18:46 +01002715 PSA_ASSERT( psa_import_key( handle, key_type,
2716 key_data->x,
2717 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002718
Gilles Peskinefe11b722018-12-18 00:24:04 +01002719 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2720 nonce->x, nonce->len,
2721 additional_data->x,
2722 additional_data->len,
2723 input_data->x, input_data->len,
2724 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002725 &output_length ),
2726 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002727
Gilles Peskine2d277862018-06-18 15:41:12 +02002728 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002729 ASSERT_COMPARE( expected_data->x, expected_data->len,
2730 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002731
Gilles Peskinea1cac842018-06-11 19:33:02 +02002732exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002733 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002734 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002735 mbedtls_psa_crypto_free( );
2736}
2737/* END_CASE */
2738
2739/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002740void signature_size( int type_arg,
2741 int bits,
2742 int alg_arg,
2743 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002744{
2745 psa_key_type_t type = type_arg;
2746 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002747 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002748 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002749exit:
2750 ;
2751}
2752/* END_CASE */
2753
2754/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002755void sign_deterministic( int key_type_arg, data_t *key_data,
2756 int alg_arg, data_t *input_data,
2757 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002758{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002759 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01002760 psa_key_type_t key_type = key_type_arg;
2761 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002762 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002763 unsigned char *signature = NULL;
2764 size_t signature_size;
2765 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002766 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01002767
Gilles Peskine8817f612018-12-18 00:18:46 +01002768 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002769
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002770 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002771 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002772 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002773
Gilles Peskine8817f612018-12-18 00:18:46 +01002774 PSA_ASSERT( psa_import_key( handle, key_type,
2775 key_data->x,
2776 key_data->len ) );
2777 PSA_ASSERT( psa_get_key_information( handle,
2778 NULL,
2779 &key_bits ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002780
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002781 /* Allocate a buffer which has the size advertized by the
2782 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002783 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2784 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002785 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002786 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002787 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002788
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002789 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002790 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2791 input_data->x, input_data->len,
2792 signature, signature_size,
2793 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002794 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002795 ASSERT_COMPARE( output_data->x, output_data->len,
2796 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002797
2798exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002799 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01002800 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002801 mbedtls_psa_crypto_free( );
2802}
2803/* END_CASE */
2804
2805/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002806void sign_fail( int key_type_arg, data_t *key_data,
2807 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002808 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002809{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002810 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01002811 psa_key_type_t key_type = key_type_arg;
2812 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002813 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002814 psa_status_t actual_status;
2815 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002816 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002817 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002818 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01002819
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002820 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002821
Gilles Peskine8817f612018-12-18 00:18:46 +01002822 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002823
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002824 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002825 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002826 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002827
Gilles Peskine8817f612018-12-18 00:18:46 +01002828 PSA_ASSERT( psa_import_key( handle, key_type,
2829 key_data->x,
2830 key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002831
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002832 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002833 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002834 signature, signature_size,
2835 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002836 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002837 /* The value of *signature_length is unspecified on error, but
2838 * whatever it is, it should be less than signature_size, so that
2839 * if the caller tries to read *signature_length bytes without
2840 * checking the error code then they don't overflow a buffer. */
2841 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002842
2843exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002844 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01002845 mbedtls_free( signature );
2846 mbedtls_psa_crypto_free( );
2847}
2848/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002849
2850/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002851void sign_verify( int key_type_arg, data_t *key_data,
2852 int alg_arg, data_t *input_data )
2853{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002854 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02002855 psa_key_type_t key_type = key_type_arg;
2856 psa_algorithm_t alg = alg_arg;
2857 size_t key_bits;
2858 unsigned char *signature = NULL;
2859 size_t signature_size;
2860 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002861 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02002862
Gilles Peskine8817f612018-12-18 00:18:46 +01002863 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002864
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002865 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002866 psa_key_policy_set_usage( &policy,
2867 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2868 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002869 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002870
Gilles Peskine8817f612018-12-18 00:18:46 +01002871 PSA_ASSERT( psa_import_key( handle, key_type,
2872 key_data->x,
2873 key_data->len ) );
2874 PSA_ASSERT( psa_get_key_information( handle,
2875 NULL,
2876 &key_bits ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002877
2878 /* Allocate a buffer which has the size advertized by the
2879 * library. */
2880 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2881 key_bits, alg );
2882 TEST_ASSERT( signature_size != 0 );
2883 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002884 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002885
2886 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002887 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2888 input_data->x, input_data->len,
2889 signature, signature_size,
2890 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002891 /* Check that the signature length looks sensible. */
2892 TEST_ASSERT( signature_length <= signature_size );
2893 TEST_ASSERT( signature_length > 0 );
2894
2895 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002896 PSA_ASSERT( psa_asymmetric_verify(
2897 handle, alg,
2898 input_data->x, input_data->len,
2899 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002900
2901 if( input_data->len != 0 )
2902 {
2903 /* Flip a bit in the input and verify that the signature is now
2904 * detected as invalid. Flip a bit at the beginning, not at the end,
2905 * because ECDSA may ignore the last few bits of the input. */
2906 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002907 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
2908 input_data->x, input_data->len,
2909 signature, signature_length ),
2910 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02002911 }
2912
2913exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002914 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02002915 mbedtls_free( signature );
2916 mbedtls_psa_crypto_free( );
2917}
2918/* END_CASE */
2919
2920/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002921void asymmetric_verify( int key_type_arg, data_t *key_data,
2922 int alg_arg, data_t *hash_data,
2923 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002924{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002925 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03002926 psa_key_type_t key_type = key_type_arg;
2927 psa_algorithm_t alg = alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002928 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03002929
Gilles Peskine69c12672018-06-28 00:07:19 +02002930 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2931
Gilles Peskine8817f612018-12-18 00:18:46 +01002932 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03002933
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002934 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002935 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002936 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
itayzafrir5c753392018-05-08 11:18:38 +03002937
Gilles Peskine8817f612018-12-18 00:18:46 +01002938 PSA_ASSERT( psa_import_key( handle, key_type,
2939 key_data->x,
2940 key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002941
Gilles Peskine8817f612018-12-18 00:18:46 +01002942 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
2943 hash_data->x, hash_data->len,
2944 signature_data->x,
2945 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002946exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002947 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03002948 mbedtls_psa_crypto_free( );
2949}
2950/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002951
2952/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002953void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2954 int alg_arg, data_t *hash_data,
2955 data_t *signature_data,
2956 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002957{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002958 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002959 psa_key_type_t key_type = key_type_arg;
2960 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002961 psa_status_t actual_status;
2962 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002963 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002964
Gilles Peskine8817f612018-12-18 00:18:46 +01002965 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002966
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002967 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002968 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002969 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002970
Gilles Peskine8817f612018-12-18 00:18:46 +01002971 PSA_ASSERT( psa_import_key( handle, key_type,
2972 key_data->x,
2973 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002974
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002975 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002976 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002977 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002978 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002979
Gilles Peskinefe11b722018-12-18 00:24:04 +01002980 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002981
2982exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002983 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002984 mbedtls_psa_crypto_free( );
2985}
2986/* END_CASE */
2987
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002988/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002989void asymmetric_encrypt( int key_type_arg,
2990 data_t *key_data,
2991 int alg_arg,
2992 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02002993 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02002994 int expected_output_length_arg,
2995 int expected_status_arg )
2996{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002997 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02002998 psa_key_type_t key_type = key_type_arg;
2999 psa_algorithm_t alg = alg_arg;
3000 size_t expected_output_length = expected_output_length_arg;
3001 size_t key_bits;
3002 unsigned char *output = NULL;
3003 size_t output_size;
3004 size_t output_length = ~0;
3005 psa_status_t actual_status;
3006 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003007 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003008
Gilles Peskine8817f612018-12-18 00:18:46 +01003009 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003010
Gilles Peskine656896e2018-06-29 19:12:28 +02003011 /* Import the key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003012 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003013 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003014 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
3015 PSA_ASSERT( psa_import_key( handle, key_type,
3016 key_data->x,
3017 key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003018
3019 /* Determine the maximum output length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003020 PSA_ASSERT( psa_get_key_information( handle,
3021 NULL,
3022 &key_bits ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003023 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003024 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003025
3026 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003027 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003028 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003029 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003030 output, output_size,
3031 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003032 TEST_EQUAL( actual_status, expected_status );
3033 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003034
Gilles Peskine68428122018-06-30 18:42:41 +02003035 /* If the label is empty, the test framework puts a non-null pointer
3036 * in label->x. Test that a null pointer works as well. */
3037 if( label->len == 0 )
3038 {
3039 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003040 if( output_size != 0 )
3041 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003042 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003043 input_data->x, input_data->len,
3044 NULL, label->len,
3045 output, output_size,
3046 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003047 TEST_EQUAL( actual_status, expected_status );
3048 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003049 }
3050
Gilles Peskine656896e2018-06-29 19:12:28 +02003051exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003052 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003053 mbedtls_free( output );
3054 mbedtls_psa_crypto_free( );
3055}
3056/* END_CASE */
3057
3058/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003059void asymmetric_encrypt_decrypt( int key_type_arg,
3060 data_t *key_data,
3061 int alg_arg,
3062 data_t *input_data,
3063 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003064{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003065 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003066 psa_key_type_t key_type = key_type_arg;
3067 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003068 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003069 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003070 size_t output_size;
3071 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003072 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003073 size_t output2_size;
3074 size_t output2_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003075 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003076
Gilles Peskine8817f612018-12-18 00:18:46 +01003077 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003078
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003079 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003080 psa_key_policy_set_usage( &policy,
3081 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003082 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003083 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003084
Gilles Peskine8817f612018-12-18 00:18:46 +01003085 PSA_ASSERT( psa_import_key( handle, key_type,
3086 key_data->x,
3087 key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003088
3089 /* Determine the maximum ciphertext length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003090 PSA_ASSERT( psa_get_key_information( handle,
3091 NULL,
3092 &key_bits ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003093 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003094 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003095 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003096 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003097
Gilles Peskineeebd7382018-06-08 18:11:54 +02003098 /* We test encryption by checking that encrypt-then-decrypt gives back
3099 * the original plaintext because of the non-optional random
3100 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003101 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3102 input_data->x, input_data->len,
3103 label->x, label->len,
3104 output, output_size,
3105 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003106 /* We don't know what ciphertext length to expect, but check that
3107 * it looks sensible. */
3108 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003109
Gilles Peskine8817f612018-12-18 00:18:46 +01003110 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3111 output, output_length,
3112 label->x, label->len,
3113 output2, output2_size,
3114 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003115 ASSERT_COMPARE( input_data->x, input_data->len,
3116 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003117
3118exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003119 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003120 mbedtls_free( output );
3121 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003122 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003123}
3124/* END_CASE */
3125
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003126/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003127void asymmetric_decrypt( int key_type_arg,
3128 data_t *key_data,
3129 int alg_arg,
3130 data_t *input_data,
3131 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003132 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003133{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003134 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003135 psa_key_type_t key_type = key_type_arg;
3136 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003137 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003138 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003139 size_t output_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003140 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003141
Gilles Peskine4abf7412018-06-18 16:35:34 +02003142 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003143 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003144
Gilles Peskine8817f612018-12-18 00:18:46 +01003145 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003146
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003147 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003148 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003149 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003150
Gilles Peskine8817f612018-12-18 00:18:46 +01003151 PSA_ASSERT( psa_import_key( handle, key_type,
3152 key_data->x,
3153 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003154
Gilles Peskine8817f612018-12-18 00:18:46 +01003155 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3156 input_data->x, input_data->len,
3157 label->x, label->len,
3158 output,
3159 output_size,
3160 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003161 ASSERT_COMPARE( expected_data->x, expected_data->len,
3162 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003163
Gilles Peskine68428122018-06-30 18:42:41 +02003164 /* If the label is empty, the test framework puts a non-null pointer
3165 * in label->x. Test that a null pointer works as well. */
3166 if( label->len == 0 )
3167 {
3168 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003169 if( output_size != 0 )
3170 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003171 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3172 input_data->x, input_data->len,
3173 NULL, label->len,
3174 output,
3175 output_size,
3176 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003177 ASSERT_COMPARE( expected_data->x, expected_data->len,
3178 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003179 }
3180
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003181exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003182 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003183 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003184 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003185}
3186/* END_CASE */
3187
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003188/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003189void asymmetric_decrypt_fail( int key_type_arg,
3190 data_t *key_data,
3191 int alg_arg,
3192 data_t *input_data,
3193 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003194 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003195{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003196 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003197 psa_key_type_t key_type = key_type_arg;
3198 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003199 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003200 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003201 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003202 psa_status_t actual_status;
3203 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003204 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003205
Gilles Peskine4abf7412018-06-18 16:35:34 +02003206 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003207 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003208
Gilles Peskine8817f612018-12-18 00:18:46 +01003209 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003210
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003211 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003212 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003213 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003214
Gilles Peskine8817f612018-12-18 00:18:46 +01003215 PSA_ASSERT( psa_import_key( handle, key_type,
3216 key_data->x,
3217 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003218
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003219 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003220 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003221 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003222 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003223 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003224 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003225 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003226
Gilles Peskine68428122018-06-30 18:42:41 +02003227 /* If the label is empty, the test framework puts a non-null pointer
3228 * in label->x. Test that a null pointer works as well. */
3229 if( label->len == 0 )
3230 {
3231 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003232 if( output_size != 0 )
3233 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003234 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003235 input_data->x, input_data->len,
3236 NULL, label->len,
3237 output, output_size,
3238 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003239 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003240 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003241 }
3242
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003243exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003244 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02003245 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003246 mbedtls_psa_crypto_free( );
3247}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003248/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003249
3250/* BEGIN_CASE */
Jaeden Amerod94d6712019-01-04 14:11:48 +00003251void crypto_generator_init( )
3252{
3253 /* Test each valid way of initializing the object, except for `= {0}`, as
3254 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3255 * though it's OK by the C standard. We could test for this, but we'd need
3256 * to supress the Clang warning for the test. */
3257 psa_crypto_generator_t func = psa_crypto_generator_init( );
3258 psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT;
3259 psa_crypto_generator_t zero;
3260
3261 memset( &zero, 0, sizeof( zero ) );
3262
3263 /* Although not technically guaranteed by the C standard nor the PSA Crypto
3264 * specification, we test that all valid ways of initializing the object
3265 * have the same bit pattern. This is a stronger requirement that may not
3266 * be valid on all platforms or PSA Crypto implementations, but implies the
3267 * weaker actual requirement is met: that a freshly initialized object, no
3268 * matter how it was initialized, acts the same as any other valid
3269 * initialization. */
3270 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
3271 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
3272}
3273/* END_CASE */
3274
3275/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003276void derive_setup( int key_type_arg,
3277 data_t *key_data,
3278 int alg_arg,
3279 data_t *salt,
3280 data_t *label,
3281 int requested_capacity_arg,
3282 int expected_status_arg )
3283{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003284 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003285 size_t key_type = key_type_arg;
3286 psa_algorithm_t alg = alg_arg;
3287 size_t requested_capacity = requested_capacity_arg;
3288 psa_status_t expected_status = expected_status_arg;
3289 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003290 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003291
Gilles Peskine8817f612018-12-18 00:18:46 +01003292 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003293
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003294 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003295 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003296 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003297
Gilles Peskine8817f612018-12-18 00:18:46 +01003298 PSA_ASSERT( psa_import_key( handle, key_type,
3299 key_data->x,
3300 key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003301
Gilles Peskinefe11b722018-12-18 00:24:04 +01003302 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3303 salt->x, salt->len,
3304 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003305 requested_capacity ),
3306 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003307
3308exit:
3309 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003310 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003311 mbedtls_psa_crypto_free( );
3312}
3313/* END_CASE */
3314
3315/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003316void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003317{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003318 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003319 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003320 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003321 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003322 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003323 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003324 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3325 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3326 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Jaeden Amero70261c52019-01-04 11:47:20 +00003327 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003328
Gilles Peskine8817f612018-12-18 00:18:46 +01003329 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003330
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003331 PSA_ASSERT( psa_allocate_key( &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003332 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003333 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003334
Gilles Peskine8817f612018-12-18 00:18:46 +01003335 PSA_ASSERT( psa_import_key( handle, key_type,
3336 key_data,
3337 sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003338
3339 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01003340 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3341 NULL, 0,
3342 NULL, 0,
3343 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003344
3345 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003346 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3347 NULL, 0,
3348 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003349 capacity ),
3350 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003351
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003352 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003353
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003354 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
3355 PSA_ERROR_INSUFFICIENT_CAPACITY );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003356
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003357exit:
3358 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003359 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003360 mbedtls_psa_crypto_free( );
3361}
3362/* END_CASE */
3363
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003364/* BEGIN_CASE */
3365void test_derive_invalid_generator_tests( )
3366{
3367 uint8_t output_buffer[16];
3368 size_t buffer_size = 16;
3369 size_t capacity = 0;
3370 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3371
Nir Sonnenschein50789302018-10-31 12:16:38 +02003372 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003373 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003374
3375 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003376 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003377
Gilles Peskine8817f612018-12-18 00:18:46 +01003378 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003379
Nir Sonnenschein50789302018-10-31 12:16:38 +02003380 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003381 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003382
Nir Sonnenschein50789302018-10-31 12:16:38 +02003383 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003384 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003385
3386exit:
3387 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003388}
3389/* END_CASE */
3390
3391/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003392void derive_output( int alg_arg,
3393 data_t *key_data,
3394 data_t *salt,
3395 data_t *label,
3396 int requested_capacity_arg,
3397 data_t *expected_output1,
3398 data_t *expected_output2 )
3399{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003400 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003401 psa_algorithm_t alg = alg_arg;
3402 size_t requested_capacity = requested_capacity_arg;
3403 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3404 uint8_t *expected_outputs[2] =
3405 {expected_output1->x, expected_output2->x};
3406 size_t output_sizes[2] =
3407 {expected_output1->len, expected_output2->len};
3408 size_t output_buffer_size = 0;
3409 uint8_t *output_buffer = NULL;
3410 size_t expected_capacity;
3411 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003412 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003413 psa_status_t status;
3414 unsigned i;
3415
3416 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3417 {
3418 if( output_sizes[i] > output_buffer_size )
3419 output_buffer_size = output_sizes[i];
3420 if( output_sizes[i] == 0 )
3421 expected_outputs[i] = NULL;
3422 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003423 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003424 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003425
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003426 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003427 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003428 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003429
Gilles Peskine8817f612018-12-18 00:18:46 +01003430 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3431 key_data->x,
3432 key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003433
3434 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003435 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3436 salt->x, salt->len,
3437 label->x, label->len,
3438 requested_capacity ) );
3439 PSA_ASSERT( psa_get_generator_capacity( &generator,
3440 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003441 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003442 expected_capacity = requested_capacity;
3443
3444 /* Expansion phase. */
3445 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3446 {
3447 /* Read some bytes. */
3448 status = psa_generator_read( &generator,
3449 output_buffer, output_sizes[i] );
3450 if( expected_capacity == 0 && output_sizes[i] == 0 )
3451 {
3452 /* Reading 0 bytes when 0 bytes are available can go either way. */
3453 TEST_ASSERT( status == PSA_SUCCESS ||
3454 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3455 continue;
3456 }
3457 else if( expected_capacity == 0 ||
3458 output_sizes[i] > expected_capacity )
3459 {
3460 /* Capacity exceeded. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003461 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003462 expected_capacity = 0;
3463 continue;
3464 }
3465 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003466 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003467 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003468 ASSERT_COMPARE( output_buffer, output_sizes[i],
3469 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003470 /* Check the generator status. */
3471 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01003472 PSA_ASSERT( psa_get_generator_capacity( &generator,
3473 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003474 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003475 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003476 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003477
3478exit:
3479 mbedtls_free( output_buffer );
3480 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003481 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003482 mbedtls_psa_crypto_free( );
3483}
3484/* END_CASE */
3485
3486/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003487void derive_full( int alg_arg,
3488 data_t *key_data,
3489 data_t *salt,
3490 data_t *label,
3491 int requested_capacity_arg )
3492{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003493 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02003494 psa_algorithm_t alg = alg_arg;
3495 size_t requested_capacity = requested_capacity_arg;
3496 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3497 unsigned char output_buffer[16];
3498 size_t expected_capacity = requested_capacity;
3499 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003500 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02003501
Gilles Peskine8817f612018-12-18 00:18:46 +01003502 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003503
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003504 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003505 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003506 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003507
Gilles Peskine8817f612018-12-18 00:18:46 +01003508 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3509 key_data->x,
3510 key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003511
3512 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003513 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3514 salt->x, salt->len,
3515 label->x, label->len,
3516 requested_capacity ) );
3517 PSA_ASSERT( psa_get_generator_capacity( &generator,
3518 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003519 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003520
3521 /* Expansion phase. */
3522 while( current_capacity > 0 )
3523 {
3524 size_t read_size = sizeof( output_buffer );
3525 if( read_size > current_capacity )
3526 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01003527 PSA_ASSERT( psa_generator_read( &generator,
3528 output_buffer,
3529 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003530 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01003531 PSA_ASSERT( psa_get_generator_capacity( &generator,
3532 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003533 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003534 }
3535
3536 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003537 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
3538 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskined54931c2018-07-17 21:06:59 +02003539
Gilles Peskine8817f612018-12-18 00:18:46 +01003540 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003541
3542exit:
3543 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003544 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02003545 mbedtls_psa_crypto_free( );
3546}
3547/* END_CASE */
3548
3549/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003550void derive_key_exercise( int alg_arg,
3551 data_t *key_data,
3552 data_t *salt,
3553 data_t *label,
3554 int derived_type_arg,
3555 int derived_bits_arg,
3556 int derived_usage_arg,
3557 int derived_alg_arg )
3558{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003559 psa_key_handle_t base_handle = 0;
3560 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003561 psa_algorithm_t alg = alg_arg;
3562 psa_key_type_t derived_type = derived_type_arg;
3563 size_t derived_bits = derived_bits_arg;
3564 psa_key_usage_t derived_usage = derived_usage_arg;
3565 psa_algorithm_t derived_alg = derived_alg_arg;
3566 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3567 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003568 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003569 psa_key_type_t got_type;
3570 size_t got_bits;
3571
Gilles Peskine8817f612018-12-18 00:18:46 +01003572 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003573
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003574 PSA_ASSERT( psa_allocate_key( &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003575 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003576 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3577 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3578 key_data->x,
3579 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003580
3581 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003582 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3583 salt->x, salt->len,
3584 label->x, label->len,
3585 capacity ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003586 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003587 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003588 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3589 PSA_ASSERT( psa_generator_import_key( derived_handle,
3590 derived_type,
3591 derived_bits,
3592 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003593
3594 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01003595 PSA_ASSERT( psa_get_key_information( derived_handle,
3596 &got_type,
3597 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003598 TEST_EQUAL( got_type, derived_type );
3599 TEST_EQUAL( got_bits, derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003600
3601 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003602 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02003603 goto exit;
3604
3605exit:
3606 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003607 psa_destroy_key( base_handle );
3608 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003609 mbedtls_psa_crypto_free( );
3610}
3611/* END_CASE */
3612
3613/* BEGIN_CASE */
3614void derive_key_export( int alg_arg,
3615 data_t *key_data,
3616 data_t *salt,
3617 data_t *label,
3618 int bytes1_arg,
3619 int bytes2_arg )
3620{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003621 psa_key_handle_t base_handle = 0;
3622 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003623 psa_algorithm_t alg = alg_arg;
3624 size_t bytes1 = bytes1_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003625 size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003626 size_t bytes2 = bytes2_arg;
3627 size_t capacity = bytes1 + bytes2;
3628 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003629 uint8_t *output_buffer = NULL;
3630 uint8_t *export_buffer = NULL;
Jaeden Amero70261c52019-01-04 11:47:20 +00003631 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003632 size_t length;
3633
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003634 ASSERT_ALLOC( output_buffer, capacity );
3635 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01003636 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003637
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003638 PSA_ASSERT( psa_allocate_key( &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003639 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003640 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3641 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3642 key_data->x,
3643 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003644
3645 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003646 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3647 salt->x, salt->len,
3648 label->x, label->len,
3649 capacity ) );
3650 PSA_ASSERT( psa_generator_read( &generator,
3651 output_buffer,
3652 capacity ) );
3653 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003654
3655 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003656 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3657 salt->x, salt->len,
3658 label->x, label->len,
3659 capacity ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003660 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003661 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003662 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3663 PSA_ASSERT( psa_generator_import_key( derived_handle,
3664 PSA_KEY_TYPE_RAW_DATA,
3665 derived_bits,
3666 &generator ) );
3667 PSA_ASSERT( psa_export_key( derived_handle,
3668 export_buffer, bytes1,
3669 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003670 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003671 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003672 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003673 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3674 PSA_ASSERT( psa_generator_import_key( derived_handle,
3675 PSA_KEY_TYPE_RAW_DATA,
3676 PSA_BYTES_TO_BITS( bytes2 ),
3677 &generator ) );
3678 PSA_ASSERT( psa_export_key( derived_handle,
3679 export_buffer + bytes1, bytes2,
3680 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003681 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003682
3683 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003684 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
3685 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003686
3687exit:
3688 mbedtls_free( output_buffer );
3689 mbedtls_free( export_buffer );
3690 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003691 psa_destroy_key( base_handle );
3692 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003693 mbedtls_psa_crypto_free( );
3694}
3695/* END_CASE */
3696
3697/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02003698void key_agreement_setup( int alg_arg,
3699 int our_key_type_arg, data_t *our_key_data,
3700 data_t *peer_key_data,
3701 int expected_status_arg )
3702{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003703 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003704 psa_algorithm_t alg = alg_arg;
3705 psa_key_type_t our_key_type = our_key_type_arg;
3706 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003707 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003708
Gilles Peskine8817f612018-12-18 00:18:46 +01003709 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003710
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003711 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003712 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003713 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3714 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3715 our_key_data->x,
3716 our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003717
Gilles Peskinefe11b722018-12-18 00:24:04 +01003718 TEST_EQUAL( psa_key_agreement( &generator,
3719 our_key,
3720 peer_key_data->x, peer_key_data->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003721 alg ),
3722 expected_status_arg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003723
3724exit:
3725 psa_generator_abort( &generator );
3726 psa_destroy_key( our_key );
3727 mbedtls_psa_crypto_free( );
3728}
3729/* END_CASE */
3730
3731/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02003732void key_agreement_capacity( int alg_arg,
3733 int our_key_type_arg, data_t *our_key_data,
3734 data_t *peer_key_data,
3735 int expected_capacity_arg )
3736{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003737 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003738 psa_algorithm_t alg = alg_arg;
3739 psa_key_type_t our_key_type = our_key_type_arg;
3740 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003741 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02003742 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02003743 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02003744
Gilles Peskine8817f612018-12-18 00:18:46 +01003745 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003746
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003747 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003748 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003749 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3750 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3751 our_key_data->x,
3752 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003753
Gilles Peskine8817f612018-12-18 00:18:46 +01003754 PSA_ASSERT( psa_key_agreement( &generator,
3755 our_key,
3756 peer_key_data->x, peer_key_data->len,
3757 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003758
Gilles Peskinebf491972018-10-25 22:36:12 +02003759 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003760 PSA_ASSERT( psa_get_generator_capacity(
3761 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003762 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02003763
Gilles Peskinebf491972018-10-25 22:36:12 +02003764 /* Test the actual capacity by reading the output. */
3765 while( actual_capacity > sizeof( output ) )
3766 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003767 PSA_ASSERT( psa_generator_read( &generator,
3768 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02003769 actual_capacity -= sizeof( output );
3770 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003771 PSA_ASSERT( psa_generator_read( &generator,
3772 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003773 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
3774 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskinebf491972018-10-25 22:36:12 +02003775
Gilles Peskine59685592018-09-18 12:11:34 +02003776exit:
3777 psa_generator_abort( &generator );
3778 psa_destroy_key( our_key );
3779 mbedtls_psa_crypto_free( );
3780}
3781/* END_CASE */
3782
3783/* BEGIN_CASE */
3784void key_agreement_output( int alg_arg,
3785 int our_key_type_arg, data_t *our_key_data,
3786 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003787 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02003788{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003789 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003790 psa_algorithm_t alg = alg_arg;
3791 psa_key_type_t our_key_type = our_key_type_arg;
3792 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003793 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003794 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02003795
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003796 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
3797 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003798
Gilles Peskine8817f612018-12-18 00:18:46 +01003799 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003800
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003801 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003802 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003803 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3804 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3805 our_key_data->x,
3806 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003807
Gilles Peskine8817f612018-12-18 00:18:46 +01003808 PSA_ASSERT( psa_key_agreement( &generator,
3809 our_key,
3810 peer_key_data->x, peer_key_data->len,
3811 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003812
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003813 PSA_ASSERT( psa_generator_read( &generator,
3814 actual_output,
3815 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003816 ASSERT_COMPARE( actual_output, expected_output1->len,
3817 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003818 if( expected_output2->len != 0 )
3819 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003820 PSA_ASSERT( psa_generator_read( &generator,
3821 actual_output,
3822 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003823 ASSERT_COMPARE( actual_output, expected_output2->len,
3824 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003825 }
Gilles Peskine59685592018-09-18 12:11:34 +02003826
3827exit:
3828 psa_generator_abort( &generator );
3829 psa_destroy_key( our_key );
3830 mbedtls_psa_crypto_free( );
3831 mbedtls_free( actual_output );
3832}
3833/* END_CASE */
3834
3835/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003836void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003837{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003838 size_t bytes = bytes_arg;
3839 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003840 unsigned char *output = NULL;
3841 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003842 size_t i;
3843 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003844
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003845 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3846 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003847 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003848
Gilles Peskine8817f612018-12-18 00:18:46 +01003849 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003850
Gilles Peskinea50d7392018-06-21 10:22:13 +02003851 /* Run several times, to ensure that every output byte will be
3852 * nonzero at least once with overwhelming probability
3853 * (2^(-8*number_of_runs)). */
3854 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003855 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003856 if( bytes != 0 )
3857 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01003858 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003859
3860 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003861 ASSERT_COMPARE( output + bytes, sizeof( trail ),
3862 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003863
3864 for( i = 0; i < bytes; i++ )
3865 {
3866 if( output[i] != 0 )
3867 ++changed[i];
3868 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003869 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003870
3871 /* Check that every byte was changed to nonzero at least once. This
3872 * validates that psa_generate_random is overwriting every byte of
3873 * the output buffer. */
3874 for( i = 0; i < bytes; i++ )
3875 {
3876 TEST_ASSERT( changed[i] != 0 );
3877 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003878
3879exit:
3880 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003881 mbedtls_free( output );
3882 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003883}
3884/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003885
3886/* BEGIN_CASE */
3887void generate_key( int type_arg,
3888 int bits_arg,
3889 int usage_arg,
3890 int alg_arg,
3891 int expected_status_arg )
3892{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003893 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003894 psa_key_type_t type = type_arg;
3895 psa_key_usage_t usage = usage_arg;
3896 size_t bits = bits_arg;
3897 psa_algorithm_t alg = alg_arg;
3898 psa_status_t expected_status = expected_status_arg;
3899 psa_key_type_t got_type;
3900 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003901 psa_status_t expected_info_status =
3902 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003903 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003904
Gilles Peskine8817f612018-12-18 00:18:46 +01003905 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003906
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003907 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003908 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003909 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003910
3911 /* Generate a key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003912 TEST_EQUAL( psa_generate_key( handle, type, bits, NULL, 0 ),
3913 expected_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003914
3915 /* Test the key information */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003916 TEST_EQUAL( psa_get_key_information( handle, &got_type, &got_bits ),
3917 expected_info_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003918 if( expected_info_status != PSA_SUCCESS )
3919 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003920 TEST_EQUAL( got_type, type );
3921 TEST_EQUAL( got_bits, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003922
Gilles Peskine818ca122018-06-20 18:16:48 +02003923 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003924 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02003925 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003926
3927exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003928 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003929 mbedtls_psa_crypto_free( );
3930}
3931/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03003932
Darryl Greend49a4992018-06-18 17:27:26 +01003933/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
3934void persistent_key_load_key_from_storage( data_t *data, int type_arg,
3935 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00003936 int alg_arg, int generation_method,
3937 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01003938{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003939 psa_key_handle_t handle = 0;
3940 psa_key_handle_t base_key;
Darryl Greend49a4992018-06-18 17:27:26 +01003941 psa_key_type_t type = (psa_key_type_t) type_arg;
3942 psa_key_type_t type_get;
3943 size_t bits_get;
Jaeden Amero70261c52019-01-04 11:47:20 +00003944 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
3945 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01003946 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
3947 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003948 psa_key_policy_t base_policy_set = PSA_KEY_POLICY_INIT;
Darryl Green0c6575a2018-11-07 16:05:30 +00003949 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
3950 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01003951 unsigned char *first_export = NULL;
3952 unsigned char *second_export = NULL;
3953 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
3954 size_t first_exported_length;
3955 size_t second_exported_length;
3956
3957 ASSERT_ALLOC( first_export, export_size );
3958 ASSERT_ALLOC( second_export, export_size );
3959
Gilles Peskine8817f612018-12-18 00:18:46 +01003960 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01003961
Gilles Peskine8817f612018-12-18 00:18:46 +01003962 PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
Gilles Peskine8817f612018-12-18 00:18:46 +01003963 &handle ) );
Darryl Greend49a4992018-06-18 17:27:26 +01003964 psa_key_policy_set_usage( &policy_set, policy_usage,
3965 policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003966 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Darryl Greend49a4992018-06-18 17:27:26 +01003967
Darryl Green0c6575a2018-11-07 16:05:30 +00003968 switch( generation_method )
3969 {
3970 case IMPORT_KEY:
3971 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01003972 PSA_ASSERT( psa_import_key( handle, type,
3973 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00003974 break;
Darryl Greend49a4992018-06-18 17:27:26 +01003975
Darryl Green0c6575a2018-11-07 16:05:30 +00003976 case GENERATE_KEY:
3977 /* Generate a key */
Gilles Peskine8817f612018-12-18 00:18:46 +01003978 PSA_ASSERT( psa_generate_key( handle, type, bits,
3979 NULL, 0 ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00003980 break;
3981
3982 case DERIVE_KEY:
3983 /* Create base key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003984 PSA_ASSERT( psa_allocate_key( &base_key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00003985 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
3986 base_policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003987 PSA_ASSERT( psa_set_key_policy(
3988 base_key, &base_policy_set ) );
3989 PSA_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3990 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00003991 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003992 PSA_ASSERT( psa_key_derivation( &generator, base_key,
3993 base_policy_alg,
3994 NULL, 0, NULL, 0,
3995 export_size ) );
3996 PSA_ASSERT( psa_generator_import_key(
3997 handle, PSA_KEY_TYPE_RAW_DATA,
3998 bits, &generator ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00003999 break;
4000 }
Darryl Greend49a4992018-06-18 17:27:26 +01004001
4002 /* Export the key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004003 TEST_EQUAL( psa_export_key( handle,
4004 first_export, export_size,
4005 &first_exported_length ),
4006 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004007
4008 /* Shutdown and restart */
4009 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004010 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004011
Darryl Greend49a4992018-06-18 17:27:26 +01004012 /* Check key slot still contains key data */
Gilles Peskine8817f612018-12-18 00:18:46 +01004013 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4014 &handle ) );
4015 PSA_ASSERT( psa_get_key_information(
4016 handle, &type_get, &bits_get ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004017 TEST_EQUAL( type_get, type );
4018 TEST_EQUAL( bits_get, (size_t) bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004019
Gilles Peskine8817f612018-12-18 00:18:46 +01004020 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004021 TEST_EQUAL( psa_key_policy_get_usage( &policy_get ), policy_usage );
4022 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_get ), policy_alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004023
4024 /* Export the key again */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004025 TEST_EQUAL( psa_export_key( handle,
4026 second_export, export_size,
4027 &second_exported_length ),
4028 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004029
Darryl Green0c6575a2018-11-07 16:05:30 +00004030 if( export_status == PSA_SUCCESS )
4031 {
4032 ASSERT_COMPARE( first_export, first_exported_length,
4033 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01004034
Darryl Green0c6575a2018-11-07 16:05:30 +00004035 switch( generation_method )
4036 {
4037 case IMPORT_KEY:
4038 ASSERT_COMPARE( data->x, data->len,
4039 first_export, first_exported_length );
4040 break;
4041 default:
4042 break;
4043 }
4044 }
4045
4046 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004047 if( ! exercise_key( handle, policy_usage, policy_alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004048 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004049
4050exit:
4051 mbedtls_free( first_export );
4052 mbedtls_free( second_export );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004053 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004054 mbedtls_psa_crypto_free();
4055}
4056/* END_CASE */