blob: bc3d78878b7657d76fecac47d996d5fc84e544b2 [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,
Gilles Peskine0ce26e32019-01-14 16:06:39 +01001642 int exercise_alg,
1643 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001644{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001645 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001646 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001647 psa_status_t status;
Gilles Peskine0ce26e32019-01-14 16:06:39 +01001648 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1649 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1650 * compatible with the policy and `payload_length_arg` is supposed to be
1651 * a valid input length to sign. If `payload_length_arg <= 0`,
1652 * `exercise_alg` is supposed to be forbidden by the policy. */
1653 int compatible_alg = payload_length_arg > 0;
1654 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001655 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1656 size_t signature_length;
1657
Gilles Peskine8817f612018-12-18 00:18:46 +01001658 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001659
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001660 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001661 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001662 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001663
Gilles Peskine8817f612018-12-18 00:18:46 +01001664 PSA_ASSERT( psa_import_key( handle, key_type,
1665 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001666
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001667 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001668 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001669 signature, sizeof( signature ),
1670 &signature_length );
Gilles Peskine0ce26e32019-01-14 16:06:39 +01001671 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001672 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001673 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001674 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001675
1676 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001677 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001678 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001679 signature, sizeof( signature ) );
Gilles Peskine0ce26e32019-01-14 16:06:39 +01001680 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001681 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001682 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001683 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001684
1685exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001686 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001687 mbedtls_psa_crypto_free( );
1688}
1689/* END_CASE */
1690
1691/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001692void derive_key_policy( int policy_usage,
1693 int policy_alg,
1694 int key_type,
1695 data_t *key_data,
1696 int exercise_alg )
1697{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001698 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001699 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001700 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1701 psa_status_t status;
1702
Gilles Peskine8817f612018-12-18 00:18:46 +01001703 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001704
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001705 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001706 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001707 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001708
Gilles Peskine8817f612018-12-18 00:18:46 +01001709 PSA_ASSERT( psa_import_key( handle, key_type,
1710 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001711
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001712 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001713 exercise_alg,
1714 NULL, 0,
1715 NULL, 0,
1716 1 );
1717 if( policy_alg == exercise_alg &&
1718 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001719 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001720 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001721 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001722
1723exit:
1724 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001725 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001726 mbedtls_psa_crypto_free( );
1727}
1728/* END_CASE */
1729
1730/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001731void agreement_key_policy( int policy_usage,
1732 int policy_alg,
1733 int key_type_arg,
1734 data_t *key_data,
1735 int exercise_alg )
1736{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001737 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001738 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001739 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001740 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1741 psa_status_t status;
1742
Gilles Peskine8817f612018-12-18 00:18:46 +01001743 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001744
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001745 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001746 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001747 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001748
Gilles Peskine8817f612018-12-18 00:18:46 +01001749 PSA_ASSERT( psa_import_key( handle, key_type,
1750 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001751
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001752 status = key_agreement_with_self( &generator, handle, exercise_alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001753
Gilles Peskine01d718c2018-09-18 12:01:02 +02001754 if( policy_alg == exercise_alg &&
1755 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001756 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001757 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001758 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001759
1760exit:
1761 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001762 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001763 mbedtls_psa_crypto_free( );
1764}
1765/* END_CASE */
1766
1767/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001768void hash_operation_init( )
1769{
1770 /* Test each valid way of initializing the object, except for `= {0}`, as
1771 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1772 * though it's OK by the C standard. We could test for this, but we'd need
1773 * to supress the Clang warning for the test. */
1774 psa_hash_operation_t func = psa_hash_operation_init( );
1775 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1776 psa_hash_operation_t zero;
1777
1778 memset( &zero, 0, sizeof( zero ) );
1779
1780 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1781 * specification, we test that all valid ways of initializing the object
1782 * have the same bit pattern. This is a stronger requirement that may not
1783 * be valid on all platforms or PSA Crypto implementations, but implies the
1784 * weaker actual requirement is met: that a freshly initialized object, no
1785 * matter how it was initialized, acts the same as any other valid
1786 * initialization. */
1787 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1788 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1789}
1790/* END_CASE */
1791
1792/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001793void hash_setup( int alg_arg,
1794 int expected_status_arg )
1795{
1796 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001797 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001798 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001799 psa_status_t status;
1800
Gilles Peskine8817f612018-12-18 00:18:46 +01001801 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001802
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001803 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001804 psa_hash_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001805 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001806
1807exit:
1808 mbedtls_psa_crypto_free( );
1809}
1810/* END_CASE */
1811
1812/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02001813void hash_bad_order( )
1814{
1815 unsigned char input[] = "";
1816 /* SHA-256 hash of an empty string */
1817 unsigned char hash[] = {
1818 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1819 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1820 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
1821 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001822 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001823
Gilles Peskine8817f612018-12-18 00:18:46 +01001824 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001825
1826 /* psa_hash_update without calling psa_hash_setup beforehand */
1827 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001828 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001829 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001830
1831 /* psa_hash_verify without calling psa_hash_setup beforehand */
1832 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001833 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001834 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001835
1836 /* psa_hash_finish without calling psa_hash_setup beforehand */
1837 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001838 TEST_EQUAL( psa_hash_finish( &operation,
1839 hash, sizeof( hash ), &hash_len ),
1840 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001841
1842exit:
1843 mbedtls_psa_crypto_free( );
1844}
1845/* END_CASE */
1846
itayzafrir27e69452018-11-01 14:26:34 +02001847/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1848void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001849{
1850 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001851 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1852 * appended to it */
1853 unsigned char hash[] = {
1854 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1855 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1856 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03001857 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001858 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001859
Gilles Peskine8817f612018-12-18 00:18:46 +01001860 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001861
itayzafrir27e69452018-11-01 14:26:34 +02001862 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001863 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001864 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001865 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001866
itayzafrir27e69452018-11-01 14:26:34 +02001867 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001868 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001869 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001870 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001871
itayzafrir27e69452018-11-01 14:26:34 +02001872 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001873 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001874 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001875 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001876
itayzafrirec93d302018-10-18 18:01:10 +03001877exit:
1878 mbedtls_psa_crypto_free( );
1879}
1880/* END_CASE */
1881
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001882/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1883void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001884{
1885 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001886 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03001887 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001888 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001889 size_t hash_len;
1890
Gilles Peskine8817f612018-12-18 00:18:46 +01001891 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001892
itayzafrir58028322018-10-25 10:22:01 +03001893 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001894 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001895 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001896 hash, expected_size - 1, &hash_len ),
1897 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001898
1899exit:
1900 mbedtls_psa_crypto_free( );
1901}
1902/* END_CASE */
1903
1904/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001905void mac_operation_init( )
1906{
1907 /* Test each valid way of initializing the object, except for `= {0}`, as
1908 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1909 * though it's OK by the C standard. We could test for this, but we'd need
1910 * to supress the Clang warning for the test. */
1911 psa_mac_operation_t func = psa_mac_operation_init( );
1912 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1913 psa_mac_operation_t zero;
1914
1915 memset( &zero, 0, sizeof( zero ) );
1916
1917 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1918 * specification, we test that all valid ways of initializing the object
1919 * have the same bit pattern. This is a stronger requirement that may not
1920 * be valid on all platforms or PSA Crypto implementations, but implies the
1921 * weaker actual requirement is met: that a freshly initialized object, no
1922 * matter how it was initialized, acts the same as any other valid
1923 * initialization. */
1924 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1925 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1926}
1927/* END_CASE */
1928
1929/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001930void mac_setup( int key_type_arg,
1931 data_t *key,
1932 int alg_arg,
1933 int expected_status_arg )
1934{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001935 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001936 psa_key_type_t key_type = key_type_arg;
1937 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001938 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001939 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00001940 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001941 psa_status_t status;
1942
Gilles Peskine8817f612018-12-18 00:18:46 +01001943 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001944
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001945 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001946 psa_key_policy_set_usage( &policy,
1947 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1948 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001949 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001950
Gilles Peskine8817f612018-12-18 00:18:46 +01001951 PSA_ASSERT( psa_import_key( handle, key_type,
1952 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001953
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001954 status = psa_mac_sign_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001955 psa_mac_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001956 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001957
1958exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001959 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001960 mbedtls_psa_crypto_free( );
1961}
1962/* END_CASE */
1963
1964/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001965void mac_sign( int key_type_arg,
1966 data_t *key,
1967 int alg_arg,
1968 data_t *input,
1969 data_t *expected_mac )
1970{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001971 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001972 psa_key_type_t key_type = key_type_arg;
1973 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001974 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00001975 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001976 /* Leave a little extra room in the output buffer. At the end of the
1977 * test, we'll check that the implementation didn't overwrite onto
1978 * this extra room. */
1979 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
1980 size_t mac_buffer_size =
1981 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
1982 size_t mac_length = 0;
1983
1984 memset( actual_mac, '+', sizeof( actual_mac ) );
1985 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
1986 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
1987
Gilles Peskine8817f612018-12-18 00:18:46 +01001988 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001989
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001990 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001991 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001992 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001993
Gilles Peskine8817f612018-12-18 00:18:46 +01001994 PSA_ASSERT( psa_import_key( handle, key_type,
1995 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001996
1997 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01001998 PSA_ASSERT( psa_mac_sign_setup( &operation,
1999 handle, alg ) );
2000 PSA_ASSERT( psa_mac_update( &operation,
2001 input->x, input->len ) );
2002 PSA_ASSERT( psa_mac_sign_finish( &operation,
2003 actual_mac, mac_buffer_size,
2004 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002005
2006 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002007 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2008 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002009
2010 /* Verify that the end of the buffer is untouched. */
2011 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2012 sizeof( actual_mac ) - mac_length ) );
2013
2014exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002015 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002016 mbedtls_psa_crypto_free( );
2017}
2018/* END_CASE */
2019
2020/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002021void mac_verify( int key_type_arg,
2022 data_t *key,
2023 int alg_arg,
2024 data_t *input,
2025 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002026{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002027 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002028 psa_key_type_t key_type = key_type_arg;
2029 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002030 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002031 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002032
Gilles Peskine69c12672018-06-28 00:07:19 +02002033 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2034
Gilles Peskine8817f612018-12-18 00:18:46 +01002035 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002036
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002037 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002038 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002039 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad16036df908f2018-04-02 08:34:15 -07002040
Gilles Peskine8817f612018-12-18 00:18:46 +01002041 PSA_ASSERT( psa_import_key( handle, key_type,
2042 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002043
Gilles Peskine8817f612018-12-18 00:18:46 +01002044 PSA_ASSERT( psa_mac_verify_setup( &operation,
2045 handle, alg ) );
2046 PSA_ASSERT( psa_destroy_key( handle ) );
2047 PSA_ASSERT( psa_mac_update( &operation,
2048 input->x, input->len ) );
2049 PSA_ASSERT( psa_mac_verify_finish( &operation,
2050 expected_mac->x,
2051 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002052
2053exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002054 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002055 mbedtls_psa_crypto_free( );
2056}
2057/* END_CASE */
2058
2059/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002060void cipher_operation_init( )
2061{
2062 /* Test each valid way of initializing the object, except for `= {0}`, as
2063 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2064 * though it's OK by the C standard. We could test for this, but we'd need
2065 * to supress the Clang warning for the test. */
2066 psa_cipher_operation_t func = psa_cipher_operation_init( );
2067 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2068 psa_cipher_operation_t zero;
2069
2070 memset( &zero, 0, sizeof( zero ) );
2071
2072 /* Although not technically guaranteed by the C standard nor the PSA Crypto
2073 * specification, we test that all valid ways of initializing the object
2074 * have the same bit pattern. This is a stronger requirement that may not
2075 * be valid on all platforms or PSA Crypto implementations, but implies the
2076 * weaker actual requirement is met: that a freshly initialized object, no
2077 * matter how it was initialized, acts the same as any other valid
2078 * initialization. */
2079 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
2080 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
2081}
2082/* END_CASE */
2083
2084/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002085void cipher_setup( int key_type_arg,
2086 data_t *key,
2087 int alg_arg,
2088 int expected_status_arg )
2089{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002090 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002091 psa_key_type_t key_type = key_type_arg;
2092 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002093 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002094 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002095 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002096 psa_status_t status;
2097
Gilles Peskine8817f612018-12-18 00:18:46 +01002098 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002099
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002100 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002101 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002102 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002103
Gilles Peskine8817f612018-12-18 00:18:46 +01002104 PSA_ASSERT( psa_import_key( handle, key_type,
2105 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002106
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002107 status = psa_cipher_encrypt_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002108 psa_cipher_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002109 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002110
2111exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002112 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002113 mbedtls_psa_crypto_free( );
2114}
2115/* END_CASE */
2116
2117/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002118void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002119 data_t *key,
2120 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002121 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002122{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002123 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002124 psa_status_t status;
2125 psa_key_type_t key_type = key_type_arg;
2126 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002127 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002128 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002129 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002130 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002131 size_t output_buffer_size = 0;
2132 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002133 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002134 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002135 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002136
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002137 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2138 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002139
Gilles Peskine8817f612018-12-18 00:18:46 +01002140 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002141
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002142 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002143 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002144 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002145
Gilles Peskine8817f612018-12-18 00:18:46 +01002146 PSA_ASSERT( psa_import_key( handle, key_type,
2147 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002148
Gilles Peskine8817f612018-12-18 00:18:46 +01002149 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2150 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002151
Gilles Peskine8817f612018-12-18 00:18:46 +01002152 PSA_ASSERT( psa_cipher_set_iv( &operation,
2153 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002154 output_buffer_size = ( (size_t) input->len +
2155 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002156 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002157
Gilles Peskine8817f612018-12-18 00:18:46 +01002158 PSA_ASSERT( psa_cipher_update( &operation,
2159 input->x, input->len,
2160 output, output_buffer_size,
2161 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002162 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002163 status = psa_cipher_finish( &operation,
2164 output + function_output_length,
2165 output_buffer_size,
2166 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002167 total_output_length += function_output_length;
2168
Gilles Peskinefe11b722018-12-18 00:24:04 +01002169 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002170 if( expected_status == PSA_SUCCESS )
2171 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002172 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002173 ASSERT_COMPARE( expected_output->x, expected_output->len,
2174 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002175 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002176
Gilles Peskine50e586b2018-06-08 14:28:46 +02002177exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002178 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002179 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002180 mbedtls_psa_crypto_free( );
2181}
2182/* END_CASE */
2183
2184/* BEGIN_CASE */
2185void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002186 data_t *key,
2187 data_t *input,
2188 int first_part_size,
2189 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002190{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002191 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002192 psa_key_type_t key_type = key_type_arg;
2193 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002194 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002195 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002196 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002197 size_t output_buffer_size = 0;
2198 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002199 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002200 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002201 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002202
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002203 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2204 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002205
Gilles Peskine8817f612018-12-18 00:18:46 +01002206 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002207
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002208 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002209 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002210 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002211
Gilles Peskine8817f612018-12-18 00:18:46 +01002212 PSA_ASSERT( psa_import_key( handle, key_type,
2213 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002214
Gilles Peskine8817f612018-12-18 00:18:46 +01002215 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2216 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002217
Gilles Peskine8817f612018-12-18 00:18:46 +01002218 PSA_ASSERT( psa_cipher_set_iv( &operation,
2219 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002220 output_buffer_size = ( (size_t) input->len +
2221 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002222 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002223
Gilles Peskine4abf7412018-06-18 16:35:34 +02002224 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002225 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2226 output, output_buffer_size,
2227 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002228 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002229 PSA_ASSERT( psa_cipher_update( &operation,
2230 input->x + first_part_size,
2231 input->len - first_part_size,
2232 output, output_buffer_size,
2233 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002234 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002235 PSA_ASSERT( psa_cipher_finish( &operation,
2236 output + function_output_length,
2237 output_buffer_size,
2238 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002239 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002240 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002241
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002242 ASSERT_COMPARE( expected_output->x, expected_output->len,
2243 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002244
2245exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002246 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002247 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002248 mbedtls_psa_crypto_free( );
2249}
2250/* END_CASE */
2251
2252/* BEGIN_CASE */
2253void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002254 data_t *key,
2255 data_t *input,
2256 int first_part_size,
2257 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002258{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002259 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002260
2261 psa_key_type_t key_type = key_type_arg;
2262 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002263 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002264 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002265 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002266 size_t output_buffer_size = 0;
2267 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002268 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002269 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002270 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002271
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002272 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2273 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002274
Gilles Peskine8817f612018-12-18 00:18:46 +01002275 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002276
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002277 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002278 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002279 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002280
Gilles Peskine8817f612018-12-18 00:18:46 +01002281 PSA_ASSERT( psa_import_key( handle, key_type,
2282 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002283
Gilles Peskine8817f612018-12-18 00:18:46 +01002284 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2285 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002286
Gilles Peskine8817f612018-12-18 00:18:46 +01002287 PSA_ASSERT( psa_cipher_set_iv( &operation,
2288 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002289
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002290 output_buffer_size = ( (size_t) input->len +
2291 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002292 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002293
Gilles Peskine4abf7412018-06-18 16:35:34 +02002294 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002295 PSA_ASSERT( psa_cipher_update( &operation,
2296 input->x, first_part_size,
2297 output, output_buffer_size,
2298 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002299 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002300 PSA_ASSERT( psa_cipher_update( &operation,
2301 input->x + first_part_size,
2302 input->len - first_part_size,
2303 output, output_buffer_size,
2304 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002305 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002306 PSA_ASSERT( psa_cipher_finish( &operation,
2307 output + function_output_length,
2308 output_buffer_size,
2309 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002310 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002311 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002312
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002313 ASSERT_COMPARE( expected_output->x, expected_output->len,
2314 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002315
2316exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002317 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002318 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002319 mbedtls_psa_crypto_free( );
2320}
2321/* END_CASE */
2322
Gilles Peskine50e586b2018-06-08 14:28:46 +02002323/* BEGIN_CASE */
2324void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002325 data_t *key,
2326 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002327 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002328{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002329 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002330 psa_status_t status;
2331 psa_key_type_t key_type = key_type_arg;
2332 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002333 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002334 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002335 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002336 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002337 size_t output_buffer_size = 0;
2338 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002339 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002340 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002341 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002342
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002343 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2344 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002345
Gilles Peskine8817f612018-12-18 00:18:46 +01002346 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002347
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002348 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002349 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002350 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002351
Gilles Peskine8817f612018-12-18 00:18:46 +01002352 PSA_ASSERT( psa_import_key( handle, key_type,
2353 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002354
Gilles Peskine8817f612018-12-18 00:18:46 +01002355 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2356 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002357
Gilles Peskine8817f612018-12-18 00:18:46 +01002358 PSA_ASSERT( psa_cipher_set_iv( &operation,
2359 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002360
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002361 output_buffer_size = ( (size_t) input->len +
2362 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002363 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002364
Gilles Peskine8817f612018-12-18 00:18:46 +01002365 PSA_ASSERT( psa_cipher_update( &operation,
2366 input->x, input->len,
2367 output, output_buffer_size,
2368 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002369 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002370 status = psa_cipher_finish( &operation,
2371 output + function_output_length,
2372 output_buffer_size,
2373 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002374 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002375 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002376
2377 if( expected_status == PSA_SUCCESS )
2378 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002379 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002380 ASSERT_COMPARE( expected_output->x, expected_output->len,
2381 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002382 }
2383
Gilles Peskine50e586b2018-06-08 14:28:46 +02002384exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002385 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002386 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002387 mbedtls_psa_crypto_free( );
2388}
2389/* END_CASE */
2390
Gilles Peskine50e586b2018-06-08 14:28:46 +02002391/* BEGIN_CASE */
2392void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002393 data_t *key,
2394 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002395{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002396 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002397 psa_key_type_t key_type = key_type_arg;
2398 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002399 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002400 size_t iv_size = 16;
2401 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002402 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002403 size_t output1_size = 0;
2404 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002405 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002406 size_t output2_size = 0;
2407 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002408 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002409 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2410 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002411 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002412
Gilles Peskine8817f612018-12-18 00:18:46 +01002413 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002414
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002415 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002416 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002417 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002418
Gilles Peskine8817f612018-12-18 00:18:46 +01002419 PSA_ASSERT( psa_import_key( handle, key_type,
2420 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002421
Gilles Peskine8817f612018-12-18 00:18:46 +01002422 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2423 handle, alg ) );
2424 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2425 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002426
Gilles Peskine8817f612018-12-18 00:18:46 +01002427 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2428 iv, iv_size,
2429 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002430 output1_size = ( (size_t) input->len +
2431 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002432 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002433
Gilles Peskine8817f612018-12-18 00:18:46 +01002434 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2435 output1, output1_size,
2436 &output1_length ) );
2437 PSA_ASSERT( psa_cipher_finish( &operation1,
2438 output1 + output1_length, output1_size,
2439 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002440
Gilles Peskine048b7f02018-06-08 14:20:49 +02002441 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002442
Gilles Peskine8817f612018-12-18 00:18:46 +01002443 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002444
2445 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002446 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002447
Gilles Peskine8817f612018-12-18 00:18:46 +01002448 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2449 iv, iv_length ) );
2450 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2451 output2, output2_size,
2452 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002453 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002454 PSA_ASSERT( psa_cipher_finish( &operation2,
2455 output2 + output2_length,
2456 output2_size,
2457 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002458
Gilles Peskine048b7f02018-06-08 14:20:49 +02002459 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002460
Gilles Peskine8817f612018-12-18 00:18:46 +01002461 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002462
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002463 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002464
2465exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002466 mbedtls_free( output1 );
2467 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002468 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03002469 mbedtls_psa_crypto_free( );
2470}
2471/* END_CASE */
2472
2473/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002474void cipher_verify_output_multipart( int alg_arg,
2475 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002476 data_t *key,
2477 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002478 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002479{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002480 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002481 psa_key_type_t key_type = key_type_arg;
2482 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002483 unsigned char iv[16] = {0};
2484 size_t iv_size = 16;
2485 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002486 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002487 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002488 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002489 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002490 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002491 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002492 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002493 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2494 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002495 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002496
Gilles Peskine8817f612018-12-18 00:18:46 +01002497 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002498
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002499 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002500 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002501 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002502
Gilles Peskine8817f612018-12-18 00:18:46 +01002503 PSA_ASSERT( psa_import_key( handle, key_type,
2504 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03002505
Gilles Peskine8817f612018-12-18 00:18:46 +01002506 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2507 handle, alg ) );
2508 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2509 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002510
Gilles Peskine8817f612018-12-18 00:18:46 +01002511 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2512 iv, iv_size,
2513 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002514 output1_buffer_size = ( (size_t) input->len +
2515 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002516 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002517
Gilles Peskine4abf7412018-06-18 16:35:34 +02002518 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002519
Gilles Peskine8817f612018-12-18 00:18:46 +01002520 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2521 output1, output1_buffer_size,
2522 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002523 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002524
Gilles Peskine8817f612018-12-18 00:18:46 +01002525 PSA_ASSERT( psa_cipher_update( &operation1,
2526 input->x + first_part_size,
2527 input->len - first_part_size,
2528 output1, output1_buffer_size,
2529 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002530 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002531
Gilles Peskine8817f612018-12-18 00:18:46 +01002532 PSA_ASSERT( psa_cipher_finish( &operation1,
2533 output1 + output1_length,
2534 output1_buffer_size - output1_length,
2535 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002536 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002537
Gilles Peskine8817f612018-12-18 00:18:46 +01002538 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002539
Gilles Peskine048b7f02018-06-08 14:20:49 +02002540 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002541 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002542
Gilles Peskine8817f612018-12-18 00:18:46 +01002543 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2544 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002545
Gilles Peskine8817f612018-12-18 00:18:46 +01002546 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2547 output2, output2_buffer_size,
2548 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002549 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002550
Gilles Peskine8817f612018-12-18 00:18:46 +01002551 PSA_ASSERT( psa_cipher_update( &operation2,
2552 output1 + first_part_size,
2553 output1_length - first_part_size,
2554 output2, output2_buffer_size,
2555 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002556 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002557
Gilles Peskine8817f612018-12-18 00:18:46 +01002558 PSA_ASSERT( psa_cipher_finish( &operation2,
2559 output2 + output2_length,
2560 output2_buffer_size - output2_length,
2561 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002562 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002563
Gilles Peskine8817f612018-12-18 00:18:46 +01002564 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002565
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002566 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002567
2568exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002569 mbedtls_free( output1 );
2570 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002571 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002572 mbedtls_psa_crypto_free( );
2573}
2574/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002575
Gilles Peskine20035e32018-02-03 22:44:14 +01002576/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002577void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002578 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002579 data_t *nonce,
2580 data_t *additional_data,
2581 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002582 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002583{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002584 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002585 psa_key_type_t key_type = key_type_arg;
2586 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002587 unsigned char *output_data = NULL;
2588 size_t output_size = 0;
2589 size_t output_length = 0;
2590 unsigned char *output_data2 = NULL;
2591 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002592 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002593 psa_status_t expected_result = expected_result_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002594 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002595
Gilles Peskine4abf7412018-06-18 16:35:34 +02002596 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002597 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002598
Gilles Peskine8817f612018-12-18 00:18:46 +01002599 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002600
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002601 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002602 psa_key_policy_set_usage( &policy,
2603 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2604 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002605 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002606
Gilles Peskine8817f612018-12-18 00:18:46 +01002607 PSA_ASSERT( psa_import_key( handle, key_type,
2608 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002609
Gilles Peskinefe11b722018-12-18 00:24:04 +01002610 TEST_EQUAL( psa_aead_encrypt( handle, alg,
2611 nonce->x, nonce->len,
2612 additional_data->x,
2613 additional_data->len,
2614 input_data->x, input_data->len,
2615 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002616 &output_length ),
2617 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002618
2619 if( PSA_SUCCESS == expected_result )
2620 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002621 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002622
Gilles Peskinefe11b722018-12-18 00:24:04 +01002623 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2624 nonce->x, nonce->len,
2625 additional_data->x,
2626 additional_data->len,
2627 output_data, output_length,
2628 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002629 &output_length2 ),
2630 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002631
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002632 ASSERT_COMPARE( input_data->x, input_data->len,
2633 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002634 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002635
Gilles Peskinea1cac842018-06-11 19:33:02 +02002636exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002637 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002638 mbedtls_free( output_data );
2639 mbedtls_free( output_data2 );
2640 mbedtls_psa_crypto_free( );
2641}
2642/* END_CASE */
2643
2644/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002645void aead_encrypt( int key_type_arg, data_t *key_data,
2646 int alg_arg,
2647 data_t *nonce,
2648 data_t *additional_data,
2649 data_t *input_data,
2650 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002651{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002652 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002653 psa_key_type_t key_type = key_type_arg;
2654 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002655 unsigned char *output_data = NULL;
2656 size_t output_size = 0;
2657 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002658 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002659 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002660
Gilles Peskine4abf7412018-06-18 16:35:34 +02002661 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002662 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002663
Gilles Peskine8817f612018-12-18 00:18:46 +01002664 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002665
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002666 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002667 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002668 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002669
Gilles Peskine8817f612018-12-18 00:18:46 +01002670 PSA_ASSERT( psa_import_key( handle, key_type,
2671 key_data->x,
2672 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002673
Gilles Peskine8817f612018-12-18 00:18:46 +01002674 PSA_ASSERT( psa_aead_encrypt( handle, alg,
2675 nonce->x, nonce->len,
2676 additional_data->x, additional_data->len,
2677 input_data->x, input_data->len,
2678 output_data, output_size,
2679 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002680
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002681 ASSERT_COMPARE( expected_result->x, expected_result->len,
2682 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002683
Gilles Peskinea1cac842018-06-11 19:33:02 +02002684exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002685 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002686 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002687 mbedtls_psa_crypto_free( );
2688}
2689/* END_CASE */
2690
2691/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002692void aead_decrypt( int key_type_arg, data_t *key_data,
2693 int alg_arg,
2694 data_t *nonce,
2695 data_t *additional_data,
2696 data_t *input_data,
2697 data_t *expected_data,
2698 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002699{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002700 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002701 psa_key_type_t key_type = key_type_arg;
2702 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002703 unsigned char *output_data = NULL;
2704 size_t output_size = 0;
2705 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002706 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002707 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002708 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002709
Gilles Peskine4abf7412018-06-18 16:35:34 +02002710 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002711 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002712
Gilles Peskine8817f612018-12-18 00:18:46 +01002713 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002714
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002715 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002716 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002717 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002718
Gilles Peskine8817f612018-12-18 00:18:46 +01002719 PSA_ASSERT( psa_import_key( handle, key_type,
2720 key_data->x,
2721 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002722
Gilles Peskinefe11b722018-12-18 00:24:04 +01002723 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2724 nonce->x, nonce->len,
2725 additional_data->x,
2726 additional_data->len,
2727 input_data->x, input_data->len,
2728 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002729 &output_length ),
2730 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002731
Gilles Peskine2d277862018-06-18 15:41:12 +02002732 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002733 ASSERT_COMPARE( expected_data->x, expected_data->len,
2734 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002735
Gilles Peskinea1cac842018-06-11 19:33:02 +02002736exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002737 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002738 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002739 mbedtls_psa_crypto_free( );
2740}
2741/* END_CASE */
2742
2743/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002744void signature_size( int type_arg,
2745 int bits,
2746 int alg_arg,
2747 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002748{
2749 psa_key_type_t type = type_arg;
2750 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002751 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002752 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002753exit:
2754 ;
2755}
2756/* END_CASE */
2757
2758/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002759void sign_deterministic( int key_type_arg, data_t *key_data,
2760 int alg_arg, data_t *input_data,
2761 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002762{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002763 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01002764 psa_key_type_t key_type = key_type_arg;
2765 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002766 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002767 unsigned char *signature = NULL;
2768 size_t signature_size;
2769 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002770 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01002771
Gilles Peskine8817f612018-12-18 00:18:46 +01002772 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002773
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002774 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002775 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002776 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002777
Gilles Peskine8817f612018-12-18 00:18:46 +01002778 PSA_ASSERT( psa_import_key( handle, key_type,
2779 key_data->x,
2780 key_data->len ) );
2781 PSA_ASSERT( psa_get_key_information( handle,
2782 NULL,
2783 &key_bits ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002784
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002785 /* Allocate a buffer which has the size advertized by the
2786 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002787 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2788 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002789 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002790 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002791 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002792
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002793 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002794 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2795 input_data->x, input_data->len,
2796 signature, signature_size,
2797 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002798 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002799 ASSERT_COMPARE( output_data->x, output_data->len,
2800 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002801
2802exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002803 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01002804 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002805 mbedtls_psa_crypto_free( );
2806}
2807/* END_CASE */
2808
2809/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002810void sign_fail( int key_type_arg, data_t *key_data,
2811 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002812 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002813{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002814 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01002815 psa_key_type_t key_type = key_type_arg;
2816 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002817 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002818 psa_status_t actual_status;
2819 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002820 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002821 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002822 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01002823
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002824 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002825
Gilles Peskine8817f612018-12-18 00:18:46 +01002826 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002827
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002828 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002829 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002830 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002831
Gilles Peskine8817f612018-12-18 00:18:46 +01002832 PSA_ASSERT( psa_import_key( handle, key_type,
2833 key_data->x,
2834 key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002835
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002836 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002837 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002838 signature, signature_size,
2839 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002840 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002841 /* The value of *signature_length is unspecified on error, but
2842 * whatever it is, it should be less than signature_size, so that
2843 * if the caller tries to read *signature_length bytes without
2844 * checking the error code then they don't overflow a buffer. */
2845 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002846
2847exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002848 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01002849 mbedtls_free( signature );
2850 mbedtls_psa_crypto_free( );
2851}
2852/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002853
2854/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002855void sign_verify( int key_type_arg, data_t *key_data,
2856 int alg_arg, data_t *input_data )
2857{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002858 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02002859 psa_key_type_t key_type = key_type_arg;
2860 psa_algorithm_t alg = alg_arg;
2861 size_t key_bits;
2862 unsigned char *signature = NULL;
2863 size_t signature_size;
2864 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002865 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02002866
Gilles Peskine8817f612018-12-18 00:18:46 +01002867 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002868
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002869 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002870 psa_key_policy_set_usage( &policy,
2871 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2872 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002873 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002874
Gilles Peskine8817f612018-12-18 00:18:46 +01002875 PSA_ASSERT( psa_import_key( handle, key_type,
2876 key_data->x,
2877 key_data->len ) );
2878 PSA_ASSERT( psa_get_key_information( handle,
2879 NULL,
2880 &key_bits ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002881
2882 /* Allocate a buffer which has the size advertized by the
2883 * library. */
2884 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2885 key_bits, alg );
2886 TEST_ASSERT( signature_size != 0 );
2887 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002888 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002889
2890 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002891 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2892 input_data->x, input_data->len,
2893 signature, signature_size,
2894 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002895 /* Check that the signature length looks sensible. */
2896 TEST_ASSERT( signature_length <= signature_size );
2897 TEST_ASSERT( signature_length > 0 );
2898
2899 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002900 PSA_ASSERT( psa_asymmetric_verify(
2901 handle, alg,
2902 input_data->x, input_data->len,
2903 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002904
2905 if( input_data->len != 0 )
2906 {
2907 /* Flip a bit in the input and verify that the signature is now
2908 * detected as invalid. Flip a bit at the beginning, not at the end,
2909 * because ECDSA may ignore the last few bits of the input. */
2910 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002911 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
2912 input_data->x, input_data->len,
2913 signature, signature_length ),
2914 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02002915 }
2916
2917exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002918 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02002919 mbedtls_free( signature );
2920 mbedtls_psa_crypto_free( );
2921}
2922/* END_CASE */
2923
2924/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002925void asymmetric_verify( int key_type_arg, data_t *key_data,
2926 int alg_arg, data_t *hash_data,
2927 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002928{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002929 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03002930 psa_key_type_t key_type = key_type_arg;
2931 psa_algorithm_t alg = alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002932 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03002933
Gilles Peskine69c12672018-06-28 00:07:19 +02002934 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2935
Gilles Peskine8817f612018-12-18 00:18:46 +01002936 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03002937
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002938 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002939 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002940 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
itayzafrir5c753392018-05-08 11:18:38 +03002941
Gilles Peskine8817f612018-12-18 00:18:46 +01002942 PSA_ASSERT( psa_import_key( handle, key_type,
2943 key_data->x,
2944 key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002945
Gilles Peskine8817f612018-12-18 00:18:46 +01002946 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
2947 hash_data->x, hash_data->len,
2948 signature_data->x,
2949 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002950exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002951 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03002952 mbedtls_psa_crypto_free( );
2953}
2954/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002955
2956/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002957void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2958 int alg_arg, data_t *hash_data,
2959 data_t *signature_data,
2960 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002961{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002962 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002963 psa_key_type_t key_type = key_type_arg;
2964 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002965 psa_status_t actual_status;
2966 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002967 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002968
Gilles Peskine8817f612018-12-18 00:18:46 +01002969 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002970
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002971 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002972 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002973 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002974
Gilles Peskine8817f612018-12-18 00:18:46 +01002975 PSA_ASSERT( psa_import_key( handle, key_type,
2976 key_data->x,
2977 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002978
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002979 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002980 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02002981 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002982 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002983
Gilles Peskinefe11b722018-12-18 00:24:04 +01002984 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002985
2986exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002987 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002988 mbedtls_psa_crypto_free( );
2989}
2990/* END_CASE */
2991
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002992/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02002993void asymmetric_encrypt( int key_type_arg,
2994 data_t *key_data,
2995 int alg_arg,
2996 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02002997 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02002998 int expected_output_length_arg,
2999 int expected_status_arg )
3000{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003001 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003002 psa_key_type_t key_type = key_type_arg;
3003 psa_algorithm_t alg = alg_arg;
3004 size_t expected_output_length = expected_output_length_arg;
3005 size_t key_bits;
3006 unsigned char *output = NULL;
3007 size_t output_size;
3008 size_t output_length = ~0;
3009 psa_status_t actual_status;
3010 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003011 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003012
Gilles Peskine8817f612018-12-18 00:18:46 +01003013 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003014
Gilles Peskine656896e2018-06-29 19:12:28 +02003015 /* Import the key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003016 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003017 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003018 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
3019 PSA_ASSERT( psa_import_key( handle, key_type,
3020 key_data->x,
3021 key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003022
3023 /* Determine the maximum output length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003024 PSA_ASSERT( psa_get_key_information( handle,
3025 NULL,
3026 &key_bits ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003027 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003028 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003029
3030 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003031 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003032 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003033 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003034 output, output_size,
3035 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003036 TEST_EQUAL( actual_status, expected_status );
3037 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003038
Gilles Peskine68428122018-06-30 18:42:41 +02003039 /* If the label is empty, the test framework puts a non-null pointer
3040 * in label->x. Test that a null pointer works as well. */
3041 if( label->len == 0 )
3042 {
3043 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003044 if( output_size != 0 )
3045 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003046 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003047 input_data->x, input_data->len,
3048 NULL, label->len,
3049 output, output_size,
3050 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003051 TEST_EQUAL( actual_status, expected_status );
3052 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003053 }
3054
Gilles Peskine656896e2018-06-29 19:12:28 +02003055exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003056 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003057 mbedtls_free( output );
3058 mbedtls_psa_crypto_free( );
3059}
3060/* END_CASE */
3061
3062/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003063void asymmetric_encrypt_decrypt( int key_type_arg,
3064 data_t *key_data,
3065 int alg_arg,
3066 data_t *input_data,
3067 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003068{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003069 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003070 psa_key_type_t key_type = key_type_arg;
3071 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003072 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003073 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003074 size_t output_size;
3075 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003076 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003077 size_t output2_size;
3078 size_t output2_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003079 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003080
Gilles Peskine8817f612018-12-18 00:18:46 +01003081 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003082
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003083 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003084 psa_key_policy_set_usage( &policy,
3085 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003086 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003087 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003088
Gilles Peskine8817f612018-12-18 00:18:46 +01003089 PSA_ASSERT( psa_import_key( handle, key_type,
3090 key_data->x,
3091 key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003092
3093 /* Determine the maximum ciphertext length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003094 PSA_ASSERT( psa_get_key_information( handle,
3095 NULL,
3096 &key_bits ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003097 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003098 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003099 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003100 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003101
Gilles Peskineeebd7382018-06-08 18:11:54 +02003102 /* We test encryption by checking that encrypt-then-decrypt gives back
3103 * the original plaintext because of the non-optional random
3104 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003105 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3106 input_data->x, input_data->len,
3107 label->x, label->len,
3108 output, output_size,
3109 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003110 /* We don't know what ciphertext length to expect, but check that
3111 * it looks sensible. */
3112 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003113
Gilles Peskine8817f612018-12-18 00:18:46 +01003114 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3115 output, output_length,
3116 label->x, label->len,
3117 output2, output2_size,
3118 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003119 ASSERT_COMPARE( input_data->x, input_data->len,
3120 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003121
3122exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003123 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003124 mbedtls_free( output );
3125 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003126 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003127}
3128/* END_CASE */
3129
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003130/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003131void asymmetric_decrypt( int key_type_arg,
3132 data_t *key_data,
3133 int alg_arg,
3134 data_t *input_data,
3135 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003136 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003137{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003138 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003139 psa_key_type_t key_type = key_type_arg;
3140 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003141 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003142 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003143 size_t output_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003144 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003145
Gilles Peskine4abf7412018-06-18 16:35:34 +02003146 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003147 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003148
Gilles Peskine8817f612018-12-18 00:18:46 +01003149 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003150
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003151 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003152 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003153 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003154
Gilles Peskine8817f612018-12-18 00:18:46 +01003155 PSA_ASSERT( psa_import_key( handle, key_type,
3156 key_data->x,
3157 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003158
Gilles Peskine8817f612018-12-18 00:18:46 +01003159 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3160 input_data->x, input_data->len,
3161 label->x, label->len,
3162 output,
3163 output_size,
3164 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003165 ASSERT_COMPARE( expected_data->x, expected_data->len,
3166 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003167
Gilles Peskine68428122018-06-30 18:42:41 +02003168 /* If the label is empty, the test framework puts a non-null pointer
3169 * in label->x. Test that a null pointer works as well. */
3170 if( label->len == 0 )
3171 {
3172 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003173 if( output_size != 0 )
3174 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003175 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3176 input_data->x, input_data->len,
3177 NULL, label->len,
3178 output,
3179 output_size,
3180 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003181 ASSERT_COMPARE( expected_data->x, expected_data->len,
3182 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003183 }
3184
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003185exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003186 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003187 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003188 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003189}
3190/* END_CASE */
3191
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003192/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003193void asymmetric_decrypt_fail( int key_type_arg,
3194 data_t *key_data,
3195 int alg_arg,
3196 data_t *input_data,
3197 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003198 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003199{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003200 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003201 psa_key_type_t key_type = key_type_arg;
3202 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003203 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003204 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003205 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003206 psa_status_t actual_status;
3207 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003208 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003209
Gilles Peskine4abf7412018-06-18 16:35:34 +02003210 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003211 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003212
Gilles Peskine8817f612018-12-18 00:18:46 +01003213 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003214
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003215 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003216 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003217 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003218
Gilles Peskine8817f612018-12-18 00:18:46 +01003219 PSA_ASSERT( psa_import_key( handle, key_type,
3220 key_data->x,
3221 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003222
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003223 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003224 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003225 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003226 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003227 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003228 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003229 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003230
Gilles Peskine68428122018-06-30 18:42:41 +02003231 /* If the label is empty, the test framework puts a non-null pointer
3232 * in label->x. Test that a null pointer works as well. */
3233 if( label->len == 0 )
3234 {
3235 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003236 if( output_size != 0 )
3237 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003238 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003239 input_data->x, input_data->len,
3240 NULL, label->len,
3241 output, output_size,
3242 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003243 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003244 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003245 }
3246
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003247exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003248 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02003249 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003250 mbedtls_psa_crypto_free( );
3251}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003252/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003253
3254/* BEGIN_CASE */
Jaeden Amerod94d6712019-01-04 14:11:48 +00003255void crypto_generator_init( )
3256{
3257 /* Test each valid way of initializing the object, except for `= {0}`, as
3258 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3259 * though it's OK by the C standard. We could test for this, but we'd need
3260 * to supress the Clang warning for the test. */
3261 psa_crypto_generator_t func = psa_crypto_generator_init( );
3262 psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT;
3263 psa_crypto_generator_t zero;
3264
3265 memset( &zero, 0, sizeof( zero ) );
3266
3267 /* Although not technically guaranteed by the C standard nor the PSA Crypto
3268 * specification, we test that all valid ways of initializing the object
3269 * have the same bit pattern. This is a stronger requirement that may not
3270 * be valid on all platforms or PSA Crypto implementations, but implies the
3271 * weaker actual requirement is met: that a freshly initialized object, no
3272 * matter how it was initialized, acts the same as any other valid
3273 * initialization. */
3274 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
3275 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
3276}
3277/* END_CASE */
3278
3279/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003280void derive_setup( int key_type_arg,
3281 data_t *key_data,
3282 int alg_arg,
3283 data_t *salt,
3284 data_t *label,
3285 int requested_capacity_arg,
3286 int expected_status_arg )
3287{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003288 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003289 size_t key_type = key_type_arg;
3290 psa_algorithm_t alg = alg_arg;
3291 size_t requested_capacity = requested_capacity_arg;
3292 psa_status_t expected_status = expected_status_arg;
3293 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003294 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003295
Gilles Peskine8817f612018-12-18 00:18:46 +01003296 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003297
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003298 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003299 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003300 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003301
Gilles Peskine8817f612018-12-18 00:18:46 +01003302 PSA_ASSERT( psa_import_key( handle, key_type,
3303 key_data->x,
3304 key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003305
Gilles Peskinefe11b722018-12-18 00:24:04 +01003306 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3307 salt->x, salt->len,
3308 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003309 requested_capacity ),
3310 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003311
3312exit:
3313 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003314 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003315 mbedtls_psa_crypto_free( );
3316}
3317/* END_CASE */
3318
3319/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003320void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003321{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003322 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003323 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003324 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003325 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003326 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003327 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003328 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3329 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3330 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Jaeden Amero70261c52019-01-04 11:47:20 +00003331 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003332
Gilles Peskine8817f612018-12-18 00:18:46 +01003333 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003334
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003335 PSA_ASSERT( psa_allocate_key( &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003336 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003337 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003338
Gilles Peskine8817f612018-12-18 00:18:46 +01003339 PSA_ASSERT( psa_import_key( handle, key_type,
3340 key_data,
3341 sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003342
3343 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01003344 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3345 NULL, 0,
3346 NULL, 0,
3347 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003348
3349 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003350 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3351 NULL, 0,
3352 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003353 capacity ),
3354 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003355
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003356 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003357
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003358 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
3359 PSA_ERROR_INSUFFICIENT_CAPACITY );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003360
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003361exit:
3362 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003363 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003364 mbedtls_psa_crypto_free( );
3365}
3366/* END_CASE */
3367
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003368/* BEGIN_CASE */
3369void test_derive_invalid_generator_tests( )
3370{
3371 uint8_t output_buffer[16];
3372 size_t buffer_size = 16;
3373 size_t capacity = 0;
3374 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3375
Nir Sonnenschein50789302018-10-31 12:16:38 +02003376 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003377 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003378
3379 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003380 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003381
Gilles Peskine8817f612018-12-18 00:18:46 +01003382 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003383
Nir Sonnenschein50789302018-10-31 12:16:38 +02003384 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003385 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003386
Nir Sonnenschein50789302018-10-31 12:16:38 +02003387 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003388 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003389
3390exit:
3391 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003392}
3393/* END_CASE */
3394
3395/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003396void derive_output( int alg_arg,
3397 data_t *key_data,
3398 data_t *salt,
3399 data_t *label,
3400 int requested_capacity_arg,
3401 data_t *expected_output1,
3402 data_t *expected_output2 )
3403{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003404 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003405 psa_algorithm_t alg = alg_arg;
3406 size_t requested_capacity = requested_capacity_arg;
3407 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3408 uint8_t *expected_outputs[2] =
3409 {expected_output1->x, expected_output2->x};
3410 size_t output_sizes[2] =
3411 {expected_output1->len, expected_output2->len};
3412 size_t output_buffer_size = 0;
3413 uint8_t *output_buffer = NULL;
3414 size_t expected_capacity;
3415 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003416 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003417 psa_status_t status;
3418 unsigned i;
3419
3420 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3421 {
3422 if( output_sizes[i] > output_buffer_size )
3423 output_buffer_size = output_sizes[i];
3424 if( output_sizes[i] == 0 )
3425 expected_outputs[i] = NULL;
3426 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003427 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003428 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003429
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003430 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003431 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003432 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003433
Gilles Peskine8817f612018-12-18 00:18:46 +01003434 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3435 key_data->x,
3436 key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003437
3438 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003439 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3440 salt->x, salt->len,
3441 label->x, label->len,
3442 requested_capacity ) );
3443 PSA_ASSERT( psa_get_generator_capacity( &generator,
3444 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003445 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003446 expected_capacity = requested_capacity;
3447
3448 /* Expansion phase. */
3449 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3450 {
3451 /* Read some bytes. */
3452 status = psa_generator_read( &generator,
3453 output_buffer, output_sizes[i] );
3454 if( expected_capacity == 0 && output_sizes[i] == 0 )
3455 {
3456 /* Reading 0 bytes when 0 bytes are available can go either way. */
3457 TEST_ASSERT( status == PSA_SUCCESS ||
3458 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3459 continue;
3460 }
3461 else if( expected_capacity == 0 ||
3462 output_sizes[i] > expected_capacity )
3463 {
3464 /* Capacity exceeded. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003465 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003466 expected_capacity = 0;
3467 continue;
3468 }
3469 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003470 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003471 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003472 ASSERT_COMPARE( output_buffer, output_sizes[i],
3473 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003474 /* Check the generator status. */
3475 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01003476 PSA_ASSERT( psa_get_generator_capacity( &generator,
3477 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003478 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003479 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003480 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003481
3482exit:
3483 mbedtls_free( output_buffer );
3484 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003485 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003486 mbedtls_psa_crypto_free( );
3487}
3488/* END_CASE */
3489
3490/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003491void derive_full( int alg_arg,
3492 data_t *key_data,
3493 data_t *salt,
3494 data_t *label,
3495 int requested_capacity_arg )
3496{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003497 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02003498 psa_algorithm_t alg = alg_arg;
3499 size_t requested_capacity = requested_capacity_arg;
3500 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3501 unsigned char output_buffer[16];
3502 size_t expected_capacity = requested_capacity;
3503 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003504 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02003505
Gilles Peskine8817f612018-12-18 00:18:46 +01003506 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003507
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003508 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003509 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003510 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003511
Gilles Peskine8817f612018-12-18 00:18:46 +01003512 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3513 key_data->x,
3514 key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003515
3516 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003517 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3518 salt->x, salt->len,
3519 label->x, label->len,
3520 requested_capacity ) );
3521 PSA_ASSERT( psa_get_generator_capacity( &generator,
3522 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003523 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003524
3525 /* Expansion phase. */
3526 while( current_capacity > 0 )
3527 {
3528 size_t read_size = sizeof( output_buffer );
3529 if( read_size > current_capacity )
3530 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01003531 PSA_ASSERT( psa_generator_read( &generator,
3532 output_buffer,
3533 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003534 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01003535 PSA_ASSERT( psa_get_generator_capacity( &generator,
3536 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003537 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003538 }
3539
3540 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003541 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
3542 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskined54931c2018-07-17 21:06:59 +02003543
Gilles Peskine8817f612018-12-18 00:18:46 +01003544 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003545
3546exit:
3547 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003548 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02003549 mbedtls_psa_crypto_free( );
3550}
3551/* END_CASE */
3552
3553/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003554void derive_key_exercise( int alg_arg,
3555 data_t *key_data,
3556 data_t *salt,
3557 data_t *label,
3558 int derived_type_arg,
3559 int derived_bits_arg,
3560 int derived_usage_arg,
3561 int derived_alg_arg )
3562{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003563 psa_key_handle_t base_handle = 0;
3564 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003565 psa_algorithm_t alg = alg_arg;
3566 psa_key_type_t derived_type = derived_type_arg;
3567 size_t derived_bits = derived_bits_arg;
3568 psa_key_usage_t derived_usage = derived_usage_arg;
3569 psa_algorithm_t derived_alg = derived_alg_arg;
3570 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3571 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003572 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003573 psa_key_type_t got_type;
3574 size_t got_bits;
3575
Gilles Peskine8817f612018-12-18 00:18:46 +01003576 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003577
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003578 PSA_ASSERT( psa_allocate_key( &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003579 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003580 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3581 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3582 key_data->x,
3583 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003584
3585 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003586 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3587 salt->x, salt->len,
3588 label->x, label->len,
3589 capacity ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003590 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003591 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003592 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3593 PSA_ASSERT( psa_generator_import_key( derived_handle,
3594 derived_type,
3595 derived_bits,
3596 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003597
3598 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01003599 PSA_ASSERT( psa_get_key_information( derived_handle,
3600 &got_type,
3601 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003602 TEST_EQUAL( got_type, derived_type );
3603 TEST_EQUAL( got_bits, derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003604
3605 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003606 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02003607 goto exit;
3608
3609exit:
3610 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003611 psa_destroy_key( base_handle );
3612 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003613 mbedtls_psa_crypto_free( );
3614}
3615/* END_CASE */
3616
3617/* BEGIN_CASE */
3618void derive_key_export( int alg_arg,
3619 data_t *key_data,
3620 data_t *salt,
3621 data_t *label,
3622 int bytes1_arg,
3623 int bytes2_arg )
3624{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003625 psa_key_handle_t base_handle = 0;
3626 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003627 psa_algorithm_t alg = alg_arg;
3628 size_t bytes1 = bytes1_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003629 size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003630 size_t bytes2 = bytes2_arg;
3631 size_t capacity = bytes1 + bytes2;
3632 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003633 uint8_t *output_buffer = NULL;
3634 uint8_t *export_buffer = NULL;
Jaeden Amero70261c52019-01-04 11:47:20 +00003635 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003636 size_t length;
3637
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003638 ASSERT_ALLOC( output_buffer, capacity );
3639 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01003640 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003641
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003642 PSA_ASSERT( psa_allocate_key( &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003643 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003644 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3645 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3646 key_data->x,
3647 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003648
3649 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003650 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3651 salt->x, salt->len,
3652 label->x, label->len,
3653 capacity ) );
3654 PSA_ASSERT( psa_generator_read( &generator,
3655 output_buffer,
3656 capacity ) );
3657 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003658
3659 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003660 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3661 salt->x, salt->len,
3662 label->x, label->len,
3663 capacity ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003664 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003665 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003666 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3667 PSA_ASSERT( psa_generator_import_key( derived_handle,
3668 PSA_KEY_TYPE_RAW_DATA,
3669 derived_bits,
3670 &generator ) );
3671 PSA_ASSERT( psa_export_key( derived_handle,
3672 export_buffer, bytes1,
3673 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003674 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003675 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003676 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003677 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3678 PSA_ASSERT( psa_generator_import_key( derived_handle,
3679 PSA_KEY_TYPE_RAW_DATA,
3680 PSA_BYTES_TO_BITS( bytes2 ),
3681 &generator ) );
3682 PSA_ASSERT( psa_export_key( derived_handle,
3683 export_buffer + bytes1, bytes2,
3684 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003685 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003686
3687 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003688 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
3689 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003690
3691exit:
3692 mbedtls_free( output_buffer );
3693 mbedtls_free( export_buffer );
3694 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003695 psa_destroy_key( base_handle );
3696 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003697 mbedtls_psa_crypto_free( );
3698}
3699/* END_CASE */
3700
3701/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02003702void key_agreement_setup( int alg_arg,
3703 int our_key_type_arg, data_t *our_key_data,
3704 data_t *peer_key_data,
3705 int expected_status_arg )
3706{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003707 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003708 psa_algorithm_t alg = alg_arg;
3709 psa_key_type_t our_key_type = our_key_type_arg;
3710 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003711 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003712
Gilles Peskine8817f612018-12-18 00:18:46 +01003713 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003714
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003715 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003716 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003717 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3718 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3719 our_key_data->x,
3720 our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003721
Gilles Peskinefe11b722018-12-18 00:24:04 +01003722 TEST_EQUAL( psa_key_agreement( &generator,
3723 our_key,
3724 peer_key_data->x, peer_key_data->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003725 alg ),
3726 expected_status_arg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003727
3728exit:
3729 psa_generator_abort( &generator );
3730 psa_destroy_key( our_key );
3731 mbedtls_psa_crypto_free( );
3732}
3733/* END_CASE */
3734
3735/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02003736void key_agreement_capacity( int alg_arg,
3737 int our_key_type_arg, data_t *our_key_data,
3738 data_t *peer_key_data,
3739 int expected_capacity_arg )
3740{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003741 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003742 psa_algorithm_t alg = alg_arg;
3743 psa_key_type_t our_key_type = our_key_type_arg;
3744 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003745 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02003746 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02003747 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02003748
Gilles Peskine8817f612018-12-18 00:18:46 +01003749 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003750
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003751 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003752 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003753 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3754 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3755 our_key_data->x,
3756 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003757
Gilles Peskine8817f612018-12-18 00:18:46 +01003758 PSA_ASSERT( psa_key_agreement( &generator,
3759 our_key,
3760 peer_key_data->x, peer_key_data->len,
3761 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003762
Gilles Peskinebf491972018-10-25 22:36:12 +02003763 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003764 PSA_ASSERT( psa_get_generator_capacity(
3765 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003766 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02003767
Gilles Peskinebf491972018-10-25 22:36:12 +02003768 /* Test the actual capacity by reading the output. */
3769 while( actual_capacity > sizeof( output ) )
3770 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003771 PSA_ASSERT( psa_generator_read( &generator,
3772 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02003773 actual_capacity -= sizeof( output );
3774 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003775 PSA_ASSERT( psa_generator_read( &generator,
3776 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003777 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
3778 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskinebf491972018-10-25 22:36:12 +02003779
Gilles Peskine59685592018-09-18 12:11:34 +02003780exit:
3781 psa_generator_abort( &generator );
3782 psa_destroy_key( our_key );
3783 mbedtls_psa_crypto_free( );
3784}
3785/* END_CASE */
3786
3787/* BEGIN_CASE */
3788void key_agreement_output( int alg_arg,
3789 int our_key_type_arg, data_t *our_key_data,
3790 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003791 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02003792{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003793 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003794 psa_algorithm_t alg = alg_arg;
3795 psa_key_type_t our_key_type = our_key_type_arg;
3796 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003797 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003798 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02003799
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003800 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
3801 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003802
Gilles Peskine8817f612018-12-18 00:18:46 +01003803 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003804
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003805 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003806 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003807 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3808 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3809 our_key_data->x,
3810 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003811
Gilles Peskine8817f612018-12-18 00:18:46 +01003812 PSA_ASSERT( psa_key_agreement( &generator,
3813 our_key,
3814 peer_key_data->x, peer_key_data->len,
3815 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003816
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003817 PSA_ASSERT( psa_generator_read( &generator,
3818 actual_output,
3819 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003820 ASSERT_COMPARE( actual_output, expected_output1->len,
3821 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003822 if( expected_output2->len != 0 )
3823 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003824 PSA_ASSERT( psa_generator_read( &generator,
3825 actual_output,
3826 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003827 ASSERT_COMPARE( actual_output, expected_output2->len,
3828 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003829 }
Gilles Peskine59685592018-09-18 12:11:34 +02003830
3831exit:
3832 psa_generator_abort( &generator );
3833 psa_destroy_key( our_key );
3834 mbedtls_psa_crypto_free( );
3835 mbedtls_free( actual_output );
3836}
3837/* END_CASE */
3838
3839/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003840void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003841{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003842 size_t bytes = bytes_arg;
3843 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003844 unsigned char *output = NULL;
3845 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003846 size_t i;
3847 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003848
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003849 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3850 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003851 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003852
Gilles Peskine8817f612018-12-18 00:18:46 +01003853 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003854
Gilles Peskinea50d7392018-06-21 10:22:13 +02003855 /* Run several times, to ensure that every output byte will be
3856 * nonzero at least once with overwhelming probability
3857 * (2^(-8*number_of_runs)). */
3858 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003859 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003860 if( bytes != 0 )
3861 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01003862 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003863
3864 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003865 ASSERT_COMPARE( output + bytes, sizeof( trail ),
3866 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003867
3868 for( i = 0; i < bytes; i++ )
3869 {
3870 if( output[i] != 0 )
3871 ++changed[i];
3872 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003873 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003874
3875 /* Check that every byte was changed to nonzero at least once. This
3876 * validates that psa_generate_random is overwriting every byte of
3877 * the output buffer. */
3878 for( i = 0; i < bytes; i++ )
3879 {
3880 TEST_ASSERT( changed[i] != 0 );
3881 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003882
3883exit:
3884 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003885 mbedtls_free( output );
3886 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003887}
3888/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003889
3890/* BEGIN_CASE */
3891void generate_key( int type_arg,
3892 int bits_arg,
3893 int usage_arg,
3894 int alg_arg,
3895 int expected_status_arg )
3896{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003897 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003898 psa_key_type_t type = type_arg;
3899 psa_key_usage_t usage = usage_arg;
3900 size_t bits = bits_arg;
3901 psa_algorithm_t alg = alg_arg;
3902 psa_status_t expected_status = expected_status_arg;
3903 psa_key_type_t got_type;
3904 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003905 psa_status_t expected_info_status =
3906 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003907 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003908
Gilles Peskine8817f612018-12-18 00:18:46 +01003909 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003910
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003911 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003912 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003913 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003914
3915 /* Generate a key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003916 TEST_EQUAL( psa_generate_key( handle, type, bits, NULL, 0 ),
3917 expected_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003918
3919 /* Test the key information */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003920 TEST_EQUAL( psa_get_key_information( handle, &got_type, &got_bits ),
3921 expected_info_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003922 if( expected_info_status != PSA_SUCCESS )
3923 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003924 TEST_EQUAL( got_type, type );
3925 TEST_EQUAL( got_bits, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003926
Gilles Peskine818ca122018-06-20 18:16:48 +02003927 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003928 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02003929 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003930
3931exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003932 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003933 mbedtls_psa_crypto_free( );
3934}
3935/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03003936
Darryl Greend49a4992018-06-18 17:27:26 +01003937/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
3938void persistent_key_load_key_from_storage( data_t *data, int type_arg,
3939 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00003940 int alg_arg, int generation_method,
3941 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01003942{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003943 psa_key_handle_t handle = 0;
3944 psa_key_handle_t base_key;
Darryl Greend49a4992018-06-18 17:27:26 +01003945 psa_key_type_t type = (psa_key_type_t) type_arg;
3946 psa_key_type_t type_get;
3947 size_t bits_get;
Jaeden Amero70261c52019-01-04 11:47:20 +00003948 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
3949 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01003950 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
3951 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003952 psa_key_policy_t base_policy_set = PSA_KEY_POLICY_INIT;
Darryl Green0c6575a2018-11-07 16:05:30 +00003953 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
3954 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01003955 unsigned char *first_export = NULL;
3956 unsigned char *second_export = NULL;
3957 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
3958 size_t first_exported_length;
3959 size_t second_exported_length;
3960
3961 ASSERT_ALLOC( first_export, export_size );
3962 ASSERT_ALLOC( second_export, export_size );
3963
Gilles Peskine8817f612018-12-18 00:18:46 +01003964 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01003965
Gilles Peskine8817f612018-12-18 00:18:46 +01003966 PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
Gilles Peskine8817f612018-12-18 00:18:46 +01003967 &handle ) );
Darryl Greend49a4992018-06-18 17:27:26 +01003968 psa_key_policy_set_usage( &policy_set, policy_usage,
3969 policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003970 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Darryl Greend49a4992018-06-18 17:27:26 +01003971
Darryl Green0c6575a2018-11-07 16:05:30 +00003972 switch( generation_method )
3973 {
3974 case IMPORT_KEY:
3975 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01003976 PSA_ASSERT( psa_import_key( handle, type,
3977 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00003978 break;
Darryl Greend49a4992018-06-18 17:27:26 +01003979
Darryl Green0c6575a2018-11-07 16:05:30 +00003980 case GENERATE_KEY:
3981 /* Generate a key */
Gilles Peskine8817f612018-12-18 00:18:46 +01003982 PSA_ASSERT( psa_generate_key( handle, type, bits,
3983 NULL, 0 ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00003984 break;
3985
3986 case DERIVE_KEY:
3987 /* Create base key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003988 PSA_ASSERT( psa_allocate_key( &base_key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00003989 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
3990 base_policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003991 PSA_ASSERT( psa_set_key_policy(
3992 base_key, &base_policy_set ) );
3993 PSA_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
3994 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00003995 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003996 PSA_ASSERT( psa_key_derivation( &generator, base_key,
3997 base_policy_alg,
3998 NULL, 0, NULL, 0,
3999 export_size ) );
4000 PSA_ASSERT( psa_generator_import_key(
4001 handle, PSA_KEY_TYPE_RAW_DATA,
4002 bits, &generator ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004003 break;
4004 }
Darryl Greend49a4992018-06-18 17:27:26 +01004005
4006 /* Export the key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004007 TEST_EQUAL( psa_export_key( handle,
4008 first_export, export_size,
4009 &first_exported_length ),
4010 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004011
4012 /* Shutdown and restart */
4013 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004014 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004015
Darryl Greend49a4992018-06-18 17:27:26 +01004016 /* Check key slot still contains key data */
Gilles Peskine8817f612018-12-18 00:18:46 +01004017 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4018 &handle ) );
4019 PSA_ASSERT( psa_get_key_information(
4020 handle, &type_get, &bits_get ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004021 TEST_EQUAL( type_get, type );
4022 TEST_EQUAL( bits_get, (size_t) bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004023
Gilles Peskine8817f612018-12-18 00:18:46 +01004024 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004025 TEST_EQUAL( psa_key_policy_get_usage( &policy_get ), policy_usage );
4026 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_get ), policy_alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004027
4028 /* Export the key again */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004029 TEST_EQUAL( psa_export_key( handle,
4030 second_export, export_size,
4031 &second_exported_length ),
4032 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004033
Darryl Green0c6575a2018-11-07 16:05:30 +00004034 if( export_status == PSA_SUCCESS )
4035 {
4036 ASSERT_COMPARE( first_export, first_exported_length,
4037 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01004038
Darryl Green0c6575a2018-11-07 16:05:30 +00004039 switch( generation_method )
4040 {
4041 case IMPORT_KEY:
4042 ASSERT_COMPARE( data->x, data->len,
4043 first_export, first_exported_length );
4044 break;
4045 default:
4046 break;
4047 }
4048 }
4049
4050 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004051 if( ! exercise_key( handle, policy_usage, policy_alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004052 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004053
4054exit:
4055 mbedtls_free( first_export );
4056 mbedtls_free( second_export );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004057 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004058 mbedtls_psa_crypto_free();
4059}
4060/* END_CASE */