blob: e821165c133f55d60796186419a0775f4d53a0df [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{
168 psa_cipher_operation_t operation;
169 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;
Moran Pekerce500072018-11-07 16:20:07 +02001156 psa_cipher_operation_t operation;
1157 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;
Moran Pekerce500072018-11-07 16:20:07 +02001213 psa_cipher_operation_t operation;
1214 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;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001495 psa_cipher_operation_t operation;
1496 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
1927/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001928void mac_operation_init( )
1929{
1930 /* Test each valid way of initializing the object, except for `= {0}`, as
1931 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1932 * though it's OK by the C standard. We could test for this, but we'd need
1933 * to supress the Clang warning for the test. */
1934 psa_mac_operation_t func = psa_mac_operation_init( );
1935 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1936 psa_mac_operation_t zero;
1937
1938 memset( &zero, 0, sizeof( zero ) );
1939
1940 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1941 * specification, we test that all valid ways of initializing the object
1942 * have the same bit pattern. This is a stronger requirement that may not
1943 * be valid on all platforms or PSA Crypto implementations, but implies the
1944 * weaker actual requirement is met: that a freshly initialized object, no
1945 * matter how it was initialized, acts the same as any other valid
1946 * initialization. */
1947 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1948 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1949}
1950/* END_CASE */
1951
1952/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001953void mac_setup( int key_type_arg,
1954 data_t *key,
1955 int alg_arg,
1956 int expected_status_arg )
1957{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001958 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001959 psa_key_type_t key_type = key_type_arg;
1960 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001961 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001962 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00001963 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001964 psa_status_t status;
1965
Gilles Peskine8817f612018-12-18 00:18:46 +01001966 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001967
Gilles Peskine8817f612018-12-18 00:18:46 +01001968 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
1969 &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001970 psa_key_policy_set_usage( &policy,
1971 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1972 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001973 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001974
Gilles Peskine8817f612018-12-18 00:18:46 +01001975 PSA_ASSERT( psa_import_key( handle, key_type,
1976 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001977
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001978 status = psa_mac_sign_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001979 psa_mac_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001980 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001981
1982exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001983 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001984 mbedtls_psa_crypto_free( );
1985}
1986/* END_CASE */
1987
1988/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001989void mac_sign( int key_type_arg,
1990 data_t *key,
1991 int alg_arg,
1992 data_t *input,
1993 data_t *expected_mac )
1994{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001995 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001996 psa_key_type_t key_type = key_type_arg;
1997 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001998 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00001999 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002000 /* Leave a little extra room in the output buffer. At the end of the
2001 * test, we'll check that the implementation didn't overwrite onto
2002 * this extra room. */
2003 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2004 size_t mac_buffer_size =
2005 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2006 size_t mac_length = 0;
2007
2008 memset( actual_mac, '+', sizeof( actual_mac ) );
2009 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2010 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2011
Gilles Peskine8817f612018-12-18 00:18:46 +01002012 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002013
Gilles Peskine8817f612018-12-18 00:18:46 +01002014 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2015 &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002016 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002017 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002018
Gilles Peskine8817f612018-12-18 00:18:46 +01002019 PSA_ASSERT( psa_import_key( handle, key_type,
2020 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002021
2022 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002023 PSA_ASSERT( psa_mac_sign_setup( &operation,
2024 handle, alg ) );
2025 PSA_ASSERT( psa_mac_update( &operation,
2026 input->x, input->len ) );
2027 PSA_ASSERT( psa_mac_sign_finish( &operation,
2028 actual_mac, mac_buffer_size,
2029 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002030
2031 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002032 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2033 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002034
2035 /* Verify that the end of the buffer is untouched. */
2036 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2037 sizeof( actual_mac ) - mac_length ) );
2038
2039exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002040 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002041 mbedtls_psa_crypto_free( );
2042}
2043/* END_CASE */
2044
2045/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002046void mac_verify( int key_type_arg,
2047 data_t *key,
2048 int alg_arg,
2049 data_t *input,
2050 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002051{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002052 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002053 psa_key_type_t key_type = key_type_arg;
2054 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002055 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002056 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002057
Gilles Peskine69c12672018-06-28 00:07:19 +02002058 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2059
Gilles Peskine8817f612018-12-18 00:18:46 +01002060 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002061
Gilles Peskine8817f612018-12-18 00:18:46 +01002062 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2063 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002064 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002065 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad16036df908f2018-04-02 08:34:15 -07002066
Gilles Peskine8817f612018-12-18 00:18:46 +01002067 PSA_ASSERT( psa_import_key( handle, key_type,
2068 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002069
Gilles Peskine8817f612018-12-18 00:18:46 +01002070 PSA_ASSERT( psa_mac_verify_setup( &operation,
2071 handle, alg ) );
2072 PSA_ASSERT( psa_destroy_key( handle ) );
2073 PSA_ASSERT( psa_mac_update( &operation,
2074 input->x, input->len ) );
2075 PSA_ASSERT( psa_mac_verify_finish( &operation,
2076 expected_mac->x,
2077 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002078
2079exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002080 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002081 mbedtls_psa_crypto_free( );
2082}
2083/* END_CASE */
2084
2085/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002086void cipher_setup( int key_type_arg,
2087 data_t *key,
2088 int alg_arg,
2089 int expected_status_arg )
2090{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002091 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002092 psa_key_type_t key_type = key_type_arg;
2093 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002094 psa_status_t expected_status = expected_status_arg;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002095 psa_cipher_operation_t operation;
Jaeden Amero70261c52019-01-04 11:47:20 +00002096 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002097 psa_status_t status;
2098
Gilles Peskine8817f612018-12-18 00:18:46 +01002099 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002100
Gilles Peskine8817f612018-12-18 00:18:46 +01002101 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2102 &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002103 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002104 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002105
Gilles Peskine8817f612018-12-18 00:18:46 +01002106 PSA_ASSERT( psa_import_key( handle, key_type,
2107 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002108
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002109 status = psa_cipher_encrypt_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002110 psa_cipher_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002111 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002112
2113exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002114 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002115 mbedtls_psa_crypto_free( );
2116}
2117/* END_CASE */
2118
2119/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002120void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002121 data_t *key,
2122 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002123 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002124{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002125 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002126 psa_status_t status;
2127 psa_key_type_t key_type = key_type_arg;
2128 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002129 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002130 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002131 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002132 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002133 size_t output_buffer_size = 0;
2134 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002135 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002136 psa_cipher_operation_t operation;
Jaeden Amero70261c52019-01-04 11:47:20 +00002137 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002138
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002139 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2140 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002141
Gilles Peskine8817f612018-12-18 00:18:46 +01002142 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002143
Gilles Peskine8817f612018-12-18 00:18:46 +01002144 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2145 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002146 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002147 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002148
Gilles Peskine8817f612018-12-18 00:18:46 +01002149 PSA_ASSERT( psa_import_key( handle, key_type,
2150 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002151
Gilles Peskine8817f612018-12-18 00:18:46 +01002152 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2153 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002154
Gilles Peskine8817f612018-12-18 00:18:46 +01002155 PSA_ASSERT( psa_cipher_set_iv( &operation,
2156 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002157 output_buffer_size = ( (size_t) input->len +
2158 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002159 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002160
Gilles Peskine8817f612018-12-18 00:18:46 +01002161 PSA_ASSERT( psa_cipher_update( &operation,
2162 input->x, input->len,
2163 output, output_buffer_size,
2164 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002165 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002166 status = psa_cipher_finish( &operation,
2167 output + function_output_length,
2168 output_buffer_size,
2169 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002170 total_output_length += function_output_length;
2171
Gilles Peskinefe11b722018-12-18 00:24:04 +01002172 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002173 if( expected_status == PSA_SUCCESS )
2174 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002175 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002176 ASSERT_COMPARE( expected_output->x, expected_output->len,
2177 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002178 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002179
Gilles Peskine50e586b2018-06-08 14:28:46 +02002180exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002181 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002182 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002183 mbedtls_psa_crypto_free( );
2184}
2185/* END_CASE */
2186
2187/* BEGIN_CASE */
2188void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002189 data_t *key,
2190 data_t *input,
2191 int first_part_size,
2192 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002193{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002194 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002195 psa_key_type_t key_type = key_type_arg;
2196 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002197 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002198 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002199 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002200 size_t output_buffer_size = 0;
2201 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002202 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002203 psa_cipher_operation_t operation;
Jaeden Amero70261c52019-01-04 11:47:20 +00002204 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002205
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002206 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2207 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002208
Gilles Peskine8817f612018-12-18 00:18:46 +01002209 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002210
Gilles Peskine8817f612018-12-18 00:18:46 +01002211 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2212 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002213 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002214 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002215
Gilles Peskine8817f612018-12-18 00:18:46 +01002216 PSA_ASSERT( psa_import_key( handle, key_type,
2217 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002218
Gilles Peskine8817f612018-12-18 00:18:46 +01002219 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2220 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002221
Gilles Peskine8817f612018-12-18 00:18:46 +01002222 PSA_ASSERT( psa_cipher_set_iv( &operation,
2223 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002224 output_buffer_size = ( (size_t) input->len +
2225 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002226 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002227
Gilles Peskine4abf7412018-06-18 16:35:34 +02002228 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002229 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2230 output, output_buffer_size,
2231 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002232 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002233 PSA_ASSERT( psa_cipher_update( &operation,
2234 input->x + first_part_size,
2235 input->len - first_part_size,
2236 output, output_buffer_size,
2237 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002238 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002239 PSA_ASSERT( psa_cipher_finish( &operation,
2240 output + function_output_length,
2241 output_buffer_size,
2242 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002243 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002244 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002245
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002246 ASSERT_COMPARE( expected_output->x, expected_output->len,
2247 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002248
2249exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002250 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002251 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002252 mbedtls_psa_crypto_free( );
2253}
2254/* END_CASE */
2255
2256/* BEGIN_CASE */
2257void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002258 data_t *key,
2259 data_t *input,
2260 int first_part_size,
2261 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002262{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002263 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002264
2265 psa_key_type_t key_type = key_type_arg;
2266 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002267 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002268 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002269 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002270 size_t output_buffer_size = 0;
2271 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002272 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002273 psa_cipher_operation_t operation;
Jaeden Amero70261c52019-01-04 11:47:20 +00002274 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002275
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002276 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2277 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002278
Gilles Peskine8817f612018-12-18 00:18:46 +01002279 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002280
Gilles Peskine8817f612018-12-18 00:18:46 +01002281 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2282 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002283 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002284 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002285
Gilles Peskine8817f612018-12-18 00:18:46 +01002286 PSA_ASSERT( psa_import_key( handle, key_type,
2287 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002288
Gilles Peskine8817f612018-12-18 00:18:46 +01002289 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2290 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002291
Gilles Peskine8817f612018-12-18 00:18:46 +01002292 PSA_ASSERT( psa_cipher_set_iv( &operation,
2293 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002294
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002295 output_buffer_size = ( (size_t) input->len +
2296 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002297 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002298
Gilles Peskine4abf7412018-06-18 16:35:34 +02002299 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002300 PSA_ASSERT( psa_cipher_update( &operation,
2301 input->x, first_part_size,
2302 output, output_buffer_size,
2303 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002304 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002305 PSA_ASSERT( psa_cipher_update( &operation,
2306 input->x + first_part_size,
2307 input->len - first_part_size,
2308 output, output_buffer_size,
2309 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002310 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002311 PSA_ASSERT( psa_cipher_finish( &operation,
2312 output + function_output_length,
2313 output_buffer_size,
2314 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002315 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002316 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002317
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002318 ASSERT_COMPARE( expected_output->x, expected_output->len,
2319 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002320
2321exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002322 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002323 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002324 mbedtls_psa_crypto_free( );
2325}
2326/* END_CASE */
2327
Gilles Peskine50e586b2018-06-08 14:28:46 +02002328/* BEGIN_CASE */
2329void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002330 data_t *key,
2331 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002332 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002333{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002334 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002335 psa_status_t status;
2336 psa_key_type_t key_type = key_type_arg;
2337 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002338 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002339 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002340 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002341 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002342 size_t output_buffer_size = 0;
2343 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002344 size_t total_output_length = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002345 psa_cipher_operation_t operation;
Jaeden Amero70261c52019-01-04 11:47:20 +00002346 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002347
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002348 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2349 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002350
Gilles Peskine8817f612018-12-18 00:18:46 +01002351 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002352
Gilles Peskine8817f612018-12-18 00:18:46 +01002353 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2354 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002355 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002356 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002357
Gilles Peskine8817f612018-12-18 00:18:46 +01002358 PSA_ASSERT( psa_import_key( handle, key_type,
2359 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002360
Gilles Peskine8817f612018-12-18 00:18:46 +01002361 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2362 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002363
Gilles Peskine8817f612018-12-18 00:18:46 +01002364 PSA_ASSERT( psa_cipher_set_iv( &operation,
2365 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002366
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002367 output_buffer_size = ( (size_t) input->len +
2368 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002369 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002370
Gilles Peskine8817f612018-12-18 00:18:46 +01002371 PSA_ASSERT( psa_cipher_update( &operation,
2372 input->x, input->len,
2373 output, output_buffer_size,
2374 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002375 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002376 status = psa_cipher_finish( &operation,
2377 output + function_output_length,
2378 output_buffer_size,
2379 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002380 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002381 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002382
2383 if( expected_status == PSA_SUCCESS )
2384 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002385 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002386 ASSERT_COMPARE( expected_output->x, expected_output->len,
2387 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002388 }
2389
Gilles Peskine50e586b2018-06-08 14:28:46 +02002390exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002391 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002392 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002393 mbedtls_psa_crypto_free( );
2394}
2395/* END_CASE */
2396
Gilles Peskine50e586b2018-06-08 14:28:46 +02002397/* BEGIN_CASE */
2398void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002399 data_t *key,
2400 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002401{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002402 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002403 psa_key_type_t key_type = key_type_arg;
2404 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002405 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002406 size_t iv_size = 16;
2407 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002408 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002409 size_t output1_size = 0;
2410 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002411 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002412 size_t output2_size = 0;
2413 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002414 size_t function_output_length = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002415 psa_cipher_operation_t operation1;
2416 psa_cipher_operation_t operation2;
Jaeden Amero70261c52019-01-04 11:47:20 +00002417 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002418
Gilles Peskine8817f612018-12-18 00:18:46 +01002419 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002420
Gilles Peskine8817f612018-12-18 00:18:46 +01002421 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2422 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002423 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002424 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002425
Gilles Peskine8817f612018-12-18 00:18:46 +01002426 PSA_ASSERT( psa_import_key( handle, key_type,
2427 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002428
Gilles Peskine8817f612018-12-18 00:18:46 +01002429 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2430 handle, alg ) );
2431 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2432 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002433
Gilles Peskine8817f612018-12-18 00:18:46 +01002434 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2435 iv, iv_size,
2436 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002437 output1_size = ( (size_t) input->len +
2438 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002439 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002440
Gilles Peskine8817f612018-12-18 00:18:46 +01002441 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2442 output1, output1_size,
2443 &output1_length ) );
2444 PSA_ASSERT( psa_cipher_finish( &operation1,
2445 output1 + output1_length, output1_size,
2446 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002447
Gilles Peskine048b7f02018-06-08 14:20:49 +02002448 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002449
Gilles Peskine8817f612018-12-18 00:18:46 +01002450 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002451
2452 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002453 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002454
Gilles Peskine8817f612018-12-18 00:18:46 +01002455 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2456 iv, iv_length ) );
2457 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2458 output2, output2_size,
2459 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002460 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002461 PSA_ASSERT( psa_cipher_finish( &operation2,
2462 output2 + output2_length,
2463 output2_size,
2464 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002465
Gilles Peskine048b7f02018-06-08 14:20:49 +02002466 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002467
Gilles Peskine8817f612018-12-18 00:18:46 +01002468 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002469
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002470 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002471
2472exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002473 mbedtls_free( output1 );
2474 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002475 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03002476 mbedtls_psa_crypto_free( );
2477}
2478/* END_CASE */
2479
2480/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002481void cipher_verify_output_multipart( int alg_arg,
2482 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002483 data_t *key,
2484 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002485 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002486{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002487 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002488 psa_key_type_t key_type = key_type_arg;
2489 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002490 unsigned char iv[16] = {0};
2491 size_t iv_size = 16;
2492 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002493 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002494 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002495 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002496 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002497 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002498 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002499 size_t function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002500 psa_cipher_operation_t operation1;
2501 psa_cipher_operation_t operation2;
Jaeden Amero70261c52019-01-04 11:47:20 +00002502 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002503
Gilles Peskine8817f612018-12-18 00:18:46 +01002504 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002505
Gilles Peskine8817f612018-12-18 00:18:46 +01002506 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2507 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002508 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002509 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002510
Gilles Peskine8817f612018-12-18 00:18:46 +01002511 PSA_ASSERT( psa_import_key( handle, key_type,
2512 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03002513
Gilles Peskine8817f612018-12-18 00:18:46 +01002514 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2515 handle, alg ) );
2516 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2517 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002518
Gilles Peskine8817f612018-12-18 00:18:46 +01002519 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2520 iv, iv_size,
2521 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002522 output1_buffer_size = ( (size_t) input->len +
2523 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002524 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002525
Gilles Peskine4abf7412018-06-18 16:35:34 +02002526 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002527
Gilles Peskine8817f612018-12-18 00:18:46 +01002528 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2529 output1, output1_buffer_size,
2530 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002531 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002532
Gilles Peskine8817f612018-12-18 00:18:46 +01002533 PSA_ASSERT( psa_cipher_update( &operation1,
2534 input->x + first_part_size,
2535 input->len - first_part_size,
2536 output1, output1_buffer_size,
2537 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002538 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002539
Gilles Peskine8817f612018-12-18 00:18:46 +01002540 PSA_ASSERT( psa_cipher_finish( &operation1,
2541 output1 + output1_length,
2542 output1_buffer_size - output1_length,
2543 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002544 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002545
Gilles Peskine8817f612018-12-18 00:18:46 +01002546 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002547
Gilles Peskine048b7f02018-06-08 14:20:49 +02002548 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002549 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002550
Gilles Peskine8817f612018-12-18 00:18:46 +01002551 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2552 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002553
Gilles Peskine8817f612018-12-18 00:18:46 +01002554 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2555 output2, output2_buffer_size,
2556 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002557 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002558
Gilles Peskine8817f612018-12-18 00:18:46 +01002559 PSA_ASSERT( psa_cipher_update( &operation2,
2560 output1 + first_part_size,
2561 output1_length - first_part_size,
2562 output2, output2_buffer_size,
2563 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002564 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002565
Gilles Peskine8817f612018-12-18 00:18:46 +01002566 PSA_ASSERT( psa_cipher_finish( &operation2,
2567 output2 + output2_length,
2568 output2_buffer_size - output2_length,
2569 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002570 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002571
Gilles Peskine8817f612018-12-18 00:18:46 +01002572 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002573
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002574 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002575
2576exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002577 mbedtls_free( output1 );
2578 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002579 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002580 mbedtls_psa_crypto_free( );
2581}
2582/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002583
Gilles Peskine20035e32018-02-03 22:44:14 +01002584/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002585void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002586 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002587 data_t *nonce,
2588 data_t *additional_data,
2589 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002590 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002591{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002592 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002593 psa_key_type_t key_type = key_type_arg;
2594 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002595 unsigned char *output_data = NULL;
2596 size_t output_size = 0;
2597 size_t output_length = 0;
2598 unsigned char *output_data2 = NULL;
2599 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002600 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002601 psa_status_t expected_result = expected_result_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002602 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002603
Gilles Peskine4abf7412018-06-18 16:35:34 +02002604 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002605 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002606
Gilles Peskine8817f612018-12-18 00:18:46 +01002607 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002608
Gilles Peskine8817f612018-12-18 00:18:46 +01002609 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2610 &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002611 psa_key_policy_set_usage( &policy,
2612 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2613 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002614 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002615
Gilles Peskine8817f612018-12-18 00:18:46 +01002616 PSA_ASSERT( psa_import_key( handle, key_type,
2617 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002618
Gilles Peskinefe11b722018-12-18 00:24:04 +01002619 TEST_EQUAL( psa_aead_encrypt( handle, alg,
2620 nonce->x, nonce->len,
2621 additional_data->x,
2622 additional_data->len,
2623 input_data->x, input_data->len,
2624 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002625 &output_length ),
2626 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002627
2628 if( PSA_SUCCESS == expected_result )
2629 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002630 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002631
Gilles Peskinefe11b722018-12-18 00:24:04 +01002632 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2633 nonce->x, nonce->len,
2634 additional_data->x,
2635 additional_data->len,
2636 output_data, output_length,
2637 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002638 &output_length2 ),
2639 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002640
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002641 ASSERT_COMPARE( input_data->x, input_data->len,
2642 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002643 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002644
Gilles Peskinea1cac842018-06-11 19:33:02 +02002645exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002646 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002647 mbedtls_free( output_data );
2648 mbedtls_free( output_data2 );
2649 mbedtls_psa_crypto_free( );
2650}
2651/* END_CASE */
2652
2653/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002654void aead_encrypt( int key_type_arg, data_t *key_data,
2655 int alg_arg,
2656 data_t *nonce,
2657 data_t *additional_data,
2658 data_t *input_data,
2659 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002660{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002661 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002662 psa_key_type_t key_type = key_type_arg;
2663 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002664 unsigned char *output_data = NULL;
2665 size_t output_size = 0;
2666 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002667 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002668 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002669
Gilles Peskine4abf7412018-06-18 16:35:34 +02002670 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002671 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002672
Gilles Peskine8817f612018-12-18 00:18:46 +01002673 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002674
Gilles Peskine8817f612018-12-18 00:18:46 +01002675 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2676 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002677 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002678 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002679
Gilles Peskine8817f612018-12-18 00:18:46 +01002680 PSA_ASSERT( psa_import_key( handle, key_type,
2681 key_data->x,
2682 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002683
Gilles Peskine8817f612018-12-18 00:18:46 +01002684 PSA_ASSERT( psa_aead_encrypt( handle, alg,
2685 nonce->x, nonce->len,
2686 additional_data->x, additional_data->len,
2687 input_data->x, input_data->len,
2688 output_data, output_size,
2689 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002690
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002691 ASSERT_COMPARE( expected_result->x, expected_result->len,
2692 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002693
Gilles Peskinea1cac842018-06-11 19:33:02 +02002694exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002695 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002696 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002697 mbedtls_psa_crypto_free( );
2698}
2699/* END_CASE */
2700
2701/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002702void aead_decrypt( int key_type_arg, data_t *key_data,
2703 int alg_arg,
2704 data_t *nonce,
2705 data_t *additional_data,
2706 data_t *input_data,
2707 data_t *expected_data,
2708 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002709{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002710 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002711 psa_key_type_t key_type = key_type_arg;
2712 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002713 unsigned char *output_data = NULL;
2714 size_t output_size = 0;
2715 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002716 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002717 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002718 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002719
Gilles Peskine4abf7412018-06-18 16:35:34 +02002720 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002721 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002722
Gilles Peskine8817f612018-12-18 00:18:46 +01002723 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002724
Gilles Peskine8817f612018-12-18 00:18:46 +01002725 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2726 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002727 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002728 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002729
Gilles Peskine8817f612018-12-18 00:18:46 +01002730 PSA_ASSERT( psa_import_key( handle, key_type,
2731 key_data->x,
2732 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002733
Gilles Peskinefe11b722018-12-18 00:24:04 +01002734 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2735 nonce->x, nonce->len,
2736 additional_data->x,
2737 additional_data->len,
2738 input_data->x, input_data->len,
2739 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002740 &output_length ),
2741 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002742
Gilles Peskine2d277862018-06-18 15:41:12 +02002743 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002744 ASSERT_COMPARE( expected_data->x, expected_data->len,
2745 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002746
Gilles Peskinea1cac842018-06-11 19:33:02 +02002747exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002748 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002749 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002750 mbedtls_psa_crypto_free( );
2751}
2752/* END_CASE */
2753
2754/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002755void signature_size( int type_arg,
2756 int bits,
2757 int alg_arg,
2758 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002759{
2760 psa_key_type_t type = type_arg;
2761 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002762 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002763 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002764exit:
2765 ;
2766}
2767/* END_CASE */
2768
2769/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002770void sign_deterministic( int key_type_arg, data_t *key_data,
2771 int alg_arg, data_t *input_data,
2772 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002773{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002774 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01002775 psa_key_type_t key_type = key_type_arg;
2776 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002777 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002778 unsigned char *signature = NULL;
2779 size_t signature_size;
2780 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002781 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01002782
Gilles Peskine8817f612018-12-18 00:18:46 +01002783 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002784
Gilles Peskine8817f612018-12-18 00:18:46 +01002785 PSA_ASSERT( psa_allocate_key( key_type,
2786 KEY_BITS_FROM_DATA( key_type, key_data ),
2787 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002788 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002789 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002790
Gilles Peskine8817f612018-12-18 00:18:46 +01002791 PSA_ASSERT( psa_import_key( handle, key_type,
2792 key_data->x,
2793 key_data->len ) );
2794 PSA_ASSERT( psa_get_key_information( handle,
2795 NULL,
2796 &key_bits ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002797
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002798 /* Allocate a buffer which has the size advertized by the
2799 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002800 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2801 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002802 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002803 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002804 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002805
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002806 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002807 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2808 input_data->x, input_data->len,
2809 signature, signature_size,
2810 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002811 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002812 ASSERT_COMPARE( output_data->x, output_data->len,
2813 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002814
2815exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002816 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01002817 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002818 mbedtls_psa_crypto_free( );
2819}
2820/* END_CASE */
2821
2822/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002823void sign_fail( int key_type_arg, data_t *key_data,
2824 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002825 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002826{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002827 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01002828 psa_key_type_t key_type = key_type_arg;
2829 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002830 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002831 psa_status_t actual_status;
2832 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002833 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002834 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002835 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01002836
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002837 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002838
Gilles Peskine8817f612018-12-18 00:18:46 +01002839 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002840
Gilles Peskine8817f612018-12-18 00:18:46 +01002841 PSA_ASSERT( psa_allocate_key( key_type,
2842 KEY_BITS_FROM_DATA( key_type, key_data ),
2843 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002844 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002845 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002846
Gilles Peskine8817f612018-12-18 00:18:46 +01002847 PSA_ASSERT( psa_import_key( handle, key_type,
2848 key_data->x,
2849 key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002850
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002851 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002852 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002853 signature, signature_size,
2854 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002855 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002856 /* The value of *signature_length is unspecified on error, but
2857 * whatever it is, it should be less than signature_size, so that
2858 * if the caller tries to read *signature_length bytes without
2859 * checking the error code then they don't overflow a buffer. */
2860 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002861
2862exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002863 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01002864 mbedtls_free( signature );
2865 mbedtls_psa_crypto_free( );
2866}
2867/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002868
2869/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002870void sign_verify( int key_type_arg, data_t *key_data,
2871 int alg_arg, data_t *input_data )
2872{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002873 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02002874 psa_key_type_t key_type = key_type_arg;
2875 psa_algorithm_t alg = alg_arg;
2876 size_t key_bits;
2877 unsigned char *signature = NULL;
2878 size_t signature_size;
2879 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002880 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02002881
Gilles Peskine8817f612018-12-18 00:18:46 +01002882 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002883
Gilles Peskine8817f612018-12-18 00:18:46 +01002884 PSA_ASSERT( psa_allocate_key( key_type,
2885 KEY_BITS_FROM_DATA( key_type, key_data ),
2886 &handle ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002887 psa_key_policy_set_usage( &policy,
2888 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2889 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002890 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002891
Gilles Peskine8817f612018-12-18 00:18:46 +01002892 PSA_ASSERT( psa_import_key( handle, key_type,
2893 key_data->x,
2894 key_data->len ) );
2895 PSA_ASSERT( psa_get_key_information( handle,
2896 NULL,
2897 &key_bits ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002898
2899 /* Allocate a buffer which has the size advertized by the
2900 * library. */
2901 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2902 key_bits, alg );
2903 TEST_ASSERT( signature_size != 0 );
2904 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002905 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002906
2907 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002908 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2909 input_data->x, input_data->len,
2910 signature, signature_size,
2911 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002912 /* Check that the signature length looks sensible. */
2913 TEST_ASSERT( signature_length <= signature_size );
2914 TEST_ASSERT( signature_length > 0 );
2915
2916 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002917 PSA_ASSERT( psa_asymmetric_verify(
2918 handle, alg,
2919 input_data->x, input_data->len,
2920 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002921
2922 if( input_data->len != 0 )
2923 {
2924 /* Flip a bit in the input and verify that the signature is now
2925 * detected as invalid. Flip a bit at the beginning, not at the end,
2926 * because ECDSA may ignore the last few bits of the input. */
2927 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002928 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
2929 input_data->x, input_data->len,
2930 signature, signature_length ),
2931 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02002932 }
2933
2934exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002935 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02002936 mbedtls_free( signature );
2937 mbedtls_psa_crypto_free( );
2938}
2939/* END_CASE */
2940
2941/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002942void asymmetric_verify( int key_type_arg, data_t *key_data,
2943 int alg_arg, data_t *hash_data,
2944 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002945{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002946 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03002947 psa_key_type_t key_type = key_type_arg;
2948 psa_algorithm_t alg = alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002949 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03002950
Gilles Peskine69c12672018-06-28 00:07:19 +02002951 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2952
Gilles Peskine8817f612018-12-18 00:18:46 +01002953 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03002954
Gilles Peskine8817f612018-12-18 00:18:46 +01002955 PSA_ASSERT( psa_allocate_key( key_type,
2956 KEY_BITS_FROM_DATA( key_type, key_data ),
2957 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002958 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002959 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
itayzafrir5c753392018-05-08 11:18:38 +03002960
Gilles Peskine8817f612018-12-18 00:18:46 +01002961 PSA_ASSERT( psa_import_key( handle, key_type,
2962 key_data->x,
2963 key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002964
Gilles Peskine8817f612018-12-18 00:18:46 +01002965 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
2966 hash_data->x, hash_data->len,
2967 signature_data->x,
2968 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002969exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002970 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03002971 mbedtls_psa_crypto_free( );
2972}
2973/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002974
2975/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002976void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
2977 int alg_arg, data_t *hash_data,
2978 data_t *signature_data,
2979 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002980{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002981 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002982 psa_key_type_t key_type = key_type_arg;
2983 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002984 psa_status_t actual_status;
2985 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002986 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002987
Gilles Peskine8817f612018-12-18 00:18:46 +01002988 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002989
Gilles Peskine8817f612018-12-18 00:18:46 +01002990 PSA_ASSERT( psa_allocate_key( key_type,
2991 KEY_BITS_FROM_DATA( key_type, key_data ),
2992 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002993 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002994 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03002995
Gilles Peskine8817f612018-12-18 00:18:46 +01002996 PSA_ASSERT( psa_import_key( handle, key_type,
2997 key_data->x,
2998 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002999
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003000 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003001 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003002 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003003 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003004
Gilles Peskinefe11b722018-12-18 00:24:04 +01003005 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003006
3007exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003008 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003009 mbedtls_psa_crypto_free( );
3010}
3011/* END_CASE */
3012
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003013/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003014void asymmetric_encrypt( int key_type_arg,
3015 data_t *key_data,
3016 int alg_arg,
3017 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003018 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003019 int expected_output_length_arg,
3020 int expected_status_arg )
3021{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003022 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003023 psa_key_type_t key_type = key_type_arg;
3024 psa_algorithm_t alg = alg_arg;
3025 size_t expected_output_length = expected_output_length_arg;
3026 size_t key_bits;
3027 unsigned char *output = NULL;
3028 size_t output_size;
3029 size_t output_length = ~0;
3030 psa_status_t actual_status;
3031 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003032 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003033
Gilles Peskine8817f612018-12-18 00:18:46 +01003034 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003035
Gilles Peskine656896e2018-06-29 19:12:28 +02003036 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01003037 PSA_ASSERT( psa_allocate_key( key_type,
3038 KEY_BITS_FROM_DATA( key_type, key_data ),
3039 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003040 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003041 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
3042 PSA_ASSERT( psa_import_key( handle, key_type,
3043 key_data->x,
3044 key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003045
3046 /* Determine the maximum output length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003047 PSA_ASSERT( psa_get_key_information( handle,
3048 NULL,
3049 &key_bits ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003050 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003051 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003052
3053 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003054 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003055 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003056 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003057 output, output_size,
3058 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003059 TEST_EQUAL( actual_status, expected_status );
3060 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003061
Gilles Peskine68428122018-06-30 18:42:41 +02003062 /* If the label is empty, the test framework puts a non-null pointer
3063 * in label->x. Test that a null pointer works as well. */
3064 if( label->len == 0 )
3065 {
3066 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003067 if( output_size != 0 )
3068 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003069 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003070 input_data->x, input_data->len,
3071 NULL, label->len,
3072 output, output_size,
3073 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003074 TEST_EQUAL( actual_status, expected_status );
3075 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003076 }
3077
Gilles Peskine656896e2018-06-29 19:12:28 +02003078exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003079 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003080 mbedtls_free( output );
3081 mbedtls_psa_crypto_free( );
3082}
3083/* END_CASE */
3084
3085/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003086void asymmetric_encrypt_decrypt( int key_type_arg,
3087 data_t *key_data,
3088 int alg_arg,
3089 data_t *input_data,
3090 data_t *label )
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;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003095 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003096 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003097 size_t output_size;
3098 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003099 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003100 size_t output2_size;
3101 size_t output2_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003102 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003103
Gilles Peskine8817f612018-12-18 00:18:46 +01003104 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003105
Gilles Peskine8817f612018-12-18 00:18:46 +01003106 PSA_ASSERT( psa_allocate_key( key_type,
3107 KEY_BITS_FROM_DATA( key_type, key_data ),
3108 &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003109 psa_key_policy_set_usage( &policy,
3110 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003111 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003112 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003113
Gilles Peskine8817f612018-12-18 00:18:46 +01003114 PSA_ASSERT( psa_import_key( handle, key_type,
3115 key_data->x,
3116 key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003117
3118 /* Determine the maximum ciphertext length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003119 PSA_ASSERT( psa_get_key_information( handle,
3120 NULL,
3121 &key_bits ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003122 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003123 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003124 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003125 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003126
Gilles Peskineeebd7382018-06-08 18:11:54 +02003127 /* We test encryption by checking that encrypt-then-decrypt gives back
3128 * the original plaintext because of the non-optional random
3129 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003130 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3131 input_data->x, input_data->len,
3132 label->x, label->len,
3133 output, output_size,
3134 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003135 /* We don't know what ciphertext length to expect, but check that
3136 * it looks sensible. */
3137 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003138
Gilles Peskine8817f612018-12-18 00:18:46 +01003139 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3140 output, output_length,
3141 label->x, label->len,
3142 output2, output2_size,
3143 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003144 ASSERT_COMPARE( input_data->x, input_data->len,
3145 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003146
3147exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003148 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003149 mbedtls_free( output );
3150 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003151 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003152}
3153/* END_CASE */
3154
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003155/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003156void asymmetric_decrypt( int key_type_arg,
3157 data_t *key_data,
3158 int alg_arg,
3159 data_t *input_data,
3160 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003161 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003162{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003163 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003164 psa_key_type_t key_type = key_type_arg;
3165 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003166 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003167 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003168 size_t output_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003169 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003170
Gilles Peskine4abf7412018-06-18 16:35:34 +02003171 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003172 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003173
Gilles Peskine8817f612018-12-18 00:18:46 +01003174 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003175
Gilles Peskine8817f612018-12-18 00:18:46 +01003176 PSA_ASSERT( psa_allocate_key( key_type,
3177 KEY_BITS_FROM_DATA( key_type, key_data ),
3178 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003179 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003180 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003181
Gilles Peskine8817f612018-12-18 00:18:46 +01003182 PSA_ASSERT( psa_import_key( handle, key_type,
3183 key_data->x,
3184 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003185
Gilles Peskine8817f612018-12-18 00:18:46 +01003186 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3187 input_data->x, input_data->len,
3188 label->x, label->len,
3189 output,
3190 output_size,
3191 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003192 ASSERT_COMPARE( expected_data->x, expected_data->len,
3193 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003194
Gilles Peskine68428122018-06-30 18:42:41 +02003195 /* If the label is empty, the test framework puts a non-null pointer
3196 * in label->x. Test that a null pointer works as well. */
3197 if( label->len == 0 )
3198 {
3199 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003200 if( output_size != 0 )
3201 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003202 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3203 input_data->x, input_data->len,
3204 NULL, label->len,
3205 output,
3206 output_size,
3207 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003208 ASSERT_COMPARE( expected_data->x, expected_data->len,
3209 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003210 }
3211
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003212exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003213 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003214 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003215 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003216}
3217/* END_CASE */
3218
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003219/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003220void asymmetric_decrypt_fail( int key_type_arg,
3221 data_t *key_data,
3222 int alg_arg,
3223 data_t *input_data,
3224 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003225 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003226{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003227 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003228 psa_key_type_t key_type = key_type_arg;
3229 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003230 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003231 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003232 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003233 psa_status_t actual_status;
3234 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003235 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003236
Gilles Peskine4abf7412018-06-18 16:35:34 +02003237 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003238 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003239
Gilles Peskine8817f612018-12-18 00:18:46 +01003240 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003241
Gilles Peskine8817f612018-12-18 00:18:46 +01003242 PSA_ASSERT( psa_allocate_key( key_type,
3243 KEY_BITS_FROM_DATA( key_type, key_data ),
3244 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003245 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003246 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003247
Gilles Peskine8817f612018-12-18 00:18:46 +01003248 PSA_ASSERT( psa_import_key( handle, key_type,
3249 key_data->x,
3250 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003251
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003252 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003253 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003254 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003255 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003256 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003257 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003258 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003259
Gilles Peskine68428122018-06-30 18:42:41 +02003260 /* If the label is empty, the test framework puts a non-null pointer
3261 * in label->x. Test that a null pointer works as well. */
3262 if( label->len == 0 )
3263 {
3264 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003265 if( output_size != 0 )
3266 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003267 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003268 input_data->x, input_data->len,
3269 NULL, label->len,
3270 output, output_size,
3271 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003272 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003273 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003274 }
3275
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003276exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003277 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02003278 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003279 mbedtls_psa_crypto_free( );
3280}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003281/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003282
3283/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003284void derive_setup( int key_type_arg,
3285 data_t *key_data,
3286 int alg_arg,
3287 data_t *salt,
3288 data_t *label,
3289 int requested_capacity_arg,
3290 int expected_status_arg )
3291{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003292 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003293 size_t key_type = key_type_arg;
3294 psa_algorithm_t alg = alg_arg;
3295 size_t requested_capacity = requested_capacity_arg;
3296 psa_status_t expected_status = expected_status_arg;
3297 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003298 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003299
Gilles Peskine8817f612018-12-18 00:18:46 +01003300 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003301
Gilles Peskine8817f612018-12-18 00:18:46 +01003302 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
3303 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003304 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003305 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003306
Gilles Peskine8817f612018-12-18 00:18:46 +01003307 PSA_ASSERT( psa_import_key( handle, key_type,
3308 key_data->x,
3309 key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003310
Gilles Peskinefe11b722018-12-18 00:24:04 +01003311 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3312 salt->x, salt->len,
3313 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003314 requested_capacity ),
3315 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003316
3317exit:
3318 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003319 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003320 mbedtls_psa_crypto_free( );
3321}
3322/* END_CASE */
3323
3324/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003325void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003326{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003327 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003328 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003329 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003330 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003331 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003332 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003333 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3334 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3335 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Jaeden Amero70261c52019-01-04 11:47:20 +00003336 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003337
Gilles Peskine8817f612018-12-18 00:18:46 +01003338 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003339
Gilles Peskine8817f612018-12-18 00:18:46 +01003340 PSA_ASSERT( psa_allocate_key( key_type,
3341 PSA_BYTES_TO_BITS( sizeof( key_data ) ),
3342 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003343 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003344 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003345
Gilles Peskine8817f612018-12-18 00:18:46 +01003346 PSA_ASSERT( psa_import_key( handle, key_type,
3347 key_data,
3348 sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003349
3350 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01003351 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3352 NULL, 0,
3353 NULL, 0,
3354 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003355
3356 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003357 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3358 NULL, 0,
3359 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003360 capacity ),
3361 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003362
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003363 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003364
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003365 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
3366 PSA_ERROR_INSUFFICIENT_CAPACITY );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003367
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003368exit:
3369 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003370 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003371 mbedtls_psa_crypto_free( );
3372}
3373/* END_CASE */
3374
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003375/* BEGIN_CASE */
3376void test_derive_invalid_generator_tests( )
3377{
3378 uint8_t output_buffer[16];
3379 size_t buffer_size = 16;
3380 size_t capacity = 0;
3381 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3382
Nir Sonnenschein50789302018-10-31 12:16:38 +02003383 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003384 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003385
3386 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003387 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003388
Gilles Peskine8817f612018-12-18 00:18:46 +01003389 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003390
Nir Sonnenschein50789302018-10-31 12:16:38 +02003391 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003392 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003393
Nir Sonnenschein50789302018-10-31 12:16:38 +02003394 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003395 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003396
3397exit:
3398 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003399}
3400/* END_CASE */
3401
3402/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003403void derive_output( int alg_arg,
3404 data_t *key_data,
3405 data_t *salt,
3406 data_t *label,
3407 int requested_capacity_arg,
3408 data_t *expected_output1,
3409 data_t *expected_output2 )
3410{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003411 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003412 psa_algorithm_t alg = alg_arg;
3413 size_t requested_capacity = requested_capacity_arg;
3414 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3415 uint8_t *expected_outputs[2] =
3416 {expected_output1->x, expected_output2->x};
3417 size_t output_sizes[2] =
3418 {expected_output1->len, expected_output2->len};
3419 size_t output_buffer_size = 0;
3420 uint8_t *output_buffer = NULL;
3421 size_t expected_capacity;
3422 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003423 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003424 psa_status_t status;
3425 unsigned i;
3426
3427 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3428 {
3429 if( output_sizes[i] > output_buffer_size )
3430 output_buffer_size = output_sizes[i];
3431 if( output_sizes[i] == 0 )
3432 expected_outputs[i] = NULL;
3433 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003434 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003435 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003436
Gilles Peskine8817f612018-12-18 00:18:46 +01003437 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3438 PSA_BYTES_TO_BITS( key_data->len ),
3439 &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +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 Peskine96ee5c72018-07-12 17:24:54 +02003442
Gilles Peskine8817f612018-12-18 00:18:46 +01003443 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3444 key_data->x,
3445 key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003446
3447 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003448 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3449 salt->x, salt->len,
3450 label->x, label->len,
3451 requested_capacity ) );
3452 PSA_ASSERT( psa_get_generator_capacity( &generator,
3453 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003454 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003455 expected_capacity = requested_capacity;
3456
3457 /* Expansion phase. */
3458 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3459 {
3460 /* Read some bytes. */
3461 status = psa_generator_read( &generator,
3462 output_buffer, output_sizes[i] );
3463 if( expected_capacity == 0 && output_sizes[i] == 0 )
3464 {
3465 /* Reading 0 bytes when 0 bytes are available can go either way. */
3466 TEST_ASSERT( status == PSA_SUCCESS ||
3467 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3468 continue;
3469 }
3470 else if( expected_capacity == 0 ||
3471 output_sizes[i] > expected_capacity )
3472 {
3473 /* Capacity exceeded. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003474 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003475 expected_capacity = 0;
3476 continue;
3477 }
3478 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003479 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003480 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003481 ASSERT_COMPARE( output_buffer, output_sizes[i],
3482 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003483 /* Check the generator status. */
3484 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01003485 PSA_ASSERT( psa_get_generator_capacity( &generator,
3486 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003487 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003488 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003489 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003490
3491exit:
3492 mbedtls_free( output_buffer );
3493 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003494 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003495 mbedtls_psa_crypto_free( );
3496}
3497/* END_CASE */
3498
3499/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003500void derive_full( int alg_arg,
3501 data_t *key_data,
3502 data_t *salt,
3503 data_t *label,
3504 int requested_capacity_arg )
3505{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003506 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02003507 psa_algorithm_t alg = alg_arg;
3508 size_t requested_capacity = requested_capacity_arg;
3509 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3510 unsigned char output_buffer[16];
3511 size_t expected_capacity = requested_capacity;
3512 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003513 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02003514
Gilles Peskine8817f612018-12-18 00:18:46 +01003515 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003516
Gilles Peskine8817f612018-12-18 00:18:46 +01003517 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3518 PSA_BYTES_TO_BITS( key_data->len ),
3519 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003520 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003521 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003522
Gilles Peskine8817f612018-12-18 00:18:46 +01003523 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3524 key_data->x,
3525 key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003526
3527 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003528 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3529 salt->x, salt->len,
3530 label->x, label->len,
3531 requested_capacity ) );
3532 PSA_ASSERT( psa_get_generator_capacity( &generator,
3533 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003534 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003535
3536 /* Expansion phase. */
3537 while( current_capacity > 0 )
3538 {
3539 size_t read_size = sizeof( output_buffer );
3540 if( read_size > current_capacity )
3541 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01003542 PSA_ASSERT( psa_generator_read( &generator,
3543 output_buffer,
3544 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003545 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01003546 PSA_ASSERT( psa_get_generator_capacity( &generator,
3547 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003548 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003549 }
3550
3551 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003552 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
3553 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskined54931c2018-07-17 21:06:59 +02003554
Gilles Peskine8817f612018-12-18 00:18:46 +01003555 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003556
3557exit:
3558 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003559 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02003560 mbedtls_psa_crypto_free( );
3561}
3562/* END_CASE */
3563
3564/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003565void derive_key_exercise( int alg_arg,
3566 data_t *key_data,
3567 data_t *salt,
3568 data_t *label,
3569 int derived_type_arg,
3570 int derived_bits_arg,
3571 int derived_usage_arg,
3572 int derived_alg_arg )
3573{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003574 psa_key_handle_t base_handle = 0;
3575 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003576 psa_algorithm_t alg = alg_arg;
3577 psa_key_type_t derived_type = derived_type_arg;
3578 size_t derived_bits = derived_bits_arg;
3579 psa_key_usage_t derived_usage = derived_usage_arg;
3580 psa_algorithm_t derived_alg = derived_alg_arg;
3581 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3582 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003583 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003584 psa_key_type_t got_type;
3585 size_t got_bits;
3586
Gilles Peskine8817f612018-12-18 00:18:46 +01003587 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003588
Gilles Peskine8817f612018-12-18 00:18:46 +01003589 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3590 PSA_BYTES_TO_BITS( key_data->len ),
3591 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003592 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003593 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3594 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3595 key_data->x,
3596 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003597
3598 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003599 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3600 salt->x, salt->len,
3601 label->x, label->len,
3602 capacity ) );
3603 PSA_ASSERT( psa_allocate_key( derived_type, derived_bits,
3604 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003605 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003606 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3607 PSA_ASSERT( psa_generator_import_key( derived_handle,
3608 derived_type,
3609 derived_bits,
3610 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003611
3612 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01003613 PSA_ASSERT( psa_get_key_information( derived_handle,
3614 &got_type,
3615 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003616 TEST_EQUAL( got_type, derived_type );
3617 TEST_EQUAL( got_bits, derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003618
3619 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003620 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02003621 goto exit;
3622
3623exit:
3624 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003625 psa_destroy_key( base_handle );
3626 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003627 mbedtls_psa_crypto_free( );
3628}
3629/* END_CASE */
3630
3631/* BEGIN_CASE */
3632void derive_key_export( int alg_arg,
3633 data_t *key_data,
3634 data_t *salt,
3635 data_t *label,
3636 int bytes1_arg,
3637 int bytes2_arg )
3638{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003639 psa_key_handle_t base_handle = 0;
3640 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003641 psa_algorithm_t alg = alg_arg;
3642 size_t bytes1 = bytes1_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003643 size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003644 size_t bytes2 = bytes2_arg;
3645 size_t capacity = bytes1 + bytes2;
3646 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003647 uint8_t *output_buffer = NULL;
3648 uint8_t *export_buffer = NULL;
Jaeden Amero70261c52019-01-04 11:47:20 +00003649 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003650 size_t length;
3651
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003652 ASSERT_ALLOC( output_buffer, capacity );
3653 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01003654 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003655
Gilles Peskine8817f612018-12-18 00:18:46 +01003656 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3657 PSA_BYTES_TO_BITS( key_data->len ),
3658 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003659 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003660 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3661 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3662 key_data->x,
3663 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003664
3665 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003666 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3667 salt->x, salt->len,
3668 label->x, label->len,
3669 capacity ) );
3670 PSA_ASSERT( psa_generator_read( &generator,
3671 output_buffer,
3672 capacity ) );
3673 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003674
3675 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003676 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3677 salt->x, salt->len,
3678 label->x, label->len,
3679 capacity ) );
3680 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, derived_bits,
3681 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003682 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003683 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3684 PSA_ASSERT( psa_generator_import_key( derived_handle,
3685 PSA_KEY_TYPE_RAW_DATA,
3686 derived_bits,
3687 &generator ) );
3688 PSA_ASSERT( psa_export_key( derived_handle,
3689 export_buffer, bytes1,
3690 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003691 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003692 PSA_ASSERT( psa_destroy_key( derived_handle ) );
3693 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA,
3694 PSA_BYTES_TO_BITS( bytes2 ),
3695 &derived_handle ) );
3696 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3697 PSA_ASSERT( psa_generator_import_key( derived_handle,
3698 PSA_KEY_TYPE_RAW_DATA,
3699 PSA_BYTES_TO_BITS( bytes2 ),
3700 &generator ) );
3701 PSA_ASSERT( psa_export_key( derived_handle,
3702 export_buffer + bytes1, bytes2,
3703 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003704 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003705
3706 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003707 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
3708 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003709
3710exit:
3711 mbedtls_free( output_buffer );
3712 mbedtls_free( export_buffer );
3713 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003714 psa_destroy_key( base_handle );
3715 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003716 mbedtls_psa_crypto_free( );
3717}
3718/* END_CASE */
3719
3720/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02003721void key_agreement_setup( int alg_arg,
3722 int our_key_type_arg, data_t *our_key_data,
3723 data_t *peer_key_data,
3724 int expected_status_arg )
3725{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003726 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003727 psa_algorithm_t alg = alg_arg;
3728 psa_key_type_t our_key_type = our_key_type_arg;
3729 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003730 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003731
Gilles Peskine8817f612018-12-18 00:18:46 +01003732 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003733
Gilles Peskine8817f612018-12-18 00:18:46 +01003734 PSA_ASSERT( psa_allocate_key( our_key_type,
3735 KEY_BITS_FROM_DATA( our_key_type,
3736 our_key_data ),
3737 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003738 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003739 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3740 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3741 our_key_data->x,
3742 our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003743
Gilles Peskinefe11b722018-12-18 00:24:04 +01003744 TEST_EQUAL( psa_key_agreement( &generator,
3745 our_key,
3746 peer_key_data->x, peer_key_data->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003747 alg ),
3748 expected_status_arg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003749
3750exit:
3751 psa_generator_abort( &generator );
3752 psa_destroy_key( our_key );
3753 mbedtls_psa_crypto_free( );
3754}
3755/* END_CASE */
3756
3757/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02003758void key_agreement_capacity( int alg_arg,
3759 int our_key_type_arg, data_t *our_key_data,
3760 data_t *peer_key_data,
3761 int expected_capacity_arg )
3762{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003763 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003764 psa_algorithm_t alg = alg_arg;
3765 psa_key_type_t our_key_type = our_key_type_arg;
3766 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003767 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02003768 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02003769 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02003770
Gilles Peskine8817f612018-12-18 00:18:46 +01003771 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003772
Gilles Peskine8817f612018-12-18 00:18:46 +01003773 PSA_ASSERT( psa_allocate_key( our_key_type,
3774 KEY_BITS_FROM_DATA( our_key_type,
3775 our_key_data ),
3776 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003777 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003778 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3779 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3780 our_key_data->x,
3781 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003782
Gilles Peskine8817f612018-12-18 00:18:46 +01003783 PSA_ASSERT( psa_key_agreement( &generator,
3784 our_key,
3785 peer_key_data->x, peer_key_data->len,
3786 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003787
Gilles Peskinebf491972018-10-25 22:36:12 +02003788 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003789 PSA_ASSERT( psa_get_generator_capacity(
3790 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003791 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02003792
Gilles Peskinebf491972018-10-25 22:36:12 +02003793 /* Test the actual capacity by reading the output. */
3794 while( actual_capacity > sizeof( output ) )
3795 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003796 PSA_ASSERT( psa_generator_read( &generator,
3797 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02003798 actual_capacity -= sizeof( output );
3799 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003800 PSA_ASSERT( psa_generator_read( &generator,
3801 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003802 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
3803 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskinebf491972018-10-25 22:36:12 +02003804
Gilles Peskine59685592018-09-18 12:11:34 +02003805exit:
3806 psa_generator_abort( &generator );
3807 psa_destroy_key( our_key );
3808 mbedtls_psa_crypto_free( );
3809}
3810/* END_CASE */
3811
3812/* BEGIN_CASE */
3813void key_agreement_output( int alg_arg,
3814 int our_key_type_arg, data_t *our_key_data,
3815 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003816 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02003817{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003818 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003819 psa_algorithm_t alg = alg_arg;
3820 psa_key_type_t our_key_type = our_key_type_arg;
3821 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003822 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003823 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02003824
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003825 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
3826 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003827
Gilles Peskine8817f612018-12-18 00:18:46 +01003828 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003829
Gilles Peskine8817f612018-12-18 00:18:46 +01003830 PSA_ASSERT( psa_allocate_key( our_key_type,
3831 KEY_BITS_FROM_DATA( our_key_type,
3832 our_key_data ),
3833 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003834 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003835 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3836 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3837 our_key_data->x,
3838 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003839
Gilles Peskine8817f612018-12-18 00:18:46 +01003840 PSA_ASSERT( psa_key_agreement( &generator,
3841 our_key,
3842 peer_key_data->x, peer_key_data->len,
3843 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003844
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003845 PSA_ASSERT( psa_generator_read( &generator,
3846 actual_output,
3847 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003848 ASSERT_COMPARE( actual_output, expected_output1->len,
3849 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003850 if( expected_output2->len != 0 )
3851 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003852 PSA_ASSERT( psa_generator_read( &generator,
3853 actual_output,
3854 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003855 ASSERT_COMPARE( actual_output, expected_output2->len,
3856 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003857 }
Gilles Peskine59685592018-09-18 12:11:34 +02003858
3859exit:
3860 psa_generator_abort( &generator );
3861 psa_destroy_key( our_key );
3862 mbedtls_psa_crypto_free( );
3863 mbedtls_free( actual_output );
3864}
3865/* END_CASE */
3866
3867/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003868void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003869{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003870 size_t bytes = bytes_arg;
3871 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003872 unsigned char *output = NULL;
3873 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003874 size_t i;
3875 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003876
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003877 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3878 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003879 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003880
Gilles Peskine8817f612018-12-18 00:18:46 +01003881 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003882
Gilles Peskinea50d7392018-06-21 10:22:13 +02003883 /* Run several times, to ensure that every output byte will be
3884 * nonzero at least once with overwhelming probability
3885 * (2^(-8*number_of_runs)). */
3886 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003887 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003888 if( bytes != 0 )
3889 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01003890 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003891
3892 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003893 ASSERT_COMPARE( output + bytes, sizeof( trail ),
3894 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003895
3896 for( i = 0; i < bytes; i++ )
3897 {
3898 if( output[i] != 0 )
3899 ++changed[i];
3900 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003901 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003902
3903 /* Check that every byte was changed to nonzero at least once. This
3904 * validates that psa_generate_random is overwriting every byte of
3905 * the output buffer. */
3906 for( i = 0; i < bytes; i++ )
3907 {
3908 TEST_ASSERT( changed[i] != 0 );
3909 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003910
3911exit:
3912 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003913 mbedtls_free( output );
3914 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003915}
3916/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003917
3918/* BEGIN_CASE */
3919void generate_key( int type_arg,
3920 int bits_arg,
3921 int usage_arg,
3922 int alg_arg,
3923 int expected_status_arg )
3924{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003925 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003926 psa_key_type_t type = type_arg;
3927 psa_key_usage_t usage = usage_arg;
3928 size_t bits = bits_arg;
3929 psa_algorithm_t alg = alg_arg;
3930 psa_status_t expected_status = expected_status_arg;
3931 psa_key_type_t got_type;
3932 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003933 psa_status_t expected_info_status =
3934 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003935 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003936
Gilles Peskine8817f612018-12-18 00:18:46 +01003937 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003938
Gilles Peskine8817f612018-12-18 00:18:46 +01003939 PSA_ASSERT( psa_allocate_key( type, bits, &handle ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003940 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003941 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003942
3943 /* Generate a key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003944 TEST_EQUAL( psa_generate_key( handle, type, bits, NULL, 0 ),
3945 expected_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003946
3947 /* Test the key information */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003948 TEST_EQUAL( psa_get_key_information( handle, &got_type, &got_bits ),
3949 expected_info_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003950 if( expected_info_status != PSA_SUCCESS )
3951 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003952 TEST_EQUAL( got_type, type );
3953 TEST_EQUAL( got_bits, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003954
Gilles Peskine818ca122018-06-20 18:16:48 +02003955 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003956 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02003957 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003958
3959exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003960 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003961 mbedtls_psa_crypto_free( );
3962}
3963/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03003964
Darryl Greend49a4992018-06-18 17:27:26 +01003965/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
3966void persistent_key_load_key_from_storage( data_t *data, int type_arg,
3967 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00003968 int alg_arg, int generation_method,
3969 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01003970{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003971 psa_key_handle_t handle = 0;
3972 psa_key_handle_t base_key;
Darryl Greend49a4992018-06-18 17:27:26 +01003973 psa_key_type_t type = (psa_key_type_t) type_arg;
3974 psa_key_type_t type_get;
3975 size_t bits_get;
Jaeden Amero70261c52019-01-04 11:47:20 +00003976 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
3977 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01003978 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
3979 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003980 psa_key_policy_t base_policy_set = PSA_KEY_POLICY_INIT;
Darryl Green0c6575a2018-11-07 16:05:30 +00003981 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
3982 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01003983 unsigned char *first_export = NULL;
3984 unsigned char *second_export = NULL;
3985 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
3986 size_t first_exported_length;
3987 size_t second_exported_length;
3988
3989 ASSERT_ALLOC( first_export, export_size );
3990 ASSERT_ALLOC( second_export, export_size );
3991
Gilles Peskine8817f612018-12-18 00:18:46 +01003992 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01003993
Gilles Peskine8817f612018-12-18 00:18:46 +01003994 PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
3995 type, bits,
3996 &handle ) );
Darryl Greend49a4992018-06-18 17:27:26 +01003997 psa_key_policy_set_usage( &policy_set, policy_usage,
3998 policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003999 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004000
Darryl Green0c6575a2018-11-07 16:05:30 +00004001 switch( generation_method )
4002 {
4003 case IMPORT_KEY:
4004 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004005 PSA_ASSERT( psa_import_key( handle, type,
4006 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004007 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004008
Darryl Green0c6575a2018-11-07 16:05:30 +00004009 case GENERATE_KEY:
4010 /* Generate a key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004011 PSA_ASSERT( psa_generate_key( handle, type, bits,
4012 NULL, 0 ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004013 break;
4014
4015 case DERIVE_KEY:
4016 /* Create base key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004017 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
4018 PSA_BYTES_TO_BITS( data->len ),
4019 &base_key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004020 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
4021 base_policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004022 PSA_ASSERT( psa_set_key_policy(
4023 base_key, &base_policy_set ) );
4024 PSA_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
4025 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004026 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004027 PSA_ASSERT( psa_key_derivation( &generator, base_key,
4028 base_policy_alg,
4029 NULL, 0, NULL, 0,
4030 export_size ) );
4031 PSA_ASSERT( psa_generator_import_key(
4032 handle, PSA_KEY_TYPE_RAW_DATA,
4033 bits, &generator ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004034 break;
4035 }
Darryl Greend49a4992018-06-18 17:27:26 +01004036
4037 /* Export the key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004038 TEST_EQUAL( psa_export_key( handle,
4039 first_export, export_size,
4040 &first_exported_length ),
4041 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004042
4043 /* Shutdown and restart */
4044 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004045 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004046
Darryl Greend49a4992018-06-18 17:27:26 +01004047 /* Check key slot still contains key data */
Gilles Peskine8817f612018-12-18 00:18:46 +01004048 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4049 &handle ) );
4050 PSA_ASSERT( psa_get_key_information(
4051 handle, &type_get, &bits_get ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004052 TEST_EQUAL( type_get, type );
4053 TEST_EQUAL( bits_get, (size_t) bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004054
Gilles Peskine8817f612018-12-18 00:18:46 +01004055 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004056 TEST_EQUAL( psa_key_policy_get_usage( &policy_get ), policy_usage );
4057 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_get ), policy_alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004058
4059 /* Export the key again */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004060 TEST_EQUAL( psa_export_key( handle,
4061 second_export, export_size,
4062 &second_exported_length ),
4063 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004064
Darryl Green0c6575a2018-11-07 16:05:30 +00004065 if( export_status == PSA_SUCCESS )
4066 {
4067 ASSERT_COMPARE( first_export, first_exported_length,
4068 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01004069
Darryl Green0c6575a2018-11-07 16:05:30 +00004070 switch( generation_method )
4071 {
4072 case IMPORT_KEY:
4073 ASSERT_COMPARE( data->x, data->len,
4074 first_export, first_exported_length );
4075 break;
4076 default:
4077 break;
4078 }
4079 }
4080
4081 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004082 if( ! exercise_key( handle, policy_usage, policy_alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004083 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004084
4085exit:
4086 mbedtls_free( first_export );
4087 mbedtls_free( second_export );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004088 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004089 mbedtls_psa_crypto_free();
4090}
4091/* END_CASE */