blob: 6916bf42e58cf15c785d576072f965f7b1e8fba6 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
4#if defined(MBEDTLS_PSA_CRYPTO_SPM)
5#include "spm/psa_defs.h"
6#endif
7
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02009#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +020010#include "mbedtls/oid.h"
11
Gilles Peskinee59236f2018-01-27 23:32:46 +010012#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030013
Jaeden Amerof24c7f82018-06-27 17:20:43 +010014/** An invalid export length that will never be set by psa_export_key(). */
15static const size_t INVALID_EXPORT_LENGTH = ~0U;
16
Gilles Peskinea7aa4422018-08-14 15:17:54 +020017/** Test if a buffer contains a constant byte value.
18 *
19 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020020 *
21 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020022 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020023 * \param size Size of the buffer in bytes.
24 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020025 * \return 1 if the buffer is all-bits-zero.
26 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020027 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020028static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020029{
30 size_t i;
31 for( i = 0; i < size; i++ )
32 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020033 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020034 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020035 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020036 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020037}
Gilles Peskine818ca122018-06-20 18:16:48 +020038
Gilles Peskine0b352bc2018-06-28 00:16:11 +020039/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
40static int asn1_write_10x( unsigned char **p,
41 unsigned char *start,
42 size_t bits,
43 unsigned char x )
44{
45 int ret;
46 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020047 if( bits == 0 )
48 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
49 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020050 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030051 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020052 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
53 *p -= len;
54 ( *p )[len-1] = x;
55 if( bits % 8 == 0 )
56 ( *p )[1] |= 1;
57 else
58 ( *p )[0] |= 1 << ( bits % 8 );
59 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
60 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
61 MBEDTLS_ASN1_INTEGER ) );
62 return( len );
63}
64
65static int construct_fake_rsa_key( unsigned char *buffer,
66 size_t buffer_size,
67 unsigned char **p,
68 size_t bits,
69 int keypair )
70{
71 size_t half_bits = ( bits + 1 ) / 2;
72 int ret;
73 int len = 0;
74 /* Construct something that looks like a DER encoding of
75 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
76 * RSAPrivateKey ::= SEQUENCE {
77 * version Version,
78 * modulus INTEGER, -- n
79 * publicExponent INTEGER, -- e
80 * privateExponent INTEGER, -- d
81 * prime1 INTEGER, -- p
82 * prime2 INTEGER, -- q
83 * exponent1 INTEGER, -- d mod (p-1)
84 * exponent2 INTEGER, -- d mod (q-1)
85 * coefficient INTEGER, -- (inverse of q) mod p
86 * otherPrimeInfos OtherPrimeInfos OPTIONAL
87 * }
88 * Or, for a public key, the same structure with only
89 * version, modulus and publicExponent.
90 */
91 *p = buffer + buffer_size;
92 if( keypair )
93 {
94 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
95 asn1_write_10x( p, buffer, half_bits, 1 ) );
96 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
97 asn1_write_10x( p, buffer, half_bits, 1 ) );
98 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
99 asn1_write_10x( p, buffer, half_bits, 1 ) );
100 MBEDTLS_ASN1_CHK_ADD( len, /* q */
101 asn1_write_10x( p, buffer, half_bits, 1 ) );
102 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
103 asn1_write_10x( p, buffer, half_bits, 3 ) );
104 MBEDTLS_ASN1_CHK_ADD( len, /* d */
105 asn1_write_10x( p, buffer, bits, 1 ) );
106 }
107 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
108 asn1_write_10x( p, buffer, 17, 1 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* n */
110 asn1_write_10x( p, buffer, bits, 1 ) );
111 if( keypair )
112 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
113 mbedtls_asn1_write_int( p, buffer, 0 ) );
114 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
115 {
116 const unsigned char tag =
117 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
118 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
119 }
120 return( len );
121}
122
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100123static int exercise_mac_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200124 psa_key_usage_t usage,
125 psa_algorithm_t alg )
126{
Jaeden Amero769ce272019-01-04 11:48:03 +0000127 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200128 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200129 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200130 size_t mac_length = sizeof( mac );
131
132 if( usage & PSA_KEY_USAGE_SIGN )
133 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100134 PSA_ASSERT( psa_mac_sign_setup( &operation,
135 handle, alg ) );
136 PSA_ASSERT( psa_mac_update( &operation,
137 input, sizeof( input ) ) );
138 PSA_ASSERT( psa_mac_sign_finish( &operation,
139 mac, sizeof( mac ),
140 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200141 }
142
143 if( usage & PSA_KEY_USAGE_VERIFY )
144 {
145 psa_status_t verify_status =
146 ( usage & PSA_KEY_USAGE_SIGN ?
147 PSA_SUCCESS :
148 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +0100149 PSA_ASSERT( psa_mac_verify_setup( &operation,
150 handle, alg ) );
151 PSA_ASSERT( psa_mac_update( &operation,
152 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100153 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
154 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200155 }
156
157 return( 1 );
158
159exit:
160 psa_mac_abort( &operation );
161 return( 0 );
162}
163
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100164static int exercise_cipher_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200165 psa_key_usage_t usage,
166 psa_algorithm_t alg )
167{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000168 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200169 unsigned char iv[16] = {0};
170 size_t iv_length = sizeof( iv );
171 const unsigned char plaintext[16] = "Hello, world...";
172 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
173 size_t ciphertext_length = sizeof( ciphertext );
174 unsigned char decrypted[sizeof( ciphertext )];
175 size_t part_length;
176
177 if( usage & PSA_KEY_USAGE_ENCRYPT )
178 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100179 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
180 handle, alg ) );
181 PSA_ASSERT( psa_cipher_generate_iv( &operation,
182 iv, sizeof( iv ),
183 &iv_length ) );
184 PSA_ASSERT( psa_cipher_update( &operation,
185 plaintext, sizeof( plaintext ),
186 ciphertext, sizeof( ciphertext ),
187 &ciphertext_length ) );
188 PSA_ASSERT( psa_cipher_finish( &operation,
189 ciphertext + ciphertext_length,
190 sizeof( ciphertext ) - ciphertext_length,
191 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200192 ciphertext_length += part_length;
193 }
194
195 if( usage & PSA_KEY_USAGE_DECRYPT )
196 {
197 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700198 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200199 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
200 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200201 size_t bits;
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100202 TEST_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200203 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
204 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100205 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
206 handle, alg ) );
207 PSA_ASSERT( psa_cipher_set_iv( &operation,
208 iv, iv_length ) );
209 PSA_ASSERT( psa_cipher_update( &operation,
210 ciphertext, ciphertext_length,
211 decrypted, sizeof( decrypted ),
212 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200213 status = psa_cipher_finish( &operation,
214 decrypted + part_length,
215 sizeof( decrypted ) - part_length,
216 &part_length );
217 /* For a stream cipher, all inputs are valid. For a block cipher,
218 * if the input is some aribtrary data rather than an actual
219 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700220 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700221 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100222 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200223 else
224 TEST_ASSERT( status == PSA_SUCCESS ||
225 status == PSA_ERROR_INVALID_PADDING );
226 }
227
228 return( 1 );
229
230exit:
231 psa_cipher_abort( &operation );
232 return( 0 );
233}
234
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100235static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200236 psa_key_usage_t usage,
237 psa_algorithm_t alg )
238{
239 unsigned char nonce[16] = {0};
240 size_t nonce_length = sizeof( nonce );
241 unsigned char plaintext[16] = "Hello, world...";
242 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
243 size_t ciphertext_length = sizeof( ciphertext );
244 size_t plaintext_length = sizeof( ciphertext );
245
246 if( usage & PSA_KEY_USAGE_ENCRYPT )
247 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100248 PSA_ASSERT( psa_aead_encrypt( handle, alg,
249 nonce, nonce_length,
250 NULL, 0,
251 plaintext, sizeof( plaintext ),
252 ciphertext, sizeof( ciphertext ),
253 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200254 }
255
256 if( usage & PSA_KEY_USAGE_DECRYPT )
257 {
258 psa_status_t verify_status =
259 ( usage & PSA_KEY_USAGE_ENCRYPT ?
260 PSA_SUCCESS :
261 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100262 TEST_EQUAL( psa_aead_decrypt( handle, alg,
263 nonce, nonce_length,
264 NULL, 0,
265 ciphertext, ciphertext_length,
266 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100267 &plaintext_length ),
268 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200269 }
270
271 return( 1 );
272
273exit:
274 return( 0 );
275}
276
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100277static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200278 psa_key_usage_t usage,
279 psa_algorithm_t alg )
280{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200281 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
282 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200283 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200284 size_t signature_length = sizeof( signature );
285
286 if( usage & PSA_KEY_USAGE_SIGN )
287 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200288 /* Some algorithms require the payload to have the size of
289 * the hash encoded in the algorithm. Use this input size
290 * even for algorithms that allow other input sizes. */
291 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
292 if( hash_alg != 0 )
293 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100294 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
295 payload, payload_length,
296 signature, sizeof( signature ),
297 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200298 }
299
300 if( usage & PSA_KEY_USAGE_VERIFY )
301 {
302 psa_status_t verify_status =
303 ( usage & PSA_KEY_USAGE_SIGN ?
304 PSA_SUCCESS :
305 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100306 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
307 payload, payload_length,
308 signature, signature_length ),
309 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200310 }
311
312 return( 1 );
313
314exit:
315 return( 0 );
316}
317
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100318static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200319 psa_key_usage_t usage,
320 psa_algorithm_t alg )
321{
322 unsigned char plaintext[256] = "Hello, world...";
323 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
324 size_t ciphertext_length = sizeof( ciphertext );
325 size_t plaintext_length = 16;
326
327 if( usage & PSA_KEY_USAGE_ENCRYPT )
328 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100329 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
330 plaintext, plaintext_length,
331 NULL, 0,
332 ciphertext, sizeof( ciphertext ),
333 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200334 }
335
336 if( usage & PSA_KEY_USAGE_DECRYPT )
337 {
338 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100339 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200340 ciphertext, ciphertext_length,
341 NULL, 0,
342 plaintext, sizeof( plaintext ),
343 &plaintext_length );
344 TEST_ASSERT( status == PSA_SUCCESS ||
345 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
346 ( status == PSA_ERROR_INVALID_ARGUMENT ||
347 status == PSA_ERROR_INVALID_PADDING ) ) );
348 }
349
350 return( 1 );
351
352exit:
353 return( 0 );
354}
Gilles Peskine02b75072018-07-01 22:31:34 +0200355
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100356static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200357 psa_key_usage_t usage,
358 psa_algorithm_t alg )
359{
360 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
361 unsigned char label[16] = "This is a label.";
362 size_t label_length = sizeof( label );
363 unsigned char seed[16] = "abcdefghijklmnop";
364 size_t seed_length = sizeof( seed );
365 unsigned char output[1];
366
367 if( usage & PSA_KEY_USAGE_DERIVE )
368 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100369 PSA_ASSERT( psa_key_derivation( &generator,
370 handle, alg,
371 label, label_length,
372 seed, seed_length,
373 sizeof( output ) ) );
374 PSA_ASSERT( psa_generator_read( &generator,
375 output,
376 sizeof( output ) ) );
377 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200378 }
379
380 return( 1 );
381
382exit:
383 return( 0 );
384}
385
Gilles Peskinec7998b72018-11-07 18:45:02 +0100386/* We need two keys to exercise key agreement. Exercise the
387 * private key against its own public key. */
388static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator,
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100389 psa_key_handle_t handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100390 psa_algorithm_t alg )
391{
392 psa_key_type_t private_key_type;
393 psa_key_type_t public_key_type;
394 size_t key_bits;
395 uint8_t *public_key = NULL;
396 size_t public_key_length;
397 /* Return UNKNOWN_ERROR if something other than the final call to
398 * psa_key_agreement fails. This isn't fully satisfactory, but it's
399 * good enough: callers will report it as a failed test anyway. */
400 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
401
Gilles Peskine8817f612018-12-18 00:18:46 +0100402 PSA_ASSERT( psa_get_key_information( handle,
403 &private_key_type,
404 &key_bits ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100405 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
406 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
407 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100408 PSA_ASSERT( psa_export_public_key( handle,
409 public_key, public_key_length,
410 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100411
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100412 status = psa_key_agreement( generator, handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100413 public_key, public_key_length,
414 alg );
415exit:
416 mbedtls_free( public_key );
417 return( status );
418}
419
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100420static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200421 psa_key_usage_t usage,
422 psa_algorithm_t alg )
423{
424 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200425 unsigned char output[1];
426 int ok = 0;
427
428 if( usage & PSA_KEY_USAGE_DERIVE )
429 {
430 /* We need two keys to exercise key agreement. Exercise the
431 * private key against its own public key. */
Gilles Peskine8817f612018-12-18 00:18:46 +0100432 PSA_ASSERT( key_agreement_with_self( &generator, handle, alg ) );
433 PSA_ASSERT( psa_generator_read( &generator,
434 output,
435 sizeof( output ) ) );
436 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200437 }
438 ok = 1;
439
440exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200441 return( ok );
442}
443
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200444static int is_oid_of_key_type( psa_key_type_t type,
445 const uint8_t *oid, size_t oid_length )
446{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200447 const uint8_t *expected_oid = NULL;
448 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200449#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200450 if( PSA_KEY_TYPE_IS_RSA( type ) )
451 {
452 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
453 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
454 }
455 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200456#endif /* MBEDTLS_RSA_C */
457#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200458 if( PSA_KEY_TYPE_IS_ECC( type ) )
459 {
460 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
461 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
462 }
463 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200464#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200465 {
466 char message[40];
467 mbedtls_snprintf( message, sizeof( message ),
468 "OID not known for key type=0x%08lx",
469 (unsigned long) type );
470 test_fail( message, __LINE__, __FILE__ );
471 return( 0 );
472 }
473
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200474 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200475 return( 1 );
476
477exit:
478 return( 0 );
479}
480
481static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
482 size_t min_bits, size_t max_bits,
483 int must_be_odd )
484{
485 size_t len;
486 size_t actual_bits;
487 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100488 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100489 MBEDTLS_ASN1_INTEGER ),
490 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200491 /* Tolerate a slight departure from DER encoding:
492 * - 0 may be represented by an empty string or a 1-byte string.
493 * - The sign bit may be used as a value bit. */
494 if( ( len == 1 && ( *p )[0] == 0 ) ||
495 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
496 {
497 ++( *p );
498 --len;
499 }
500 if( min_bits == 0 && len == 0 )
501 return( 1 );
502 msb = ( *p )[0];
503 TEST_ASSERT( msb != 0 );
504 actual_bits = 8 * ( len - 1 );
505 while( msb != 0 )
506 {
507 msb >>= 1;
508 ++actual_bits;
509 }
510 TEST_ASSERT( actual_bits >= min_bits );
511 TEST_ASSERT( actual_bits <= max_bits );
512 if( must_be_odd )
513 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
514 *p += len;
515 return( 1 );
516exit:
517 return( 0 );
518}
519
520static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
521 size_t *len,
522 unsigned char n, unsigned char tag )
523{
524 int ret;
525 ret = mbedtls_asn1_get_tag( p, end, len,
526 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
527 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
528 if( ret != 0 )
529 return( ret );
530 end = *p + *len;
531 ret = mbedtls_asn1_get_tag( p, end, len, tag );
532 if( ret != 0 )
533 return( ret );
534 if( *p + *len != end )
535 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
536 return( 0 );
537}
538
539static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
540 uint8_t *exported, size_t exported_length )
541{
542 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100543 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200544 else
545 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200546
547#if defined(MBEDTLS_DES_C)
548 if( type == PSA_KEY_TYPE_DES )
549 {
550 /* Check the parity bits. */
551 unsigned i;
552 for( i = 0; i < bits / 8; i++ )
553 {
554 unsigned bit_count = 0;
555 unsigned m;
556 for( m = 1; m <= 0x100; m <<= 1 )
557 {
558 if( exported[i] & m )
559 ++bit_count;
560 }
561 TEST_ASSERT( bit_count % 2 != 0 );
562 }
563 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200564 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200565#endif
566
567#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
568 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
569 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200570 uint8_t *p = exported;
571 uint8_t *end = exported + exported_length;
572 size_t len;
573 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200574 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200575 * modulus INTEGER, -- n
576 * publicExponent INTEGER, -- e
577 * privateExponent INTEGER, -- d
578 * prime1 INTEGER, -- p
579 * prime2 INTEGER, -- q
580 * exponent1 INTEGER, -- d mod (p-1)
581 * exponent2 INTEGER, -- d mod (q-1)
582 * coefficient INTEGER, -- (inverse of q) mod p
583 * }
584 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100585 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
586 MBEDTLS_ASN1_SEQUENCE |
587 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
588 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200589 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
590 goto exit;
591 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
592 goto exit;
593 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
594 goto exit;
595 /* Require d to be at least half the size of n. */
596 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
597 goto exit;
598 /* Require p and q to be at most half the size of n, rounded up. */
599 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
600 goto exit;
601 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
602 goto exit;
603 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
604 goto exit;
605 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
606 goto exit;
607 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
608 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100609 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100610 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200611 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200612#endif /* MBEDTLS_RSA_C */
613
614#if defined(MBEDTLS_ECP_C)
615 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
616 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100617 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100618 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100619 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200620 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200621#endif /* MBEDTLS_ECP_C */
622
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200623 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
624 {
625 uint8_t *p = exported;
626 uint8_t *end = exported + exported_length;
627 size_t len;
628 mbedtls_asn1_buf alg;
629 mbedtls_asn1_buf params;
630 mbedtls_asn1_bitstring bitstring;
631 /* SubjectPublicKeyInfo ::= SEQUENCE {
632 * algorithm AlgorithmIdentifier,
633 * subjectPublicKey BIT STRING }
634 * AlgorithmIdentifier ::= SEQUENCE {
635 * algorithm OBJECT IDENTIFIER,
636 * parameters ANY DEFINED BY algorithm OPTIONAL }
637 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100638 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
639 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100640 MBEDTLS_ASN1_CONSTRUCTED ),
641 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100642 TEST_EQUAL( p + len, end );
643 TEST_EQUAL( mbedtls_asn1_get_alg( &p, end, &alg, &params ), 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200644 if( ! is_oid_of_key_type( type, alg.p, alg.len ) )
645 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100646 TEST_EQUAL( mbedtls_asn1_get_bitstring( &p, end, &bitstring ), 0 );
647 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200648 p = bitstring.p;
649#if defined(MBEDTLS_RSA_C)
650 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
651 {
652 /* RSAPublicKey ::= SEQUENCE {
653 * modulus INTEGER, -- n
654 * publicExponent INTEGER } -- e
655 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100656 TEST_EQUAL( bitstring.unused_bits, 0 );
657 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
658 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100659 MBEDTLS_ASN1_CONSTRUCTED ),
660 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100661 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200662 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
663 goto exit;
664 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
665 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100666 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200667 }
668 else
669#endif /* MBEDTLS_RSA_C */
670#if defined(MBEDTLS_ECP_C)
671 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
672 {
673 /* ECPoint ::= ...
Gilles Peskinec6290c02018-08-13 17:24:59 +0200674 * -- first 8 bits: 0x04 (uncompressed representation);
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200675 * -- then x_P as an n-bit string, big endian;
676 * -- then y_P as a n-bit string, big endian,
677 * -- where n is the order of the curve.
678 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100679 TEST_EQUAL( bitstring.unused_bits, 0 );
680 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
681 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200682 }
683 else
684#endif /* MBEDTLS_ECP_C */
685 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100686 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200687 mbedtls_snprintf( message, sizeof( message ),
688 "No sanity check for public key type=0x%08lx",
689 (unsigned long) type );
690 test_fail( message, __LINE__, __FILE__ );
691 return( 0 );
692 }
693 }
694 else
695
696 {
697 /* No sanity checks for other types */
698 }
699
700 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200701
702exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200703 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200704}
705
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100706static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200707 psa_key_usage_t usage )
708{
709 psa_key_type_t type;
710 size_t bits;
711 uint8_t *exported = NULL;
712 size_t exported_size = 0;
713 size_t exported_length = 0;
714 int ok = 0;
715
Gilles Peskine8817f612018-12-18 00:18:46 +0100716 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200717
718 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
719 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200720 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100721 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
722 PSA_ERROR_NOT_PERMITTED );
Gilles Peskined14664a2018-08-10 19:07:32 +0200723 return( 1 );
724 }
725
Gilles Peskined14664a2018-08-10 19:07:32 +0200726 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200727 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200728
Gilles Peskine8817f612018-12-18 00:18:46 +0100729 PSA_ASSERT( psa_export_key( handle,
730 exported, exported_size,
731 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200732 ok = exported_key_sanity_check( type, bits, exported, exported_length );
733
734exit:
735 mbedtls_free( exported );
736 return( ok );
737}
738
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100739static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200740{
741 psa_key_type_t type;
742 psa_key_type_t public_type;
743 size_t bits;
744 uint8_t *exported = NULL;
745 size_t exported_size = 0;
746 size_t exported_length = 0;
747 int ok = 0;
748
Gilles Peskine8817f612018-12-18 00:18:46 +0100749 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200750 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
751 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100752 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100753 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200754 return( 1 );
755 }
756
757 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
758 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200759 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200760
Gilles Peskine8817f612018-12-18 00:18:46 +0100761 PSA_ASSERT( psa_export_public_key( handle,
762 exported, exported_size,
763 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200764 ok = exported_key_sanity_check( public_type, bits,
765 exported, exported_length );
766
767exit:
768 mbedtls_free( exported );
769 return( ok );
770}
771
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100772static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +0200773 psa_key_usage_t usage,
774 psa_algorithm_t alg )
775{
776 int ok;
777 if( alg == 0 )
778 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
779 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100780 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200781 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100782 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200783 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100784 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200785 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100786 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200787 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100788 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200789 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100790 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200791 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100792 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200793 else
794 {
795 char message[40];
796 mbedtls_snprintf( message, sizeof( message ),
797 "No code to exercise alg=0x%08lx",
798 (unsigned long) alg );
799 test_fail( message, __LINE__, __FILE__ );
800 ok = 0;
801 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200802
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100803 ok = ok && exercise_export_key( handle, usage );
804 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +0200805
Gilles Peskine02b75072018-07-01 22:31:34 +0200806 return( ok );
807}
808
Gilles Peskine10df3412018-10-25 22:35:43 +0200809static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
810 psa_algorithm_t alg )
811{
812 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
813 {
814 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
815 PSA_KEY_USAGE_VERIFY :
816 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
817 }
818 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
819 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
820 {
821 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
822 PSA_KEY_USAGE_ENCRYPT :
823 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
824 }
825 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
826 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
827 {
828 return( PSA_KEY_USAGE_DERIVE );
829 }
830 else
831 {
832 return( 0 );
833 }
834
835}
Darryl Green0c6575a2018-11-07 16:05:30 +0000836
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100837/* An overapproximation of the amount of storage needed for a key of the
838 * given type and with the given content. The API doesn't make it easy
839 * to find a good value for the size. The current implementation doesn't
840 * care about the value anyway. */
841#define KEY_BITS_FROM_DATA( type, data ) \
842 ( data )->len
843
Darryl Green0c6575a2018-11-07 16:05:30 +0000844typedef enum {
845 IMPORT_KEY = 0,
846 GENERATE_KEY = 1,
847 DERIVE_KEY = 2
848} generate_method;
849
Gilles Peskinee59236f2018-01-27 23:32:46 +0100850/* END_HEADER */
851
852/* BEGIN_DEPENDENCIES
853 * depends_on:MBEDTLS_PSA_CRYPTO_C
854 * END_DEPENDENCIES
855 */
856
857/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200858void static_checks( )
859{
860 size_t max_truncated_mac_size =
861 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
862
863 /* Check that the length for a truncated MAC always fits in the algorithm
864 * encoding. The shifted mask is the maximum truncated value. The
865 * untruncated algorithm may be one byte larger. */
866 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
867}
868/* END_CASE */
869
870/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200871void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100873 psa_key_handle_t handle = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200874 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100875 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100876
Gilles Peskine8817f612018-12-18 00:18:46 +0100877 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100878
Gilles Peskine8817f612018-12-18 00:18:46 +0100879 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
880 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100881 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100882 TEST_EQUAL( status, expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100883 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100884 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100885
886exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100887 mbedtls_psa_crypto_free( );
888}
889/* END_CASE */
890
891/* BEGIN_CASE */
Gilles Peskinea4261682018-12-03 11:34:01 +0100892void import_twice( int alg_arg, int usage_arg,
893 int type1_arg, data_t *data1,
894 int expected_import1_status_arg,
895 int type2_arg, data_t *data2,
896 int expected_import2_status_arg )
897{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100898 psa_key_handle_t handle = 0;
Gilles Peskinea4261682018-12-03 11:34:01 +0100899 psa_algorithm_t alg = alg_arg;
900 psa_key_usage_t usage = usage_arg;
901 psa_key_type_t type1 = type1_arg;
902 psa_status_t expected_import1_status = expected_import1_status_arg;
903 psa_key_type_t type2 = type2_arg;
904 psa_status_t expected_import2_status = expected_import2_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +0000905 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea4261682018-12-03 11:34:01 +0100906 psa_status_t status;
907
Gilles Peskine8817f612018-12-18 00:18:46 +0100908 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100909
Gilles Peskine8817f612018-12-18 00:18:46 +0100910 PSA_ASSERT( psa_allocate_key( type1,
911 MAX( KEY_BITS_FROM_DATA( type1, data1 ),
912 KEY_BITS_FROM_DATA( type2, data2 ) ),
913 &handle ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100914 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100915 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100916
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100917 status = psa_import_key( handle, type1, data1->x, data1->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100918 TEST_EQUAL( status, expected_import1_status );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100919 status = psa_import_key( handle, type2, data2->x, data2->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100920 TEST_EQUAL( status, expected_import2_status );
Gilles Peskinea4261682018-12-03 11:34:01 +0100921
922 if( expected_import1_status == PSA_SUCCESS ||
923 expected_import2_status == PSA_SUCCESS )
924 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100925 TEST_ASSERT( exercise_key( handle, usage, alg ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100926 }
927
928exit:
929 mbedtls_psa_crypto_free( );
930}
931/* END_CASE */
932
933/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200934void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
935{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100936 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200937 size_t bits = bits_arg;
938 psa_status_t expected_status = expected_status_arg;
939 psa_status_t status;
940 psa_key_type_t type =
941 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
942 size_t buffer_size = /* Slight overapproximations */
943 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200944 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200945 unsigned char *p;
946 int ret;
947 size_t length;
948
Gilles Peskine8817f612018-12-18 00:18:46 +0100949 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200950 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200951
952 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
953 bits, keypair ) ) >= 0 );
954 length = ret;
955
956 /* Try importing the key */
Gilles Peskine8817f612018-12-18 00:18:46 +0100957 PSA_ASSERT( psa_allocate_key( type, bits, &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100958 status = psa_import_key( handle, type, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100959 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200960 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100961 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200962
963exit:
964 mbedtls_free( buffer );
965 mbedtls_psa_crypto_free( );
966}
967/* END_CASE */
968
969/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300970void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300971 int type_arg,
972 int alg_arg,
973 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100974 int expected_bits,
975 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200976 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100977 int canonical_input )
978{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100979 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100980 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200981 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200982 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100983 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100984 unsigned char *exported = NULL;
985 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100986 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100987 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100988 size_t reexported_length;
989 psa_key_type_t got_type;
990 size_t got_bits;
Jaeden Amero70261c52019-01-04 11:47:20 +0000991 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100992
Moran Pekercb088e72018-07-17 17:36:59 +0300993 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200994 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100995 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200996 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100997 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100998
Gilles Peskine8817f612018-12-18 00:18:46 +0100999 PSA_ASSERT( psa_allocate_key( type, expected_bits, &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001000 psa_key_policy_set_usage( &policy, usage_arg, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001001 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001002
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001003 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1004 PSA_ERROR_EMPTY_SLOT );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001005
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001006 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001007 PSA_ASSERT( psa_import_key( handle, type,
1008 data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001009
1010 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001011 PSA_ASSERT( psa_get_key_information( handle,
1012 &got_type,
1013 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001014 TEST_EQUAL( got_type, type );
1015 TEST_EQUAL( got_bits, (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001016
1017 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001018 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001019 exported, export_size,
1020 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001021 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001022
1023 /* The exported length must be set by psa_export_key() to a value between 0
1024 * and export_size. On errors, the exported length must be 0. */
1025 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1026 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1027 TEST_ASSERT( exported_length <= export_size );
1028
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001029 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001030 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001031 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001032 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001033 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001034 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001035 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001036
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001037 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001038 goto exit;
1039
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001040 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001041 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001042 else
1043 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001044 psa_key_handle_t handle2;
Gilles Peskine8817f612018-12-18 00:18:46 +01001045 PSA_ASSERT( psa_allocate_key( type, expected_bits, &handle2 ) );
1046 PSA_ASSERT( psa_set_key_policy( handle2, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001047
Gilles Peskine8817f612018-12-18 00:18:46 +01001048 PSA_ASSERT( psa_import_key( handle2, type,
1049 exported,
1050 exported_length ) );
1051 PSA_ASSERT( psa_export_key( handle2,
1052 reexported,
1053 export_size,
1054 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001055 ASSERT_COMPARE( exported, exported_length,
1056 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001057 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001058 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001059 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001060
1061destroy:
1062 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001063 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001064 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1065 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001066
1067exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001068 mbedtls_free( exported );
1069 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001070 mbedtls_psa_crypto_free( );
1071}
1072/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001073
Moran Pekerf709f4a2018-06-06 17:26:04 +03001074/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001075void import_key_nonempty_slot( )
1076{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001077 psa_key_handle_t handle = 0;
Moran Peker28a38e62018-11-07 16:18:24 +02001078 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1079 psa_status_t status;
1080 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
Gilles Peskine8817f612018-12-18 00:18:46 +01001081 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001082
Gilles Peskine8817f612018-12-18 00:18:46 +01001083 PSA_ASSERT( psa_allocate_key( type, PSA_BYTES_TO_BITS( sizeof( data ) ),
1084 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001085
Moran Peker28a38e62018-11-07 16:18:24 +02001086 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001087 PSA_ASSERT( psa_import_key( handle, type,
1088 data, sizeof( data ) ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001089
1090 /* Import the key again */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001091 status = psa_import_key( handle, type, data, sizeof( data ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001092 TEST_EQUAL( status, PSA_ERROR_OCCUPIED_SLOT );
Moran Peker28a38e62018-11-07 16:18:24 +02001093
1094exit:
1095 mbedtls_psa_crypto_free( );
1096}
1097/* END_CASE */
1098
1099/* BEGIN_CASE */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001100void export_invalid_handle( int handle, int expected_export_status_arg )
Moran Peker28a38e62018-11-07 16:18:24 +02001101{
1102 psa_status_t status;
1103 unsigned char *exported = NULL;
1104 size_t export_size = 0;
1105 size_t exported_length = INVALID_EXPORT_LENGTH;
1106 psa_status_t expected_export_status = expected_export_status_arg;
1107
Gilles Peskine8817f612018-12-18 00:18:46 +01001108 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001109
1110 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001111 status = psa_export_key( (psa_key_handle_t) handle,
Moran Peker28a38e62018-11-07 16:18:24 +02001112 exported, export_size,
1113 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001114 TEST_EQUAL( status, expected_export_status );
Moran Peker28a38e62018-11-07 16:18:24 +02001115
1116exit:
1117 mbedtls_psa_crypto_free( );
1118}
1119/* END_CASE */
1120
1121/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001122void export_with_no_key_activity( )
1123{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001124 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001125 psa_algorithm_t alg = PSA_ALG_CTR;
1126 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001127 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001128 unsigned char *exported = NULL;
1129 size_t export_size = 0;
1130 size_t exported_length = INVALID_EXPORT_LENGTH;
1131
Gilles Peskine8817f612018-12-18 00:18:46 +01001132 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001133
Gilles Peskine8817f612018-12-18 00:18:46 +01001134 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1135 &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001136 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001137 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001138
1139 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001140 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001141 exported, export_size,
1142 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001143 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001144
1145exit:
1146 mbedtls_psa_crypto_free( );
1147}
1148/* END_CASE */
1149
1150/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001151void cipher_with_no_key_activity( )
1152{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001153 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001154 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001155 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001156 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001157 int exercise_alg = PSA_ALG_CTR;
1158
Gilles Peskine8817f612018-12-18 00:18:46 +01001159 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001160
Gilles Peskine8817f612018-12-18 00:18:46 +01001161 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1162 &handle ) );
Moran Pekerce500072018-11-07 16:20:07 +02001163 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001164 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerce500072018-11-07 16:20:07 +02001165
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001166 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001167 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001168
1169exit:
1170 psa_cipher_abort( &operation );
1171 mbedtls_psa_crypto_free( );
1172}
1173/* END_CASE */
1174
1175/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001176void export_after_import_failure( data_t *data, int type_arg,
1177 int expected_import_status_arg )
1178{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001179 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001180 psa_key_type_t type = type_arg;
1181 psa_status_t status;
1182 unsigned char *exported = NULL;
1183 size_t export_size = 0;
1184 psa_status_t expected_import_status = expected_import_status_arg;
1185 size_t exported_length = INVALID_EXPORT_LENGTH;
1186
Gilles Peskine8817f612018-12-18 00:18:46 +01001187 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001188
Gilles Peskine8817f612018-12-18 00:18:46 +01001189 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1190 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001191
Moran Peker34550092018-11-07 16:19:34 +02001192 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001193 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001194 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001195 TEST_EQUAL( status, expected_import_status );
Moran Peker34550092018-11-07 16:19:34 +02001196
1197 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001198 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001199 exported, export_size,
1200 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001201 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001202
1203exit:
1204 mbedtls_psa_crypto_free( );
1205}
1206/* END_CASE */
1207
1208/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001209void cipher_after_import_failure( data_t *data, int type_arg,
1210 int expected_import_status_arg )
1211{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001212 psa_key_handle_t handle = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001213 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001214 psa_key_type_t type = type_arg;
1215 psa_status_t status;
1216 psa_status_t expected_import_status = expected_import_status_arg;
1217 int exercise_alg = PSA_ALG_CTR;
1218
Gilles Peskine8817f612018-12-18 00:18:46 +01001219 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001220
Gilles Peskine8817f612018-12-18 00:18:46 +01001221 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1222 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001223
Moran Pekerce500072018-11-07 16:20:07 +02001224 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001225 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001226 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001227 TEST_EQUAL( status, expected_import_status );
Moran Pekerce500072018-11-07 16:20:07 +02001228
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001229 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001230 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001231
1232exit:
1233 psa_cipher_abort( &operation );
1234 mbedtls_psa_crypto_free( );
1235}
1236/* END_CASE */
1237
1238/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001239void export_after_destroy_key( data_t *data, int type_arg )
1240{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001241 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001242 psa_key_type_t type = type_arg;
1243 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001244 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001245 psa_algorithm_t alg = PSA_ALG_CTR;
1246 unsigned char *exported = NULL;
1247 size_t export_size = 0;
1248 size_t exported_length = INVALID_EXPORT_LENGTH;
1249
Gilles Peskine8817f612018-12-18 00:18:46 +01001250 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001251
Gilles Peskine8817f612018-12-18 00:18:46 +01001252 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1253 &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001254 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001255 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001256 export_size = (ptrdiff_t) data->len;
1257 ASSERT_ALLOC( exported, export_size );
1258
1259 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001260 PSA_ASSERT( psa_import_key( handle, type,
1261 data->x, data->len ) );
Moran Peker34550092018-11-07 16:19:34 +02001262
Gilles Peskine8817f612018-12-18 00:18:46 +01001263 PSA_ASSERT( psa_export_key( handle, exported, export_size,
1264 &exported_length ) );
Moran Peker34550092018-11-07 16:19:34 +02001265
1266 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001267 PSA_ASSERT( psa_destroy_key( handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001268
1269 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001270 status = psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001271 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001272 TEST_EQUAL( status, PSA_ERROR_INVALID_HANDLE );
Moran Peker34550092018-11-07 16:19:34 +02001273
1274exit:
1275 mbedtls_free( exported );
1276 mbedtls_psa_crypto_free( );
1277}
1278/* END_CASE */
1279
1280/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001281void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001282 int type_arg,
1283 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001284 int export_size_delta,
1285 int expected_export_status_arg,
1286 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001287{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001288 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001289 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001290 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001291 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001292 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001293 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001294 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001295 size_t exported_length = INVALID_EXPORT_LENGTH;
Jaeden Amero70261c52019-01-04 11:47:20 +00001296 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001297
Gilles Peskine8817f612018-12-18 00:18:46 +01001298 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001299
Gilles Peskine8817f612018-12-18 00:18:46 +01001300 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1301 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001302 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001303 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001304
1305 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001306 PSA_ASSERT( psa_import_key( handle, type,
1307 data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001308
Gilles Peskine49c25912018-10-29 15:15:31 +01001309 /* Export the public key */
1310 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001311 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001312 exported, export_size,
1313 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001314 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001315 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001316 {
1317 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1318 size_t bits;
Gilles Peskine8817f612018-12-18 00:18:46 +01001319 PSA_ASSERT( psa_get_key_information( handle, NULL, &bits ) );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001320 TEST_ASSERT( expected_public_key->len <=
1321 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001322 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1323 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001324 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001325
1326exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001327 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001328 psa_destroy_key( handle );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001329 mbedtls_psa_crypto_free( );
1330}
1331/* END_CASE */
1332
Gilles Peskine20035e32018-02-03 22:44:14 +01001333/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001334void import_and_exercise_key( data_t *data,
1335 int type_arg,
1336 int bits_arg,
1337 int alg_arg )
1338{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001339 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001340 psa_key_type_t type = type_arg;
1341 size_t bits = bits_arg;
1342 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001343 psa_key_usage_t usage = usage_to_exercise( type, alg );
Jaeden Amero70261c52019-01-04 11:47:20 +00001344 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001345 psa_key_type_t got_type;
1346 size_t got_bits;
1347 psa_status_t status;
1348
Gilles Peskine8817f612018-12-18 00:18:46 +01001349 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001350
Gilles Peskine8817f612018-12-18 00:18:46 +01001351 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1352 &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001353 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001354 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001355
1356 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001357 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01001358 PSA_ASSERT( status );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001359
1360 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001361 PSA_ASSERT( psa_get_key_information( handle,
1362 &got_type,
1363 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001364 TEST_EQUAL( got_type, type );
1365 TEST_EQUAL( got_bits, bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001366
1367 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001368 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001369 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001370
1371exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001372 psa_destroy_key( handle );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001373 mbedtls_psa_crypto_free( );
1374}
1375/* END_CASE */
1376
1377/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001378void key_policy( int usage_arg, int alg_arg )
1379{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001380 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001381 psa_algorithm_t alg = alg_arg;
1382 psa_key_usage_t usage = usage_arg;
1383 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1384 unsigned char key[32] = {0};
Jaeden Amero70261c52019-01-04 11:47:20 +00001385 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
1386 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001387
1388 memset( key, 0x2a, sizeof( key ) );
1389
Gilles Peskine8817f612018-12-18 00:18:46 +01001390 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001391
Gilles Peskine8817f612018-12-18 00:18:46 +01001392 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( sizeof( key ) ),
1393 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001394 psa_key_policy_set_usage( &policy_set, usage, alg );
1395
Gilles Peskinefe11b722018-12-18 00:24:04 +01001396 TEST_EQUAL( psa_key_policy_get_usage( &policy_set ), usage );
1397 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_set ), alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001398 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001399
Gilles Peskine8817f612018-12-18 00:18:46 +01001400 PSA_ASSERT( psa_import_key( handle, key_type,
1401 key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001402
Gilles Peskine8817f612018-12-18 00:18:46 +01001403 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001404
Gilles Peskinefe11b722018-12-18 00:24:04 +01001405 TEST_EQUAL( policy_get.usage, policy_set.usage );
1406 TEST_EQUAL( policy_get.alg, policy_set.alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001407
1408exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001409 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001410 mbedtls_psa_crypto_free( );
1411}
1412/* END_CASE */
1413
1414/* BEGIN_CASE */
Jaeden Amero70261c52019-01-04 11:47:20 +00001415void key_policy_init( )
1416{
1417 /* Test each valid way of initializing the object, except for `= {0}`, as
1418 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1419 * though it's OK by the C standard. We could test for this, but we'd need
1420 * to supress the Clang warning for the test. */
1421 psa_key_policy_t func = psa_key_policy_init( );
1422 psa_key_policy_t init = PSA_KEY_POLICY_INIT;
1423 psa_key_policy_t zero;
1424
1425 memset( &zero, 0, sizeof( zero ) );
1426
1427 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1428 * specification, we test that all valid ways of initializing the object
1429 * have the same bit pattern. This is a stronger requirement that may not
1430 * be valid on all platforms or PSA Crypto implementations, but implies the
1431 * weaker actual requirement is met: that a freshly initialized object, no
1432 * matter how it was initialized, acts the same as any other valid
1433 * initialization. */
1434 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1435 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1436}
1437/* END_CASE */
1438
1439/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001440void mac_key_policy( int policy_usage,
1441 int policy_alg,
1442 int key_type,
1443 data_t *key_data,
1444 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001445{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001446 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001447 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001448 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001449 psa_status_t status;
1450 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001451
Gilles Peskine8817f612018-12-18 00:18:46 +01001452 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001453
Gilles Peskine8817f612018-12-18 00:18:46 +01001454 PSA_ASSERT( psa_allocate_key( key_type,
1455 KEY_BITS_FROM_DATA( key_type, key_data ),
1456 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001457 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001458 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001459
Gilles Peskine8817f612018-12-18 00:18:46 +01001460 PSA_ASSERT( psa_import_key( handle, key_type,
1461 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001462
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001463 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001464 if( policy_alg == exercise_alg &&
1465 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001466 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001467 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001468 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001469 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001470
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001471 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001472 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001473 if( policy_alg == exercise_alg &&
1474 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001475 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001476 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001477 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001478
1479exit:
1480 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001481 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001482 mbedtls_psa_crypto_free( );
1483}
1484/* END_CASE */
1485
1486/* BEGIN_CASE */
1487void cipher_key_policy( int policy_usage,
1488 int policy_alg,
1489 int key_type,
1490 data_t *key_data,
1491 int exercise_alg )
1492{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001493 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001494 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001495 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001496 psa_status_t status;
1497
Gilles Peskine8817f612018-12-18 00:18:46 +01001498 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001499
Gilles Peskine8817f612018-12-18 00:18:46 +01001500 PSA_ASSERT( psa_allocate_key( key_type,
1501 KEY_BITS_FROM_DATA( key_type, key_data ),
1502 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001503 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001504 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001505
Gilles Peskine8817f612018-12-18 00:18:46 +01001506 PSA_ASSERT( psa_import_key( handle, key_type,
1507 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001508
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001509 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001510 if( policy_alg == exercise_alg &&
1511 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001512 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001513 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001514 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001515 psa_cipher_abort( &operation );
1516
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001517 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001518 if( policy_alg == exercise_alg &&
1519 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001520 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001521 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001522 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001523
1524exit:
1525 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001526 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001527 mbedtls_psa_crypto_free( );
1528}
1529/* END_CASE */
1530
1531/* BEGIN_CASE */
1532void aead_key_policy( int policy_usage,
1533 int policy_alg,
1534 int key_type,
1535 data_t *key_data,
1536 int nonce_length_arg,
1537 int tag_length_arg,
1538 int exercise_alg )
1539{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001540 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001541 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001542 psa_status_t status;
1543 unsigned char nonce[16] = {0};
1544 size_t nonce_length = nonce_length_arg;
1545 unsigned char tag[16];
1546 size_t tag_length = tag_length_arg;
1547 size_t output_length;
1548
1549 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1550 TEST_ASSERT( tag_length <= sizeof( tag ) );
1551
Gilles Peskine8817f612018-12-18 00:18:46 +01001552 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001553
Gilles Peskine8817f612018-12-18 00:18:46 +01001554 PSA_ASSERT( psa_allocate_key( key_type,
1555 KEY_BITS_FROM_DATA( key_type, key_data ),
1556 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001557 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001558 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001559
Gilles Peskine8817f612018-12-18 00:18:46 +01001560 PSA_ASSERT( psa_import_key( handle, key_type,
1561 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001562
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001563 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001564 nonce, nonce_length,
1565 NULL, 0,
1566 NULL, 0,
1567 tag, tag_length,
1568 &output_length );
1569 if( policy_alg == exercise_alg &&
1570 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001571 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001572 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001573 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001574
1575 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001576 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001577 nonce, nonce_length,
1578 NULL, 0,
1579 tag, tag_length,
1580 NULL, 0,
1581 &output_length );
1582 if( policy_alg == exercise_alg &&
1583 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001584 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001585 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001586 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001587
1588exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001589 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001590 mbedtls_psa_crypto_free( );
1591}
1592/* END_CASE */
1593
1594/* BEGIN_CASE */
1595void asymmetric_encryption_key_policy( int policy_usage,
1596 int policy_alg,
1597 int key_type,
1598 data_t *key_data,
1599 int exercise_alg )
1600{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001601 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001602 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001603 psa_status_t status;
1604 size_t key_bits;
1605 size_t buffer_length;
1606 unsigned char *buffer = NULL;
1607 size_t output_length;
1608
Gilles Peskine8817f612018-12-18 00:18:46 +01001609 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001610
Gilles Peskine8817f612018-12-18 00:18:46 +01001611 PSA_ASSERT( psa_allocate_key( key_type,
1612 KEY_BITS_FROM_DATA( key_type, key_data ),
1613 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001614 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001615 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001616
Gilles Peskine8817f612018-12-18 00:18:46 +01001617 PSA_ASSERT( psa_import_key( handle, key_type,
1618 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001619
Gilles Peskine8817f612018-12-18 00:18:46 +01001620 PSA_ASSERT( psa_get_key_information( handle,
1621 NULL,
1622 &key_bits ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001623 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1624 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001625 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001626
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001627 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001628 NULL, 0,
1629 NULL, 0,
1630 buffer, buffer_length,
1631 &output_length );
1632 if( policy_alg == exercise_alg &&
1633 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001634 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001635 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001636 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001637
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001638 if( buffer_length != 0 )
1639 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001640 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001641 buffer, buffer_length,
1642 NULL, 0,
1643 buffer, buffer_length,
1644 &output_length );
1645 if( policy_alg == exercise_alg &&
1646 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001647 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001648 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001649 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001650
1651exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001652 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001653 mbedtls_psa_crypto_free( );
1654 mbedtls_free( buffer );
1655}
1656/* END_CASE */
1657
1658/* BEGIN_CASE */
1659void asymmetric_signature_key_policy( int policy_usage,
1660 int policy_alg,
1661 int key_type,
1662 data_t *key_data,
1663 int exercise_alg )
1664{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001665 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001666 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001667 psa_status_t status;
1668 unsigned char payload[16] = {1};
1669 size_t payload_length = sizeof( payload );
1670 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1671 size_t signature_length;
1672
Gilles Peskine8817f612018-12-18 00:18:46 +01001673 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001674
Gilles Peskine8817f612018-12-18 00:18:46 +01001675 PSA_ASSERT( psa_allocate_key( key_type,
1676 KEY_BITS_FROM_DATA( key_type, key_data ),
1677 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001678 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001679 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001680
Gilles Peskine8817f612018-12-18 00:18:46 +01001681 PSA_ASSERT( psa_import_key( handle, key_type,
1682 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001683
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001684 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001685 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001686 signature, sizeof( signature ),
1687 &signature_length );
1688 if( policy_alg == exercise_alg &&
1689 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001690 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001691 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001692 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001693
1694 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001695 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001696 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001697 signature, sizeof( signature ) );
1698 if( policy_alg == exercise_alg &&
1699 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001700 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001701 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001702 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001703
1704exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001705 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001706 mbedtls_psa_crypto_free( );
1707}
1708/* END_CASE */
1709
1710/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001711void derive_key_policy( int policy_usage,
1712 int policy_alg,
1713 int key_type,
1714 data_t *key_data,
1715 int exercise_alg )
1716{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001717 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001718 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001719 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1720 psa_status_t status;
1721
Gilles Peskine8817f612018-12-18 00:18:46 +01001722 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001723
Gilles Peskine8817f612018-12-18 00:18:46 +01001724 PSA_ASSERT( psa_allocate_key( key_type,
1725 KEY_BITS_FROM_DATA( key_type, key_data ),
1726 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001727 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001728 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001729
Gilles Peskine8817f612018-12-18 00:18:46 +01001730 PSA_ASSERT( psa_import_key( handle, key_type,
1731 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001732
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001733 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001734 exercise_alg,
1735 NULL, 0,
1736 NULL, 0,
1737 1 );
1738 if( policy_alg == exercise_alg &&
1739 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001740 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001741 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001742 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001743
1744exit:
1745 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001746 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001747 mbedtls_psa_crypto_free( );
1748}
1749/* END_CASE */
1750
1751/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001752void agreement_key_policy( int policy_usage,
1753 int policy_alg,
1754 int key_type_arg,
1755 data_t *key_data,
1756 int exercise_alg )
1757{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001758 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001759 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001760 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001761 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1762 psa_status_t status;
1763
Gilles Peskine8817f612018-12-18 00:18:46 +01001764 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001765
Gilles Peskine8817f612018-12-18 00:18:46 +01001766 PSA_ASSERT( psa_allocate_key( key_type,
1767 KEY_BITS_FROM_DATA( key_type, key_data ),
1768 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001769 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001770 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001771
Gilles Peskine8817f612018-12-18 00:18:46 +01001772 PSA_ASSERT( psa_import_key( handle, key_type,
1773 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001774
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001775 status = key_agreement_with_self( &generator, handle, exercise_alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001776
Gilles Peskine01d718c2018-09-18 12:01:02 +02001777 if( policy_alg == exercise_alg &&
1778 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001779 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001780 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001781 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001782
1783exit:
1784 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001785 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001786 mbedtls_psa_crypto_free( );
1787}
1788/* END_CASE */
1789
1790/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001791void hash_operation_init( )
1792{
1793 /* Test each valid way of initializing the object, except for `= {0}`, as
1794 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1795 * though it's OK by the C standard. We could test for this, but we'd need
1796 * to supress the Clang warning for the test. */
1797 psa_hash_operation_t func = psa_hash_operation_init( );
1798 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1799 psa_hash_operation_t zero;
1800
1801 memset( &zero, 0, sizeof( zero ) );
1802
1803 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1804 * specification, we test that all valid ways of initializing the object
1805 * have the same bit pattern. This is a stronger requirement that may not
1806 * be valid on all platforms or PSA Crypto implementations, but implies the
1807 * weaker actual requirement is met: that a freshly initialized object, no
1808 * matter how it was initialized, acts the same as any other valid
1809 * initialization. */
1810 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1811 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1812}
1813/* END_CASE */
1814
1815/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001816void hash_setup( int alg_arg,
1817 int expected_status_arg )
1818{
1819 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001820 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001821 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001822 psa_status_t status;
1823
Gilles Peskine8817f612018-12-18 00:18:46 +01001824 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001825
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001826 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001827 psa_hash_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001828 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001829
1830exit:
1831 mbedtls_psa_crypto_free( );
1832}
1833/* END_CASE */
1834
1835/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02001836void hash_bad_order( )
1837{
1838 unsigned char input[] = "";
1839 /* SHA-256 hash of an empty string */
1840 unsigned char hash[] = {
1841 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1842 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1843 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
1844 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001845 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001846
Gilles Peskine8817f612018-12-18 00:18:46 +01001847 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001848
1849 /* psa_hash_update without calling psa_hash_setup beforehand */
1850 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001851 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001852 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001853
1854 /* psa_hash_verify without calling psa_hash_setup beforehand */
1855 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001856 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001857 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001858
1859 /* psa_hash_finish without calling psa_hash_setup beforehand */
1860 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001861 TEST_EQUAL( psa_hash_finish( &operation,
1862 hash, sizeof( hash ), &hash_len ),
1863 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001864
1865exit:
1866 mbedtls_psa_crypto_free( );
1867}
1868/* END_CASE */
1869
itayzafrir27e69452018-11-01 14:26:34 +02001870/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1871void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001872{
1873 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001874 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1875 * appended to it */
1876 unsigned char hash[] = {
1877 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1878 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1879 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03001880 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001881 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001882
Gilles Peskine8817f612018-12-18 00:18:46 +01001883 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001884
itayzafrir27e69452018-11-01 14:26:34 +02001885 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001886 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001887 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001888 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001889
itayzafrir27e69452018-11-01 14:26:34 +02001890 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001891 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001892 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001893 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001894
itayzafrir27e69452018-11-01 14:26:34 +02001895 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001896 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001897 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001898 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001899
itayzafrirec93d302018-10-18 18:01:10 +03001900exit:
1901 mbedtls_psa_crypto_free( );
1902}
1903/* END_CASE */
1904
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001905/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1906void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001907{
1908 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001909 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03001910 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001911 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001912 size_t hash_len;
1913
Gilles Peskine8817f612018-12-18 00:18:46 +01001914 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001915
itayzafrir58028322018-10-25 10:22:01 +03001916 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001917 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001918 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001919 hash, expected_size - 1, &hash_len ),
1920 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001921
1922exit:
1923 mbedtls_psa_crypto_free( );
1924}
1925/* END_CASE */
1926
1927/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001928void mac_operation_init( )
1929{
1930 /* Test each valid way of initializing the object, except for `= {0}`, as
1931 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1932 * though it's OK by the C standard. We could test for this, but we'd need
1933 * to supress the Clang warning for the test. */
1934 psa_mac_operation_t func = psa_mac_operation_init( );
1935 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1936 psa_mac_operation_t zero;
1937
1938 memset( &zero, 0, sizeof( zero ) );
1939
1940 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1941 * specification, we test that all valid ways of initializing the object
1942 * have the same bit pattern. This is a stronger requirement that may not
1943 * be valid on all platforms or PSA Crypto implementations, but implies the
1944 * weaker actual requirement is met: that a freshly initialized object, no
1945 * matter how it was initialized, acts the same as any other valid
1946 * initialization. */
1947 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1948 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1949}
1950/* END_CASE */
1951
1952/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001953void mac_setup( int key_type_arg,
1954 data_t *key,
1955 int alg_arg,
1956 int expected_status_arg )
1957{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001958 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001959 psa_key_type_t key_type = key_type_arg;
1960 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001961 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001962 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00001963 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001964 psa_status_t status;
1965
Gilles Peskine8817f612018-12-18 00:18:46 +01001966 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001967
Gilles Peskine8817f612018-12-18 00:18:46 +01001968 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
1969 &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001970 psa_key_policy_set_usage( &policy,
1971 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1972 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001973 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001974
Gilles Peskine8817f612018-12-18 00:18:46 +01001975 PSA_ASSERT( psa_import_key( handle, key_type,
1976 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001977
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001978 status = psa_mac_sign_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001979 psa_mac_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001980 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001981
1982exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001983 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001984 mbedtls_psa_crypto_free( );
1985}
1986/* END_CASE */
1987
1988/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001989void mac_sign( int key_type_arg,
1990 data_t *key,
1991 int alg_arg,
1992 data_t *input,
1993 data_t *expected_mac )
1994{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001995 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001996 psa_key_type_t key_type = key_type_arg;
1997 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001998 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00001999 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002000 /* Leave a little extra room in the output buffer. At the end of the
2001 * test, we'll check that the implementation didn't overwrite onto
2002 * this extra room. */
2003 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2004 size_t mac_buffer_size =
2005 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2006 size_t mac_length = 0;
2007
2008 memset( actual_mac, '+', sizeof( actual_mac ) );
2009 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2010 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2011
Gilles Peskine8817f612018-12-18 00:18:46 +01002012 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002013
Gilles Peskine8817f612018-12-18 00:18:46 +01002014 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2015 &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002016 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002017 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002018
Gilles Peskine8817f612018-12-18 00:18:46 +01002019 PSA_ASSERT( psa_import_key( handle, key_type,
2020 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002021
2022 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002023 PSA_ASSERT( psa_mac_sign_setup( &operation,
2024 handle, alg ) );
2025 PSA_ASSERT( psa_mac_update( &operation,
2026 input->x, input->len ) );
2027 PSA_ASSERT( psa_mac_sign_finish( &operation,
2028 actual_mac, mac_buffer_size,
2029 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002030
2031 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002032 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2033 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002034
2035 /* Verify that the end of the buffer is untouched. */
2036 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2037 sizeof( actual_mac ) - mac_length ) );
2038
2039exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002040 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002041 mbedtls_psa_crypto_free( );
2042}
2043/* END_CASE */
2044
2045/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002046void mac_verify( int key_type_arg,
2047 data_t *key,
2048 int alg_arg,
2049 data_t *input,
2050 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002051{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002052 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002053 psa_key_type_t key_type = key_type_arg;
2054 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002055 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002056 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002057
Gilles Peskine69c12672018-06-28 00:07:19 +02002058 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2059
Gilles Peskine8817f612018-12-18 00:18:46 +01002060 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002061
Gilles Peskine8817f612018-12-18 00:18:46 +01002062 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2063 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002064 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002065 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad16036df908f2018-04-02 08:34:15 -07002066
Gilles Peskine8817f612018-12-18 00:18:46 +01002067 PSA_ASSERT( psa_import_key( handle, key_type,
2068 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002069
Gilles Peskine8817f612018-12-18 00:18:46 +01002070 PSA_ASSERT( psa_mac_verify_setup( &operation,
2071 handle, alg ) );
2072 PSA_ASSERT( psa_destroy_key( handle ) );
2073 PSA_ASSERT( psa_mac_update( &operation,
2074 input->x, input->len ) );
2075 PSA_ASSERT( psa_mac_verify_finish( &operation,
2076 expected_mac->x,
2077 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002078
2079exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002080 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002081 mbedtls_psa_crypto_free( );
2082}
2083/* END_CASE */
2084
2085/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002086void cipher_operation_init( )
2087{
2088 /* Test each valid way of initializing the object, except for `= {0}`, as
2089 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2090 * though it's OK by the C standard. We could test for this, but we'd need
2091 * to supress the Clang warning for the test. */
2092 psa_cipher_operation_t func = psa_cipher_operation_init( );
2093 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2094 psa_cipher_operation_t zero;
2095
2096 memset( &zero, 0, sizeof( zero ) );
2097
2098 /* Although not technically guaranteed by the C standard nor the PSA Crypto
2099 * specification, we test that all valid ways of initializing the object
2100 * have the same bit pattern. This is a stronger requirement that may not
2101 * be valid on all platforms or PSA Crypto implementations, but implies the
2102 * weaker actual requirement is met: that a freshly initialized object, no
2103 * matter how it was initialized, acts the same as any other valid
2104 * initialization. */
2105 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
2106 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
2107}
2108/* END_CASE */
2109
2110/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002111void cipher_setup( int key_type_arg,
2112 data_t *key,
2113 int alg_arg,
2114 int expected_status_arg )
2115{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002116 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002117 psa_key_type_t key_type = key_type_arg;
2118 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002119 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002120 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002121 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002122 psa_status_t status;
2123
Gilles Peskine8817f612018-12-18 00:18:46 +01002124 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002125
Gilles Peskine8817f612018-12-18 00:18:46 +01002126 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2127 &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002128 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002129 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002130
Gilles Peskine8817f612018-12-18 00:18:46 +01002131 PSA_ASSERT( psa_import_key( handle, key_type,
2132 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002133
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002134 status = psa_cipher_encrypt_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002135 psa_cipher_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002136 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002137
2138exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002139 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002140 mbedtls_psa_crypto_free( );
2141}
2142/* END_CASE */
2143
2144/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002145void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002146 data_t *key,
2147 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002148 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002149{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002150 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002151 psa_status_t status;
2152 psa_key_type_t key_type = key_type_arg;
2153 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002154 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002155 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002156 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002157 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002158 size_t output_buffer_size = 0;
2159 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002160 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002161 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002162 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002163
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002164 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2165 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002166
Gilles Peskine8817f612018-12-18 00:18:46 +01002167 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002168
Gilles Peskine8817f612018-12-18 00:18:46 +01002169 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2170 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002171 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002172 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002173
Gilles Peskine8817f612018-12-18 00:18:46 +01002174 PSA_ASSERT( psa_import_key( handle, key_type,
2175 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002176
Gilles Peskine8817f612018-12-18 00:18:46 +01002177 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2178 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002179
Gilles Peskine8817f612018-12-18 00:18:46 +01002180 PSA_ASSERT( psa_cipher_set_iv( &operation,
2181 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002182 output_buffer_size = ( (size_t) input->len +
2183 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002184 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002185
Gilles Peskine8817f612018-12-18 00:18:46 +01002186 PSA_ASSERT( psa_cipher_update( &operation,
2187 input->x, input->len,
2188 output, output_buffer_size,
2189 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002190 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002191 status = psa_cipher_finish( &operation,
2192 output + function_output_length,
2193 output_buffer_size,
2194 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002195 total_output_length += function_output_length;
2196
Gilles Peskinefe11b722018-12-18 00:24:04 +01002197 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002198 if( expected_status == PSA_SUCCESS )
2199 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002200 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002201 ASSERT_COMPARE( expected_output->x, expected_output->len,
2202 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002203 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002204
Gilles Peskine50e586b2018-06-08 14:28:46 +02002205exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002206 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002207 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002208 mbedtls_psa_crypto_free( );
2209}
2210/* END_CASE */
2211
2212/* BEGIN_CASE */
2213void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002214 data_t *key,
2215 data_t *input,
2216 int first_part_size,
2217 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002218{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002219 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002220 psa_key_type_t key_type = key_type_arg;
2221 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002222 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002223 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002224 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002225 size_t output_buffer_size = 0;
2226 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002227 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002228 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002229 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002230
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002231 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2232 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002233
Gilles Peskine8817f612018-12-18 00:18:46 +01002234 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002235
Gilles Peskine8817f612018-12-18 00:18:46 +01002236 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2237 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002238 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002239 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002240
Gilles Peskine8817f612018-12-18 00:18:46 +01002241 PSA_ASSERT( psa_import_key( handle, key_type,
2242 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002243
Gilles Peskine8817f612018-12-18 00:18:46 +01002244 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2245 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002246
Gilles Peskine8817f612018-12-18 00:18:46 +01002247 PSA_ASSERT( psa_cipher_set_iv( &operation,
2248 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002249 output_buffer_size = ( (size_t) input->len +
2250 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002251 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002252
Gilles Peskine4abf7412018-06-18 16:35:34 +02002253 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002254 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2255 output, output_buffer_size,
2256 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002257 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002258 PSA_ASSERT( psa_cipher_update( &operation,
2259 input->x + first_part_size,
2260 input->len - first_part_size,
2261 output, output_buffer_size,
2262 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002263 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002264 PSA_ASSERT( psa_cipher_finish( &operation,
2265 output + function_output_length,
2266 output_buffer_size,
2267 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002268 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002269 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002270
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002271 ASSERT_COMPARE( expected_output->x, expected_output->len,
2272 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002273
2274exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002275 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002276 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002277 mbedtls_psa_crypto_free( );
2278}
2279/* END_CASE */
2280
2281/* BEGIN_CASE */
2282void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002283 data_t *key,
2284 data_t *input,
2285 int first_part_size,
2286 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002287{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002288 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002289
2290 psa_key_type_t key_type = key_type_arg;
2291 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002292 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002293 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002294 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002295 size_t output_buffer_size = 0;
2296 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002297 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002298 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002299 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002300
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002301 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2302 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002303
Gilles Peskine8817f612018-12-18 00:18:46 +01002304 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002305
Gilles Peskine8817f612018-12-18 00:18:46 +01002306 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2307 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002308 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002309 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002310
Gilles Peskine8817f612018-12-18 00:18:46 +01002311 PSA_ASSERT( psa_import_key( handle, key_type,
2312 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002313
Gilles Peskine8817f612018-12-18 00:18:46 +01002314 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2315 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002316
Gilles Peskine8817f612018-12-18 00:18:46 +01002317 PSA_ASSERT( psa_cipher_set_iv( &operation,
2318 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002319
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002320 output_buffer_size = ( (size_t) input->len +
2321 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002322 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002323
Gilles Peskine4abf7412018-06-18 16:35:34 +02002324 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002325 PSA_ASSERT( psa_cipher_update( &operation,
2326 input->x, first_part_size,
2327 output, output_buffer_size,
2328 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002329 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002330 PSA_ASSERT( psa_cipher_update( &operation,
2331 input->x + first_part_size,
2332 input->len - first_part_size,
2333 output, output_buffer_size,
2334 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002335 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002336 PSA_ASSERT( psa_cipher_finish( &operation,
2337 output + function_output_length,
2338 output_buffer_size,
2339 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002340 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002341 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002342
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002343 ASSERT_COMPARE( expected_output->x, expected_output->len,
2344 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002345
2346exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002347 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002348 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002349 mbedtls_psa_crypto_free( );
2350}
2351/* END_CASE */
2352
Gilles Peskine50e586b2018-06-08 14:28:46 +02002353/* BEGIN_CASE */
2354void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002355 data_t *key,
2356 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002357 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002358{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002359 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002360 psa_status_t status;
2361 psa_key_type_t key_type = key_type_arg;
2362 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002363 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002364 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002365 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002366 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002367 size_t output_buffer_size = 0;
2368 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002369 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002370 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002371 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002372
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002373 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2374 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002375
Gilles Peskine8817f612018-12-18 00:18:46 +01002376 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002377
Gilles Peskine8817f612018-12-18 00:18:46 +01002378 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2379 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002380 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002381 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002382
Gilles Peskine8817f612018-12-18 00:18:46 +01002383 PSA_ASSERT( psa_import_key( handle, key_type,
2384 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002385
Gilles Peskine8817f612018-12-18 00:18:46 +01002386 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2387 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002388
Gilles Peskine8817f612018-12-18 00:18:46 +01002389 PSA_ASSERT( psa_cipher_set_iv( &operation,
2390 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002391
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002392 output_buffer_size = ( (size_t) input->len +
2393 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002394 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002395
Gilles Peskine8817f612018-12-18 00:18:46 +01002396 PSA_ASSERT( psa_cipher_update( &operation,
2397 input->x, input->len,
2398 output, output_buffer_size,
2399 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002400 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002401 status = psa_cipher_finish( &operation,
2402 output + function_output_length,
2403 output_buffer_size,
2404 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002405 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002406 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002407
2408 if( expected_status == PSA_SUCCESS )
2409 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002410 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002411 ASSERT_COMPARE( expected_output->x, expected_output->len,
2412 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002413 }
2414
Gilles Peskine50e586b2018-06-08 14:28:46 +02002415exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002416 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002417 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002418 mbedtls_psa_crypto_free( );
2419}
2420/* END_CASE */
2421
Gilles Peskine50e586b2018-06-08 14:28:46 +02002422/* BEGIN_CASE */
2423void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002424 data_t *key,
2425 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002426{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002427 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002428 psa_key_type_t key_type = key_type_arg;
2429 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002430 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002431 size_t iv_size = 16;
2432 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002433 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002434 size_t output1_size = 0;
2435 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002436 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002437 size_t output2_size = 0;
2438 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002439 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002440 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2441 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002442 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002443
Gilles Peskine8817f612018-12-18 00:18:46 +01002444 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002445
Gilles Peskine8817f612018-12-18 00:18:46 +01002446 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2447 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002448 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002449 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002450
Gilles Peskine8817f612018-12-18 00:18:46 +01002451 PSA_ASSERT( psa_import_key( handle, key_type,
2452 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002453
Gilles Peskine8817f612018-12-18 00:18:46 +01002454 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2455 handle, alg ) );
2456 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2457 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002458
Gilles Peskine8817f612018-12-18 00:18:46 +01002459 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2460 iv, iv_size,
2461 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002462 output1_size = ( (size_t) input->len +
2463 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002464 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002465
Gilles Peskine8817f612018-12-18 00:18:46 +01002466 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2467 output1, output1_size,
2468 &output1_length ) );
2469 PSA_ASSERT( psa_cipher_finish( &operation1,
2470 output1 + output1_length, output1_size,
2471 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002472
Gilles Peskine048b7f02018-06-08 14:20:49 +02002473 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002474
Gilles Peskine8817f612018-12-18 00:18:46 +01002475 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002476
2477 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002478 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002479
Gilles Peskine8817f612018-12-18 00:18:46 +01002480 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2481 iv, iv_length ) );
2482 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2483 output2, output2_size,
2484 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002485 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002486 PSA_ASSERT( psa_cipher_finish( &operation2,
2487 output2 + output2_length,
2488 output2_size,
2489 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002490
Gilles Peskine048b7f02018-06-08 14:20:49 +02002491 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002492
Gilles Peskine8817f612018-12-18 00:18:46 +01002493 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002494
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002495 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002496
2497exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002498 mbedtls_free( output1 );
2499 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002500 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03002501 mbedtls_psa_crypto_free( );
2502}
2503/* END_CASE */
2504
2505/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002506void cipher_verify_output_multipart( int alg_arg,
2507 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002508 data_t *key,
2509 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002510 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002511{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002512 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002513 psa_key_type_t key_type = key_type_arg;
2514 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002515 unsigned char iv[16] = {0};
2516 size_t iv_size = 16;
2517 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002518 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002519 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002520 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002521 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002522 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002523 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002524 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002525 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2526 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002527 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002528
Gilles Peskine8817f612018-12-18 00:18:46 +01002529 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002530
Gilles Peskine8817f612018-12-18 00:18:46 +01002531 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2532 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002533 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002534 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002535
Gilles Peskine8817f612018-12-18 00:18:46 +01002536 PSA_ASSERT( psa_import_key( handle, key_type,
2537 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03002538
Gilles Peskine8817f612018-12-18 00:18:46 +01002539 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2540 handle, alg ) );
2541 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2542 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002543
Gilles Peskine8817f612018-12-18 00:18:46 +01002544 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2545 iv, iv_size,
2546 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002547 output1_buffer_size = ( (size_t) input->len +
2548 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002549 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002550
Gilles Peskine4abf7412018-06-18 16:35:34 +02002551 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002552
Gilles Peskine8817f612018-12-18 00:18:46 +01002553 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2554 output1, output1_buffer_size,
2555 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002556 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002557
Gilles Peskine8817f612018-12-18 00:18:46 +01002558 PSA_ASSERT( psa_cipher_update( &operation1,
2559 input->x + first_part_size,
2560 input->len - first_part_size,
2561 output1, output1_buffer_size,
2562 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002563 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002564
Gilles Peskine8817f612018-12-18 00:18:46 +01002565 PSA_ASSERT( psa_cipher_finish( &operation1,
2566 output1 + output1_length,
2567 output1_buffer_size - output1_length,
2568 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002569 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002570
Gilles Peskine8817f612018-12-18 00:18:46 +01002571 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002572
Gilles Peskine048b7f02018-06-08 14:20:49 +02002573 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002574 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002575
Gilles Peskine8817f612018-12-18 00:18:46 +01002576 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2577 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002578
Gilles Peskine8817f612018-12-18 00:18:46 +01002579 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2580 output2, output2_buffer_size,
2581 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002582 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002583
Gilles Peskine8817f612018-12-18 00:18:46 +01002584 PSA_ASSERT( psa_cipher_update( &operation2,
2585 output1 + first_part_size,
2586 output1_length - first_part_size,
2587 output2, output2_buffer_size,
2588 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002589 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002590
Gilles Peskine8817f612018-12-18 00:18:46 +01002591 PSA_ASSERT( psa_cipher_finish( &operation2,
2592 output2 + output2_length,
2593 output2_buffer_size - output2_length,
2594 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002595 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002596
Gilles Peskine8817f612018-12-18 00:18:46 +01002597 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002598
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002599 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002600
2601exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002602 mbedtls_free( output1 );
2603 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002604 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002605 mbedtls_psa_crypto_free( );
2606}
2607/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002608
Gilles Peskine20035e32018-02-03 22:44:14 +01002609/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002610void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002611 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002612 data_t *nonce,
2613 data_t *additional_data,
2614 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002615 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002616{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002617 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002618 psa_key_type_t key_type = key_type_arg;
2619 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002620 unsigned char *output_data = NULL;
2621 size_t output_size = 0;
2622 size_t output_length = 0;
2623 unsigned char *output_data2 = NULL;
2624 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002625 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002626 psa_status_t expected_result = expected_result_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002627 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002628
Gilles Peskine4abf7412018-06-18 16:35:34 +02002629 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002630 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002631
Gilles Peskine8817f612018-12-18 00:18:46 +01002632 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002633
Gilles Peskine8817f612018-12-18 00:18:46 +01002634 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2635 &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002636 psa_key_policy_set_usage( &policy,
2637 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2638 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002639 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002640
Gilles Peskine8817f612018-12-18 00:18:46 +01002641 PSA_ASSERT( psa_import_key( handle, key_type,
2642 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002643
Gilles Peskinefe11b722018-12-18 00:24:04 +01002644 TEST_EQUAL( psa_aead_encrypt( handle, alg,
2645 nonce->x, nonce->len,
2646 additional_data->x,
2647 additional_data->len,
2648 input_data->x, input_data->len,
2649 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002650 &output_length ),
2651 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002652
2653 if( PSA_SUCCESS == expected_result )
2654 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002655 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002656
Gilles Peskinefe11b722018-12-18 00:24:04 +01002657 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2658 nonce->x, nonce->len,
2659 additional_data->x,
2660 additional_data->len,
2661 output_data, output_length,
2662 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002663 &output_length2 ),
2664 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002665
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002666 ASSERT_COMPARE( input_data->x, input_data->len,
2667 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002668 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002669
Gilles Peskinea1cac842018-06-11 19:33:02 +02002670exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002671 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002672 mbedtls_free( output_data );
2673 mbedtls_free( output_data2 );
2674 mbedtls_psa_crypto_free( );
2675}
2676/* END_CASE */
2677
2678/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002679void aead_encrypt( int key_type_arg, data_t *key_data,
2680 int alg_arg,
2681 data_t *nonce,
2682 data_t *additional_data,
2683 data_t *input_data,
2684 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002685{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002686 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002687 psa_key_type_t key_type = key_type_arg;
2688 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002689 unsigned char *output_data = NULL;
2690 size_t output_size = 0;
2691 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002692 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002693 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002694
Gilles Peskine4abf7412018-06-18 16:35:34 +02002695 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002696 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002697
Gilles Peskine8817f612018-12-18 00:18:46 +01002698 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002699
Gilles Peskine8817f612018-12-18 00:18:46 +01002700 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2701 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002702 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002703 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002704
Gilles Peskine8817f612018-12-18 00:18:46 +01002705 PSA_ASSERT( psa_import_key( handle, key_type,
2706 key_data->x,
2707 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002708
Gilles Peskine8817f612018-12-18 00:18:46 +01002709 PSA_ASSERT( psa_aead_encrypt( handle, alg,
2710 nonce->x, nonce->len,
2711 additional_data->x, additional_data->len,
2712 input_data->x, input_data->len,
2713 output_data, output_size,
2714 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002715
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002716 ASSERT_COMPARE( expected_result->x, expected_result->len,
2717 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002718
Gilles Peskinea1cac842018-06-11 19:33:02 +02002719exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002720 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002721 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002722 mbedtls_psa_crypto_free( );
2723}
2724/* END_CASE */
2725
2726/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002727void aead_decrypt( int key_type_arg, data_t *key_data,
2728 int alg_arg,
2729 data_t *nonce,
2730 data_t *additional_data,
2731 data_t *input_data,
2732 data_t *expected_data,
2733 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002734{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002735 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002736 psa_key_type_t key_type = key_type_arg;
2737 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002738 unsigned char *output_data = NULL;
2739 size_t output_size = 0;
2740 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002741 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002742 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002743 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002744
Gilles Peskine4abf7412018-06-18 16:35:34 +02002745 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002746 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002747
Gilles Peskine8817f612018-12-18 00:18:46 +01002748 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002749
Gilles Peskine8817f612018-12-18 00:18:46 +01002750 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2751 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002752 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002753 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002754
Gilles Peskine8817f612018-12-18 00:18:46 +01002755 PSA_ASSERT( psa_import_key( handle, key_type,
2756 key_data->x,
2757 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002758
Gilles Peskinefe11b722018-12-18 00:24:04 +01002759 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2760 nonce->x, nonce->len,
2761 additional_data->x,
2762 additional_data->len,
2763 input_data->x, input_data->len,
2764 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002765 &output_length ),
2766 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002767
Gilles Peskine2d277862018-06-18 15:41:12 +02002768 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002769 ASSERT_COMPARE( expected_data->x, expected_data->len,
2770 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002771
Gilles Peskinea1cac842018-06-11 19:33:02 +02002772exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002773 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002774 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002775 mbedtls_psa_crypto_free( );
2776}
2777/* END_CASE */
2778
2779/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002780void signature_size( int type_arg,
2781 int bits,
2782 int alg_arg,
2783 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002784{
2785 psa_key_type_t type = type_arg;
2786 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002787 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002788 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002789exit:
2790 ;
2791}
2792/* END_CASE */
2793
2794/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002795void sign_deterministic( int key_type_arg, data_t *key_data,
2796 int alg_arg, data_t *input_data,
2797 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002798{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002799 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01002800 psa_key_type_t key_type = key_type_arg;
2801 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002802 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002803 unsigned char *signature = NULL;
2804 size_t signature_size;
2805 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002806 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01002807
Gilles Peskine8817f612018-12-18 00:18:46 +01002808 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002809
Gilles Peskine8817f612018-12-18 00:18:46 +01002810 PSA_ASSERT( psa_allocate_key( key_type,
2811 KEY_BITS_FROM_DATA( key_type, key_data ),
2812 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002813 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002814 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002815
Gilles Peskine8817f612018-12-18 00:18:46 +01002816 PSA_ASSERT( psa_import_key( handle, key_type,
2817 key_data->x,
2818 key_data->len ) );
2819 PSA_ASSERT( psa_get_key_information( handle,
2820 NULL,
2821 &key_bits ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002822
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002823 /* Allocate a buffer which has the size advertized by the
2824 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002825 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2826 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002827 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002828 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002829 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002830
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002831 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002832 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2833 input_data->x, input_data->len,
2834 signature, signature_size,
2835 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002836 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002837 ASSERT_COMPARE( output_data->x, output_data->len,
2838 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002839
2840exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002841 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01002842 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002843 mbedtls_psa_crypto_free( );
2844}
2845/* END_CASE */
2846
2847/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002848void sign_fail( int key_type_arg, data_t *key_data,
2849 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002850 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002851{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002852 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01002853 psa_key_type_t key_type = key_type_arg;
2854 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002855 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002856 psa_status_t actual_status;
2857 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002858 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002859 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002860 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01002861
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002862 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002863
Gilles Peskine8817f612018-12-18 00:18:46 +01002864 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002865
Gilles Peskine8817f612018-12-18 00:18:46 +01002866 PSA_ASSERT( psa_allocate_key( key_type,
2867 KEY_BITS_FROM_DATA( key_type, key_data ),
2868 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002869 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002870 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002871
Gilles Peskine8817f612018-12-18 00:18:46 +01002872 PSA_ASSERT( psa_import_key( handle, key_type,
2873 key_data->x,
2874 key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002875
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002876 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002877 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002878 signature, signature_size,
2879 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002880 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002881 /* The value of *signature_length is unspecified on error, but
2882 * whatever it is, it should be less than signature_size, so that
2883 * if the caller tries to read *signature_length bytes without
2884 * checking the error code then they don't overflow a buffer. */
2885 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002886
2887exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002888 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01002889 mbedtls_free( signature );
2890 mbedtls_psa_crypto_free( );
2891}
2892/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002893
2894/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002895void sign_verify( int key_type_arg, data_t *key_data,
2896 int alg_arg, data_t *input_data )
2897{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002898 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02002899 psa_key_type_t key_type = key_type_arg;
2900 psa_algorithm_t alg = alg_arg;
2901 size_t key_bits;
2902 unsigned char *signature = NULL;
2903 size_t signature_size;
2904 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002905 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02002906
Gilles Peskine8817f612018-12-18 00:18:46 +01002907 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002908
Gilles Peskine8817f612018-12-18 00:18:46 +01002909 PSA_ASSERT( psa_allocate_key( key_type,
2910 KEY_BITS_FROM_DATA( key_type, key_data ),
2911 &handle ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002912 psa_key_policy_set_usage( &policy,
2913 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2914 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002915 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002916
Gilles Peskine8817f612018-12-18 00:18:46 +01002917 PSA_ASSERT( psa_import_key( handle, key_type,
2918 key_data->x,
2919 key_data->len ) );
2920 PSA_ASSERT( psa_get_key_information( handle,
2921 NULL,
2922 &key_bits ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002923
2924 /* Allocate a buffer which has the size advertized by the
2925 * library. */
2926 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2927 key_bits, alg );
2928 TEST_ASSERT( signature_size != 0 );
2929 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002930 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002931
2932 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002933 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2934 input_data->x, input_data->len,
2935 signature, signature_size,
2936 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002937 /* Check that the signature length looks sensible. */
2938 TEST_ASSERT( signature_length <= signature_size );
2939 TEST_ASSERT( signature_length > 0 );
2940
2941 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002942 PSA_ASSERT( psa_asymmetric_verify(
2943 handle, alg,
2944 input_data->x, input_data->len,
2945 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002946
2947 if( input_data->len != 0 )
2948 {
2949 /* Flip a bit in the input and verify that the signature is now
2950 * detected as invalid. Flip a bit at the beginning, not at the end,
2951 * because ECDSA may ignore the last few bits of the input. */
2952 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002953 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
2954 input_data->x, input_data->len,
2955 signature, signature_length ),
2956 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02002957 }
2958
2959exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002960 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02002961 mbedtls_free( signature );
2962 mbedtls_psa_crypto_free( );
2963}
2964/* END_CASE */
2965
2966/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002967void asymmetric_verify( int key_type_arg, data_t *key_data,
2968 int alg_arg, data_t *hash_data,
2969 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002970{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002971 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03002972 psa_key_type_t key_type = key_type_arg;
2973 psa_algorithm_t alg = alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002974 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03002975
Gilles Peskine69c12672018-06-28 00:07:19 +02002976 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2977
Gilles Peskine8817f612018-12-18 00:18:46 +01002978 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03002979
Gilles Peskine8817f612018-12-18 00:18:46 +01002980 PSA_ASSERT( psa_allocate_key( key_type,
2981 KEY_BITS_FROM_DATA( key_type, key_data ),
2982 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002983 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002984 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
itayzafrir5c753392018-05-08 11:18:38 +03002985
Gilles Peskine8817f612018-12-18 00:18:46 +01002986 PSA_ASSERT( psa_import_key( handle, key_type,
2987 key_data->x,
2988 key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002989
Gilles Peskine8817f612018-12-18 00:18:46 +01002990 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
2991 hash_data->x, hash_data->len,
2992 signature_data->x,
2993 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03002994exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002995 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03002996 mbedtls_psa_crypto_free( );
2997}
2998/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002999
3000/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003001void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3002 int alg_arg, data_t *hash_data,
3003 data_t *signature_data,
3004 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003005{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003006 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003007 psa_key_type_t key_type = key_type_arg;
3008 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003009 psa_status_t actual_status;
3010 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003011 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003012
Gilles Peskine8817f612018-12-18 00:18:46 +01003013 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003014
Gilles Peskine8817f612018-12-18 00:18:46 +01003015 PSA_ASSERT( psa_allocate_key( key_type,
3016 KEY_BITS_FROM_DATA( key_type, key_data ),
3017 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003018 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003019 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003020
Gilles Peskine8817f612018-12-18 00:18:46 +01003021 PSA_ASSERT( psa_import_key( handle, key_type,
3022 key_data->x,
3023 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003024
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003025 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003026 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003027 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003028 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003029
Gilles Peskinefe11b722018-12-18 00:24:04 +01003030 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003031
3032exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003033 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003034 mbedtls_psa_crypto_free( );
3035}
3036/* END_CASE */
3037
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003038/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003039void asymmetric_encrypt( int key_type_arg,
3040 data_t *key_data,
3041 int alg_arg,
3042 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003043 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003044 int expected_output_length_arg,
3045 int expected_status_arg )
3046{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003047 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003048 psa_key_type_t key_type = key_type_arg;
3049 psa_algorithm_t alg = alg_arg;
3050 size_t expected_output_length = expected_output_length_arg;
3051 size_t key_bits;
3052 unsigned char *output = NULL;
3053 size_t output_size;
3054 size_t output_length = ~0;
3055 psa_status_t actual_status;
3056 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003057 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003058
Gilles Peskine8817f612018-12-18 00:18:46 +01003059 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003060
Gilles Peskine656896e2018-06-29 19:12:28 +02003061 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01003062 PSA_ASSERT( psa_allocate_key( key_type,
3063 KEY_BITS_FROM_DATA( key_type, key_data ),
3064 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003065 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003066 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
3067 PSA_ASSERT( psa_import_key( handle, key_type,
3068 key_data->x,
3069 key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003070
3071 /* Determine the maximum output length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003072 PSA_ASSERT( psa_get_key_information( handle,
3073 NULL,
3074 &key_bits ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003075 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003076 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003077
3078 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003079 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003080 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003081 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003082 output, output_size,
3083 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003084 TEST_EQUAL( actual_status, expected_status );
3085 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003086
Gilles Peskine68428122018-06-30 18:42:41 +02003087 /* If the label is empty, the test framework puts a non-null pointer
3088 * in label->x. Test that a null pointer works as well. */
3089 if( label->len == 0 )
3090 {
3091 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003092 if( output_size != 0 )
3093 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003094 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003095 input_data->x, input_data->len,
3096 NULL, label->len,
3097 output, output_size,
3098 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003099 TEST_EQUAL( actual_status, expected_status );
3100 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003101 }
3102
Gilles Peskine656896e2018-06-29 19:12:28 +02003103exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003104 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003105 mbedtls_free( output );
3106 mbedtls_psa_crypto_free( );
3107}
3108/* END_CASE */
3109
3110/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003111void asymmetric_encrypt_decrypt( int key_type_arg,
3112 data_t *key_data,
3113 int alg_arg,
3114 data_t *input_data,
3115 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003116{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003117 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003118 psa_key_type_t key_type = key_type_arg;
3119 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003120 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003121 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003122 size_t output_size;
3123 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003124 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003125 size_t output2_size;
3126 size_t output2_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003127 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003128
Gilles Peskine8817f612018-12-18 00:18:46 +01003129 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003130
Gilles Peskine8817f612018-12-18 00:18:46 +01003131 PSA_ASSERT( psa_allocate_key( key_type,
3132 KEY_BITS_FROM_DATA( key_type, key_data ),
3133 &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003134 psa_key_policy_set_usage( &policy,
3135 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003136 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003137 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003138
Gilles Peskine8817f612018-12-18 00:18:46 +01003139 PSA_ASSERT( psa_import_key( handle, key_type,
3140 key_data->x,
3141 key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003142
3143 /* Determine the maximum ciphertext length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003144 PSA_ASSERT( psa_get_key_information( handle,
3145 NULL,
3146 &key_bits ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003147 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003148 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003149 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003150 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003151
Gilles Peskineeebd7382018-06-08 18:11:54 +02003152 /* We test encryption by checking that encrypt-then-decrypt gives back
3153 * the original plaintext because of the non-optional random
3154 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003155 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3156 input_data->x, input_data->len,
3157 label->x, label->len,
3158 output, output_size,
3159 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003160 /* We don't know what ciphertext length to expect, but check that
3161 * it looks sensible. */
3162 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003163
Gilles Peskine8817f612018-12-18 00:18:46 +01003164 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3165 output, output_length,
3166 label->x, label->len,
3167 output2, output2_size,
3168 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003169 ASSERT_COMPARE( input_data->x, input_data->len,
3170 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003171
3172exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003173 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003174 mbedtls_free( output );
3175 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003176 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003177}
3178/* END_CASE */
3179
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003180/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003181void asymmetric_decrypt( int key_type_arg,
3182 data_t *key_data,
3183 int alg_arg,
3184 data_t *input_data,
3185 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003186 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003187{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003188 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003189 psa_key_type_t key_type = key_type_arg;
3190 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003191 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003192 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003193 size_t output_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003194 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003195
Gilles Peskine4abf7412018-06-18 16:35:34 +02003196 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003197 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003198
Gilles Peskine8817f612018-12-18 00:18:46 +01003199 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003200
Gilles Peskine8817f612018-12-18 00:18:46 +01003201 PSA_ASSERT( psa_allocate_key( key_type,
3202 KEY_BITS_FROM_DATA( key_type, key_data ),
3203 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003204 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003205 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003206
Gilles Peskine8817f612018-12-18 00:18:46 +01003207 PSA_ASSERT( psa_import_key( handle, key_type,
3208 key_data->x,
3209 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003210
Gilles Peskine8817f612018-12-18 00:18:46 +01003211 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3212 input_data->x, input_data->len,
3213 label->x, label->len,
3214 output,
3215 output_size,
3216 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003217 ASSERT_COMPARE( expected_data->x, expected_data->len,
3218 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003219
Gilles Peskine68428122018-06-30 18:42:41 +02003220 /* If the label is empty, the test framework puts a non-null pointer
3221 * in label->x. Test that a null pointer works as well. */
3222 if( label->len == 0 )
3223 {
3224 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003225 if( output_size != 0 )
3226 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003227 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3228 input_data->x, input_data->len,
3229 NULL, label->len,
3230 output,
3231 output_size,
3232 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003233 ASSERT_COMPARE( expected_data->x, expected_data->len,
3234 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003235 }
3236
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003237exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003238 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003239 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003240 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003241}
3242/* END_CASE */
3243
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003244/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003245void asymmetric_decrypt_fail( int key_type_arg,
3246 data_t *key_data,
3247 int alg_arg,
3248 data_t *input_data,
3249 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003250 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003251{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003252 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003253 psa_key_type_t key_type = key_type_arg;
3254 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003255 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003256 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003257 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003258 psa_status_t actual_status;
3259 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003260 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003261
Gilles Peskine4abf7412018-06-18 16:35:34 +02003262 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003263 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003264
Gilles Peskine8817f612018-12-18 00:18:46 +01003265 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003266
Gilles Peskine8817f612018-12-18 00:18:46 +01003267 PSA_ASSERT( psa_allocate_key( key_type,
3268 KEY_BITS_FROM_DATA( key_type, key_data ),
3269 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003270 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003271 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003272
Gilles Peskine8817f612018-12-18 00:18:46 +01003273 PSA_ASSERT( psa_import_key( handle, key_type,
3274 key_data->x,
3275 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003276
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003277 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003278 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003279 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003280 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003281 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003282 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003283 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003284
Gilles Peskine68428122018-06-30 18:42:41 +02003285 /* If the label is empty, the test framework puts a non-null pointer
3286 * in label->x. Test that a null pointer works as well. */
3287 if( label->len == 0 )
3288 {
3289 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003290 if( output_size != 0 )
3291 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003292 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003293 input_data->x, input_data->len,
3294 NULL, label->len,
3295 output, output_size,
3296 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003297 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003298 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003299 }
3300
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003301exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003302 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02003303 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003304 mbedtls_psa_crypto_free( );
3305}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003306/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003307
3308/* BEGIN_CASE */
Jaeden Amerod94d6712019-01-04 14:11:48 +00003309void crypto_generator_init( )
3310{
3311 /* Test each valid way of initializing the object, except for `= {0}`, as
3312 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3313 * though it's OK by the C standard. We could test for this, but we'd need
3314 * to supress the Clang warning for the test. */
3315 psa_crypto_generator_t func = psa_crypto_generator_init( );
3316 psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT;
3317 psa_crypto_generator_t zero;
3318
3319 memset( &zero, 0, sizeof( zero ) );
3320
3321 /* Although not technically guaranteed by the C standard nor the PSA Crypto
3322 * specification, we test that all valid ways of initializing the object
3323 * have the same bit pattern. This is a stronger requirement that may not
3324 * be valid on all platforms or PSA Crypto implementations, but implies the
3325 * weaker actual requirement is met: that a freshly initialized object, no
3326 * matter how it was initialized, acts the same as any other valid
3327 * initialization. */
3328 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
3329 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
3330}
3331/* END_CASE */
3332
3333/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003334void derive_setup( int key_type_arg,
3335 data_t *key_data,
3336 int alg_arg,
3337 data_t *salt,
3338 data_t *label,
3339 int requested_capacity_arg,
3340 int expected_status_arg )
3341{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003342 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003343 size_t key_type = key_type_arg;
3344 psa_algorithm_t alg = alg_arg;
3345 size_t requested_capacity = requested_capacity_arg;
3346 psa_status_t expected_status = expected_status_arg;
3347 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003348 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003349
Gilles Peskine8817f612018-12-18 00:18:46 +01003350 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003351
Gilles Peskine8817f612018-12-18 00:18:46 +01003352 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
3353 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003354 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003355 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003356
Gilles Peskine8817f612018-12-18 00:18:46 +01003357 PSA_ASSERT( psa_import_key( handle, key_type,
3358 key_data->x,
3359 key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003360
Gilles Peskinefe11b722018-12-18 00:24:04 +01003361 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3362 salt->x, salt->len,
3363 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003364 requested_capacity ),
3365 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003366
3367exit:
3368 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003369 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003370 mbedtls_psa_crypto_free( );
3371}
3372/* END_CASE */
3373
3374/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003375void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003376{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003377 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003378 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003379 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003380 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003381 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003382 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003383 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3384 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3385 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Jaeden Amero70261c52019-01-04 11:47:20 +00003386 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003387
Gilles Peskine8817f612018-12-18 00:18:46 +01003388 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003389
Gilles Peskine8817f612018-12-18 00:18:46 +01003390 PSA_ASSERT( psa_allocate_key( key_type,
3391 PSA_BYTES_TO_BITS( sizeof( key_data ) ),
3392 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003393 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003394 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003395
Gilles Peskine8817f612018-12-18 00:18:46 +01003396 PSA_ASSERT( psa_import_key( handle, key_type,
3397 key_data,
3398 sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003399
3400 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01003401 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3402 NULL, 0,
3403 NULL, 0,
3404 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003405
3406 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003407 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3408 NULL, 0,
3409 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003410 capacity ),
3411 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003412
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003413 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003414
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003415 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
3416 PSA_ERROR_INSUFFICIENT_CAPACITY );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003417
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003418exit:
3419 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003420 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003421 mbedtls_psa_crypto_free( );
3422}
3423/* END_CASE */
3424
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003425/* BEGIN_CASE */
3426void test_derive_invalid_generator_tests( )
3427{
3428 uint8_t output_buffer[16];
3429 size_t buffer_size = 16;
3430 size_t capacity = 0;
3431 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3432
Nir Sonnenschein50789302018-10-31 12:16:38 +02003433 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003434 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003435
3436 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003437 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003438
Gilles Peskine8817f612018-12-18 00:18:46 +01003439 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003440
Nir Sonnenschein50789302018-10-31 12:16:38 +02003441 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003442 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003443
Nir Sonnenschein50789302018-10-31 12:16:38 +02003444 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003445 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003446
3447exit:
3448 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003449}
3450/* END_CASE */
3451
3452/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003453void derive_output( int alg_arg,
3454 data_t *key_data,
3455 data_t *salt,
3456 data_t *label,
3457 int requested_capacity_arg,
3458 data_t *expected_output1,
3459 data_t *expected_output2 )
3460{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003461 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003462 psa_algorithm_t alg = alg_arg;
3463 size_t requested_capacity = requested_capacity_arg;
3464 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3465 uint8_t *expected_outputs[2] =
3466 {expected_output1->x, expected_output2->x};
3467 size_t output_sizes[2] =
3468 {expected_output1->len, expected_output2->len};
3469 size_t output_buffer_size = 0;
3470 uint8_t *output_buffer = NULL;
3471 size_t expected_capacity;
3472 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003473 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003474 psa_status_t status;
3475 unsigned i;
3476
3477 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3478 {
3479 if( output_sizes[i] > output_buffer_size )
3480 output_buffer_size = output_sizes[i];
3481 if( output_sizes[i] == 0 )
3482 expected_outputs[i] = NULL;
3483 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003484 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003485 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003486
Gilles Peskine8817f612018-12-18 00:18:46 +01003487 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3488 PSA_BYTES_TO_BITS( key_data->len ),
3489 &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003490 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003491 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003492
Gilles Peskine8817f612018-12-18 00:18:46 +01003493 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3494 key_data->x,
3495 key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003496
3497 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003498 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3499 salt->x, salt->len,
3500 label->x, label->len,
3501 requested_capacity ) );
3502 PSA_ASSERT( psa_get_generator_capacity( &generator,
3503 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003504 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003505 expected_capacity = requested_capacity;
3506
3507 /* Expansion phase. */
3508 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3509 {
3510 /* Read some bytes. */
3511 status = psa_generator_read( &generator,
3512 output_buffer, output_sizes[i] );
3513 if( expected_capacity == 0 && output_sizes[i] == 0 )
3514 {
3515 /* Reading 0 bytes when 0 bytes are available can go either way. */
3516 TEST_ASSERT( status == PSA_SUCCESS ||
3517 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3518 continue;
3519 }
3520 else if( expected_capacity == 0 ||
3521 output_sizes[i] > expected_capacity )
3522 {
3523 /* Capacity exceeded. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003524 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003525 expected_capacity = 0;
3526 continue;
3527 }
3528 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003529 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003530 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003531 ASSERT_COMPARE( output_buffer, output_sizes[i],
3532 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003533 /* Check the generator status. */
3534 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01003535 PSA_ASSERT( psa_get_generator_capacity( &generator,
3536 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003537 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003538 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003539 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003540
3541exit:
3542 mbedtls_free( output_buffer );
3543 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003544 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003545 mbedtls_psa_crypto_free( );
3546}
3547/* END_CASE */
3548
3549/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003550void derive_full( int alg_arg,
3551 data_t *key_data,
3552 data_t *salt,
3553 data_t *label,
3554 int requested_capacity_arg )
3555{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003556 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02003557 psa_algorithm_t alg = alg_arg;
3558 size_t requested_capacity = requested_capacity_arg;
3559 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3560 unsigned char output_buffer[16];
3561 size_t expected_capacity = requested_capacity;
3562 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003563 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02003564
Gilles Peskine8817f612018-12-18 00:18:46 +01003565 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003566
Gilles Peskine8817f612018-12-18 00:18:46 +01003567 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3568 PSA_BYTES_TO_BITS( key_data->len ),
3569 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003570 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003571 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003572
Gilles Peskine8817f612018-12-18 00:18:46 +01003573 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3574 key_data->x,
3575 key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003576
3577 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003578 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3579 salt->x, salt->len,
3580 label->x, label->len,
3581 requested_capacity ) );
3582 PSA_ASSERT( psa_get_generator_capacity( &generator,
3583 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003584 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003585
3586 /* Expansion phase. */
3587 while( current_capacity > 0 )
3588 {
3589 size_t read_size = sizeof( output_buffer );
3590 if( read_size > current_capacity )
3591 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01003592 PSA_ASSERT( psa_generator_read( &generator,
3593 output_buffer,
3594 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003595 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01003596 PSA_ASSERT( psa_get_generator_capacity( &generator,
3597 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003598 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003599 }
3600
3601 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003602 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
3603 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskined54931c2018-07-17 21:06:59 +02003604
Gilles Peskine8817f612018-12-18 00:18:46 +01003605 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003606
3607exit:
3608 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003609 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02003610 mbedtls_psa_crypto_free( );
3611}
3612/* END_CASE */
3613
3614/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003615void derive_key_exercise( int alg_arg,
3616 data_t *key_data,
3617 data_t *salt,
3618 data_t *label,
3619 int derived_type_arg,
3620 int derived_bits_arg,
3621 int derived_usage_arg,
3622 int derived_alg_arg )
3623{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003624 psa_key_handle_t base_handle = 0;
3625 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003626 psa_algorithm_t alg = alg_arg;
3627 psa_key_type_t derived_type = derived_type_arg;
3628 size_t derived_bits = derived_bits_arg;
3629 psa_key_usage_t derived_usage = derived_usage_arg;
3630 psa_algorithm_t derived_alg = derived_alg_arg;
3631 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3632 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003633 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003634 psa_key_type_t got_type;
3635 size_t got_bits;
3636
Gilles Peskine8817f612018-12-18 00:18:46 +01003637 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003638
Gilles Peskine8817f612018-12-18 00:18:46 +01003639 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3640 PSA_BYTES_TO_BITS( key_data->len ),
3641 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003642 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003643 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3644 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3645 key_data->x,
3646 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003647
3648 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003649 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3650 salt->x, salt->len,
3651 label->x, label->len,
3652 capacity ) );
3653 PSA_ASSERT( psa_allocate_key( derived_type, derived_bits,
3654 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003655 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003656 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3657 PSA_ASSERT( psa_generator_import_key( derived_handle,
3658 derived_type,
3659 derived_bits,
3660 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003661
3662 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01003663 PSA_ASSERT( psa_get_key_information( derived_handle,
3664 &got_type,
3665 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003666 TEST_EQUAL( got_type, derived_type );
3667 TEST_EQUAL( got_bits, derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003668
3669 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003670 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02003671 goto exit;
3672
3673exit:
3674 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003675 psa_destroy_key( base_handle );
3676 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003677 mbedtls_psa_crypto_free( );
3678}
3679/* END_CASE */
3680
3681/* BEGIN_CASE */
3682void derive_key_export( int alg_arg,
3683 data_t *key_data,
3684 data_t *salt,
3685 data_t *label,
3686 int bytes1_arg,
3687 int bytes2_arg )
3688{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003689 psa_key_handle_t base_handle = 0;
3690 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003691 psa_algorithm_t alg = alg_arg;
3692 size_t bytes1 = bytes1_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003693 size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003694 size_t bytes2 = bytes2_arg;
3695 size_t capacity = bytes1 + bytes2;
3696 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003697 uint8_t *output_buffer = NULL;
3698 uint8_t *export_buffer = NULL;
Jaeden Amero70261c52019-01-04 11:47:20 +00003699 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003700 size_t length;
3701
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003702 ASSERT_ALLOC( output_buffer, capacity );
3703 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01003704 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003705
Gilles Peskine8817f612018-12-18 00:18:46 +01003706 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3707 PSA_BYTES_TO_BITS( key_data->len ),
3708 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003709 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003710 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3711 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3712 key_data->x,
3713 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003714
3715 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003716 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3717 salt->x, salt->len,
3718 label->x, label->len,
3719 capacity ) );
3720 PSA_ASSERT( psa_generator_read( &generator,
3721 output_buffer,
3722 capacity ) );
3723 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003724
3725 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003726 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3727 salt->x, salt->len,
3728 label->x, label->len,
3729 capacity ) );
3730 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, derived_bits,
3731 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003732 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003733 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3734 PSA_ASSERT( psa_generator_import_key( derived_handle,
3735 PSA_KEY_TYPE_RAW_DATA,
3736 derived_bits,
3737 &generator ) );
3738 PSA_ASSERT( psa_export_key( derived_handle,
3739 export_buffer, bytes1,
3740 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003741 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003742 PSA_ASSERT( psa_destroy_key( derived_handle ) );
3743 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA,
3744 PSA_BYTES_TO_BITS( bytes2 ),
3745 &derived_handle ) );
3746 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3747 PSA_ASSERT( psa_generator_import_key( derived_handle,
3748 PSA_KEY_TYPE_RAW_DATA,
3749 PSA_BYTES_TO_BITS( bytes2 ),
3750 &generator ) );
3751 PSA_ASSERT( psa_export_key( derived_handle,
3752 export_buffer + bytes1, bytes2,
3753 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003754 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003755
3756 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003757 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
3758 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003759
3760exit:
3761 mbedtls_free( output_buffer );
3762 mbedtls_free( export_buffer );
3763 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003764 psa_destroy_key( base_handle );
3765 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003766 mbedtls_psa_crypto_free( );
3767}
3768/* END_CASE */
3769
3770/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02003771void key_agreement_setup( int alg_arg,
3772 int our_key_type_arg, data_t *our_key_data,
3773 data_t *peer_key_data,
3774 int expected_status_arg )
3775{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003776 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003777 psa_algorithm_t alg = alg_arg;
3778 psa_key_type_t our_key_type = our_key_type_arg;
3779 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003780 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003781
Gilles Peskine8817f612018-12-18 00:18:46 +01003782 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003783
Gilles Peskine8817f612018-12-18 00:18:46 +01003784 PSA_ASSERT( psa_allocate_key( our_key_type,
3785 KEY_BITS_FROM_DATA( our_key_type,
3786 our_key_data ),
3787 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003788 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003789 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3790 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3791 our_key_data->x,
3792 our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003793
Gilles Peskinefe11b722018-12-18 00:24:04 +01003794 TEST_EQUAL( psa_key_agreement( &generator,
3795 our_key,
3796 peer_key_data->x, peer_key_data->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003797 alg ),
3798 expected_status_arg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003799
3800exit:
3801 psa_generator_abort( &generator );
3802 psa_destroy_key( our_key );
3803 mbedtls_psa_crypto_free( );
3804}
3805/* END_CASE */
3806
3807/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02003808void key_agreement_capacity( int alg_arg,
3809 int our_key_type_arg, data_t *our_key_data,
3810 data_t *peer_key_data,
3811 int expected_capacity_arg )
3812{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003813 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003814 psa_algorithm_t alg = alg_arg;
3815 psa_key_type_t our_key_type = our_key_type_arg;
3816 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003817 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02003818 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02003819 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02003820
Gilles Peskine8817f612018-12-18 00:18:46 +01003821 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003822
Gilles Peskine8817f612018-12-18 00:18:46 +01003823 PSA_ASSERT( psa_allocate_key( our_key_type,
3824 KEY_BITS_FROM_DATA( our_key_type,
3825 our_key_data ),
3826 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003827 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003828 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3829 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3830 our_key_data->x,
3831 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003832
Gilles Peskine8817f612018-12-18 00:18:46 +01003833 PSA_ASSERT( psa_key_agreement( &generator,
3834 our_key,
3835 peer_key_data->x, peer_key_data->len,
3836 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003837
Gilles Peskinebf491972018-10-25 22:36:12 +02003838 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003839 PSA_ASSERT( psa_get_generator_capacity(
3840 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003841 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02003842
Gilles Peskinebf491972018-10-25 22:36:12 +02003843 /* Test the actual capacity by reading the output. */
3844 while( actual_capacity > sizeof( output ) )
3845 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003846 PSA_ASSERT( psa_generator_read( &generator,
3847 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02003848 actual_capacity -= sizeof( output );
3849 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003850 PSA_ASSERT( psa_generator_read( &generator,
3851 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003852 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
3853 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskinebf491972018-10-25 22:36:12 +02003854
Gilles Peskine59685592018-09-18 12:11:34 +02003855exit:
3856 psa_generator_abort( &generator );
3857 psa_destroy_key( our_key );
3858 mbedtls_psa_crypto_free( );
3859}
3860/* END_CASE */
3861
3862/* BEGIN_CASE */
3863void key_agreement_output( int alg_arg,
3864 int our_key_type_arg, data_t *our_key_data,
3865 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003866 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02003867{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003868 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003869 psa_algorithm_t alg = alg_arg;
3870 psa_key_type_t our_key_type = our_key_type_arg;
3871 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003872 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003873 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02003874
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003875 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
3876 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003877
Gilles Peskine8817f612018-12-18 00:18:46 +01003878 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003879
Gilles Peskine8817f612018-12-18 00:18:46 +01003880 PSA_ASSERT( psa_allocate_key( our_key_type,
3881 KEY_BITS_FROM_DATA( our_key_type,
3882 our_key_data ),
3883 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003884 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003885 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3886 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3887 our_key_data->x,
3888 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003889
Gilles Peskine8817f612018-12-18 00:18:46 +01003890 PSA_ASSERT( psa_key_agreement( &generator,
3891 our_key,
3892 peer_key_data->x, peer_key_data->len,
3893 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003894
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003895 PSA_ASSERT( psa_generator_read( &generator,
3896 actual_output,
3897 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003898 ASSERT_COMPARE( actual_output, expected_output1->len,
3899 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003900 if( expected_output2->len != 0 )
3901 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003902 PSA_ASSERT( psa_generator_read( &generator,
3903 actual_output,
3904 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003905 ASSERT_COMPARE( actual_output, expected_output2->len,
3906 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003907 }
Gilles Peskine59685592018-09-18 12:11:34 +02003908
3909exit:
3910 psa_generator_abort( &generator );
3911 psa_destroy_key( our_key );
3912 mbedtls_psa_crypto_free( );
3913 mbedtls_free( actual_output );
3914}
3915/* END_CASE */
3916
3917/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003918void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003919{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003920 size_t bytes = bytes_arg;
3921 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003922 unsigned char *output = NULL;
3923 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003924 size_t i;
3925 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003926
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003927 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3928 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003929 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003930
Gilles Peskine8817f612018-12-18 00:18:46 +01003931 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003932
Gilles Peskinea50d7392018-06-21 10:22:13 +02003933 /* Run several times, to ensure that every output byte will be
3934 * nonzero at least once with overwhelming probability
3935 * (2^(-8*number_of_runs)). */
3936 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003937 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003938 if( bytes != 0 )
3939 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01003940 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003941
3942 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003943 ASSERT_COMPARE( output + bytes, sizeof( trail ),
3944 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003945
3946 for( i = 0; i < bytes; i++ )
3947 {
3948 if( output[i] != 0 )
3949 ++changed[i];
3950 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003951 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02003952
3953 /* Check that every byte was changed to nonzero at least once. This
3954 * validates that psa_generate_random is overwriting every byte of
3955 * the output buffer. */
3956 for( i = 0; i < bytes; i++ )
3957 {
3958 TEST_ASSERT( changed[i] != 0 );
3959 }
Gilles Peskine05d69892018-06-19 22:00:52 +02003960
3961exit:
3962 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003963 mbedtls_free( output );
3964 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02003965}
3966/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02003967
3968/* BEGIN_CASE */
3969void generate_key( int type_arg,
3970 int bits_arg,
3971 int usage_arg,
3972 int alg_arg,
3973 int expected_status_arg )
3974{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003975 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003976 psa_key_type_t type = type_arg;
3977 psa_key_usage_t usage = usage_arg;
3978 size_t bits = bits_arg;
3979 psa_algorithm_t alg = alg_arg;
3980 psa_status_t expected_status = expected_status_arg;
3981 psa_key_type_t got_type;
3982 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003983 psa_status_t expected_info_status =
3984 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003985 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003986
Gilles Peskine8817f612018-12-18 00:18:46 +01003987 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003988
Gilles Peskine8817f612018-12-18 00:18:46 +01003989 PSA_ASSERT( psa_allocate_key( type, bits, &handle ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003990 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003991 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003992
3993 /* Generate a key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003994 TEST_EQUAL( psa_generate_key( handle, type, bits, NULL, 0 ),
3995 expected_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003996
3997 /* Test the key information */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003998 TEST_EQUAL( psa_get_key_information( handle, &got_type, &got_bits ),
3999 expected_info_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004000 if( expected_info_status != PSA_SUCCESS )
4001 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004002 TEST_EQUAL( got_type, type );
4003 TEST_EQUAL( got_bits, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004004
Gilles Peskine818ca122018-06-20 18:16:48 +02004005 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004006 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004007 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004008
4009exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004010 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004011 mbedtls_psa_crypto_free( );
4012}
4013/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004014
Darryl Greend49a4992018-06-18 17:27:26 +01004015/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
4016void persistent_key_load_key_from_storage( data_t *data, int type_arg,
4017 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00004018 int alg_arg, int generation_method,
4019 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01004020{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004021 psa_key_handle_t handle = 0;
4022 psa_key_handle_t base_key;
Darryl Greend49a4992018-06-18 17:27:26 +01004023 psa_key_type_t type = (psa_key_type_t) type_arg;
4024 psa_key_type_t type_get;
4025 size_t bits_get;
Jaeden Amero70261c52019-01-04 11:47:20 +00004026 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
4027 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004028 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
4029 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00004030 psa_key_policy_t base_policy_set = PSA_KEY_POLICY_INIT;
Darryl Green0c6575a2018-11-07 16:05:30 +00004031 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
4032 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004033 unsigned char *first_export = NULL;
4034 unsigned char *second_export = NULL;
4035 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4036 size_t first_exported_length;
4037 size_t second_exported_length;
4038
4039 ASSERT_ALLOC( first_export, export_size );
4040 ASSERT_ALLOC( second_export, export_size );
4041
Gilles Peskine8817f612018-12-18 00:18:46 +01004042 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004043
Gilles Peskine8817f612018-12-18 00:18:46 +01004044 PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4045 type, bits,
4046 &handle ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004047 psa_key_policy_set_usage( &policy_set, policy_usage,
4048 policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004049 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004050
Darryl Green0c6575a2018-11-07 16:05:30 +00004051 switch( generation_method )
4052 {
4053 case IMPORT_KEY:
4054 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004055 PSA_ASSERT( psa_import_key( handle, type,
4056 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004057 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004058
Darryl Green0c6575a2018-11-07 16:05:30 +00004059 case GENERATE_KEY:
4060 /* Generate a key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004061 PSA_ASSERT( psa_generate_key( handle, type, bits,
4062 NULL, 0 ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004063 break;
4064
4065 case DERIVE_KEY:
4066 /* Create base key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004067 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
4068 PSA_BYTES_TO_BITS( data->len ),
4069 &base_key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004070 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
4071 base_policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004072 PSA_ASSERT( psa_set_key_policy(
4073 base_key, &base_policy_set ) );
4074 PSA_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
4075 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004076 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004077 PSA_ASSERT( psa_key_derivation( &generator, base_key,
4078 base_policy_alg,
4079 NULL, 0, NULL, 0,
4080 export_size ) );
4081 PSA_ASSERT( psa_generator_import_key(
4082 handle, PSA_KEY_TYPE_RAW_DATA,
4083 bits, &generator ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004084 break;
4085 }
Darryl Greend49a4992018-06-18 17:27:26 +01004086
4087 /* Export the key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004088 TEST_EQUAL( psa_export_key( handle,
4089 first_export, export_size,
4090 &first_exported_length ),
4091 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004092
4093 /* Shutdown and restart */
4094 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004095 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004096
Darryl Greend49a4992018-06-18 17:27:26 +01004097 /* Check key slot still contains key data */
Gilles Peskine8817f612018-12-18 00:18:46 +01004098 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4099 &handle ) );
4100 PSA_ASSERT( psa_get_key_information(
4101 handle, &type_get, &bits_get ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004102 TEST_EQUAL( type_get, type );
4103 TEST_EQUAL( bits_get, (size_t) bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004104
Gilles Peskine8817f612018-12-18 00:18:46 +01004105 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004106 TEST_EQUAL( psa_key_policy_get_usage( &policy_get ), policy_usage );
4107 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_get ), policy_alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004108
4109 /* Export the key again */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004110 TEST_EQUAL( psa_export_key( handle,
4111 second_export, export_size,
4112 &second_exported_length ),
4113 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004114
Darryl Green0c6575a2018-11-07 16:05:30 +00004115 if( export_status == PSA_SUCCESS )
4116 {
4117 ASSERT_COMPARE( first_export, first_exported_length,
4118 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01004119
Darryl Green0c6575a2018-11-07 16:05:30 +00004120 switch( generation_method )
4121 {
4122 case IMPORT_KEY:
4123 ASSERT_COMPARE( data->x, data->len,
4124 first_export, first_exported_length );
4125 break;
4126 default:
4127 break;
4128 }
4129 }
4130
4131 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004132 if( ! exercise_key( handle, policy_usage, policy_alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004133 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004134
4135exit:
4136 mbedtls_free( first_export );
4137 mbedtls_free( second_export );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004138 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004139 mbedtls_psa_crypto_free();
4140}
4141/* END_CASE */