blob: 2a49141da850e1d8205f409c1f7cb145e9e9d05c [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;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200628#if defined(MBEDTLS_RSA_C)
629 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
630 {
631 /* RSAPublicKey ::= SEQUENCE {
632 * modulus INTEGER, -- n
633 * publicExponent INTEGER } -- e
634 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100635 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
636 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100637 MBEDTLS_ASN1_CONSTRUCTED ),
638 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100639 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200640 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
641 goto exit;
642 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
643 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100644 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200645 }
646 else
647#endif /* MBEDTLS_RSA_C */
648#if defined(MBEDTLS_ECP_C)
649 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
650 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000651 /* The representation of an ECC public key is:
652 * - The byte 0x04;
653 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
654 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
655 * - where m is the bit size associated with the curve.
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200656 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100657 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
658 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200659 }
660 else
661#endif /* MBEDTLS_ECP_C */
662 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100663 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200664 mbedtls_snprintf( message, sizeof( message ),
665 "No sanity check for public key type=0x%08lx",
666 (unsigned long) type );
667 test_fail( message, __LINE__, __FILE__ );
668 return( 0 );
669 }
670 }
671 else
672
673 {
674 /* No sanity checks for other types */
675 }
676
677 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200678
679exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200680 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200681}
682
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100683static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200684 psa_key_usage_t usage )
685{
686 psa_key_type_t type;
687 size_t bits;
688 uint8_t *exported = NULL;
689 size_t exported_size = 0;
690 size_t exported_length = 0;
691 int ok = 0;
692
Gilles Peskine8817f612018-12-18 00:18:46 +0100693 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200694
695 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
696 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200697 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100698 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
699 PSA_ERROR_NOT_PERMITTED );
Gilles Peskined14664a2018-08-10 19:07:32 +0200700 return( 1 );
701 }
702
Gilles Peskined14664a2018-08-10 19:07:32 +0200703 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200704 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200705
Gilles Peskine8817f612018-12-18 00:18:46 +0100706 PSA_ASSERT( psa_export_key( handle,
707 exported, exported_size,
708 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200709 ok = exported_key_sanity_check( type, bits, exported, exported_length );
710
711exit:
712 mbedtls_free( exported );
713 return( ok );
714}
715
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100716static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200717{
718 psa_key_type_t type;
719 psa_key_type_t public_type;
720 size_t bits;
721 uint8_t *exported = NULL;
722 size_t exported_size = 0;
723 size_t exported_length = 0;
724 int ok = 0;
725
Gilles Peskine8817f612018-12-18 00:18:46 +0100726 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200727 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
728 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100729 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100730 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200731 return( 1 );
732 }
733
734 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
735 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200736 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200737
Gilles Peskine8817f612018-12-18 00:18:46 +0100738 PSA_ASSERT( psa_export_public_key( handle,
739 exported, exported_size,
740 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200741 ok = exported_key_sanity_check( public_type, bits,
742 exported, exported_length );
743
744exit:
745 mbedtls_free( exported );
746 return( ok );
747}
748
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100749static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +0200750 psa_key_usage_t usage,
751 psa_algorithm_t alg )
752{
753 int ok;
754 if( alg == 0 )
755 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
756 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100757 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200758 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100759 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200760 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100761 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200762 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100763 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200764 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100765 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200766 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100767 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200768 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100769 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200770 else
771 {
772 char message[40];
773 mbedtls_snprintf( message, sizeof( message ),
774 "No code to exercise alg=0x%08lx",
775 (unsigned long) alg );
776 test_fail( message, __LINE__, __FILE__ );
777 ok = 0;
778 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200779
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100780 ok = ok && exercise_export_key( handle, usage );
781 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +0200782
Gilles Peskine02b75072018-07-01 22:31:34 +0200783 return( ok );
784}
785
Gilles Peskine10df3412018-10-25 22:35:43 +0200786static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
787 psa_algorithm_t alg )
788{
789 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
790 {
791 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
792 PSA_KEY_USAGE_VERIFY :
793 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
794 }
795 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
796 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
797 {
798 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
799 PSA_KEY_USAGE_ENCRYPT :
800 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
801 }
802 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
803 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
804 {
805 return( PSA_KEY_USAGE_DERIVE );
806 }
807 else
808 {
809 return( 0 );
810 }
811
812}
Darryl Green0c6575a2018-11-07 16:05:30 +0000813
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100814/* An overapproximation of the amount of storage needed for a key of the
815 * given type and with the given content. The API doesn't make it easy
816 * to find a good value for the size. The current implementation doesn't
817 * care about the value anyway. */
818#define KEY_BITS_FROM_DATA( type, data ) \
819 ( data )->len
820
Darryl Green0c6575a2018-11-07 16:05:30 +0000821typedef enum {
822 IMPORT_KEY = 0,
823 GENERATE_KEY = 1,
824 DERIVE_KEY = 2
825} generate_method;
826
Gilles Peskinee59236f2018-01-27 23:32:46 +0100827/* END_HEADER */
828
829/* BEGIN_DEPENDENCIES
830 * depends_on:MBEDTLS_PSA_CRYPTO_C
831 * END_DEPENDENCIES
832 */
833
834/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200835void static_checks( )
836{
837 size_t max_truncated_mac_size =
838 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
839
840 /* Check that the length for a truncated MAC always fits in the algorithm
841 * encoding. The shifted mask is the maximum truncated value. The
842 * untruncated algorithm may be one byte larger. */
843 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
844}
845/* END_CASE */
846
847/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200848void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100849{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100850 psa_key_handle_t handle = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200851 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100852 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100853
Gilles Peskine8817f612018-12-18 00:18:46 +0100854 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100855
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100856 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100857 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100858 TEST_EQUAL( status, expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100859 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100860 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100861
862exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100863 mbedtls_psa_crypto_free( );
864}
865/* END_CASE */
866
867/* BEGIN_CASE */
Gilles Peskinea4261682018-12-03 11:34:01 +0100868void import_twice( int alg_arg, int usage_arg,
869 int type1_arg, data_t *data1,
870 int expected_import1_status_arg,
871 int type2_arg, data_t *data2,
872 int expected_import2_status_arg )
873{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100874 psa_key_handle_t handle = 0;
Gilles Peskinea4261682018-12-03 11:34:01 +0100875 psa_algorithm_t alg = alg_arg;
876 psa_key_usage_t usage = usage_arg;
877 psa_key_type_t type1 = type1_arg;
878 psa_status_t expected_import1_status = expected_import1_status_arg;
879 psa_key_type_t type2 = type2_arg;
880 psa_status_t expected_import2_status = expected_import2_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +0000881 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea4261682018-12-03 11:34:01 +0100882 psa_status_t status;
883
Gilles Peskine8817f612018-12-18 00:18:46 +0100884 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100885
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100886 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100887 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100888 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100889
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100890 status = psa_import_key( handle, type1, data1->x, data1->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100891 TEST_EQUAL( status, expected_import1_status );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100892 status = psa_import_key( handle, type2, data2->x, data2->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100893 TEST_EQUAL( status, expected_import2_status );
Gilles Peskinea4261682018-12-03 11:34:01 +0100894
895 if( expected_import1_status == PSA_SUCCESS ||
896 expected_import2_status == PSA_SUCCESS )
897 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100898 TEST_ASSERT( exercise_key( handle, usage, alg ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100899 }
900
901exit:
902 mbedtls_psa_crypto_free( );
903}
904/* END_CASE */
905
906/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200907void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
908{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100909 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200910 size_t bits = bits_arg;
911 psa_status_t expected_status = expected_status_arg;
912 psa_status_t status;
913 psa_key_type_t type =
914 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
915 size_t buffer_size = /* Slight overapproximations */
916 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200917 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200918 unsigned char *p;
919 int ret;
920 size_t length;
921
Gilles Peskine8817f612018-12-18 00:18:46 +0100922 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200923 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200924
925 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
926 bits, keypair ) ) >= 0 );
927 length = ret;
928
929 /* Try importing the key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100930 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100931 status = psa_import_key( handle, type, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100932 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200933 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100934 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200935
936exit:
937 mbedtls_free( buffer );
938 mbedtls_psa_crypto_free( );
939}
940/* END_CASE */
941
942/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300943void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300944 int type_arg,
945 int alg_arg,
946 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100947 int expected_bits,
948 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200949 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100950 int canonical_input )
951{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100952 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100953 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200954 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200955 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100956 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100957 unsigned char *exported = NULL;
958 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100959 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100960 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100961 size_t reexported_length;
962 psa_key_type_t got_type;
963 size_t got_bits;
Jaeden Amero70261c52019-01-04 11:47:20 +0000964 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100965
Moran Pekercb088e72018-07-17 17:36:59 +0300966 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200967 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100968 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200969 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100970 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100971
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100972 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +0200973 psa_key_policy_set_usage( &policy, usage_arg, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100974 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100975
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100976 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
977 PSA_ERROR_EMPTY_SLOT );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700978
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100979 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +0100980 PSA_ASSERT( psa_import_key( handle, type,
981 data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100982
983 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +0100984 PSA_ASSERT( psa_get_key_information( handle,
985 &got_type,
986 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100987 TEST_EQUAL( got_type, type );
988 TEST_EQUAL( got_bits, (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100989
990 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100991 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100992 exported, export_size,
993 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100994 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100995
996 /* The exported length must be set by psa_export_key() to a value between 0
997 * and export_size. On errors, the exported length must be 0. */
998 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
999 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1000 TEST_ASSERT( exported_length <= export_size );
1001
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001002 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001003 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001004 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001005 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001006 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001007 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001008 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001009
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001010 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001011 goto exit;
1012
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001013 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001014 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001015 else
1016 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001017 psa_key_handle_t handle2;
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001018 PSA_ASSERT( psa_allocate_key( &handle2 ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001019 PSA_ASSERT( psa_set_key_policy( handle2, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001020
Gilles Peskine8817f612018-12-18 00:18:46 +01001021 PSA_ASSERT( psa_import_key( handle2, type,
1022 exported,
1023 exported_length ) );
1024 PSA_ASSERT( psa_export_key( handle2,
1025 reexported,
1026 export_size,
1027 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001028 ASSERT_COMPARE( exported, exported_length,
1029 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001030 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001031 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001032 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001033
1034destroy:
1035 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001036 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001037 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1038 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001039
1040exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001041 mbedtls_free( exported );
1042 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001043 mbedtls_psa_crypto_free( );
1044}
1045/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001046
Moran Pekerf709f4a2018-06-06 17:26:04 +03001047/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001048void import_key_nonempty_slot( )
1049{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001050 psa_key_handle_t handle = 0;
Moran Peker28a38e62018-11-07 16:18:24 +02001051 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1052 psa_status_t status;
1053 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
Gilles Peskine8817f612018-12-18 00:18:46 +01001054 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001055
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001056 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001057
Moran Peker28a38e62018-11-07 16:18:24 +02001058 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001059 PSA_ASSERT( psa_import_key( handle, type,
1060 data, sizeof( data ) ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001061
1062 /* Import the key again */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001063 status = psa_import_key( handle, type, data, sizeof( data ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001064 TEST_EQUAL( status, PSA_ERROR_OCCUPIED_SLOT );
Moran Peker28a38e62018-11-07 16:18:24 +02001065
1066exit:
1067 mbedtls_psa_crypto_free( );
1068}
1069/* END_CASE */
1070
1071/* BEGIN_CASE */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001072void export_invalid_handle( int handle, int expected_export_status_arg )
Moran Peker28a38e62018-11-07 16:18:24 +02001073{
1074 psa_status_t status;
1075 unsigned char *exported = NULL;
1076 size_t export_size = 0;
1077 size_t exported_length = INVALID_EXPORT_LENGTH;
1078 psa_status_t expected_export_status = expected_export_status_arg;
1079
Gilles Peskine8817f612018-12-18 00:18:46 +01001080 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001081
1082 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001083 status = psa_export_key( (psa_key_handle_t) handle,
Moran Peker28a38e62018-11-07 16:18:24 +02001084 exported, export_size,
1085 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001086 TEST_EQUAL( status, expected_export_status );
Moran Peker28a38e62018-11-07 16:18:24 +02001087
1088exit:
1089 mbedtls_psa_crypto_free( );
1090}
1091/* END_CASE */
1092
1093/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001094void export_with_no_key_activity( )
1095{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001096 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001097 psa_algorithm_t alg = PSA_ALG_CTR;
1098 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001099 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001100 unsigned char *exported = NULL;
1101 size_t export_size = 0;
1102 size_t exported_length = INVALID_EXPORT_LENGTH;
1103
Gilles Peskine8817f612018-12-18 00:18:46 +01001104 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001105
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001106 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001107 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001108 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001109
1110 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001111 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001112 exported, export_size,
1113 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001114 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001115
1116exit:
1117 mbedtls_psa_crypto_free( );
1118}
1119/* END_CASE */
1120
1121/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001122void cipher_with_no_key_activity( )
1123{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001124 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001125 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001126 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001127 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001128 int exercise_alg = PSA_ALG_CTR;
1129
Gilles Peskine8817f612018-12-18 00:18:46 +01001130 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001131
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001132 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekerce500072018-11-07 16:20:07 +02001133 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001134 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerce500072018-11-07 16:20:07 +02001135
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001136 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001137 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001138
1139exit:
1140 psa_cipher_abort( &operation );
1141 mbedtls_psa_crypto_free( );
1142}
1143/* END_CASE */
1144
1145/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001146void export_after_import_failure( data_t *data, int type_arg,
1147 int expected_import_status_arg )
1148{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001149 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001150 psa_key_type_t type = type_arg;
1151 psa_status_t status;
1152 unsigned char *exported = NULL;
1153 size_t export_size = 0;
1154 psa_status_t expected_import_status = expected_import_status_arg;
1155 size_t exported_length = INVALID_EXPORT_LENGTH;
1156
Gilles Peskine8817f612018-12-18 00:18:46 +01001157 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001158
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001159 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001160
Moran Peker34550092018-11-07 16:19:34 +02001161 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001162 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001163 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001164 TEST_EQUAL( status, expected_import_status );
Moran Peker34550092018-11-07 16:19:34 +02001165
1166 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001167 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001168 exported, export_size,
1169 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001170 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001171
1172exit:
1173 mbedtls_psa_crypto_free( );
1174}
1175/* END_CASE */
1176
1177/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001178void cipher_after_import_failure( data_t *data, int type_arg,
1179 int expected_import_status_arg )
1180{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001181 psa_key_handle_t handle = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001182 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001183 psa_key_type_t type = type_arg;
1184 psa_status_t status;
1185 psa_status_t expected_import_status = expected_import_status_arg;
1186 int exercise_alg = PSA_ALG_CTR;
1187
Gilles Peskine8817f612018-12-18 00:18:46 +01001188 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001189
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001190 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001191
Moran Pekerce500072018-11-07 16:20:07 +02001192 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001193 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001194 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001195 TEST_EQUAL( status, expected_import_status );
Moran Pekerce500072018-11-07 16:20:07 +02001196
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001197 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001198 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001199
1200exit:
1201 psa_cipher_abort( &operation );
1202 mbedtls_psa_crypto_free( );
1203}
1204/* END_CASE */
1205
1206/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001207void export_after_destroy_key( data_t *data, int type_arg )
1208{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001209 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001210 psa_key_type_t type = type_arg;
1211 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001212 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001213 psa_algorithm_t alg = PSA_ALG_CTR;
1214 unsigned char *exported = NULL;
1215 size_t export_size = 0;
1216 size_t exported_length = INVALID_EXPORT_LENGTH;
1217
Gilles Peskine8817f612018-12-18 00:18:46 +01001218 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001219
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001220 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001221 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001222 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001223 export_size = (ptrdiff_t) data->len;
1224 ASSERT_ALLOC( exported, export_size );
1225
1226 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001227 PSA_ASSERT( psa_import_key( handle, type,
1228 data->x, data->len ) );
Moran Peker34550092018-11-07 16:19:34 +02001229
Gilles Peskine8817f612018-12-18 00:18:46 +01001230 PSA_ASSERT( psa_export_key( handle, exported, export_size,
1231 &exported_length ) );
Moran Peker34550092018-11-07 16:19:34 +02001232
1233 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001234 PSA_ASSERT( psa_destroy_key( handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001235
1236 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001237 status = psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001238 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001239 TEST_EQUAL( status, PSA_ERROR_INVALID_HANDLE );
Moran Peker34550092018-11-07 16:19:34 +02001240
1241exit:
1242 mbedtls_free( exported );
1243 mbedtls_psa_crypto_free( );
1244}
1245/* END_CASE */
1246
1247/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001248void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001249 int type_arg,
1250 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001251 int export_size_delta,
1252 int expected_export_status_arg,
1253 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001254{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001255 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001256 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001257 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001258 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001259 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001260 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001261 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001262 size_t exported_length = INVALID_EXPORT_LENGTH;
Jaeden Amero70261c52019-01-04 11:47:20 +00001263 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001264
Gilles Peskine8817f612018-12-18 00:18:46 +01001265 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001266
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001267 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001268 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001269 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001270
1271 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001272 PSA_ASSERT( psa_import_key( handle, type,
1273 data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001274
Gilles Peskine49c25912018-10-29 15:15:31 +01001275 /* Export the public key */
1276 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001277 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001278 exported, export_size,
1279 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001280 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001281 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001282 {
1283 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1284 size_t bits;
Gilles Peskine8817f612018-12-18 00:18:46 +01001285 PSA_ASSERT( psa_get_key_information( handle, NULL, &bits ) );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001286 TEST_ASSERT( expected_public_key->len <=
1287 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001288 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1289 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001290 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001291
1292exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001293 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001294 psa_destroy_key( handle );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001295 mbedtls_psa_crypto_free( );
1296}
1297/* END_CASE */
1298
Gilles Peskine20035e32018-02-03 22:44:14 +01001299/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001300void import_and_exercise_key( data_t *data,
1301 int type_arg,
1302 int bits_arg,
1303 int alg_arg )
1304{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001305 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001306 psa_key_type_t type = type_arg;
1307 size_t bits = bits_arg;
1308 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001309 psa_key_usage_t usage = usage_to_exercise( type, alg );
Jaeden Amero70261c52019-01-04 11:47:20 +00001310 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001311 psa_key_type_t got_type;
1312 size_t got_bits;
1313 psa_status_t status;
1314
Gilles Peskine8817f612018-12-18 00:18:46 +01001315 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001316
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001317 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001318 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001319 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001320
1321 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001322 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01001323 PSA_ASSERT( status );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001324
1325 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001326 PSA_ASSERT( psa_get_key_information( handle,
1327 &got_type,
1328 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001329 TEST_EQUAL( got_type, type );
1330 TEST_EQUAL( got_bits, bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001331
1332 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001333 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001334 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001335
1336exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001337 psa_destroy_key( handle );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001338 mbedtls_psa_crypto_free( );
1339}
1340/* END_CASE */
1341
1342/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001343void key_policy( int usage_arg, int alg_arg )
1344{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001345 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001346 psa_algorithm_t alg = alg_arg;
1347 psa_key_usage_t usage = usage_arg;
1348 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1349 unsigned char key[32] = {0};
Jaeden Amero70261c52019-01-04 11:47:20 +00001350 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
1351 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001352
1353 memset( key, 0x2a, sizeof( key ) );
1354
Gilles Peskine8817f612018-12-18 00:18:46 +01001355 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001356
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001357 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001358 psa_key_policy_set_usage( &policy_set, usage, alg );
1359
Gilles Peskinefe11b722018-12-18 00:24:04 +01001360 TEST_EQUAL( psa_key_policy_get_usage( &policy_set ), usage );
1361 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_set ), alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001362 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001363
Gilles Peskine8817f612018-12-18 00:18:46 +01001364 PSA_ASSERT( psa_import_key( handle, key_type,
1365 key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001366
Gilles Peskine8817f612018-12-18 00:18:46 +01001367 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001368
Gilles Peskinefe11b722018-12-18 00:24:04 +01001369 TEST_EQUAL( policy_get.usage, policy_set.usage );
1370 TEST_EQUAL( policy_get.alg, policy_set.alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001371
1372exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001373 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001374 mbedtls_psa_crypto_free( );
1375}
1376/* END_CASE */
1377
1378/* BEGIN_CASE */
Jaeden Amero70261c52019-01-04 11:47:20 +00001379void key_policy_init( )
1380{
1381 /* Test each valid way of initializing the object, except for `= {0}`, as
1382 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1383 * though it's OK by the C standard. We could test for this, but we'd need
1384 * to supress the Clang warning for the test. */
1385 psa_key_policy_t func = psa_key_policy_init( );
1386 psa_key_policy_t init = PSA_KEY_POLICY_INIT;
1387 psa_key_policy_t zero;
1388
1389 memset( &zero, 0, sizeof( zero ) );
1390
1391 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1392 * specification, we test that all valid ways of initializing the object
1393 * have the same bit pattern. This is a stronger requirement that may not
1394 * be valid on all platforms or PSA Crypto implementations, but implies the
1395 * weaker actual requirement is met: that a freshly initialized object, no
1396 * matter how it was initialized, acts the same as any other valid
1397 * initialization. */
1398 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1399 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1400}
1401/* END_CASE */
1402
1403/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001404void mac_key_policy( int policy_usage,
1405 int policy_alg,
1406 int key_type,
1407 data_t *key_data,
1408 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001409{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001410 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001411 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001412 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001413 psa_status_t status;
1414 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001415
Gilles Peskine8817f612018-12-18 00:18:46 +01001416 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001417
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001418 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001419 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001420 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001421
Gilles Peskine8817f612018-12-18 00:18:46 +01001422 PSA_ASSERT( psa_import_key( handle, key_type,
1423 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001424
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001425 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001426 if( policy_alg == exercise_alg &&
1427 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001428 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001429 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001430 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001431 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001432
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001433 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001434 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001435 if( policy_alg == exercise_alg &&
1436 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001437 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001438 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001439 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001440
1441exit:
1442 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001443 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001444 mbedtls_psa_crypto_free( );
1445}
1446/* END_CASE */
1447
1448/* BEGIN_CASE */
1449void cipher_key_policy( int policy_usage,
1450 int policy_alg,
1451 int key_type,
1452 data_t *key_data,
1453 int exercise_alg )
1454{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001455 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001456 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001457 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001458 psa_status_t status;
1459
Gilles Peskine8817f612018-12-18 00:18:46 +01001460 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001461
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001462 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001463 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001464 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001465
Gilles Peskine8817f612018-12-18 00:18:46 +01001466 PSA_ASSERT( psa_import_key( handle, key_type,
1467 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001468
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001469 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001470 if( policy_alg == exercise_alg &&
1471 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001472 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001473 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001474 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001475 psa_cipher_abort( &operation );
1476
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001477 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001478 if( policy_alg == exercise_alg &&
1479 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001480 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001481 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001482 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001483
1484exit:
1485 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001486 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001487 mbedtls_psa_crypto_free( );
1488}
1489/* END_CASE */
1490
1491/* BEGIN_CASE */
1492void aead_key_policy( int policy_usage,
1493 int policy_alg,
1494 int key_type,
1495 data_t *key_data,
1496 int nonce_length_arg,
1497 int tag_length_arg,
1498 int exercise_alg )
1499{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001500 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001501 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001502 psa_status_t status;
1503 unsigned char nonce[16] = {0};
1504 size_t nonce_length = nonce_length_arg;
1505 unsigned char tag[16];
1506 size_t tag_length = tag_length_arg;
1507 size_t output_length;
1508
1509 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1510 TEST_ASSERT( tag_length <= sizeof( tag ) );
1511
Gilles Peskine8817f612018-12-18 00:18:46 +01001512 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001513
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001514 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001515 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001516 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001517
Gilles Peskine8817f612018-12-18 00:18:46 +01001518 PSA_ASSERT( psa_import_key( handle, key_type,
1519 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001520
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001521 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001522 nonce, nonce_length,
1523 NULL, 0,
1524 NULL, 0,
1525 tag, tag_length,
1526 &output_length );
1527 if( policy_alg == exercise_alg &&
1528 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001529 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001530 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001531 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001532
1533 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001534 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001535 nonce, nonce_length,
1536 NULL, 0,
1537 tag, tag_length,
1538 NULL, 0,
1539 &output_length );
1540 if( policy_alg == exercise_alg &&
1541 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001542 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001543 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001544 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001545
1546exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001547 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001548 mbedtls_psa_crypto_free( );
1549}
1550/* END_CASE */
1551
1552/* BEGIN_CASE */
1553void asymmetric_encryption_key_policy( int policy_usage,
1554 int policy_alg,
1555 int key_type,
1556 data_t *key_data,
1557 int exercise_alg )
1558{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001559 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001560 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001561 psa_status_t status;
1562 size_t key_bits;
1563 size_t buffer_length;
1564 unsigned char *buffer = NULL;
1565 size_t output_length;
1566
Gilles Peskine8817f612018-12-18 00:18:46 +01001567 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001568
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001569 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001570 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001571 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001572
Gilles Peskine8817f612018-12-18 00:18:46 +01001573 PSA_ASSERT( psa_import_key( handle, key_type,
1574 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001575
Gilles Peskine8817f612018-12-18 00:18:46 +01001576 PSA_ASSERT( psa_get_key_information( handle,
1577 NULL,
1578 &key_bits ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001579 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1580 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001581 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001582
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001583 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001584 NULL, 0,
1585 NULL, 0,
1586 buffer, buffer_length,
1587 &output_length );
1588 if( policy_alg == exercise_alg &&
1589 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001590 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001591 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001592 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001593
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001594 if( buffer_length != 0 )
1595 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001596 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001597 buffer, buffer_length,
1598 NULL, 0,
1599 buffer, buffer_length,
1600 &output_length );
1601 if( policy_alg == exercise_alg &&
1602 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001603 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001604 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001605 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001606
1607exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001608 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001609 mbedtls_psa_crypto_free( );
1610 mbedtls_free( buffer );
1611}
1612/* END_CASE */
1613
1614/* BEGIN_CASE */
1615void asymmetric_signature_key_policy( int policy_usage,
1616 int policy_alg,
1617 int key_type,
1618 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001619 int exercise_alg,
1620 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001621{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001622 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001623 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001624 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001625 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1626 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1627 * compatible with the policy and `payload_length_arg` is supposed to be
1628 * a valid input length to sign. If `payload_length_arg <= 0`,
1629 * `exercise_alg` is supposed to be forbidden by the policy. */
1630 int compatible_alg = payload_length_arg > 0;
1631 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001632 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1633 size_t signature_length;
1634
Gilles Peskine8817f612018-12-18 00:18:46 +01001635 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001636
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001637 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001638 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001639 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001640
Gilles Peskine8817f612018-12-18 00:18:46 +01001641 PSA_ASSERT( psa_import_key( handle, key_type,
1642 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001643
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001644 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001645 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001646 signature, sizeof( signature ),
1647 &signature_length );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001648 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001649 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001650 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001651 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001652
1653 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001654 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001655 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001656 signature, sizeof( signature ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001657 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001658 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001659 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001660 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001661
1662exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001663 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001664 mbedtls_psa_crypto_free( );
1665}
1666/* END_CASE */
1667
1668/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001669void derive_key_policy( int policy_usage,
1670 int policy_alg,
1671 int key_type,
1672 data_t *key_data,
1673 int exercise_alg )
1674{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001675 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001676 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001677 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1678 psa_status_t status;
1679
Gilles Peskine8817f612018-12-18 00:18:46 +01001680 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001681
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001682 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001683 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001684 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001685
Gilles Peskine8817f612018-12-18 00:18:46 +01001686 PSA_ASSERT( psa_import_key( handle, key_type,
1687 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001688
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001689 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001690 exercise_alg,
1691 NULL, 0,
1692 NULL, 0,
1693 1 );
1694 if( policy_alg == exercise_alg &&
1695 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001696 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001697 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001698 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001699
1700exit:
1701 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001702 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001703 mbedtls_psa_crypto_free( );
1704}
1705/* END_CASE */
1706
1707/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001708void agreement_key_policy( int policy_usage,
1709 int policy_alg,
1710 int key_type_arg,
1711 data_t *key_data,
1712 int exercise_alg )
1713{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001714 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001715 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001716 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001717 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1718 psa_status_t status;
1719
Gilles Peskine8817f612018-12-18 00:18:46 +01001720 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001721
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001722 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001723 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001724 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001725
Gilles Peskine8817f612018-12-18 00:18:46 +01001726 PSA_ASSERT( psa_import_key( handle, key_type,
1727 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001728
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001729 status = key_agreement_with_self( &generator, handle, exercise_alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001730
Gilles Peskine01d718c2018-09-18 12:01:02 +02001731 if( policy_alg == exercise_alg &&
1732 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001733 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001734 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001735 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001736
1737exit:
1738 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001739 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001740 mbedtls_psa_crypto_free( );
1741}
1742/* END_CASE */
1743
1744/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001745void hash_operation_init( )
1746{
1747 /* Test each valid way of initializing the object, except for `= {0}`, as
1748 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1749 * though it's OK by the C standard. We could test for this, but we'd need
1750 * to supress the Clang warning for the test. */
1751 psa_hash_operation_t func = psa_hash_operation_init( );
1752 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1753 psa_hash_operation_t zero;
1754
1755 memset( &zero, 0, sizeof( zero ) );
1756
1757 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1758 * specification, we test that all valid ways of initializing the object
1759 * have the same bit pattern. This is a stronger requirement that may not
1760 * be valid on all platforms or PSA Crypto implementations, but implies the
1761 * weaker actual requirement is met: that a freshly initialized object, no
1762 * matter how it was initialized, acts the same as any other valid
1763 * initialization. */
1764 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1765 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1766}
1767/* END_CASE */
1768
1769/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001770void hash_setup( int alg_arg,
1771 int expected_status_arg )
1772{
1773 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001774 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001775 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001776 psa_status_t status;
1777
Gilles Peskine8817f612018-12-18 00:18:46 +01001778 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001779
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001780 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001781 psa_hash_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001782 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001783
1784exit:
1785 mbedtls_psa_crypto_free( );
1786}
1787/* END_CASE */
1788
1789/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02001790void hash_bad_order( )
1791{
1792 unsigned char input[] = "";
1793 /* SHA-256 hash of an empty string */
1794 unsigned char hash[] = {
1795 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1796 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1797 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
1798 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001799 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001800
Gilles Peskine8817f612018-12-18 00:18:46 +01001801 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001802
1803 /* psa_hash_update without calling psa_hash_setup beforehand */
1804 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001805 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001806 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001807
1808 /* psa_hash_verify without calling psa_hash_setup beforehand */
1809 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001810 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001811 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001812
1813 /* psa_hash_finish without calling psa_hash_setup beforehand */
1814 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001815 TEST_EQUAL( psa_hash_finish( &operation,
1816 hash, sizeof( hash ), &hash_len ),
1817 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001818
1819exit:
1820 mbedtls_psa_crypto_free( );
1821}
1822/* END_CASE */
1823
itayzafrir27e69452018-11-01 14:26:34 +02001824/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1825void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001826{
1827 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001828 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1829 * appended to it */
1830 unsigned char hash[] = {
1831 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1832 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1833 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03001834 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001835 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001836
Gilles Peskine8817f612018-12-18 00:18:46 +01001837 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001838
itayzafrir27e69452018-11-01 14:26:34 +02001839 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001840 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001841 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001842 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001843
itayzafrir27e69452018-11-01 14:26:34 +02001844 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001845 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001846 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001847 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001848
itayzafrir27e69452018-11-01 14:26:34 +02001849 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001850 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001851 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001852 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001853
itayzafrirec93d302018-10-18 18:01:10 +03001854exit:
1855 mbedtls_psa_crypto_free( );
1856}
1857/* END_CASE */
1858
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001859/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1860void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001861{
1862 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001863 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03001864 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001865 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001866 size_t hash_len;
1867
Gilles Peskine8817f612018-12-18 00:18:46 +01001868 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001869
itayzafrir58028322018-10-25 10:22:01 +03001870 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001871 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001872 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001873 hash, expected_size - 1, &hash_len ),
1874 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001875
1876exit:
1877 mbedtls_psa_crypto_free( );
1878}
1879/* END_CASE */
1880
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001881/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1882void hash_clone_source_state( )
1883{
1884 psa_algorithm_t alg = PSA_ALG_SHA_256;
1885 unsigned char hash[PSA_HASH_MAX_SIZE];
1886 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1887 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1888 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1889 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1890 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1891 size_t hash_len;
1892
1893 PSA_ASSERT( psa_crypto_init( ) );
1894 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1895
1896 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1897 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1898 PSA_ASSERT( psa_hash_finish( &op_finished,
1899 hash, sizeof( hash ), &hash_len ) );
1900 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1901 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1902
1903 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1904 PSA_ERROR_BAD_STATE );
1905
1906 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1907 PSA_ASSERT( psa_hash_finish( &op_init,
1908 hash, sizeof( hash ), &hash_len ) );
1909 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1910 PSA_ASSERT( psa_hash_finish( &op_finished,
1911 hash, sizeof( hash ), &hash_len ) );
1912 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1913 PSA_ASSERT( psa_hash_finish( &op_aborted,
1914 hash, sizeof( hash ), &hash_len ) );
1915
1916exit:
1917 psa_hash_abort( &op_source );
1918 psa_hash_abort( &op_init );
1919 psa_hash_abort( &op_setup );
1920 psa_hash_abort( &op_finished );
1921 psa_hash_abort( &op_aborted );
1922 mbedtls_psa_crypto_free( );
1923}
1924/* END_CASE */
1925
1926/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1927void hash_clone_target_state( )
1928{
1929 psa_algorithm_t alg = PSA_ALG_SHA_256;
1930 unsigned char hash[PSA_HASH_MAX_SIZE];
1931 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1932 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1933 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1934 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1935 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1936 size_t hash_len;
1937
1938 PSA_ASSERT( psa_crypto_init( ) );
1939
1940 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1941 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1942 PSA_ASSERT( psa_hash_finish( &op_finished,
1943 hash, sizeof( hash ), &hash_len ) );
1944 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1945 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1946
1947 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1948 PSA_ASSERT( psa_hash_finish( &op_target,
1949 hash, sizeof( hash ), &hash_len ) );
1950
1951 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1952 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1953 PSA_ERROR_BAD_STATE );
1954 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1955 PSA_ERROR_BAD_STATE );
1956
1957exit:
1958 psa_hash_abort( &op_target );
1959 psa_hash_abort( &op_init );
1960 psa_hash_abort( &op_setup );
1961 psa_hash_abort( &op_finished );
1962 psa_hash_abort( &op_aborted );
1963 mbedtls_psa_crypto_free( );
1964}
1965/* END_CASE */
1966
itayzafrir58028322018-10-25 10:22:01 +03001967/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001968void mac_operation_init( )
1969{
1970 /* Test each valid way of initializing the object, except for `= {0}`, as
1971 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1972 * though it's OK by the C standard. We could test for this, but we'd need
1973 * to supress the Clang warning for the test. */
1974 psa_mac_operation_t func = psa_mac_operation_init( );
1975 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1976 psa_mac_operation_t zero;
1977
1978 memset( &zero, 0, sizeof( zero ) );
1979
1980 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1981 * specification, we test that all valid ways of initializing the object
1982 * have the same bit pattern. This is a stronger requirement that may not
1983 * be valid on all platforms or PSA Crypto implementations, but implies the
1984 * weaker actual requirement is met: that a freshly initialized object, no
1985 * matter how it was initialized, acts the same as any other valid
1986 * initialization. */
1987 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1988 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1989}
1990/* END_CASE */
1991
1992/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001993void mac_setup( int key_type_arg,
1994 data_t *key,
1995 int alg_arg,
1996 int expected_status_arg )
1997{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001998 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001999 psa_key_type_t key_type = key_type_arg;
2000 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002001 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002002 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002003 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002004 psa_status_t status;
2005
Gilles Peskine8817f612018-12-18 00:18:46 +01002006 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002007
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002008 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002009 psa_key_policy_set_usage( &policy,
2010 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2011 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002012 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002013
Gilles Peskine8817f612018-12-18 00:18:46 +01002014 PSA_ASSERT( psa_import_key( handle, key_type,
2015 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002016
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002017 status = psa_mac_sign_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002018 psa_mac_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002019 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002020
2021exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002022 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002023 mbedtls_psa_crypto_free( );
2024}
2025/* END_CASE */
2026
2027/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002028void mac_sign( int key_type_arg,
2029 data_t *key,
2030 int alg_arg,
2031 data_t *input,
2032 data_t *expected_mac )
2033{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002034 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002035 psa_key_type_t key_type = key_type_arg;
2036 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002037 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002038 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002039 /* Leave a little extra room in the output buffer. At the end of the
2040 * test, we'll check that the implementation didn't overwrite onto
2041 * this extra room. */
2042 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2043 size_t mac_buffer_size =
2044 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2045 size_t mac_length = 0;
2046
2047 memset( actual_mac, '+', sizeof( actual_mac ) );
2048 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2049 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2050
Gilles Peskine8817f612018-12-18 00:18:46 +01002051 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002052
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002053 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002054 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002055 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002056
Gilles Peskine8817f612018-12-18 00:18:46 +01002057 PSA_ASSERT( psa_import_key( handle, key_type,
2058 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002059
2060 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002061 PSA_ASSERT( psa_mac_sign_setup( &operation,
2062 handle, alg ) );
2063 PSA_ASSERT( psa_mac_update( &operation,
2064 input->x, input->len ) );
2065 PSA_ASSERT( psa_mac_sign_finish( &operation,
2066 actual_mac, mac_buffer_size,
2067 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002068
2069 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002070 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2071 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002072
2073 /* Verify that the end of the buffer is untouched. */
2074 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2075 sizeof( actual_mac ) - mac_length ) );
2076
2077exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002078 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002079 mbedtls_psa_crypto_free( );
2080}
2081/* END_CASE */
2082
2083/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002084void mac_verify( int key_type_arg,
2085 data_t *key,
2086 int alg_arg,
2087 data_t *input,
2088 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002089{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002090 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002091 psa_key_type_t key_type = key_type_arg;
2092 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002093 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002094 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002095
Gilles Peskine69c12672018-06-28 00:07:19 +02002096 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2097
Gilles Peskine8817f612018-12-18 00:18:46 +01002098 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002099
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002100 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002101 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002102 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad16036df908f2018-04-02 08:34:15 -07002103
Gilles Peskine8817f612018-12-18 00:18:46 +01002104 PSA_ASSERT( psa_import_key( handle, key_type,
2105 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002106
Gilles Peskine8817f612018-12-18 00:18:46 +01002107 PSA_ASSERT( psa_mac_verify_setup( &operation,
2108 handle, alg ) );
2109 PSA_ASSERT( psa_destroy_key( handle ) );
2110 PSA_ASSERT( psa_mac_update( &operation,
2111 input->x, input->len ) );
2112 PSA_ASSERT( psa_mac_verify_finish( &operation,
2113 expected_mac->x,
2114 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002115
2116exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002117 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002118 mbedtls_psa_crypto_free( );
2119}
2120/* END_CASE */
2121
2122/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002123void cipher_operation_init( )
2124{
2125 /* Test each valid way of initializing the object, except for `= {0}`, as
2126 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2127 * though it's OK by the C standard. We could test for this, but we'd need
2128 * to supress the Clang warning for the test. */
2129 psa_cipher_operation_t func = psa_cipher_operation_init( );
2130 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2131 psa_cipher_operation_t zero;
2132
2133 memset( &zero, 0, sizeof( zero ) );
2134
2135 /* Although not technically guaranteed by the C standard nor the PSA Crypto
2136 * specification, we test that all valid ways of initializing the object
2137 * have the same bit pattern. This is a stronger requirement that may not
2138 * be valid on all platforms or PSA Crypto implementations, but implies the
2139 * weaker actual requirement is met: that a freshly initialized object, no
2140 * matter how it was initialized, acts the same as any other valid
2141 * initialization. */
2142 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
2143 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
2144}
2145/* END_CASE */
2146
2147/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002148void cipher_setup( int key_type_arg,
2149 data_t *key,
2150 int alg_arg,
2151 int expected_status_arg )
2152{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002153 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002154 psa_key_type_t key_type = key_type_arg;
2155 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002156 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002157 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002158 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002159 psa_status_t status;
2160
Gilles Peskine8817f612018-12-18 00:18:46 +01002161 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002162
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002163 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002164 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002165 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002166
Gilles Peskine8817f612018-12-18 00:18:46 +01002167 PSA_ASSERT( psa_import_key( handle, key_type,
2168 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002169
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002170 status = psa_cipher_encrypt_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002171 psa_cipher_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002172 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002173
2174exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002175 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002176 mbedtls_psa_crypto_free( );
2177}
2178/* END_CASE */
2179
2180/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002181void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002182 data_t *key,
2183 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002184 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002185{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002186 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002187 psa_status_t status;
2188 psa_key_type_t key_type = key_type_arg;
2189 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002190 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002191 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002192 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002193 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002194 size_t output_buffer_size = 0;
2195 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002196 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002197 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002198 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002199
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002200 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2201 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002202
Gilles Peskine8817f612018-12-18 00:18:46 +01002203 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002204
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002205 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002206 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002207 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002208
Gilles Peskine8817f612018-12-18 00:18:46 +01002209 PSA_ASSERT( psa_import_key( handle, key_type,
2210 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002211
Gilles Peskine8817f612018-12-18 00:18:46 +01002212 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2213 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002214
Gilles Peskine8817f612018-12-18 00:18:46 +01002215 PSA_ASSERT( psa_cipher_set_iv( &operation,
2216 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002217 output_buffer_size = ( (size_t) input->len +
2218 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002219 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002220
Gilles Peskine8817f612018-12-18 00:18:46 +01002221 PSA_ASSERT( psa_cipher_update( &operation,
2222 input->x, input->len,
2223 output, output_buffer_size,
2224 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002225 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002226 status = psa_cipher_finish( &operation,
2227 output + function_output_length,
2228 output_buffer_size,
2229 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002230 total_output_length += function_output_length;
2231
Gilles Peskinefe11b722018-12-18 00:24:04 +01002232 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002233 if( expected_status == PSA_SUCCESS )
2234 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002235 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002236 ASSERT_COMPARE( expected_output->x, expected_output->len,
2237 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002238 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002239
Gilles Peskine50e586b2018-06-08 14:28:46 +02002240exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002241 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002242 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002243 mbedtls_psa_crypto_free( );
2244}
2245/* END_CASE */
2246
2247/* BEGIN_CASE */
2248void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002249 data_t *key,
2250 data_t *input,
2251 int first_part_size,
2252 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002253{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002254 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002255 psa_key_type_t key_type = key_type_arg;
2256 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002257 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002258 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002259 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002260 size_t output_buffer_size = 0;
2261 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002262 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002263 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002264 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002265
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002266 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2267 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002268
Gilles Peskine8817f612018-12-18 00:18:46 +01002269 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002270
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002271 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002272 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002273 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002274
Gilles Peskine8817f612018-12-18 00:18:46 +01002275 PSA_ASSERT( psa_import_key( handle, key_type,
2276 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002277
Gilles Peskine8817f612018-12-18 00:18:46 +01002278 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2279 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002280
Gilles Peskine8817f612018-12-18 00:18:46 +01002281 PSA_ASSERT( psa_cipher_set_iv( &operation,
2282 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002283 output_buffer_size = ( (size_t) input->len +
2284 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002285 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002286
Gilles Peskine4abf7412018-06-18 16:35:34 +02002287 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002288 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2289 output, output_buffer_size,
2290 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002291 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002292 PSA_ASSERT( psa_cipher_update( &operation,
2293 input->x + first_part_size,
2294 input->len - first_part_size,
2295 output, output_buffer_size,
2296 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002297 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002298 PSA_ASSERT( psa_cipher_finish( &operation,
2299 output + function_output_length,
2300 output_buffer_size,
2301 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002302 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002303 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002304
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002305 ASSERT_COMPARE( expected_output->x, expected_output->len,
2306 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002307
2308exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002309 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002310 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002311 mbedtls_psa_crypto_free( );
2312}
2313/* END_CASE */
2314
2315/* BEGIN_CASE */
2316void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002317 data_t *key,
2318 data_t *input,
2319 int first_part_size,
2320 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002321{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002322 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002323
2324 psa_key_type_t key_type = key_type_arg;
2325 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002326 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002327 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002328 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002329 size_t output_buffer_size = 0;
2330 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002331 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002332 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002333 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002334
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002335 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2336 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002337
Gilles Peskine8817f612018-12-18 00:18:46 +01002338 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002339
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002340 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002341 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002342 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002343
Gilles Peskine8817f612018-12-18 00:18:46 +01002344 PSA_ASSERT( psa_import_key( handle, key_type,
2345 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002346
Gilles Peskine8817f612018-12-18 00:18:46 +01002347 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2348 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002349
Gilles Peskine8817f612018-12-18 00:18:46 +01002350 PSA_ASSERT( psa_cipher_set_iv( &operation,
2351 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002352
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002353 output_buffer_size = ( (size_t) input->len +
2354 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002355 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002356
Gilles Peskine4abf7412018-06-18 16:35:34 +02002357 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002358 PSA_ASSERT( psa_cipher_update( &operation,
2359 input->x, first_part_size,
2360 output, output_buffer_size,
2361 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002362 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002363 PSA_ASSERT( psa_cipher_update( &operation,
2364 input->x + first_part_size,
2365 input->len - first_part_size,
2366 output, output_buffer_size,
2367 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002368 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002369 PSA_ASSERT( psa_cipher_finish( &operation,
2370 output + function_output_length,
2371 output_buffer_size,
2372 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002373 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002374 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002375
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002376 ASSERT_COMPARE( expected_output->x, expected_output->len,
2377 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002378
2379exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002380 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002381 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002382 mbedtls_psa_crypto_free( );
2383}
2384/* END_CASE */
2385
Gilles Peskine50e586b2018-06-08 14:28:46 +02002386/* BEGIN_CASE */
2387void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002388 data_t *key,
2389 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002390 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002391{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002392 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002393 psa_status_t status;
2394 psa_key_type_t key_type = key_type_arg;
2395 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002396 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002397 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002398 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002399 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002400 size_t output_buffer_size = 0;
2401 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002402 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002403 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002404 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002405
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002406 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2407 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002408
Gilles Peskine8817f612018-12-18 00:18:46 +01002409 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002410
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002411 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002412 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002413 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002414
Gilles Peskine8817f612018-12-18 00:18:46 +01002415 PSA_ASSERT( psa_import_key( handle, key_type,
2416 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002417
Gilles Peskine8817f612018-12-18 00:18:46 +01002418 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2419 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002420
Gilles Peskine8817f612018-12-18 00:18:46 +01002421 PSA_ASSERT( psa_cipher_set_iv( &operation,
2422 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002423
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002424 output_buffer_size = ( (size_t) input->len +
2425 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002426 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002427
Gilles Peskine8817f612018-12-18 00:18:46 +01002428 PSA_ASSERT( psa_cipher_update( &operation,
2429 input->x, input->len,
2430 output, output_buffer_size,
2431 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002432 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002433 status = psa_cipher_finish( &operation,
2434 output + function_output_length,
2435 output_buffer_size,
2436 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002437 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002438 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002439
2440 if( expected_status == PSA_SUCCESS )
2441 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002442 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002443 ASSERT_COMPARE( expected_output->x, expected_output->len,
2444 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002445 }
2446
Gilles Peskine50e586b2018-06-08 14:28:46 +02002447exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002448 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002449 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002450 mbedtls_psa_crypto_free( );
2451}
2452/* END_CASE */
2453
Gilles Peskine50e586b2018-06-08 14:28:46 +02002454/* BEGIN_CASE */
2455void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002456 data_t *key,
2457 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002458{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002459 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002460 psa_key_type_t key_type = key_type_arg;
2461 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002462 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002463 size_t iv_size = 16;
2464 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002465 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002466 size_t output1_size = 0;
2467 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002468 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002469 size_t output2_size = 0;
2470 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002471 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002472 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2473 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002474 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002475
Gilles Peskine8817f612018-12-18 00:18:46 +01002476 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002477
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002478 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002479 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002480 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002481
Gilles Peskine8817f612018-12-18 00:18:46 +01002482 PSA_ASSERT( psa_import_key( handle, key_type,
2483 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002484
Gilles Peskine8817f612018-12-18 00:18:46 +01002485 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2486 handle, alg ) );
2487 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2488 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002489
Gilles Peskine8817f612018-12-18 00:18:46 +01002490 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2491 iv, iv_size,
2492 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002493 output1_size = ( (size_t) input->len +
2494 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002495 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002496
Gilles Peskine8817f612018-12-18 00:18:46 +01002497 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2498 output1, output1_size,
2499 &output1_length ) );
2500 PSA_ASSERT( psa_cipher_finish( &operation1,
2501 output1 + output1_length, output1_size,
2502 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002503
Gilles Peskine048b7f02018-06-08 14:20:49 +02002504 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002505
Gilles Peskine8817f612018-12-18 00:18:46 +01002506 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002507
2508 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002509 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002510
Gilles Peskine8817f612018-12-18 00:18:46 +01002511 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2512 iv, iv_length ) );
2513 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2514 output2, output2_size,
2515 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002516 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002517 PSA_ASSERT( psa_cipher_finish( &operation2,
2518 output2 + output2_length,
2519 output2_size,
2520 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002521
Gilles Peskine048b7f02018-06-08 14:20:49 +02002522 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002523
Gilles Peskine8817f612018-12-18 00:18:46 +01002524 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002525
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002526 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002527
2528exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002529 mbedtls_free( output1 );
2530 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002531 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03002532 mbedtls_psa_crypto_free( );
2533}
2534/* END_CASE */
2535
2536/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002537void cipher_verify_output_multipart( int alg_arg,
2538 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002539 data_t *key,
2540 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002541 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002542{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002543 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002544 psa_key_type_t key_type = key_type_arg;
2545 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002546 unsigned char iv[16] = {0};
2547 size_t iv_size = 16;
2548 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002549 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002550 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002551 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002552 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002553 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002554 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002555 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002556 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2557 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002558 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002559
Gilles Peskine8817f612018-12-18 00:18:46 +01002560 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002561
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002562 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002563 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002564 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002565
Gilles Peskine8817f612018-12-18 00:18:46 +01002566 PSA_ASSERT( psa_import_key( handle, key_type,
2567 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03002568
Gilles Peskine8817f612018-12-18 00:18:46 +01002569 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2570 handle, alg ) );
2571 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2572 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002573
Gilles Peskine8817f612018-12-18 00:18:46 +01002574 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2575 iv, iv_size,
2576 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002577 output1_buffer_size = ( (size_t) input->len +
2578 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002579 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002580
Gilles Peskine4abf7412018-06-18 16:35:34 +02002581 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002582
Gilles Peskine8817f612018-12-18 00:18:46 +01002583 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2584 output1, output1_buffer_size,
2585 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002586 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002587
Gilles Peskine8817f612018-12-18 00:18:46 +01002588 PSA_ASSERT( psa_cipher_update( &operation1,
2589 input->x + first_part_size,
2590 input->len - first_part_size,
2591 output1, output1_buffer_size,
2592 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002593 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002594
Gilles Peskine8817f612018-12-18 00:18:46 +01002595 PSA_ASSERT( psa_cipher_finish( &operation1,
2596 output1 + output1_length,
2597 output1_buffer_size - output1_length,
2598 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002599 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002600
Gilles Peskine8817f612018-12-18 00:18:46 +01002601 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002602
Gilles Peskine048b7f02018-06-08 14:20:49 +02002603 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002604 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002605
Gilles Peskine8817f612018-12-18 00:18:46 +01002606 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2607 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002608
Gilles Peskine8817f612018-12-18 00:18:46 +01002609 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2610 output2, output2_buffer_size,
2611 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002612 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002613
Gilles Peskine8817f612018-12-18 00:18:46 +01002614 PSA_ASSERT( psa_cipher_update( &operation2,
2615 output1 + first_part_size,
2616 output1_length - first_part_size,
2617 output2, output2_buffer_size,
2618 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002619 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002620
Gilles Peskine8817f612018-12-18 00:18:46 +01002621 PSA_ASSERT( psa_cipher_finish( &operation2,
2622 output2 + output2_length,
2623 output2_buffer_size - output2_length,
2624 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002625 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002626
Gilles Peskine8817f612018-12-18 00:18:46 +01002627 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002628
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002629 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002630
2631exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002632 mbedtls_free( output1 );
2633 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002634 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002635 mbedtls_psa_crypto_free( );
2636}
2637/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002638
Gilles Peskine20035e32018-02-03 22:44:14 +01002639/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002640void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002641 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002642 data_t *nonce,
2643 data_t *additional_data,
2644 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002645 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002646{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002647 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002648 psa_key_type_t key_type = key_type_arg;
2649 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002650 unsigned char *output_data = NULL;
2651 size_t output_size = 0;
2652 size_t output_length = 0;
2653 unsigned char *output_data2 = NULL;
2654 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002655 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002656 psa_status_t expected_result = expected_result_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002657 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002658
Gilles Peskine4abf7412018-06-18 16:35:34 +02002659 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002660 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002661
Gilles Peskine8817f612018-12-18 00:18:46 +01002662 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002663
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002664 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002665 psa_key_policy_set_usage( &policy,
2666 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2667 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, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002672
Gilles Peskinefe11b722018-12-18 00:24:04 +01002673 TEST_EQUAL( psa_aead_encrypt( handle, alg,
2674 nonce->x, nonce->len,
2675 additional_data->x,
2676 additional_data->len,
2677 input_data->x, input_data->len,
2678 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002679 &output_length ),
2680 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002681
2682 if( PSA_SUCCESS == expected_result )
2683 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002684 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002685
Gilles Peskinefe11b722018-12-18 00:24:04 +01002686 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2687 nonce->x, nonce->len,
2688 additional_data->x,
2689 additional_data->len,
2690 output_data, output_length,
2691 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002692 &output_length2 ),
2693 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002694
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002695 ASSERT_COMPARE( input_data->x, input_data->len,
2696 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002697 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002698
Gilles Peskinea1cac842018-06-11 19:33:02 +02002699exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002700 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002701 mbedtls_free( output_data );
2702 mbedtls_free( output_data2 );
2703 mbedtls_psa_crypto_free( );
2704}
2705/* END_CASE */
2706
2707/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002708void aead_encrypt( int key_type_arg, data_t *key_data,
2709 int alg_arg,
2710 data_t *nonce,
2711 data_t *additional_data,
2712 data_t *input_data,
2713 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002714{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002715 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002716 psa_key_type_t key_type = key_type_arg;
2717 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002718 unsigned char *output_data = NULL;
2719 size_t output_size = 0;
2720 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002721 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002722 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002723
Gilles Peskine4abf7412018-06-18 16:35:34 +02002724 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002725 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002726
Gilles Peskine8817f612018-12-18 00:18:46 +01002727 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002728
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002729 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002730 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002731 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002732
Gilles Peskine8817f612018-12-18 00:18:46 +01002733 PSA_ASSERT( psa_import_key( handle, key_type,
2734 key_data->x,
2735 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002736
Gilles Peskine8817f612018-12-18 00:18:46 +01002737 PSA_ASSERT( psa_aead_encrypt( handle, alg,
2738 nonce->x, nonce->len,
2739 additional_data->x, additional_data->len,
2740 input_data->x, input_data->len,
2741 output_data, output_size,
2742 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002743
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002744 ASSERT_COMPARE( expected_result->x, expected_result->len,
2745 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002746
Gilles Peskinea1cac842018-06-11 19:33:02 +02002747exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002748 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002749 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002750 mbedtls_psa_crypto_free( );
2751}
2752/* END_CASE */
2753
2754/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002755void aead_decrypt( int key_type_arg, data_t *key_data,
2756 int alg_arg,
2757 data_t *nonce,
2758 data_t *additional_data,
2759 data_t *input_data,
2760 data_t *expected_data,
2761 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002762{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002763 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002764 psa_key_type_t key_type = key_type_arg;
2765 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002766 unsigned char *output_data = NULL;
2767 size_t output_size = 0;
2768 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002769 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002770 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002771 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002772
Gilles Peskine4abf7412018-06-18 16:35:34 +02002773 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002774 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002775
Gilles Peskine8817f612018-12-18 00:18:46 +01002776 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002777
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002778 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002779 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002780 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002781
Gilles Peskine8817f612018-12-18 00:18:46 +01002782 PSA_ASSERT( psa_import_key( handle, key_type,
2783 key_data->x,
2784 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002785
Gilles Peskinefe11b722018-12-18 00:24:04 +01002786 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2787 nonce->x, nonce->len,
2788 additional_data->x,
2789 additional_data->len,
2790 input_data->x, input_data->len,
2791 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002792 &output_length ),
2793 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002794
Gilles Peskine2d277862018-06-18 15:41:12 +02002795 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002796 ASSERT_COMPARE( expected_data->x, expected_data->len,
2797 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002798
Gilles Peskinea1cac842018-06-11 19:33:02 +02002799exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002800 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002801 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002802 mbedtls_psa_crypto_free( );
2803}
2804/* END_CASE */
2805
2806/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002807void signature_size( int type_arg,
2808 int bits,
2809 int alg_arg,
2810 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002811{
2812 psa_key_type_t type = type_arg;
2813 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002814 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002815 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002816exit:
2817 ;
2818}
2819/* END_CASE */
2820
2821/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002822void sign_deterministic( int key_type_arg, data_t *key_data,
2823 int alg_arg, data_t *input_data,
2824 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002825{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002826 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01002827 psa_key_type_t key_type = key_type_arg;
2828 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002829 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002830 unsigned char *signature = NULL;
2831 size_t signature_size;
2832 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002833 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01002834
Gilles Peskine8817f612018-12-18 00:18:46 +01002835 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002836
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002837 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002838 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002839 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002840
Gilles Peskine8817f612018-12-18 00:18:46 +01002841 PSA_ASSERT( psa_import_key( handle, key_type,
2842 key_data->x,
2843 key_data->len ) );
2844 PSA_ASSERT( psa_get_key_information( handle,
2845 NULL,
2846 &key_bits ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002847
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002848 /* Allocate a buffer which has the size advertized by the
2849 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002850 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2851 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002852 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002853 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002854 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002855
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002856 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002857 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2858 input_data->x, input_data->len,
2859 signature, signature_size,
2860 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002861 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002862 ASSERT_COMPARE( output_data->x, output_data->len,
2863 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002864
2865exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002866 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01002867 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002868 mbedtls_psa_crypto_free( );
2869}
2870/* END_CASE */
2871
2872/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002873void sign_fail( int key_type_arg, data_t *key_data,
2874 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002875 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002876{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002877 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01002878 psa_key_type_t key_type = key_type_arg;
2879 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002880 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002881 psa_status_t actual_status;
2882 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002883 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002884 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002885 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01002886
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002887 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002888
Gilles Peskine8817f612018-12-18 00:18:46 +01002889 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002890
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002891 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002892 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002893 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002894
Gilles Peskine8817f612018-12-18 00:18:46 +01002895 PSA_ASSERT( psa_import_key( handle, key_type,
2896 key_data->x,
2897 key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002898
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002899 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002900 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002901 signature, signature_size,
2902 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002903 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002904 /* The value of *signature_length is unspecified on error, but
2905 * whatever it is, it should be less than signature_size, so that
2906 * if the caller tries to read *signature_length bytes without
2907 * checking the error code then they don't overflow a buffer. */
2908 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002909
2910exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002911 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01002912 mbedtls_free( signature );
2913 mbedtls_psa_crypto_free( );
2914}
2915/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002916
2917/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002918void sign_verify( int key_type_arg, data_t *key_data,
2919 int alg_arg, data_t *input_data )
2920{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002921 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02002922 psa_key_type_t key_type = key_type_arg;
2923 psa_algorithm_t alg = alg_arg;
2924 size_t key_bits;
2925 unsigned char *signature = NULL;
2926 size_t signature_size;
2927 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002928 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02002929
Gilles Peskine8817f612018-12-18 00:18:46 +01002930 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002931
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002932 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002933 psa_key_policy_set_usage( &policy,
2934 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2935 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002936 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002937
Gilles Peskine8817f612018-12-18 00:18:46 +01002938 PSA_ASSERT( psa_import_key( handle, key_type,
2939 key_data->x,
2940 key_data->len ) );
2941 PSA_ASSERT( psa_get_key_information( handle,
2942 NULL,
2943 &key_bits ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002944
2945 /* Allocate a buffer which has the size advertized by the
2946 * library. */
2947 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2948 key_bits, alg );
2949 TEST_ASSERT( signature_size != 0 );
2950 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002951 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002952
2953 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002954 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2955 input_data->x, input_data->len,
2956 signature, signature_size,
2957 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002958 /* Check that the signature length looks sensible. */
2959 TEST_ASSERT( signature_length <= signature_size );
2960 TEST_ASSERT( signature_length > 0 );
2961
2962 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002963 PSA_ASSERT( psa_asymmetric_verify(
2964 handle, alg,
2965 input_data->x, input_data->len,
2966 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002967
2968 if( input_data->len != 0 )
2969 {
2970 /* Flip a bit in the input and verify that the signature is now
2971 * detected as invalid. Flip a bit at the beginning, not at the end,
2972 * because ECDSA may ignore the last few bits of the input. */
2973 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002974 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
2975 input_data->x, input_data->len,
2976 signature, signature_length ),
2977 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02002978 }
2979
2980exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002981 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02002982 mbedtls_free( signature );
2983 mbedtls_psa_crypto_free( );
2984}
2985/* END_CASE */
2986
2987/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002988void asymmetric_verify( int key_type_arg, data_t *key_data,
2989 int alg_arg, data_t *hash_data,
2990 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002991{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002992 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03002993 psa_key_type_t key_type = key_type_arg;
2994 psa_algorithm_t alg = alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002995 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03002996
Gilles Peskine69c12672018-06-28 00:07:19 +02002997 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2998
Gilles Peskine8817f612018-12-18 00:18:46 +01002999 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003000
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003001 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003002 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003003 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
itayzafrir5c753392018-05-08 11:18:38 +03003004
Gilles Peskine8817f612018-12-18 00:18:46 +01003005 PSA_ASSERT( psa_import_key( handle, key_type,
3006 key_data->x,
3007 key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003008
Gilles Peskine8817f612018-12-18 00:18:46 +01003009 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3010 hash_data->x, hash_data->len,
3011 signature_data->x,
3012 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003013exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003014 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03003015 mbedtls_psa_crypto_free( );
3016}
3017/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003018
3019/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003020void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3021 int alg_arg, data_t *hash_data,
3022 data_t *signature_data,
3023 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003024{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003025 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003026 psa_key_type_t key_type = key_type_arg;
3027 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003028 psa_status_t actual_status;
3029 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003030 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003031
Gilles Peskine8817f612018-12-18 00:18:46 +01003032 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003033
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003034 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003035 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003036 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003037
Gilles Peskine8817f612018-12-18 00:18:46 +01003038 PSA_ASSERT( psa_import_key( handle, key_type,
3039 key_data->x,
3040 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003041
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003042 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003043 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003044 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003045 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003046
Gilles Peskinefe11b722018-12-18 00:24:04 +01003047 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003048
3049exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003050 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003051 mbedtls_psa_crypto_free( );
3052}
3053/* END_CASE */
3054
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003055/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003056void asymmetric_encrypt( int key_type_arg,
3057 data_t *key_data,
3058 int alg_arg,
3059 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003060 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003061 int expected_output_length_arg,
3062 int expected_status_arg )
3063{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003064 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003065 psa_key_type_t key_type = key_type_arg;
3066 psa_algorithm_t alg = alg_arg;
3067 size_t expected_output_length = expected_output_length_arg;
3068 size_t key_bits;
3069 unsigned char *output = NULL;
3070 size_t output_size;
3071 size_t output_length = ~0;
3072 psa_status_t actual_status;
3073 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003074 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003075
Gilles Peskine8817f612018-12-18 00:18:46 +01003076 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003077
Gilles Peskine656896e2018-06-29 19:12:28 +02003078 /* Import the key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003079 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003080 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003081 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
3082 PSA_ASSERT( psa_import_key( handle, key_type,
3083 key_data->x,
3084 key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003085
3086 /* Determine the maximum output length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003087 PSA_ASSERT( psa_get_key_information( handle,
3088 NULL,
3089 &key_bits ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003090 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003091 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003092
3093 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003094 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003095 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003096 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003097 output, output_size,
3098 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003099 TEST_EQUAL( actual_status, expected_status );
3100 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003101
Gilles Peskine68428122018-06-30 18:42:41 +02003102 /* If the label is empty, the test framework puts a non-null pointer
3103 * in label->x. Test that a null pointer works as well. */
3104 if( label->len == 0 )
3105 {
3106 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003107 if( output_size != 0 )
3108 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003109 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003110 input_data->x, input_data->len,
3111 NULL, label->len,
3112 output, output_size,
3113 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003114 TEST_EQUAL( actual_status, expected_status );
3115 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003116 }
3117
Gilles Peskine656896e2018-06-29 19:12:28 +02003118exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003119 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003120 mbedtls_free( output );
3121 mbedtls_psa_crypto_free( );
3122}
3123/* END_CASE */
3124
3125/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003126void asymmetric_encrypt_decrypt( int key_type_arg,
3127 data_t *key_data,
3128 int alg_arg,
3129 data_t *input_data,
3130 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003131{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003132 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003133 psa_key_type_t key_type = key_type_arg;
3134 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003135 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003136 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003137 size_t output_size;
3138 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003139 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003140 size_t output2_size;
3141 size_t output2_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003142 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003143
Gilles Peskine8817f612018-12-18 00:18:46 +01003144 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003145
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003146 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003147 psa_key_policy_set_usage( &policy,
3148 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003149 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003150 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003151
Gilles Peskine8817f612018-12-18 00:18:46 +01003152 PSA_ASSERT( psa_import_key( handle, key_type,
3153 key_data->x,
3154 key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003155
3156 /* Determine the maximum ciphertext length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003157 PSA_ASSERT( psa_get_key_information( handle,
3158 NULL,
3159 &key_bits ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003160 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003161 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003162 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003163 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003164
Gilles Peskineeebd7382018-06-08 18:11:54 +02003165 /* We test encryption by checking that encrypt-then-decrypt gives back
3166 * the original plaintext because of the non-optional random
3167 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003168 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3169 input_data->x, input_data->len,
3170 label->x, label->len,
3171 output, output_size,
3172 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003173 /* We don't know what ciphertext length to expect, but check that
3174 * it looks sensible. */
3175 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003176
Gilles Peskine8817f612018-12-18 00:18:46 +01003177 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3178 output, output_length,
3179 label->x, label->len,
3180 output2, output2_size,
3181 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003182 ASSERT_COMPARE( input_data->x, input_data->len,
3183 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003184
3185exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003186 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003187 mbedtls_free( output );
3188 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003189 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003190}
3191/* END_CASE */
3192
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003193/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003194void asymmetric_decrypt( int key_type_arg,
3195 data_t *key_data,
3196 int alg_arg,
3197 data_t *input_data,
3198 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003199 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003200{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003201 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003202 psa_key_type_t key_type = key_type_arg;
3203 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003204 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003205 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003206 size_t output_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003207 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003208
Jaeden Amero412654a2019-02-06 12:57:46 +00003209 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003210 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003211
Gilles Peskine8817f612018-12-18 00:18:46 +01003212 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003213
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003214 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003215 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003216 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003217
Gilles Peskine8817f612018-12-18 00:18:46 +01003218 PSA_ASSERT( psa_import_key( handle, key_type,
3219 key_data->x,
3220 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003221
Gilles Peskine8817f612018-12-18 00:18:46 +01003222 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3223 input_data->x, input_data->len,
3224 label->x, label->len,
3225 output,
3226 output_size,
3227 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003228 ASSERT_COMPARE( expected_data->x, expected_data->len,
3229 output, output_length );
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 Peskine8817f612018-12-18 00:18:46 +01003238 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3239 input_data->x, input_data->len,
3240 NULL, label->len,
3241 output,
3242 output_size,
3243 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003244 ASSERT_COMPARE( expected_data->x, expected_data->len,
3245 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003246 }
3247
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003248exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003249 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003250 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003251 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003252}
3253/* END_CASE */
3254
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003255/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003256void asymmetric_decrypt_fail( int key_type_arg,
3257 data_t *key_data,
3258 int alg_arg,
3259 data_t *input_data,
3260 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00003261 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02003262 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003263{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003264 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003265 psa_key_type_t key_type = key_type_arg;
3266 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003267 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00003268 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003269 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003270 psa_status_t actual_status;
3271 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003272 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003273
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003274 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003275
Gilles Peskine8817f612018-12-18 00:18:46 +01003276 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003277
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003278 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003279 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003280 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003281
Gilles Peskine8817f612018-12-18 00:18:46 +01003282 PSA_ASSERT( psa_import_key( handle, key_type,
3283 key_data->x,
3284 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003285
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003286 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003287 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003288 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003289 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003290 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003291 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003292 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003293
Gilles Peskine68428122018-06-30 18:42:41 +02003294 /* If the label is empty, the test framework puts a non-null pointer
3295 * in label->x. Test that a null pointer works as well. */
3296 if( label->len == 0 )
3297 {
3298 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003299 if( output_size != 0 )
3300 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003301 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003302 input_data->x, input_data->len,
3303 NULL, label->len,
3304 output, output_size,
3305 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003306 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003307 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003308 }
3309
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003310exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003311 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02003312 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003313 mbedtls_psa_crypto_free( );
3314}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003315/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003316
3317/* BEGIN_CASE */
Jaeden Amerod94d6712019-01-04 14:11:48 +00003318void crypto_generator_init( )
3319{
3320 /* Test each valid way of initializing the object, except for `= {0}`, as
3321 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3322 * though it's OK by the C standard. We could test for this, but we'd need
3323 * to supress the Clang warning for the test. */
3324 psa_crypto_generator_t func = psa_crypto_generator_init( );
3325 psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT;
3326 psa_crypto_generator_t zero;
3327
3328 memset( &zero, 0, sizeof( zero ) );
3329
3330 /* Although not technically guaranteed by the C standard nor the PSA Crypto
3331 * specification, we test that all valid ways of initializing the object
3332 * have the same bit pattern. This is a stronger requirement that may not
3333 * be valid on all platforms or PSA Crypto implementations, but implies the
3334 * weaker actual requirement is met: that a freshly initialized object, no
3335 * matter how it was initialized, acts the same as any other valid
3336 * initialization. */
3337 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
3338 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
3339}
3340/* END_CASE */
3341
3342/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003343void derive_setup( int key_type_arg,
3344 data_t *key_data,
3345 int alg_arg,
3346 data_t *salt,
3347 data_t *label,
3348 int requested_capacity_arg,
3349 int expected_status_arg )
3350{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003351 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003352 size_t key_type = key_type_arg;
3353 psa_algorithm_t alg = alg_arg;
3354 size_t requested_capacity = requested_capacity_arg;
3355 psa_status_t expected_status = expected_status_arg;
3356 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003357 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003358
Gilles Peskine8817f612018-12-18 00:18:46 +01003359 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003360
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003361 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003362 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003363 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003364
Gilles Peskine8817f612018-12-18 00:18:46 +01003365 PSA_ASSERT( psa_import_key( handle, key_type,
3366 key_data->x,
3367 key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003368
Gilles Peskinefe11b722018-12-18 00:24:04 +01003369 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3370 salt->x, salt->len,
3371 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003372 requested_capacity ),
3373 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003374
3375exit:
3376 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003377 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003378 mbedtls_psa_crypto_free( );
3379}
3380/* END_CASE */
3381
3382/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003383void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003384{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003385 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003386 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003387 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003388 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003389 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003390 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003391 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3392 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3393 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Jaeden Amero70261c52019-01-04 11:47:20 +00003394 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003395
Gilles Peskine8817f612018-12-18 00:18:46 +01003396 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003397
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003398 PSA_ASSERT( psa_allocate_key( &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003399 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003400 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003401
Gilles Peskine8817f612018-12-18 00:18:46 +01003402 PSA_ASSERT( psa_import_key( handle, key_type,
3403 key_data,
3404 sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003405
3406 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01003407 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3408 NULL, 0,
3409 NULL, 0,
3410 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003411
3412 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003413 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3414 NULL, 0,
3415 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003416 capacity ),
3417 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003418
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003419 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003420
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003421 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
3422 PSA_ERROR_INSUFFICIENT_CAPACITY );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003423
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003424exit:
3425 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003426 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003427 mbedtls_psa_crypto_free( );
3428}
3429/* END_CASE */
3430
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003431/* BEGIN_CASE */
3432void test_derive_invalid_generator_tests( )
3433{
3434 uint8_t output_buffer[16];
3435 size_t buffer_size = 16;
3436 size_t capacity = 0;
3437 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3438
Nir Sonnenschein50789302018-10-31 12:16:38 +02003439 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003440 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003441
3442 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003443 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003444
Gilles Peskine8817f612018-12-18 00:18:46 +01003445 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003446
Nir Sonnenschein50789302018-10-31 12:16:38 +02003447 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003448 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003449
Nir Sonnenschein50789302018-10-31 12:16:38 +02003450 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003451 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003452
3453exit:
3454 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003455}
3456/* END_CASE */
3457
3458/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003459void derive_output( int alg_arg,
3460 data_t *key_data,
3461 data_t *salt,
3462 data_t *label,
3463 int requested_capacity_arg,
3464 data_t *expected_output1,
3465 data_t *expected_output2 )
3466{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003467 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003468 psa_algorithm_t alg = alg_arg;
3469 size_t requested_capacity = requested_capacity_arg;
3470 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3471 uint8_t *expected_outputs[2] =
3472 {expected_output1->x, expected_output2->x};
3473 size_t output_sizes[2] =
3474 {expected_output1->len, expected_output2->len};
3475 size_t output_buffer_size = 0;
3476 uint8_t *output_buffer = NULL;
3477 size_t expected_capacity;
3478 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003479 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003480 psa_status_t status;
3481 unsigned i;
3482
3483 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3484 {
3485 if( output_sizes[i] > output_buffer_size )
3486 output_buffer_size = output_sizes[i];
3487 if( output_sizes[i] == 0 )
3488 expected_outputs[i] = NULL;
3489 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003490 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003491 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003492
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003493 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003494 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003495 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003496
Gilles Peskine8817f612018-12-18 00:18:46 +01003497 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3498 key_data->x,
3499 key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003500
3501 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003502 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3503 salt->x, salt->len,
3504 label->x, label->len,
3505 requested_capacity ) );
3506 PSA_ASSERT( psa_get_generator_capacity( &generator,
3507 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003508 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003509 expected_capacity = requested_capacity;
3510
3511 /* Expansion phase. */
3512 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3513 {
3514 /* Read some bytes. */
3515 status = psa_generator_read( &generator,
3516 output_buffer, output_sizes[i] );
3517 if( expected_capacity == 0 && output_sizes[i] == 0 )
3518 {
3519 /* Reading 0 bytes when 0 bytes are available can go either way. */
3520 TEST_ASSERT( status == PSA_SUCCESS ||
3521 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3522 continue;
3523 }
3524 else if( expected_capacity == 0 ||
3525 output_sizes[i] > expected_capacity )
3526 {
3527 /* Capacity exceeded. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003528 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003529 expected_capacity = 0;
3530 continue;
3531 }
3532 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003533 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003534 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003535 ASSERT_COMPARE( output_buffer, output_sizes[i],
3536 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003537 /* Check the generator status. */
3538 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01003539 PSA_ASSERT( psa_get_generator_capacity( &generator,
3540 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003541 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003542 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003543 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003544
3545exit:
3546 mbedtls_free( output_buffer );
3547 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003548 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003549 mbedtls_psa_crypto_free( );
3550}
3551/* END_CASE */
3552
3553/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003554void derive_full( int alg_arg,
3555 data_t *key_data,
3556 data_t *salt,
3557 data_t *label,
3558 int requested_capacity_arg )
3559{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003560 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02003561 psa_algorithm_t alg = alg_arg;
3562 size_t requested_capacity = requested_capacity_arg;
3563 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3564 unsigned char output_buffer[16];
3565 size_t expected_capacity = requested_capacity;
3566 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003567 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02003568
Gilles Peskine8817f612018-12-18 00:18:46 +01003569 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003570
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003571 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003572 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003573 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003574
Gilles Peskine8817f612018-12-18 00:18:46 +01003575 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3576 key_data->x,
3577 key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003578
3579 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003580 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3581 salt->x, salt->len,
3582 label->x, label->len,
3583 requested_capacity ) );
3584 PSA_ASSERT( psa_get_generator_capacity( &generator,
3585 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003586 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003587
3588 /* Expansion phase. */
3589 while( current_capacity > 0 )
3590 {
3591 size_t read_size = sizeof( output_buffer );
3592 if( read_size > current_capacity )
3593 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01003594 PSA_ASSERT( psa_generator_read( &generator,
3595 output_buffer,
3596 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003597 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01003598 PSA_ASSERT( psa_get_generator_capacity( &generator,
3599 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003600 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003601 }
3602
3603 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003604 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
3605 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskined54931c2018-07-17 21:06:59 +02003606
Gilles Peskine8817f612018-12-18 00:18:46 +01003607 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003608
3609exit:
3610 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003611 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02003612 mbedtls_psa_crypto_free( );
3613}
3614/* END_CASE */
3615
3616/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003617void derive_key_exercise( int alg_arg,
3618 data_t *key_data,
3619 data_t *salt,
3620 data_t *label,
3621 int derived_type_arg,
3622 int derived_bits_arg,
3623 int derived_usage_arg,
3624 int derived_alg_arg )
3625{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003626 psa_key_handle_t base_handle = 0;
3627 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003628 psa_algorithm_t alg = alg_arg;
3629 psa_key_type_t derived_type = derived_type_arg;
3630 size_t derived_bits = derived_bits_arg;
3631 psa_key_usage_t derived_usage = derived_usage_arg;
3632 psa_algorithm_t derived_alg = derived_alg_arg;
3633 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3634 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
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 psa_key_type_t got_type;
3637 size_t got_bits;
3638
Gilles Peskine8817f612018-12-18 00:18:46 +01003639 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003640
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003641 PSA_ASSERT( psa_allocate_key( &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003642 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003643 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3644 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3645 key_data->x,
3646 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003647
3648 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003649 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3650 salt->x, salt->len,
3651 label->x, label->len,
3652 capacity ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003653 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003654 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003655 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3656 PSA_ASSERT( psa_generator_import_key( derived_handle,
3657 derived_type,
3658 derived_bits,
3659 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003660
3661 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01003662 PSA_ASSERT( psa_get_key_information( derived_handle,
3663 &got_type,
3664 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003665 TEST_EQUAL( got_type, derived_type );
3666 TEST_EQUAL( got_bits, derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003667
3668 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003669 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02003670 goto exit;
3671
3672exit:
3673 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003674 psa_destroy_key( base_handle );
3675 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003676 mbedtls_psa_crypto_free( );
3677}
3678/* END_CASE */
3679
3680/* BEGIN_CASE */
3681void derive_key_export( int alg_arg,
3682 data_t *key_data,
3683 data_t *salt,
3684 data_t *label,
3685 int bytes1_arg,
3686 int bytes2_arg )
3687{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003688 psa_key_handle_t base_handle = 0;
3689 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003690 psa_algorithm_t alg = alg_arg;
3691 size_t bytes1 = bytes1_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003692 size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003693 size_t bytes2 = bytes2_arg;
3694 size_t capacity = bytes1 + bytes2;
3695 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003696 uint8_t *output_buffer = NULL;
3697 uint8_t *export_buffer = NULL;
Jaeden Amero70261c52019-01-04 11:47:20 +00003698 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003699 size_t length;
3700
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003701 ASSERT_ALLOC( output_buffer, capacity );
3702 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01003703 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003704
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003705 PSA_ASSERT( psa_allocate_key( &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003706 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003707 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3708 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3709 key_data->x,
3710 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003711
3712 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003713 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3714 salt->x, salt->len,
3715 label->x, label->len,
3716 capacity ) );
3717 PSA_ASSERT( psa_generator_read( &generator,
3718 output_buffer,
3719 capacity ) );
3720 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003721
3722 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003723 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3724 salt->x, salt->len,
3725 label->x, label->len,
3726 capacity ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003727 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003728 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003729 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3730 PSA_ASSERT( psa_generator_import_key( derived_handle,
3731 PSA_KEY_TYPE_RAW_DATA,
3732 derived_bits,
3733 &generator ) );
3734 PSA_ASSERT( psa_export_key( derived_handle,
3735 export_buffer, bytes1,
3736 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003737 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003738 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003739 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003740 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3741 PSA_ASSERT( psa_generator_import_key( derived_handle,
3742 PSA_KEY_TYPE_RAW_DATA,
3743 PSA_BYTES_TO_BITS( bytes2 ),
3744 &generator ) );
3745 PSA_ASSERT( psa_export_key( derived_handle,
3746 export_buffer + bytes1, bytes2,
3747 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003748 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003749
3750 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003751 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
3752 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003753
3754exit:
3755 mbedtls_free( output_buffer );
3756 mbedtls_free( export_buffer );
3757 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003758 psa_destroy_key( base_handle );
3759 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003760 mbedtls_psa_crypto_free( );
3761}
3762/* END_CASE */
3763
3764/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02003765void key_agreement_setup( int alg_arg,
3766 int our_key_type_arg, data_t *our_key_data,
3767 data_t *peer_key_data,
3768 int expected_status_arg )
3769{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003770 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003771 psa_algorithm_t alg = alg_arg;
3772 psa_key_type_t our_key_type = our_key_type_arg;
3773 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003774 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003775
Gilles Peskine8817f612018-12-18 00:18:46 +01003776 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003777
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003778 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003779 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003780 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3781 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3782 our_key_data->x,
3783 our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003784
Gilles Peskinefe11b722018-12-18 00:24:04 +01003785 TEST_EQUAL( psa_key_agreement( &generator,
3786 our_key,
3787 peer_key_data->x, peer_key_data->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003788 alg ),
3789 expected_status_arg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003790
3791exit:
3792 psa_generator_abort( &generator );
3793 psa_destroy_key( our_key );
3794 mbedtls_psa_crypto_free( );
3795}
3796/* END_CASE */
3797
3798/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02003799void key_agreement_capacity( int alg_arg,
3800 int our_key_type_arg, data_t *our_key_data,
3801 data_t *peer_key_data,
3802 int expected_capacity_arg )
3803{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003804 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003805 psa_algorithm_t alg = alg_arg;
3806 psa_key_type_t our_key_type = our_key_type_arg;
3807 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003808 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02003809 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02003810 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02003811
Gilles Peskine8817f612018-12-18 00:18:46 +01003812 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003813
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003814 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003815 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003816 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3817 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3818 our_key_data->x,
3819 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003820
Gilles Peskine8817f612018-12-18 00:18:46 +01003821 PSA_ASSERT( psa_key_agreement( &generator,
3822 our_key,
3823 peer_key_data->x, peer_key_data->len,
3824 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003825
Gilles Peskinebf491972018-10-25 22:36:12 +02003826 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003827 PSA_ASSERT( psa_get_generator_capacity(
3828 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003829 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02003830
Gilles Peskinebf491972018-10-25 22:36:12 +02003831 /* Test the actual capacity by reading the output. */
3832 while( actual_capacity > sizeof( output ) )
3833 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003834 PSA_ASSERT( psa_generator_read( &generator,
3835 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02003836 actual_capacity -= sizeof( output );
3837 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003838 PSA_ASSERT( psa_generator_read( &generator,
3839 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003840 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
3841 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskinebf491972018-10-25 22:36:12 +02003842
Gilles Peskine59685592018-09-18 12:11:34 +02003843exit:
3844 psa_generator_abort( &generator );
3845 psa_destroy_key( our_key );
3846 mbedtls_psa_crypto_free( );
3847}
3848/* END_CASE */
3849
3850/* BEGIN_CASE */
3851void key_agreement_output( int alg_arg,
3852 int our_key_type_arg, data_t *our_key_data,
3853 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003854 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02003855{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003856 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003857 psa_algorithm_t alg = alg_arg;
3858 psa_key_type_t our_key_type = our_key_type_arg;
3859 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003860 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003861 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02003862
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003863 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
3864 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003865
Gilles Peskine8817f612018-12-18 00:18:46 +01003866 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003867
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003868 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003869 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003870 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3871 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3872 our_key_data->x,
3873 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003874
Gilles Peskine8817f612018-12-18 00:18:46 +01003875 PSA_ASSERT( psa_key_agreement( &generator,
3876 our_key,
3877 peer_key_data->x, peer_key_data->len,
3878 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003879
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003880 PSA_ASSERT( psa_generator_read( &generator,
3881 actual_output,
3882 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003883 ASSERT_COMPARE( actual_output, expected_output1->len,
3884 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003885 if( expected_output2->len != 0 )
3886 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003887 PSA_ASSERT( psa_generator_read( &generator,
3888 actual_output,
3889 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003890 ASSERT_COMPARE( actual_output, expected_output2->len,
3891 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003892 }
Gilles Peskine59685592018-09-18 12:11:34 +02003893
3894exit:
3895 psa_generator_abort( &generator );
3896 psa_destroy_key( our_key );
3897 mbedtls_psa_crypto_free( );
3898 mbedtls_free( actual_output );
3899}
3900/* END_CASE */
3901
3902/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003903void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003904{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003905 size_t bytes = bytes_arg;
3906 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003907 unsigned char *output = NULL;
3908 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003909 size_t i;
3910 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003911
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003912 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3913 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003914 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003915
Gilles Peskine8817f612018-12-18 00:18:46 +01003916 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003917
Gilles Peskinea50d7392018-06-21 10:22:13 +02003918 /* Run several times, to ensure that every output byte will be
3919 * nonzero at least once with overwhelming probability
3920 * (2^(-8*number_of_runs)). */
3921 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003922 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003923 if( bytes != 0 )
3924 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01003925 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003926
3927 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003928 ASSERT_COMPARE( output + bytes, sizeof( trail ),
3929 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003930
3931 for( i = 0; i < bytes; i++ )
3932 {
3933 if( output[i] != 0 )
3934 ++changed[i];
3935 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003936 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003937
3938 /* Check that every byte was changed to nonzero at least once. This
3939 * validates that psa_generate_random is overwriting every byte of
3940 * the output buffer. */
3941 for( i = 0; i < bytes; i++ )
3942 {
3943 TEST_ASSERT( changed[i] != 0 );
3944 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003945
3946exit:
3947 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003948 mbedtls_free( output );
3949 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003950}
3951/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003952
3953/* BEGIN_CASE */
3954void generate_key( int type_arg,
3955 int bits_arg,
3956 int usage_arg,
3957 int alg_arg,
3958 int expected_status_arg )
3959{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003960 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003961 psa_key_type_t type = type_arg;
3962 psa_key_usage_t usage = usage_arg;
3963 size_t bits = bits_arg;
3964 psa_algorithm_t alg = alg_arg;
3965 psa_status_t expected_status = expected_status_arg;
3966 psa_key_type_t got_type;
3967 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003968 psa_status_t expected_info_status =
3969 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003970 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003971
Gilles Peskine8817f612018-12-18 00:18:46 +01003972 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003973
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003974 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003975 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003976 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003977
3978 /* Generate a key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003979 TEST_EQUAL( psa_generate_key( handle, type, bits, NULL, 0 ),
3980 expected_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003981
3982 /* Test the key information */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003983 TEST_EQUAL( psa_get_key_information( handle, &got_type, &got_bits ),
3984 expected_info_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003985 if( expected_info_status != PSA_SUCCESS )
3986 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003987 TEST_EQUAL( got_type, type );
3988 TEST_EQUAL( got_bits, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003989
Gilles Peskine818ca122018-06-20 18:16:48 +02003990 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003991 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02003992 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003993
3994exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003995 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003996 mbedtls_psa_crypto_free( );
3997}
3998/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03003999
Darryl Greend49a4992018-06-18 17:27:26 +01004000/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
4001void persistent_key_load_key_from_storage( data_t *data, int type_arg,
4002 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00004003 int alg_arg, int generation_method,
4004 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01004005{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004006 psa_key_handle_t handle = 0;
4007 psa_key_handle_t base_key;
Darryl Greend49a4992018-06-18 17:27:26 +01004008 psa_key_type_t type = (psa_key_type_t) type_arg;
4009 psa_key_type_t type_get;
4010 size_t bits_get;
Jaeden Amero70261c52019-01-04 11:47:20 +00004011 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
4012 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004013 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
4014 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00004015 psa_key_policy_t base_policy_set = PSA_KEY_POLICY_INIT;
Darryl Green0c6575a2018-11-07 16:05:30 +00004016 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
4017 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004018 unsigned char *first_export = NULL;
4019 unsigned char *second_export = NULL;
4020 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4021 size_t first_exported_length;
4022 size_t second_exported_length;
4023
4024 ASSERT_ALLOC( first_export, export_size );
4025 ASSERT_ALLOC( second_export, export_size );
4026
Gilles Peskine8817f612018-12-18 00:18:46 +01004027 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004028
Gilles Peskine8817f612018-12-18 00:18:46 +01004029 PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
Gilles Peskine8817f612018-12-18 00:18:46 +01004030 &handle ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004031 psa_key_policy_set_usage( &policy_set, policy_usage,
4032 policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004033 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004034
Darryl Green0c6575a2018-11-07 16:05:30 +00004035 switch( generation_method )
4036 {
4037 case IMPORT_KEY:
4038 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004039 PSA_ASSERT( psa_import_key( handle, type,
4040 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004041 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004042
Darryl Green0c6575a2018-11-07 16:05:30 +00004043 case GENERATE_KEY:
4044 /* Generate a key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004045 PSA_ASSERT( psa_generate_key( handle, type, bits,
4046 NULL, 0 ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004047 break;
4048
4049 case DERIVE_KEY:
4050 /* Create base key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004051 PSA_ASSERT( psa_allocate_key( &base_key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004052 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
4053 base_policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004054 PSA_ASSERT( psa_set_key_policy(
4055 base_key, &base_policy_set ) );
4056 PSA_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
4057 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004058 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004059 PSA_ASSERT( psa_key_derivation( &generator, base_key,
4060 base_policy_alg,
4061 NULL, 0, NULL, 0,
4062 export_size ) );
4063 PSA_ASSERT( psa_generator_import_key(
4064 handle, PSA_KEY_TYPE_RAW_DATA,
4065 bits, &generator ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004066 break;
4067 }
Darryl Greend49a4992018-06-18 17:27:26 +01004068
4069 /* Export the key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004070 TEST_EQUAL( psa_export_key( handle,
4071 first_export, export_size,
4072 &first_exported_length ),
4073 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004074
4075 /* Shutdown and restart */
4076 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004077 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004078
Darryl Greend49a4992018-06-18 17:27:26 +01004079 /* Check key slot still contains key data */
Gilles Peskine8817f612018-12-18 00:18:46 +01004080 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4081 &handle ) );
4082 PSA_ASSERT( psa_get_key_information(
4083 handle, &type_get, &bits_get ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004084 TEST_EQUAL( type_get, type );
4085 TEST_EQUAL( bits_get, (size_t) bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004086
Gilles Peskine8817f612018-12-18 00:18:46 +01004087 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004088 TEST_EQUAL( psa_key_policy_get_usage( &policy_get ), policy_usage );
4089 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_get ), policy_alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004090
4091 /* Export the key again */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004092 TEST_EQUAL( psa_export_key( handle,
4093 second_export, export_size,
4094 &second_exported_length ),
4095 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004096
Darryl Green0c6575a2018-11-07 16:05:30 +00004097 if( export_status == PSA_SUCCESS )
4098 {
4099 ASSERT_COMPARE( first_export, first_exported_length,
4100 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01004101
Darryl Green0c6575a2018-11-07 16:05:30 +00004102 switch( generation_method )
4103 {
4104 case IMPORT_KEY:
4105 ASSERT_COMPARE( data->x, data->len,
4106 first_export, first_exported_length );
4107 break;
4108 default:
4109 break;
4110 }
4111 }
4112
4113 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004114 if( ! exercise_key( handle, policy_usage, policy_alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004115 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004116
4117exit:
4118 mbedtls_free( first_export );
4119 mbedtls_free( second_export );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004120 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004121 mbedtls_psa_crypto_free();
4122}
4123/* END_CASE */