blob: 4d6cefb0e54e105cc24daa56f1ca84ce8a32b979 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
4#if defined(MBEDTLS_PSA_CRYPTO_SPM)
5#include "spm/psa_defs.h"
6#endif
7
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02009#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +020010#include "mbedtls/oid.h"
11
Gilles Peskinee59236f2018-01-27 23:32:46 +010012#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030013
Jaeden Amerof24c7f82018-06-27 17:20:43 +010014/** An invalid export length that will never be set by psa_export_key(). */
15static const size_t INVALID_EXPORT_LENGTH = ~0U;
16
Gilles Peskinea7aa4422018-08-14 15:17:54 +020017/** Test if a buffer contains a constant byte value.
18 *
19 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020020 *
21 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020022 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020023 * \param size Size of the buffer in bytes.
24 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020025 * \return 1 if the buffer is all-bits-zero.
26 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020027 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020028static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020029{
30 size_t i;
31 for( i = 0; i < size; i++ )
32 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020033 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020034 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020035 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020036 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037}
Gilles Peskine818ca122018-06-20 18:16:48 +020038
Gilles Peskine0b352bc2018-06-28 00:16:11 +020039/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
40static int asn1_write_10x( unsigned char **p,
41 unsigned char *start,
42 size_t bits,
43 unsigned char x )
44{
45 int ret;
46 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020047 if( bits == 0 )
48 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
49 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020050 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030051 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020052 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
53 *p -= len;
54 ( *p )[len-1] = x;
55 if( bits % 8 == 0 )
56 ( *p )[1] |= 1;
57 else
58 ( *p )[0] |= 1 << ( bits % 8 );
59 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
60 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
61 MBEDTLS_ASN1_INTEGER ) );
62 return( len );
63}
64
65static int construct_fake_rsa_key( unsigned char *buffer,
66 size_t buffer_size,
67 unsigned char **p,
68 size_t bits,
69 int keypair )
70{
71 size_t half_bits = ( bits + 1 ) / 2;
72 int ret;
73 int len = 0;
74 /* Construct something that looks like a DER encoding of
75 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
76 * RSAPrivateKey ::= SEQUENCE {
77 * version Version,
78 * modulus INTEGER, -- n
79 * publicExponent INTEGER, -- e
80 * privateExponent INTEGER, -- d
81 * prime1 INTEGER, -- p
82 * prime2 INTEGER, -- q
83 * exponent1 INTEGER, -- d mod (p-1)
84 * exponent2 INTEGER, -- d mod (q-1)
85 * coefficient INTEGER, -- (inverse of q) mod p
86 * otherPrimeInfos OtherPrimeInfos OPTIONAL
87 * }
88 * Or, for a public key, the same structure with only
89 * version, modulus and publicExponent.
90 */
91 *p = buffer + buffer_size;
92 if( keypair )
93 {
94 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
95 asn1_write_10x( p, buffer, half_bits, 1 ) );
96 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
97 asn1_write_10x( p, buffer, half_bits, 1 ) );
98 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
99 asn1_write_10x( p, buffer, half_bits, 1 ) );
100 MBEDTLS_ASN1_CHK_ADD( len, /* q */
101 asn1_write_10x( p, buffer, half_bits, 1 ) );
102 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
103 asn1_write_10x( p, buffer, half_bits, 3 ) );
104 MBEDTLS_ASN1_CHK_ADD( len, /* d */
105 asn1_write_10x( p, buffer, bits, 1 ) );
106 }
107 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
108 asn1_write_10x( p, buffer, 17, 1 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* n */
110 asn1_write_10x( p, buffer, bits, 1 ) );
111 if( keypair )
112 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
113 mbedtls_asn1_write_int( p, buffer, 0 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
115 {
116 const unsigned char tag =
117 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
118 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
119 }
120 return( len );
121}
122
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100123static int exercise_mac_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200124 psa_key_usage_t usage,
125 psa_algorithm_t alg )
126{
Jaeden Amero769ce272019-01-04 11:48:03 +0000127 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200128 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200129 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200130 size_t mac_length = sizeof( mac );
131
132 if( usage & PSA_KEY_USAGE_SIGN )
133 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100134 PSA_ASSERT( psa_mac_sign_setup( &operation,
135 handle, alg ) );
136 PSA_ASSERT( psa_mac_update( &operation,
137 input, sizeof( input ) ) );
138 PSA_ASSERT( psa_mac_sign_finish( &operation,
139 mac, sizeof( mac ),
140 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200141 }
142
143 if( usage & PSA_KEY_USAGE_VERIFY )
144 {
145 psa_status_t verify_status =
146 ( usage & PSA_KEY_USAGE_SIGN ?
147 PSA_SUCCESS :
148 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +0100149 PSA_ASSERT( psa_mac_verify_setup( &operation,
150 handle, alg ) );
151 PSA_ASSERT( psa_mac_update( &operation,
152 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100153 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
154 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200155 }
156
157 return( 1 );
158
159exit:
160 psa_mac_abort( &operation );
161 return( 0 );
162}
163
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100164static int exercise_cipher_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200165 psa_key_usage_t usage,
166 psa_algorithm_t alg )
167{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000168 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200169 unsigned char iv[16] = {0};
170 size_t iv_length = sizeof( iv );
171 const unsigned char plaintext[16] = "Hello, world...";
172 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
173 size_t ciphertext_length = sizeof( ciphertext );
174 unsigned char decrypted[sizeof( ciphertext )];
175 size_t part_length;
176
177 if( usage & PSA_KEY_USAGE_ENCRYPT )
178 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100179 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
180 handle, alg ) );
181 PSA_ASSERT( psa_cipher_generate_iv( &operation,
182 iv, sizeof( iv ),
183 &iv_length ) );
184 PSA_ASSERT( psa_cipher_update( &operation,
185 plaintext, sizeof( plaintext ),
186 ciphertext, sizeof( ciphertext ),
187 &ciphertext_length ) );
188 PSA_ASSERT( psa_cipher_finish( &operation,
189 ciphertext + ciphertext_length,
190 sizeof( ciphertext ) - ciphertext_length,
191 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200192 ciphertext_length += part_length;
193 }
194
195 if( usage & PSA_KEY_USAGE_DECRYPT )
196 {
197 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700198 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200199 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
200 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200201 size_t bits;
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100202 TEST_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200203 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
204 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100205 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
206 handle, alg ) );
207 PSA_ASSERT( psa_cipher_set_iv( &operation,
208 iv, iv_length ) );
209 PSA_ASSERT( psa_cipher_update( &operation,
210 ciphertext, ciphertext_length,
211 decrypted, sizeof( decrypted ),
212 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200213 status = psa_cipher_finish( &operation,
214 decrypted + part_length,
215 sizeof( decrypted ) - part_length,
216 &part_length );
217 /* For a stream cipher, all inputs are valid. For a block cipher,
218 * if the input is some aribtrary data rather than an actual
219 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700220 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700221 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100222 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200223 else
224 TEST_ASSERT( status == PSA_SUCCESS ||
225 status == PSA_ERROR_INVALID_PADDING );
226 }
227
228 return( 1 );
229
230exit:
231 psa_cipher_abort( &operation );
232 return( 0 );
233}
234
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100235static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200236 psa_key_usage_t usage,
237 psa_algorithm_t alg )
238{
239 unsigned char nonce[16] = {0};
240 size_t nonce_length = sizeof( nonce );
241 unsigned char plaintext[16] = "Hello, world...";
242 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
243 size_t ciphertext_length = sizeof( ciphertext );
244 size_t plaintext_length = sizeof( ciphertext );
245
246 if( usage & PSA_KEY_USAGE_ENCRYPT )
247 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100248 PSA_ASSERT( psa_aead_encrypt( handle, alg,
249 nonce, nonce_length,
250 NULL, 0,
251 plaintext, sizeof( plaintext ),
252 ciphertext, sizeof( ciphertext ),
253 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200254 }
255
256 if( usage & PSA_KEY_USAGE_DECRYPT )
257 {
258 psa_status_t verify_status =
259 ( usage & PSA_KEY_USAGE_ENCRYPT ?
260 PSA_SUCCESS :
261 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100262 TEST_EQUAL( psa_aead_decrypt( handle, alg,
263 nonce, nonce_length,
264 NULL, 0,
265 ciphertext, ciphertext_length,
266 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100267 &plaintext_length ),
268 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200269 }
270
271 return( 1 );
272
273exit:
274 return( 0 );
275}
276
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100277static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200278 psa_key_usage_t usage,
279 psa_algorithm_t alg )
280{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200281 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
282 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200283 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200284 size_t signature_length = sizeof( signature );
285
286 if( usage & PSA_KEY_USAGE_SIGN )
287 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200288 /* Some algorithms require the payload to have the size of
289 * the hash encoded in the algorithm. Use this input size
290 * even for algorithms that allow other input sizes. */
291 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
292 if( hash_alg != 0 )
293 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100294 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
295 payload, payload_length,
296 signature, sizeof( signature ),
297 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200298 }
299
300 if( usage & PSA_KEY_USAGE_VERIFY )
301 {
302 psa_status_t verify_status =
303 ( usage & PSA_KEY_USAGE_SIGN ?
304 PSA_SUCCESS :
305 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100306 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
307 payload, payload_length,
308 signature, signature_length ),
309 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200310 }
311
312 return( 1 );
313
314exit:
315 return( 0 );
316}
317
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100318static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200319 psa_key_usage_t usage,
320 psa_algorithm_t alg )
321{
322 unsigned char plaintext[256] = "Hello, world...";
323 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
324 size_t ciphertext_length = sizeof( ciphertext );
325 size_t plaintext_length = 16;
326
327 if( usage & PSA_KEY_USAGE_ENCRYPT )
328 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100329 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
330 plaintext, plaintext_length,
331 NULL, 0,
332 ciphertext, sizeof( ciphertext ),
333 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200334 }
335
336 if( usage & PSA_KEY_USAGE_DECRYPT )
337 {
338 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100339 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200340 ciphertext, ciphertext_length,
341 NULL, 0,
342 plaintext, sizeof( plaintext ),
343 &plaintext_length );
344 TEST_ASSERT( status == PSA_SUCCESS ||
345 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
346 ( status == PSA_ERROR_INVALID_ARGUMENT ||
347 status == PSA_ERROR_INVALID_PADDING ) ) );
348 }
349
350 return( 1 );
351
352exit:
353 return( 0 );
354}
Gilles Peskine02b75072018-07-01 22:31:34 +0200355
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100356static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200357 psa_key_usage_t usage,
358 psa_algorithm_t alg )
359{
360 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
361 unsigned char label[16] = "This is a label.";
362 size_t label_length = sizeof( label );
363 unsigned char seed[16] = "abcdefghijklmnop";
364 size_t seed_length = sizeof( seed );
365 unsigned char output[1];
366
367 if( usage & PSA_KEY_USAGE_DERIVE )
368 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100369 PSA_ASSERT( psa_key_derivation( &generator,
370 handle, alg,
371 label, label_length,
372 seed, seed_length,
373 sizeof( output ) ) );
374 PSA_ASSERT( psa_generator_read( &generator,
375 output,
376 sizeof( output ) ) );
377 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200378 }
379
380 return( 1 );
381
382exit:
383 return( 0 );
384}
385
Gilles Peskinec7998b72018-11-07 18:45:02 +0100386/* We need two keys to exercise key agreement. Exercise the
387 * private key against its own public key. */
388static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator,
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100389 psa_key_handle_t handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100390 psa_algorithm_t alg )
391{
392 psa_key_type_t private_key_type;
393 psa_key_type_t public_key_type;
394 size_t key_bits;
395 uint8_t *public_key = NULL;
396 size_t public_key_length;
397 /* Return UNKNOWN_ERROR if something other than the final call to
398 * psa_key_agreement fails. This isn't fully satisfactory, but it's
399 * good enough: callers will report it as a failed test anyway. */
400 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
401
Gilles Peskine8817f612018-12-18 00:18:46 +0100402 PSA_ASSERT( psa_get_key_information( handle,
403 &private_key_type,
404 &key_bits ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100405 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
406 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
407 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100408 PSA_ASSERT( psa_export_public_key( handle,
409 public_key, public_key_length,
410 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100411
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100412 status = psa_key_agreement( generator, handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100413 public_key, public_key_length,
414 alg );
415exit:
416 mbedtls_free( public_key );
417 return( status );
418}
419
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100420static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200421 psa_key_usage_t usage,
422 psa_algorithm_t alg )
423{
424 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200425 unsigned char output[1];
426 int ok = 0;
427
428 if( usage & PSA_KEY_USAGE_DERIVE )
429 {
430 /* We need two keys to exercise key agreement. Exercise the
431 * private key against its own public key. */
Gilles Peskine8817f612018-12-18 00:18:46 +0100432 PSA_ASSERT( key_agreement_with_self( &generator, handle, alg ) );
433 PSA_ASSERT( psa_generator_read( &generator,
434 output,
435 sizeof( output ) ) );
436 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200437 }
438 ok = 1;
439
440exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200441 return( ok );
442}
443
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200444static int is_oid_of_key_type( psa_key_type_t type,
445 const uint8_t *oid, size_t oid_length )
446{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200447 const uint8_t *expected_oid = NULL;
448 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200449#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200450 if( PSA_KEY_TYPE_IS_RSA( type ) )
451 {
452 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
453 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
454 }
455 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200456#endif /* MBEDTLS_RSA_C */
457#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200458 if( PSA_KEY_TYPE_IS_ECC( type ) )
459 {
460 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
461 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
462 }
463 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200464#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200465 {
466 char message[40];
467 mbedtls_snprintf( message, sizeof( message ),
468 "OID not known for key type=0x%08lx",
469 (unsigned long) type );
470 test_fail( message, __LINE__, __FILE__ );
471 return( 0 );
472 }
473
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200474 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200475 return( 1 );
476
477exit:
478 return( 0 );
479}
480
481static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
482 size_t min_bits, size_t max_bits,
483 int must_be_odd )
484{
485 size_t len;
486 size_t actual_bits;
487 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100488 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100489 MBEDTLS_ASN1_INTEGER ),
490 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200491 /* Tolerate a slight departure from DER encoding:
492 * - 0 may be represented by an empty string or a 1-byte string.
493 * - The sign bit may be used as a value bit. */
494 if( ( len == 1 && ( *p )[0] == 0 ) ||
495 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
496 {
497 ++( *p );
498 --len;
499 }
500 if( min_bits == 0 && len == 0 )
501 return( 1 );
502 msb = ( *p )[0];
503 TEST_ASSERT( msb != 0 );
504 actual_bits = 8 * ( len - 1 );
505 while( msb != 0 )
506 {
507 msb >>= 1;
508 ++actual_bits;
509 }
510 TEST_ASSERT( actual_bits >= min_bits );
511 TEST_ASSERT( actual_bits <= max_bits );
512 if( must_be_odd )
513 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
514 *p += len;
515 return( 1 );
516exit:
517 return( 0 );
518}
519
520static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
521 size_t *len,
522 unsigned char n, unsigned char tag )
523{
524 int ret;
525 ret = mbedtls_asn1_get_tag( p, end, len,
526 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
527 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
528 if( ret != 0 )
529 return( ret );
530 end = *p + *len;
531 ret = mbedtls_asn1_get_tag( p, end, len, tag );
532 if( ret != 0 )
533 return( ret );
534 if( *p + *len != end )
535 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
536 return( 0 );
537}
538
539static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
540 uint8_t *exported, size_t exported_length )
541{
542 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100543 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200544 else
545 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200546
547#if defined(MBEDTLS_DES_C)
548 if( type == PSA_KEY_TYPE_DES )
549 {
550 /* Check the parity bits. */
551 unsigned i;
552 for( i = 0; i < bits / 8; i++ )
553 {
554 unsigned bit_count = 0;
555 unsigned m;
556 for( m = 1; m <= 0x100; m <<= 1 )
557 {
558 if( exported[i] & m )
559 ++bit_count;
560 }
561 TEST_ASSERT( bit_count % 2 != 0 );
562 }
563 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200564 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200565#endif
566
567#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
568 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
569 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200570 uint8_t *p = exported;
571 uint8_t *end = exported + exported_length;
572 size_t len;
573 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200574 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200575 * modulus INTEGER, -- n
576 * publicExponent INTEGER, -- e
577 * privateExponent INTEGER, -- d
578 * prime1 INTEGER, -- p
579 * prime2 INTEGER, -- q
580 * exponent1 INTEGER, -- d mod (p-1)
581 * exponent2 INTEGER, -- d mod (q-1)
582 * coefficient INTEGER, -- (inverse of q) mod p
583 * }
584 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100585 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
586 MBEDTLS_ASN1_SEQUENCE |
587 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
588 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200589 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
590 goto exit;
591 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
592 goto exit;
593 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
594 goto exit;
595 /* Require d to be at least half the size of n. */
596 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
597 goto exit;
598 /* Require p and q to be at most half the size of n, rounded up. */
599 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
600 goto exit;
601 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
602 goto exit;
603 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
604 goto exit;
605 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
606 goto exit;
607 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
608 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100609 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100610 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200611 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200612#endif /* MBEDTLS_RSA_C */
613
614#if defined(MBEDTLS_ECP_C)
615 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
616 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100617 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100618 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100619 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200620 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200621#endif /* MBEDTLS_ECP_C */
622
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200623 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
624 {
625 uint8_t *p = exported;
626 uint8_t *end = exported + exported_length;
627 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200628#if defined(MBEDTLS_RSA_C)
629 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
630 {
631 /* RSAPublicKey ::= SEQUENCE {
632 * modulus INTEGER, -- n
633 * publicExponent INTEGER } -- e
634 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100635 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
636 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100637 MBEDTLS_ASN1_CONSTRUCTED ),
638 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100639 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200640 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
641 goto exit;
642 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
643 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100644 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200645 }
646 else
647#endif /* MBEDTLS_RSA_C */
648#if defined(MBEDTLS_ECP_C)
649 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
650 {
Jaeden Amero6b196002019-01-10 10:23:21 +0000651 mbedtls_asn1_buf alg;
652 mbedtls_asn1_buf params;
653 mbedtls_asn1_bitstring bitstring;
654 /* SubjectPublicKeyInfo ::= SEQUENCE {
655 * algorithm AlgorithmIdentifier,
656 * subjectPublicKey BIT STRING }
657 * AlgorithmIdentifier ::= SEQUENCE {
658 * algorithm OBJECT IDENTIFIER,
659 * parameters ANY DEFINED BY algorithm OPTIONAL }
660 */
661
662 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
663 MBEDTLS_ASN1_SEQUENCE |
664 MBEDTLS_ASN1_CONSTRUCTED ),
665 0 );
666 TEST_EQUAL( p + len, end );
667 TEST_EQUAL( mbedtls_asn1_get_alg( &p, end, &alg, &params ), 0 );
668 if( ! is_oid_of_key_type( type, alg.p, alg.len ) )
669 goto exit;
670 TEST_EQUAL( mbedtls_asn1_get_bitstring( &p, end, &bitstring ), 0 );
671 TEST_EQUAL( p, end );
672 p = bitstring.p;
673
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200674 /* ECPoint ::= ...
Gilles Peskinec6290c02018-08-13 17:24:59 +0200675 * -- first 8 bits: 0x04 (uncompressed representation);
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200676 * -- then x_P as an n-bit string, big endian;
677 * -- then y_P as a n-bit string, big endian,
678 * -- where n is the order of the curve.
679 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100680 TEST_EQUAL( bitstring.unused_bits, 0 );
681 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
682 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200683 }
684 else
685#endif /* MBEDTLS_ECP_C */
686 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100687 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200688 mbedtls_snprintf( message, sizeof( message ),
689 "No sanity check for public key type=0x%08lx",
690 (unsigned long) type );
691 test_fail( message, __LINE__, __FILE__ );
692 return( 0 );
693 }
694 }
695 else
696
697 {
698 /* No sanity checks for other types */
699 }
700
701 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200702
703exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200704 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200705}
706
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100707static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200708 psa_key_usage_t usage )
709{
710 psa_key_type_t type;
711 size_t bits;
712 uint8_t *exported = NULL;
713 size_t exported_size = 0;
714 size_t exported_length = 0;
715 int ok = 0;
716
Gilles Peskine8817f612018-12-18 00:18:46 +0100717 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200718
719 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
720 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200721 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100722 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
723 PSA_ERROR_NOT_PERMITTED );
Gilles Peskined14664a2018-08-10 19:07:32 +0200724 return( 1 );
725 }
726
Gilles Peskined14664a2018-08-10 19:07:32 +0200727 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200728 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200729
Gilles Peskine8817f612018-12-18 00:18:46 +0100730 PSA_ASSERT( psa_export_key( handle,
731 exported, exported_size,
732 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200733 ok = exported_key_sanity_check( type, bits, exported, exported_length );
734
735exit:
736 mbedtls_free( exported );
737 return( ok );
738}
739
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100740static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200741{
742 psa_key_type_t type;
743 psa_key_type_t public_type;
744 size_t bits;
745 uint8_t *exported = NULL;
746 size_t exported_size = 0;
747 size_t exported_length = 0;
748 int ok = 0;
749
Gilles Peskine8817f612018-12-18 00:18:46 +0100750 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200751 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
752 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100753 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100754 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200755 return( 1 );
756 }
757
758 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
759 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200760 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200761
Gilles Peskine8817f612018-12-18 00:18:46 +0100762 PSA_ASSERT( psa_export_public_key( handle,
763 exported, exported_size,
764 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200765 ok = exported_key_sanity_check( public_type, bits,
766 exported, exported_length );
767
768exit:
769 mbedtls_free( exported );
770 return( ok );
771}
772
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100773static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +0200774 psa_key_usage_t usage,
775 psa_algorithm_t alg )
776{
777 int ok;
778 if( alg == 0 )
779 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
780 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100781 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200782 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100783 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200784 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100785 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200786 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100787 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200788 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100789 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200790 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100791 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200792 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100793 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200794 else
795 {
796 char message[40];
797 mbedtls_snprintf( message, sizeof( message ),
798 "No code to exercise alg=0x%08lx",
799 (unsigned long) alg );
800 test_fail( message, __LINE__, __FILE__ );
801 ok = 0;
802 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200803
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100804 ok = ok && exercise_export_key( handle, usage );
805 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +0200806
Gilles Peskine02b75072018-07-01 22:31:34 +0200807 return( ok );
808}
809
Gilles Peskine10df3412018-10-25 22:35:43 +0200810static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
811 psa_algorithm_t alg )
812{
813 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
814 {
815 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
816 PSA_KEY_USAGE_VERIFY :
817 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
818 }
819 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
820 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
821 {
822 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
823 PSA_KEY_USAGE_ENCRYPT :
824 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
825 }
826 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
827 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
828 {
829 return( PSA_KEY_USAGE_DERIVE );
830 }
831 else
832 {
833 return( 0 );
834 }
835
836}
Darryl Green0c6575a2018-11-07 16:05:30 +0000837
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100838/* An overapproximation of the amount of storage needed for a key of the
839 * given type and with the given content. The API doesn't make it easy
840 * to find a good value for the size. The current implementation doesn't
841 * care about the value anyway. */
842#define KEY_BITS_FROM_DATA( type, data ) \
843 ( data )->len
844
Darryl Green0c6575a2018-11-07 16:05:30 +0000845typedef enum {
846 IMPORT_KEY = 0,
847 GENERATE_KEY = 1,
848 DERIVE_KEY = 2
849} generate_method;
850
Gilles Peskinee59236f2018-01-27 23:32:46 +0100851/* END_HEADER */
852
853/* BEGIN_DEPENDENCIES
854 * depends_on:MBEDTLS_PSA_CRYPTO_C
855 * END_DEPENDENCIES
856 */
857
858/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200859void static_checks( )
860{
861 size_t max_truncated_mac_size =
862 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
863
864 /* Check that the length for a truncated MAC always fits in the algorithm
865 * encoding. The shifted mask is the maximum truncated value. The
866 * untruncated algorithm may be one byte larger. */
867 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
868}
869/* END_CASE */
870
871/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200872void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100873{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100874 psa_key_handle_t handle = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200875 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100876 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100877
Gilles Peskine8817f612018-12-18 00:18:46 +0100878 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100879
Gilles Peskine8817f612018-12-18 00:18:46 +0100880 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
881 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100882 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100883 TEST_EQUAL( status, expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100884 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100885 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100886
887exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100888 mbedtls_psa_crypto_free( );
889}
890/* END_CASE */
891
892/* BEGIN_CASE */
Gilles Peskinea4261682018-12-03 11:34:01 +0100893void import_twice( int alg_arg, int usage_arg,
894 int type1_arg, data_t *data1,
895 int expected_import1_status_arg,
896 int type2_arg, data_t *data2,
897 int expected_import2_status_arg )
898{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100899 psa_key_handle_t handle = 0;
Gilles Peskinea4261682018-12-03 11:34:01 +0100900 psa_algorithm_t alg = alg_arg;
901 psa_key_usage_t usage = usage_arg;
902 psa_key_type_t type1 = type1_arg;
903 psa_status_t expected_import1_status = expected_import1_status_arg;
904 psa_key_type_t type2 = type2_arg;
905 psa_status_t expected_import2_status = expected_import2_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +0000906 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea4261682018-12-03 11:34:01 +0100907 psa_status_t status;
908
Gilles Peskine8817f612018-12-18 00:18:46 +0100909 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100910
Gilles Peskine8817f612018-12-18 00:18:46 +0100911 PSA_ASSERT( psa_allocate_key( type1,
912 MAX( KEY_BITS_FROM_DATA( type1, data1 ),
913 KEY_BITS_FROM_DATA( type2, data2 ) ),
914 &handle ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100915 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100916 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100917
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100918 status = psa_import_key( handle, type1, data1->x, data1->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100919 TEST_EQUAL( status, expected_import1_status );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100920 status = psa_import_key( handle, type2, data2->x, data2->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100921 TEST_EQUAL( status, expected_import2_status );
Gilles Peskinea4261682018-12-03 11:34:01 +0100922
923 if( expected_import1_status == PSA_SUCCESS ||
924 expected_import2_status == PSA_SUCCESS )
925 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100926 TEST_ASSERT( exercise_key( handle, usage, alg ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100927 }
928
929exit:
930 mbedtls_psa_crypto_free( );
931}
932/* END_CASE */
933
934/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200935void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
936{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100937 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200938 size_t bits = bits_arg;
939 psa_status_t expected_status = expected_status_arg;
940 psa_status_t status;
941 psa_key_type_t type =
942 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
943 size_t buffer_size = /* Slight overapproximations */
944 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200945 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200946 unsigned char *p;
947 int ret;
948 size_t length;
949
Gilles Peskine8817f612018-12-18 00:18:46 +0100950 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200951 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200952
953 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
954 bits, keypair ) ) >= 0 );
955 length = ret;
956
957 /* Try importing the key */
Gilles Peskine8817f612018-12-18 00:18:46 +0100958 PSA_ASSERT( psa_allocate_key( type, bits, &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100959 status = psa_import_key( handle, type, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100960 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200961 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100962 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200963
964exit:
965 mbedtls_free( buffer );
966 mbedtls_psa_crypto_free( );
967}
968/* END_CASE */
969
970/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300971void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300972 int type_arg,
973 int alg_arg,
974 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100975 int expected_bits,
976 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200977 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100978 int canonical_input )
979{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100980 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100981 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200982 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200983 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100984 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100985 unsigned char *exported = NULL;
986 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100987 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100988 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100989 size_t reexported_length;
990 psa_key_type_t got_type;
991 size_t got_bits;
Jaeden Amero70261c52019-01-04 11:47:20 +0000992 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100993
Moran Pekercb088e72018-07-17 17:36:59 +0300994 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200995 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100996 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200997 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100998 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100999
Gilles Peskine8817f612018-12-18 00:18:46 +01001000 PSA_ASSERT( psa_allocate_key( type, expected_bits, &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001001 psa_key_policy_set_usage( &policy, usage_arg, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001002 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001003
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001004 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1005 PSA_ERROR_EMPTY_SLOT );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001006
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001007 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001008 PSA_ASSERT( psa_import_key( handle, type,
1009 data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001010
1011 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001012 PSA_ASSERT( psa_get_key_information( handle,
1013 &got_type,
1014 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001015 TEST_EQUAL( got_type, type );
1016 TEST_EQUAL( got_bits, (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001017
1018 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001019 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001020 exported, export_size,
1021 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001022 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001023
1024 /* The exported length must be set by psa_export_key() to a value between 0
1025 * and export_size. On errors, the exported length must be 0. */
1026 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1027 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1028 TEST_ASSERT( exported_length <= export_size );
1029
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001030 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001031 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001032 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001033 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001034 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001035 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001036 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001037
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001038 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001039 goto exit;
1040
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001041 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001042 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001043 else
1044 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001045 psa_key_handle_t handle2;
Gilles Peskine8817f612018-12-18 00:18:46 +01001046 PSA_ASSERT( psa_allocate_key( type, expected_bits, &handle2 ) );
1047 PSA_ASSERT( psa_set_key_policy( handle2, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001048
Gilles Peskine8817f612018-12-18 00:18:46 +01001049 PSA_ASSERT( psa_import_key( handle2, type,
1050 exported,
1051 exported_length ) );
1052 PSA_ASSERT( psa_export_key( handle2,
1053 reexported,
1054 export_size,
1055 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001056 ASSERT_COMPARE( exported, exported_length,
1057 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001058 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001059 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001060 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001061
1062destroy:
1063 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001064 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001065 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1066 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001067
1068exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001069 mbedtls_free( exported );
1070 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001071 mbedtls_psa_crypto_free( );
1072}
1073/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001074
Moran Pekerf709f4a2018-06-06 17:26:04 +03001075/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001076void import_key_nonempty_slot( )
1077{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001078 psa_key_handle_t handle = 0;
Moran Peker28a38e62018-11-07 16:18:24 +02001079 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1080 psa_status_t status;
1081 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
Gilles Peskine8817f612018-12-18 00:18:46 +01001082 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001083
Gilles Peskine8817f612018-12-18 00:18:46 +01001084 PSA_ASSERT( psa_allocate_key( type, PSA_BYTES_TO_BITS( sizeof( data ) ),
1085 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001086
Moran Peker28a38e62018-11-07 16:18:24 +02001087 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001088 PSA_ASSERT( psa_import_key( handle, type,
1089 data, sizeof( data ) ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001090
1091 /* Import the key again */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001092 status = psa_import_key( handle, type, data, sizeof( data ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001093 TEST_EQUAL( status, PSA_ERROR_OCCUPIED_SLOT );
Moran Peker28a38e62018-11-07 16:18:24 +02001094
1095exit:
1096 mbedtls_psa_crypto_free( );
1097}
1098/* END_CASE */
1099
1100/* BEGIN_CASE */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001101void export_invalid_handle( int handle, int expected_export_status_arg )
Moran Peker28a38e62018-11-07 16:18:24 +02001102{
1103 psa_status_t status;
1104 unsigned char *exported = NULL;
1105 size_t export_size = 0;
1106 size_t exported_length = INVALID_EXPORT_LENGTH;
1107 psa_status_t expected_export_status = expected_export_status_arg;
1108
Gilles Peskine8817f612018-12-18 00:18:46 +01001109 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001110
1111 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001112 status = psa_export_key( (psa_key_handle_t) handle,
Moran Peker28a38e62018-11-07 16:18:24 +02001113 exported, export_size,
1114 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001115 TEST_EQUAL( status, expected_export_status );
Moran Peker28a38e62018-11-07 16:18:24 +02001116
1117exit:
1118 mbedtls_psa_crypto_free( );
1119}
1120/* END_CASE */
1121
1122/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001123void export_with_no_key_activity( )
1124{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001125 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001126 psa_algorithm_t alg = PSA_ALG_CTR;
1127 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001128 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001129 unsigned char *exported = NULL;
1130 size_t export_size = 0;
1131 size_t exported_length = INVALID_EXPORT_LENGTH;
1132
Gilles Peskine8817f612018-12-18 00:18:46 +01001133 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001134
Gilles Peskine8817f612018-12-18 00:18:46 +01001135 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1136 &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001137 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001138 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001139
1140 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001141 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001142 exported, export_size,
1143 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001144 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001145
1146exit:
1147 mbedtls_psa_crypto_free( );
1148}
1149/* END_CASE */
1150
1151/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001152void cipher_with_no_key_activity( )
1153{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001154 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001155 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001156 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001157 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001158 int exercise_alg = PSA_ALG_CTR;
1159
Gilles Peskine8817f612018-12-18 00:18:46 +01001160 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001161
Gilles Peskine8817f612018-12-18 00:18:46 +01001162 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1163 &handle ) );
Moran Pekerce500072018-11-07 16:20:07 +02001164 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001165 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerce500072018-11-07 16:20:07 +02001166
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001167 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001168 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001169
1170exit:
1171 psa_cipher_abort( &operation );
1172 mbedtls_psa_crypto_free( );
1173}
1174/* END_CASE */
1175
1176/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001177void export_after_import_failure( data_t *data, int type_arg,
1178 int expected_import_status_arg )
1179{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001180 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001181 psa_key_type_t type = type_arg;
1182 psa_status_t status;
1183 unsigned char *exported = NULL;
1184 size_t export_size = 0;
1185 psa_status_t expected_import_status = expected_import_status_arg;
1186 size_t exported_length = INVALID_EXPORT_LENGTH;
1187
Gilles Peskine8817f612018-12-18 00:18:46 +01001188 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001189
Gilles Peskine8817f612018-12-18 00:18:46 +01001190 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1191 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001192
Moran Peker34550092018-11-07 16:19:34 +02001193 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001194 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001195 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001196 TEST_EQUAL( status, expected_import_status );
Moran Peker34550092018-11-07 16:19:34 +02001197
1198 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001199 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001200 exported, export_size,
1201 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001202 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001203
1204exit:
1205 mbedtls_psa_crypto_free( );
1206}
1207/* END_CASE */
1208
1209/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001210void cipher_after_import_failure( data_t *data, int type_arg,
1211 int expected_import_status_arg )
1212{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001213 psa_key_handle_t handle = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001214 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001215 psa_key_type_t type = type_arg;
1216 psa_status_t status;
1217 psa_status_t expected_import_status = expected_import_status_arg;
1218 int exercise_alg = PSA_ALG_CTR;
1219
Gilles Peskine8817f612018-12-18 00:18:46 +01001220 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001221
Gilles Peskine8817f612018-12-18 00:18:46 +01001222 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1223 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001224
Moran Pekerce500072018-11-07 16:20:07 +02001225 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001226 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001227 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001228 TEST_EQUAL( status, expected_import_status );
Moran Pekerce500072018-11-07 16:20:07 +02001229
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001230 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001231 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001232
1233exit:
1234 psa_cipher_abort( &operation );
1235 mbedtls_psa_crypto_free( );
1236}
1237/* END_CASE */
1238
1239/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001240void export_after_destroy_key( data_t *data, int type_arg )
1241{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001242 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001243 psa_key_type_t type = type_arg;
1244 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001245 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001246 psa_algorithm_t alg = PSA_ALG_CTR;
1247 unsigned char *exported = NULL;
1248 size_t export_size = 0;
1249 size_t exported_length = INVALID_EXPORT_LENGTH;
1250
Gilles Peskine8817f612018-12-18 00:18:46 +01001251 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001252
Gilles Peskine8817f612018-12-18 00:18:46 +01001253 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1254 &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001255 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001256 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001257 export_size = (ptrdiff_t) data->len;
1258 ASSERT_ALLOC( exported, export_size );
1259
1260 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001261 PSA_ASSERT( psa_import_key( handle, type,
1262 data->x, data->len ) );
Moran Peker34550092018-11-07 16:19:34 +02001263
Gilles Peskine8817f612018-12-18 00:18:46 +01001264 PSA_ASSERT( psa_export_key( handle, exported, export_size,
1265 &exported_length ) );
Moran Peker34550092018-11-07 16:19:34 +02001266
1267 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001268 PSA_ASSERT( psa_destroy_key( handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001269
1270 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001271 status = psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001272 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001273 TEST_EQUAL( status, PSA_ERROR_INVALID_HANDLE );
Moran Peker34550092018-11-07 16:19:34 +02001274
1275exit:
1276 mbedtls_free( exported );
1277 mbedtls_psa_crypto_free( );
1278}
1279/* END_CASE */
1280
1281/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001282void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001283 int type_arg,
1284 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001285 int export_size_delta,
1286 int expected_export_status_arg,
1287 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001288{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001289 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001290 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001291 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001292 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001293 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001294 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001295 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001296 size_t exported_length = INVALID_EXPORT_LENGTH;
Jaeden Amero70261c52019-01-04 11:47:20 +00001297 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001298
Gilles Peskine8817f612018-12-18 00:18:46 +01001299 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001300
Gilles Peskine8817f612018-12-18 00:18:46 +01001301 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1302 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001303 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001304 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001305
1306 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001307 PSA_ASSERT( psa_import_key( handle, type,
1308 data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001309
Gilles Peskine49c25912018-10-29 15:15:31 +01001310 /* Export the public key */
1311 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001312 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001313 exported, export_size,
1314 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001315 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001316 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001317 {
1318 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1319 size_t bits;
Gilles Peskine8817f612018-12-18 00:18:46 +01001320 PSA_ASSERT( psa_get_key_information( handle, NULL, &bits ) );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001321 TEST_ASSERT( expected_public_key->len <=
1322 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001323 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1324 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001325 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001326
1327exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001328 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001329 psa_destroy_key( handle );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001330 mbedtls_psa_crypto_free( );
1331}
1332/* END_CASE */
1333
Gilles Peskine20035e32018-02-03 22:44:14 +01001334/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001335void import_and_exercise_key( data_t *data,
1336 int type_arg,
1337 int bits_arg,
1338 int alg_arg )
1339{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001340 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001341 psa_key_type_t type = type_arg;
1342 size_t bits = bits_arg;
1343 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001344 psa_key_usage_t usage = usage_to_exercise( type, alg );
Jaeden Amero70261c52019-01-04 11:47:20 +00001345 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001346 psa_key_type_t got_type;
1347 size_t got_bits;
1348 psa_status_t status;
1349
Gilles Peskine8817f612018-12-18 00:18:46 +01001350 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001351
Gilles Peskine8817f612018-12-18 00:18:46 +01001352 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1353 &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001354 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001355 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001356
1357 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001358 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01001359 PSA_ASSERT( status );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001360
1361 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001362 PSA_ASSERT( psa_get_key_information( handle,
1363 &got_type,
1364 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001365 TEST_EQUAL( got_type, type );
1366 TEST_EQUAL( got_bits, bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001367
1368 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001369 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001370 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001371
1372exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001373 psa_destroy_key( handle );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001374 mbedtls_psa_crypto_free( );
1375}
1376/* END_CASE */
1377
1378/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001379void key_policy( int usage_arg, int alg_arg )
1380{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001381 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001382 psa_algorithm_t alg = alg_arg;
1383 psa_key_usage_t usage = usage_arg;
1384 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1385 unsigned char key[32] = {0};
Jaeden Amero70261c52019-01-04 11:47:20 +00001386 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
1387 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001388
1389 memset( key, 0x2a, sizeof( key ) );
1390
Gilles Peskine8817f612018-12-18 00:18:46 +01001391 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001392
Gilles Peskine8817f612018-12-18 00:18:46 +01001393 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( sizeof( key ) ),
1394 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001395 psa_key_policy_set_usage( &policy_set, usage, alg );
1396
Gilles Peskinefe11b722018-12-18 00:24:04 +01001397 TEST_EQUAL( psa_key_policy_get_usage( &policy_set ), usage );
1398 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_set ), alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001399 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001400
Gilles Peskine8817f612018-12-18 00:18:46 +01001401 PSA_ASSERT( psa_import_key( handle, key_type,
1402 key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001403
Gilles Peskine8817f612018-12-18 00:18:46 +01001404 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001405
Gilles Peskinefe11b722018-12-18 00:24:04 +01001406 TEST_EQUAL( policy_get.usage, policy_set.usage );
1407 TEST_EQUAL( policy_get.alg, policy_set.alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001408
1409exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001410 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001411 mbedtls_psa_crypto_free( );
1412}
1413/* END_CASE */
1414
1415/* BEGIN_CASE */
Jaeden Amero70261c52019-01-04 11:47:20 +00001416void key_policy_init( )
1417{
1418 /* Test each valid way of initializing the object, except for `= {0}`, as
1419 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1420 * though it's OK by the C standard. We could test for this, but we'd need
1421 * to supress the Clang warning for the test. */
1422 psa_key_policy_t func = psa_key_policy_init( );
1423 psa_key_policy_t init = PSA_KEY_POLICY_INIT;
1424 psa_key_policy_t zero;
1425
1426 memset( &zero, 0, sizeof( zero ) );
1427
1428 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1429 * specification, we test that all valid ways of initializing the object
1430 * have the same bit pattern. This is a stronger requirement that may not
1431 * be valid on all platforms or PSA Crypto implementations, but implies the
1432 * weaker actual requirement is met: that a freshly initialized object, no
1433 * matter how it was initialized, acts the same as any other valid
1434 * initialization. */
1435 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1436 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1437}
1438/* END_CASE */
1439
1440/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001441void mac_key_policy( int policy_usage,
1442 int policy_alg,
1443 int key_type,
1444 data_t *key_data,
1445 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001446{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001447 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001448 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001449 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001450 psa_status_t status;
1451 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001452
Gilles Peskine8817f612018-12-18 00:18:46 +01001453 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001454
Gilles Peskine8817f612018-12-18 00:18:46 +01001455 PSA_ASSERT( psa_allocate_key( key_type,
1456 KEY_BITS_FROM_DATA( key_type, key_data ),
1457 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001458 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001459 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001460
Gilles Peskine8817f612018-12-18 00:18:46 +01001461 PSA_ASSERT( psa_import_key( handle, key_type,
1462 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001463
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001464 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001465 if( policy_alg == exercise_alg &&
1466 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001467 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001468 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001469 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001470 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001471
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001472 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001473 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001474 if( policy_alg == exercise_alg &&
1475 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001476 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001477 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001478 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001479
1480exit:
1481 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001482 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001483 mbedtls_psa_crypto_free( );
1484}
1485/* END_CASE */
1486
1487/* BEGIN_CASE */
1488void cipher_key_policy( int policy_usage,
1489 int policy_alg,
1490 int key_type,
1491 data_t *key_data,
1492 int exercise_alg )
1493{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001494 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001495 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001496 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001497 psa_status_t status;
1498
Gilles Peskine8817f612018-12-18 00:18:46 +01001499 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001500
Gilles Peskine8817f612018-12-18 00:18:46 +01001501 PSA_ASSERT( psa_allocate_key( key_type,
1502 KEY_BITS_FROM_DATA( key_type, key_data ),
1503 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001504 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001505 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001506
Gilles Peskine8817f612018-12-18 00:18:46 +01001507 PSA_ASSERT( psa_import_key( handle, key_type,
1508 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001509
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001510 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001511 if( policy_alg == exercise_alg &&
1512 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001513 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001514 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001515 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001516 psa_cipher_abort( &operation );
1517
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001518 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001519 if( policy_alg == exercise_alg &&
1520 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001521 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001522 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001523 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001524
1525exit:
1526 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001527 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001528 mbedtls_psa_crypto_free( );
1529}
1530/* END_CASE */
1531
1532/* BEGIN_CASE */
1533void aead_key_policy( int policy_usage,
1534 int policy_alg,
1535 int key_type,
1536 data_t *key_data,
1537 int nonce_length_arg,
1538 int tag_length_arg,
1539 int exercise_alg )
1540{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001541 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001542 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001543 psa_status_t status;
1544 unsigned char nonce[16] = {0};
1545 size_t nonce_length = nonce_length_arg;
1546 unsigned char tag[16];
1547 size_t tag_length = tag_length_arg;
1548 size_t output_length;
1549
1550 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1551 TEST_ASSERT( tag_length <= sizeof( tag ) );
1552
Gilles Peskine8817f612018-12-18 00:18:46 +01001553 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001554
Gilles Peskine8817f612018-12-18 00:18:46 +01001555 PSA_ASSERT( psa_allocate_key( key_type,
1556 KEY_BITS_FROM_DATA( key_type, key_data ),
1557 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001558 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001559 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001560
Gilles Peskine8817f612018-12-18 00:18:46 +01001561 PSA_ASSERT( psa_import_key( handle, key_type,
1562 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001563
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001564 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001565 nonce, nonce_length,
1566 NULL, 0,
1567 NULL, 0,
1568 tag, tag_length,
1569 &output_length );
1570 if( policy_alg == exercise_alg &&
1571 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001572 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001573 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001574 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001575
1576 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001577 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001578 nonce, nonce_length,
1579 NULL, 0,
1580 tag, tag_length,
1581 NULL, 0,
1582 &output_length );
1583 if( policy_alg == exercise_alg &&
1584 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001585 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001586 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001587 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001588
1589exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001590 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001591 mbedtls_psa_crypto_free( );
1592}
1593/* END_CASE */
1594
1595/* BEGIN_CASE */
1596void asymmetric_encryption_key_policy( int policy_usage,
1597 int policy_alg,
1598 int key_type,
1599 data_t *key_data,
1600 int exercise_alg )
1601{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001602 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001603 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001604 psa_status_t status;
1605 size_t key_bits;
1606 size_t buffer_length;
1607 unsigned char *buffer = NULL;
1608 size_t output_length;
1609
Gilles Peskine8817f612018-12-18 00:18:46 +01001610 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001611
Gilles Peskine8817f612018-12-18 00:18:46 +01001612 PSA_ASSERT( psa_allocate_key( key_type,
1613 KEY_BITS_FROM_DATA( key_type, key_data ),
1614 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001615 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001616 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001617
Gilles Peskine8817f612018-12-18 00:18:46 +01001618 PSA_ASSERT( psa_import_key( handle, key_type,
1619 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001620
Gilles Peskine8817f612018-12-18 00:18:46 +01001621 PSA_ASSERT( psa_get_key_information( handle,
1622 NULL,
1623 &key_bits ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001624 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1625 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001626 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001627
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001628 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001629 NULL, 0,
1630 NULL, 0,
1631 buffer, buffer_length,
1632 &output_length );
1633 if( policy_alg == exercise_alg &&
1634 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001635 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001636 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001637 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001638
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001639 if( buffer_length != 0 )
1640 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001641 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001642 buffer, buffer_length,
1643 NULL, 0,
1644 buffer, buffer_length,
1645 &output_length );
1646 if( policy_alg == exercise_alg &&
1647 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001648 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001649 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001650 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001651
1652exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001653 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001654 mbedtls_psa_crypto_free( );
1655 mbedtls_free( buffer );
1656}
1657/* END_CASE */
1658
1659/* BEGIN_CASE */
1660void asymmetric_signature_key_policy( int policy_usage,
1661 int policy_alg,
1662 int key_type,
1663 data_t *key_data,
1664 int exercise_alg )
1665{
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;
1669 unsigned char payload[16] = {1};
1670 size_t payload_length = sizeof( payload );
1671 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1672 size_t signature_length;
1673
Gilles Peskine8817f612018-12-18 00:18:46 +01001674 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001675
Gilles Peskine8817f612018-12-18 00:18:46 +01001676 PSA_ASSERT( psa_allocate_key( key_type,
1677 KEY_BITS_FROM_DATA( key_type, key_data ),
1678 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001679 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001680 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001681
Gilles Peskine8817f612018-12-18 00:18:46 +01001682 PSA_ASSERT( psa_import_key( handle, key_type,
1683 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001684
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001685 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001686 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001687 signature, sizeof( signature ),
1688 &signature_length );
1689 if( policy_alg == exercise_alg &&
1690 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001691 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001692 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001693 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001694
1695 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001696 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001697 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001698 signature, sizeof( signature ) );
1699 if( policy_alg == exercise_alg &&
1700 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001701 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001702 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001703 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001704
1705exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001706 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001707 mbedtls_psa_crypto_free( );
1708}
1709/* END_CASE */
1710
1711/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001712void derive_key_policy( int policy_usage,
1713 int policy_alg,
1714 int key_type,
1715 data_t *key_data,
1716 int exercise_alg )
1717{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001718 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001719 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001720 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1721 psa_status_t status;
1722
Gilles Peskine8817f612018-12-18 00:18:46 +01001723 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001724
Gilles Peskine8817f612018-12-18 00:18:46 +01001725 PSA_ASSERT( psa_allocate_key( key_type,
1726 KEY_BITS_FROM_DATA( key_type, key_data ),
1727 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001728 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001729 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001730
Gilles Peskine8817f612018-12-18 00:18:46 +01001731 PSA_ASSERT( psa_import_key( handle, key_type,
1732 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001733
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001734 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001735 exercise_alg,
1736 NULL, 0,
1737 NULL, 0,
1738 1 );
1739 if( policy_alg == exercise_alg &&
1740 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001741 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001742 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001743 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001744
1745exit:
1746 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001747 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001748 mbedtls_psa_crypto_free( );
1749}
1750/* END_CASE */
1751
1752/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001753void agreement_key_policy( int policy_usage,
1754 int policy_alg,
1755 int key_type_arg,
1756 data_t *key_data,
1757 int exercise_alg )
1758{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001759 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001760 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001761 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001762 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1763 psa_status_t status;
1764
Gilles Peskine8817f612018-12-18 00:18:46 +01001765 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001766
Gilles Peskine8817f612018-12-18 00:18:46 +01001767 PSA_ASSERT( psa_allocate_key( key_type,
1768 KEY_BITS_FROM_DATA( key_type, key_data ),
1769 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001770 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001771 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001772
Gilles Peskine8817f612018-12-18 00:18:46 +01001773 PSA_ASSERT( psa_import_key( handle, key_type,
1774 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001775
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001776 status = key_agreement_with_self( &generator, handle, exercise_alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001777
Gilles Peskine01d718c2018-09-18 12:01:02 +02001778 if( policy_alg == exercise_alg &&
1779 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001780 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001781 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001782 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001783
1784exit:
1785 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001786 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001787 mbedtls_psa_crypto_free( );
1788}
1789/* END_CASE */
1790
1791/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001792void hash_operation_init( )
1793{
1794 /* Test each valid way of initializing the object, except for `= {0}`, as
1795 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1796 * though it's OK by the C standard. We could test for this, but we'd need
1797 * to supress the Clang warning for the test. */
1798 psa_hash_operation_t func = psa_hash_operation_init( );
1799 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1800 psa_hash_operation_t zero;
1801
1802 memset( &zero, 0, sizeof( zero ) );
1803
1804 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1805 * specification, we test that all valid ways of initializing the object
1806 * have the same bit pattern. This is a stronger requirement that may not
1807 * be valid on all platforms or PSA Crypto implementations, but implies the
1808 * weaker actual requirement is met: that a freshly initialized object, no
1809 * matter how it was initialized, acts the same as any other valid
1810 * initialization. */
1811 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1812 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1813}
1814/* END_CASE */
1815
1816/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001817void hash_setup( int alg_arg,
1818 int expected_status_arg )
1819{
1820 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001821 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001822 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001823 psa_status_t status;
1824
Gilles Peskine8817f612018-12-18 00:18:46 +01001825 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001826
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001827 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001828 psa_hash_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001829 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001830
1831exit:
1832 mbedtls_psa_crypto_free( );
1833}
1834/* END_CASE */
1835
1836/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02001837void hash_bad_order( )
1838{
1839 unsigned char input[] = "";
1840 /* SHA-256 hash of an empty string */
1841 unsigned char hash[] = {
1842 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1843 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1844 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
1845 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001846 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001847
Gilles Peskine8817f612018-12-18 00:18:46 +01001848 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001849
1850 /* psa_hash_update without calling psa_hash_setup beforehand */
1851 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001852 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001853 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001854
1855 /* psa_hash_verify without calling psa_hash_setup beforehand */
1856 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001857 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001858 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001859
1860 /* psa_hash_finish without calling psa_hash_setup beforehand */
1861 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001862 TEST_EQUAL( psa_hash_finish( &operation,
1863 hash, sizeof( hash ), &hash_len ),
1864 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001865
1866exit:
1867 mbedtls_psa_crypto_free( );
1868}
1869/* END_CASE */
1870
itayzafrir27e69452018-11-01 14:26:34 +02001871/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1872void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001873{
1874 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001875 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1876 * appended to it */
1877 unsigned char hash[] = {
1878 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1879 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1880 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03001881 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001882 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001883
Gilles Peskine8817f612018-12-18 00:18:46 +01001884 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001885
itayzafrir27e69452018-11-01 14:26:34 +02001886 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001887 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001888 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001889 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001890
itayzafrir27e69452018-11-01 14:26:34 +02001891 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001892 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001893 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001894 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001895
itayzafrir27e69452018-11-01 14:26:34 +02001896 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001897 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001898 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001899 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001900
itayzafrirec93d302018-10-18 18:01:10 +03001901exit:
1902 mbedtls_psa_crypto_free( );
1903}
1904/* END_CASE */
1905
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001906/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1907void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001908{
1909 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001910 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03001911 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001912 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001913 size_t hash_len;
1914
Gilles Peskine8817f612018-12-18 00:18:46 +01001915 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001916
itayzafrir58028322018-10-25 10:22:01 +03001917 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001918 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001919 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001920 hash, expected_size - 1, &hash_len ),
1921 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001922
1923exit:
1924 mbedtls_psa_crypto_free( );
1925}
1926/* END_CASE */
1927
1928/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001929void mac_operation_init( )
1930{
1931 /* Test each valid way of initializing the object, except for `= {0}`, as
1932 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1933 * though it's OK by the C standard. We could test for this, but we'd need
1934 * to supress the Clang warning for the test. */
1935 psa_mac_operation_t func = psa_mac_operation_init( );
1936 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1937 psa_mac_operation_t zero;
1938
1939 memset( &zero, 0, sizeof( zero ) );
1940
1941 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1942 * specification, we test that all valid ways of initializing the object
1943 * have the same bit pattern. This is a stronger requirement that may not
1944 * be valid on all platforms or PSA Crypto implementations, but implies the
1945 * weaker actual requirement is met: that a freshly initialized object, no
1946 * matter how it was initialized, acts the same as any other valid
1947 * initialization. */
1948 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1949 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1950}
1951/* END_CASE */
1952
1953/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001954void mac_setup( int key_type_arg,
1955 data_t *key,
1956 int alg_arg,
1957 int expected_status_arg )
1958{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001959 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001960 psa_key_type_t key_type = key_type_arg;
1961 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001962 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001963 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00001964 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001965 psa_status_t status;
1966
Gilles Peskine8817f612018-12-18 00:18:46 +01001967 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001968
Gilles Peskine8817f612018-12-18 00:18:46 +01001969 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
1970 &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001971 psa_key_policy_set_usage( &policy,
1972 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1973 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001974 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001975
Gilles Peskine8817f612018-12-18 00:18:46 +01001976 PSA_ASSERT( psa_import_key( handle, key_type,
1977 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001978
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001979 status = psa_mac_sign_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001980 psa_mac_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001981 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001982
1983exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001984 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001985 mbedtls_psa_crypto_free( );
1986}
1987/* END_CASE */
1988
1989/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001990void mac_sign( int key_type_arg,
1991 data_t *key,
1992 int alg_arg,
1993 data_t *input,
1994 data_t *expected_mac )
1995{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001996 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001997 psa_key_type_t key_type = key_type_arg;
1998 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001999 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002000 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002001 /* Leave a little extra room in the output buffer. At the end of the
2002 * test, we'll check that the implementation didn't overwrite onto
2003 * this extra room. */
2004 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2005 size_t mac_buffer_size =
2006 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2007 size_t mac_length = 0;
2008
2009 memset( actual_mac, '+', sizeof( actual_mac ) );
2010 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2011 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2012
Gilles Peskine8817f612018-12-18 00:18:46 +01002013 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002014
Gilles Peskine8817f612018-12-18 00:18:46 +01002015 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2016 &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002017 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002018 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002019
Gilles Peskine8817f612018-12-18 00:18:46 +01002020 PSA_ASSERT( psa_import_key( handle, key_type,
2021 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002022
2023 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002024 PSA_ASSERT( psa_mac_sign_setup( &operation,
2025 handle, alg ) );
2026 PSA_ASSERT( psa_mac_update( &operation,
2027 input->x, input->len ) );
2028 PSA_ASSERT( psa_mac_sign_finish( &operation,
2029 actual_mac, mac_buffer_size,
2030 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002031
2032 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002033 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2034 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002035
2036 /* Verify that the end of the buffer is untouched. */
2037 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2038 sizeof( actual_mac ) - mac_length ) );
2039
2040exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002041 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002042 mbedtls_psa_crypto_free( );
2043}
2044/* END_CASE */
2045
2046/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002047void mac_verify( int key_type_arg,
2048 data_t *key,
2049 int alg_arg,
2050 data_t *input,
2051 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002052{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002053 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002054 psa_key_type_t key_type = key_type_arg;
2055 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002056 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002057 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002058
Gilles Peskine69c12672018-06-28 00:07:19 +02002059 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2060
Gilles Peskine8817f612018-12-18 00:18:46 +01002061 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002062
Gilles Peskine8817f612018-12-18 00:18:46 +01002063 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2064 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002065 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002066 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad16036df908f2018-04-02 08:34:15 -07002067
Gilles Peskine8817f612018-12-18 00:18:46 +01002068 PSA_ASSERT( psa_import_key( handle, key_type,
2069 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002070
Gilles Peskine8817f612018-12-18 00:18:46 +01002071 PSA_ASSERT( psa_mac_verify_setup( &operation,
2072 handle, alg ) );
2073 PSA_ASSERT( psa_destroy_key( handle ) );
2074 PSA_ASSERT( psa_mac_update( &operation,
2075 input->x, input->len ) );
2076 PSA_ASSERT( psa_mac_verify_finish( &operation,
2077 expected_mac->x,
2078 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002079
2080exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002081 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002082 mbedtls_psa_crypto_free( );
2083}
2084/* END_CASE */
2085
2086/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002087void cipher_operation_init( )
2088{
2089 /* Test each valid way of initializing the object, except for `= {0}`, as
2090 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2091 * though it's OK by the C standard. We could test for this, but we'd need
2092 * to supress the Clang warning for the test. */
2093 psa_cipher_operation_t func = psa_cipher_operation_init( );
2094 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2095 psa_cipher_operation_t zero;
2096
2097 memset( &zero, 0, sizeof( zero ) );
2098
2099 /* Although not technically guaranteed by the C standard nor the PSA Crypto
2100 * specification, we test that all valid ways of initializing the object
2101 * have the same bit pattern. This is a stronger requirement that may not
2102 * be valid on all platforms or PSA Crypto implementations, but implies the
2103 * weaker actual requirement is met: that a freshly initialized object, no
2104 * matter how it was initialized, acts the same as any other valid
2105 * initialization. */
2106 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
2107 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
2108}
2109/* END_CASE */
2110
2111/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002112void cipher_setup( int key_type_arg,
2113 data_t *key,
2114 int alg_arg,
2115 int expected_status_arg )
2116{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002117 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002118 psa_key_type_t key_type = key_type_arg;
2119 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002120 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002121 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002122 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002123 psa_status_t status;
2124
Gilles Peskine8817f612018-12-18 00:18:46 +01002125 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002126
Gilles Peskine8817f612018-12-18 00:18:46 +01002127 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2128 &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002129 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002130 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002131
Gilles Peskine8817f612018-12-18 00:18:46 +01002132 PSA_ASSERT( psa_import_key( handle, key_type,
2133 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002134
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002135 status = psa_cipher_encrypt_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002136 psa_cipher_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002137 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002138
2139exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002140 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002141 mbedtls_psa_crypto_free( );
2142}
2143/* END_CASE */
2144
2145/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002146void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002147 data_t *key,
2148 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002149 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002150{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002151 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002152 psa_status_t status;
2153 psa_key_type_t key_type = key_type_arg;
2154 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002155 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002156 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002157 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002158 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002159 size_t output_buffer_size = 0;
2160 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002161 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002162 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002163 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002164
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002165 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2166 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002167
Gilles Peskine8817f612018-12-18 00:18:46 +01002168 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002169
Gilles Peskine8817f612018-12-18 00:18:46 +01002170 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2171 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002172 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002173 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002174
Gilles Peskine8817f612018-12-18 00:18:46 +01002175 PSA_ASSERT( psa_import_key( handle, key_type,
2176 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002177
Gilles Peskine8817f612018-12-18 00:18:46 +01002178 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2179 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002180
Gilles Peskine8817f612018-12-18 00:18:46 +01002181 PSA_ASSERT( psa_cipher_set_iv( &operation,
2182 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002183 output_buffer_size = ( (size_t) input->len +
2184 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002185 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002186
Gilles Peskine8817f612018-12-18 00:18:46 +01002187 PSA_ASSERT( psa_cipher_update( &operation,
2188 input->x, input->len,
2189 output, output_buffer_size,
2190 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002191 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002192 status = psa_cipher_finish( &operation,
2193 output + function_output_length,
2194 output_buffer_size,
2195 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002196 total_output_length += function_output_length;
2197
Gilles Peskinefe11b722018-12-18 00:24:04 +01002198 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002199 if( expected_status == PSA_SUCCESS )
2200 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002201 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002202 ASSERT_COMPARE( expected_output->x, expected_output->len,
2203 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002204 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002205
Gilles Peskine50e586b2018-06-08 14:28:46 +02002206exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002207 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002208 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002209 mbedtls_psa_crypto_free( );
2210}
2211/* END_CASE */
2212
2213/* BEGIN_CASE */
2214void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002215 data_t *key,
2216 data_t *input,
2217 int first_part_size,
2218 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002219{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002220 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002221 psa_key_type_t key_type = key_type_arg;
2222 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002223 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002224 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002225 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002226 size_t output_buffer_size = 0;
2227 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002228 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002229 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002230 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002231
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002232 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2233 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002234
Gilles Peskine8817f612018-12-18 00:18:46 +01002235 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002236
Gilles Peskine8817f612018-12-18 00:18:46 +01002237 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2238 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002239 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002240 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002241
Gilles Peskine8817f612018-12-18 00:18:46 +01002242 PSA_ASSERT( psa_import_key( handle, key_type,
2243 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002244
Gilles Peskine8817f612018-12-18 00:18:46 +01002245 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2246 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002247
Gilles Peskine8817f612018-12-18 00:18:46 +01002248 PSA_ASSERT( psa_cipher_set_iv( &operation,
2249 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002250 output_buffer_size = ( (size_t) input->len +
2251 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002252 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002253
Gilles Peskine4abf7412018-06-18 16:35:34 +02002254 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002255 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2256 output, output_buffer_size,
2257 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002258 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002259 PSA_ASSERT( psa_cipher_update( &operation,
2260 input->x + first_part_size,
2261 input->len - first_part_size,
2262 output, output_buffer_size,
2263 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002264 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002265 PSA_ASSERT( psa_cipher_finish( &operation,
2266 output + function_output_length,
2267 output_buffer_size,
2268 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002269 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002270 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002271
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002272 ASSERT_COMPARE( expected_output->x, expected_output->len,
2273 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002274
2275exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002276 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002277 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002278 mbedtls_psa_crypto_free( );
2279}
2280/* END_CASE */
2281
2282/* BEGIN_CASE */
2283void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002284 data_t *key,
2285 data_t *input,
2286 int first_part_size,
2287 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002288{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002289 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002290
2291 psa_key_type_t key_type = key_type_arg;
2292 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002293 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002294 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002295 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002296 size_t output_buffer_size = 0;
2297 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002298 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002299 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002300 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002301
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002302 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2303 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002304
Gilles Peskine8817f612018-12-18 00:18:46 +01002305 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002306
Gilles Peskine8817f612018-12-18 00:18:46 +01002307 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2308 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002309 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002310 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002311
Gilles Peskine8817f612018-12-18 00:18:46 +01002312 PSA_ASSERT( psa_import_key( handle, key_type,
2313 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002314
Gilles Peskine8817f612018-12-18 00:18:46 +01002315 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2316 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002317
Gilles Peskine8817f612018-12-18 00:18:46 +01002318 PSA_ASSERT( psa_cipher_set_iv( &operation,
2319 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002320
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002321 output_buffer_size = ( (size_t) input->len +
2322 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002323 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002324
Gilles Peskine4abf7412018-06-18 16:35:34 +02002325 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002326 PSA_ASSERT( psa_cipher_update( &operation,
2327 input->x, first_part_size,
2328 output, output_buffer_size,
2329 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002330 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002331 PSA_ASSERT( psa_cipher_update( &operation,
2332 input->x + first_part_size,
2333 input->len - first_part_size,
2334 output, output_buffer_size,
2335 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002336 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002337 PSA_ASSERT( psa_cipher_finish( &operation,
2338 output + function_output_length,
2339 output_buffer_size,
2340 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002341 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002342 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002343
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002344 ASSERT_COMPARE( expected_output->x, expected_output->len,
2345 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002346
2347exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002348 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002349 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002350 mbedtls_psa_crypto_free( );
2351}
2352/* END_CASE */
2353
Gilles Peskine50e586b2018-06-08 14:28:46 +02002354/* BEGIN_CASE */
2355void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002356 data_t *key,
2357 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002358 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002359{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002360 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002361 psa_status_t status;
2362 psa_key_type_t key_type = key_type_arg;
2363 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002364 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002365 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002366 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002367 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002368 size_t output_buffer_size = 0;
2369 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002370 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002371 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002372 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002373
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002374 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2375 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002376
Gilles Peskine8817f612018-12-18 00:18:46 +01002377 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002378
Gilles Peskine8817f612018-12-18 00:18:46 +01002379 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2380 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002381 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002382 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002383
Gilles Peskine8817f612018-12-18 00:18:46 +01002384 PSA_ASSERT( psa_import_key( handle, key_type,
2385 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002386
Gilles Peskine8817f612018-12-18 00:18:46 +01002387 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2388 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002389
Gilles Peskine8817f612018-12-18 00:18:46 +01002390 PSA_ASSERT( psa_cipher_set_iv( &operation,
2391 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002392
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002393 output_buffer_size = ( (size_t) input->len +
2394 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002395 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002396
Gilles Peskine8817f612018-12-18 00:18:46 +01002397 PSA_ASSERT( psa_cipher_update( &operation,
2398 input->x, input->len,
2399 output, output_buffer_size,
2400 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002401 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002402 status = psa_cipher_finish( &operation,
2403 output + function_output_length,
2404 output_buffer_size,
2405 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002406 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002407 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002408
2409 if( expected_status == PSA_SUCCESS )
2410 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002411 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002412 ASSERT_COMPARE( expected_output->x, expected_output->len,
2413 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002414 }
2415
Gilles Peskine50e586b2018-06-08 14:28:46 +02002416exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002417 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002418 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002419 mbedtls_psa_crypto_free( );
2420}
2421/* END_CASE */
2422
Gilles Peskine50e586b2018-06-08 14:28:46 +02002423/* BEGIN_CASE */
2424void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002425 data_t *key,
2426 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002427{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002428 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002429 psa_key_type_t key_type = key_type_arg;
2430 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002431 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002432 size_t iv_size = 16;
2433 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002434 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002435 size_t output1_size = 0;
2436 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002437 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002438 size_t output2_size = 0;
2439 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002440 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002441 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2442 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002443 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002444
Gilles Peskine8817f612018-12-18 00:18:46 +01002445 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002446
Gilles Peskine8817f612018-12-18 00:18:46 +01002447 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2448 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002449 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002450 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002451
Gilles Peskine8817f612018-12-18 00:18:46 +01002452 PSA_ASSERT( psa_import_key( handle, key_type,
2453 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002454
Gilles Peskine8817f612018-12-18 00:18:46 +01002455 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2456 handle, alg ) );
2457 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2458 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002459
Gilles Peskine8817f612018-12-18 00:18:46 +01002460 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2461 iv, iv_size,
2462 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002463 output1_size = ( (size_t) input->len +
2464 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002465 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002466
Gilles Peskine8817f612018-12-18 00:18:46 +01002467 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2468 output1, output1_size,
2469 &output1_length ) );
2470 PSA_ASSERT( psa_cipher_finish( &operation1,
2471 output1 + output1_length, output1_size,
2472 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002473
Gilles Peskine048b7f02018-06-08 14:20:49 +02002474 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002475
Gilles Peskine8817f612018-12-18 00:18:46 +01002476 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002477
2478 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002479 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002480
Gilles Peskine8817f612018-12-18 00:18:46 +01002481 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2482 iv, iv_length ) );
2483 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2484 output2, output2_size,
2485 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002486 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002487 PSA_ASSERT( psa_cipher_finish( &operation2,
2488 output2 + output2_length,
2489 output2_size,
2490 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002491
Gilles Peskine048b7f02018-06-08 14:20:49 +02002492 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002493
Gilles Peskine8817f612018-12-18 00:18:46 +01002494 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002495
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002496 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002497
2498exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002499 mbedtls_free( output1 );
2500 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002501 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03002502 mbedtls_psa_crypto_free( );
2503}
2504/* END_CASE */
2505
2506/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002507void cipher_verify_output_multipart( int alg_arg,
2508 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002509 data_t *key,
2510 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002511 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002512{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002513 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002514 psa_key_type_t key_type = key_type_arg;
2515 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002516 unsigned char iv[16] = {0};
2517 size_t iv_size = 16;
2518 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002519 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002520 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002521 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002522 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002523 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002524 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002525 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002526 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2527 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002528 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002529
Gilles Peskine8817f612018-12-18 00:18:46 +01002530 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002531
Gilles Peskine8817f612018-12-18 00:18:46 +01002532 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2533 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002534 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002535 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002536
Gilles Peskine8817f612018-12-18 00:18:46 +01002537 PSA_ASSERT( psa_import_key( handle, key_type,
2538 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03002539
Gilles Peskine8817f612018-12-18 00:18:46 +01002540 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2541 handle, alg ) );
2542 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2543 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002544
Gilles Peskine8817f612018-12-18 00:18:46 +01002545 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2546 iv, iv_size,
2547 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002548 output1_buffer_size = ( (size_t) input->len +
2549 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002550 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002551
Gilles Peskine4abf7412018-06-18 16:35:34 +02002552 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002553
Gilles Peskine8817f612018-12-18 00:18:46 +01002554 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2555 output1, output1_buffer_size,
2556 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002557 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002558
Gilles Peskine8817f612018-12-18 00:18:46 +01002559 PSA_ASSERT( psa_cipher_update( &operation1,
2560 input->x + first_part_size,
2561 input->len - first_part_size,
2562 output1, output1_buffer_size,
2563 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002564 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002565
Gilles Peskine8817f612018-12-18 00:18:46 +01002566 PSA_ASSERT( psa_cipher_finish( &operation1,
2567 output1 + output1_length,
2568 output1_buffer_size - output1_length,
2569 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002570 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002571
Gilles Peskine8817f612018-12-18 00:18:46 +01002572 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002573
Gilles Peskine048b7f02018-06-08 14:20:49 +02002574 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002575 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002576
Gilles Peskine8817f612018-12-18 00:18:46 +01002577 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2578 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002579
Gilles Peskine8817f612018-12-18 00:18:46 +01002580 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2581 output2, output2_buffer_size,
2582 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002583 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002584
Gilles Peskine8817f612018-12-18 00:18:46 +01002585 PSA_ASSERT( psa_cipher_update( &operation2,
2586 output1 + first_part_size,
2587 output1_length - first_part_size,
2588 output2, output2_buffer_size,
2589 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002590 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002591
Gilles Peskine8817f612018-12-18 00:18:46 +01002592 PSA_ASSERT( psa_cipher_finish( &operation2,
2593 output2 + output2_length,
2594 output2_buffer_size - output2_length,
2595 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002596 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002597
Gilles Peskine8817f612018-12-18 00:18:46 +01002598 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002599
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002600 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002601
2602exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002603 mbedtls_free( output1 );
2604 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002605 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002606 mbedtls_psa_crypto_free( );
2607}
2608/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002609
Gilles Peskine20035e32018-02-03 22:44:14 +01002610/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002611void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002612 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002613 data_t *nonce,
2614 data_t *additional_data,
2615 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002616 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002617{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002618 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002619 psa_key_type_t key_type = key_type_arg;
2620 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002621 unsigned char *output_data = NULL;
2622 size_t output_size = 0;
2623 size_t output_length = 0;
2624 unsigned char *output_data2 = NULL;
2625 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002626 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002627 psa_status_t expected_result = expected_result_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002628 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002629
Gilles Peskine4abf7412018-06-18 16:35:34 +02002630 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002631 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002632
Gilles Peskine8817f612018-12-18 00:18:46 +01002633 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002634
Gilles Peskine8817f612018-12-18 00:18:46 +01002635 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2636 &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002637 psa_key_policy_set_usage( &policy,
2638 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2639 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002640 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002641
Gilles Peskine8817f612018-12-18 00:18:46 +01002642 PSA_ASSERT( psa_import_key( handle, key_type,
2643 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002644
Gilles Peskinefe11b722018-12-18 00:24:04 +01002645 TEST_EQUAL( psa_aead_encrypt( handle, alg,
2646 nonce->x, nonce->len,
2647 additional_data->x,
2648 additional_data->len,
2649 input_data->x, input_data->len,
2650 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002651 &output_length ),
2652 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002653
2654 if( PSA_SUCCESS == expected_result )
2655 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002656 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002657
Gilles Peskinefe11b722018-12-18 00:24:04 +01002658 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2659 nonce->x, nonce->len,
2660 additional_data->x,
2661 additional_data->len,
2662 output_data, output_length,
2663 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002664 &output_length2 ),
2665 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002666
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002667 ASSERT_COMPARE( input_data->x, input_data->len,
2668 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002669 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002670
Gilles Peskinea1cac842018-06-11 19:33:02 +02002671exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002672 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002673 mbedtls_free( output_data );
2674 mbedtls_free( output_data2 );
2675 mbedtls_psa_crypto_free( );
2676}
2677/* END_CASE */
2678
2679/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002680void aead_encrypt( int key_type_arg, data_t *key_data,
2681 int alg_arg,
2682 data_t *nonce,
2683 data_t *additional_data,
2684 data_t *input_data,
2685 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002686{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002687 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002688 psa_key_type_t key_type = key_type_arg;
2689 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002690 unsigned char *output_data = NULL;
2691 size_t output_size = 0;
2692 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002693 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002694 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002695
Gilles Peskine4abf7412018-06-18 16:35:34 +02002696 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002697 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002698
Gilles Peskine8817f612018-12-18 00:18:46 +01002699 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002700
Gilles Peskine8817f612018-12-18 00:18:46 +01002701 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2702 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002703 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002704 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002705
Gilles Peskine8817f612018-12-18 00:18:46 +01002706 PSA_ASSERT( psa_import_key( handle, key_type,
2707 key_data->x,
2708 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002709
Gilles Peskine8817f612018-12-18 00:18:46 +01002710 PSA_ASSERT( psa_aead_encrypt( handle, alg,
2711 nonce->x, nonce->len,
2712 additional_data->x, additional_data->len,
2713 input_data->x, input_data->len,
2714 output_data, output_size,
2715 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002716
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002717 ASSERT_COMPARE( expected_result->x, expected_result->len,
2718 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002719
Gilles Peskinea1cac842018-06-11 19:33:02 +02002720exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002721 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002722 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002723 mbedtls_psa_crypto_free( );
2724}
2725/* END_CASE */
2726
2727/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002728void aead_decrypt( int key_type_arg, data_t *key_data,
2729 int alg_arg,
2730 data_t *nonce,
2731 data_t *additional_data,
2732 data_t *input_data,
2733 data_t *expected_data,
2734 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002735{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002736 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002737 psa_key_type_t key_type = key_type_arg;
2738 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002739 unsigned char *output_data = NULL;
2740 size_t output_size = 0;
2741 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002742 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002743 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002744 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002745
Gilles Peskine4abf7412018-06-18 16:35:34 +02002746 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002747 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002748
Gilles Peskine8817f612018-12-18 00:18:46 +01002749 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002750
Gilles Peskine8817f612018-12-18 00:18:46 +01002751 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2752 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002753 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002754 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002755
Gilles Peskine8817f612018-12-18 00:18:46 +01002756 PSA_ASSERT( psa_import_key( handle, key_type,
2757 key_data->x,
2758 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002759
Gilles Peskinefe11b722018-12-18 00:24:04 +01002760 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2761 nonce->x, nonce->len,
2762 additional_data->x,
2763 additional_data->len,
2764 input_data->x, input_data->len,
2765 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002766 &output_length ),
2767 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002768
Gilles Peskine2d277862018-06-18 15:41:12 +02002769 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002770 ASSERT_COMPARE( expected_data->x, expected_data->len,
2771 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002772
Gilles Peskinea1cac842018-06-11 19:33:02 +02002773exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002774 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002775 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002776 mbedtls_psa_crypto_free( );
2777}
2778/* END_CASE */
2779
2780/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002781void signature_size( int type_arg,
2782 int bits,
2783 int alg_arg,
2784 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002785{
2786 psa_key_type_t type = type_arg;
2787 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002788 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002789 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002790exit:
2791 ;
2792}
2793/* END_CASE */
2794
2795/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002796void sign_deterministic( int key_type_arg, data_t *key_data,
2797 int alg_arg, data_t *input_data,
2798 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002799{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002800 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01002801 psa_key_type_t key_type = key_type_arg;
2802 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002803 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002804 unsigned char *signature = NULL;
2805 size_t signature_size;
2806 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002807 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01002808
Gilles Peskine8817f612018-12-18 00:18:46 +01002809 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002810
Gilles Peskine8817f612018-12-18 00:18:46 +01002811 PSA_ASSERT( psa_allocate_key( key_type,
2812 KEY_BITS_FROM_DATA( key_type, key_data ),
2813 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002814 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002815 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002816
Gilles Peskine8817f612018-12-18 00:18:46 +01002817 PSA_ASSERT( psa_import_key( handle, key_type,
2818 key_data->x,
2819 key_data->len ) );
2820 PSA_ASSERT( psa_get_key_information( handle,
2821 NULL,
2822 &key_bits ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002823
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002824 /* Allocate a buffer which has the size advertized by the
2825 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002826 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2827 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002828 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002829 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002830 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002831
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002832 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002833 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2834 input_data->x, input_data->len,
2835 signature, signature_size,
2836 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002837 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002838 ASSERT_COMPARE( output_data->x, output_data->len,
2839 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002840
2841exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002842 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01002843 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002844 mbedtls_psa_crypto_free( );
2845}
2846/* END_CASE */
2847
2848/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002849void sign_fail( int key_type_arg, data_t *key_data,
2850 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002851 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002852{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002853 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01002854 psa_key_type_t key_type = key_type_arg;
2855 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002856 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002857 psa_status_t actual_status;
2858 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002859 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002860 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002861 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01002862
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002863 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002864
Gilles Peskine8817f612018-12-18 00:18:46 +01002865 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002866
Gilles Peskine8817f612018-12-18 00:18:46 +01002867 PSA_ASSERT( psa_allocate_key( key_type,
2868 KEY_BITS_FROM_DATA( key_type, key_data ),
2869 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002870 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002871 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002872
Gilles Peskine8817f612018-12-18 00:18:46 +01002873 PSA_ASSERT( psa_import_key( handle, key_type,
2874 key_data->x,
2875 key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002876
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002877 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002878 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002879 signature, signature_size,
2880 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002881 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002882 /* The value of *signature_length is unspecified on error, but
2883 * whatever it is, it should be less than signature_size, so that
2884 * if the caller tries to read *signature_length bytes without
2885 * checking the error code then they don't overflow a buffer. */
2886 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002887
2888exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002889 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01002890 mbedtls_free( signature );
2891 mbedtls_psa_crypto_free( );
2892}
2893/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002894
2895/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002896void sign_verify( int key_type_arg, data_t *key_data,
2897 int alg_arg, data_t *input_data )
2898{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002899 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02002900 psa_key_type_t key_type = key_type_arg;
2901 psa_algorithm_t alg = alg_arg;
2902 size_t key_bits;
2903 unsigned char *signature = NULL;
2904 size_t signature_size;
2905 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002906 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02002907
Gilles Peskine8817f612018-12-18 00:18:46 +01002908 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002909
Gilles Peskine8817f612018-12-18 00:18:46 +01002910 PSA_ASSERT( psa_allocate_key( key_type,
2911 KEY_BITS_FROM_DATA( key_type, key_data ),
2912 &handle ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002913 psa_key_policy_set_usage( &policy,
2914 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2915 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002916 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002917
Gilles Peskine8817f612018-12-18 00:18:46 +01002918 PSA_ASSERT( psa_import_key( handle, key_type,
2919 key_data->x,
2920 key_data->len ) );
2921 PSA_ASSERT( psa_get_key_information( handle,
2922 NULL,
2923 &key_bits ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002924
2925 /* Allocate a buffer which has the size advertized by the
2926 * library. */
2927 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2928 key_bits, alg );
2929 TEST_ASSERT( signature_size != 0 );
2930 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002931 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002932
2933 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002934 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2935 input_data->x, input_data->len,
2936 signature, signature_size,
2937 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002938 /* Check that the signature length looks sensible. */
2939 TEST_ASSERT( signature_length <= signature_size );
2940 TEST_ASSERT( signature_length > 0 );
2941
2942 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002943 PSA_ASSERT( psa_asymmetric_verify(
2944 handle, alg,
2945 input_data->x, input_data->len,
2946 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002947
2948 if( input_data->len != 0 )
2949 {
2950 /* Flip a bit in the input and verify that the signature is now
2951 * detected as invalid. Flip a bit at the beginning, not at the end,
2952 * because ECDSA may ignore the last few bits of the input. */
2953 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002954 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
2955 input_data->x, input_data->len,
2956 signature, signature_length ),
2957 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02002958 }
2959
2960exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002961 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02002962 mbedtls_free( signature );
2963 mbedtls_psa_crypto_free( );
2964}
2965/* END_CASE */
2966
2967/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002968void asymmetric_verify( int key_type_arg, data_t *key_data,
2969 int alg_arg, data_t *hash_data,
2970 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002971{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002972 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03002973 psa_key_type_t key_type = key_type_arg;
2974 psa_algorithm_t alg = alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002975 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03002976
Gilles Peskine69c12672018-06-28 00:07:19 +02002977 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2978
Gilles Peskine8817f612018-12-18 00:18:46 +01002979 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03002980
Gilles Peskine8817f612018-12-18 00:18:46 +01002981 PSA_ASSERT( psa_allocate_key( key_type,
2982 KEY_BITS_FROM_DATA( key_type, key_data ),
2983 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002984 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002985 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
itayzafrir5c753392018-05-08 11:18:38 +03002986
Gilles Peskine8817f612018-12-18 00:18:46 +01002987 PSA_ASSERT( psa_import_key( handle, key_type,
2988 key_data->x,
2989 key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002990
Gilles Peskine8817f612018-12-18 00:18:46 +01002991 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
2992 hash_data->x, hash_data->len,
2993 signature_data->x,
2994 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002995exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002996 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03002997 mbedtls_psa_crypto_free( );
2998}
2999/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003000
3001/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003002void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3003 int alg_arg, data_t *hash_data,
3004 data_t *signature_data,
3005 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003006{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003007 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003008 psa_key_type_t key_type = key_type_arg;
3009 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003010 psa_status_t actual_status;
3011 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003012 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003013
Gilles Peskine8817f612018-12-18 00:18:46 +01003014 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003015
Gilles Peskine8817f612018-12-18 00:18:46 +01003016 PSA_ASSERT( psa_allocate_key( key_type,
3017 KEY_BITS_FROM_DATA( key_type, key_data ),
3018 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003019 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003020 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003021
Gilles Peskine8817f612018-12-18 00:18:46 +01003022 PSA_ASSERT( psa_import_key( handle, key_type,
3023 key_data->x,
3024 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003025
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003026 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003027 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003028 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003029 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003030
Gilles Peskinefe11b722018-12-18 00:24:04 +01003031 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003032
3033exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003034 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003035 mbedtls_psa_crypto_free( );
3036}
3037/* END_CASE */
3038
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003039/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003040void asymmetric_encrypt( int key_type_arg,
3041 data_t *key_data,
3042 int alg_arg,
3043 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003044 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003045 int expected_output_length_arg,
3046 int expected_status_arg )
3047{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003048 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003049 psa_key_type_t key_type = key_type_arg;
3050 psa_algorithm_t alg = alg_arg;
3051 size_t expected_output_length = expected_output_length_arg;
3052 size_t key_bits;
3053 unsigned char *output = NULL;
3054 size_t output_size;
3055 size_t output_length = ~0;
3056 psa_status_t actual_status;
3057 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003058 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003059
Gilles Peskine8817f612018-12-18 00:18:46 +01003060 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003061
Gilles Peskine656896e2018-06-29 19:12:28 +02003062 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01003063 PSA_ASSERT( psa_allocate_key( key_type,
3064 KEY_BITS_FROM_DATA( key_type, key_data ),
3065 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003066 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003067 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
3068 PSA_ASSERT( psa_import_key( handle, key_type,
3069 key_data->x,
3070 key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003071
3072 /* Determine the maximum output length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003073 PSA_ASSERT( psa_get_key_information( handle,
3074 NULL,
3075 &key_bits ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003076 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003077 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003078
3079 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003080 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003081 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003082 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003083 output, output_size,
3084 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003085 TEST_EQUAL( actual_status, expected_status );
3086 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003087
Gilles Peskine68428122018-06-30 18:42:41 +02003088 /* If the label is empty, the test framework puts a non-null pointer
3089 * in label->x. Test that a null pointer works as well. */
3090 if( label->len == 0 )
3091 {
3092 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003093 if( output_size != 0 )
3094 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003095 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003096 input_data->x, input_data->len,
3097 NULL, label->len,
3098 output, output_size,
3099 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003100 TEST_EQUAL( actual_status, expected_status );
3101 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003102 }
3103
Gilles Peskine656896e2018-06-29 19:12:28 +02003104exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003105 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003106 mbedtls_free( output );
3107 mbedtls_psa_crypto_free( );
3108}
3109/* END_CASE */
3110
3111/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003112void asymmetric_encrypt_decrypt( int key_type_arg,
3113 data_t *key_data,
3114 int alg_arg,
3115 data_t *input_data,
3116 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003117{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003118 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003119 psa_key_type_t key_type = key_type_arg;
3120 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003121 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003122 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003123 size_t output_size;
3124 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003125 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003126 size_t output2_size;
3127 size_t output2_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003128 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003129
Gilles Peskine8817f612018-12-18 00:18:46 +01003130 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003131
Gilles Peskine8817f612018-12-18 00:18:46 +01003132 PSA_ASSERT( psa_allocate_key( key_type,
3133 KEY_BITS_FROM_DATA( key_type, key_data ),
3134 &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003135 psa_key_policy_set_usage( &policy,
3136 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003137 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003138 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003139
Gilles Peskine8817f612018-12-18 00:18:46 +01003140 PSA_ASSERT( psa_import_key( handle, key_type,
3141 key_data->x,
3142 key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003143
3144 /* Determine the maximum ciphertext length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003145 PSA_ASSERT( psa_get_key_information( handle,
3146 NULL,
3147 &key_bits ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003148 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003149 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003150 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003151 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003152
Gilles Peskineeebd7382018-06-08 18:11:54 +02003153 /* We test encryption by checking that encrypt-then-decrypt gives back
3154 * the original plaintext because of the non-optional random
3155 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003156 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3157 input_data->x, input_data->len,
3158 label->x, label->len,
3159 output, output_size,
3160 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003161 /* We don't know what ciphertext length to expect, but check that
3162 * it looks sensible. */
3163 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003164
Gilles Peskine8817f612018-12-18 00:18:46 +01003165 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3166 output, output_length,
3167 label->x, label->len,
3168 output2, output2_size,
3169 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003170 ASSERT_COMPARE( input_data->x, input_data->len,
3171 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003172
3173exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003174 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003175 mbedtls_free( output );
3176 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003177 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003178}
3179/* END_CASE */
3180
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003181/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003182void asymmetric_decrypt( int key_type_arg,
3183 data_t *key_data,
3184 int alg_arg,
3185 data_t *input_data,
3186 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003187 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003188{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003189 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003190 psa_key_type_t key_type = key_type_arg;
3191 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003192 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003193 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003194 size_t output_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003195 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003196
Gilles Peskine4abf7412018-06-18 16:35:34 +02003197 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003198 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003199
Gilles Peskine8817f612018-12-18 00:18:46 +01003200 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003201
Gilles Peskine8817f612018-12-18 00:18:46 +01003202 PSA_ASSERT( psa_allocate_key( key_type,
3203 KEY_BITS_FROM_DATA( key_type, key_data ),
3204 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003205 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003206 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003207
Gilles Peskine8817f612018-12-18 00:18:46 +01003208 PSA_ASSERT( psa_import_key( handle, key_type,
3209 key_data->x,
3210 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003211
Gilles Peskine8817f612018-12-18 00:18:46 +01003212 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3213 input_data->x, input_data->len,
3214 label->x, label->len,
3215 output,
3216 output_size,
3217 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003218 ASSERT_COMPARE( expected_data->x, expected_data->len,
3219 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003220
Gilles Peskine68428122018-06-30 18:42:41 +02003221 /* If the label is empty, the test framework puts a non-null pointer
3222 * in label->x. Test that a null pointer works as well. */
3223 if( label->len == 0 )
3224 {
3225 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003226 if( output_size != 0 )
3227 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003228 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3229 input_data->x, input_data->len,
3230 NULL, label->len,
3231 output,
3232 output_size,
3233 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003234 ASSERT_COMPARE( expected_data->x, expected_data->len,
3235 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003236 }
3237
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003238exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003239 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003240 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003241 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003242}
3243/* END_CASE */
3244
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003245/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003246void asymmetric_decrypt_fail( int key_type_arg,
3247 data_t *key_data,
3248 int alg_arg,
3249 data_t *input_data,
3250 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003251 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003252{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003253 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003254 psa_key_type_t key_type = key_type_arg;
3255 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003256 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003257 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003258 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003259 psa_status_t actual_status;
3260 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003261 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003262
Gilles Peskine4abf7412018-06-18 16:35:34 +02003263 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003264 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003265
Gilles Peskine8817f612018-12-18 00:18:46 +01003266 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003267
Gilles Peskine8817f612018-12-18 00:18:46 +01003268 PSA_ASSERT( psa_allocate_key( key_type,
3269 KEY_BITS_FROM_DATA( key_type, key_data ),
3270 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003271 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003272 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003273
Gilles Peskine8817f612018-12-18 00:18:46 +01003274 PSA_ASSERT( psa_import_key( handle, key_type,
3275 key_data->x,
3276 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003277
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003278 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003279 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003280 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003281 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003282 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003283 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003284 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003285
Gilles Peskine68428122018-06-30 18:42:41 +02003286 /* If the label is empty, the test framework puts a non-null pointer
3287 * in label->x. Test that a null pointer works as well. */
3288 if( label->len == 0 )
3289 {
3290 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003291 if( output_size != 0 )
3292 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003293 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003294 input_data->x, input_data->len,
3295 NULL, label->len,
3296 output, output_size,
3297 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003298 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003299 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003300 }
3301
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003302exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003303 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02003304 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003305 mbedtls_psa_crypto_free( );
3306}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003307/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003308
3309/* BEGIN_CASE */
Jaeden Amerod94d6712019-01-04 14:11:48 +00003310void crypto_generator_init( )
3311{
3312 /* Test each valid way of initializing the object, except for `= {0}`, as
3313 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3314 * though it's OK by the C standard. We could test for this, but we'd need
3315 * to supress the Clang warning for the test. */
3316 psa_crypto_generator_t func = psa_crypto_generator_init( );
3317 psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT;
3318 psa_crypto_generator_t zero;
3319
3320 memset( &zero, 0, sizeof( zero ) );
3321
3322 /* Although not technically guaranteed by the C standard nor the PSA Crypto
3323 * specification, we test that all valid ways of initializing the object
3324 * have the same bit pattern. This is a stronger requirement that may not
3325 * be valid on all platforms or PSA Crypto implementations, but implies the
3326 * weaker actual requirement is met: that a freshly initialized object, no
3327 * matter how it was initialized, acts the same as any other valid
3328 * initialization. */
3329 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
3330 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
3331}
3332/* END_CASE */
3333
3334/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003335void derive_setup( int key_type_arg,
3336 data_t *key_data,
3337 int alg_arg,
3338 data_t *salt,
3339 data_t *label,
3340 int requested_capacity_arg,
3341 int expected_status_arg )
3342{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003343 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003344 size_t key_type = key_type_arg;
3345 psa_algorithm_t alg = alg_arg;
3346 size_t requested_capacity = requested_capacity_arg;
3347 psa_status_t expected_status = expected_status_arg;
3348 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003349 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003350
Gilles Peskine8817f612018-12-18 00:18:46 +01003351 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003352
Gilles Peskine8817f612018-12-18 00:18:46 +01003353 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
3354 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003355 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003356 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003357
Gilles Peskine8817f612018-12-18 00:18:46 +01003358 PSA_ASSERT( psa_import_key( handle, key_type,
3359 key_data->x,
3360 key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003361
Gilles Peskinefe11b722018-12-18 00:24:04 +01003362 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3363 salt->x, salt->len,
3364 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003365 requested_capacity ),
3366 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003367
3368exit:
3369 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003370 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003371 mbedtls_psa_crypto_free( );
3372}
3373/* END_CASE */
3374
3375/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003376void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003377{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003378 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003379 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003380 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003381 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003382 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003383 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003384 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3385 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3386 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Jaeden Amero70261c52019-01-04 11:47:20 +00003387 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003388
Gilles Peskine8817f612018-12-18 00:18:46 +01003389 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003390
Gilles Peskine8817f612018-12-18 00:18:46 +01003391 PSA_ASSERT( psa_allocate_key( key_type,
3392 PSA_BYTES_TO_BITS( sizeof( key_data ) ),
3393 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003394 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003395 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003396
Gilles Peskine8817f612018-12-18 00:18:46 +01003397 PSA_ASSERT( psa_import_key( handle, key_type,
3398 key_data,
3399 sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003400
3401 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01003402 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3403 NULL, 0,
3404 NULL, 0,
3405 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003406
3407 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003408 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3409 NULL, 0,
3410 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003411 capacity ),
3412 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003413
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003414 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003415
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003416 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
3417 PSA_ERROR_INSUFFICIENT_CAPACITY );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003418
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003419exit:
3420 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003421 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003422 mbedtls_psa_crypto_free( );
3423}
3424/* END_CASE */
3425
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003426/* BEGIN_CASE */
3427void test_derive_invalid_generator_tests( )
3428{
3429 uint8_t output_buffer[16];
3430 size_t buffer_size = 16;
3431 size_t capacity = 0;
3432 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3433
Nir Sonnenschein50789302018-10-31 12:16:38 +02003434 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003435 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003436
3437 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003438 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003439
Gilles Peskine8817f612018-12-18 00:18:46 +01003440 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003441
Nir Sonnenschein50789302018-10-31 12:16:38 +02003442 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003443 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003444
Nir Sonnenschein50789302018-10-31 12:16:38 +02003445 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003446 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003447
3448exit:
3449 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003450}
3451/* END_CASE */
3452
3453/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003454void derive_output( int alg_arg,
3455 data_t *key_data,
3456 data_t *salt,
3457 data_t *label,
3458 int requested_capacity_arg,
3459 data_t *expected_output1,
3460 data_t *expected_output2 )
3461{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003462 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003463 psa_algorithm_t alg = alg_arg;
3464 size_t requested_capacity = requested_capacity_arg;
3465 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3466 uint8_t *expected_outputs[2] =
3467 {expected_output1->x, expected_output2->x};
3468 size_t output_sizes[2] =
3469 {expected_output1->len, expected_output2->len};
3470 size_t output_buffer_size = 0;
3471 uint8_t *output_buffer = NULL;
3472 size_t expected_capacity;
3473 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003474 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003475 psa_status_t status;
3476 unsigned i;
3477
3478 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3479 {
3480 if( output_sizes[i] > output_buffer_size )
3481 output_buffer_size = output_sizes[i];
3482 if( output_sizes[i] == 0 )
3483 expected_outputs[i] = NULL;
3484 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003485 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003486 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003487
Gilles Peskine8817f612018-12-18 00:18:46 +01003488 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3489 PSA_BYTES_TO_BITS( key_data->len ),
3490 &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003491 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003492 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003493
Gilles Peskine8817f612018-12-18 00:18:46 +01003494 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3495 key_data->x,
3496 key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003497
3498 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003499 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3500 salt->x, salt->len,
3501 label->x, label->len,
3502 requested_capacity ) );
3503 PSA_ASSERT( psa_get_generator_capacity( &generator,
3504 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003505 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003506 expected_capacity = requested_capacity;
3507
3508 /* Expansion phase. */
3509 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3510 {
3511 /* Read some bytes. */
3512 status = psa_generator_read( &generator,
3513 output_buffer, output_sizes[i] );
3514 if( expected_capacity == 0 && output_sizes[i] == 0 )
3515 {
3516 /* Reading 0 bytes when 0 bytes are available can go either way. */
3517 TEST_ASSERT( status == PSA_SUCCESS ||
3518 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3519 continue;
3520 }
3521 else if( expected_capacity == 0 ||
3522 output_sizes[i] > expected_capacity )
3523 {
3524 /* Capacity exceeded. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003525 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003526 expected_capacity = 0;
3527 continue;
3528 }
3529 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003530 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003531 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003532 ASSERT_COMPARE( output_buffer, output_sizes[i],
3533 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003534 /* Check the generator status. */
3535 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01003536 PSA_ASSERT( psa_get_generator_capacity( &generator,
3537 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003538 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003539 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003540 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003541
3542exit:
3543 mbedtls_free( output_buffer );
3544 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003545 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003546 mbedtls_psa_crypto_free( );
3547}
3548/* END_CASE */
3549
3550/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003551void derive_full( int alg_arg,
3552 data_t *key_data,
3553 data_t *salt,
3554 data_t *label,
3555 int requested_capacity_arg )
3556{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003557 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02003558 psa_algorithm_t alg = alg_arg;
3559 size_t requested_capacity = requested_capacity_arg;
3560 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3561 unsigned char output_buffer[16];
3562 size_t expected_capacity = requested_capacity;
3563 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003564 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02003565
Gilles Peskine8817f612018-12-18 00:18:46 +01003566 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003567
Gilles Peskine8817f612018-12-18 00:18:46 +01003568 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3569 PSA_BYTES_TO_BITS( key_data->len ),
3570 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003571 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003572 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003573
Gilles Peskine8817f612018-12-18 00:18:46 +01003574 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3575 key_data->x,
3576 key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003577
3578 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003579 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3580 salt->x, salt->len,
3581 label->x, label->len,
3582 requested_capacity ) );
3583 PSA_ASSERT( psa_get_generator_capacity( &generator,
3584 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003585 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003586
3587 /* Expansion phase. */
3588 while( current_capacity > 0 )
3589 {
3590 size_t read_size = sizeof( output_buffer );
3591 if( read_size > current_capacity )
3592 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01003593 PSA_ASSERT( psa_generator_read( &generator,
3594 output_buffer,
3595 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003596 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01003597 PSA_ASSERT( psa_get_generator_capacity( &generator,
3598 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003599 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003600 }
3601
3602 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003603 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
3604 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskined54931c2018-07-17 21:06:59 +02003605
Gilles Peskine8817f612018-12-18 00:18:46 +01003606 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003607
3608exit:
3609 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003610 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02003611 mbedtls_psa_crypto_free( );
3612}
3613/* END_CASE */
3614
3615/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003616void derive_key_exercise( int alg_arg,
3617 data_t *key_data,
3618 data_t *salt,
3619 data_t *label,
3620 int derived_type_arg,
3621 int derived_bits_arg,
3622 int derived_usage_arg,
3623 int derived_alg_arg )
3624{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003625 psa_key_handle_t base_handle = 0;
3626 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003627 psa_algorithm_t alg = alg_arg;
3628 psa_key_type_t derived_type = derived_type_arg;
3629 size_t derived_bits = derived_bits_arg;
3630 psa_key_usage_t derived_usage = derived_usage_arg;
3631 psa_algorithm_t derived_alg = derived_alg_arg;
3632 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3633 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003634 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003635 psa_key_type_t got_type;
3636 size_t got_bits;
3637
Gilles Peskine8817f612018-12-18 00:18:46 +01003638 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003639
Gilles Peskine8817f612018-12-18 00:18:46 +01003640 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3641 PSA_BYTES_TO_BITS( key_data->len ),
3642 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003643 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003644 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3645 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3646 key_data->x,
3647 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003648
3649 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003650 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3651 salt->x, salt->len,
3652 label->x, label->len,
3653 capacity ) );
3654 PSA_ASSERT( psa_allocate_key( derived_type, derived_bits,
3655 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003656 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003657 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3658 PSA_ASSERT( psa_generator_import_key( derived_handle,
3659 derived_type,
3660 derived_bits,
3661 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003662
3663 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01003664 PSA_ASSERT( psa_get_key_information( derived_handle,
3665 &got_type,
3666 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003667 TEST_EQUAL( got_type, derived_type );
3668 TEST_EQUAL( got_bits, derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003669
3670 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003671 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02003672 goto exit;
3673
3674exit:
3675 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003676 psa_destroy_key( base_handle );
3677 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003678 mbedtls_psa_crypto_free( );
3679}
3680/* END_CASE */
3681
3682/* BEGIN_CASE */
3683void derive_key_export( int alg_arg,
3684 data_t *key_data,
3685 data_t *salt,
3686 data_t *label,
3687 int bytes1_arg,
3688 int bytes2_arg )
3689{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003690 psa_key_handle_t base_handle = 0;
3691 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003692 psa_algorithm_t alg = alg_arg;
3693 size_t bytes1 = bytes1_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003694 size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003695 size_t bytes2 = bytes2_arg;
3696 size_t capacity = bytes1 + bytes2;
3697 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003698 uint8_t *output_buffer = NULL;
3699 uint8_t *export_buffer = NULL;
Jaeden Amero70261c52019-01-04 11:47:20 +00003700 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003701 size_t length;
3702
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003703 ASSERT_ALLOC( output_buffer, capacity );
3704 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01003705 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003706
Gilles Peskine8817f612018-12-18 00:18:46 +01003707 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3708 PSA_BYTES_TO_BITS( key_data->len ),
3709 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003710 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003711 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3712 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3713 key_data->x,
3714 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003715
3716 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003717 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3718 salt->x, salt->len,
3719 label->x, label->len,
3720 capacity ) );
3721 PSA_ASSERT( psa_generator_read( &generator,
3722 output_buffer,
3723 capacity ) );
3724 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003725
3726 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003727 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3728 salt->x, salt->len,
3729 label->x, label->len,
3730 capacity ) );
3731 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, derived_bits,
3732 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003733 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003734 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3735 PSA_ASSERT( psa_generator_import_key( derived_handle,
3736 PSA_KEY_TYPE_RAW_DATA,
3737 derived_bits,
3738 &generator ) );
3739 PSA_ASSERT( psa_export_key( derived_handle,
3740 export_buffer, bytes1,
3741 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003742 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003743 PSA_ASSERT( psa_destroy_key( derived_handle ) );
3744 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA,
3745 PSA_BYTES_TO_BITS( bytes2 ),
3746 &derived_handle ) );
3747 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3748 PSA_ASSERT( psa_generator_import_key( derived_handle,
3749 PSA_KEY_TYPE_RAW_DATA,
3750 PSA_BYTES_TO_BITS( bytes2 ),
3751 &generator ) );
3752 PSA_ASSERT( psa_export_key( derived_handle,
3753 export_buffer + bytes1, bytes2,
3754 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003755 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003756
3757 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003758 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
3759 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003760
3761exit:
3762 mbedtls_free( output_buffer );
3763 mbedtls_free( export_buffer );
3764 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003765 psa_destroy_key( base_handle );
3766 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003767 mbedtls_psa_crypto_free( );
3768}
3769/* END_CASE */
3770
3771/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02003772void key_agreement_setup( int alg_arg,
3773 int our_key_type_arg, data_t *our_key_data,
3774 data_t *peer_key_data,
3775 int expected_status_arg )
3776{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003777 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003778 psa_algorithm_t alg = alg_arg;
3779 psa_key_type_t our_key_type = our_key_type_arg;
3780 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003781 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003782
Gilles Peskine8817f612018-12-18 00:18:46 +01003783 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003784
Gilles Peskine8817f612018-12-18 00:18:46 +01003785 PSA_ASSERT( psa_allocate_key( our_key_type,
3786 KEY_BITS_FROM_DATA( our_key_type,
3787 our_key_data ),
3788 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003789 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003790 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3791 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3792 our_key_data->x,
3793 our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003794
Gilles Peskinefe11b722018-12-18 00:24:04 +01003795 TEST_EQUAL( psa_key_agreement( &generator,
3796 our_key,
3797 peer_key_data->x, peer_key_data->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003798 alg ),
3799 expected_status_arg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003800
3801exit:
3802 psa_generator_abort( &generator );
3803 psa_destroy_key( our_key );
3804 mbedtls_psa_crypto_free( );
3805}
3806/* END_CASE */
3807
3808/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02003809void key_agreement_capacity( int alg_arg,
3810 int our_key_type_arg, data_t *our_key_data,
3811 data_t *peer_key_data,
3812 int expected_capacity_arg )
3813{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003814 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003815 psa_algorithm_t alg = alg_arg;
3816 psa_key_type_t our_key_type = our_key_type_arg;
3817 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003818 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02003819 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02003820 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02003821
Gilles Peskine8817f612018-12-18 00:18:46 +01003822 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003823
Gilles Peskine8817f612018-12-18 00:18:46 +01003824 PSA_ASSERT( psa_allocate_key( our_key_type,
3825 KEY_BITS_FROM_DATA( our_key_type,
3826 our_key_data ),
3827 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003828 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003829 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3830 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3831 our_key_data->x,
3832 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003833
Gilles Peskine8817f612018-12-18 00:18:46 +01003834 PSA_ASSERT( psa_key_agreement( &generator,
3835 our_key,
3836 peer_key_data->x, peer_key_data->len,
3837 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003838
Gilles Peskinebf491972018-10-25 22:36:12 +02003839 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003840 PSA_ASSERT( psa_get_generator_capacity(
3841 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003842 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02003843
Gilles Peskinebf491972018-10-25 22:36:12 +02003844 /* Test the actual capacity by reading the output. */
3845 while( actual_capacity > sizeof( output ) )
3846 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003847 PSA_ASSERT( psa_generator_read( &generator,
3848 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02003849 actual_capacity -= sizeof( output );
3850 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003851 PSA_ASSERT( psa_generator_read( &generator,
3852 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003853 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
3854 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskinebf491972018-10-25 22:36:12 +02003855
Gilles Peskine59685592018-09-18 12:11:34 +02003856exit:
3857 psa_generator_abort( &generator );
3858 psa_destroy_key( our_key );
3859 mbedtls_psa_crypto_free( );
3860}
3861/* END_CASE */
3862
3863/* BEGIN_CASE */
3864void key_agreement_output( int alg_arg,
3865 int our_key_type_arg, data_t *our_key_data,
3866 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003867 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02003868{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003869 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003870 psa_algorithm_t alg = alg_arg;
3871 psa_key_type_t our_key_type = our_key_type_arg;
3872 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003873 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003874 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02003875
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003876 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
3877 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003878
Gilles Peskine8817f612018-12-18 00:18:46 +01003879 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003880
Gilles Peskine8817f612018-12-18 00:18:46 +01003881 PSA_ASSERT( psa_allocate_key( our_key_type,
3882 KEY_BITS_FROM_DATA( our_key_type,
3883 our_key_data ),
3884 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003885 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003886 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3887 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3888 our_key_data->x,
3889 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003890
Gilles Peskine8817f612018-12-18 00:18:46 +01003891 PSA_ASSERT( psa_key_agreement( &generator,
3892 our_key,
3893 peer_key_data->x, peer_key_data->len,
3894 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003895
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003896 PSA_ASSERT( psa_generator_read( &generator,
3897 actual_output,
3898 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003899 ASSERT_COMPARE( actual_output, expected_output1->len,
3900 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003901 if( expected_output2->len != 0 )
3902 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003903 PSA_ASSERT( psa_generator_read( &generator,
3904 actual_output,
3905 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003906 ASSERT_COMPARE( actual_output, expected_output2->len,
3907 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003908 }
Gilles Peskine59685592018-09-18 12:11:34 +02003909
3910exit:
3911 psa_generator_abort( &generator );
3912 psa_destroy_key( our_key );
3913 mbedtls_psa_crypto_free( );
3914 mbedtls_free( actual_output );
3915}
3916/* END_CASE */
3917
3918/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003919void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003920{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003921 size_t bytes = bytes_arg;
3922 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003923 unsigned char *output = NULL;
3924 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003925 size_t i;
3926 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003927
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003928 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3929 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003930 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003931
Gilles Peskine8817f612018-12-18 00:18:46 +01003932 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003933
Gilles Peskinea50d7392018-06-21 10:22:13 +02003934 /* Run several times, to ensure that every output byte will be
3935 * nonzero at least once with overwhelming probability
3936 * (2^(-8*number_of_runs)). */
3937 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003938 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003939 if( bytes != 0 )
3940 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01003941 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003942
3943 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003944 ASSERT_COMPARE( output + bytes, sizeof( trail ),
3945 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003946
3947 for( i = 0; i < bytes; i++ )
3948 {
3949 if( output[i] != 0 )
3950 ++changed[i];
3951 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003952 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003953
3954 /* Check that every byte was changed to nonzero at least once. This
3955 * validates that psa_generate_random is overwriting every byte of
3956 * the output buffer. */
3957 for( i = 0; i < bytes; i++ )
3958 {
3959 TEST_ASSERT( changed[i] != 0 );
3960 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003961
3962exit:
3963 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003964 mbedtls_free( output );
3965 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003966}
3967/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003968
3969/* BEGIN_CASE */
3970void generate_key( int type_arg,
3971 int bits_arg,
3972 int usage_arg,
3973 int alg_arg,
3974 int expected_status_arg )
3975{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003976 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003977 psa_key_type_t type = type_arg;
3978 psa_key_usage_t usage = usage_arg;
3979 size_t bits = bits_arg;
3980 psa_algorithm_t alg = alg_arg;
3981 psa_status_t expected_status = expected_status_arg;
3982 psa_key_type_t got_type;
3983 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003984 psa_status_t expected_info_status =
3985 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003986 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003987
Gilles Peskine8817f612018-12-18 00:18:46 +01003988 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003989
Gilles Peskine8817f612018-12-18 00:18:46 +01003990 PSA_ASSERT( psa_allocate_key( type, bits, &handle ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003991 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003992 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003993
3994 /* Generate a key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003995 TEST_EQUAL( psa_generate_key( handle, type, bits, NULL, 0 ),
3996 expected_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003997
3998 /* Test the key information */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003999 TEST_EQUAL( psa_get_key_information( handle, &got_type, &got_bits ),
4000 expected_info_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004001 if( expected_info_status != PSA_SUCCESS )
4002 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004003 TEST_EQUAL( got_type, type );
4004 TEST_EQUAL( got_bits, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004005
Gilles Peskine818ca122018-06-20 18:16:48 +02004006 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004007 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004008 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004009
4010exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004011 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004012 mbedtls_psa_crypto_free( );
4013}
4014/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004015
Darryl Greend49a4992018-06-18 17:27:26 +01004016/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
4017void persistent_key_load_key_from_storage( data_t *data, int type_arg,
4018 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00004019 int alg_arg, int generation_method,
4020 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01004021{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004022 psa_key_handle_t handle = 0;
4023 psa_key_handle_t base_key;
Darryl Greend49a4992018-06-18 17:27:26 +01004024 psa_key_type_t type = (psa_key_type_t) type_arg;
4025 psa_key_type_t type_get;
4026 size_t bits_get;
Jaeden Amero70261c52019-01-04 11:47:20 +00004027 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
4028 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004029 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
4030 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00004031 psa_key_policy_t base_policy_set = PSA_KEY_POLICY_INIT;
Darryl Green0c6575a2018-11-07 16:05:30 +00004032 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
4033 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004034 unsigned char *first_export = NULL;
4035 unsigned char *second_export = NULL;
4036 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4037 size_t first_exported_length;
4038 size_t second_exported_length;
4039
4040 ASSERT_ALLOC( first_export, export_size );
4041 ASSERT_ALLOC( second_export, export_size );
4042
Gilles Peskine8817f612018-12-18 00:18:46 +01004043 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004044
Gilles Peskine8817f612018-12-18 00:18:46 +01004045 PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4046 type, bits,
4047 &handle ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004048 psa_key_policy_set_usage( &policy_set, policy_usage,
4049 policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004050 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004051
Darryl Green0c6575a2018-11-07 16:05:30 +00004052 switch( generation_method )
4053 {
4054 case IMPORT_KEY:
4055 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004056 PSA_ASSERT( psa_import_key( handle, type,
4057 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004058 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004059
Darryl Green0c6575a2018-11-07 16:05:30 +00004060 case GENERATE_KEY:
4061 /* Generate a key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004062 PSA_ASSERT( psa_generate_key( handle, type, bits,
4063 NULL, 0 ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004064 break;
4065
4066 case DERIVE_KEY:
4067 /* Create base key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004068 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
4069 PSA_BYTES_TO_BITS( data->len ),
4070 &base_key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004071 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
4072 base_policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004073 PSA_ASSERT( psa_set_key_policy(
4074 base_key, &base_policy_set ) );
4075 PSA_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
4076 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004077 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004078 PSA_ASSERT( psa_key_derivation( &generator, base_key,
4079 base_policy_alg,
4080 NULL, 0, NULL, 0,
4081 export_size ) );
4082 PSA_ASSERT( psa_generator_import_key(
4083 handle, PSA_KEY_TYPE_RAW_DATA,
4084 bits, &generator ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004085 break;
4086 }
Darryl Greend49a4992018-06-18 17:27:26 +01004087
4088 /* Export the key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004089 TEST_EQUAL( psa_export_key( handle,
4090 first_export, export_size,
4091 &first_exported_length ),
4092 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004093
4094 /* Shutdown and restart */
4095 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004096 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004097
Darryl Greend49a4992018-06-18 17:27:26 +01004098 /* Check key slot still contains key data */
Gilles Peskine8817f612018-12-18 00:18:46 +01004099 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4100 &handle ) );
4101 PSA_ASSERT( psa_get_key_information(
4102 handle, &type_get, &bits_get ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004103 TEST_EQUAL( type_get, type );
4104 TEST_EQUAL( bits_get, (size_t) bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004105
Gilles Peskine8817f612018-12-18 00:18:46 +01004106 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004107 TEST_EQUAL( psa_key_policy_get_usage( &policy_get ), policy_usage );
4108 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_get ), policy_alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004109
4110 /* Export the key again */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004111 TEST_EQUAL( psa_export_key( handle,
4112 second_export, export_size,
4113 &second_exported_length ),
4114 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004115
Darryl Green0c6575a2018-11-07 16:05:30 +00004116 if( export_status == PSA_SUCCESS )
4117 {
4118 ASSERT_COMPARE( first_export, first_exported_length,
4119 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01004120
Darryl Green0c6575a2018-11-07 16:05:30 +00004121 switch( generation_method )
4122 {
4123 case IMPORT_KEY:
4124 ASSERT_COMPARE( data->x, data->len,
4125 first_export, first_exported_length );
4126 break;
4127 default:
4128 break;
4129 }
4130 }
4131
4132 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004133 if( ! exercise_key( handle, policy_usage, policy_alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004134 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004135
4136exit:
4137 mbedtls_free( first_export );
4138 mbedtls_free( second_export );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004139 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004140 mbedtls_psa_crypto_free();
4141}
4142/* END_CASE */