blob: 24ffe805c0f6c74f170bae31dfe9c91ccf5e7d42 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
4#if defined(MBEDTLS_PSA_CRYPTO_SPM)
5#include "spm/psa_defs.h"
6#endif
7
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02009#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +020010#include "mbedtls/oid.h"
11
Gilles Peskinee59236f2018-01-27 23:32:46 +010012#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030013
Jaeden Amerof24c7f82018-06-27 17:20:43 +010014/** An invalid export length that will never be set by psa_export_key(). */
15static const size_t INVALID_EXPORT_LENGTH = ~0U;
16
Gilles Peskinea7aa4422018-08-14 15:17:54 +020017/** Test if a buffer contains a constant byte value.
18 *
19 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020020 *
21 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020022 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020023 * \param size Size of the buffer in bytes.
24 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020025 * \return 1 if the buffer is all-bits-zero.
26 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020027 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020028static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020029{
30 size_t i;
31 for( i = 0; i < size; i++ )
32 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020033 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020034 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020035 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020036 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037}
Gilles Peskine818ca122018-06-20 18:16:48 +020038
Gilles Peskine0b352bc2018-06-28 00:16:11 +020039/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
40static int asn1_write_10x( unsigned char **p,
41 unsigned char *start,
42 size_t bits,
43 unsigned char x )
44{
45 int ret;
46 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020047 if( bits == 0 )
48 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
49 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020050 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030051 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020052 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
53 *p -= len;
54 ( *p )[len-1] = x;
55 if( bits % 8 == 0 )
56 ( *p )[1] |= 1;
57 else
58 ( *p )[0] |= 1 << ( bits % 8 );
59 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
60 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
61 MBEDTLS_ASN1_INTEGER ) );
62 return( len );
63}
64
65static int construct_fake_rsa_key( unsigned char *buffer,
66 size_t buffer_size,
67 unsigned char **p,
68 size_t bits,
69 int keypair )
70{
71 size_t half_bits = ( bits + 1 ) / 2;
72 int ret;
73 int len = 0;
74 /* Construct something that looks like a DER encoding of
75 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
76 * RSAPrivateKey ::= SEQUENCE {
77 * version Version,
78 * modulus INTEGER, -- n
79 * publicExponent INTEGER, -- e
80 * privateExponent INTEGER, -- d
81 * prime1 INTEGER, -- p
82 * prime2 INTEGER, -- q
83 * exponent1 INTEGER, -- d mod (p-1)
84 * exponent2 INTEGER, -- d mod (q-1)
85 * coefficient INTEGER, -- (inverse of q) mod p
86 * otherPrimeInfos OtherPrimeInfos OPTIONAL
87 * }
88 * Or, for a public key, the same structure with only
89 * version, modulus and publicExponent.
90 */
91 *p = buffer + buffer_size;
92 if( keypair )
93 {
94 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
95 asn1_write_10x( p, buffer, half_bits, 1 ) );
96 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
97 asn1_write_10x( p, buffer, half_bits, 1 ) );
98 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
99 asn1_write_10x( p, buffer, half_bits, 1 ) );
100 MBEDTLS_ASN1_CHK_ADD( len, /* q */
101 asn1_write_10x( p, buffer, half_bits, 1 ) );
102 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
103 asn1_write_10x( p, buffer, half_bits, 3 ) );
104 MBEDTLS_ASN1_CHK_ADD( len, /* d */
105 asn1_write_10x( p, buffer, bits, 1 ) );
106 }
107 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
108 asn1_write_10x( p, buffer, 17, 1 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* n */
110 asn1_write_10x( p, buffer, bits, 1 ) );
111 if( keypair )
112 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
113 mbedtls_asn1_write_int( p, buffer, 0 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
115 {
116 const unsigned char tag =
117 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
118 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
119 }
120 return( len );
121}
122
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100123static int exercise_mac_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200124 psa_key_usage_t usage,
125 psa_algorithm_t alg )
126{
Jaeden Amero769ce272019-01-04 11:48:03 +0000127 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200128 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200129 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200130 size_t mac_length = sizeof( mac );
131
132 if( usage & PSA_KEY_USAGE_SIGN )
133 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100134 PSA_ASSERT( psa_mac_sign_setup( &operation,
135 handle, alg ) );
136 PSA_ASSERT( psa_mac_update( &operation,
137 input, sizeof( input ) ) );
138 PSA_ASSERT( psa_mac_sign_finish( &operation,
139 mac, sizeof( mac ),
140 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200141 }
142
143 if( usage & PSA_KEY_USAGE_VERIFY )
144 {
145 psa_status_t verify_status =
146 ( usage & PSA_KEY_USAGE_SIGN ?
147 PSA_SUCCESS :
148 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +0100149 PSA_ASSERT( psa_mac_verify_setup( &operation,
150 handle, alg ) );
151 PSA_ASSERT( psa_mac_update( &operation,
152 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100153 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
154 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200155 }
156
157 return( 1 );
158
159exit:
160 psa_mac_abort( &operation );
161 return( 0 );
162}
163
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100164static int exercise_cipher_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200165 psa_key_usage_t usage,
166 psa_algorithm_t alg )
167{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000168 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200169 unsigned char iv[16] = {0};
170 size_t iv_length = sizeof( iv );
171 const unsigned char plaintext[16] = "Hello, world...";
172 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
173 size_t ciphertext_length = sizeof( ciphertext );
174 unsigned char decrypted[sizeof( ciphertext )];
175 size_t part_length;
176
177 if( usage & PSA_KEY_USAGE_ENCRYPT )
178 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100179 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
180 handle, alg ) );
181 PSA_ASSERT( psa_cipher_generate_iv( &operation,
182 iv, sizeof( iv ),
183 &iv_length ) );
184 PSA_ASSERT( psa_cipher_update( &operation,
185 plaintext, sizeof( plaintext ),
186 ciphertext, sizeof( ciphertext ),
187 &ciphertext_length ) );
188 PSA_ASSERT( psa_cipher_finish( &operation,
189 ciphertext + ciphertext_length,
190 sizeof( ciphertext ) - ciphertext_length,
191 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200192 ciphertext_length += part_length;
193 }
194
195 if( usage & PSA_KEY_USAGE_DECRYPT )
196 {
197 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700198 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200199 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
200 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200201 size_t bits;
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100202 TEST_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200203 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
204 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100205 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
206 handle, alg ) );
207 PSA_ASSERT( psa_cipher_set_iv( &operation,
208 iv, iv_length ) );
209 PSA_ASSERT( psa_cipher_update( &operation,
210 ciphertext, ciphertext_length,
211 decrypted, sizeof( decrypted ),
212 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200213 status = psa_cipher_finish( &operation,
214 decrypted + part_length,
215 sizeof( decrypted ) - part_length,
216 &part_length );
217 /* For a stream cipher, all inputs are valid. For a block cipher,
218 * if the input is some aribtrary data rather than an actual
219 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700220 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700221 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100222 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200223 else
224 TEST_ASSERT( status == PSA_SUCCESS ||
225 status == PSA_ERROR_INVALID_PADDING );
226 }
227
228 return( 1 );
229
230exit:
231 psa_cipher_abort( &operation );
232 return( 0 );
233}
234
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100235static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200236 psa_key_usage_t usage,
237 psa_algorithm_t alg )
238{
239 unsigned char nonce[16] = {0};
240 size_t nonce_length = sizeof( nonce );
241 unsigned char plaintext[16] = "Hello, world...";
242 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
243 size_t ciphertext_length = sizeof( ciphertext );
244 size_t plaintext_length = sizeof( ciphertext );
245
246 if( usage & PSA_KEY_USAGE_ENCRYPT )
247 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100248 PSA_ASSERT( psa_aead_encrypt( handle, alg,
249 nonce, nonce_length,
250 NULL, 0,
251 plaintext, sizeof( plaintext ),
252 ciphertext, sizeof( ciphertext ),
253 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200254 }
255
256 if( usage & PSA_KEY_USAGE_DECRYPT )
257 {
258 psa_status_t verify_status =
259 ( usage & PSA_KEY_USAGE_ENCRYPT ?
260 PSA_SUCCESS :
261 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100262 TEST_EQUAL( psa_aead_decrypt( handle, alg,
263 nonce, nonce_length,
264 NULL, 0,
265 ciphertext, ciphertext_length,
266 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100267 &plaintext_length ),
268 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200269 }
270
271 return( 1 );
272
273exit:
274 return( 0 );
275}
276
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100277static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200278 psa_key_usage_t usage,
279 psa_algorithm_t alg )
280{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200281 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
282 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200283 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200284 size_t signature_length = sizeof( signature );
285
286 if( usage & PSA_KEY_USAGE_SIGN )
287 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200288 /* Some algorithms require the payload to have the size of
289 * the hash encoded in the algorithm. Use this input size
290 * even for algorithms that allow other input sizes. */
291 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
292 if( hash_alg != 0 )
293 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100294 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
295 payload, payload_length,
296 signature, sizeof( signature ),
297 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200298 }
299
300 if( usage & PSA_KEY_USAGE_VERIFY )
301 {
302 psa_status_t verify_status =
303 ( usage & PSA_KEY_USAGE_SIGN ?
304 PSA_SUCCESS :
305 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100306 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
307 payload, payload_length,
308 signature, signature_length ),
309 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200310 }
311
312 return( 1 );
313
314exit:
315 return( 0 );
316}
317
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100318static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200319 psa_key_usage_t usage,
320 psa_algorithm_t alg )
321{
322 unsigned char plaintext[256] = "Hello, world...";
323 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
324 size_t ciphertext_length = sizeof( ciphertext );
325 size_t plaintext_length = 16;
326
327 if( usage & PSA_KEY_USAGE_ENCRYPT )
328 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100329 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
330 plaintext, plaintext_length,
331 NULL, 0,
332 ciphertext, sizeof( ciphertext ),
333 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200334 }
335
336 if( usage & PSA_KEY_USAGE_DECRYPT )
337 {
338 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100339 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200340 ciphertext, ciphertext_length,
341 NULL, 0,
342 plaintext, sizeof( plaintext ),
343 &plaintext_length );
344 TEST_ASSERT( status == PSA_SUCCESS ||
345 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
346 ( status == PSA_ERROR_INVALID_ARGUMENT ||
347 status == PSA_ERROR_INVALID_PADDING ) ) );
348 }
349
350 return( 1 );
351
352exit:
353 return( 0 );
354}
Gilles Peskine02b75072018-07-01 22:31:34 +0200355
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100356static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200357 psa_key_usage_t usage,
358 psa_algorithm_t alg )
359{
360 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
361 unsigned char label[16] = "This is a label.";
362 size_t label_length = sizeof( label );
363 unsigned char seed[16] = "abcdefghijklmnop";
364 size_t seed_length = sizeof( seed );
365 unsigned char output[1];
366
367 if( usage & PSA_KEY_USAGE_DERIVE )
368 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100369 PSA_ASSERT( psa_key_derivation( &generator,
370 handle, alg,
371 label, label_length,
372 seed, seed_length,
373 sizeof( output ) ) );
374 PSA_ASSERT( psa_generator_read( &generator,
375 output,
376 sizeof( output ) ) );
377 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200378 }
379
380 return( 1 );
381
382exit:
383 return( 0 );
384}
385
Gilles Peskinec7998b72018-11-07 18:45:02 +0100386/* We need two keys to exercise key agreement. Exercise the
387 * private key against its own public key. */
388static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator,
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100389 psa_key_handle_t handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100390 psa_algorithm_t alg )
391{
392 psa_key_type_t private_key_type;
393 psa_key_type_t public_key_type;
394 size_t key_bits;
395 uint8_t *public_key = NULL;
396 size_t public_key_length;
397 /* Return UNKNOWN_ERROR if something other than the final call to
398 * psa_key_agreement fails. This isn't fully satisfactory, but it's
399 * good enough: callers will report it as a failed test anyway. */
400 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
401
Gilles Peskine8817f612018-12-18 00:18:46 +0100402 PSA_ASSERT( psa_get_key_information( handle,
403 &private_key_type,
404 &key_bits ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100405 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
406 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
407 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100408 PSA_ASSERT( psa_export_public_key( handle,
409 public_key, public_key_length,
410 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100411
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100412 status = psa_key_agreement( generator, handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100413 public_key, public_key_length,
414 alg );
415exit:
416 mbedtls_free( public_key );
417 return( status );
418}
419
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100420static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200421 psa_key_usage_t usage,
422 psa_algorithm_t alg )
423{
424 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200425 unsigned char output[1];
426 int ok = 0;
427
428 if( usage & PSA_KEY_USAGE_DERIVE )
429 {
430 /* We need two keys to exercise key agreement. Exercise the
431 * private key against its own public key. */
Gilles Peskine8817f612018-12-18 00:18:46 +0100432 PSA_ASSERT( key_agreement_with_self( &generator, handle, alg ) );
433 PSA_ASSERT( psa_generator_read( &generator,
434 output,
435 sizeof( output ) ) );
436 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200437 }
438 ok = 1;
439
440exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200441 return( ok );
442}
443
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200444static int is_oid_of_key_type( psa_key_type_t type,
445 const uint8_t *oid, size_t oid_length )
446{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200447 const uint8_t *expected_oid = NULL;
448 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200449#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200450 if( PSA_KEY_TYPE_IS_RSA( type ) )
451 {
452 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
453 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
454 }
455 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200456#endif /* MBEDTLS_RSA_C */
457#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200458 if( PSA_KEY_TYPE_IS_ECC( type ) )
459 {
460 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
461 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
462 }
463 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200464#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200465 {
466 char message[40];
467 mbedtls_snprintf( message, sizeof( message ),
468 "OID not known for key type=0x%08lx",
469 (unsigned long) type );
470 test_fail( message, __LINE__, __FILE__ );
471 return( 0 );
472 }
473
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200474 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200475 return( 1 );
476
477exit:
478 return( 0 );
479}
480
481static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
482 size_t min_bits, size_t max_bits,
483 int must_be_odd )
484{
485 size_t len;
486 size_t actual_bits;
487 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100488 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100489 MBEDTLS_ASN1_INTEGER ),
490 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200491 /* Tolerate a slight departure from DER encoding:
492 * - 0 may be represented by an empty string or a 1-byte string.
493 * - The sign bit may be used as a value bit. */
494 if( ( len == 1 && ( *p )[0] == 0 ) ||
495 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
496 {
497 ++( *p );
498 --len;
499 }
500 if( min_bits == 0 && len == 0 )
501 return( 1 );
502 msb = ( *p )[0];
503 TEST_ASSERT( msb != 0 );
504 actual_bits = 8 * ( len - 1 );
505 while( msb != 0 )
506 {
507 msb >>= 1;
508 ++actual_bits;
509 }
510 TEST_ASSERT( actual_bits >= min_bits );
511 TEST_ASSERT( actual_bits <= max_bits );
512 if( must_be_odd )
513 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
514 *p += len;
515 return( 1 );
516exit:
517 return( 0 );
518}
519
520static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
521 size_t *len,
522 unsigned char n, unsigned char tag )
523{
524 int ret;
525 ret = mbedtls_asn1_get_tag( p, end, len,
526 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
527 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
528 if( ret != 0 )
529 return( ret );
530 end = *p + *len;
531 ret = mbedtls_asn1_get_tag( p, end, len, tag );
532 if( ret != 0 )
533 return( ret );
534 if( *p + *len != end )
535 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
536 return( 0 );
537}
538
539static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
540 uint8_t *exported, size_t exported_length )
541{
542 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100543 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200544 else
545 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200546
547#if defined(MBEDTLS_DES_C)
548 if( type == PSA_KEY_TYPE_DES )
549 {
550 /* Check the parity bits. */
551 unsigned i;
552 for( i = 0; i < bits / 8; i++ )
553 {
554 unsigned bit_count = 0;
555 unsigned m;
556 for( m = 1; m <= 0x100; m <<= 1 )
557 {
558 if( exported[i] & m )
559 ++bit_count;
560 }
561 TEST_ASSERT( bit_count % 2 != 0 );
562 }
563 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200564 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200565#endif
566
567#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
568 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
569 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200570 uint8_t *p = exported;
571 uint8_t *end = exported + exported_length;
572 size_t len;
573 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200574 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200575 * modulus INTEGER, -- n
576 * publicExponent INTEGER, -- e
577 * privateExponent INTEGER, -- d
578 * prime1 INTEGER, -- p
579 * prime2 INTEGER, -- q
580 * exponent1 INTEGER, -- d mod (p-1)
581 * exponent2 INTEGER, -- d mod (q-1)
582 * coefficient INTEGER, -- (inverse of q) mod p
583 * }
584 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100585 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
586 MBEDTLS_ASN1_SEQUENCE |
587 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
588 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200589 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
590 goto exit;
591 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
592 goto exit;
593 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
594 goto exit;
595 /* Require d to be at least half the size of n. */
596 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
597 goto exit;
598 /* Require p and q to be at most half the size of n, rounded up. */
599 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
600 goto exit;
601 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
602 goto exit;
603 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
604 goto exit;
605 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
606 goto exit;
607 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
608 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100609 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100610 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200611 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200612#endif /* MBEDTLS_RSA_C */
613
614#if defined(MBEDTLS_ECP_C)
615 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
616 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100617 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100618 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100619 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200620 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200621#endif /* MBEDTLS_ECP_C */
622
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200623 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
624 {
625 uint8_t *p = exported;
626 uint8_t *end = exported + exported_length;
627 size_t len;
628 mbedtls_asn1_buf alg;
629 mbedtls_asn1_buf params;
630 mbedtls_asn1_bitstring bitstring;
631 /* SubjectPublicKeyInfo ::= SEQUENCE {
632 * algorithm AlgorithmIdentifier,
633 * subjectPublicKey BIT STRING }
634 * AlgorithmIdentifier ::= SEQUENCE {
635 * algorithm OBJECT IDENTIFIER,
636 * parameters ANY DEFINED BY algorithm OPTIONAL }
637 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100638 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
639 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100640 MBEDTLS_ASN1_CONSTRUCTED ),
641 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100642 TEST_EQUAL( p + len, end );
643 TEST_EQUAL( mbedtls_asn1_get_alg( &p, end, &alg, &params ), 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200644 if( ! is_oid_of_key_type( type, alg.p, alg.len ) )
645 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100646 TEST_EQUAL( mbedtls_asn1_get_bitstring( &p, end, &bitstring ), 0 );
647 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200648 p = bitstring.p;
649#if defined(MBEDTLS_RSA_C)
650 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
651 {
652 /* RSAPublicKey ::= SEQUENCE {
653 * modulus INTEGER, -- n
654 * publicExponent INTEGER } -- e
655 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100656 TEST_EQUAL( bitstring.unused_bits, 0 );
657 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
658 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100659 MBEDTLS_ASN1_CONSTRUCTED ),
660 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100661 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200662 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
663 goto exit;
664 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
665 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100666 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200667 }
668 else
669#endif /* MBEDTLS_RSA_C */
670#if defined(MBEDTLS_ECP_C)
671 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
672 {
673 /* ECPoint ::= ...
Gilles Peskinec6290c02018-08-13 17:24:59 +0200674 * -- first 8 bits: 0x04 (uncompressed representation);
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200675 * -- then x_P as an n-bit string, big endian;
676 * -- then y_P as a n-bit string, big endian,
677 * -- where n is the order of the curve.
678 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100679 TEST_EQUAL( bitstring.unused_bits, 0 );
680 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
681 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200682 }
683 else
684#endif /* MBEDTLS_ECP_C */
685 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100686 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200687 mbedtls_snprintf( message, sizeof( message ),
688 "No sanity check for public key type=0x%08lx",
689 (unsigned long) type );
690 test_fail( message, __LINE__, __FILE__ );
691 return( 0 );
692 }
693 }
694 else
695
696 {
697 /* No sanity checks for other types */
698 }
699
700 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200701
702exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200703 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200704}
705
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100706static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200707 psa_key_usage_t usage )
708{
709 psa_key_type_t type;
710 size_t bits;
711 uint8_t *exported = NULL;
712 size_t exported_size = 0;
713 size_t exported_length = 0;
714 int ok = 0;
715
Gilles Peskine8817f612018-12-18 00:18:46 +0100716 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200717
718 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
719 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200720 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100721 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
722 PSA_ERROR_NOT_PERMITTED );
Gilles Peskined14664a2018-08-10 19:07:32 +0200723 return( 1 );
724 }
725
Gilles Peskined14664a2018-08-10 19:07:32 +0200726 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200727 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200728
Gilles Peskine8817f612018-12-18 00:18:46 +0100729 PSA_ASSERT( psa_export_key( handle,
730 exported, exported_size,
731 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200732 ok = exported_key_sanity_check( type, bits, exported, exported_length );
733
734exit:
735 mbedtls_free( exported );
736 return( ok );
737}
738
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100739static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200740{
741 psa_key_type_t type;
742 psa_key_type_t public_type;
743 size_t bits;
744 uint8_t *exported = NULL;
745 size_t exported_size = 0;
746 size_t exported_length = 0;
747 int ok = 0;
748
Gilles Peskine8817f612018-12-18 00:18:46 +0100749 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200750 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
751 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100752 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100753 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200754 return( 1 );
755 }
756
757 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
758 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200759 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200760
Gilles Peskine8817f612018-12-18 00:18:46 +0100761 PSA_ASSERT( psa_export_public_key( handle,
762 exported, exported_size,
763 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200764 ok = exported_key_sanity_check( public_type, bits,
765 exported, exported_length );
766
767exit:
768 mbedtls_free( exported );
769 return( ok );
770}
771
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100772static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +0200773 psa_key_usage_t usage,
774 psa_algorithm_t alg )
775{
776 int ok;
777 if( alg == 0 )
778 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
779 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100780 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200781 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100782 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200783 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100784 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200785 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100786 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200787 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100788 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200789 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100790 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200791 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100792 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200793 else
794 {
795 char message[40];
796 mbedtls_snprintf( message, sizeof( message ),
797 "No code to exercise alg=0x%08lx",
798 (unsigned long) alg );
799 test_fail( message, __LINE__, __FILE__ );
800 ok = 0;
801 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200802
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100803 ok = ok && exercise_export_key( handle, usage );
804 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +0200805
Gilles Peskine02b75072018-07-01 22:31:34 +0200806 return( ok );
807}
808
Gilles Peskine10df3412018-10-25 22:35:43 +0200809static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
810 psa_algorithm_t alg )
811{
812 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
813 {
814 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
815 PSA_KEY_USAGE_VERIFY :
816 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
817 }
818 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
819 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
820 {
821 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
822 PSA_KEY_USAGE_ENCRYPT :
823 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
824 }
825 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
826 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
827 {
828 return( PSA_KEY_USAGE_DERIVE );
829 }
830 else
831 {
832 return( 0 );
833 }
834
835}
Darryl Green0c6575a2018-11-07 16:05:30 +0000836
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100837/* An overapproximation of the amount of storage needed for a key of the
838 * given type and with the given content. The API doesn't make it easy
839 * to find a good value for the size. The current implementation doesn't
840 * care about the value anyway. */
841#define KEY_BITS_FROM_DATA( type, data ) \
842 ( data )->len
843
Darryl Green0c6575a2018-11-07 16:05:30 +0000844typedef enum {
845 IMPORT_KEY = 0,
846 GENERATE_KEY = 1,
847 DERIVE_KEY = 2
848} generate_method;
849
Gilles Peskinee59236f2018-01-27 23:32:46 +0100850/* END_HEADER */
851
852/* BEGIN_DEPENDENCIES
853 * depends_on:MBEDTLS_PSA_CRYPTO_C
854 * END_DEPENDENCIES
855 */
856
857/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200858void static_checks( )
859{
860 size_t max_truncated_mac_size =
861 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
862
863 /* Check that the length for a truncated MAC always fits in the algorithm
864 * encoding. The shifted mask is the maximum truncated value. The
865 * untruncated algorithm may be one byte larger. */
866 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
867}
868/* END_CASE */
869
870/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200871void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100873 psa_key_handle_t handle = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200874 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100875 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100876
Gilles Peskine8817f612018-12-18 00:18:46 +0100877 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100878
Gilles Peskine8817f612018-12-18 00:18:46 +0100879 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
880 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100881 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100882 TEST_EQUAL( status, expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100883 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100884 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100885
886exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100887 mbedtls_psa_crypto_free( );
888}
889/* END_CASE */
890
891/* BEGIN_CASE */
Gilles Peskinea4261682018-12-03 11:34:01 +0100892void import_twice( int alg_arg, int usage_arg,
893 int type1_arg, data_t *data1,
894 int expected_import1_status_arg,
895 int type2_arg, data_t *data2,
896 int expected_import2_status_arg )
897{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100898 psa_key_handle_t handle = 0;
Gilles Peskinea4261682018-12-03 11:34:01 +0100899 psa_algorithm_t alg = alg_arg;
900 psa_key_usage_t usage = usage_arg;
901 psa_key_type_t type1 = type1_arg;
902 psa_status_t expected_import1_status = expected_import1_status_arg;
903 psa_key_type_t type2 = type2_arg;
904 psa_status_t expected_import2_status = expected_import2_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +0000905 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea4261682018-12-03 11:34:01 +0100906 psa_status_t status;
907
Gilles Peskine8817f612018-12-18 00:18:46 +0100908 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100909
Gilles Peskine8817f612018-12-18 00:18:46 +0100910 PSA_ASSERT( psa_allocate_key( type1,
911 MAX( KEY_BITS_FROM_DATA( type1, data1 ),
912 KEY_BITS_FROM_DATA( type2, data2 ) ),
913 &handle ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100914 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100915 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100916
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100917 status = psa_import_key( handle, type1, data1->x, data1->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100918 TEST_EQUAL( status, expected_import1_status );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100919 status = psa_import_key( handle, type2, data2->x, data2->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100920 TEST_EQUAL( status, expected_import2_status );
Gilles Peskinea4261682018-12-03 11:34:01 +0100921
922 if( expected_import1_status == PSA_SUCCESS ||
923 expected_import2_status == PSA_SUCCESS )
924 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100925 TEST_ASSERT( exercise_key( handle, usage, alg ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100926 }
927
928exit:
929 mbedtls_psa_crypto_free( );
930}
931/* END_CASE */
932
933/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200934void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
935{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100936 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200937 size_t bits = bits_arg;
938 psa_status_t expected_status = expected_status_arg;
939 psa_status_t status;
940 psa_key_type_t type =
941 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
942 size_t buffer_size = /* Slight overapproximations */
943 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200944 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200945 unsigned char *p;
946 int ret;
947 size_t length;
948
Gilles Peskine8817f612018-12-18 00:18:46 +0100949 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200950 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200951
952 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
953 bits, keypair ) ) >= 0 );
954 length = ret;
955
956 /* Try importing the key */
Gilles Peskine8817f612018-12-18 00:18:46 +0100957 PSA_ASSERT( psa_allocate_key( type, bits, &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100958 status = psa_import_key( handle, type, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100959 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200960 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100961 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200962
963exit:
964 mbedtls_free( buffer );
965 mbedtls_psa_crypto_free( );
966}
967/* END_CASE */
968
969/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300970void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300971 int type_arg,
972 int alg_arg,
973 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100974 int expected_bits,
975 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200976 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100977 int canonical_input )
978{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100979 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100980 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200981 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200982 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100983 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100984 unsigned char *exported = NULL;
985 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100986 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100987 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100988 size_t reexported_length;
989 psa_key_type_t got_type;
990 size_t got_bits;
Jaeden Amero70261c52019-01-04 11:47:20 +0000991 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100992
Moran Pekercb088e72018-07-17 17:36:59 +0300993 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200994 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100995 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200996 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100997 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100998
Gilles Peskine8817f612018-12-18 00:18:46 +0100999 PSA_ASSERT( psa_allocate_key( type, expected_bits, &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001000 psa_key_policy_set_usage( &policy, usage_arg, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001001 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001002
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001003 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1004 PSA_ERROR_EMPTY_SLOT );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001005
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001006 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001007 PSA_ASSERT( psa_import_key( handle, type,
1008 data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001009
1010 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001011 PSA_ASSERT( psa_get_key_information( handle,
1012 &got_type,
1013 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001014 TEST_EQUAL( got_type, type );
1015 TEST_EQUAL( got_bits, (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001016
1017 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001018 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001019 exported, export_size,
1020 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001021 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001022
1023 /* The exported length must be set by psa_export_key() to a value between 0
1024 * and export_size. On errors, the exported length must be 0. */
1025 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1026 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1027 TEST_ASSERT( exported_length <= export_size );
1028
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001029 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001030 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001031 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001032 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001033 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001034 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001035 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001036
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001037 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001038 goto exit;
1039
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001040 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001041 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001042 else
1043 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001044 psa_key_handle_t handle2;
Gilles Peskine8817f612018-12-18 00:18:46 +01001045 PSA_ASSERT( psa_allocate_key( type, expected_bits, &handle2 ) );
1046 PSA_ASSERT( psa_set_key_policy( handle2, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001047
Gilles Peskine8817f612018-12-18 00:18:46 +01001048 PSA_ASSERT( psa_import_key( handle2, type,
1049 exported,
1050 exported_length ) );
1051 PSA_ASSERT( psa_export_key( handle2,
1052 reexported,
1053 export_size,
1054 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001055 ASSERT_COMPARE( exported, exported_length,
1056 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001057 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001058 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001059 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001060
1061destroy:
1062 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001063 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001064 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1065 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001066
1067exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001068 mbedtls_free( exported );
1069 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001070 mbedtls_psa_crypto_free( );
1071}
1072/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001073
Moran Pekerf709f4a2018-06-06 17:26:04 +03001074/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001075void import_key_nonempty_slot( )
1076{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001077 psa_key_handle_t handle = 0;
Moran Peker28a38e62018-11-07 16:18:24 +02001078 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1079 psa_status_t status;
1080 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
Gilles Peskine8817f612018-12-18 00:18:46 +01001081 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001082
Gilles Peskine8817f612018-12-18 00:18:46 +01001083 PSA_ASSERT( psa_allocate_key( type, PSA_BYTES_TO_BITS( sizeof( data ) ),
1084 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001085
Moran Peker28a38e62018-11-07 16:18:24 +02001086 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001087 PSA_ASSERT( psa_import_key( handle, type,
1088 data, sizeof( data ) ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001089
1090 /* Import the key again */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001091 status = psa_import_key( handle, type, data, sizeof( data ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001092 TEST_EQUAL( status, PSA_ERROR_OCCUPIED_SLOT );
Moran Peker28a38e62018-11-07 16:18:24 +02001093
1094exit:
1095 mbedtls_psa_crypto_free( );
1096}
1097/* END_CASE */
1098
1099/* BEGIN_CASE */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001100void export_invalid_handle( int handle, int expected_export_status_arg )
Moran Peker28a38e62018-11-07 16:18:24 +02001101{
1102 psa_status_t status;
1103 unsigned char *exported = NULL;
1104 size_t export_size = 0;
1105 size_t exported_length = INVALID_EXPORT_LENGTH;
1106 psa_status_t expected_export_status = expected_export_status_arg;
1107
Gilles Peskine8817f612018-12-18 00:18:46 +01001108 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001109
1110 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001111 status = psa_export_key( (psa_key_handle_t) handle,
Moran Peker28a38e62018-11-07 16:18:24 +02001112 exported, export_size,
1113 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001114 TEST_EQUAL( status, expected_export_status );
Moran Peker28a38e62018-11-07 16:18:24 +02001115
1116exit:
1117 mbedtls_psa_crypto_free( );
1118}
1119/* END_CASE */
1120
1121/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001122void export_with_no_key_activity( )
1123{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001124 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001125 psa_algorithm_t alg = PSA_ALG_CTR;
1126 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001127 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001128 unsigned char *exported = NULL;
1129 size_t export_size = 0;
1130 size_t exported_length = INVALID_EXPORT_LENGTH;
1131
Gilles Peskine8817f612018-12-18 00:18:46 +01001132 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001133
Gilles Peskine8817f612018-12-18 00:18:46 +01001134 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1135 &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001136 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001137 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001138
1139 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001140 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001141 exported, export_size,
1142 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001143 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001144
1145exit:
1146 mbedtls_psa_crypto_free( );
1147}
1148/* END_CASE */
1149
1150/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001151void cipher_with_no_key_activity( )
1152{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001153 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001154 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001155 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001156 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001157 int exercise_alg = PSA_ALG_CTR;
1158
Gilles Peskine8817f612018-12-18 00:18:46 +01001159 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001160
Gilles Peskine8817f612018-12-18 00:18:46 +01001161 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1162 &handle ) );
Moran Pekerce500072018-11-07 16:20:07 +02001163 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001164 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerce500072018-11-07 16:20:07 +02001165
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001166 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001167 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001168
1169exit:
1170 psa_cipher_abort( &operation );
1171 mbedtls_psa_crypto_free( );
1172}
1173/* END_CASE */
1174
1175/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001176void export_after_import_failure( data_t *data, int type_arg,
1177 int expected_import_status_arg )
1178{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001179 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001180 psa_key_type_t type = type_arg;
1181 psa_status_t status;
1182 unsigned char *exported = NULL;
1183 size_t export_size = 0;
1184 psa_status_t expected_import_status = expected_import_status_arg;
1185 size_t exported_length = INVALID_EXPORT_LENGTH;
1186
Gilles Peskine8817f612018-12-18 00:18:46 +01001187 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001188
Gilles Peskine8817f612018-12-18 00:18:46 +01001189 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1190 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001191
Moran Peker34550092018-11-07 16:19:34 +02001192 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001193 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001194 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001195 TEST_EQUAL( status, expected_import_status );
Moran Peker34550092018-11-07 16:19:34 +02001196
1197 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001198 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001199 exported, export_size,
1200 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001201 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001202
1203exit:
1204 mbedtls_psa_crypto_free( );
1205}
1206/* END_CASE */
1207
1208/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001209void cipher_after_import_failure( data_t *data, int type_arg,
1210 int expected_import_status_arg )
1211{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001212 psa_key_handle_t handle = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001213 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001214 psa_key_type_t type = type_arg;
1215 psa_status_t status;
1216 psa_status_t expected_import_status = expected_import_status_arg;
1217 int exercise_alg = PSA_ALG_CTR;
1218
Gilles Peskine8817f612018-12-18 00:18:46 +01001219 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001220
Gilles Peskine8817f612018-12-18 00:18:46 +01001221 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1222 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001223
Moran Pekerce500072018-11-07 16:20:07 +02001224 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001225 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001226 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001227 TEST_EQUAL( status, expected_import_status );
Moran Pekerce500072018-11-07 16:20:07 +02001228
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001229 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001230 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001231
1232exit:
1233 psa_cipher_abort( &operation );
1234 mbedtls_psa_crypto_free( );
1235}
1236/* END_CASE */
1237
1238/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001239void export_after_destroy_key( data_t *data, int type_arg )
1240{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001241 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001242 psa_key_type_t type = type_arg;
1243 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001244 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001245 psa_algorithm_t alg = PSA_ALG_CTR;
1246 unsigned char *exported = NULL;
1247 size_t export_size = 0;
1248 size_t exported_length = INVALID_EXPORT_LENGTH;
1249
Gilles Peskine8817f612018-12-18 00:18:46 +01001250 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001251
Gilles Peskine8817f612018-12-18 00:18:46 +01001252 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1253 &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001254 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001255 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001256 export_size = (ptrdiff_t) data->len;
1257 ASSERT_ALLOC( exported, export_size );
1258
1259 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001260 PSA_ASSERT( psa_import_key( handle, type,
1261 data->x, data->len ) );
Moran Peker34550092018-11-07 16:19:34 +02001262
Gilles Peskine8817f612018-12-18 00:18:46 +01001263 PSA_ASSERT( psa_export_key( handle, exported, export_size,
1264 &exported_length ) );
Moran Peker34550092018-11-07 16:19:34 +02001265
1266 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001267 PSA_ASSERT( psa_destroy_key( handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001268
1269 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001270 status = psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001271 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001272 TEST_EQUAL( status, PSA_ERROR_INVALID_HANDLE );
Moran Peker34550092018-11-07 16:19:34 +02001273
1274exit:
1275 mbedtls_free( exported );
1276 mbedtls_psa_crypto_free( );
1277}
1278/* END_CASE */
1279
1280/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001281void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001282 int type_arg,
1283 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001284 int export_size_delta,
1285 int expected_export_status_arg,
1286 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001287{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001288 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001289 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001290 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001291 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001292 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001293 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001294 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001295 size_t exported_length = INVALID_EXPORT_LENGTH;
Jaeden Amero70261c52019-01-04 11:47:20 +00001296 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001297
Gilles Peskine8817f612018-12-18 00:18:46 +01001298 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001299
Gilles Peskine8817f612018-12-18 00:18:46 +01001300 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1301 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001302 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001303 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001304
1305 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001306 PSA_ASSERT( psa_import_key( handle, type,
1307 data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001308
Gilles Peskine49c25912018-10-29 15:15:31 +01001309 /* Export the public key */
1310 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001311 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001312 exported, export_size,
1313 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001314 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001315 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001316 {
1317 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1318 size_t bits;
Gilles Peskine8817f612018-12-18 00:18:46 +01001319 PSA_ASSERT( psa_get_key_information( handle, NULL, &bits ) );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001320 TEST_ASSERT( expected_public_key->len <=
1321 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001322 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1323 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001324 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001325
1326exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001327 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001328 psa_destroy_key( handle );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001329 mbedtls_psa_crypto_free( );
1330}
1331/* END_CASE */
1332
Gilles Peskine20035e32018-02-03 22:44:14 +01001333/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001334void import_and_exercise_key( data_t *data,
1335 int type_arg,
1336 int bits_arg,
1337 int alg_arg )
1338{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001339 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001340 psa_key_type_t type = type_arg;
1341 size_t bits = bits_arg;
1342 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001343 psa_key_usage_t usage = usage_to_exercise( type, alg );
Jaeden Amero70261c52019-01-04 11:47:20 +00001344 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001345 psa_key_type_t got_type;
1346 size_t got_bits;
1347 psa_status_t status;
1348
Gilles Peskine8817f612018-12-18 00:18:46 +01001349 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001350
Gilles Peskine8817f612018-12-18 00:18:46 +01001351 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1352 &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001353 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001354 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001355
1356 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001357 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01001358 PSA_ASSERT( status );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001359
1360 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001361 PSA_ASSERT( psa_get_key_information( handle,
1362 &got_type,
1363 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001364 TEST_EQUAL( got_type, type );
1365 TEST_EQUAL( got_bits, bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001366
1367 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001368 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001369 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001370
1371exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001372 psa_destroy_key( handle );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001373 mbedtls_psa_crypto_free( );
1374}
1375/* END_CASE */
1376
1377/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001378void key_policy( int usage_arg, int alg_arg )
1379{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001380 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001381 psa_algorithm_t alg = alg_arg;
1382 psa_key_usage_t usage = usage_arg;
1383 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1384 unsigned char key[32] = {0};
Jaeden Amero70261c52019-01-04 11:47:20 +00001385 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
1386 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001387
1388 memset( key, 0x2a, sizeof( key ) );
1389
Gilles Peskine8817f612018-12-18 00:18:46 +01001390 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001391
Gilles Peskine8817f612018-12-18 00:18:46 +01001392 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( sizeof( key ) ),
1393 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001394 psa_key_policy_set_usage( &policy_set, usage, alg );
1395
Gilles Peskinefe11b722018-12-18 00:24:04 +01001396 TEST_EQUAL( psa_key_policy_get_usage( &policy_set ), usage );
1397 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_set ), alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001398 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001399
Gilles Peskine8817f612018-12-18 00:18:46 +01001400 PSA_ASSERT( psa_import_key( handle, key_type,
1401 key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001402
Gilles Peskine8817f612018-12-18 00:18:46 +01001403 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001404
Gilles Peskinefe11b722018-12-18 00:24:04 +01001405 TEST_EQUAL( policy_get.usage, policy_set.usage );
1406 TEST_EQUAL( policy_get.alg, policy_set.alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001407
1408exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001409 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001410 mbedtls_psa_crypto_free( );
1411}
1412/* END_CASE */
1413
1414/* BEGIN_CASE */
Jaeden Amero70261c52019-01-04 11:47:20 +00001415void key_policy_init( )
1416{
1417 /* Test each valid way of initializing the object, except for `= {0}`, as
1418 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1419 * though it's OK by the C standard. We could test for this, but we'd need
1420 * to supress the Clang warning for the test. */
1421 psa_key_policy_t func = psa_key_policy_init( );
1422 psa_key_policy_t init = PSA_KEY_POLICY_INIT;
1423 psa_key_policy_t zero;
1424
1425 memset( &zero, 0, sizeof( zero ) );
1426
1427 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1428 * specification, we test that all valid ways of initializing the object
1429 * have the same bit pattern. This is a stronger requirement that may not
1430 * be valid on all platforms or PSA Crypto implementations, but implies the
1431 * weaker actual requirement is met: that a freshly initialized object, no
1432 * matter how it was initialized, acts the same as any other valid
1433 * initialization. */
1434 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1435 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1436}
1437/* END_CASE */
1438
1439/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001440void mac_key_policy( int policy_usage,
1441 int policy_alg,
1442 int key_type,
1443 data_t *key_data,
1444 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001445{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001446 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001447 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001448 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001449 psa_status_t status;
1450 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001451
Gilles Peskine8817f612018-12-18 00:18:46 +01001452 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001453
Gilles Peskine8817f612018-12-18 00:18:46 +01001454 PSA_ASSERT( psa_allocate_key( key_type,
1455 KEY_BITS_FROM_DATA( key_type, key_data ),
1456 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001457 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001458 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001459
Gilles Peskine8817f612018-12-18 00:18:46 +01001460 PSA_ASSERT( psa_import_key( handle, key_type,
1461 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001462
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001463 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001464 if( policy_alg == exercise_alg &&
1465 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001466 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001467 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001468 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001469 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001470
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001471 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001472 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001473 if( policy_alg == exercise_alg &&
1474 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001475 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001476 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001477 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001478
1479exit:
1480 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001481 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001482 mbedtls_psa_crypto_free( );
1483}
1484/* END_CASE */
1485
1486/* BEGIN_CASE */
1487void cipher_key_policy( int policy_usage,
1488 int policy_alg,
1489 int key_type,
1490 data_t *key_data,
1491 int exercise_alg )
1492{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001493 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001494 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001495 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001496 psa_status_t status;
1497
Gilles Peskine8817f612018-12-18 00:18:46 +01001498 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001499
Gilles Peskine8817f612018-12-18 00:18:46 +01001500 PSA_ASSERT( psa_allocate_key( key_type,
1501 KEY_BITS_FROM_DATA( key_type, key_data ),
1502 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001503 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001504 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001505
Gilles Peskine8817f612018-12-18 00:18:46 +01001506 PSA_ASSERT( psa_import_key( handle, key_type,
1507 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001508
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001509 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001510 if( policy_alg == exercise_alg &&
1511 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001512 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001513 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001514 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001515 psa_cipher_abort( &operation );
1516
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001517 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001518 if( policy_alg == exercise_alg &&
1519 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001520 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001521 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001522 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001523
1524exit:
1525 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001526 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001527 mbedtls_psa_crypto_free( );
1528}
1529/* END_CASE */
1530
1531/* BEGIN_CASE */
1532void aead_key_policy( int policy_usage,
1533 int policy_alg,
1534 int key_type,
1535 data_t *key_data,
1536 int nonce_length_arg,
1537 int tag_length_arg,
1538 int exercise_alg )
1539{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001540 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001541 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001542 psa_status_t status;
1543 unsigned char nonce[16] = {0};
1544 size_t nonce_length = nonce_length_arg;
1545 unsigned char tag[16];
1546 size_t tag_length = tag_length_arg;
1547 size_t output_length;
1548
1549 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1550 TEST_ASSERT( tag_length <= sizeof( tag ) );
1551
Gilles Peskine8817f612018-12-18 00:18:46 +01001552 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001553
Gilles Peskine8817f612018-12-18 00:18:46 +01001554 PSA_ASSERT( psa_allocate_key( key_type,
1555 KEY_BITS_FROM_DATA( key_type, key_data ),
1556 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001557 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001558 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001559
Gilles Peskine8817f612018-12-18 00:18:46 +01001560 PSA_ASSERT( psa_import_key( handle, key_type,
1561 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001562
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001563 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001564 nonce, nonce_length,
1565 NULL, 0,
1566 NULL, 0,
1567 tag, tag_length,
1568 &output_length );
1569 if( policy_alg == exercise_alg &&
1570 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001571 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001572 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001573 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001574
1575 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001576 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001577 nonce, nonce_length,
1578 NULL, 0,
1579 tag, tag_length,
1580 NULL, 0,
1581 &output_length );
1582 if( policy_alg == exercise_alg &&
1583 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001584 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001585 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001586 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001587
1588exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001589 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001590 mbedtls_psa_crypto_free( );
1591}
1592/* END_CASE */
1593
1594/* BEGIN_CASE */
1595void asymmetric_encryption_key_policy( int policy_usage,
1596 int policy_alg,
1597 int key_type,
1598 data_t *key_data,
1599 int exercise_alg )
1600{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001601 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001602 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001603 psa_status_t status;
1604 size_t key_bits;
1605 size_t buffer_length;
1606 unsigned char *buffer = NULL;
1607 size_t output_length;
1608
Gilles Peskine8817f612018-12-18 00:18:46 +01001609 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001610
Gilles Peskine8817f612018-12-18 00:18:46 +01001611 PSA_ASSERT( psa_allocate_key( key_type,
1612 KEY_BITS_FROM_DATA( key_type, key_data ),
1613 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001614 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001615 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001616
Gilles Peskine8817f612018-12-18 00:18:46 +01001617 PSA_ASSERT( psa_import_key( handle, key_type,
1618 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001619
Gilles Peskine8817f612018-12-18 00:18:46 +01001620 PSA_ASSERT( psa_get_key_information( handle,
1621 NULL,
1622 &key_bits ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001623 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1624 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001625 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001626
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001627 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001628 NULL, 0,
1629 NULL, 0,
1630 buffer, buffer_length,
1631 &output_length );
1632 if( policy_alg == exercise_alg &&
1633 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001634 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001635 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001636 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001637
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001638 if( buffer_length != 0 )
1639 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001640 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001641 buffer, buffer_length,
1642 NULL, 0,
1643 buffer, buffer_length,
1644 &output_length );
1645 if( policy_alg == exercise_alg &&
1646 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001647 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001648 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001649 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001650
1651exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001652 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001653 mbedtls_psa_crypto_free( );
1654 mbedtls_free( buffer );
1655}
1656/* END_CASE */
1657
1658/* BEGIN_CASE */
1659void asymmetric_signature_key_policy( int policy_usage,
1660 int policy_alg,
1661 int key_type,
1662 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001663 int exercise_alg,
1664 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001665{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001666 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001667 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001668 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001669 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1670 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1671 * compatible with the policy and `payload_length_arg` is supposed to be
1672 * a valid input length to sign. If `payload_length_arg <= 0`,
1673 * `exercise_alg` is supposed to be forbidden by the policy. */
1674 int compatible_alg = payload_length_arg > 0;
1675 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001676 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1677 size_t signature_length;
1678
Gilles Peskine8817f612018-12-18 00:18:46 +01001679 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001680
Gilles Peskine8817f612018-12-18 00:18:46 +01001681 PSA_ASSERT( psa_allocate_key( key_type,
1682 KEY_BITS_FROM_DATA( key_type, key_data ),
1683 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001684 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001685 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001686
Gilles Peskine8817f612018-12-18 00:18:46 +01001687 PSA_ASSERT( psa_import_key( handle, key_type,
1688 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001689
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001690 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001691 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001692 signature, sizeof( signature ),
1693 &signature_length );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001694 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001695 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001696 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001697 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001698
1699 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001700 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001701 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001702 signature, sizeof( signature ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001703 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001704 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001705 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001706 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001707
1708exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001709 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001710 mbedtls_psa_crypto_free( );
1711}
1712/* END_CASE */
1713
1714/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001715void derive_key_policy( int policy_usage,
1716 int policy_alg,
1717 int key_type,
1718 data_t *key_data,
1719 int exercise_alg )
1720{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001721 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001722 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001723 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1724 psa_status_t status;
1725
Gilles Peskine8817f612018-12-18 00:18:46 +01001726 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001727
Gilles Peskine8817f612018-12-18 00:18:46 +01001728 PSA_ASSERT( psa_allocate_key( key_type,
1729 KEY_BITS_FROM_DATA( key_type, key_data ),
1730 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001731 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001732 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001733
Gilles Peskine8817f612018-12-18 00:18:46 +01001734 PSA_ASSERT( psa_import_key( handle, key_type,
1735 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001736
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001737 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001738 exercise_alg,
1739 NULL, 0,
1740 NULL, 0,
1741 1 );
1742 if( policy_alg == exercise_alg &&
1743 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001744 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001745 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001746 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001747
1748exit:
1749 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001750 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001751 mbedtls_psa_crypto_free( );
1752}
1753/* END_CASE */
1754
1755/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001756void agreement_key_policy( int policy_usage,
1757 int policy_alg,
1758 int key_type_arg,
1759 data_t *key_data,
1760 int exercise_alg )
1761{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001762 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001763 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001764 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001765 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1766 psa_status_t status;
1767
Gilles Peskine8817f612018-12-18 00:18:46 +01001768 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001769
Gilles Peskine8817f612018-12-18 00:18:46 +01001770 PSA_ASSERT( psa_allocate_key( key_type,
1771 KEY_BITS_FROM_DATA( key_type, key_data ),
1772 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001773 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001774 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001775
Gilles Peskine8817f612018-12-18 00:18:46 +01001776 PSA_ASSERT( psa_import_key( handle, key_type,
1777 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001778
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001779 status = key_agreement_with_self( &generator, handle, exercise_alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001780
Gilles Peskine01d718c2018-09-18 12:01:02 +02001781 if( policy_alg == exercise_alg &&
1782 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001783 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001784 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001785 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001786
1787exit:
1788 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001789 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001790 mbedtls_psa_crypto_free( );
1791}
1792/* END_CASE */
1793
1794/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001795void hash_operation_init( )
1796{
1797 /* Test each valid way of initializing the object, except for `= {0}`, as
1798 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1799 * though it's OK by the C standard. We could test for this, but we'd need
1800 * to supress the Clang warning for the test. */
1801 psa_hash_operation_t func = psa_hash_operation_init( );
1802 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1803 psa_hash_operation_t zero;
1804
1805 memset( &zero, 0, sizeof( zero ) );
1806
1807 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1808 * specification, we test that all valid ways of initializing the object
1809 * have the same bit pattern. This is a stronger requirement that may not
1810 * be valid on all platforms or PSA Crypto implementations, but implies the
1811 * weaker actual requirement is met: that a freshly initialized object, no
1812 * matter how it was initialized, acts the same as any other valid
1813 * initialization. */
1814 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1815 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1816}
1817/* END_CASE */
1818
1819/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001820void hash_setup( int alg_arg,
1821 int expected_status_arg )
1822{
1823 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001824 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001825 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001826 psa_status_t status;
1827
Gilles Peskine8817f612018-12-18 00:18:46 +01001828 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001829
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001830 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001831 psa_hash_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001832 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001833
1834exit:
1835 mbedtls_psa_crypto_free( );
1836}
1837/* END_CASE */
1838
1839/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02001840void hash_bad_order( )
1841{
1842 unsigned char input[] = "";
1843 /* SHA-256 hash of an empty string */
1844 unsigned char hash[] = {
1845 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1846 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1847 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
1848 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001849 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001850
Gilles Peskine8817f612018-12-18 00:18:46 +01001851 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001852
1853 /* psa_hash_update without calling psa_hash_setup beforehand */
1854 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001855 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001856 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001857
1858 /* psa_hash_verify without calling psa_hash_setup beforehand */
1859 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001860 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001861 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001862
1863 /* psa_hash_finish without calling psa_hash_setup beforehand */
1864 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001865 TEST_EQUAL( psa_hash_finish( &operation,
1866 hash, sizeof( hash ), &hash_len ),
1867 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001868
1869exit:
1870 mbedtls_psa_crypto_free( );
1871}
1872/* END_CASE */
1873
itayzafrir27e69452018-11-01 14:26:34 +02001874/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1875void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001876{
1877 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001878 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1879 * appended to it */
1880 unsigned char hash[] = {
1881 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1882 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1883 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03001884 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001885 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001886
Gilles Peskine8817f612018-12-18 00:18:46 +01001887 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001888
itayzafrir27e69452018-11-01 14:26:34 +02001889 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001890 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001891 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001892 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001893
itayzafrir27e69452018-11-01 14:26:34 +02001894 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001895 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001896 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001897 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001898
itayzafrir27e69452018-11-01 14:26:34 +02001899 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001900 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001901 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001902 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001903
itayzafrirec93d302018-10-18 18:01:10 +03001904exit:
1905 mbedtls_psa_crypto_free( );
1906}
1907/* END_CASE */
1908
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001909/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1910void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001911{
1912 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001913 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03001914 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001915 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001916 size_t hash_len;
1917
Gilles Peskine8817f612018-12-18 00:18:46 +01001918 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001919
itayzafrir58028322018-10-25 10:22:01 +03001920 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001921 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001922 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001923 hash, expected_size - 1, &hash_len ),
1924 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001925
1926exit:
1927 mbedtls_psa_crypto_free( );
1928}
1929/* END_CASE */
1930
1931/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001932void mac_operation_init( )
1933{
1934 /* Test each valid way of initializing the object, except for `= {0}`, as
1935 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1936 * though it's OK by the C standard. We could test for this, but we'd need
1937 * to supress the Clang warning for the test. */
1938 psa_mac_operation_t func = psa_mac_operation_init( );
1939 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1940 psa_mac_operation_t zero;
1941
1942 memset( &zero, 0, sizeof( zero ) );
1943
1944 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1945 * specification, we test that all valid ways of initializing the object
1946 * have the same bit pattern. This is a stronger requirement that may not
1947 * be valid on all platforms or PSA Crypto implementations, but implies the
1948 * weaker actual requirement is met: that a freshly initialized object, no
1949 * matter how it was initialized, acts the same as any other valid
1950 * initialization. */
1951 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1952 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1953}
1954/* END_CASE */
1955
1956/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001957void mac_setup( int key_type_arg,
1958 data_t *key,
1959 int alg_arg,
1960 int expected_status_arg )
1961{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001962 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001963 psa_key_type_t key_type = key_type_arg;
1964 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001965 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001966 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00001967 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001968 psa_status_t status;
1969
Gilles Peskine8817f612018-12-18 00:18:46 +01001970 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001971
Gilles Peskine8817f612018-12-18 00:18:46 +01001972 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
1973 &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001974 psa_key_policy_set_usage( &policy,
1975 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1976 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001977 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001978
Gilles Peskine8817f612018-12-18 00:18:46 +01001979 PSA_ASSERT( psa_import_key( handle, key_type,
1980 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001981
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001982 status = psa_mac_sign_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001983 psa_mac_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001984 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001985
1986exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001987 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001988 mbedtls_psa_crypto_free( );
1989}
1990/* END_CASE */
1991
1992/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001993void mac_sign( int key_type_arg,
1994 data_t *key,
1995 int alg_arg,
1996 data_t *input,
1997 data_t *expected_mac )
1998{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001999 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002000 psa_key_type_t key_type = key_type_arg;
2001 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002002 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002003 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002004 /* Leave a little extra room in the output buffer. At the end of the
2005 * test, we'll check that the implementation didn't overwrite onto
2006 * this extra room. */
2007 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2008 size_t mac_buffer_size =
2009 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2010 size_t mac_length = 0;
2011
2012 memset( actual_mac, '+', sizeof( actual_mac ) );
2013 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2014 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2015
Gilles Peskine8817f612018-12-18 00:18:46 +01002016 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002017
Gilles Peskine8817f612018-12-18 00:18:46 +01002018 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2019 &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002020 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002021 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002022
Gilles Peskine8817f612018-12-18 00:18:46 +01002023 PSA_ASSERT( psa_import_key( handle, key_type,
2024 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002025
2026 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002027 PSA_ASSERT( psa_mac_sign_setup( &operation,
2028 handle, alg ) );
2029 PSA_ASSERT( psa_mac_update( &operation,
2030 input->x, input->len ) );
2031 PSA_ASSERT( psa_mac_sign_finish( &operation,
2032 actual_mac, mac_buffer_size,
2033 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002034
2035 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002036 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2037 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002038
2039 /* Verify that the end of the buffer is untouched. */
2040 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2041 sizeof( actual_mac ) - mac_length ) );
2042
2043exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002044 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002045 mbedtls_psa_crypto_free( );
2046}
2047/* END_CASE */
2048
2049/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002050void mac_verify( int key_type_arg,
2051 data_t *key,
2052 int alg_arg,
2053 data_t *input,
2054 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002055{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002056 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002057 psa_key_type_t key_type = key_type_arg;
2058 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002059 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002060 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002061
Gilles Peskine69c12672018-06-28 00:07:19 +02002062 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2063
Gilles Peskine8817f612018-12-18 00:18:46 +01002064 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002065
Gilles Peskine8817f612018-12-18 00:18:46 +01002066 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2067 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002068 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002069 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad16036df908f2018-04-02 08:34:15 -07002070
Gilles Peskine8817f612018-12-18 00:18:46 +01002071 PSA_ASSERT( psa_import_key( handle, key_type,
2072 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002073
Gilles Peskine8817f612018-12-18 00:18:46 +01002074 PSA_ASSERT( psa_mac_verify_setup( &operation,
2075 handle, alg ) );
2076 PSA_ASSERT( psa_destroy_key( handle ) );
2077 PSA_ASSERT( psa_mac_update( &operation,
2078 input->x, input->len ) );
2079 PSA_ASSERT( psa_mac_verify_finish( &operation,
2080 expected_mac->x,
2081 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002082
2083exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002084 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002085 mbedtls_psa_crypto_free( );
2086}
2087/* END_CASE */
2088
2089/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002090void cipher_operation_init( )
2091{
2092 /* Test each valid way of initializing the object, except for `= {0}`, as
2093 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2094 * though it's OK by the C standard. We could test for this, but we'd need
2095 * to supress the Clang warning for the test. */
2096 psa_cipher_operation_t func = psa_cipher_operation_init( );
2097 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2098 psa_cipher_operation_t zero;
2099
2100 memset( &zero, 0, sizeof( zero ) );
2101
2102 /* Although not technically guaranteed by the C standard nor the PSA Crypto
2103 * specification, we test that all valid ways of initializing the object
2104 * have the same bit pattern. This is a stronger requirement that may not
2105 * be valid on all platforms or PSA Crypto implementations, but implies the
2106 * weaker actual requirement is met: that a freshly initialized object, no
2107 * matter how it was initialized, acts the same as any other valid
2108 * initialization. */
2109 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
2110 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
2111}
2112/* END_CASE */
2113
2114/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002115void cipher_setup( int key_type_arg,
2116 data_t *key,
2117 int alg_arg,
2118 int expected_status_arg )
2119{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002120 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002121 psa_key_type_t key_type = key_type_arg;
2122 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002123 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002124 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002125 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002126 psa_status_t status;
2127
Gilles Peskine8817f612018-12-18 00:18:46 +01002128 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002129
Gilles Peskine8817f612018-12-18 00:18:46 +01002130 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2131 &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002132 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002133 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002134
Gilles Peskine8817f612018-12-18 00:18:46 +01002135 PSA_ASSERT( psa_import_key( handle, key_type,
2136 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002137
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002138 status = psa_cipher_encrypt_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002139 psa_cipher_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002140 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002141
2142exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002143 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002144 mbedtls_psa_crypto_free( );
2145}
2146/* END_CASE */
2147
2148/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002149void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002150 data_t *key,
2151 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002152 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002153{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002154 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002155 psa_status_t status;
2156 psa_key_type_t key_type = key_type_arg;
2157 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002158 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002159 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002160 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002161 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002162 size_t output_buffer_size = 0;
2163 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002164 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002165 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002166 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002167
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002168 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2169 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002170
Gilles Peskine8817f612018-12-18 00:18:46 +01002171 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002172
Gilles Peskine8817f612018-12-18 00:18:46 +01002173 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2174 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002175 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002176 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002177
Gilles Peskine8817f612018-12-18 00:18:46 +01002178 PSA_ASSERT( psa_import_key( handle, key_type,
2179 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002180
Gilles Peskine8817f612018-12-18 00:18:46 +01002181 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2182 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002183
Gilles Peskine8817f612018-12-18 00:18:46 +01002184 PSA_ASSERT( psa_cipher_set_iv( &operation,
2185 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002186 output_buffer_size = ( (size_t) input->len +
2187 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002188 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002189
Gilles Peskine8817f612018-12-18 00:18:46 +01002190 PSA_ASSERT( psa_cipher_update( &operation,
2191 input->x, input->len,
2192 output, output_buffer_size,
2193 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002194 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002195 status = psa_cipher_finish( &operation,
2196 output + function_output_length,
2197 output_buffer_size,
2198 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002199 total_output_length += function_output_length;
2200
Gilles Peskinefe11b722018-12-18 00:24:04 +01002201 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002202 if( expected_status == PSA_SUCCESS )
2203 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002204 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002205 ASSERT_COMPARE( expected_output->x, expected_output->len,
2206 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002207 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002208
Gilles Peskine50e586b2018-06-08 14:28:46 +02002209exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002210 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002211 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002212 mbedtls_psa_crypto_free( );
2213}
2214/* END_CASE */
2215
2216/* BEGIN_CASE */
2217void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002218 data_t *key,
2219 data_t *input,
2220 int first_part_size,
2221 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002222{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002223 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002224 psa_key_type_t key_type = key_type_arg;
2225 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002226 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002227 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002228 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002229 size_t output_buffer_size = 0;
2230 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002231 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002232 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002233 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002234
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002235 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2236 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002237
Gilles Peskine8817f612018-12-18 00:18:46 +01002238 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002239
Gilles Peskine8817f612018-12-18 00:18:46 +01002240 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2241 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002242 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002243 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002244
Gilles Peskine8817f612018-12-18 00:18:46 +01002245 PSA_ASSERT( psa_import_key( handle, key_type,
2246 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002247
Gilles Peskine8817f612018-12-18 00:18:46 +01002248 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2249 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002250
Gilles Peskine8817f612018-12-18 00:18:46 +01002251 PSA_ASSERT( psa_cipher_set_iv( &operation,
2252 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002253 output_buffer_size = ( (size_t) input->len +
2254 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002255 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002256
Gilles Peskine4abf7412018-06-18 16:35:34 +02002257 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002258 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2259 output, output_buffer_size,
2260 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002261 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002262 PSA_ASSERT( psa_cipher_update( &operation,
2263 input->x + first_part_size,
2264 input->len - first_part_size,
2265 output, output_buffer_size,
2266 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002267 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002268 PSA_ASSERT( psa_cipher_finish( &operation,
2269 output + function_output_length,
2270 output_buffer_size,
2271 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002272 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002273 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002274
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002275 ASSERT_COMPARE( expected_output->x, expected_output->len,
2276 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002277
2278exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002279 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002280 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002281 mbedtls_psa_crypto_free( );
2282}
2283/* END_CASE */
2284
2285/* BEGIN_CASE */
2286void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002287 data_t *key,
2288 data_t *input,
2289 int first_part_size,
2290 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002291{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002292 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002293
2294 psa_key_type_t key_type = key_type_arg;
2295 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002296 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002297 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002298 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002299 size_t output_buffer_size = 0;
2300 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002301 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002302 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002303 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002304
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002305 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2306 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002307
Gilles Peskine8817f612018-12-18 00:18:46 +01002308 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002309
Gilles Peskine8817f612018-12-18 00:18:46 +01002310 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2311 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002312 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002313 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002314
Gilles Peskine8817f612018-12-18 00:18:46 +01002315 PSA_ASSERT( psa_import_key( handle, key_type,
2316 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002317
Gilles Peskine8817f612018-12-18 00:18:46 +01002318 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2319 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002320
Gilles Peskine8817f612018-12-18 00:18:46 +01002321 PSA_ASSERT( psa_cipher_set_iv( &operation,
2322 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002323
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002324 output_buffer_size = ( (size_t) input->len +
2325 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002326 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002327
Gilles Peskine4abf7412018-06-18 16:35:34 +02002328 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002329 PSA_ASSERT( psa_cipher_update( &operation,
2330 input->x, first_part_size,
2331 output, output_buffer_size,
2332 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002333 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002334 PSA_ASSERT( psa_cipher_update( &operation,
2335 input->x + first_part_size,
2336 input->len - first_part_size,
2337 output, output_buffer_size,
2338 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002339 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002340 PSA_ASSERT( psa_cipher_finish( &operation,
2341 output + function_output_length,
2342 output_buffer_size,
2343 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002344 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002345 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002346
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002347 ASSERT_COMPARE( expected_output->x, expected_output->len,
2348 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002349
2350exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002351 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002352 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002353 mbedtls_psa_crypto_free( );
2354}
2355/* END_CASE */
2356
Gilles Peskine50e586b2018-06-08 14:28:46 +02002357/* BEGIN_CASE */
2358void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002359 data_t *key,
2360 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002361 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002362{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002363 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002364 psa_status_t status;
2365 psa_key_type_t key_type = key_type_arg;
2366 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002367 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002368 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002369 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002370 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002371 size_t output_buffer_size = 0;
2372 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002373 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002374 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002375 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002376
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002377 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2378 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002379
Gilles Peskine8817f612018-12-18 00:18:46 +01002380 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002381
Gilles Peskine8817f612018-12-18 00:18:46 +01002382 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2383 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002384 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002385 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002386
Gilles Peskine8817f612018-12-18 00:18:46 +01002387 PSA_ASSERT( psa_import_key( handle, key_type,
2388 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002389
Gilles Peskine8817f612018-12-18 00:18:46 +01002390 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2391 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002392
Gilles Peskine8817f612018-12-18 00:18:46 +01002393 PSA_ASSERT( psa_cipher_set_iv( &operation,
2394 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002395
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002396 output_buffer_size = ( (size_t) input->len +
2397 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002398 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002399
Gilles Peskine8817f612018-12-18 00:18:46 +01002400 PSA_ASSERT( psa_cipher_update( &operation,
2401 input->x, input->len,
2402 output, output_buffer_size,
2403 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002404 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002405 status = psa_cipher_finish( &operation,
2406 output + function_output_length,
2407 output_buffer_size,
2408 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002409 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002410 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002411
2412 if( expected_status == PSA_SUCCESS )
2413 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002414 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002415 ASSERT_COMPARE( expected_output->x, expected_output->len,
2416 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002417 }
2418
Gilles Peskine50e586b2018-06-08 14:28:46 +02002419exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002420 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002421 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002422 mbedtls_psa_crypto_free( );
2423}
2424/* END_CASE */
2425
Gilles Peskine50e586b2018-06-08 14:28:46 +02002426/* BEGIN_CASE */
2427void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002428 data_t *key,
2429 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002430{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002431 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002432 psa_key_type_t key_type = key_type_arg;
2433 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002434 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002435 size_t iv_size = 16;
2436 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002437 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002438 size_t output1_size = 0;
2439 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002440 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002441 size_t output2_size = 0;
2442 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002443 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002444 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2445 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002446 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002447
Gilles Peskine8817f612018-12-18 00:18:46 +01002448 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002449
Gilles Peskine8817f612018-12-18 00:18:46 +01002450 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2451 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002452 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002453 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002454
Gilles Peskine8817f612018-12-18 00:18:46 +01002455 PSA_ASSERT( psa_import_key( handle, key_type,
2456 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002457
Gilles Peskine8817f612018-12-18 00:18:46 +01002458 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2459 handle, alg ) );
2460 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2461 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002462
Gilles Peskine8817f612018-12-18 00:18:46 +01002463 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2464 iv, iv_size,
2465 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002466 output1_size = ( (size_t) input->len +
2467 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002468 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002469
Gilles Peskine8817f612018-12-18 00:18:46 +01002470 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2471 output1, output1_size,
2472 &output1_length ) );
2473 PSA_ASSERT( psa_cipher_finish( &operation1,
2474 output1 + output1_length, output1_size,
2475 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002476
Gilles Peskine048b7f02018-06-08 14:20:49 +02002477 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002478
Gilles Peskine8817f612018-12-18 00:18:46 +01002479 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002480
2481 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002482 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002483
Gilles Peskine8817f612018-12-18 00:18:46 +01002484 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2485 iv, iv_length ) );
2486 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2487 output2, output2_size,
2488 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002489 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002490 PSA_ASSERT( psa_cipher_finish( &operation2,
2491 output2 + output2_length,
2492 output2_size,
2493 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002494
Gilles Peskine048b7f02018-06-08 14:20:49 +02002495 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002496
Gilles Peskine8817f612018-12-18 00:18:46 +01002497 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002498
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002499 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002500
2501exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002502 mbedtls_free( output1 );
2503 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002504 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03002505 mbedtls_psa_crypto_free( );
2506}
2507/* END_CASE */
2508
2509/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002510void cipher_verify_output_multipart( int alg_arg,
2511 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002512 data_t *key,
2513 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002514 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002515{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002516 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002517 psa_key_type_t key_type = key_type_arg;
2518 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002519 unsigned char iv[16] = {0};
2520 size_t iv_size = 16;
2521 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002522 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002523 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002524 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002525 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002526 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002527 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002528 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002529 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2530 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002531 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002532
Gilles Peskine8817f612018-12-18 00:18:46 +01002533 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002534
Gilles Peskine8817f612018-12-18 00:18:46 +01002535 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2536 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002537 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002538 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002539
Gilles Peskine8817f612018-12-18 00:18:46 +01002540 PSA_ASSERT( psa_import_key( handle, key_type,
2541 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03002542
Gilles Peskine8817f612018-12-18 00:18:46 +01002543 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2544 handle, alg ) );
2545 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2546 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002547
Gilles Peskine8817f612018-12-18 00:18:46 +01002548 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2549 iv, iv_size,
2550 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002551 output1_buffer_size = ( (size_t) input->len +
2552 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002553 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002554
Gilles Peskine4abf7412018-06-18 16:35:34 +02002555 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002556
Gilles Peskine8817f612018-12-18 00:18:46 +01002557 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2558 output1, output1_buffer_size,
2559 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002560 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002561
Gilles Peskine8817f612018-12-18 00:18:46 +01002562 PSA_ASSERT( psa_cipher_update( &operation1,
2563 input->x + first_part_size,
2564 input->len - first_part_size,
2565 output1, output1_buffer_size,
2566 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002567 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002568
Gilles Peskine8817f612018-12-18 00:18:46 +01002569 PSA_ASSERT( psa_cipher_finish( &operation1,
2570 output1 + output1_length,
2571 output1_buffer_size - output1_length,
2572 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002573 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002574
Gilles Peskine8817f612018-12-18 00:18:46 +01002575 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002576
Gilles Peskine048b7f02018-06-08 14:20:49 +02002577 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002578 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002579
Gilles Peskine8817f612018-12-18 00:18:46 +01002580 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2581 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002582
Gilles Peskine8817f612018-12-18 00:18:46 +01002583 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2584 output2, output2_buffer_size,
2585 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002586 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002587
Gilles Peskine8817f612018-12-18 00:18:46 +01002588 PSA_ASSERT( psa_cipher_update( &operation2,
2589 output1 + first_part_size,
2590 output1_length - first_part_size,
2591 output2, output2_buffer_size,
2592 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002593 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002594
Gilles Peskine8817f612018-12-18 00:18:46 +01002595 PSA_ASSERT( psa_cipher_finish( &operation2,
2596 output2 + output2_length,
2597 output2_buffer_size - output2_length,
2598 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002599 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002600
Gilles Peskine8817f612018-12-18 00:18:46 +01002601 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002602
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002603 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002604
2605exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002606 mbedtls_free( output1 );
2607 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002608 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002609 mbedtls_psa_crypto_free( );
2610}
2611/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002612
Gilles Peskine20035e32018-02-03 22:44:14 +01002613/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002614void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002615 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002616 data_t *nonce,
2617 data_t *additional_data,
2618 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002619 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002620{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002621 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002622 psa_key_type_t key_type = key_type_arg;
2623 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002624 unsigned char *output_data = NULL;
2625 size_t output_size = 0;
2626 size_t output_length = 0;
2627 unsigned char *output_data2 = NULL;
2628 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002629 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002630 psa_status_t expected_result = expected_result_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002631 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002632
Gilles Peskine4abf7412018-06-18 16:35:34 +02002633 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002634 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002635
Gilles Peskine8817f612018-12-18 00:18:46 +01002636 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002637
Gilles Peskine8817f612018-12-18 00:18:46 +01002638 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2639 &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002640 psa_key_policy_set_usage( &policy,
2641 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2642 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002643 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002644
Gilles Peskine8817f612018-12-18 00:18:46 +01002645 PSA_ASSERT( psa_import_key( handle, key_type,
2646 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002647
Gilles Peskinefe11b722018-12-18 00:24:04 +01002648 TEST_EQUAL( psa_aead_encrypt( handle, alg,
2649 nonce->x, nonce->len,
2650 additional_data->x,
2651 additional_data->len,
2652 input_data->x, input_data->len,
2653 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002654 &output_length ),
2655 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002656
2657 if( PSA_SUCCESS == expected_result )
2658 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002659 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002660
Gilles Peskinefe11b722018-12-18 00:24:04 +01002661 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2662 nonce->x, nonce->len,
2663 additional_data->x,
2664 additional_data->len,
2665 output_data, output_length,
2666 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002667 &output_length2 ),
2668 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002669
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002670 ASSERT_COMPARE( input_data->x, input_data->len,
2671 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002672 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002673
Gilles Peskinea1cac842018-06-11 19:33:02 +02002674exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002675 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002676 mbedtls_free( output_data );
2677 mbedtls_free( output_data2 );
2678 mbedtls_psa_crypto_free( );
2679}
2680/* END_CASE */
2681
2682/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002683void aead_encrypt( int key_type_arg, data_t *key_data,
2684 int alg_arg,
2685 data_t *nonce,
2686 data_t *additional_data,
2687 data_t *input_data,
2688 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002689{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002690 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002691 psa_key_type_t key_type = key_type_arg;
2692 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002693 unsigned char *output_data = NULL;
2694 size_t output_size = 0;
2695 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002696 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002697 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002698
Gilles Peskine4abf7412018-06-18 16:35:34 +02002699 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002700 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002701
Gilles Peskine8817f612018-12-18 00:18:46 +01002702 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002703
Gilles Peskine8817f612018-12-18 00:18:46 +01002704 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2705 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002706 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002707 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002708
Gilles Peskine8817f612018-12-18 00:18:46 +01002709 PSA_ASSERT( psa_import_key( handle, key_type,
2710 key_data->x,
2711 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002712
Gilles Peskine8817f612018-12-18 00:18:46 +01002713 PSA_ASSERT( psa_aead_encrypt( handle, alg,
2714 nonce->x, nonce->len,
2715 additional_data->x, additional_data->len,
2716 input_data->x, input_data->len,
2717 output_data, output_size,
2718 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002719
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002720 ASSERT_COMPARE( expected_result->x, expected_result->len,
2721 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002722
Gilles Peskinea1cac842018-06-11 19:33:02 +02002723exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002724 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002725 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002726 mbedtls_psa_crypto_free( );
2727}
2728/* END_CASE */
2729
2730/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002731void aead_decrypt( int key_type_arg, data_t *key_data,
2732 int alg_arg,
2733 data_t *nonce,
2734 data_t *additional_data,
2735 data_t *input_data,
2736 data_t *expected_data,
2737 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002738{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002739 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002740 psa_key_type_t key_type = key_type_arg;
2741 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002742 unsigned char *output_data = NULL;
2743 size_t output_size = 0;
2744 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002745 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002746 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002747 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002748
Gilles Peskine4abf7412018-06-18 16:35:34 +02002749 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002750 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002751
Gilles Peskine8817f612018-12-18 00:18:46 +01002752 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002753
Gilles Peskine8817f612018-12-18 00:18:46 +01002754 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2755 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002756 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002757 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002758
Gilles Peskine8817f612018-12-18 00:18:46 +01002759 PSA_ASSERT( psa_import_key( handle, key_type,
2760 key_data->x,
2761 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002762
Gilles Peskinefe11b722018-12-18 00:24:04 +01002763 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2764 nonce->x, nonce->len,
2765 additional_data->x,
2766 additional_data->len,
2767 input_data->x, input_data->len,
2768 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002769 &output_length ),
2770 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002771
Gilles Peskine2d277862018-06-18 15:41:12 +02002772 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002773 ASSERT_COMPARE( expected_data->x, expected_data->len,
2774 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002775
Gilles Peskinea1cac842018-06-11 19:33:02 +02002776exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002777 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002778 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002779 mbedtls_psa_crypto_free( );
2780}
2781/* END_CASE */
2782
2783/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002784void signature_size( int type_arg,
2785 int bits,
2786 int alg_arg,
2787 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002788{
2789 psa_key_type_t type = type_arg;
2790 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002791 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002792 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002793exit:
2794 ;
2795}
2796/* END_CASE */
2797
2798/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002799void sign_deterministic( int key_type_arg, data_t *key_data,
2800 int alg_arg, data_t *input_data,
2801 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002802{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002803 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01002804 psa_key_type_t key_type = key_type_arg;
2805 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002806 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002807 unsigned char *signature = NULL;
2808 size_t signature_size;
2809 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002810 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01002811
Gilles Peskine8817f612018-12-18 00:18:46 +01002812 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002813
Gilles Peskine8817f612018-12-18 00:18:46 +01002814 PSA_ASSERT( psa_allocate_key( key_type,
2815 KEY_BITS_FROM_DATA( key_type, key_data ),
2816 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002817 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002818 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002819
Gilles Peskine8817f612018-12-18 00:18:46 +01002820 PSA_ASSERT( psa_import_key( handle, key_type,
2821 key_data->x,
2822 key_data->len ) );
2823 PSA_ASSERT( psa_get_key_information( handle,
2824 NULL,
2825 &key_bits ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002826
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002827 /* Allocate a buffer which has the size advertized by the
2828 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002829 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2830 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002831 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002832 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002833 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002834
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002835 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002836 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2837 input_data->x, input_data->len,
2838 signature, signature_size,
2839 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002840 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002841 ASSERT_COMPARE( output_data->x, output_data->len,
2842 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002843
2844exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002845 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01002846 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002847 mbedtls_psa_crypto_free( );
2848}
2849/* END_CASE */
2850
2851/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002852void sign_fail( int key_type_arg, data_t *key_data,
2853 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002854 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002855{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002856 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01002857 psa_key_type_t key_type = key_type_arg;
2858 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002859 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002860 psa_status_t actual_status;
2861 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002862 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002863 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002864 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01002865
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002866 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002867
Gilles Peskine8817f612018-12-18 00:18:46 +01002868 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002869
Gilles Peskine8817f612018-12-18 00:18:46 +01002870 PSA_ASSERT( psa_allocate_key( key_type,
2871 KEY_BITS_FROM_DATA( key_type, key_data ),
2872 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002873 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002874 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002875
Gilles Peskine8817f612018-12-18 00:18:46 +01002876 PSA_ASSERT( psa_import_key( handle, key_type,
2877 key_data->x,
2878 key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002879
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002880 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002881 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002882 signature, signature_size,
2883 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002884 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002885 /* The value of *signature_length is unspecified on error, but
2886 * whatever it is, it should be less than signature_size, so that
2887 * if the caller tries to read *signature_length bytes without
2888 * checking the error code then they don't overflow a buffer. */
2889 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002890
2891exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002892 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01002893 mbedtls_free( signature );
2894 mbedtls_psa_crypto_free( );
2895}
2896/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002897
2898/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002899void sign_verify( int key_type_arg, data_t *key_data,
2900 int alg_arg, data_t *input_data )
2901{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002902 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02002903 psa_key_type_t key_type = key_type_arg;
2904 psa_algorithm_t alg = alg_arg;
2905 size_t key_bits;
2906 unsigned char *signature = NULL;
2907 size_t signature_size;
2908 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002909 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02002910
Gilles Peskine8817f612018-12-18 00:18:46 +01002911 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002912
Gilles Peskine8817f612018-12-18 00:18:46 +01002913 PSA_ASSERT( psa_allocate_key( key_type,
2914 KEY_BITS_FROM_DATA( key_type, key_data ),
2915 &handle ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002916 psa_key_policy_set_usage( &policy,
2917 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2918 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002919 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002920
Gilles Peskine8817f612018-12-18 00:18:46 +01002921 PSA_ASSERT( psa_import_key( handle, key_type,
2922 key_data->x,
2923 key_data->len ) );
2924 PSA_ASSERT( psa_get_key_information( handle,
2925 NULL,
2926 &key_bits ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002927
2928 /* Allocate a buffer which has the size advertized by the
2929 * library. */
2930 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2931 key_bits, alg );
2932 TEST_ASSERT( signature_size != 0 );
2933 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002934 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002935
2936 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002937 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2938 input_data->x, input_data->len,
2939 signature, signature_size,
2940 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002941 /* Check that the signature length looks sensible. */
2942 TEST_ASSERT( signature_length <= signature_size );
2943 TEST_ASSERT( signature_length > 0 );
2944
2945 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002946 PSA_ASSERT( psa_asymmetric_verify(
2947 handle, alg,
2948 input_data->x, input_data->len,
2949 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002950
2951 if( input_data->len != 0 )
2952 {
2953 /* Flip a bit in the input and verify that the signature is now
2954 * detected as invalid. Flip a bit at the beginning, not at the end,
2955 * because ECDSA may ignore the last few bits of the input. */
2956 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002957 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
2958 input_data->x, input_data->len,
2959 signature, signature_length ),
2960 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02002961 }
2962
2963exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002964 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02002965 mbedtls_free( signature );
2966 mbedtls_psa_crypto_free( );
2967}
2968/* END_CASE */
2969
2970/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002971void asymmetric_verify( int key_type_arg, data_t *key_data,
2972 int alg_arg, data_t *hash_data,
2973 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002974{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002975 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03002976 psa_key_type_t key_type = key_type_arg;
2977 psa_algorithm_t alg = alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002978 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03002979
Gilles Peskine69c12672018-06-28 00:07:19 +02002980 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2981
Gilles Peskine8817f612018-12-18 00:18:46 +01002982 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03002983
Gilles Peskine8817f612018-12-18 00:18:46 +01002984 PSA_ASSERT( psa_allocate_key( key_type,
2985 KEY_BITS_FROM_DATA( key_type, key_data ),
2986 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002987 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002988 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
itayzafrir5c753392018-05-08 11:18:38 +03002989
Gilles Peskine8817f612018-12-18 00:18:46 +01002990 PSA_ASSERT( psa_import_key( handle, key_type,
2991 key_data->x,
2992 key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002993
Gilles Peskine8817f612018-12-18 00:18:46 +01002994 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
2995 hash_data->x, hash_data->len,
2996 signature_data->x,
2997 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002998exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002999 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03003000 mbedtls_psa_crypto_free( );
3001}
3002/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003003
3004/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003005void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3006 int alg_arg, data_t *hash_data,
3007 data_t *signature_data,
3008 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003009{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003010 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003011 psa_key_type_t key_type = key_type_arg;
3012 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003013 psa_status_t actual_status;
3014 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003015 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003016
Gilles Peskine8817f612018-12-18 00:18:46 +01003017 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003018
Gilles Peskine8817f612018-12-18 00:18:46 +01003019 PSA_ASSERT( psa_allocate_key( key_type,
3020 KEY_BITS_FROM_DATA( key_type, key_data ),
3021 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003022 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003023 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003024
Gilles Peskine8817f612018-12-18 00:18:46 +01003025 PSA_ASSERT( psa_import_key( handle, key_type,
3026 key_data->x,
3027 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003028
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003029 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003030 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003031 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003032 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003033
Gilles Peskinefe11b722018-12-18 00:24:04 +01003034 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003035
3036exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003037 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003038 mbedtls_psa_crypto_free( );
3039}
3040/* END_CASE */
3041
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003042/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003043void asymmetric_encrypt( int key_type_arg,
3044 data_t *key_data,
3045 int alg_arg,
3046 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003047 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003048 int expected_output_length_arg,
3049 int expected_status_arg )
3050{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003051 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003052 psa_key_type_t key_type = key_type_arg;
3053 psa_algorithm_t alg = alg_arg;
3054 size_t expected_output_length = expected_output_length_arg;
3055 size_t key_bits;
3056 unsigned char *output = NULL;
3057 size_t output_size;
3058 size_t output_length = ~0;
3059 psa_status_t actual_status;
3060 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003061 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003062
Gilles Peskine8817f612018-12-18 00:18:46 +01003063 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003064
Gilles Peskine656896e2018-06-29 19:12:28 +02003065 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01003066 PSA_ASSERT( psa_allocate_key( key_type,
3067 KEY_BITS_FROM_DATA( key_type, key_data ),
3068 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003069 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003070 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
3071 PSA_ASSERT( psa_import_key( handle, key_type,
3072 key_data->x,
3073 key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003074
3075 /* Determine the maximum output length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003076 PSA_ASSERT( psa_get_key_information( handle,
3077 NULL,
3078 &key_bits ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003079 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003080 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003081
3082 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003083 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003084 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003085 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003086 output, output_size,
3087 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003088 TEST_EQUAL( actual_status, expected_status );
3089 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003090
Gilles Peskine68428122018-06-30 18:42:41 +02003091 /* If the label is empty, the test framework puts a non-null pointer
3092 * in label->x. Test that a null pointer works as well. */
3093 if( label->len == 0 )
3094 {
3095 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003096 if( output_size != 0 )
3097 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003098 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003099 input_data->x, input_data->len,
3100 NULL, label->len,
3101 output, output_size,
3102 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003103 TEST_EQUAL( actual_status, expected_status );
3104 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003105 }
3106
Gilles Peskine656896e2018-06-29 19:12:28 +02003107exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003108 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003109 mbedtls_free( output );
3110 mbedtls_psa_crypto_free( );
3111}
3112/* END_CASE */
3113
3114/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003115void asymmetric_encrypt_decrypt( int key_type_arg,
3116 data_t *key_data,
3117 int alg_arg,
3118 data_t *input_data,
3119 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003120{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003121 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003122 psa_key_type_t key_type = key_type_arg;
3123 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003124 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003125 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003126 size_t output_size;
3127 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003128 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003129 size_t output2_size;
3130 size_t output2_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003131 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003132
Gilles Peskine8817f612018-12-18 00:18:46 +01003133 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003134
Gilles Peskine8817f612018-12-18 00:18:46 +01003135 PSA_ASSERT( psa_allocate_key( key_type,
3136 KEY_BITS_FROM_DATA( key_type, key_data ),
3137 &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003138 psa_key_policy_set_usage( &policy,
3139 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003140 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003141 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003142
Gilles Peskine8817f612018-12-18 00:18:46 +01003143 PSA_ASSERT( psa_import_key( handle, key_type,
3144 key_data->x,
3145 key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003146
3147 /* Determine the maximum ciphertext length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003148 PSA_ASSERT( psa_get_key_information( handle,
3149 NULL,
3150 &key_bits ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003151 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003152 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003153 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003154 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003155
Gilles Peskineeebd7382018-06-08 18:11:54 +02003156 /* We test encryption by checking that encrypt-then-decrypt gives back
3157 * the original plaintext because of the non-optional random
3158 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003159 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3160 input_data->x, input_data->len,
3161 label->x, label->len,
3162 output, output_size,
3163 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003164 /* We don't know what ciphertext length to expect, but check that
3165 * it looks sensible. */
3166 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003167
Gilles Peskine8817f612018-12-18 00:18:46 +01003168 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3169 output, output_length,
3170 label->x, label->len,
3171 output2, output2_size,
3172 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003173 ASSERT_COMPARE( input_data->x, input_data->len,
3174 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003175
3176exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003177 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003178 mbedtls_free( output );
3179 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003180 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003181}
3182/* END_CASE */
3183
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003184/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003185void asymmetric_decrypt( int key_type_arg,
3186 data_t *key_data,
3187 int alg_arg,
3188 data_t *input_data,
3189 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003190 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003191{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003192 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003193 psa_key_type_t key_type = key_type_arg;
3194 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003195 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003196 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003197 size_t output_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003198 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003199
Gilles Peskine4abf7412018-06-18 16:35:34 +02003200 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003201 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003202
Gilles Peskine8817f612018-12-18 00:18:46 +01003203 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003204
Gilles Peskine8817f612018-12-18 00:18:46 +01003205 PSA_ASSERT( psa_allocate_key( key_type,
3206 KEY_BITS_FROM_DATA( key_type, key_data ),
3207 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003208 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003209 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003210
Gilles Peskine8817f612018-12-18 00:18:46 +01003211 PSA_ASSERT( psa_import_key( handle, key_type,
3212 key_data->x,
3213 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003214
Gilles Peskine8817f612018-12-18 00:18:46 +01003215 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3216 input_data->x, input_data->len,
3217 label->x, label->len,
3218 output,
3219 output_size,
3220 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003221 ASSERT_COMPARE( expected_data->x, expected_data->len,
3222 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003223
Gilles Peskine68428122018-06-30 18:42:41 +02003224 /* If the label is empty, the test framework puts a non-null pointer
3225 * in label->x. Test that a null pointer works as well. */
3226 if( label->len == 0 )
3227 {
3228 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003229 if( output_size != 0 )
3230 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003231 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3232 input_data->x, input_data->len,
3233 NULL, label->len,
3234 output,
3235 output_size,
3236 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003237 ASSERT_COMPARE( expected_data->x, expected_data->len,
3238 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003239 }
3240
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003241exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003242 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003243 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003244 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003245}
3246/* END_CASE */
3247
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003248/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003249void asymmetric_decrypt_fail( int key_type_arg,
3250 data_t *key_data,
3251 int alg_arg,
3252 data_t *input_data,
3253 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003254 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003255{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003256 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003257 psa_key_type_t key_type = key_type_arg;
3258 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003259 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003260 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003261 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003262 psa_status_t actual_status;
3263 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003264 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003265
Gilles Peskine4abf7412018-06-18 16:35:34 +02003266 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003267 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003268
Gilles Peskine8817f612018-12-18 00:18:46 +01003269 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003270
Gilles Peskine8817f612018-12-18 00:18:46 +01003271 PSA_ASSERT( psa_allocate_key( key_type,
3272 KEY_BITS_FROM_DATA( key_type, key_data ),
3273 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003274 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003275 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003276
Gilles Peskine8817f612018-12-18 00:18:46 +01003277 PSA_ASSERT( psa_import_key( handle, key_type,
3278 key_data->x,
3279 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003280
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003281 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003282 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003283 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003284 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003285 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003286 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003287 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003288
Gilles Peskine68428122018-06-30 18:42:41 +02003289 /* If the label is empty, the test framework puts a non-null pointer
3290 * in label->x. Test that a null pointer works as well. */
3291 if( label->len == 0 )
3292 {
3293 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003294 if( output_size != 0 )
3295 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003296 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003297 input_data->x, input_data->len,
3298 NULL, label->len,
3299 output, output_size,
3300 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003301 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003302 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003303 }
3304
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003305exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003306 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02003307 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003308 mbedtls_psa_crypto_free( );
3309}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003310/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003311
3312/* BEGIN_CASE */
Jaeden Amerod94d6712019-01-04 14:11:48 +00003313void crypto_generator_init( )
3314{
3315 /* Test each valid way of initializing the object, except for `= {0}`, as
3316 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3317 * though it's OK by the C standard. We could test for this, but we'd need
3318 * to supress the Clang warning for the test. */
3319 psa_crypto_generator_t func = psa_crypto_generator_init( );
3320 psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT;
3321 psa_crypto_generator_t zero;
3322
3323 memset( &zero, 0, sizeof( zero ) );
3324
3325 /* Although not technically guaranteed by the C standard nor the PSA Crypto
3326 * specification, we test that all valid ways of initializing the object
3327 * have the same bit pattern. This is a stronger requirement that may not
3328 * be valid on all platforms or PSA Crypto implementations, but implies the
3329 * weaker actual requirement is met: that a freshly initialized object, no
3330 * matter how it was initialized, acts the same as any other valid
3331 * initialization. */
3332 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
3333 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
3334}
3335/* END_CASE */
3336
3337/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003338void derive_setup( int key_type_arg,
3339 data_t *key_data,
3340 int alg_arg,
3341 data_t *salt,
3342 data_t *label,
3343 int requested_capacity_arg,
3344 int expected_status_arg )
3345{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003346 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003347 size_t key_type = key_type_arg;
3348 psa_algorithm_t alg = alg_arg;
3349 size_t requested_capacity = requested_capacity_arg;
3350 psa_status_t expected_status = expected_status_arg;
3351 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003352 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003353
Gilles Peskine8817f612018-12-18 00:18:46 +01003354 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003355
Gilles Peskine8817f612018-12-18 00:18:46 +01003356 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
3357 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003358 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003359 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003360
Gilles Peskine8817f612018-12-18 00:18:46 +01003361 PSA_ASSERT( psa_import_key( handle, key_type,
3362 key_data->x,
3363 key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003364
Gilles Peskinefe11b722018-12-18 00:24:04 +01003365 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3366 salt->x, salt->len,
3367 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003368 requested_capacity ),
3369 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003370
3371exit:
3372 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003373 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003374 mbedtls_psa_crypto_free( );
3375}
3376/* END_CASE */
3377
3378/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003379void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003380{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003381 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003382 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003383 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003384 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003385 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003386 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003387 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3388 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3389 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Jaeden Amero70261c52019-01-04 11:47:20 +00003390 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003391
Gilles Peskine8817f612018-12-18 00:18:46 +01003392 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003393
Gilles Peskine8817f612018-12-18 00:18:46 +01003394 PSA_ASSERT( psa_allocate_key( key_type,
3395 PSA_BYTES_TO_BITS( sizeof( key_data ) ),
3396 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003397 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003398 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003399
Gilles Peskine8817f612018-12-18 00:18:46 +01003400 PSA_ASSERT( psa_import_key( handle, key_type,
3401 key_data,
3402 sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003403
3404 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01003405 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3406 NULL, 0,
3407 NULL, 0,
3408 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003409
3410 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003411 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3412 NULL, 0,
3413 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003414 capacity ),
3415 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003416
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003417 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003418
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003419 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
3420 PSA_ERROR_INSUFFICIENT_CAPACITY );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003421
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003422exit:
3423 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003424 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003425 mbedtls_psa_crypto_free( );
3426}
3427/* END_CASE */
3428
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003429/* BEGIN_CASE */
3430void test_derive_invalid_generator_tests( )
3431{
3432 uint8_t output_buffer[16];
3433 size_t buffer_size = 16;
3434 size_t capacity = 0;
3435 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3436
Nir Sonnenschein50789302018-10-31 12:16:38 +02003437 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003438 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003439
3440 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003441 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003442
Gilles Peskine8817f612018-12-18 00:18:46 +01003443 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003444
Nir Sonnenschein50789302018-10-31 12:16:38 +02003445 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003446 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003447
Nir Sonnenschein50789302018-10-31 12:16:38 +02003448 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003449 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003450
3451exit:
3452 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003453}
3454/* END_CASE */
3455
3456/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003457void derive_output( int alg_arg,
3458 data_t *key_data,
3459 data_t *salt,
3460 data_t *label,
3461 int requested_capacity_arg,
3462 data_t *expected_output1,
3463 data_t *expected_output2 )
3464{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003465 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003466 psa_algorithm_t alg = alg_arg;
3467 size_t requested_capacity = requested_capacity_arg;
3468 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3469 uint8_t *expected_outputs[2] =
3470 {expected_output1->x, expected_output2->x};
3471 size_t output_sizes[2] =
3472 {expected_output1->len, expected_output2->len};
3473 size_t output_buffer_size = 0;
3474 uint8_t *output_buffer = NULL;
3475 size_t expected_capacity;
3476 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003477 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003478 psa_status_t status;
3479 unsigned i;
3480
3481 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3482 {
3483 if( output_sizes[i] > output_buffer_size )
3484 output_buffer_size = output_sizes[i];
3485 if( output_sizes[i] == 0 )
3486 expected_outputs[i] = NULL;
3487 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003488 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003489 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003490
Gilles Peskine8817f612018-12-18 00:18:46 +01003491 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3492 PSA_BYTES_TO_BITS( key_data->len ),
3493 &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003494 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003495 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003496
Gilles Peskine8817f612018-12-18 00:18:46 +01003497 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3498 key_data->x,
3499 key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003500
3501 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003502 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3503 salt->x, salt->len,
3504 label->x, label->len,
3505 requested_capacity ) );
3506 PSA_ASSERT( psa_get_generator_capacity( &generator,
3507 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003508 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003509 expected_capacity = requested_capacity;
3510
3511 /* Expansion phase. */
3512 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3513 {
3514 /* Read some bytes. */
3515 status = psa_generator_read( &generator,
3516 output_buffer, output_sizes[i] );
3517 if( expected_capacity == 0 && output_sizes[i] == 0 )
3518 {
3519 /* Reading 0 bytes when 0 bytes are available can go either way. */
3520 TEST_ASSERT( status == PSA_SUCCESS ||
3521 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3522 continue;
3523 }
3524 else if( expected_capacity == 0 ||
3525 output_sizes[i] > expected_capacity )
3526 {
3527 /* Capacity exceeded. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003528 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003529 expected_capacity = 0;
3530 continue;
3531 }
3532 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003533 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003534 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003535 ASSERT_COMPARE( output_buffer, output_sizes[i],
3536 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003537 /* Check the generator status. */
3538 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01003539 PSA_ASSERT( psa_get_generator_capacity( &generator,
3540 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003541 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003542 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003543 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003544
3545exit:
3546 mbedtls_free( output_buffer );
3547 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003548 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003549 mbedtls_psa_crypto_free( );
3550}
3551/* END_CASE */
3552
3553/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003554void derive_full( int alg_arg,
3555 data_t *key_data,
3556 data_t *salt,
3557 data_t *label,
3558 int requested_capacity_arg )
3559{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003560 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02003561 psa_algorithm_t alg = alg_arg;
3562 size_t requested_capacity = requested_capacity_arg;
3563 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3564 unsigned char output_buffer[16];
3565 size_t expected_capacity = requested_capacity;
3566 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003567 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02003568
Gilles Peskine8817f612018-12-18 00:18:46 +01003569 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003570
Gilles Peskine8817f612018-12-18 00:18:46 +01003571 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3572 PSA_BYTES_TO_BITS( key_data->len ),
3573 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003574 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003575 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003576
Gilles Peskine8817f612018-12-18 00:18:46 +01003577 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3578 key_data->x,
3579 key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003580
3581 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003582 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3583 salt->x, salt->len,
3584 label->x, label->len,
3585 requested_capacity ) );
3586 PSA_ASSERT( psa_get_generator_capacity( &generator,
3587 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003588 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003589
3590 /* Expansion phase. */
3591 while( current_capacity > 0 )
3592 {
3593 size_t read_size = sizeof( output_buffer );
3594 if( read_size > current_capacity )
3595 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01003596 PSA_ASSERT( psa_generator_read( &generator,
3597 output_buffer,
3598 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003599 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01003600 PSA_ASSERT( psa_get_generator_capacity( &generator,
3601 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003602 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003603 }
3604
3605 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003606 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
3607 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskined54931c2018-07-17 21:06:59 +02003608
Gilles Peskine8817f612018-12-18 00:18:46 +01003609 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003610
3611exit:
3612 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003613 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02003614 mbedtls_psa_crypto_free( );
3615}
3616/* END_CASE */
3617
3618/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003619void derive_key_exercise( int alg_arg,
3620 data_t *key_data,
3621 data_t *salt,
3622 data_t *label,
3623 int derived_type_arg,
3624 int derived_bits_arg,
3625 int derived_usage_arg,
3626 int derived_alg_arg )
3627{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003628 psa_key_handle_t base_handle = 0;
3629 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003630 psa_algorithm_t alg = alg_arg;
3631 psa_key_type_t derived_type = derived_type_arg;
3632 size_t derived_bits = derived_bits_arg;
3633 psa_key_usage_t derived_usage = derived_usage_arg;
3634 psa_algorithm_t derived_alg = derived_alg_arg;
3635 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3636 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003637 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003638 psa_key_type_t got_type;
3639 size_t got_bits;
3640
Gilles Peskine8817f612018-12-18 00:18:46 +01003641 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003642
Gilles Peskine8817f612018-12-18 00:18:46 +01003643 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3644 PSA_BYTES_TO_BITS( key_data->len ),
3645 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003646 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003647 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3648 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3649 key_data->x,
3650 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003651
3652 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003653 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3654 salt->x, salt->len,
3655 label->x, label->len,
3656 capacity ) );
3657 PSA_ASSERT( psa_allocate_key( derived_type, derived_bits,
3658 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003659 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003660 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3661 PSA_ASSERT( psa_generator_import_key( derived_handle,
3662 derived_type,
3663 derived_bits,
3664 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003665
3666 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01003667 PSA_ASSERT( psa_get_key_information( derived_handle,
3668 &got_type,
3669 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003670 TEST_EQUAL( got_type, derived_type );
3671 TEST_EQUAL( got_bits, derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003672
3673 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003674 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02003675 goto exit;
3676
3677exit:
3678 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003679 psa_destroy_key( base_handle );
3680 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003681 mbedtls_psa_crypto_free( );
3682}
3683/* END_CASE */
3684
3685/* BEGIN_CASE */
3686void derive_key_export( int alg_arg,
3687 data_t *key_data,
3688 data_t *salt,
3689 data_t *label,
3690 int bytes1_arg,
3691 int bytes2_arg )
3692{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003693 psa_key_handle_t base_handle = 0;
3694 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003695 psa_algorithm_t alg = alg_arg;
3696 size_t bytes1 = bytes1_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003697 size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003698 size_t bytes2 = bytes2_arg;
3699 size_t capacity = bytes1 + bytes2;
3700 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003701 uint8_t *output_buffer = NULL;
3702 uint8_t *export_buffer = NULL;
Jaeden Amero70261c52019-01-04 11:47:20 +00003703 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003704 size_t length;
3705
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003706 ASSERT_ALLOC( output_buffer, capacity );
3707 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01003708 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003709
Gilles Peskine8817f612018-12-18 00:18:46 +01003710 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3711 PSA_BYTES_TO_BITS( key_data->len ),
3712 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003713 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003714 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3715 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3716 key_data->x,
3717 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003718
3719 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003720 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3721 salt->x, salt->len,
3722 label->x, label->len,
3723 capacity ) );
3724 PSA_ASSERT( psa_generator_read( &generator,
3725 output_buffer,
3726 capacity ) );
3727 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003728
3729 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003730 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3731 salt->x, salt->len,
3732 label->x, label->len,
3733 capacity ) );
3734 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, derived_bits,
3735 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003736 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003737 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3738 PSA_ASSERT( psa_generator_import_key( derived_handle,
3739 PSA_KEY_TYPE_RAW_DATA,
3740 derived_bits,
3741 &generator ) );
3742 PSA_ASSERT( psa_export_key( derived_handle,
3743 export_buffer, bytes1,
3744 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003745 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003746 PSA_ASSERT( psa_destroy_key( derived_handle ) );
3747 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA,
3748 PSA_BYTES_TO_BITS( bytes2 ),
3749 &derived_handle ) );
3750 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3751 PSA_ASSERT( psa_generator_import_key( derived_handle,
3752 PSA_KEY_TYPE_RAW_DATA,
3753 PSA_BYTES_TO_BITS( bytes2 ),
3754 &generator ) );
3755 PSA_ASSERT( psa_export_key( derived_handle,
3756 export_buffer + bytes1, bytes2,
3757 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003758 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003759
3760 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003761 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
3762 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003763
3764exit:
3765 mbedtls_free( output_buffer );
3766 mbedtls_free( export_buffer );
3767 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003768 psa_destroy_key( base_handle );
3769 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003770 mbedtls_psa_crypto_free( );
3771}
3772/* END_CASE */
3773
3774/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02003775void key_agreement_setup( int alg_arg,
3776 int our_key_type_arg, data_t *our_key_data,
3777 data_t *peer_key_data,
3778 int expected_status_arg )
3779{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003780 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003781 psa_algorithm_t alg = alg_arg;
3782 psa_key_type_t our_key_type = our_key_type_arg;
3783 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003784 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003785
Gilles Peskine8817f612018-12-18 00:18:46 +01003786 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003787
Gilles Peskine8817f612018-12-18 00:18:46 +01003788 PSA_ASSERT( psa_allocate_key( our_key_type,
3789 KEY_BITS_FROM_DATA( our_key_type,
3790 our_key_data ),
3791 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003792 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003793 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3794 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3795 our_key_data->x,
3796 our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003797
Gilles Peskinefe11b722018-12-18 00:24:04 +01003798 TEST_EQUAL( psa_key_agreement( &generator,
3799 our_key,
3800 peer_key_data->x, peer_key_data->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003801 alg ),
3802 expected_status_arg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003803
3804exit:
3805 psa_generator_abort( &generator );
3806 psa_destroy_key( our_key );
3807 mbedtls_psa_crypto_free( );
3808}
3809/* END_CASE */
3810
3811/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02003812void key_agreement_capacity( int alg_arg,
3813 int our_key_type_arg, data_t *our_key_data,
3814 data_t *peer_key_data,
3815 int expected_capacity_arg )
3816{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003817 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003818 psa_algorithm_t alg = alg_arg;
3819 psa_key_type_t our_key_type = our_key_type_arg;
3820 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003821 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02003822 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02003823 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02003824
Gilles Peskine8817f612018-12-18 00:18:46 +01003825 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003826
Gilles Peskine8817f612018-12-18 00:18:46 +01003827 PSA_ASSERT( psa_allocate_key( our_key_type,
3828 KEY_BITS_FROM_DATA( our_key_type,
3829 our_key_data ),
3830 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003831 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003832 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3833 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3834 our_key_data->x,
3835 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003836
Gilles Peskine8817f612018-12-18 00:18:46 +01003837 PSA_ASSERT( psa_key_agreement( &generator,
3838 our_key,
3839 peer_key_data->x, peer_key_data->len,
3840 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003841
Gilles Peskinebf491972018-10-25 22:36:12 +02003842 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003843 PSA_ASSERT( psa_get_generator_capacity(
3844 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003845 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02003846
Gilles Peskinebf491972018-10-25 22:36:12 +02003847 /* Test the actual capacity by reading the output. */
3848 while( actual_capacity > sizeof( output ) )
3849 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003850 PSA_ASSERT( psa_generator_read( &generator,
3851 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02003852 actual_capacity -= sizeof( output );
3853 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003854 PSA_ASSERT( psa_generator_read( &generator,
3855 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003856 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
3857 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskinebf491972018-10-25 22:36:12 +02003858
Gilles Peskine59685592018-09-18 12:11:34 +02003859exit:
3860 psa_generator_abort( &generator );
3861 psa_destroy_key( our_key );
3862 mbedtls_psa_crypto_free( );
3863}
3864/* END_CASE */
3865
3866/* BEGIN_CASE */
3867void key_agreement_output( int alg_arg,
3868 int our_key_type_arg, data_t *our_key_data,
3869 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003870 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02003871{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003872 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003873 psa_algorithm_t alg = alg_arg;
3874 psa_key_type_t our_key_type = our_key_type_arg;
3875 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003876 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003877 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02003878
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003879 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
3880 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003881
Gilles Peskine8817f612018-12-18 00:18:46 +01003882 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003883
Gilles Peskine8817f612018-12-18 00:18:46 +01003884 PSA_ASSERT( psa_allocate_key( our_key_type,
3885 KEY_BITS_FROM_DATA( our_key_type,
3886 our_key_data ),
3887 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003888 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003889 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3890 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3891 our_key_data->x,
3892 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003893
Gilles Peskine8817f612018-12-18 00:18:46 +01003894 PSA_ASSERT( psa_key_agreement( &generator,
3895 our_key,
3896 peer_key_data->x, peer_key_data->len,
3897 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003898
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003899 PSA_ASSERT( psa_generator_read( &generator,
3900 actual_output,
3901 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003902 ASSERT_COMPARE( actual_output, expected_output1->len,
3903 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003904 if( expected_output2->len != 0 )
3905 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003906 PSA_ASSERT( psa_generator_read( &generator,
3907 actual_output,
3908 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003909 ASSERT_COMPARE( actual_output, expected_output2->len,
3910 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003911 }
Gilles Peskine59685592018-09-18 12:11:34 +02003912
3913exit:
3914 psa_generator_abort( &generator );
3915 psa_destroy_key( our_key );
3916 mbedtls_psa_crypto_free( );
3917 mbedtls_free( actual_output );
3918}
3919/* END_CASE */
3920
3921/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003922void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003923{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003924 size_t bytes = bytes_arg;
3925 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003926 unsigned char *output = NULL;
3927 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003928 size_t i;
3929 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003930
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003931 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3932 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003933 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003934
Gilles Peskine8817f612018-12-18 00:18:46 +01003935 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003936
Gilles Peskinea50d7392018-06-21 10:22:13 +02003937 /* Run several times, to ensure that every output byte will be
3938 * nonzero at least once with overwhelming probability
3939 * (2^(-8*number_of_runs)). */
3940 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003941 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003942 if( bytes != 0 )
3943 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01003944 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003945
3946 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003947 ASSERT_COMPARE( output + bytes, sizeof( trail ),
3948 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003949
3950 for( i = 0; i < bytes; i++ )
3951 {
3952 if( output[i] != 0 )
3953 ++changed[i];
3954 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003955 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003956
3957 /* Check that every byte was changed to nonzero at least once. This
3958 * validates that psa_generate_random is overwriting every byte of
3959 * the output buffer. */
3960 for( i = 0; i < bytes; i++ )
3961 {
3962 TEST_ASSERT( changed[i] != 0 );
3963 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003964
3965exit:
3966 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003967 mbedtls_free( output );
3968 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003969}
3970/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003971
3972/* BEGIN_CASE */
3973void generate_key( int type_arg,
3974 int bits_arg,
3975 int usage_arg,
3976 int alg_arg,
3977 int expected_status_arg )
3978{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003979 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003980 psa_key_type_t type = type_arg;
3981 psa_key_usage_t usage = usage_arg;
3982 size_t bits = bits_arg;
3983 psa_algorithm_t alg = alg_arg;
3984 psa_status_t expected_status = expected_status_arg;
3985 psa_key_type_t got_type;
3986 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003987 psa_status_t expected_info_status =
3988 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003989 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003990
Gilles Peskine8817f612018-12-18 00:18:46 +01003991 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003992
Gilles Peskine8817f612018-12-18 00:18:46 +01003993 PSA_ASSERT( psa_allocate_key( type, bits, &handle ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003994 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003995 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003996
3997 /* Generate a key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003998 TEST_EQUAL( psa_generate_key( handle, type, bits, NULL, 0 ),
3999 expected_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004000
4001 /* Test the key information */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004002 TEST_EQUAL( psa_get_key_information( handle, &got_type, &got_bits ),
4003 expected_info_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004004 if( expected_info_status != PSA_SUCCESS )
4005 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004006 TEST_EQUAL( got_type, type );
4007 TEST_EQUAL( got_bits, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004008
Gilles Peskine818ca122018-06-20 18:16:48 +02004009 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004010 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004011 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004012
4013exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004014 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004015 mbedtls_psa_crypto_free( );
4016}
4017/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004018
Darryl Greend49a4992018-06-18 17:27:26 +01004019/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
4020void persistent_key_load_key_from_storage( data_t *data, int type_arg,
4021 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00004022 int alg_arg, int generation_method,
4023 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01004024{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004025 psa_key_handle_t handle = 0;
4026 psa_key_handle_t base_key;
Darryl Greend49a4992018-06-18 17:27:26 +01004027 psa_key_type_t type = (psa_key_type_t) type_arg;
4028 psa_key_type_t type_get;
4029 size_t bits_get;
Jaeden Amero70261c52019-01-04 11:47:20 +00004030 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
4031 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004032 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
4033 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00004034 psa_key_policy_t base_policy_set = PSA_KEY_POLICY_INIT;
Darryl Green0c6575a2018-11-07 16:05:30 +00004035 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
4036 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004037 unsigned char *first_export = NULL;
4038 unsigned char *second_export = NULL;
4039 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4040 size_t first_exported_length;
4041 size_t second_exported_length;
4042
4043 ASSERT_ALLOC( first_export, export_size );
4044 ASSERT_ALLOC( second_export, export_size );
4045
Gilles Peskine8817f612018-12-18 00:18:46 +01004046 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004047
Gilles Peskine8817f612018-12-18 00:18:46 +01004048 PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4049 type, bits,
4050 &handle ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004051 psa_key_policy_set_usage( &policy_set, policy_usage,
4052 policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004053 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004054
Darryl Green0c6575a2018-11-07 16:05:30 +00004055 switch( generation_method )
4056 {
4057 case IMPORT_KEY:
4058 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004059 PSA_ASSERT( psa_import_key( handle, type,
4060 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004061 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004062
Darryl Green0c6575a2018-11-07 16:05:30 +00004063 case GENERATE_KEY:
4064 /* Generate a key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004065 PSA_ASSERT( psa_generate_key( handle, type, bits,
4066 NULL, 0 ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004067 break;
4068
4069 case DERIVE_KEY:
4070 /* Create base key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004071 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
4072 PSA_BYTES_TO_BITS( data->len ),
4073 &base_key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004074 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
4075 base_policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004076 PSA_ASSERT( psa_set_key_policy(
4077 base_key, &base_policy_set ) );
4078 PSA_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
4079 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004080 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004081 PSA_ASSERT( psa_key_derivation( &generator, base_key,
4082 base_policy_alg,
4083 NULL, 0, NULL, 0,
4084 export_size ) );
4085 PSA_ASSERT( psa_generator_import_key(
4086 handle, PSA_KEY_TYPE_RAW_DATA,
4087 bits, &generator ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004088 break;
4089 }
Darryl Greend49a4992018-06-18 17:27:26 +01004090
4091 /* Export the key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004092 TEST_EQUAL( psa_export_key( handle,
4093 first_export, export_size,
4094 &first_exported_length ),
4095 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004096
4097 /* Shutdown and restart */
4098 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004099 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004100
Darryl Greend49a4992018-06-18 17:27:26 +01004101 /* Check key slot still contains key data */
Gilles Peskine8817f612018-12-18 00:18:46 +01004102 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4103 &handle ) );
4104 PSA_ASSERT( psa_get_key_information(
4105 handle, &type_get, &bits_get ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004106 TEST_EQUAL( type_get, type );
4107 TEST_EQUAL( bits_get, (size_t) bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004108
Gilles Peskine8817f612018-12-18 00:18:46 +01004109 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004110 TEST_EQUAL( psa_key_policy_get_usage( &policy_get ), policy_usage );
4111 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_get ), policy_alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004112
4113 /* Export the key again */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004114 TEST_EQUAL( psa_export_key( handle,
4115 second_export, export_size,
4116 &second_exported_length ),
4117 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004118
Darryl Green0c6575a2018-11-07 16:05:30 +00004119 if( export_status == PSA_SUCCESS )
4120 {
4121 ASSERT_COMPARE( first_export, first_exported_length,
4122 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01004123
Darryl Green0c6575a2018-11-07 16:05:30 +00004124 switch( generation_method )
4125 {
4126 case IMPORT_KEY:
4127 ASSERT_COMPARE( data->x, data->len,
4128 first_export, first_exported_length );
4129 break;
4130 default:
4131 break;
4132 }
4133 }
4134
4135 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004136 if( ! exercise_key( handle, policy_usage, policy_alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004137 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004138
4139exit:
4140 mbedtls_free( first_export );
4141 mbedtls_free( second_export );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004142 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004143 mbedtls_psa_crypto_free();
4144}
4145/* END_CASE */