blob: 3865d9007d113c083db5bf22c398bcf410575807 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
4#if defined(MBEDTLS_PSA_CRYPTO_SPM)
5#include "spm/psa_defs.h"
6#endif
7
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02009#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +020010#include "mbedtls/oid.h"
11
Gilles Peskinee59236f2018-01-27 23:32:46 +010012#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030013
Jaeden Amerof24c7f82018-06-27 17:20:43 +010014/** An invalid export length that will never be set by psa_export_key(). */
15static const size_t INVALID_EXPORT_LENGTH = ~0U;
16
Gilles Peskinea7aa4422018-08-14 15:17:54 +020017/** Test if a buffer contains a constant byte value.
18 *
19 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020020 *
21 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020022 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020023 * \param size Size of the buffer in bytes.
24 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020025 * \return 1 if the buffer is all-bits-zero.
26 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020027 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020028static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020029{
30 size_t i;
31 for( i = 0; i < size; i++ )
32 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020033 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020034 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020035 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020036 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037}
Gilles Peskine818ca122018-06-20 18:16:48 +020038
Gilles Peskine0b352bc2018-06-28 00:16:11 +020039/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
40static int asn1_write_10x( unsigned char **p,
41 unsigned char *start,
42 size_t bits,
43 unsigned char x )
44{
45 int ret;
46 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020047 if( bits == 0 )
48 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
49 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020050 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030051 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020052 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
53 *p -= len;
54 ( *p )[len-1] = x;
55 if( bits % 8 == 0 )
56 ( *p )[1] |= 1;
57 else
58 ( *p )[0] |= 1 << ( bits % 8 );
59 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
60 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
61 MBEDTLS_ASN1_INTEGER ) );
62 return( len );
63}
64
65static int construct_fake_rsa_key( unsigned char *buffer,
66 size_t buffer_size,
67 unsigned char **p,
68 size_t bits,
69 int keypair )
70{
71 size_t half_bits = ( bits + 1 ) / 2;
72 int ret;
73 int len = 0;
74 /* Construct something that looks like a DER encoding of
75 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
76 * RSAPrivateKey ::= SEQUENCE {
77 * version Version,
78 * modulus INTEGER, -- n
79 * publicExponent INTEGER, -- e
80 * privateExponent INTEGER, -- d
81 * prime1 INTEGER, -- p
82 * prime2 INTEGER, -- q
83 * exponent1 INTEGER, -- d mod (p-1)
84 * exponent2 INTEGER, -- d mod (q-1)
85 * coefficient INTEGER, -- (inverse of q) mod p
86 * otherPrimeInfos OtherPrimeInfos OPTIONAL
87 * }
88 * Or, for a public key, the same structure with only
89 * version, modulus and publicExponent.
90 */
91 *p = buffer + buffer_size;
92 if( keypair )
93 {
94 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
95 asn1_write_10x( p, buffer, half_bits, 1 ) );
96 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
97 asn1_write_10x( p, buffer, half_bits, 1 ) );
98 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
99 asn1_write_10x( p, buffer, half_bits, 1 ) );
100 MBEDTLS_ASN1_CHK_ADD( len, /* q */
101 asn1_write_10x( p, buffer, half_bits, 1 ) );
102 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
103 asn1_write_10x( p, buffer, half_bits, 3 ) );
104 MBEDTLS_ASN1_CHK_ADD( len, /* d */
105 asn1_write_10x( p, buffer, bits, 1 ) );
106 }
107 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
108 asn1_write_10x( p, buffer, 17, 1 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* n */
110 asn1_write_10x( p, buffer, bits, 1 ) );
111 if( keypair )
112 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
113 mbedtls_asn1_write_int( p, buffer, 0 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
115 {
116 const unsigned char tag =
117 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
118 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
119 }
120 return( len );
121}
122
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100123static int exercise_mac_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200124 psa_key_usage_t usage,
125 psa_algorithm_t alg )
126{
Jaeden Amero769ce272019-01-04 11:48:03 +0000127 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200128 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200129 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200130 size_t mac_length = sizeof( mac );
131
132 if( usage & PSA_KEY_USAGE_SIGN )
133 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100134 PSA_ASSERT( psa_mac_sign_setup( &operation,
135 handle, alg ) );
136 PSA_ASSERT( psa_mac_update( &operation,
137 input, sizeof( input ) ) );
138 PSA_ASSERT( psa_mac_sign_finish( &operation,
139 mac, sizeof( mac ),
140 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200141 }
142
143 if( usage & PSA_KEY_USAGE_VERIFY )
144 {
145 psa_status_t verify_status =
146 ( usage & PSA_KEY_USAGE_SIGN ?
147 PSA_SUCCESS :
148 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +0100149 PSA_ASSERT( psa_mac_verify_setup( &operation,
150 handle, alg ) );
151 PSA_ASSERT( psa_mac_update( &operation,
152 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100153 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
154 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200155 }
156
157 return( 1 );
158
159exit:
160 psa_mac_abort( &operation );
161 return( 0 );
162}
163
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100164static int exercise_cipher_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200165 psa_key_usage_t usage,
166 psa_algorithm_t alg )
167{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000168 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200169 unsigned char iv[16] = {0};
170 size_t iv_length = sizeof( iv );
171 const unsigned char plaintext[16] = "Hello, world...";
172 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
173 size_t ciphertext_length = sizeof( ciphertext );
174 unsigned char decrypted[sizeof( ciphertext )];
175 size_t part_length;
176
177 if( usage & PSA_KEY_USAGE_ENCRYPT )
178 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100179 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
180 handle, alg ) );
181 PSA_ASSERT( psa_cipher_generate_iv( &operation,
182 iv, sizeof( iv ),
183 &iv_length ) );
184 PSA_ASSERT( psa_cipher_update( &operation,
185 plaintext, sizeof( plaintext ),
186 ciphertext, sizeof( ciphertext ),
187 &ciphertext_length ) );
188 PSA_ASSERT( psa_cipher_finish( &operation,
189 ciphertext + ciphertext_length,
190 sizeof( ciphertext ) - ciphertext_length,
191 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200192 ciphertext_length += part_length;
193 }
194
195 if( usage & PSA_KEY_USAGE_DECRYPT )
196 {
197 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700198 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200199 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
200 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200201 size_t bits;
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100202 TEST_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200203 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
204 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100205 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
206 handle, alg ) );
207 PSA_ASSERT( psa_cipher_set_iv( &operation,
208 iv, iv_length ) );
209 PSA_ASSERT( psa_cipher_update( &operation,
210 ciphertext, ciphertext_length,
211 decrypted, sizeof( decrypted ),
212 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200213 status = psa_cipher_finish( &operation,
214 decrypted + part_length,
215 sizeof( decrypted ) - part_length,
216 &part_length );
217 /* For a stream cipher, all inputs are valid. For a block cipher,
218 * if the input is some aribtrary data rather than an actual
219 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700220 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700221 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100222 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200223 else
224 TEST_ASSERT( status == PSA_SUCCESS ||
225 status == PSA_ERROR_INVALID_PADDING );
226 }
227
228 return( 1 );
229
230exit:
231 psa_cipher_abort( &operation );
232 return( 0 );
233}
234
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100235static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200236 psa_key_usage_t usage,
237 psa_algorithm_t alg )
238{
239 unsigned char nonce[16] = {0};
240 size_t nonce_length = sizeof( nonce );
241 unsigned char plaintext[16] = "Hello, world...";
242 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
243 size_t ciphertext_length = sizeof( ciphertext );
244 size_t plaintext_length = sizeof( ciphertext );
245
246 if( usage & PSA_KEY_USAGE_ENCRYPT )
247 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100248 PSA_ASSERT( psa_aead_encrypt( handle, alg,
249 nonce, nonce_length,
250 NULL, 0,
251 plaintext, sizeof( plaintext ),
252 ciphertext, sizeof( ciphertext ),
253 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200254 }
255
256 if( usage & PSA_KEY_USAGE_DECRYPT )
257 {
258 psa_status_t verify_status =
259 ( usage & PSA_KEY_USAGE_ENCRYPT ?
260 PSA_SUCCESS :
261 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100262 TEST_EQUAL( psa_aead_decrypt( handle, alg,
263 nonce, nonce_length,
264 NULL, 0,
265 ciphertext, ciphertext_length,
266 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100267 &plaintext_length ),
268 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200269 }
270
271 return( 1 );
272
273exit:
274 return( 0 );
275}
276
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100277static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200278 psa_key_usage_t usage,
279 psa_algorithm_t alg )
280{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200281 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
282 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200283 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200284 size_t signature_length = sizeof( signature );
285
286 if( usage & PSA_KEY_USAGE_SIGN )
287 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200288 /* Some algorithms require the payload to have the size of
289 * the hash encoded in the algorithm. Use this input size
290 * even for algorithms that allow other input sizes. */
291 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
292 if( hash_alg != 0 )
293 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100294 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
295 payload, payload_length,
296 signature, sizeof( signature ),
297 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200298 }
299
300 if( usage & PSA_KEY_USAGE_VERIFY )
301 {
302 psa_status_t verify_status =
303 ( usage & PSA_KEY_USAGE_SIGN ?
304 PSA_SUCCESS :
305 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100306 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
307 payload, payload_length,
308 signature, signature_length ),
309 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200310 }
311
312 return( 1 );
313
314exit:
315 return( 0 );
316}
317
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100318static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200319 psa_key_usage_t usage,
320 psa_algorithm_t alg )
321{
322 unsigned char plaintext[256] = "Hello, world...";
323 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
324 size_t ciphertext_length = sizeof( ciphertext );
325 size_t plaintext_length = 16;
326
327 if( usage & PSA_KEY_USAGE_ENCRYPT )
328 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100329 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
330 plaintext, plaintext_length,
331 NULL, 0,
332 ciphertext, sizeof( ciphertext ),
333 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200334 }
335
336 if( usage & PSA_KEY_USAGE_DECRYPT )
337 {
338 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100339 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200340 ciphertext, ciphertext_length,
341 NULL, 0,
342 plaintext, sizeof( plaintext ),
343 &plaintext_length );
344 TEST_ASSERT( status == PSA_SUCCESS ||
345 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
346 ( status == PSA_ERROR_INVALID_ARGUMENT ||
347 status == PSA_ERROR_INVALID_PADDING ) ) );
348 }
349
350 return( 1 );
351
352exit:
353 return( 0 );
354}
Gilles Peskine02b75072018-07-01 22:31:34 +0200355
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100356static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200357 psa_key_usage_t usage,
358 psa_algorithm_t alg )
359{
360 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
361 unsigned char label[16] = "This is a label.";
362 size_t label_length = sizeof( label );
363 unsigned char seed[16] = "abcdefghijklmnop";
364 size_t seed_length = sizeof( seed );
365 unsigned char output[1];
366
367 if( usage & PSA_KEY_USAGE_DERIVE )
368 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100369 PSA_ASSERT( psa_key_derivation( &generator,
370 handle, alg,
371 label, label_length,
372 seed, seed_length,
373 sizeof( output ) ) );
374 PSA_ASSERT( psa_generator_read( &generator,
375 output,
376 sizeof( output ) ) );
377 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200378 }
379
380 return( 1 );
381
382exit:
383 return( 0 );
384}
385
Gilles Peskinec7998b72018-11-07 18:45:02 +0100386/* We need two keys to exercise key agreement. Exercise the
387 * private key against its own public key. */
388static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator,
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100389 psa_key_handle_t handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100390 psa_algorithm_t alg )
391{
392 psa_key_type_t private_key_type;
393 psa_key_type_t public_key_type;
394 size_t key_bits;
395 uint8_t *public_key = NULL;
396 size_t public_key_length;
397 /* Return UNKNOWN_ERROR if something other than the final call to
398 * psa_key_agreement fails. This isn't fully satisfactory, but it's
399 * good enough: callers will report it as a failed test anyway. */
400 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
401
Gilles Peskine8817f612018-12-18 00:18:46 +0100402 PSA_ASSERT( psa_get_key_information( handle,
403 &private_key_type,
404 &key_bits ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100405 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
406 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
407 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100408 PSA_ASSERT( psa_export_public_key( handle,
409 public_key, public_key_length,
410 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100411
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100412 status = psa_key_agreement( generator, handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100413 public_key, public_key_length,
414 alg );
415exit:
416 mbedtls_free( public_key );
417 return( status );
418}
419
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100420static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200421 psa_key_usage_t usage,
422 psa_algorithm_t alg )
423{
424 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200425 unsigned char output[1];
426 int ok = 0;
427
428 if( usage & PSA_KEY_USAGE_DERIVE )
429 {
430 /* We need two keys to exercise key agreement. Exercise the
431 * private key against its own public key. */
Gilles Peskine8817f612018-12-18 00:18:46 +0100432 PSA_ASSERT( key_agreement_with_self( &generator, handle, alg ) );
433 PSA_ASSERT( psa_generator_read( &generator,
434 output,
435 sizeof( output ) ) );
436 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200437 }
438 ok = 1;
439
440exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200441 return( ok );
442}
443
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200444static int is_oid_of_key_type( psa_key_type_t type,
445 const uint8_t *oid, size_t oid_length )
446{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200447 const uint8_t *expected_oid = NULL;
448 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200449#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200450 if( PSA_KEY_TYPE_IS_RSA( type ) )
451 {
452 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
453 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
454 }
455 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200456#endif /* MBEDTLS_RSA_C */
457#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200458 if( PSA_KEY_TYPE_IS_ECC( type ) )
459 {
460 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
461 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
462 }
463 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200464#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200465 {
466 char message[40];
467 mbedtls_snprintf( message, sizeof( message ),
468 "OID not known for key type=0x%08lx",
469 (unsigned long) type );
470 test_fail( message, __LINE__, __FILE__ );
471 return( 0 );
472 }
473
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200474 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200475 return( 1 );
476
477exit:
478 return( 0 );
479}
480
481static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
482 size_t min_bits, size_t max_bits,
483 int must_be_odd )
484{
485 size_t len;
486 size_t actual_bits;
487 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100488 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100489 MBEDTLS_ASN1_INTEGER ),
490 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200491 /* Tolerate a slight departure from DER encoding:
492 * - 0 may be represented by an empty string or a 1-byte string.
493 * - The sign bit may be used as a value bit. */
494 if( ( len == 1 && ( *p )[0] == 0 ) ||
495 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
496 {
497 ++( *p );
498 --len;
499 }
500 if( min_bits == 0 && len == 0 )
501 return( 1 );
502 msb = ( *p )[0];
503 TEST_ASSERT( msb != 0 );
504 actual_bits = 8 * ( len - 1 );
505 while( msb != 0 )
506 {
507 msb >>= 1;
508 ++actual_bits;
509 }
510 TEST_ASSERT( actual_bits >= min_bits );
511 TEST_ASSERT( actual_bits <= max_bits );
512 if( must_be_odd )
513 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
514 *p += len;
515 return( 1 );
516exit:
517 return( 0 );
518}
519
520static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
521 size_t *len,
522 unsigned char n, unsigned char tag )
523{
524 int ret;
525 ret = mbedtls_asn1_get_tag( p, end, len,
526 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
527 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
528 if( ret != 0 )
529 return( ret );
530 end = *p + *len;
531 ret = mbedtls_asn1_get_tag( p, end, len, tag );
532 if( ret != 0 )
533 return( ret );
534 if( *p + *len != end )
535 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
536 return( 0 );
537}
538
539static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
540 uint8_t *exported, size_t exported_length )
541{
542 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100543 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200544 else
545 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200546
547#if defined(MBEDTLS_DES_C)
548 if( type == PSA_KEY_TYPE_DES )
549 {
550 /* Check the parity bits. */
551 unsigned i;
552 for( i = 0; i < bits / 8; i++ )
553 {
554 unsigned bit_count = 0;
555 unsigned m;
556 for( m = 1; m <= 0x100; m <<= 1 )
557 {
558 if( exported[i] & m )
559 ++bit_count;
560 }
561 TEST_ASSERT( bit_count % 2 != 0 );
562 }
563 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200564 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200565#endif
566
567#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
568 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
569 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200570 uint8_t *p = exported;
571 uint8_t *end = exported + exported_length;
572 size_t len;
573 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200574 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200575 * modulus INTEGER, -- n
576 * publicExponent INTEGER, -- e
577 * privateExponent INTEGER, -- d
578 * prime1 INTEGER, -- p
579 * prime2 INTEGER, -- q
580 * exponent1 INTEGER, -- d mod (p-1)
581 * exponent2 INTEGER, -- d mod (q-1)
582 * coefficient INTEGER, -- (inverse of q) mod p
583 * }
584 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100585 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
586 MBEDTLS_ASN1_SEQUENCE |
587 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
588 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200589 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
590 goto exit;
591 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
592 goto exit;
593 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
594 goto exit;
595 /* Require d to be at least half the size of n. */
596 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
597 goto exit;
598 /* Require p and q to be at most half the size of n, rounded up. */
599 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
600 goto exit;
601 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
602 goto exit;
603 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
604 goto exit;
605 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
606 goto exit;
607 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
608 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100609 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100610 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200611 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200612#endif /* MBEDTLS_RSA_C */
613
614#if defined(MBEDTLS_ECP_C)
615 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
616 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100617 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100618 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100619 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200620 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200621#endif /* MBEDTLS_ECP_C */
622
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200623 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
624 {
625 uint8_t *p = exported;
626 uint8_t *end = exported + exported_length;
627 size_t len;
628 mbedtls_asn1_buf alg;
629 mbedtls_asn1_buf params;
630 mbedtls_asn1_bitstring bitstring;
631 /* SubjectPublicKeyInfo ::= SEQUENCE {
632 * algorithm AlgorithmIdentifier,
633 * subjectPublicKey BIT STRING }
634 * AlgorithmIdentifier ::= SEQUENCE {
635 * algorithm OBJECT IDENTIFIER,
636 * parameters ANY DEFINED BY algorithm OPTIONAL }
637 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100638 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
639 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100640 MBEDTLS_ASN1_CONSTRUCTED ),
641 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100642 TEST_EQUAL( p + len, end );
643 TEST_EQUAL( mbedtls_asn1_get_alg( &p, end, &alg, &params ), 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200644 if( ! is_oid_of_key_type( type, alg.p, alg.len ) )
645 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100646 TEST_EQUAL( mbedtls_asn1_get_bitstring( &p, end, &bitstring ), 0 );
647 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200648 p = bitstring.p;
649#if defined(MBEDTLS_RSA_C)
650 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
651 {
652 /* RSAPublicKey ::= SEQUENCE {
653 * modulus INTEGER, -- n
654 * publicExponent INTEGER } -- e
655 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100656 TEST_EQUAL( bitstring.unused_bits, 0 );
657 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
658 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100659 MBEDTLS_ASN1_CONSTRUCTED ),
660 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100661 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200662 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
663 goto exit;
664 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
665 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100666 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200667 }
668 else
669#endif /* MBEDTLS_RSA_C */
670#if defined(MBEDTLS_ECP_C)
671 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
672 {
673 /* ECPoint ::= ...
Gilles Peskinec6290c02018-08-13 17:24:59 +0200674 * -- first 8 bits: 0x04 (uncompressed representation);
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200675 * -- then x_P as an n-bit string, big endian;
676 * -- then y_P as a n-bit string, big endian,
677 * -- where n is the order of the curve.
678 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100679 TEST_EQUAL( bitstring.unused_bits, 0 );
680 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
681 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200682 }
683 else
684#endif /* MBEDTLS_ECP_C */
685 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100686 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200687 mbedtls_snprintf( message, sizeof( message ),
688 "No sanity check for public key type=0x%08lx",
689 (unsigned long) type );
690 test_fail( message, __LINE__, __FILE__ );
691 return( 0 );
692 }
693 }
694 else
695
696 {
697 /* No sanity checks for other types */
698 }
699
700 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200701
702exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200703 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200704}
705
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100706static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200707 psa_key_usage_t usage )
708{
709 psa_key_type_t type;
710 size_t bits;
711 uint8_t *exported = NULL;
712 size_t exported_size = 0;
713 size_t exported_length = 0;
714 int ok = 0;
715
Gilles Peskine8817f612018-12-18 00:18:46 +0100716 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200717
718 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
719 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200720 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100721 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
722 PSA_ERROR_NOT_PERMITTED );
Gilles Peskined14664a2018-08-10 19:07:32 +0200723 return( 1 );
724 }
725
Gilles Peskined14664a2018-08-10 19:07:32 +0200726 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200727 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200728
Gilles Peskine8817f612018-12-18 00:18:46 +0100729 PSA_ASSERT( psa_export_key( handle,
730 exported, exported_size,
731 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200732 ok = exported_key_sanity_check( type, bits, exported, exported_length );
733
734exit:
735 mbedtls_free( exported );
736 return( ok );
737}
738
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100739static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200740{
741 psa_key_type_t type;
742 psa_key_type_t public_type;
743 size_t bits;
744 uint8_t *exported = NULL;
745 size_t exported_size = 0;
746 size_t exported_length = 0;
747 int ok = 0;
748
Gilles Peskine8817f612018-12-18 00:18:46 +0100749 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200750 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
751 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100752 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100753 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200754 return( 1 );
755 }
756
757 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
758 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200759 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200760
Gilles Peskine8817f612018-12-18 00:18:46 +0100761 PSA_ASSERT( psa_export_public_key( handle,
762 exported, exported_size,
763 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200764 ok = exported_key_sanity_check( public_type, bits,
765 exported, exported_length );
766
767exit:
768 mbedtls_free( exported );
769 return( ok );
770}
771
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100772static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +0200773 psa_key_usage_t usage,
774 psa_algorithm_t alg )
775{
776 int ok;
777 if( alg == 0 )
778 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
779 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100780 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200781 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100782 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200783 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100784 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200785 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100786 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200787 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100788 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200789 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100790 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200791 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100792 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200793 else
794 {
795 char message[40];
796 mbedtls_snprintf( message, sizeof( message ),
797 "No code to exercise alg=0x%08lx",
798 (unsigned long) alg );
799 test_fail( message, __LINE__, __FILE__ );
800 ok = 0;
801 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200802
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100803 ok = ok && exercise_export_key( handle, usage );
804 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +0200805
Gilles Peskine02b75072018-07-01 22:31:34 +0200806 return( ok );
807}
808
Gilles Peskine10df3412018-10-25 22:35:43 +0200809static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
810 psa_algorithm_t alg )
811{
812 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
813 {
814 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
815 PSA_KEY_USAGE_VERIFY :
816 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
817 }
818 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
819 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
820 {
821 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
822 PSA_KEY_USAGE_ENCRYPT :
823 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
824 }
825 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
826 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
827 {
828 return( PSA_KEY_USAGE_DERIVE );
829 }
830 else
831 {
832 return( 0 );
833 }
834
835}
Darryl Green0c6575a2018-11-07 16:05:30 +0000836
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100837/* An overapproximation of the amount of storage needed for a key of the
838 * given type and with the given content. The API doesn't make it easy
839 * to find a good value for the size. The current implementation doesn't
840 * care about the value anyway. */
841#define KEY_BITS_FROM_DATA( type, data ) \
842 ( data )->len
843
Darryl Green0c6575a2018-11-07 16:05:30 +0000844typedef enum {
845 IMPORT_KEY = 0,
846 GENERATE_KEY = 1,
847 DERIVE_KEY = 2
848} generate_method;
849
Gilles Peskinee59236f2018-01-27 23:32:46 +0100850/* END_HEADER */
851
852/* BEGIN_DEPENDENCIES
853 * depends_on:MBEDTLS_PSA_CRYPTO_C
854 * END_DEPENDENCIES
855 */
856
857/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200858void static_checks( )
859{
860 size_t max_truncated_mac_size =
861 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
862
863 /* Check that the length for a truncated MAC always fits in the algorithm
864 * encoding. The shifted mask is the maximum truncated value. The
865 * untruncated algorithm may be one byte larger. */
866 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
867}
868/* END_CASE */
869
870/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200871void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100873 psa_key_handle_t handle = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200874 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100875 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100876
Gilles Peskine8817f612018-12-18 00:18:46 +0100877 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100878
Gilles Peskine8817f612018-12-18 00:18:46 +0100879 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
880 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100881 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100882 TEST_EQUAL( status, expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100883 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100884 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100885
886exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100887 mbedtls_psa_crypto_free( );
888}
889/* END_CASE */
890
891/* BEGIN_CASE */
Gilles Peskinea4261682018-12-03 11:34:01 +0100892void import_twice( int alg_arg, int usage_arg,
893 int type1_arg, data_t *data1,
894 int expected_import1_status_arg,
895 int type2_arg, data_t *data2,
896 int expected_import2_status_arg )
897{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100898 psa_key_handle_t handle = 0;
Gilles Peskinea4261682018-12-03 11:34:01 +0100899 psa_algorithm_t alg = alg_arg;
900 psa_key_usage_t usage = usage_arg;
901 psa_key_type_t type1 = type1_arg;
902 psa_status_t expected_import1_status = expected_import1_status_arg;
903 psa_key_type_t type2 = type2_arg;
904 psa_status_t expected_import2_status = expected_import2_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +0000905 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea4261682018-12-03 11:34:01 +0100906 psa_status_t status;
907
Gilles Peskine8817f612018-12-18 00:18:46 +0100908 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100909
Gilles Peskine8817f612018-12-18 00:18:46 +0100910 PSA_ASSERT( psa_allocate_key( type1,
911 MAX( KEY_BITS_FROM_DATA( type1, data1 ),
912 KEY_BITS_FROM_DATA( type2, data2 ) ),
913 &handle ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100914 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100915 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100916
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100917 status = psa_import_key( handle, type1, data1->x, data1->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100918 TEST_EQUAL( status, expected_import1_status );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100919 status = psa_import_key( handle, type2, data2->x, data2->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100920 TEST_EQUAL( status, expected_import2_status );
Gilles Peskinea4261682018-12-03 11:34:01 +0100921
922 if( expected_import1_status == PSA_SUCCESS ||
923 expected_import2_status == PSA_SUCCESS )
924 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100925 TEST_ASSERT( exercise_key( handle, usage, alg ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100926 }
927
928exit:
929 mbedtls_psa_crypto_free( );
930}
931/* END_CASE */
932
933/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200934void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
935{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100936 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200937 size_t bits = bits_arg;
938 psa_status_t expected_status = expected_status_arg;
939 psa_status_t status;
940 psa_key_type_t type =
941 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
942 size_t buffer_size = /* Slight overapproximations */
943 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200944 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200945 unsigned char *p;
946 int ret;
947 size_t length;
948
Gilles Peskine8817f612018-12-18 00:18:46 +0100949 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200950 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200951
952 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
953 bits, keypair ) ) >= 0 );
954 length = ret;
955
956 /* Try importing the key */
Gilles Peskine8817f612018-12-18 00:18:46 +0100957 PSA_ASSERT( psa_allocate_key( type, bits, &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100958 status = psa_import_key( handle, type, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100959 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200960 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100961 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200962
963exit:
964 mbedtls_free( buffer );
965 mbedtls_psa_crypto_free( );
966}
967/* END_CASE */
968
969/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300970void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300971 int type_arg,
972 int alg_arg,
973 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100974 int expected_bits,
975 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200976 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100977 int canonical_input )
978{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100979 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100980 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200981 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200982 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100983 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100984 unsigned char *exported = NULL;
985 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100986 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100987 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100988 size_t reexported_length;
989 psa_key_type_t got_type;
990 size_t got_bits;
Jaeden Amero70261c52019-01-04 11:47:20 +0000991 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100992
Moran Pekercb088e72018-07-17 17:36:59 +0300993 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200994 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100995 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200996 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100997 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100998
Gilles Peskine8817f612018-12-18 00:18:46 +0100999 PSA_ASSERT( psa_allocate_key( type, expected_bits, &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001000 psa_key_policy_set_usage( &policy, usage_arg, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001001 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001002
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001003 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1004 PSA_ERROR_EMPTY_SLOT );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001005
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001006 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001007 PSA_ASSERT( psa_import_key( handle, type,
1008 data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001009
1010 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001011 PSA_ASSERT( psa_get_key_information( handle,
1012 &got_type,
1013 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001014 TEST_EQUAL( got_type, type );
1015 TEST_EQUAL( got_bits, (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001016
1017 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001018 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001019 exported, export_size,
1020 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001021 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001022
1023 /* The exported length must be set by psa_export_key() to a value between 0
1024 * and export_size. On errors, the exported length must be 0. */
1025 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1026 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1027 TEST_ASSERT( exported_length <= export_size );
1028
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001029 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001030 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001031 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001032 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001033 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001034 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001035 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001036
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001037 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001038 goto exit;
1039
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001040 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001041 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001042 else
1043 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001044 psa_key_handle_t handle2;
Gilles Peskine8817f612018-12-18 00:18:46 +01001045 PSA_ASSERT( psa_allocate_key( type, expected_bits, &handle2 ) );
1046 PSA_ASSERT( psa_set_key_policy( handle2, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001047
Gilles Peskine8817f612018-12-18 00:18:46 +01001048 PSA_ASSERT( psa_import_key( handle2, type,
1049 exported,
1050 exported_length ) );
1051 PSA_ASSERT( psa_export_key( handle2,
1052 reexported,
1053 export_size,
1054 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001055 ASSERT_COMPARE( exported, exported_length,
1056 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001057 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001058 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001059 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001060
1061destroy:
1062 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001063 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001064 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1065 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001066
1067exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001068 mbedtls_free( exported );
1069 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001070 mbedtls_psa_crypto_free( );
1071}
1072/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001073
Moran Pekerf709f4a2018-06-06 17:26:04 +03001074/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001075void import_key_nonempty_slot( )
1076{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001077 psa_key_handle_t handle = 0;
Moran Peker28a38e62018-11-07 16:18:24 +02001078 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1079 psa_status_t status;
1080 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
Gilles Peskine8817f612018-12-18 00:18:46 +01001081 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001082
Gilles Peskine8817f612018-12-18 00:18:46 +01001083 PSA_ASSERT( psa_allocate_key( type, PSA_BYTES_TO_BITS( sizeof( data ) ),
1084 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001085
Moran Peker28a38e62018-11-07 16:18:24 +02001086 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001087 PSA_ASSERT( psa_import_key( handle, type,
1088 data, sizeof( data ) ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001089
1090 /* Import the key again */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001091 status = psa_import_key( handle, type, data, sizeof( data ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001092 TEST_EQUAL( status, PSA_ERROR_OCCUPIED_SLOT );
Moran Peker28a38e62018-11-07 16:18:24 +02001093
1094exit:
1095 mbedtls_psa_crypto_free( );
1096}
1097/* END_CASE */
1098
1099/* BEGIN_CASE */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001100void export_invalid_handle( int handle, int expected_export_status_arg )
Moran Peker28a38e62018-11-07 16:18:24 +02001101{
1102 psa_status_t status;
1103 unsigned char *exported = NULL;
1104 size_t export_size = 0;
1105 size_t exported_length = INVALID_EXPORT_LENGTH;
1106 psa_status_t expected_export_status = expected_export_status_arg;
1107
Gilles Peskine8817f612018-12-18 00:18:46 +01001108 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001109
1110 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001111 status = psa_export_key( (psa_key_handle_t) handle,
Moran Peker28a38e62018-11-07 16:18:24 +02001112 exported, export_size,
1113 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001114 TEST_EQUAL( status, expected_export_status );
Moran Peker28a38e62018-11-07 16:18:24 +02001115
1116exit:
1117 mbedtls_psa_crypto_free( );
1118}
1119/* END_CASE */
1120
1121/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001122void export_with_no_key_activity( )
1123{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001124 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001125 psa_algorithm_t alg = PSA_ALG_CTR;
1126 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001127 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001128 unsigned char *exported = NULL;
1129 size_t export_size = 0;
1130 size_t exported_length = INVALID_EXPORT_LENGTH;
1131
Gilles Peskine8817f612018-12-18 00:18:46 +01001132 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001133
Gilles Peskine8817f612018-12-18 00:18:46 +01001134 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1135 &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001136 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001137 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001138
1139 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001140 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001141 exported, export_size,
1142 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001143 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001144
1145exit:
1146 mbedtls_psa_crypto_free( );
1147}
1148/* END_CASE */
1149
1150/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001151void cipher_with_no_key_activity( )
1152{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001153 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001154 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001155 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001156 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001157 int exercise_alg = PSA_ALG_CTR;
1158
Gilles Peskine8817f612018-12-18 00:18:46 +01001159 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001160
Gilles Peskine8817f612018-12-18 00:18:46 +01001161 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1162 &handle ) );
Moran Pekerce500072018-11-07 16:20:07 +02001163 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001164 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerce500072018-11-07 16:20:07 +02001165
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001166 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001167 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001168
1169exit:
1170 psa_cipher_abort( &operation );
1171 mbedtls_psa_crypto_free( );
1172}
1173/* END_CASE */
1174
1175/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001176void export_after_import_failure( data_t *data, int type_arg,
1177 int expected_import_status_arg )
1178{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001179 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001180 psa_key_type_t type = type_arg;
1181 psa_status_t status;
1182 unsigned char *exported = NULL;
1183 size_t export_size = 0;
1184 psa_status_t expected_import_status = expected_import_status_arg;
1185 size_t exported_length = INVALID_EXPORT_LENGTH;
1186
Gilles Peskine8817f612018-12-18 00:18:46 +01001187 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001188
Gilles Peskine8817f612018-12-18 00:18:46 +01001189 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1190 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001191
Moran Peker34550092018-11-07 16:19:34 +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 Peker34550092018-11-07 16:19:34 +02001196
1197 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001198 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001199 exported, export_size,
1200 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001201 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001202
1203exit:
1204 mbedtls_psa_crypto_free( );
1205}
1206/* END_CASE */
1207
1208/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001209void cipher_after_import_failure( data_t *data, int type_arg,
1210 int expected_import_status_arg )
1211{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001212 psa_key_handle_t handle = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001213 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001214 psa_key_type_t type = type_arg;
1215 psa_status_t status;
1216 psa_status_t expected_import_status = expected_import_status_arg;
1217 int exercise_alg = PSA_ALG_CTR;
1218
Gilles Peskine8817f612018-12-18 00:18:46 +01001219 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001220
Gilles Peskine8817f612018-12-18 00:18:46 +01001221 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1222 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001223
Moran Pekerce500072018-11-07 16:20:07 +02001224 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001225 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001226 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001227 TEST_EQUAL( status, expected_import_status );
Moran Pekerce500072018-11-07 16:20:07 +02001228
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001229 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001230 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001231
1232exit:
1233 psa_cipher_abort( &operation );
1234 mbedtls_psa_crypto_free( );
1235}
1236/* END_CASE */
1237
1238/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001239void export_after_destroy_key( data_t *data, int type_arg )
1240{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001241 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001242 psa_key_type_t type = type_arg;
1243 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001244 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001245 psa_algorithm_t alg = PSA_ALG_CTR;
1246 unsigned char *exported = NULL;
1247 size_t export_size = 0;
1248 size_t exported_length = INVALID_EXPORT_LENGTH;
1249
Gilles Peskine8817f612018-12-18 00:18:46 +01001250 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001251
Gilles Peskine8817f612018-12-18 00:18:46 +01001252 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1253 &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001254 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001255 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001256 export_size = (ptrdiff_t) data->len;
1257 ASSERT_ALLOC( exported, export_size );
1258
1259 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001260 PSA_ASSERT( psa_import_key( handle, type,
1261 data->x, data->len ) );
Moran Peker34550092018-11-07 16:19:34 +02001262
Gilles Peskine8817f612018-12-18 00:18:46 +01001263 PSA_ASSERT( psa_export_key( handle, exported, export_size,
1264 &exported_length ) );
Moran Peker34550092018-11-07 16:19:34 +02001265
1266 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001267 PSA_ASSERT( psa_destroy_key( handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001268
1269 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001270 status = psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001271 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001272 TEST_EQUAL( status, PSA_ERROR_INVALID_HANDLE );
Moran Peker34550092018-11-07 16:19:34 +02001273
1274exit:
1275 mbedtls_free( exported );
1276 mbedtls_psa_crypto_free( );
1277}
1278/* END_CASE */
1279
1280/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001281void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001282 int type_arg,
1283 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001284 int export_size_delta,
1285 int expected_export_status_arg,
1286 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001287{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001288 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001289 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001290 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001291 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001292 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001293 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001294 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001295 size_t exported_length = INVALID_EXPORT_LENGTH;
Jaeden Amero70261c52019-01-04 11:47:20 +00001296 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001297
Gilles Peskine8817f612018-12-18 00:18:46 +01001298 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001299
Gilles Peskine8817f612018-12-18 00:18:46 +01001300 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1301 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001302 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001303 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001304
1305 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001306 PSA_ASSERT( psa_import_key( handle, type,
1307 data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001308
Gilles Peskine49c25912018-10-29 15:15:31 +01001309 /* Export the public key */
1310 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001311 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001312 exported, export_size,
1313 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001314 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001315 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001316 {
1317 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1318 size_t bits;
Gilles Peskine8817f612018-12-18 00:18:46 +01001319 PSA_ASSERT( psa_get_key_information( handle, NULL, &bits ) );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001320 TEST_ASSERT( expected_public_key->len <=
1321 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001322 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1323 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001324 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001325
1326exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001327 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001328 psa_destroy_key( handle );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001329 mbedtls_psa_crypto_free( );
1330}
1331/* END_CASE */
1332
Gilles Peskine20035e32018-02-03 22:44:14 +01001333/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001334void import_and_exercise_key( data_t *data,
1335 int type_arg,
1336 int bits_arg,
1337 int alg_arg )
1338{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001339 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001340 psa_key_type_t type = type_arg;
1341 size_t bits = bits_arg;
1342 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001343 psa_key_usage_t usage = usage_to_exercise( type, alg );
Jaeden Amero70261c52019-01-04 11:47:20 +00001344 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001345 psa_key_type_t got_type;
1346 size_t got_bits;
1347 psa_status_t status;
1348
Gilles Peskine8817f612018-12-18 00:18:46 +01001349 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001350
Gilles Peskine8817f612018-12-18 00:18:46 +01001351 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1352 &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001353 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001354 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001355
1356 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001357 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01001358 PSA_ASSERT( status );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001359
1360 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001361 PSA_ASSERT( psa_get_key_information( handle,
1362 &got_type,
1363 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001364 TEST_EQUAL( got_type, type );
1365 TEST_EQUAL( got_bits, bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001366
1367 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001368 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001369 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001370
1371exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001372 psa_destroy_key( handle );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001373 mbedtls_psa_crypto_free( );
1374}
1375/* END_CASE */
1376
1377/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001378void key_policy( int usage_arg, int alg_arg )
1379{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001380 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001381 psa_algorithm_t alg = alg_arg;
1382 psa_key_usage_t usage = usage_arg;
1383 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1384 unsigned char key[32] = {0};
Jaeden Amero70261c52019-01-04 11:47:20 +00001385 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
1386 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001387
1388 memset( key, 0x2a, sizeof( key ) );
1389
Gilles Peskine8817f612018-12-18 00:18:46 +01001390 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001391
Gilles Peskine8817f612018-12-18 00:18:46 +01001392 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( sizeof( key ) ),
1393 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001394 psa_key_policy_set_usage( &policy_set, usage, alg );
1395
Gilles Peskinefe11b722018-12-18 00:24:04 +01001396 TEST_EQUAL( psa_key_policy_get_usage( &policy_set ), usage );
1397 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_set ), alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001398 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001399
Gilles Peskine8817f612018-12-18 00:18:46 +01001400 PSA_ASSERT( psa_import_key( handle, key_type,
1401 key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001402
Gilles Peskine8817f612018-12-18 00:18:46 +01001403 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001404
Gilles Peskinefe11b722018-12-18 00:24:04 +01001405 TEST_EQUAL( policy_get.usage, policy_set.usage );
1406 TEST_EQUAL( policy_get.alg, policy_set.alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001407
1408exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001409 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001410 mbedtls_psa_crypto_free( );
1411}
1412/* END_CASE */
1413
1414/* BEGIN_CASE */
Jaeden Amero70261c52019-01-04 11:47:20 +00001415void key_policy_init( )
1416{
1417 /* Test each valid way of initializing the object, except for `= {0}`, as
1418 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1419 * though it's OK by the C standard. We could test for this, but we'd need
1420 * to supress the Clang warning for the test. */
1421 psa_key_policy_t func = psa_key_policy_init( );
1422 psa_key_policy_t init = PSA_KEY_POLICY_INIT;
1423 psa_key_policy_t zero;
1424
1425 memset( &zero, 0, sizeof( zero ) );
1426
1427 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1428 * specification, we test that all valid ways of initializing the object
1429 * have the same bit pattern. This is a stronger requirement that may not
1430 * be valid on all platforms or PSA Crypto implementations, but implies the
1431 * weaker actual requirement is met: that a freshly initialized object, no
1432 * matter how it was initialized, acts the same as any other valid
1433 * initialization. */
1434 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1435 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1436}
1437/* END_CASE */
1438
1439/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001440void mac_key_policy( int policy_usage,
1441 int policy_alg,
1442 int key_type,
1443 data_t *key_data,
1444 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001445{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001446 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001447 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001448 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001449 psa_status_t status;
1450 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001451
Gilles Peskine8817f612018-12-18 00:18:46 +01001452 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001453
Gilles Peskine8817f612018-12-18 00:18:46 +01001454 PSA_ASSERT( psa_allocate_key( key_type,
1455 KEY_BITS_FROM_DATA( key_type, key_data ),
1456 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001457 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001458 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001459
Gilles Peskine8817f612018-12-18 00:18:46 +01001460 PSA_ASSERT( psa_import_key( handle, key_type,
1461 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001462
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001463 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001464 if( policy_alg == exercise_alg &&
1465 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001466 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001467 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001468 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001469 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001470
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001471 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001472 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001473 if( policy_alg == exercise_alg &&
1474 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001475 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001476 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001477 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001478
1479exit:
1480 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001481 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001482 mbedtls_psa_crypto_free( );
1483}
1484/* END_CASE */
1485
1486/* BEGIN_CASE */
1487void cipher_key_policy( int policy_usage,
1488 int policy_alg,
1489 int key_type,
1490 data_t *key_data,
1491 int exercise_alg )
1492{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001493 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001494 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001495 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001496 psa_status_t status;
1497
Gilles Peskine8817f612018-12-18 00:18:46 +01001498 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001499
Gilles Peskine8817f612018-12-18 00:18:46 +01001500 PSA_ASSERT( psa_allocate_key( key_type,
1501 KEY_BITS_FROM_DATA( key_type, key_data ),
1502 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001503 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001504 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001505
Gilles Peskine8817f612018-12-18 00:18:46 +01001506 PSA_ASSERT( psa_import_key( handle, key_type,
1507 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001508
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001509 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001510 if( policy_alg == exercise_alg &&
1511 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001512 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001513 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001514 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001515 psa_cipher_abort( &operation );
1516
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001517 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001518 if( policy_alg == exercise_alg &&
1519 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001520 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001521 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001522 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001523
1524exit:
1525 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001526 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001527 mbedtls_psa_crypto_free( );
1528}
1529/* END_CASE */
1530
1531/* BEGIN_CASE */
1532void aead_key_policy( int policy_usage,
1533 int policy_alg,
1534 int key_type,
1535 data_t *key_data,
1536 int nonce_length_arg,
1537 int tag_length_arg,
1538 int exercise_alg )
1539{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001540 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001541 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001542 psa_status_t status;
1543 unsigned char nonce[16] = {0};
1544 size_t nonce_length = nonce_length_arg;
1545 unsigned char tag[16];
1546 size_t tag_length = tag_length_arg;
1547 size_t output_length;
1548
1549 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1550 TEST_ASSERT( tag_length <= sizeof( tag ) );
1551
Gilles Peskine8817f612018-12-18 00:18:46 +01001552 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001553
Gilles Peskine8817f612018-12-18 00:18:46 +01001554 PSA_ASSERT( psa_allocate_key( key_type,
1555 KEY_BITS_FROM_DATA( key_type, key_data ),
1556 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001557 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001558 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001559
Gilles Peskine8817f612018-12-18 00:18:46 +01001560 PSA_ASSERT( psa_import_key( handle, key_type,
1561 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001562
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001563 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001564 nonce, nonce_length,
1565 NULL, 0,
1566 NULL, 0,
1567 tag, tag_length,
1568 &output_length );
1569 if( policy_alg == exercise_alg &&
1570 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001571 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001572 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001573 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001574
1575 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001576 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001577 nonce, nonce_length,
1578 NULL, 0,
1579 tag, tag_length,
1580 NULL, 0,
1581 &output_length );
1582 if( policy_alg == exercise_alg &&
1583 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001584 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001585 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001586 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001587
1588exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001589 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001590 mbedtls_psa_crypto_free( );
1591}
1592/* END_CASE */
1593
1594/* BEGIN_CASE */
1595void asymmetric_encryption_key_policy( int policy_usage,
1596 int policy_alg,
1597 int key_type,
1598 data_t *key_data,
1599 int exercise_alg )
1600{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001601 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001602 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001603 psa_status_t status;
1604 size_t key_bits;
1605 size_t buffer_length;
1606 unsigned char *buffer = NULL;
1607 size_t output_length;
1608
Gilles Peskine8817f612018-12-18 00:18:46 +01001609 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001610
Gilles Peskine8817f612018-12-18 00:18:46 +01001611 PSA_ASSERT( psa_allocate_key( key_type,
1612 KEY_BITS_FROM_DATA( key_type, key_data ),
1613 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001614 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001615 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001616
Gilles Peskine8817f612018-12-18 00:18:46 +01001617 PSA_ASSERT( psa_import_key( handle, key_type,
1618 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001619
Gilles Peskine8817f612018-12-18 00:18:46 +01001620 PSA_ASSERT( psa_get_key_information( handle,
1621 NULL,
1622 &key_bits ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001623 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1624 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001625 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001626
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001627 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001628 NULL, 0,
1629 NULL, 0,
1630 buffer, buffer_length,
1631 &output_length );
1632 if( policy_alg == exercise_alg &&
1633 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001634 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001635 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001636 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001637
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001638 if( buffer_length != 0 )
1639 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001640 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001641 buffer, buffer_length,
1642 NULL, 0,
1643 buffer, buffer_length,
1644 &output_length );
1645 if( policy_alg == exercise_alg &&
1646 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001647 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001648 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001649 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001650
1651exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001652 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001653 mbedtls_psa_crypto_free( );
1654 mbedtls_free( buffer );
1655}
1656/* END_CASE */
1657
1658/* BEGIN_CASE */
1659void asymmetric_signature_key_policy( int policy_usage,
1660 int policy_alg,
1661 int key_type,
1662 data_t *key_data,
1663 int exercise_alg )
1664{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001665 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001666 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001667 psa_status_t status;
1668 unsigned char payload[16] = {1};
1669 size_t payload_length = sizeof( payload );
1670 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1671 size_t signature_length;
1672
Gilles Peskine8817f612018-12-18 00:18:46 +01001673 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001674
Gilles Peskine8817f612018-12-18 00:18:46 +01001675 PSA_ASSERT( psa_allocate_key( key_type,
1676 KEY_BITS_FROM_DATA( key_type, key_data ),
1677 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001678 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001679 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001680
Gilles Peskine8817f612018-12-18 00:18:46 +01001681 PSA_ASSERT( psa_import_key( handle, key_type,
1682 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001683
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001684 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001685 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001686 signature, sizeof( signature ),
1687 &signature_length );
1688 if( policy_alg == exercise_alg &&
1689 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001690 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001691 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001692 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001693
1694 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001695 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001696 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001697 signature, sizeof( signature ) );
1698 if( policy_alg == exercise_alg &&
1699 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001700 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001701 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001702 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001703
1704exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001705 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001706 mbedtls_psa_crypto_free( );
1707}
1708/* END_CASE */
1709
1710/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001711void derive_key_policy( int policy_usage,
1712 int policy_alg,
1713 int key_type,
1714 data_t *key_data,
1715 int exercise_alg )
1716{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001717 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001718 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001719 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1720 psa_status_t status;
1721
Gilles Peskine8817f612018-12-18 00:18:46 +01001722 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001723
Gilles Peskine8817f612018-12-18 00:18:46 +01001724 PSA_ASSERT( psa_allocate_key( key_type,
1725 KEY_BITS_FROM_DATA( key_type, key_data ),
1726 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001727 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001728 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001729
Gilles Peskine8817f612018-12-18 00:18:46 +01001730 PSA_ASSERT( psa_import_key( handle, key_type,
1731 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001732
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001733 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001734 exercise_alg,
1735 NULL, 0,
1736 NULL, 0,
1737 1 );
1738 if( policy_alg == exercise_alg &&
1739 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001740 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001741 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001742 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001743
1744exit:
1745 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001746 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001747 mbedtls_psa_crypto_free( );
1748}
1749/* END_CASE */
1750
1751/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001752void agreement_key_policy( int policy_usage,
1753 int policy_alg,
1754 int key_type_arg,
1755 data_t *key_data,
1756 int exercise_alg )
1757{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001758 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001759 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001760 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001761 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1762 psa_status_t status;
1763
Gilles Peskine8817f612018-12-18 00:18:46 +01001764 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001765
Gilles Peskine8817f612018-12-18 00:18:46 +01001766 PSA_ASSERT( psa_allocate_key( key_type,
1767 KEY_BITS_FROM_DATA( key_type, key_data ),
1768 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001769 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001770 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001771
Gilles Peskine8817f612018-12-18 00:18:46 +01001772 PSA_ASSERT( psa_import_key( handle, key_type,
1773 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001774
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001775 status = key_agreement_with_self( &generator, handle, exercise_alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001776
Gilles Peskine01d718c2018-09-18 12:01:02 +02001777 if( policy_alg == exercise_alg &&
1778 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001779 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001780 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001781 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001782
1783exit:
1784 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001785 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001786 mbedtls_psa_crypto_free( );
1787}
1788/* END_CASE */
1789
1790/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001791void hash_operation_init( )
1792{
1793 /* Test each valid way of initializing the object, except for `= {0}`, as
1794 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1795 * though it's OK by the C standard. We could test for this, but we'd need
1796 * to supress the Clang warning for the test. */
1797 psa_hash_operation_t func = psa_hash_operation_init( );
1798 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1799 psa_hash_operation_t zero;
1800
1801 memset( &zero, 0, sizeof( zero ) );
1802
1803 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1804 * specification, we test that all valid ways of initializing the object
1805 * have the same bit pattern. This is a stronger requirement that may not
1806 * be valid on all platforms or PSA Crypto implementations, but implies the
1807 * weaker actual requirement is met: that a freshly initialized object, no
1808 * matter how it was initialized, acts the same as any other valid
1809 * initialization. */
1810 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1811 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1812}
1813/* END_CASE */
1814
1815/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001816void hash_setup( int alg_arg,
1817 int expected_status_arg )
1818{
1819 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001820 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001821 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001822 psa_status_t status;
1823
Gilles Peskine8817f612018-12-18 00:18:46 +01001824 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001825
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001826 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001827 psa_hash_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001828 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001829
1830exit:
1831 mbedtls_psa_crypto_free( );
1832}
1833/* END_CASE */
1834
1835/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02001836void hash_bad_order( )
1837{
1838 unsigned char input[] = "";
1839 /* SHA-256 hash of an empty string */
1840 unsigned char hash[] = {
1841 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1842 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1843 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
1844 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001845 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001846
Gilles Peskine8817f612018-12-18 00:18:46 +01001847 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001848
1849 /* psa_hash_update without calling psa_hash_setup beforehand */
1850 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001851 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001852 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001853
1854 /* psa_hash_verify without calling psa_hash_setup beforehand */
1855 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001856 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001857 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001858
1859 /* psa_hash_finish without calling psa_hash_setup beforehand */
1860 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001861 TEST_EQUAL( psa_hash_finish( &operation,
1862 hash, sizeof( hash ), &hash_len ),
1863 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001864
1865exit:
1866 mbedtls_psa_crypto_free( );
1867}
1868/* END_CASE */
1869
itayzafrir27e69452018-11-01 14:26:34 +02001870/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1871void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001872{
1873 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001874 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1875 * appended to it */
1876 unsigned char hash[] = {
1877 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1878 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1879 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03001880 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001881 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001882
Gilles Peskine8817f612018-12-18 00:18:46 +01001883 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001884
itayzafrir27e69452018-11-01 14:26:34 +02001885 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001886 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001887 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001888 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001889
itayzafrir27e69452018-11-01 14:26:34 +02001890 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001891 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001892 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001893 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001894
itayzafrir27e69452018-11-01 14:26:34 +02001895 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001896 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001897 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001898 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001899
itayzafrirec93d302018-10-18 18:01:10 +03001900exit:
1901 mbedtls_psa_crypto_free( );
1902}
1903/* END_CASE */
1904
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001905/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1906void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001907{
1908 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001909 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03001910 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001911 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001912 size_t hash_len;
1913
Gilles Peskine8817f612018-12-18 00:18:46 +01001914 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001915
itayzafrir58028322018-10-25 10:22:01 +03001916 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001917 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001918 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001919 hash, expected_size - 1, &hash_len ),
1920 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001921
1922exit:
1923 mbedtls_psa_crypto_free( );
1924}
1925/* END_CASE */
1926
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001927/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1928void hash_clone_source_state( )
1929{
1930 psa_algorithm_t alg = PSA_ALG_SHA_256;
1931 unsigned char hash[PSA_HASH_MAX_SIZE];
1932 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1933 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1934 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1935 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1936 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1937 size_t hash_len;
1938
1939 PSA_ASSERT( psa_crypto_init( ) );
1940 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1941
1942 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1943 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1944 PSA_ASSERT( psa_hash_finish( &op_finished,
1945 hash, sizeof( hash ), &hash_len ) );
1946 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1947 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1948
1949 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1950 PSA_ERROR_BAD_STATE );
1951
1952 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1953 PSA_ASSERT( psa_hash_finish( &op_init,
1954 hash, sizeof( hash ), &hash_len ) );
1955 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1956 PSA_ASSERT( psa_hash_finish( &op_finished,
1957 hash, sizeof( hash ), &hash_len ) );
1958 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1959 PSA_ASSERT( psa_hash_finish( &op_aborted,
1960 hash, sizeof( hash ), &hash_len ) );
1961
1962exit:
1963 psa_hash_abort( &op_source );
1964 psa_hash_abort( &op_init );
1965 psa_hash_abort( &op_setup );
1966 psa_hash_abort( &op_finished );
1967 psa_hash_abort( &op_aborted );
1968 mbedtls_psa_crypto_free( );
1969}
1970/* END_CASE */
1971
1972/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1973void hash_clone_target_state( )
1974{
1975 psa_algorithm_t alg = PSA_ALG_SHA_256;
1976 unsigned char hash[PSA_HASH_MAX_SIZE];
1977 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1978 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1979 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1980 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1981 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1982 size_t hash_len;
1983
1984 PSA_ASSERT( psa_crypto_init( ) );
1985
1986 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1987 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1988 PSA_ASSERT( psa_hash_finish( &op_finished,
1989 hash, sizeof( hash ), &hash_len ) );
1990 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1991 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1992
1993 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1994 PSA_ASSERT( psa_hash_finish( &op_target,
1995 hash, sizeof( hash ), &hash_len ) );
1996
1997 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1998 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1999 PSA_ERROR_BAD_STATE );
2000 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2001 PSA_ERROR_BAD_STATE );
2002
2003exit:
2004 psa_hash_abort( &op_target );
2005 psa_hash_abort( &op_init );
2006 psa_hash_abort( &op_setup );
2007 psa_hash_abort( &op_finished );
2008 psa_hash_abort( &op_aborted );
2009 mbedtls_psa_crypto_free( );
2010}
2011/* END_CASE */
2012
itayzafrir58028322018-10-25 10:22:01 +03002013/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002014void mac_operation_init( )
2015{
2016 /* Test each valid way of initializing the object, except for `= {0}`, as
2017 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2018 * though it's OK by the C standard. We could test for this, but we'd need
2019 * to supress the Clang warning for the test. */
2020 psa_mac_operation_t func = psa_mac_operation_init( );
2021 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2022 psa_mac_operation_t zero;
2023
2024 memset( &zero, 0, sizeof( zero ) );
2025
2026 /* Although not technically guaranteed by the C standard nor the PSA Crypto
2027 * specification, we test that all valid ways of initializing the object
2028 * have the same bit pattern. This is a stronger requirement that may not
2029 * be valid on all platforms or PSA Crypto implementations, but implies the
2030 * weaker actual requirement is met: that a freshly initialized object, no
2031 * matter how it was initialized, acts the same as any other valid
2032 * initialization. */
2033 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
2034 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
2035}
2036/* END_CASE */
2037
2038/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002039void mac_setup( int key_type_arg,
2040 data_t *key,
2041 int alg_arg,
2042 int expected_status_arg )
2043{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002044 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002045 psa_key_type_t key_type = key_type_arg;
2046 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002047 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002048 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002049 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002050 psa_status_t status;
2051
Gilles Peskine8817f612018-12-18 00:18:46 +01002052 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002053
Gilles Peskine8817f612018-12-18 00:18:46 +01002054 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2055 &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002056 psa_key_policy_set_usage( &policy,
2057 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2058 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002059 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002060
Gilles Peskine8817f612018-12-18 00:18:46 +01002061 PSA_ASSERT( psa_import_key( handle, key_type,
2062 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002063
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002064 status = psa_mac_sign_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002065 psa_mac_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002066 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002067
2068exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002069 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002070 mbedtls_psa_crypto_free( );
2071}
2072/* END_CASE */
2073
2074/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002075void mac_sign( int key_type_arg,
2076 data_t *key,
2077 int alg_arg,
2078 data_t *input,
2079 data_t *expected_mac )
2080{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002081 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002082 psa_key_type_t key_type = key_type_arg;
2083 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002084 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002085 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002086 /* Leave a little extra room in the output buffer. At the end of the
2087 * test, we'll check that the implementation didn't overwrite onto
2088 * this extra room. */
2089 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2090 size_t mac_buffer_size =
2091 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2092 size_t mac_length = 0;
2093
2094 memset( actual_mac, '+', sizeof( actual_mac ) );
2095 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2096 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2097
Gilles Peskine8817f612018-12-18 00:18:46 +01002098 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002099
Gilles Peskine8817f612018-12-18 00:18:46 +01002100 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2101 &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002102 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002103 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002104
Gilles Peskine8817f612018-12-18 00:18:46 +01002105 PSA_ASSERT( psa_import_key( handle, key_type,
2106 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002107
2108 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002109 PSA_ASSERT( psa_mac_sign_setup( &operation,
2110 handle, alg ) );
2111 PSA_ASSERT( psa_mac_update( &operation,
2112 input->x, input->len ) );
2113 PSA_ASSERT( psa_mac_sign_finish( &operation,
2114 actual_mac, mac_buffer_size,
2115 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002116
2117 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002118 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2119 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002120
2121 /* Verify that the end of the buffer is untouched. */
2122 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2123 sizeof( actual_mac ) - mac_length ) );
2124
2125exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002126 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002127 mbedtls_psa_crypto_free( );
2128}
2129/* END_CASE */
2130
2131/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002132void mac_verify( int key_type_arg,
2133 data_t *key,
2134 int alg_arg,
2135 data_t *input,
2136 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002137{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002138 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002139 psa_key_type_t key_type = key_type_arg;
2140 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002141 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002142 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002143
Gilles Peskine69c12672018-06-28 00:07:19 +02002144 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2145
Gilles Peskine8817f612018-12-18 00:18:46 +01002146 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002147
Gilles Peskine8817f612018-12-18 00:18:46 +01002148 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2149 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002150 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002151 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad16036df908f2018-04-02 08:34:15 -07002152
Gilles Peskine8817f612018-12-18 00:18:46 +01002153 PSA_ASSERT( psa_import_key( handle, key_type,
2154 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002155
Gilles Peskine8817f612018-12-18 00:18:46 +01002156 PSA_ASSERT( psa_mac_verify_setup( &operation,
2157 handle, alg ) );
2158 PSA_ASSERT( psa_destroy_key( handle ) );
2159 PSA_ASSERT( psa_mac_update( &operation,
2160 input->x, input->len ) );
2161 PSA_ASSERT( psa_mac_verify_finish( &operation,
2162 expected_mac->x,
2163 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002164
2165exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002166 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002167 mbedtls_psa_crypto_free( );
2168}
2169/* END_CASE */
2170
2171/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002172void cipher_operation_init( )
2173{
2174 /* Test each valid way of initializing the object, except for `= {0}`, as
2175 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2176 * though it's OK by the C standard. We could test for this, but we'd need
2177 * to supress the Clang warning for the test. */
2178 psa_cipher_operation_t func = psa_cipher_operation_init( );
2179 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2180 psa_cipher_operation_t zero;
2181
2182 memset( &zero, 0, sizeof( zero ) );
2183
2184 /* Although not technically guaranteed by the C standard nor the PSA Crypto
2185 * specification, we test that all valid ways of initializing the object
2186 * have the same bit pattern. This is a stronger requirement that may not
2187 * be valid on all platforms or PSA Crypto implementations, but implies the
2188 * weaker actual requirement is met: that a freshly initialized object, no
2189 * matter how it was initialized, acts the same as any other valid
2190 * initialization. */
2191 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
2192 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
2193}
2194/* END_CASE */
2195
2196/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002197void cipher_setup( int key_type_arg,
2198 data_t *key,
2199 int alg_arg,
2200 int expected_status_arg )
2201{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002202 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002203 psa_key_type_t key_type = key_type_arg;
2204 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002205 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002206 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002207 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002208 psa_status_t status;
2209
Gilles Peskine8817f612018-12-18 00:18:46 +01002210 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002211
Gilles Peskine8817f612018-12-18 00:18:46 +01002212 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2213 &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002214 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002215 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002216
Gilles Peskine8817f612018-12-18 00:18:46 +01002217 PSA_ASSERT( psa_import_key( handle, key_type,
2218 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002219
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002220 status = psa_cipher_encrypt_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002221 psa_cipher_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002222 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002223
2224exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002225 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002226 mbedtls_psa_crypto_free( );
2227}
2228/* END_CASE */
2229
2230/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002231void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002232 data_t *key,
2233 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002234 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002235{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002236 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002237 psa_status_t status;
2238 psa_key_type_t key_type = key_type_arg;
2239 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002240 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002241 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002242 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002243 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002244 size_t output_buffer_size = 0;
2245 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002246 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002247 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002248 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002249
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002250 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2251 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002252
Gilles Peskine8817f612018-12-18 00:18:46 +01002253 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002254
Gilles Peskine8817f612018-12-18 00:18:46 +01002255 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2256 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002257 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002258 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002259
Gilles Peskine8817f612018-12-18 00:18:46 +01002260 PSA_ASSERT( psa_import_key( handle, key_type,
2261 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002262
Gilles Peskine8817f612018-12-18 00:18:46 +01002263 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2264 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002265
Gilles Peskine8817f612018-12-18 00:18:46 +01002266 PSA_ASSERT( psa_cipher_set_iv( &operation,
2267 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002268 output_buffer_size = ( (size_t) input->len +
2269 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002270 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002271
Gilles Peskine8817f612018-12-18 00:18:46 +01002272 PSA_ASSERT( psa_cipher_update( &operation,
2273 input->x, input->len,
2274 output, output_buffer_size,
2275 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002276 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002277 status = psa_cipher_finish( &operation,
2278 output + function_output_length,
2279 output_buffer_size,
2280 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002281 total_output_length += function_output_length;
2282
Gilles Peskinefe11b722018-12-18 00:24:04 +01002283 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002284 if( expected_status == PSA_SUCCESS )
2285 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002286 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002287 ASSERT_COMPARE( expected_output->x, expected_output->len,
2288 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002289 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002290
Gilles Peskine50e586b2018-06-08 14:28:46 +02002291exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002292 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002293 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002294 mbedtls_psa_crypto_free( );
2295}
2296/* END_CASE */
2297
2298/* BEGIN_CASE */
2299void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002300 data_t *key,
2301 data_t *input,
2302 int first_part_size,
2303 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002304{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002305 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002306 psa_key_type_t key_type = key_type_arg;
2307 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002308 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002309 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002310 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002311 size_t output_buffer_size = 0;
2312 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002313 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002314 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002315 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002316
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002317 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2318 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002319
Gilles Peskine8817f612018-12-18 00:18:46 +01002320 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002321
Gilles Peskine8817f612018-12-18 00:18:46 +01002322 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2323 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002324 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002325 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002326
Gilles Peskine8817f612018-12-18 00:18:46 +01002327 PSA_ASSERT( psa_import_key( handle, key_type,
2328 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002329
Gilles Peskine8817f612018-12-18 00:18:46 +01002330 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2331 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002332
Gilles Peskine8817f612018-12-18 00:18:46 +01002333 PSA_ASSERT( psa_cipher_set_iv( &operation,
2334 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002335 output_buffer_size = ( (size_t) input->len +
2336 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002337 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002338
Gilles Peskine4abf7412018-06-18 16:35:34 +02002339 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002340 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2341 output, output_buffer_size,
2342 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002343 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002344 PSA_ASSERT( psa_cipher_update( &operation,
2345 input->x + first_part_size,
2346 input->len - first_part_size,
2347 output, output_buffer_size,
2348 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002349 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002350 PSA_ASSERT( psa_cipher_finish( &operation,
2351 output + function_output_length,
2352 output_buffer_size,
2353 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002354 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002355 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002356
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002357 ASSERT_COMPARE( expected_output->x, expected_output->len,
2358 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002359
2360exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002361 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002362 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002363 mbedtls_psa_crypto_free( );
2364}
2365/* END_CASE */
2366
2367/* BEGIN_CASE */
2368void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002369 data_t *key,
2370 data_t *input,
2371 int first_part_size,
2372 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002373{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002374 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002375
2376 psa_key_type_t key_type = key_type_arg;
2377 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002378 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002379 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002380 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002381 size_t output_buffer_size = 0;
2382 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002383 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002384 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002385 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002386
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002387 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2388 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002389
Gilles Peskine8817f612018-12-18 00:18:46 +01002390 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002391
Gilles Peskine8817f612018-12-18 00:18:46 +01002392 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2393 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002394 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002395 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002396
Gilles Peskine8817f612018-12-18 00:18:46 +01002397 PSA_ASSERT( psa_import_key( handle, key_type,
2398 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002399
Gilles Peskine8817f612018-12-18 00:18:46 +01002400 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2401 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002402
Gilles Peskine8817f612018-12-18 00:18:46 +01002403 PSA_ASSERT( psa_cipher_set_iv( &operation,
2404 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002405
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002406 output_buffer_size = ( (size_t) input->len +
2407 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002408 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002409
Gilles Peskine4abf7412018-06-18 16:35:34 +02002410 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002411 PSA_ASSERT( psa_cipher_update( &operation,
2412 input->x, first_part_size,
2413 output, output_buffer_size,
2414 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002415 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002416 PSA_ASSERT( psa_cipher_update( &operation,
2417 input->x + first_part_size,
2418 input->len - first_part_size,
2419 output, output_buffer_size,
2420 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002421 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002422 PSA_ASSERT( psa_cipher_finish( &operation,
2423 output + function_output_length,
2424 output_buffer_size,
2425 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002426 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002427 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002428
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002429 ASSERT_COMPARE( expected_output->x, expected_output->len,
2430 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002431
2432exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002433 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002434 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002435 mbedtls_psa_crypto_free( );
2436}
2437/* END_CASE */
2438
Gilles Peskine50e586b2018-06-08 14:28:46 +02002439/* BEGIN_CASE */
2440void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002441 data_t *key,
2442 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002443 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002444{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002445 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002446 psa_status_t status;
2447 psa_key_type_t key_type = key_type_arg;
2448 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002449 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002450 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002451 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002452 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002453 size_t output_buffer_size = 0;
2454 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002455 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002456 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002457 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002458
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002459 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2460 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002461
Gilles Peskine8817f612018-12-18 00:18:46 +01002462 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002463
Gilles Peskine8817f612018-12-18 00:18:46 +01002464 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2465 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002466 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002467 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002468
Gilles Peskine8817f612018-12-18 00:18:46 +01002469 PSA_ASSERT( psa_import_key( handle, key_type,
2470 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002471
Gilles Peskine8817f612018-12-18 00:18:46 +01002472 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2473 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002474
Gilles Peskine8817f612018-12-18 00:18:46 +01002475 PSA_ASSERT( psa_cipher_set_iv( &operation,
2476 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002477
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002478 output_buffer_size = ( (size_t) input->len +
2479 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002480 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002481
Gilles Peskine8817f612018-12-18 00:18:46 +01002482 PSA_ASSERT( psa_cipher_update( &operation,
2483 input->x, input->len,
2484 output, output_buffer_size,
2485 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002486 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002487 status = psa_cipher_finish( &operation,
2488 output + function_output_length,
2489 output_buffer_size,
2490 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002491 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002492 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002493
2494 if( expected_status == PSA_SUCCESS )
2495 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002496 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002497 ASSERT_COMPARE( expected_output->x, expected_output->len,
2498 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002499 }
2500
Gilles Peskine50e586b2018-06-08 14:28:46 +02002501exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002502 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002503 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002504 mbedtls_psa_crypto_free( );
2505}
2506/* END_CASE */
2507
Gilles Peskine50e586b2018-06-08 14:28:46 +02002508/* BEGIN_CASE */
2509void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002510 data_t *key,
2511 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002512{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002513 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002514 psa_key_type_t key_type = key_type_arg;
2515 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002516 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002517 size_t iv_size = 16;
2518 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002519 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002520 size_t output1_size = 0;
2521 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002522 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002523 size_t output2_size = 0;
2524 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002525 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002526 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2527 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002528 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002529
Gilles Peskine8817f612018-12-18 00:18:46 +01002530 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002531
Gilles Peskine8817f612018-12-18 00:18:46 +01002532 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2533 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002534 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002535 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002536
Gilles Peskine8817f612018-12-18 00:18:46 +01002537 PSA_ASSERT( psa_import_key( handle, key_type,
2538 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002539
Gilles Peskine8817f612018-12-18 00:18:46 +01002540 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2541 handle, alg ) );
2542 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2543 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002544
Gilles Peskine8817f612018-12-18 00:18:46 +01002545 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2546 iv, iv_size,
2547 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002548 output1_size = ( (size_t) input->len +
2549 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002550 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002551
Gilles Peskine8817f612018-12-18 00:18:46 +01002552 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2553 output1, output1_size,
2554 &output1_length ) );
2555 PSA_ASSERT( psa_cipher_finish( &operation1,
2556 output1 + output1_length, output1_size,
2557 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002558
Gilles Peskine048b7f02018-06-08 14:20:49 +02002559 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002560
Gilles Peskine8817f612018-12-18 00:18:46 +01002561 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002562
2563 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002564 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002565
Gilles Peskine8817f612018-12-18 00:18:46 +01002566 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2567 iv, iv_length ) );
2568 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2569 output2, output2_size,
2570 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002571 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002572 PSA_ASSERT( psa_cipher_finish( &operation2,
2573 output2 + output2_length,
2574 output2_size,
2575 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002576
Gilles Peskine048b7f02018-06-08 14:20:49 +02002577 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002578
Gilles Peskine8817f612018-12-18 00:18:46 +01002579 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002580
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002581 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002582
2583exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002584 mbedtls_free( output1 );
2585 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002586 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03002587 mbedtls_psa_crypto_free( );
2588}
2589/* END_CASE */
2590
2591/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002592void cipher_verify_output_multipart( int alg_arg,
2593 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002594 data_t *key,
2595 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002596 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002597{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002598 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002599 psa_key_type_t key_type = key_type_arg;
2600 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002601 unsigned char iv[16] = {0};
2602 size_t iv_size = 16;
2603 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002604 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002605 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002606 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002607 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002608 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002609 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002610 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002611 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2612 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002613 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002614
Gilles Peskine8817f612018-12-18 00:18:46 +01002615 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002616
Gilles Peskine8817f612018-12-18 00:18:46 +01002617 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2618 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002619 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002620 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002621
Gilles Peskine8817f612018-12-18 00:18:46 +01002622 PSA_ASSERT( psa_import_key( handle, key_type,
2623 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03002624
Gilles Peskine8817f612018-12-18 00:18:46 +01002625 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2626 handle, alg ) );
2627 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2628 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002629
Gilles Peskine8817f612018-12-18 00:18:46 +01002630 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2631 iv, iv_size,
2632 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002633 output1_buffer_size = ( (size_t) input->len +
2634 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002635 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002636
Gilles Peskine4abf7412018-06-18 16:35:34 +02002637 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002638
Gilles Peskine8817f612018-12-18 00:18:46 +01002639 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2640 output1, output1_buffer_size,
2641 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002642 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002643
Gilles Peskine8817f612018-12-18 00:18:46 +01002644 PSA_ASSERT( psa_cipher_update( &operation1,
2645 input->x + first_part_size,
2646 input->len - first_part_size,
2647 output1, output1_buffer_size,
2648 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002649 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002650
Gilles Peskine8817f612018-12-18 00:18:46 +01002651 PSA_ASSERT( psa_cipher_finish( &operation1,
2652 output1 + output1_length,
2653 output1_buffer_size - output1_length,
2654 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002655 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002656
Gilles Peskine8817f612018-12-18 00:18:46 +01002657 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002658
Gilles Peskine048b7f02018-06-08 14:20:49 +02002659 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002660 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002661
Gilles Peskine8817f612018-12-18 00:18:46 +01002662 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2663 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002664
Gilles Peskine8817f612018-12-18 00:18:46 +01002665 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2666 output2, output2_buffer_size,
2667 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002668 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002669
Gilles Peskine8817f612018-12-18 00:18:46 +01002670 PSA_ASSERT( psa_cipher_update( &operation2,
2671 output1 + first_part_size,
2672 output1_length - first_part_size,
2673 output2, output2_buffer_size,
2674 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002675 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002676
Gilles Peskine8817f612018-12-18 00:18:46 +01002677 PSA_ASSERT( psa_cipher_finish( &operation2,
2678 output2 + output2_length,
2679 output2_buffer_size - output2_length,
2680 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002681 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002682
Gilles Peskine8817f612018-12-18 00:18:46 +01002683 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002684
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002685 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002686
2687exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002688 mbedtls_free( output1 );
2689 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002690 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002691 mbedtls_psa_crypto_free( );
2692}
2693/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002694
Gilles Peskine20035e32018-02-03 22:44:14 +01002695/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002696void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002697 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002698 data_t *nonce,
2699 data_t *additional_data,
2700 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002701 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002702{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002703 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002704 psa_key_type_t key_type = key_type_arg;
2705 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002706 unsigned char *output_data = NULL;
2707 size_t output_size = 0;
2708 size_t output_length = 0;
2709 unsigned char *output_data2 = NULL;
2710 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002711 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002712 psa_status_t expected_result = expected_result_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002713 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002714
Gilles Peskine4abf7412018-06-18 16:35:34 +02002715 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002716 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002717
Gilles Peskine8817f612018-12-18 00:18:46 +01002718 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002719
Gilles Peskine8817f612018-12-18 00:18:46 +01002720 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2721 &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002722 psa_key_policy_set_usage( &policy,
2723 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2724 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002725 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002726
Gilles Peskine8817f612018-12-18 00:18:46 +01002727 PSA_ASSERT( psa_import_key( handle, key_type,
2728 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002729
Gilles Peskinefe11b722018-12-18 00:24:04 +01002730 TEST_EQUAL( psa_aead_encrypt( handle, alg,
2731 nonce->x, nonce->len,
2732 additional_data->x,
2733 additional_data->len,
2734 input_data->x, input_data->len,
2735 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002736 &output_length ),
2737 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002738
2739 if( PSA_SUCCESS == expected_result )
2740 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002741 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002742
Gilles Peskinefe11b722018-12-18 00:24:04 +01002743 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2744 nonce->x, nonce->len,
2745 additional_data->x,
2746 additional_data->len,
2747 output_data, output_length,
2748 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002749 &output_length2 ),
2750 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002751
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002752 ASSERT_COMPARE( input_data->x, input_data->len,
2753 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002754 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002755
Gilles Peskinea1cac842018-06-11 19:33:02 +02002756exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002757 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002758 mbedtls_free( output_data );
2759 mbedtls_free( output_data2 );
2760 mbedtls_psa_crypto_free( );
2761}
2762/* END_CASE */
2763
2764/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002765void aead_encrypt( int key_type_arg, data_t *key_data,
2766 int alg_arg,
2767 data_t *nonce,
2768 data_t *additional_data,
2769 data_t *input_data,
2770 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002771{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002772 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002773 psa_key_type_t key_type = key_type_arg;
2774 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002775 unsigned char *output_data = NULL;
2776 size_t output_size = 0;
2777 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002778 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002779 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002780
Gilles Peskine4abf7412018-06-18 16:35:34 +02002781 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002782 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002783
Gilles Peskine8817f612018-12-18 00:18:46 +01002784 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002785
Gilles Peskine8817f612018-12-18 00:18:46 +01002786 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2787 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002788 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002789 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002790
Gilles Peskine8817f612018-12-18 00:18:46 +01002791 PSA_ASSERT( psa_import_key( handle, key_type,
2792 key_data->x,
2793 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002794
Gilles Peskine8817f612018-12-18 00:18:46 +01002795 PSA_ASSERT( psa_aead_encrypt( handle, alg,
2796 nonce->x, nonce->len,
2797 additional_data->x, additional_data->len,
2798 input_data->x, input_data->len,
2799 output_data, output_size,
2800 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002801
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002802 ASSERT_COMPARE( expected_result->x, expected_result->len,
2803 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002804
Gilles Peskinea1cac842018-06-11 19:33:02 +02002805exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002806 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002807 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002808 mbedtls_psa_crypto_free( );
2809}
2810/* END_CASE */
2811
2812/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002813void aead_decrypt( int key_type_arg, data_t *key_data,
2814 int alg_arg,
2815 data_t *nonce,
2816 data_t *additional_data,
2817 data_t *input_data,
2818 data_t *expected_data,
2819 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002820{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002821 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002822 psa_key_type_t key_type = key_type_arg;
2823 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002824 unsigned char *output_data = NULL;
2825 size_t output_size = 0;
2826 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002827 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002828 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002829 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002830
Gilles Peskine4abf7412018-06-18 16:35:34 +02002831 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002832 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002833
Gilles Peskine8817f612018-12-18 00:18:46 +01002834 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002835
Gilles Peskine8817f612018-12-18 00:18:46 +01002836 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2837 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002838 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002839 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002840
Gilles Peskine8817f612018-12-18 00:18:46 +01002841 PSA_ASSERT( psa_import_key( handle, key_type,
2842 key_data->x,
2843 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002844
Gilles Peskinefe11b722018-12-18 00:24:04 +01002845 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2846 nonce->x, nonce->len,
2847 additional_data->x,
2848 additional_data->len,
2849 input_data->x, input_data->len,
2850 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002851 &output_length ),
2852 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002853
Gilles Peskine2d277862018-06-18 15:41:12 +02002854 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002855 ASSERT_COMPARE( expected_data->x, expected_data->len,
2856 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002857
Gilles Peskinea1cac842018-06-11 19:33:02 +02002858exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002859 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002860 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002861 mbedtls_psa_crypto_free( );
2862}
2863/* END_CASE */
2864
2865/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002866void signature_size( int type_arg,
2867 int bits,
2868 int alg_arg,
2869 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002870{
2871 psa_key_type_t type = type_arg;
2872 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002873 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002874 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002875exit:
2876 ;
2877}
2878/* END_CASE */
2879
2880/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002881void sign_deterministic( int key_type_arg, data_t *key_data,
2882 int alg_arg, data_t *input_data,
2883 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002884{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002885 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01002886 psa_key_type_t key_type = key_type_arg;
2887 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002888 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002889 unsigned char *signature = NULL;
2890 size_t signature_size;
2891 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002892 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01002893
Gilles Peskine8817f612018-12-18 00:18:46 +01002894 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002895
Gilles Peskine8817f612018-12-18 00:18:46 +01002896 PSA_ASSERT( psa_allocate_key( key_type,
2897 KEY_BITS_FROM_DATA( key_type, key_data ),
2898 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002899 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002900 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002901
Gilles Peskine8817f612018-12-18 00:18:46 +01002902 PSA_ASSERT( psa_import_key( handle, key_type,
2903 key_data->x,
2904 key_data->len ) );
2905 PSA_ASSERT( psa_get_key_information( handle,
2906 NULL,
2907 &key_bits ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002908
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002909 /* Allocate a buffer which has the size advertized by the
2910 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002911 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2912 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002913 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002914 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002915 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002916
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002917 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002918 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2919 input_data->x, input_data->len,
2920 signature, signature_size,
2921 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002922 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002923 ASSERT_COMPARE( output_data->x, output_data->len,
2924 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002925
2926exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002927 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01002928 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002929 mbedtls_psa_crypto_free( );
2930}
2931/* END_CASE */
2932
2933/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002934void sign_fail( int key_type_arg, data_t *key_data,
2935 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002936 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002937{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002938 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01002939 psa_key_type_t key_type = key_type_arg;
2940 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002941 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002942 psa_status_t actual_status;
2943 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002944 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002945 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002946 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01002947
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002948 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002949
Gilles Peskine8817f612018-12-18 00:18:46 +01002950 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002951
Gilles Peskine8817f612018-12-18 00:18:46 +01002952 PSA_ASSERT( psa_allocate_key( key_type,
2953 KEY_BITS_FROM_DATA( key_type, key_data ),
2954 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002955 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002956 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002957
Gilles Peskine8817f612018-12-18 00:18:46 +01002958 PSA_ASSERT( psa_import_key( handle, key_type,
2959 key_data->x,
2960 key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002961
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002962 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002963 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002964 signature, signature_size,
2965 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002966 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002967 /* The value of *signature_length is unspecified on error, but
2968 * whatever it is, it should be less than signature_size, so that
2969 * if the caller tries to read *signature_length bytes without
2970 * checking the error code then they don't overflow a buffer. */
2971 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002972
2973exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002974 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01002975 mbedtls_free( signature );
2976 mbedtls_psa_crypto_free( );
2977}
2978/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002979
2980/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002981void sign_verify( int key_type_arg, data_t *key_data,
2982 int alg_arg, data_t *input_data )
2983{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002984 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02002985 psa_key_type_t key_type = key_type_arg;
2986 psa_algorithm_t alg = alg_arg;
2987 size_t key_bits;
2988 unsigned char *signature = NULL;
2989 size_t signature_size;
2990 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002991 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02002992
Gilles Peskine8817f612018-12-18 00:18:46 +01002993 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002994
Gilles Peskine8817f612018-12-18 00:18:46 +01002995 PSA_ASSERT( psa_allocate_key( key_type,
2996 KEY_BITS_FROM_DATA( key_type, key_data ),
2997 &handle ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002998 psa_key_policy_set_usage( &policy,
2999 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
3000 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003001 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003002
Gilles Peskine8817f612018-12-18 00:18:46 +01003003 PSA_ASSERT( psa_import_key( handle, key_type,
3004 key_data->x,
3005 key_data->len ) );
3006 PSA_ASSERT( psa_get_key_information( handle,
3007 NULL,
3008 &key_bits ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003009
3010 /* Allocate a buffer which has the size advertized by the
3011 * library. */
3012 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3013 key_bits, alg );
3014 TEST_ASSERT( signature_size != 0 );
3015 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003016 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003017
3018 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003019 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3020 input_data->x, input_data->len,
3021 signature, signature_size,
3022 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003023 /* Check that the signature length looks sensible. */
3024 TEST_ASSERT( signature_length <= signature_size );
3025 TEST_ASSERT( signature_length > 0 );
3026
3027 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003028 PSA_ASSERT( psa_asymmetric_verify(
3029 handle, alg,
3030 input_data->x, input_data->len,
3031 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003032
3033 if( input_data->len != 0 )
3034 {
3035 /* Flip a bit in the input and verify that the signature is now
3036 * detected as invalid. Flip a bit at the beginning, not at the end,
3037 * because ECDSA may ignore the last few bits of the input. */
3038 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003039 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
3040 input_data->x, input_data->len,
3041 signature, signature_length ),
3042 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003043 }
3044
3045exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003046 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003047 mbedtls_free( signature );
3048 mbedtls_psa_crypto_free( );
3049}
3050/* END_CASE */
3051
3052/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003053void asymmetric_verify( int key_type_arg, data_t *key_data,
3054 int alg_arg, data_t *hash_data,
3055 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003056{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003057 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003058 psa_key_type_t key_type = key_type_arg;
3059 psa_algorithm_t alg = alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003060 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003061
Gilles Peskine69c12672018-06-28 00:07:19 +02003062 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3063
Gilles Peskine8817f612018-12-18 00:18:46 +01003064 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003065
Gilles Peskine8817f612018-12-18 00:18:46 +01003066 PSA_ASSERT( psa_allocate_key( key_type,
3067 KEY_BITS_FROM_DATA( key_type, key_data ),
3068 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003069 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003070 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
itayzafrir5c753392018-05-08 11:18:38 +03003071
Gilles Peskine8817f612018-12-18 00:18:46 +01003072 PSA_ASSERT( psa_import_key( handle, key_type,
3073 key_data->x,
3074 key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003075
Gilles Peskine8817f612018-12-18 00:18:46 +01003076 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3077 hash_data->x, hash_data->len,
3078 signature_data->x,
3079 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003080exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003081 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03003082 mbedtls_psa_crypto_free( );
3083}
3084/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003085
3086/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003087void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3088 int alg_arg, data_t *hash_data,
3089 data_t *signature_data,
3090 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003091{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003092 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003093 psa_key_type_t key_type = key_type_arg;
3094 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003095 psa_status_t actual_status;
3096 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003097 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003098
Gilles Peskine8817f612018-12-18 00:18:46 +01003099 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003100
Gilles Peskine8817f612018-12-18 00:18:46 +01003101 PSA_ASSERT( psa_allocate_key( key_type,
3102 KEY_BITS_FROM_DATA( key_type, key_data ),
3103 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003104 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003105 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003106
Gilles Peskine8817f612018-12-18 00:18:46 +01003107 PSA_ASSERT( psa_import_key( handle, key_type,
3108 key_data->x,
3109 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003110
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003111 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003112 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003113 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003114 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003115
Gilles Peskinefe11b722018-12-18 00:24:04 +01003116 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003117
3118exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003119 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003120 mbedtls_psa_crypto_free( );
3121}
3122/* END_CASE */
3123
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003124/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003125void asymmetric_encrypt( int key_type_arg,
3126 data_t *key_data,
3127 int alg_arg,
3128 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003129 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003130 int expected_output_length_arg,
3131 int expected_status_arg )
3132{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003133 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003134 psa_key_type_t key_type = key_type_arg;
3135 psa_algorithm_t alg = alg_arg;
3136 size_t expected_output_length = expected_output_length_arg;
3137 size_t key_bits;
3138 unsigned char *output = NULL;
3139 size_t output_size;
3140 size_t output_length = ~0;
3141 psa_status_t actual_status;
3142 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003143 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003144
Gilles Peskine8817f612018-12-18 00:18:46 +01003145 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003146
Gilles Peskine656896e2018-06-29 19:12:28 +02003147 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01003148 PSA_ASSERT( psa_allocate_key( key_type,
3149 KEY_BITS_FROM_DATA( key_type, key_data ),
3150 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003151 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003152 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
3153 PSA_ASSERT( psa_import_key( handle, key_type,
3154 key_data->x,
3155 key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003156
3157 /* Determine the maximum output length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003158 PSA_ASSERT( psa_get_key_information( handle,
3159 NULL,
3160 &key_bits ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003161 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003162 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003163
3164 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003165 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003166 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003167 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003168 output, output_size,
3169 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003170 TEST_EQUAL( actual_status, expected_status );
3171 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003172
Gilles Peskine68428122018-06-30 18:42:41 +02003173 /* If the label is empty, the test framework puts a non-null pointer
3174 * in label->x. Test that a null pointer works as well. */
3175 if( label->len == 0 )
3176 {
3177 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003178 if( output_size != 0 )
3179 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003180 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003181 input_data->x, input_data->len,
3182 NULL, label->len,
3183 output, output_size,
3184 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003185 TEST_EQUAL( actual_status, expected_status );
3186 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003187 }
3188
Gilles Peskine656896e2018-06-29 19:12:28 +02003189exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003190 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003191 mbedtls_free( output );
3192 mbedtls_psa_crypto_free( );
3193}
3194/* END_CASE */
3195
3196/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003197void asymmetric_encrypt_decrypt( int key_type_arg,
3198 data_t *key_data,
3199 int alg_arg,
3200 data_t *input_data,
3201 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003202{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003203 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003204 psa_key_type_t key_type = key_type_arg;
3205 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003206 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003207 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003208 size_t output_size;
3209 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003210 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003211 size_t output2_size;
3212 size_t output2_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003213 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003214
Gilles Peskine8817f612018-12-18 00:18:46 +01003215 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003216
Gilles Peskine8817f612018-12-18 00:18:46 +01003217 PSA_ASSERT( psa_allocate_key( key_type,
3218 KEY_BITS_FROM_DATA( key_type, key_data ),
3219 &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003220 psa_key_policy_set_usage( &policy,
3221 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003222 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003223 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003224
Gilles Peskine8817f612018-12-18 00:18:46 +01003225 PSA_ASSERT( psa_import_key( handle, key_type,
3226 key_data->x,
3227 key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003228
3229 /* Determine the maximum ciphertext length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003230 PSA_ASSERT( psa_get_key_information( handle,
3231 NULL,
3232 &key_bits ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003233 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003234 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003235 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003236 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003237
Gilles Peskineeebd7382018-06-08 18:11:54 +02003238 /* We test encryption by checking that encrypt-then-decrypt gives back
3239 * the original plaintext because of the non-optional random
3240 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003241 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3242 input_data->x, input_data->len,
3243 label->x, label->len,
3244 output, output_size,
3245 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003246 /* We don't know what ciphertext length to expect, but check that
3247 * it looks sensible. */
3248 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003249
Gilles Peskine8817f612018-12-18 00:18:46 +01003250 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3251 output, output_length,
3252 label->x, label->len,
3253 output2, output2_size,
3254 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003255 ASSERT_COMPARE( input_data->x, input_data->len,
3256 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003257
3258exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003259 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003260 mbedtls_free( output );
3261 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003262 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003263}
3264/* END_CASE */
3265
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003266/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003267void asymmetric_decrypt( int key_type_arg,
3268 data_t *key_data,
3269 int alg_arg,
3270 data_t *input_data,
3271 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003272 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003273{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003274 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003275 psa_key_type_t key_type = key_type_arg;
3276 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003277 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003278 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003279 size_t output_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003280 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003281
Gilles Peskine4abf7412018-06-18 16:35:34 +02003282 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003283 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003284
Gilles Peskine8817f612018-12-18 00:18:46 +01003285 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003286
Gilles Peskine8817f612018-12-18 00:18:46 +01003287 PSA_ASSERT( psa_allocate_key( key_type,
3288 KEY_BITS_FROM_DATA( key_type, key_data ),
3289 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003290 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003291 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003292
Gilles Peskine8817f612018-12-18 00:18:46 +01003293 PSA_ASSERT( psa_import_key( handle, key_type,
3294 key_data->x,
3295 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003296
Gilles Peskine8817f612018-12-18 00:18:46 +01003297 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3298 input_data->x, input_data->len,
3299 label->x, label->len,
3300 output,
3301 output_size,
3302 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003303 ASSERT_COMPARE( expected_data->x, expected_data->len,
3304 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003305
Gilles Peskine68428122018-06-30 18:42:41 +02003306 /* If the label is empty, the test framework puts a non-null pointer
3307 * in label->x. Test that a null pointer works as well. */
3308 if( label->len == 0 )
3309 {
3310 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003311 if( output_size != 0 )
3312 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003313 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3314 input_data->x, input_data->len,
3315 NULL, label->len,
3316 output,
3317 output_size,
3318 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003319 ASSERT_COMPARE( expected_data->x, expected_data->len,
3320 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003321 }
3322
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003323exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003324 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003325 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003326 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003327}
3328/* END_CASE */
3329
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003330/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003331void asymmetric_decrypt_fail( int key_type_arg,
3332 data_t *key_data,
3333 int alg_arg,
3334 data_t *input_data,
3335 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003336 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003337{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003338 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003339 psa_key_type_t key_type = key_type_arg;
3340 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003341 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003342 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003343 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003344 psa_status_t actual_status;
3345 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003346 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003347
Gilles Peskine4abf7412018-06-18 16:35:34 +02003348 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003349 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003350
Gilles Peskine8817f612018-12-18 00:18:46 +01003351 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003352
Gilles Peskine8817f612018-12-18 00:18:46 +01003353 PSA_ASSERT( psa_allocate_key( key_type,
3354 KEY_BITS_FROM_DATA( key_type, key_data ),
3355 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003356 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003357 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003358
Gilles Peskine8817f612018-12-18 00:18:46 +01003359 PSA_ASSERT( psa_import_key( handle, key_type,
3360 key_data->x,
3361 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003362
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003363 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003364 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003365 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003366 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003367 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003368 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003369 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003370
Gilles Peskine68428122018-06-30 18:42:41 +02003371 /* If the label is empty, the test framework puts a non-null pointer
3372 * in label->x. Test that a null pointer works as well. */
3373 if( label->len == 0 )
3374 {
3375 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003376 if( output_size != 0 )
3377 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003378 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003379 input_data->x, input_data->len,
3380 NULL, label->len,
3381 output, output_size,
3382 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003383 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003384 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003385 }
3386
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003387exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003388 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02003389 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003390 mbedtls_psa_crypto_free( );
3391}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003392/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003393
3394/* BEGIN_CASE */
Jaeden Amerod94d6712019-01-04 14:11:48 +00003395void crypto_generator_init( )
3396{
3397 /* Test each valid way of initializing the object, except for `= {0}`, as
3398 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3399 * though it's OK by the C standard. We could test for this, but we'd need
3400 * to supress the Clang warning for the test. */
3401 psa_crypto_generator_t func = psa_crypto_generator_init( );
3402 psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT;
3403 psa_crypto_generator_t zero;
3404
3405 memset( &zero, 0, sizeof( zero ) );
3406
3407 /* Although not technically guaranteed by the C standard nor the PSA Crypto
3408 * specification, we test that all valid ways of initializing the object
3409 * have the same bit pattern. This is a stronger requirement that may not
3410 * be valid on all platforms or PSA Crypto implementations, but implies the
3411 * weaker actual requirement is met: that a freshly initialized object, no
3412 * matter how it was initialized, acts the same as any other valid
3413 * initialization. */
3414 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
3415 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
3416}
3417/* END_CASE */
3418
3419/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003420void derive_setup( int key_type_arg,
3421 data_t *key_data,
3422 int alg_arg,
3423 data_t *salt,
3424 data_t *label,
3425 int requested_capacity_arg,
3426 int expected_status_arg )
3427{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003428 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003429 size_t key_type = key_type_arg;
3430 psa_algorithm_t alg = alg_arg;
3431 size_t requested_capacity = requested_capacity_arg;
3432 psa_status_t expected_status = expected_status_arg;
3433 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003434 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003435
Gilles Peskine8817f612018-12-18 00:18:46 +01003436 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003437
Gilles Peskine8817f612018-12-18 00:18:46 +01003438 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
3439 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003440 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003441 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003442
Gilles Peskine8817f612018-12-18 00:18:46 +01003443 PSA_ASSERT( psa_import_key( handle, key_type,
3444 key_data->x,
3445 key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003446
Gilles Peskinefe11b722018-12-18 00:24:04 +01003447 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3448 salt->x, salt->len,
3449 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003450 requested_capacity ),
3451 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003452
3453exit:
3454 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003455 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003456 mbedtls_psa_crypto_free( );
3457}
3458/* END_CASE */
3459
3460/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003461void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003462{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003463 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003464 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003465 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003466 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003467 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003468 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003469 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3470 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3471 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Jaeden Amero70261c52019-01-04 11:47:20 +00003472 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003473
Gilles Peskine8817f612018-12-18 00:18:46 +01003474 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003475
Gilles Peskine8817f612018-12-18 00:18:46 +01003476 PSA_ASSERT( psa_allocate_key( key_type,
3477 PSA_BYTES_TO_BITS( sizeof( key_data ) ),
3478 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003479 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003480 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003481
Gilles Peskine8817f612018-12-18 00:18:46 +01003482 PSA_ASSERT( psa_import_key( handle, key_type,
3483 key_data,
3484 sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003485
3486 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01003487 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3488 NULL, 0,
3489 NULL, 0,
3490 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003491
3492 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003493 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3494 NULL, 0,
3495 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003496 capacity ),
3497 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003498
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003499 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003500
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003501 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
3502 PSA_ERROR_INSUFFICIENT_CAPACITY );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003503
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003504exit:
3505 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003506 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003507 mbedtls_psa_crypto_free( );
3508}
3509/* END_CASE */
3510
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003511/* BEGIN_CASE */
3512void test_derive_invalid_generator_tests( )
3513{
3514 uint8_t output_buffer[16];
3515 size_t buffer_size = 16;
3516 size_t capacity = 0;
3517 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3518
Nir Sonnenschein50789302018-10-31 12:16:38 +02003519 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003520 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003521
3522 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003523 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003524
Gilles Peskine8817f612018-12-18 00:18:46 +01003525 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003526
Nir Sonnenschein50789302018-10-31 12:16:38 +02003527 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003528 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003529
Nir Sonnenschein50789302018-10-31 12:16:38 +02003530 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003531 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003532
3533exit:
3534 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003535}
3536/* END_CASE */
3537
3538/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003539void derive_output( int alg_arg,
3540 data_t *key_data,
3541 data_t *salt,
3542 data_t *label,
3543 int requested_capacity_arg,
3544 data_t *expected_output1,
3545 data_t *expected_output2 )
3546{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003547 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003548 psa_algorithm_t alg = alg_arg;
3549 size_t requested_capacity = requested_capacity_arg;
3550 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3551 uint8_t *expected_outputs[2] =
3552 {expected_output1->x, expected_output2->x};
3553 size_t output_sizes[2] =
3554 {expected_output1->len, expected_output2->len};
3555 size_t output_buffer_size = 0;
3556 uint8_t *output_buffer = NULL;
3557 size_t expected_capacity;
3558 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003559 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003560 psa_status_t status;
3561 unsigned i;
3562
3563 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3564 {
3565 if( output_sizes[i] > output_buffer_size )
3566 output_buffer_size = output_sizes[i];
3567 if( output_sizes[i] == 0 )
3568 expected_outputs[i] = NULL;
3569 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003570 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003571 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003572
Gilles Peskine8817f612018-12-18 00:18:46 +01003573 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3574 PSA_BYTES_TO_BITS( key_data->len ),
3575 &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003576 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003577 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003578
Gilles Peskine8817f612018-12-18 00:18:46 +01003579 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3580 key_data->x,
3581 key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003582
3583 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003584 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3585 salt->x, salt->len,
3586 label->x, label->len,
3587 requested_capacity ) );
3588 PSA_ASSERT( psa_get_generator_capacity( &generator,
3589 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003590 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003591 expected_capacity = requested_capacity;
3592
3593 /* Expansion phase. */
3594 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3595 {
3596 /* Read some bytes. */
3597 status = psa_generator_read( &generator,
3598 output_buffer, output_sizes[i] );
3599 if( expected_capacity == 0 && output_sizes[i] == 0 )
3600 {
3601 /* Reading 0 bytes when 0 bytes are available can go either way. */
3602 TEST_ASSERT( status == PSA_SUCCESS ||
3603 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3604 continue;
3605 }
3606 else if( expected_capacity == 0 ||
3607 output_sizes[i] > expected_capacity )
3608 {
3609 /* Capacity exceeded. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003610 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003611 expected_capacity = 0;
3612 continue;
3613 }
3614 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003615 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003616 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003617 ASSERT_COMPARE( output_buffer, output_sizes[i],
3618 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003619 /* Check the generator status. */
3620 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01003621 PSA_ASSERT( psa_get_generator_capacity( &generator,
3622 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003623 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003624 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003625 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003626
3627exit:
3628 mbedtls_free( output_buffer );
3629 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003630 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003631 mbedtls_psa_crypto_free( );
3632}
3633/* END_CASE */
3634
3635/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003636void derive_full( int alg_arg,
3637 data_t *key_data,
3638 data_t *salt,
3639 data_t *label,
3640 int requested_capacity_arg )
3641{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003642 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02003643 psa_algorithm_t alg = alg_arg;
3644 size_t requested_capacity = requested_capacity_arg;
3645 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3646 unsigned char output_buffer[16];
3647 size_t expected_capacity = requested_capacity;
3648 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003649 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02003650
Gilles Peskine8817f612018-12-18 00:18:46 +01003651 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003652
Gilles Peskine8817f612018-12-18 00:18:46 +01003653 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3654 PSA_BYTES_TO_BITS( key_data->len ),
3655 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003656 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003657 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003658
Gilles Peskine8817f612018-12-18 00:18:46 +01003659 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3660 key_data->x,
3661 key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003662
3663 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003664 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3665 salt->x, salt->len,
3666 label->x, label->len,
3667 requested_capacity ) );
3668 PSA_ASSERT( psa_get_generator_capacity( &generator,
3669 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003670 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003671
3672 /* Expansion phase. */
3673 while( current_capacity > 0 )
3674 {
3675 size_t read_size = sizeof( output_buffer );
3676 if( read_size > current_capacity )
3677 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01003678 PSA_ASSERT( psa_generator_read( &generator,
3679 output_buffer,
3680 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003681 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01003682 PSA_ASSERT( psa_get_generator_capacity( &generator,
3683 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003684 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003685 }
3686
3687 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003688 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
3689 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskined54931c2018-07-17 21:06:59 +02003690
Gilles Peskine8817f612018-12-18 00:18:46 +01003691 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003692
3693exit:
3694 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003695 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02003696 mbedtls_psa_crypto_free( );
3697}
3698/* END_CASE */
3699
3700/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003701void derive_key_exercise( int alg_arg,
3702 data_t *key_data,
3703 data_t *salt,
3704 data_t *label,
3705 int derived_type_arg,
3706 int derived_bits_arg,
3707 int derived_usage_arg,
3708 int derived_alg_arg )
3709{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003710 psa_key_handle_t base_handle = 0;
3711 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003712 psa_algorithm_t alg = alg_arg;
3713 psa_key_type_t derived_type = derived_type_arg;
3714 size_t derived_bits = derived_bits_arg;
3715 psa_key_usage_t derived_usage = derived_usage_arg;
3716 psa_algorithm_t derived_alg = derived_alg_arg;
3717 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3718 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003719 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003720 psa_key_type_t got_type;
3721 size_t got_bits;
3722
Gilles Peskine8817f612018-12-18 00:18:46 +01003723 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003724
Gilles Peskine8817f612018-12-18 00:18:46 +01003725 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3726 PSA_BYTES_TO_BITS( key_data->len ),
3727 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003728 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003729 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3730 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3731 key_data->x,
3732 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003733
3734 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003735 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3736 salt->x, salt->len,
3737 label->x, label->len,
3738 capacity ) );
3739 PSA_ASSERT( psa_allocate_key( derived_type, derived_bits,
3740 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003741 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003742 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3743 PSA_ASSERT( psa_generator_import_key( derived_handle,
3744 derived_type,
3745 derived_bits,
3746 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003747
3748 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01003749 PSA_ASSERT( psa_get_key_information( derived_handle,
3750 &got_type,
3751 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003752 TEST_EQUAL( got_type, derived_type );
3753 TEST_EQUAL( got_bits, derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003754
3755 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003756 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02003757 goto exit;
3758
3759exit:
3760 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003761 psa_destroy_key( base_handle );
3762 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003763 mbedtls_psa_crypto_free( );
3764}
3765/* END_CASE */
3766
3767/* BEGIN_CASE */
3768void derive_key_export( int alg_arg,
3769 data_t *key_data,
3770 data_t *salt,
3771 data_t *label,
3772 int bytes1_arg,
3773 int bytes2_arg )
3774{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003775 psa_key_handle_t base_handle = 0;
3776 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003777 psa_algorithm_t alg = alg_arg;
3778 size_t bytes1 = bytes1_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003779 size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003780 size_t bytes2 = bytes2_arg;
3781 size_t capacity = bytes1 + bytes2;
3782 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003783 uint8_t *output_buffer = NULL;
3784 uint8_t *export_buffer = NULL;
Jaeden Amero70261c52019-01-04 11:47:20 +00003785 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003786 size_t length;
3787
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003788 ASSERT_ALLOC( output_buffer, capacity );
3789 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01003790 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003791
Gilles Peskine8817f612018-12-18 00:18:46 +01003792 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3793 PSA_BYTES_TO_BITS( key_data->len ),
3794 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003795 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003796 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3797 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3798 key_data->x,
3799 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003800
3801 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003802 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3803 salt->x, salt->len,
3804 label->x, label->len,
3805 capacity ) );
3806 PSA_ASSERT( psa_generator_read( &generator,
3807 output_buffer,
3808 capacity ) );
3809 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003810
3811 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003812 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3813 salt->x, salt->len,
3814 label->x, label->len,
3815 capacity ) );
3816 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, derived_bits,
3817 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003818 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003819 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3820 PSA_ASSERT( psa_generator_import_key( derived_handle,
3821 PSA_KEY_TYPE_RAW_DATA,
3822 derived_bits,
3823 &generator ) );
3824 PSA_ASSERT( psa_export_key( derived_handle,
3825 export_buffer, bytes1,
3826 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003827 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003828 PSA_ASSERT( psa_destroy_key( derived_handle ) );
3829 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA,
3830 PSA_BYTES_TO_BITS( bytes2 ),
3831 &derived_handle ) );
3832 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3833 PSA_ASSERT( psa_generator_import_key( derived_handle,
3834 PSA_KEY_TYPE_RAW_DATA,
3835 PSA_BYTES_TO_BITS( bytes2 ),
3836 &generator ) );
3837 PSA_ASSERT( psa_export_key( derived_handle,
3838 export_buffer + bytes1, bytes2,
3839 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003840 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003841
3842 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003843 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
3844 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003845
3846exit:
3847 mbedtls_free( output_buffer );
3848 mbedtls_free( export_buffer );
3849 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003850 psa_destroy_key( base_handle );
3851 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003852 mbedtls_psa_crypto_free( );
3853}
3854/* END_CASE */
3855
3856/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02003857void key_agreement_setup( int alg_arg,
3858 int our_key_type_arg, data_t *our_key_data,
3859 data_t *peer_key_data,
3860 int expected_status_arg )
3861{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003862 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003863 psa_algorithm_t alg = alg_arg;
3864 psa_key_type_t our_key_type = our_key_type_arg;
3865 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003866 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003867
Gilles Peskine8817f612018-12-18 00:18:46 +01003868 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003869
Gilles Peskine8817f612018-12-18 00:18:46 +01003870 PSA_ASSERT( psa_allocate_key( our_key_type,
3871 KEY_BITS_FROM_DATA( our_key_type,
3872 our_key_data ),
3873 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003874 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003875 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3876 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3877 our_key_data->x,
3878 our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003879
Gilles Peskinefe11b722018-12-18 00:24:04 +01003880 TEST_EQUAL( psa_key_agreement( &generator,
3881 our_key,
3882 peer_key_data->x, peer_key_data->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003883 alg ),
3884 expected_status_arg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003885
3886exit:
3887 psa_generator_abort( &generator );
3888 psa_destroy_key( our_key );
3889 mbedtls_psa_crypto_free( );
3890}
3891/* END_CASE */
3892
3893/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02003894void key_agreement_capacity( int alg_arg,
3895 int our_key_type_arg, data_t *our_key_data,
3896 data_t *peer_key_data,
3897 int expected_capacity_arg )
3898{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003899 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003900 psa_algorithm_t alg = alg_arg;
3901 psa_key_type_t our_key_type = our_key_type_arg;
3902 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003903 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02003904 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02003905 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02003906
Gilles Peskine8817f612018-12-18 00:18:46 +01003907 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003908
Gilles Peskine8817f612018-12-18 00:18:46 +01003909 PSA_ASSERT( psa_allocate_key( our_key_type,
3910 KEY_BITS_FROM_DATA( our_key_type,
3911 our_key_data ),
3912 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003913 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003914 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3915 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3916 our_key_data->x,
3917 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003918
Gilles Peskine8817f612018-12-18 00:18:46 +01003919 PSA_ASSERT( psa_key_agreement( &generator,
3920 our_key,
3921 peer_key_data->x, peer_key_data->len,
3922 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003923
Gilles Peskinebf491972018-10-25 22:36:12 +02003924 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003925 PSA_ASSERT( psa_get_generator_capacity(
3926 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003927 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02003928
Gilles Peskinebf491972018-10-25 22:36:12 +02003929 /* Test the actual capacity by reading the output. */
3930 while( actual_capacity > sizeof( output ) )
3931 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003932 PSA_ASSERT( psa_generator_read( &generator,
3933 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02003934 actual_capacity -= sizeof( output );
3935 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003936 PSA_ASSERT( psa_generator_read( &generator,
3937 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003938 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
3939 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskinebf491972018-10-25 22:36:12 +02003940
Gilles Peskine59685592018-09-18 12:11:34 +02003941exit:
3942 psa_generator_abort( &generator );
3943 psa_destroy_key( our_key );
3944 mbedtls_psa_crypto_free( );
3945}
3946/* END_CASE */
3947
3948/* BEGIN_CASE */
3949void key_agreement_output( int alg_arg,
3950 int our_key_type_arg, data_t *our_key_data,
3951 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003952 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02003953{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003954 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003955 psa_algorithm_t alg = alg_arg;
3956 psa_key_type_t our_key_type = our_key_type_arg;
3957 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003958 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003959 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02003960
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003961 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
3962 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003963
Gilles Peskine8817f612018-12-18 00:18:46 +01003964 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003965
Gilles Peskine8817f612018-12-18 00:18:46 +01003966 PSA_ASSERT( psa_allocate_key( our_key_type,
3967 KEY_BITS_FROM_DATA( our_key_type,
3968 our_key_data ),
3969 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003970 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003971 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3972 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3973 our_key_data->x,
3974 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003975
Gilles Peskine8817f612018-12-18 00:18:46 +01003976 PSA_ASSERT( psa_key_agreement( &generator,
3977 our_key,
3978 peer_key_data->x, peer_key_data->len,
3979 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003980
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003981 PSA_ASSERT( psa_generator_read( &generator,
3982 actual_output,
3983 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003984 ASSERT_COMPARE( actual_output, expected_output1->len,
3985 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003986 if( expected_output2->len != 0 )
3987 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003988 PSA_ASSERT( psa_generator_read( &generator,
3989 actual_output,
3990 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003991 ASSERT_COMPARE( actual_output, expected_output2->len,
3992 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003993 }
Gilles Peskine59685592018-09-18 12:11:34 +02003994
3995exit:
3996 psa_generator_abort( &generator );
3997 psa_destroy_key( our_key );
3998 mbedtls_psa_crypto_free( );
3999 mbedtls_free( actual_output );
4000}
4001/* END_CASE */
4002
4003/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004004void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004005{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004006 size_t bytes = bytes_arg;
4007 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004008 unsigned char *output = NULL;
4009 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004010 size_t i;
4011 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004012
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004013 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
4014 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004015 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004016
Gilles Peskine8817f612018-12-18 00:18:46 +01004017 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004018
Gilles Peskinea50d7392018-06-21 10:22:13 +02004019 /* Run several times, to ensure that every output byte will be
4020 * nonzero at least once with overwhelming probability
4021 * (2^(-8*number_of_runs)). */
4022 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004023 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004024 if( bytes != 0 )
4025 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004026 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004027
4028 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004029 ASSERT_COMPARE( output + bytes, sizeof( trail ),
4030 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004031
4032 for( i = 0; i < bytes; i++ )
4033 {
4034 if( output[i] != 0 )
4035 ++changed[i];
4036 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004037 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004038
4039 /* Check that every byte was changed to nonzero at least once. This
4040 * validates that psa_generate_random is overwriting every byte of
4041 * the output buffer. */
4042 for( i = 0; i < bytes; i++ )
4043 {
4044 TEST_ASSERT( changed[i] != 0 );
4045 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004046
4047exit:
4048 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004049 mbedtls_free( output );
4050 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004051}
4052/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004053
4054/* BEGIN_CASE */
4055void generate_key( int type_arg,
4056 int bits_arg,
4057 int usage_arg,
4058 int alg_arg,
4059 int expected_status_arg )
4060{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004061 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004062 psa_key_type_t type = type_arg;
4063 psa_key_usage_t usage = usage_arg;
4064 size_t bits = bits_arg;
4065 psa_algorithm_t alg = alg_arg;
4066 psa_status_t expected_status = expected_status_arg;
4067 psa_key_type_t got_type;
4068 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004069 psa_status_t expected_info_status =
4070 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004071 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004072
Gilles Peskine8817f612018-12-18 00:18:46 +01004073 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004074
Gilles Peskine8817f612018-12-18 00:18:46 +01004075 PSA_ASSERT( psa_allocate_key( type, bits, &handle ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004076 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004077 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004078
4079 /* Generate a key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004080 TEST_EQUAL( psa_generate_key( handle, type, bits, NULL, 0 ),
4081 expected_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004082
4083 /* Test the key information */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004084 TEST_EQUAL( psa_get_key_information( handle, &got_type, &got_bits ),
4085 expected_info_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004086 if( expected_info_status != PSA_SUCCESS )
4087 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004088 TEST_EQUAL( got_type, type );
4089 TEST_EQUAL( got_bits, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004090
Gilles Peskine818ca122018-06-20 18:16:48 +02004091 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004092 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004093 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004094
4095exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004096 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004097 mbedtls_psa_crypto_free( );
4098}
4099/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004100
Darryl Greend49a4992018-06-18 17:27:26 +01004101/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
4102void persistent_key_load_key_from_storage( data_t *data, int type_arg,
4103 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00004104 int alg_arg, int generation_method,
4105 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01004106{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004107 psa_key_handle_t handle = 0;
4108 psa_key_handle_t base_key;
Darryl Greend49a4992018-06-18 17:27:26 +01004109 psa_key_type_t type = (psa_key_type_t) type_arg;
4110 psa_key_type_t type_get;
4111 size_t bits_get;
Jaeden Amero70261c52019-01-04 11:47:20 +00004112 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
4113 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004114 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
4115 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00004116 psa_key_policy_t base_policy_set = PSA_KEY_POLICY_INIT;
Darryl Green0c6575a2018-11-07 16:05:30 +00004117 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
4118 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004119 unsigned char *first_export = NULL;
4120 unsigned char *second_export = NULL;
4121 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4122 size_t first_exported_length;
4123 size_t second_exported_length;
4124
4125 ASSERT_ALLOC( first_export, export_size );
4126 ASSERT_ALLOC( second_export, export_size );
4127
Gilles Peskine8817f612018-12-18 00:18:46 +01004128 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004129
Gilles Peskine8817f612018-12-18 00:18:46 +01004130 PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4131 type, bits,
4132 &handle ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004133 psa_key_policy_set_usage( &policy_set, policy_usage,
4134 policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004135 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004136
Darryl Green0c6575a2018-11-07 16:05:30 +00004137 switch( generation_method )
4138 {
4139 case IMPORT_KEY:
4140 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004141 PSA_ASSERT( psa_import_key( handle, type,
4142 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004143 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004144
Darryl Green0c6575a2018-11-07 16:05:30 +00004145 case GENERATE_KEY:
4146 /* Generate a key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004147 PSA_ASSERT( psa_generate_key( handle, type, bits,
4148 NULL, 0 ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004149 break;
4150
4151 case DERIVE_KEY:
4152 /* Create base key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004153 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
4154 PSA_BYTES_TO_BITS( data->len ),
4155 &base_key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004156 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
4157 base_policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004158 PSA_ASSERT( psa_set_key_policy(
4159 base_key, &base_policy_set ) );
4160 PSA_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
4161 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004162 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004163 PSA_ASSERT( psa_key_derivation( &generator, base_key,
4164 base_policy_alg,
4165 NULL, 0, NULL, 0,
4166 export_size ) );
4167 PSA_ASSERT( psa_generator_import_key(
4168 handle, PSA_KEY_TYPE_RAW_DATA,
4169 bits, &generator ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004170 break;
4171 }
Darryl Greend49a4992018-06-18 17:27:26 +01004172
4173 /* Export the key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004174 TEST_EQUAL( psa_export_key( handle,
4175 first_export, export_size,
4176 &first_exported_length ),
4177 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004178
4179 /* Shutdown and restart */
4180 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004181 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004182
Darryl Greend49a4992018-06-18 17:27:26 +01004183 /* Check key slot still contains key data */
Gilles Peskine8817f612018-12-18 00:18:46 +01004184 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4185 &handle ) );
4186 PSA_ASSERT( psa_get_key_information(
4187 handle, &type_get, &bits_get ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004188 TEST_EQUAL( type_get, type );
4189 TEST_EQUAL( bits_get, (size_t) bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004190
Gilles Peskine8817f612018-12-18 00:18:46 +01004191 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004192 TEST_EQUAL( psa_key_policy_get_usage( &policy_get ), policy_usage );
4193 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_get ), policy_alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004194
4195 /* Export the key again */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004196 TEST_EQUAL( psa_export_key( handle,
4197 second_export, export_size,
4198 &second_exported_length ),
4199 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004200
Darryl Green0c6575a2018-11-07 16:05:30 +00004201 if( export_status == PSA_SUCCESS )
4202 {
4203 ASSERT_COMPARE( first_export, first_exported_length,
4204 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01004205
Darryl Green0c6575a2018-11-07 16:05:30 +00004206 switch( generation_method )
4207 {
4208 case IMPORT_KEY:
4209 ASSERT_COMPARE( data->x, data->len,
4210 first_export, first_exported_length );
4211 break;
4212 default:
4213 break;
4214 }
4215 }
4216
4217 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004218 if( ! exercise_key( handle, policy_usage, policy_alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004219 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004220
4221exit:
4222 mbedtls_free( first_export );
4223 mbedtls_free( second_export );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004224 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004225 mbedtls_psa_crypto_free();
4226}
4227/* END_CASE */