blob: 9b8e01c23d42ffc6648363ab9af317fed723a763 [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 Peskineb70a0fd2019-01-07 22:59:38 +0100369 if( PSA_ALG_IS_HKDF( alg ) )
370 {
371 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
372 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
373 PSA_KDF_STEP_SALT,
374 label,
375 label_length ) );
376 PSA_ASSERT( psa_key_derivation_input_key( &generator,
377 PSA_KDF_STEP_SECRET,
378 handle ) );
379 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
380 PSA_KDF_STEP_INFO,
381 seed,
382 seed_length ) );
383 }
384 else
385 {
386 // legacy
387 PSA_ASSERT( psa_key_derivation( &generator,
388 handle, alg,
389 label, label_length,
390 seed, seed_length,
391 sizeof( output ) ) );
392 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100393 PSA_ASSERT( psa_generator_read( &generator,
394 output,
395 sizeof( output ) ) );
396 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200397 }
398
399 return( 1 );
400
401exit:
402 return( 0 );
403}
404
Gilles Peskinec7998b72018-11-07 18:45:02 +0100405/* We need two keys to exercise key agreement. Exercise the
406 * private key against its own public key. */
407static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator,
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100408 psa_key_handle_t handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100409 psa_algorithm_t alg )
410{
411 psa_key_type_t private_key_type;
412 psa_key_type_t public_key_type;
413 size_t key_bits;
414 uint8_t *public_key = NULL;
415 size_t public_key_length;
416 /* Return UNKNOWN_ERROR if something other than the final call to
417 * psa_key_agreement fails. This isn't fully satisfactory, but it's
418 * good enough: callers will report it as a failed test anyway. */
419 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
420
Gilles Peskine8817f612018-12-18 00:18:46 +0100421 PSA_ASSERT( psa_get_key_information( handle,
422 &private_key_type,
423 &key_bits ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100424 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
425 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
426 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100427 PSA_ASSERT( psa_export_public_key( handle,
428 public_key, public_key_length,
429 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100430
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100431 status = psa_key_agreement( generator, handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100432 public_key, public_key_length,
433 alg );
434exit:
435 mbedtls_free( public_key );
436 return( status );
437}
438
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100439static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200440 psa_key_usage_t usage,
441 psa_algorithm_t alg )
442{
443 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200444 unsigned char output[1];
445 int ok = 0;
446
447 if( usage & PSA_KEY_USAGE_DERIVE )
448 {
449 /* We need two keys to exercise key agreement. Exercise the
450 * private key against its own public key. */
Gilles Peskine8817f612018-12-18 00:18:46 +0100451 PSA_ASSERT( key_agreement_with_self( &generator, handle, alg ) );
452 PSA_ASSERT( psa_generator_read( &generator,
453 output,
454 sizeof( output ) ) );
455 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200456 }
457 ok = 1;
458
459exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200460 return( ok );
461}
462
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200463static int is_oid_of_key_type( psa_key_type_t type,
464 const uint8_t *oid, size_t oid_length )
465{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200466 const uint8_t *expected_oid = NULL;
467 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200468#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200469 if( PSA_KEY_TYPE_IS_RSA( type ) )
470 {
471 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
472 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
473 }
474 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200475#endif /* MBEDTLS_RSA_C */
476#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200477 if( PSA_KEY_TYPE_IS_ECC( type ) )
478 {
479 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
480 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
481 }
482 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200483#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200484 {
485 char message[40];
486 mbedtls_snprintf( message, sizeof( message ),
487 "OID not known for key type=0x%08lx",
488 (unsigned long) type );
489 test_fail( message, __LINE__, __FILE__ );
490 return( 0 );
491 }
492
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200493 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200494 return( 1 );
495
496exit:
497 return( 0 );
498}
499
500static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
501 size_t min_bits, size_t max_bits,
502 int must_be_odd )
503{
504 size_t len;
505 size_t actual_bits;
506 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100507 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100508 MBEDTLS_ASN1_INTEGER ),
509 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200510 /* Tolerate a slight departure from DER encoding:
511 * - 0 may be represented by an empty string or a 1-byte string.
512 * - The sign bit may be used as a value bit. */
513 if( ( len == 1 && ( *p )[0] == 0 ) ||
514 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
515 {
516 ++( *p );
517 --len;
518 }
519 if( min_bits == 0 && len == 0 )
520 return( 1 );
521 msb = ( *p )[0];
522 TEST_ASSERT( msb != 0 );
523 actual_bits = 8 * ( len - 1 );
524 while( msb != 0 )
525 {
526 msb >>= 1;
527 ++actual_bits;
528 }
529 TEST_ASSERT( actual_bits >= min_bits );
530 TEST_ASSERT( actual_bits <= max_bits );
531 if( must_be_odd )
532 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
533 *p += len;
534 return( 1 );
535exit:
536 return( 0 );
537}
538
539static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
540 size_t *len,
541 unsigned char n, unsigned char tag )
542{
543 int ret;
544 ret = mbedtls_asn1_get_tag( p, end, len,
545 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
546 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
547 if( ret != 0 )
548 return( ret );
549 end = *p + *len;
550 ret = mbedtls_asn1_get_tag( p, end, len, tag );
551 if( ret != 0 )
552 return( ret );
553 if( *p + *len != end )
554 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
555 return( 0 );
556}
557
558static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
559 uint8_t *exported, size_t exported_length )
560{
561 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100562 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200563 else
564 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200565
566#if defined(MBEDTLS_DES_C)
567 if( type == PSA_KEY_TYPE_DES )
568 {
569 /* Check the parity bits. */
570 unsigned i;
571 for( i = 0; i < bits / 8; i++ )
572 {
573 unsigned bit_count = 0;
574 unsigned m;
575 for( m = 1; m <= 0x100; m <<= 1 )
576 {
577 if( exported[i] & m )
578 ++bit_count;
579 }
580 TEST_ASSERT( bit_count % 2 != 0 );
581 }
582 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200583 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200584#endif
585
586#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
587 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
588 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200589 uint8_t *p = exported;
590 uint8_t *end = exported + exported_length;
591 size_t len;
592 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200593 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200594 * modulus INTEGER, -- n
595 * publicExponent INTEGER, -- e
596 * privateExponent INTEGER, -- d
597 * prime1 INTEGER, -- p
598 * prime2 INTEGER, -- q
599 * exponent1 INTEGER, -- d mod (p-1)
600 * exponent2 INTEGER, -- d mod (q-1)
601 * coefficient INTEGER, -- (inverse of q) mod p
602 * }
603 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100604 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
605 MBEDTLS_ASN1_SEQUENCE |
606 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
607 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200608 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
609 goto exit;
610 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
611 goto exit;
612 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
613 goto exit;
614 /* Require d to be at least half the size of n. */
615 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
616 goto exit;
617 /* Require p and q to be at most half the size of n, rounded up. */
618 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
619 goto exit;
620 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
621 goto exit;
622 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
623 goto exit;
624 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
625 goto exit;
626 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
627 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100628 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100629 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200630 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200631#endif /* MBEDTLS_RSA_C */
632
633#if defined(MBEDTLS_ECP_C)
634 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
635 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100636 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100637 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100638 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200639 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200640#endif /* MBEDTLS_ECP_C */
641
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200642 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
643 {
644 uint8_t *p = exported;
645 uint8_t *end = exported + exported_length;
646 size_t len;
647 mbedtls_asn1_buf alg;
648 mbedtls_asn1_buf params;
649 mbedtls_asn1_bitstring bitstring;
650 /* SubjectPublicKeyInfo ::= SEQUENCE {
651 * algorithm AlgorithmIdentifier,
652 * subjectPublicKey BIT STRING }
653 * AlgorithmIdentifier ::= SEQUENCE {
654 * algorithm OBJECT IDENTIFIER,
655 * parameters ANY DEFINED BY algorithm OPTIONAL }
656 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100657 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 );
662 TEST_EQUAL( mbedtls_asn1_get_alg( &p, end, &alg, &params ), 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200663 if( ! is_oid_of_key_type( type, alg.p, alg.len ) )
664 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100665 TEST_EQUAL( mbedtls_asn1_get_bitstring( &p, end, &bitstring ), 0 );
666 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200667 p = bitstring.p;
668#if defined(MBEDTLS_RSA_C)
669 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
670 {
671 /* RSAPublicKey ::= SEQUENCE {
672 * modulus INTEGER, -- n
673 * publicExponent INTEGER } -- e
674 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100675 TEST_EQUAL( bitstring.unused_bits, 0 );
676 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
677 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100678 MBEDTLS_ASN1_CONSTRUCTED ),
679 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100680 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200681 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
682 goto exit;
683 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
684 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100685 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200686 }
687 else
688#endif /* MBEDTLS_RSA_C */
689#if defined(MBEDTLS_ECP_C)
690 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
691 {
692 /* ECPoint ::= ...
Gilles Peskinec6290c02018-08-13 17:24:59 +0200693 * -- first 8 bits: 0x04 (uncompressed representation);
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200694 * -- then x_P as an n-bit string, big endian;
695 * -- then y_P as a n-bit string, big endian,
696 * -- where n is the order of the curve.
697 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100698 TEST_EQUAL( bitstring.unused_bits, 0 );
699 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
700 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200701 }
702 else
703#endif /* MBEDTLS_ECP_C */
704 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100705 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200706 mbedtls_snprintf( message, sizeof( message ),
707 "No sanity check for public key type=0x%08lx",
708 (unsigned long) type );
709 test_fail( message, __LINE__, __FILE__ );
710 return( 0 );
711 }
712 }
713 else
714
715 {
716 /* No sanity checks for other types */
717 }
718
719 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200720
721exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200722 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200723}
724
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100725static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200726 psa_key_usage_t usage )
727{
728 psa_key_type_t type;
729 size_t bits;
730 uint8_t *exported = NULL;
731 size_t exported_size = 0;
732 size_t exported_length = 0;
733 int ok = 0;
734
Gilles Peskine8817f612018-12-18 00:18:46 +0100735 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200736
737 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
738 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200739 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100740 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
741 PSA_ERROR_NOT_PERMITTED );
Gilles Peskined14664a2018-08-10 19:07:32 +0200742 return( 1 );
743 }
744
Gilles Peskined14664a2018-08-10 19:07:32 +0200745 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200746 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200747
Gilles Peskine8817f612018-12-18 00:18:46 +0100748 PSA_ASSERT( psa_export_key( handle,
749 exported, exported_size,
750 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200751 ok = exported_key_sanity_check( type, bits, exported, exported_length );
752
753exit:
754 mbedtls_free( exported );
755 return( ok );
756}
757
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100758static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200759{
760 psa_key_type_t type;
761 psa_key_type_t public_type;
762 size_t bits;
763 uint8_t *exported = NULL;
764 size_t exported_size = 0;
765 size_t exported_length = 0;
766 int ok = 0;
767
Gilles Peskine8817f612018-12-18 00:18:46 +0100768 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200769 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
770 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100771 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100772 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200773 return( 1 );
774 }
775
776 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
777 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200778 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200779
Gilles Peskine8817f612018-12-18 00:18:46 +0100780 PSA_ASSERT( psa_export_public_key( handle,
781 exported, exported_size,
782 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200783 ok = exported_key_sanity_check( public_type, bits,
784 exported, exported_length );
785
786exit:
787 mbedtls_free( exported );
788 return( ok );
789}
790
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100791static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +0200792 psa_key_usage_t usage,
793 psa_algorithm_t alg )
794{
795 int ok;
796 if( alg == 0 )
797 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
798 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100799 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200800 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100801 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200802 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100803 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200804 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100805 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200806 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100807 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200808 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100809 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200810 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100811 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200812 else
813 {
814 char message[40];
815 mbedtls_snprintf( message, sizeof( message ),
816 "No code to exercise alg=0x%08lx",
817 (unsigned long) alg );
818 test_fail( message, __LINE__, __FILE__ );
819 ok = 0;
820 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200821
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100822 ok = ok && exercise_export_key( handle, usage );
823 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +0200824
Gilles Peskine02b75072018-07-01 22:31:34 +0200825 return( ok );
826}
827
Gilles Peskine10df3412018-10-25 22:35:43 +0200828static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
829 psa_algorithm_t alg )
830{
831 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
832 {
833 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
834 PSA_KEY_USAGE_VERIFY :
835 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
836 }
837 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
838 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
839 {
840 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
841 PSA_KEY_USAGE_ENCRYPT :
842 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
843 }
844 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
845 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
846 {
847 return( PSA_KEY_USAGE_DERIVE );
848 }
849 else
850 {
851 return( 0 );
852 }
853
854}
Darryl Green0c6575a2018-11-07 16:05:30 +0000855
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100856/* An overapproximation of the amount of storage needed for a key of the
857 * given type and with the given content. The API doesn't make it easy
858 * to find a good value for the size. The current implementation doesn't
859 * care about the value anyway. */
860#define KEY_BITS_FROM_DATA( type, data ) \
861 ( data )->len
862
Darryl Green0c6575a2018-11-07 16:05:30 +0000863typedef enum {
864 IMPORT_KEY = 0,
865 GENERATE_KEY = 1,
866 DERIVE_KEY = 2
867} generate_method;
868
Gilles Peskinee59236f2018-01-27 23:32:46 +0100869/* END_HEADER */
870
871/* BEGIN_DEPENDENCIES
872 * depends_on:MBEDTLS_PSA_CRYPTO_C
873 * END_DEPENDENCIES
874 */
875
876/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200877void static_checks( )
878{
879 size_t max_truncated_mac_size =
880 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
881
882 /* Check that the length for a truncated MAC always fits in the algorithm
883 * encoding. The shifted mask is the maximum truncated value. The
884 * untruncated algorithm may be one byte larger. */
885 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
886}
887/* END_CASE */
888
889/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200890void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100891{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100892 psa_key_handle_t handle = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200893 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100894 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100895
Gilles Peskine8817f612018-12-18 00:18:46 +0100896 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100897
Gilles Peskine8817f612018-12-18 00:18:46 +0100898 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
899 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100900 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100901 TEST_EQUAL( status, expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100902 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100903 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100904
905exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100906 mbedtls_psa_crypto_free( );
907}
908/* END_CASE */
909
910/* BEGIN_CASE */
Gilles Peskinea4261682018-12-03 11:34:01 +0100911void import_twice( int alg_arg, int usage_arg,
912 int type1_arg, data_t *data1,
913 int expected_import1_status_arg,
914 int type2_arg, data_t *data2,
915 int expected_import2_status_arg )
916{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100917 psa_key_handle_t handle = 0;
Gilles Peskinea4261682018-12-03 11:34:01 +0100918 psa_algorithm_t alg = alg_arg;
919 psa_key_usage_t usage = usage_arg;
920 psa_key_type_t type1 = type1_arg;
921 psa_status_t expected_import1_status = expected_import1_status_arg;
922 psa_key_type_t type2 = type2_arg;
923 psa_status_t expected_import2_status = expected_import2_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +0000924 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea4261682018-12-03 11:34:01 +0100925 psa_status_t status;
926
Gilles Peskine8817f612018-12-18 00:18:46 +0100927 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100928
Gilles Peskine8817f612018-12-18 00:18:46 +0100929 PSA_ASSERT( psa_allocate_key( type1,
930 MAX( KEY_BITS_FROM_DATA( type1, data1 ),
931 KEY_BITS_FROM_DATA( type2, data2 ) ),
932 &handle ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100933 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100934 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100935
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100936 status = psa_import_key( handle, type1, data1->x, data1->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100937 TEST_EQUAL( status, expected_import1_status );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100938 status = psa_import_key( handle, type2, data2->x, data2->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100939 TEST_EQUAL( status, expected_import2_status );
Gilles Peskinea4261682018-12-03 11:34:01 +0100940
941 if( expected_import1_status == PSA_SUCCESS ||
942 expected_import2_status == PSA_SUCCESS )
943 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100944 TEST_ASSERT( exercise_key( handle, usage, alg ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100945 }
946
947exit:
948 mbedtls_psa_crypto_free( );
949}
950/* END_CASE */
951
952/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200953void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
954{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100955 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200956 size_t bits = bits_arg;
957 psa_status_t expected_status = expected_status_arg;
958 psa_status_t status;
959 psa_key_type_t type =
960 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
961 size_t buffer_size = /* Slight overapproximations */
962 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200963 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200964 unsigned char *p;
965 int ret;
966 size_t length;
967
Gilles Peskine8817f612018-12-18 00:18:46 +0100968 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200969 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200970
971 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
972 bits, keypair ) ) >= 0 );
973 length = ret;
974
975 /* Try importing the key */
Gilles Peskine8817f612018-12-18 00:18:46 +0100976 PSA_ASSERT( psa_allocate_key( type, bits, &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100977 status = psa_import_key( handle, type, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100978 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200979 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100980 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200981
982exit:
983 mbedtls_free( buffer );
984 mbedtls_psa_crypto_free( );
985}
986/* END_CASE */
987
988/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300989void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300990 int type_arg,
991 int alg_arg,
992 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100993 int expected_bits,
994 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200995 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100996 int canonical_input )
997{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100998 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100999 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001000 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001001 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001002 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001003 unsigned char *exported = NULL;
1004 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001005 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001006 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001007 size_t reexported_length;
1008 psa_key_type_t got_type;
1009 size_t got_bits;
Jaeden Amero70261c52019-01-04 11:47:20 +00001010 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001011
Moran Pekercb088e72018-07-17 17:36:59 +03001012 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001013 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001014 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001015 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001016 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001017
Gilles Peskine8817f612018-12-18 00:18:46 +01001018 PSA_ASSERT( psa_allocate_key( type, expected_bits, &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001019 psa_key_policy_set_usage( &policy, usage_arg, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001020 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001021
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001022 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1023 PSA_ERROR_EMPTY_SLOT );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001024
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001025 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001026 PSA_ASSERT( psa_import_key( handle, type,
1027 data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001028
1029 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001030 PSA_ASSERT( psa_get_key_information( handle,
1031 &got_type,
1032 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001033 TEST_EQUAL( got_type, type );
1034 TEST_EQUAL( got_bits, (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001035
1036 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001037 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001038 exported, export_size,
1039 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001040 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001041
1042 /* The exported length must be set by psa_export_key() to a value between 0
1043 * and export_size. On errors, the exported length must be 0. */
1044 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1045 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1046 TEST_ASSERT( exported_length <= export_size );
1047
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001048 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001049 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001050 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001051 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001052 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001053 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001054 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001055
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001056 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001057 goto exit;
1058
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001059 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001060 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001061 else
1062 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001063 psa_key_handle_t handle2;
Gilles Peskine8817f612018-12-18 00:18:46 +01001064 PSA_ASSERT( psa_allocate_key( type, expected_bits, &handle2 ) );
1065 PSA_ASSERT( psa_set_key_policy( handle2, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001066
Gilles Peskine8817f612018-12-18 00:18:46 +01001067 PSA_ASSERT( psa_import_key( handle2, type,
1068 exported,
1069 exported_length ) );
1070 PSA_ASSERT( psa_export_key( handle2,
1071 reexported,
1072 export_size,
1073 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001074 ASSERT_COMPARE( exported, exported_length,
1075 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001076 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001077 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001078 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001079
1080destroy:
1081 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001082 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001083 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1084 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001085
1086exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001087 mbedtls_free( exported );
1088 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001089 mbedtls_psa_crypto_free( );
1090}
1091/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001092
Moran Pekerf709f4a2018-06-06 17:26:04 +03001093/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001094void import_key_nonempty_slot( )
1095{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001096 psa_key_handle_t handle = 0;
Moran Peker28a38e62018-11-07 16:18:24 +02001097 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1098 psa_status_t status;
1099 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
Gilles Peskine8817f612018-12-18 00:18:46 +01001100 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001101
Gilles Peskine8817f612018-12-18 00:18:46 +01001102 PSA_ASSERT( psa_allocate_key( type, PSA_BYTES_TO_BITS( sizeof( data ) ),
1103 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001104
Moran Peker28a38e62018-11-07 16:18:24 +02001105 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001106 PSA_ASSERT( psa_import_key( handle, type,
1107 data, sizeof( data ) ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001108
1109 /* Import the key again */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001110 status = psa_import_key( handle, type, data, sizeof( data ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001111 TEST_EQUAL( status, PSA_ERROR_OCCUPIED_SLOT );
Moran Peker28a38e62018-11-07 16:18:24 +02001112
1113exit:
1114 mbedtls_psa_crypto_free( );
1115}
1116/* END_CASE */
1117
1118/* BEGIN_CASE */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001119void export_invalid_handle( int handle, int expected_export_status_arg )
Moran Peker28a38e62018-11-07 16:18:24 +02001120{
1121 psa_status_t status;
1122 unsigned char *exported = NULL;
1123 size_t export_size = 0;
1124 size_t exported_length = INVALID_EXPORT_LENGTH;
1125 psa_status_t expected_export_status = expected_export_status_arg;
1126
Gilles Peskine8817f612018-12-18 00:18:46 +01001127 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001128
1129 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001130 status = psa_export_key( (psa_key_handle_t) handle,
Moran Peker28a38e62018-11-07 16:18:24 +02001131 exported, export_size,
1132 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001133 TEST_EQUAL( status, expected_export_status );
Moran Peker28a38e62018-11-07 16:18:24 +02001134
1135exit:
1136 mbedtls_psa_crypto_free( );
1137}
1138/* END_CASE */
1139
1140/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001141void export_with_no_key_activity( )
1142{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001143 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001144 psa_algorithm_t alg = PSA_ALG_CTR;
1145 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001146 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001147 unsigned char *exported = NULL;
1148 size_t export_size = 0;
1149 size_t exported_length = INVALID_EXPORT_LENGTH;
1150
Gilles Peskine8817f612018-12-18 00:18:46 +01001151 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001152
Gilles Peskine8817f612018-12-18 00:18:46 +01001153 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1154 &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001155 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001156 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001157
1158 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001159 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001160 exported, export_size,
1161 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001162 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001163
1164exit:
1165 mbedtls_psa_crypto_free( );
1166}
1167/* END_CASE */
1168
1169/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001170void cipher_with_no_key_activity( )
1171{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001172 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001173 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001174 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001175 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001176 int exercise_alg = PSA_ALG_CTR;
1177
Gilles Peskine8817f612018-12-18 00:18:46 +01001178 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001179
Gilles Peskine8817f612018-12-18 00:18:46 +01001180 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1181 &handle ) );
Moran Pekerce500072018-11-07 16:20:07 +02001182 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001183 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerce500072018-11-07 16:20:07 +02001184
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001185 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001186 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001187
1188exit:
1189 psa_cipher_abort( &operation );
1190 mbedtls_psa_crypto_free( );
1191}
1192/* END_CASE */
1193
1194/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001195void export_after_import_failure( data_t *data, int type_arg,
1196 int expected_import_status_arg )
1197{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001198 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001199 psa_key_type_t type = type_arg;
1200 psa_status_t status;
1201 unsigned char *exported = NULL;
1202 size_t export_size = 0;
1203 psa_status_t expected_import_status = expected_import_status_arg;
1204 size_t exported_length = INVALID_EXPORT_LENGTH;
1205
Gilles Peskine8817f612018-12-18 00:18:46 +01001206 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001207
Gilles Peskine8817f612018-12-18 00:18:46 +01001208 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1209 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001210
Moran Peker34550092018-11-07 16:19:34 +02001211 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001212 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001213 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001214 TEST_EQUAL( status, expected_import_status );
Moran Peker34550092018-11-07 16:19:34 +02001215
1216 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001217 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001218 exported, export_size,
1219 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001220 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001221
1222exit:
1223 mbedtls_psa_crypto_free( );
1224}
1225/* END_CASE */
1226
1227/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001228void cipher_after_import_failure( data_t *data, int type_arg,
1229 int expected_import_status_arg )
1230{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001231 psa_key_handle_t handle = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001232 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001233 psa_key_type_t type = type_arg;
1234 psa_status_t status;
1235 psa_status_t expected_import_status = expected_import_status_arg;
1236 int exercise_alg = PSA_ALG_CTR;
1237
Gilles Peskine8817f612018-12-18 00:18:46 +01001238 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001239
Gilles Peskine8817f612018-12-18 00:18:46 +01001240 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1241 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001242
Moran Pekerce500072018-11-07 16:20:07 +02001243 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001244 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001245 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001246 TEST_EQUAL( status, expected_import_status );
Moran Pekerce500072018-11-07 16:20:07 +02001247
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001248 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001249 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001250
1251exit:
1252 psa_cipher_abort( &operation );
1253 mbedtls_psa_crypto_free( );
1254}
1255/* END_CASE */
1256
1257/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001258void export_after_destroy_key( data_t *data, int type_arg )
1259{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001260 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001261 psa_key_type_t type = type_arg;
1262 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001263 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001264 psa_algorithm_t alg = PSA_ALG_CTR;
1265 unsigned char *exported = NULL;
1266 size_t export_size = 0;
1267 size_t exported_length = INVALID_EXPORT_LENGTH;
1268
Gilles Peskine8817f612018-12-18 00:18:46 +01001269 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001270
Gilles Peskine8817f612018-12-18 00:18:46 +01001271 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1272 &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001273 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001274 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001275 export_size = (ptrdiff_t) data->len;
1276 ASSERT_ALLOC( exported, export_size );
1277
1278 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001279 PSA_ASSERT( psa_import_key( handle, type,
1280 data->x, data->len ) );
Moran Peker34550092018-11-07 16:19:34 +02001281
Gilles Peskine8817f612018-12-18 00:18:46 +01001282 PSA_ASSERT( psa_export_key( handle, exported, export_size,
1283 &exported_length ) );
Moran Peker34550092018-11-07 16:19:34 +02001284
1285 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001286 PSA_ASSERT( psa_destroy_key( handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001287
1288 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001289 status = psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001290 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001291 TEST_EQUAL( status, PSA_ERROR_INVALID_HANDLE );
Moran Peker34550092018-11-07 16:19:34 +02001292
1293exit:
1294 mbedtls_free( exported );
1295 mbedtls_psa_crypto_free( );
1296}
1297/* END_CASE */
1298
1299/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001300void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001301 int type_arg,
1302 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001303 int export_size_delta,
1304 int expected_export_status_arg,
1305 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001306{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001307 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001308 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001309 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001310 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001311 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001312 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001313 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001314 size_t exported_length = INVALID_EXPORT_LENGTH;
Jaeden Amero70261c52019-01-04 11:47:20 +00001315 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001316
Gilles Peskine8817f612018-12-18 00:18:46 +01001317 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001318
Gilles Peskine8817f612018-12-18 00:18:46 +01001319 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1320 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001321 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001322 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001323
1324 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001325 PSA_ASSERT( psa_import_key( handle, type,
1326 data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001327
Gilles Peskine49c25912018-10-29 15:15:31 +01001328 /* Export the public key */
1329 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001330 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001331 exported, export_size,
1332 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001333 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001334 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001335 {
1336 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1337 size_t bits;
Gilles Peskine8817f612018-12-18 00:18:46 +01001338 PSA_ASSERT( psa_get_key_information( handle, NULL, &bits ) );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001339 TEST_ASSERT( expected_public_key->len <=
1340 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001341 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1342 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001343 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001344
1345exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001346 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001347 psa_destroy_key( handle );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001348 mbedtls_psa_crypto_free( );
1349}
1350/* END_CASE */
1351
Gilles Peskine20035e32018-02-03 22:44:14 +01001352/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001353void import_and_exercise_key( data_t *data,
1354 int type_arg,
1355 int bits_arg,
1356 int alg_arg )
1357{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001358 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001359 psa_key_type_t type = type_arg;
1360 size_t bits = bits_arg;
1361 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001362 psa_key_usage_t usage = usage_to_exercise( type, alg );
Jaeden Amero70261c52019-01-04 11:47:20 +00001363 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001364 psa_key_type_t got_type;
1365 size_t got_bits;
1366 psa_status_t status;
1367
Gilles Peskine8817f612018-12-18 00:18:46 +01001368 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001369
Gilles Peskine8817f612018-12-18 00:18:46 +01001370 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1371 &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001372 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001373 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001374
1375 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001376 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01001377 PSA_ASSERT( status );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001378
1379 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001380 PSA_ASSERT( psa_get_key_information( handle,
1381 &got_type,
1382 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001383 TEST_EQUAL( got_type, type );
1384 TEST_EQUAL( got_bits, bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001385
1386 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001387 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001388 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001389
1390exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001391 psa_destroy_key( handle );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001392 mbedtls_psa_crypto_free( );
1393}
1394/* END_CASE */
1395
1396/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001397void key_policy( int usage_arg, int alg_arg )
1398{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001399 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001400 psa_algorithm_t alg = alg_arg;
1401 psa_key_usage_t usage = usage_arg;
1402 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1403 unsigned char key[32] = {0};
Jaeden Amero70261c52019-01-04 11:47:20 +00001404 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
1405 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001406
1407 memset( key, 0x2a, sizeof( key ) );
1408
Gilles Peskine8817f612018-12-18 00:18:46 +01001409 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001410
Gilles Peskine8817f612018-12-18 00:18:46 +01001411 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( sizeof( key ) ),
1412 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001413 psa_key_policy_set_usage( &policy_set, usage, alg );
1414
Gilles Peskinefe11b722018-12-18 00:24:04 +01001415 TEST_EQUAL( psa_key_policy_get_usage( &policy_set ), usage );
1416 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_set ), alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001417 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001418
Gilles Peskine8817f612018-12-18 00:18:46 +01001419 PSA_ASSERT( psa_import_key( handle, key_type,
1420 key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001421
Gilles Peskine8817f612018-12-18 00:18:46 +01001422 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001423
Gilles Peskinefe11b722018-12-18 00:24:04 +01001424 TEST_EQUAL( policy_get.usage, policy_set.usage );
1425 TEST_EQUAL( policy_get.alg, policy_set.alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001426
1427exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001428 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001429 mbedtls_psa_crypto_free( );
1430}
1431/* END_CASE */
1432
1433/* BEGIN_CASE */
Jaeden Amero70261c52019-01-04 11:47:20 +00001434void key_policy_init( )
1435{
1436 /* Test each valid way of initializing the object, except for `= {0}`, as
1437 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1438 * though it's OK by the C standard. We could test for this, but we'd need
1439 * to supress the Clang warning for the test. */
1440 psa_key_policy_t func = psa_key_policy_init( );
1441 psa_key_policy_t init = PSA_KEY_POLICY_INIT;
1442 psa_key_policy_t zero;
1443
1444 memset( &zero, 0, sizeof( zero ) );
1445
1446 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1447 * specification, we test that all valid ways of initializing the object
1448 * have the same bit pattern. This is a stronger requirement that may not
1449 * be valid on all platforms or PSA Crypto implementations, but implies the
1450 * weaker actual requirement is met: that a freshly initialized object, no
1451 * matter how it was initialized, acts the same as any other valid
1452 * initialization. */
1453 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1454 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1455}
1456/* END_CASE */
1457
1458/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001459void mac_key_policy( int policy_usage,
1460 int policy_alg,
1461 int key_type,
1462 data_t *key_data,
1463 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001464{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001465 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001466 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001467 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001468 psa_status_t status;
1469 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001470
Gilles Peskine8817f612018-12-18 00:18:46 +01001471 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001472
Gilles Peskine8817f612018-12-18 00:18:46 +01001473 PSA_ASSERT( psa_allocate_key( key_type,
1474 KEY_BITS_FROM_DATA( key_type, key_data ),
1475 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001476 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001477 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001478
Gilles Peskine8817f612018-12-18 00:18:46 +01001479 PSA_ASSERT( psa_import_key( handle, key_type,
1480 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001481
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001482 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001483 if( policy_alg == exercise_alg &&
1484 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001485 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001486 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001487 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001488 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001489
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001490 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001491 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001492 if( policy_alg == exercise_alg &&
1493 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001494 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001495 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001496 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001497
1498exit:
1499 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001500 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001501 mbedtls_psa_crypto_free( );
1502}
1503/* END_CASE */
1504
1505/* BEGIN_CASE */
1506void cipher_key_policy( int policy_usage,
1507 int policy_alg,
1508 int key_type,
1509 data_t *key_data,
1510 int exercise_alg )
1511{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001512 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001513 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001514 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001515 psa_status_t status;
1516
Gilles Peskine8817f612018-12-18 00:18:46 +01001517 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001518
Gilles Peskine8817f612018-12-18 00:18:46 +01001519 PSA_ASSERT( psa_allocate_key( key_type,
1520 KEY_BITS_FROM_DATA( key_type, key_data ),
1521 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001522 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001523 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001524
Gilles Peskine8817f612018-12-18 00:18:46 +01001525 PSA_ASSERT( psa_import_key( handle, key_type,
1526 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001527
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001528 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001529 if( policy_alg == exercise_alg &&
1530 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001531 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001532 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001533 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001534 psa_cipher_abort( &operation );
1535
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001536 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001537 if( policy_alg == exercise_alg &&
1538 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001539 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001540 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001541 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001542
1543exit:
1544 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001545 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001546 mbedtls_psa_crypto_free( );
1547}
1548/* END_CASE */
1549
1550/* BEGIN_CASE */
1551void aead_key_policy( int policy_usage,
1552 int policy_alg,
1553 int key_type,
1554 data_t *key_data,
1555 int nonce_length_arg,
1556 int tag_length_arg,
1557 int exercise_alg )
1558{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001559 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001560 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001561 psa_status_t status;
1562 unsigned char nonce[16] = {0};
1563 size_t nonce_length = nonce_length_arg;
1564 unsigned char tag[16];
1565 size_t tag_length = tag_length_arg;
1566 size_t output_length;
1567
1568 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1569 TEST_ASSERT( tag_length <= sizeof( tag ) );
1570
Gilles Peskine8817f612018-12-18 00:18:46 +01001571 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001572
Gilles Peskine8817f612018-12-18 00:18:46 +01001573 PSA_ASSERT( psa_allocate_key( key_type,
1574 KEY_BITS_FROM_DATA( key_type, key_data ),
1575 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001576 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001577 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001578
Gilles Peskine8817f612018-12-18 00:18:46 +01001579 PSA_ASSERT( psa_import_key( handle, key_type,
1580 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001581
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001582 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001583 nonce, nonce_length,
1584 NULL, 0,
1585 NULL, 0,
1586 tag, tag_length,
1587 &output_length );
1588 if( policy_alg == exercise_alg &&
1589 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001590 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001591 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001592 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001593
1594 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001595 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001596 nonce, nonce_length,
1597 NULL, 0,
1598 tag, tag_length,
1599 NULL, 0,
1600 &output_length );
1601 if( policy_alg == exercise_alg &&
1602 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001603 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001604 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001605 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001606
1607exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001608 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001609 mbedtls_psa_crypto_free( );
1610}
1611/* END_CASE */
1612
1613/* BEGIN_CASE */
1614void asymmetric_encryption_key_policy( int policy_usage,
1615 int policy_alg,
1616 int key_type,
1617 data_t *key_data,
1618 int exercise_alg )
1619{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001620 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001621 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001622 psa_status_t status;
1623 size_t key_bits;
1624 size_t buffer_length;
1625 unsigned char *buffer = NULL;
1626 size_t output_length;
1627
Gilles Peskine8817f612018-12-18 00:18:46 +01001628 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001629
Gilles Peskine8817f612018-12-18 00:18:46 +01001630 PSA_ASSERT( psa_allocate_key( key_type,
1631 KEY_BITS_FROM_DATA( key_type, key_data ),
1632 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001633 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001634 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001635
Gilles Peskine8817f612018-12-18 00:18:46 +01001636 PSA_ASSERT( psa_import_key( handle, key_type,
1637 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001638
Gilles Peskine8817f612018-12-18 00:18:46 +01001639 PSA_ASSERT( psa_get_key_information( handle,
1640 NULL,
1641 &key_bits ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001642 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1643 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001644 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001645
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001646 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001647 NULL, 0,
1648 NULL, 0,
1649 buffer, buffer_length,
1650 &output_length );
1651 if( policy_alg == exercise_alg &&
1652 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001653 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001654 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001655 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001656
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001657 if( buffer_length != 0 )
1658 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001659 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001660 buffer, buffer_length,
1661 NULL, 0,
1662 buffer, buffer_length,
1663 &output_length );
1664 if( policy_alg == exercise_alg &&
1665 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001666 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001667 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001668 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001669
1670exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001671 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001672 mbedtls_psa_crypto_free( );
1673 mbedtls_free( buffer );
1674}
1675/* END_CASE */
1676
1677/* BEGIN_CASE */
1678void asymmetric_signature_key_policy( int policy_usage,
1679 int policy_alg,
1680 int key_type,
1681 data_t *key_data,
1682 int exercise_alg )
1683{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001684 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001685 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001686 psa_status_t status;
1687 unsigned char payload[16] = {1};
1688 size_t payload_length = sizeof( payload );
1689 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1690 size_t signature_length;
1691
Gilles Peskine8817f612018-12-18 00:18:46 +01001692 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001693
Gilles Peskine8817f612018-12-18 00:18:46 +01001694 PSA_ASSERT( psa_allocate_key( key_type,
1695 KEY_BITS_FROM_DATA( key_type, key_data ),
1696 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001697 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001698 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001699
Gilles Peskine8817f612018-12-18 00:18:46 +01001700 PSA_ASSERT( psa_import_key( handle, key_type,
1701 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001702
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001703 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001704 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001705 signature, sizeof( signature ),
1706 &signature_length );
1707 if( policy_alg == exercise_alg &&
1708 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001709 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001710 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001711 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001712
1713 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001714 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001715 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001716 signature, sizeof( signature ) );
1717 if( policy_alg == exercise_alg &&
1718 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001719 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001720 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001721 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001722
1723exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001724 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001725 mbedtls_psa_crypto_free( );
1726}
1727/* END_CASE */
1728
1729/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001730void derive_key_policy( int policy_usage,
1731 int policy_alg,
1732 int key_type,
1733 data_t *key_data,
1734 int exercise_alg )
1735{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001736 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001737 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001738 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1739 psa_status_t status;
1740
Gilles Peskine8817f612018-12-18 00:18:46 +01001741 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001742
Gilles Peskine8817f612018-12-18 00:18:46 +01001743 PSA_ASSERT( psa_allocate_key( key_type,
1744 KEY_BITS_FROM_DATA( key_type, key_data ),
1745 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001746 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001747 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001748
Gilles Peskine8817f612018-12-18 00:18:46 +01001749 PSA_ASSERT( psa_import_key( handle, key_type,
1750 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001751
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001752 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001753 exercise_alg,
1754 NULL, 0,
1755 NULL, 0,
1756 1 );
1757 if( policy_alg == exercise_alg &&
1758 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001759 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001760 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001761 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001762
1763exit:
1764 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001765 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001766 mbedtls_psa_crypto_free( );
1767}
1768/* END_CASE */
1769
1770/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001771void agreement_key_policy( int policy_usage,
1772 int policy_alg,
1773 int key_type_arg,
1774 data_t *key_data,
1775 int exercise_alg )
1776{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001777 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001778 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001779 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001780 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1781 psa_status_t status;
1782
Gilles Peskine8817f612018-12-18 00:18:46 +01001783 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001784
Gilles Peskine8817f612018-12-18 00:18:46 +01001785 PSA_ASSERT( psa_allocate_key( key_type,
1786 KEY_BITS_FROM_DATA( key_type, key_data ),
1787 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001788 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001789 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001790
Gilles Peskine8817f612018-12-18 00:18:46 +01001791 PSA_ASSERT( psa_import_key( handle, key_type,
1792 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001793
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001794 status = key_agreement_with_self( &generator, handle, exercise_alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001795
Gilles Peskine01d718c2018-09-18 12:01:02 +02001796 if( policy_alg == exercise_alg &&
1797 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001798 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001799 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001800 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001801
1802exit:
1803 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001804 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001805 mbedtls_psa_crypto_free( );
1806}
1807/* END_CASE */
1808
1809/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001810void hash_operation_init( )
1811{
1812 /* Test each valid way of initializing the object, except for `= {0}`, as
1813 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1814 * though it's OK by the C standard. We could test for this, but we'd need
1815 * to supress the Clang warning for the test. */
1816 psa_hash_operation_t func = psa_hash_operation_init( );
1817 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1818 psa_hash_operation_t zero;
1819
1820 memset( &zero, 0, sizeof( zero ) );
1821
1822 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1823 * specification, we test that all valid ways of initializing the object
1824 * have the same bit pattern. This is a stronger requirement that may not
1825 * be valid on all platforms or PSA Crypto implementations, but implies the
1826 * weaker actual requirement is met: that a freshly initialized object, no
1827 * matter how it was initialized, acts the same as any other valid
1828 * initialization. */
1829 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1830 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1831}
1832/* END_CASE */
1833
1834/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001835void hash_setup( int alg_arg,
1836 int expected_status_arg )
1837{
1838 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001839 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001840 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001841 psa_status_t status;
1842
Gilles Peskine8817f612018-12-18 00:18:46 +01001843 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001844
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001845 status = psa_hash_setup( &operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001846 psa_hash_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001847 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001848
1849exit:
1850 mbedtls_psa_crypto_free( );
1851}
1852/* END_CASE */
1853
1854/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02001855void hash_bad_order( )
1856{
1857 unsigned char input[] = "";
1858 /* SHA-256 hash of an empty string */
1859 unsigned char hash[] = {
1860 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1861 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1862 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
1863 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001864 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001865
Gilles Peskine8817f612018-12-18 00:18:46 +01001866 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001867
1868 /* psa_hash_update without calling psa_hash_setup beforehand */
1869 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001870 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001871 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001872
1873 /* psa_hash_verify without calling psa_hash_setup beforehand */
1874 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001875 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001876 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001877
1878 /* psa_hash_finish without calling psa_hash_setup beforehand */
1879 memset( &operation, 0, sizeof( operation ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001880 TEST_EQUAL( psa_hash_finish( &operation,
1881 hash, sizeof( hash ), &hash_len ),
1882 PSA_ERROR_INVALID_ARGUMENT );
itayzafrirf86548d2018-11-01 10:44:32 +02001883
1884exit:
1885 mbedtls_psa_crypto_free( );
1886}
1887/* END_CASE */
1888
itayzafrir27e69452018-11-01 14:26:34 +02001889/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1890void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001891{
1892 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001893 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1894 * appended to it */
1895 unsigned char hash[] = {
1896 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1897 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1898 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03001899 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001900 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001901
Gilles Peskine8817f612018-12-18 00:18:46 +01001902 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001903
itayzafrir27e69452018-11-01 14:26:34 +02001904 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001905 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001906 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001907 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001908
itayzafrir27e69452018-11-01 14:26:34 +02001909 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001910 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001911 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001912 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001913
itayzafrir27e69452018-11-01 14:26:34 +02001914 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001915 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001916 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001917 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001918
itayzafrirec93d302018-10-18 18:01:10 +03001919exit:
1920 mbedtls_psa_crypto_free( );
1921}
1922/* END_CASE */
1923
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001924/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1925void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001926{
1927 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001928 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03001929 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001930 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001931 size_t hash_len;
1932
Gilles Peskine8817f612018-12-18 00:18:46 +01001933 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001934
itayzafrir58028322018-10-25 10:22:01 +03001935 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001936 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001937 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001938 hash, expected_size - 1, &hash_len ),
1939 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001940
1941exit:
1942 mbedtls_psa_crypto_free( );
1943}
1944/* END_CASE */
1945
1946/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001947void mac_operation_init( )
1948{
1949 /* Test each valid way of initializing the object, except for `= {0}`, as
1950 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1951 * though it's OK by the C standard. We could test for this, but we'd need
1952 * to supress the Clang warning for the test. */
1953 psa_mac_operation_t func = psa_mac_operation_init( );
1954 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1955 psa_mac_operation_t zero;
1956
1957 memset( &zero, 0, sizeof( zero ) );
1958
1959 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1960 * specification, we test that all valid ways of initializing the object
1961 * have the same bit pattern. This is a stronger requirement that may not
1962 * be valid on all platforms or PSA Crypto implementations, but implies the
1963 * weaker actual requirement is met: that a freshly initialized object, no
1964 * matter how it was initialized, acts the same as any other valid
1965 * initialization. */
1966 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1967 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1968}
1969/* END_CASE */
1970
1971/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001972void mac_setup( int key_type_arg,
1973 data_t *key,
1974 int alg_arg,
1975 int expected_status_arg )
1976{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001977 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001978 psa_key_type_t key_type = key_type_arg;
1979 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001980 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001981 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00001982 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001983 psa_status_t status;
1984
Gilles Peskine8817f612018-12-18 00:18:46 +01001985 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001986
Gilles Peskine8817f612018-12-18 00:18:46 +01001987 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
1988 &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001989 psa_key_policy_set_usage( &policy,
1990 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
1991 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001992 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001993
Gilles Peskine8817f612018-12-18 00:18:46 +01001994 PSA_ASSERT( psa_import_key( handle, key_type,
1995 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001996
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001997 status = psa_mac_sign_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001998 psa_mac_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001999 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002000
2001exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002002 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002003 mbedtls_psa_crypto_free( );
2004}
2005/* END_CASE */
2006
2007/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002008void mac_sign( int key_type_arg,
2009 data_t *key,
2010 int alg_arg,
2011 data_t *input,
2012 data_t *expected_mac )
2013{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002014 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002015 psa_key_type_t key_type = key_type_arg;
2016 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002017 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002018 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002019 /* Leave a little extra room in the output buffer. At the end of the
2020 * test, we'll check that the implementation didn't overwrite onto
2021 * this extra room. */
2022 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2023 size_t mac_buffer_size =
2024 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2025 size_t mac_length = 0;
2026
2027 memset( actual_mac, '+', sizeof( actual_mac ) );
2028 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2029 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2030
Gilles Peskine8817f612018-12-18 00:18:46 +01002031 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002032
Gilles Peskine8817f612018-12-18 00:18:46 +01002033 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2034 &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002035 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002036 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002037
Gilles Peskine8817f612018-12-18 00:18:46 +01002038 PSA_ASSERT( psa_import_key( handle, key_type,
2039 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002040
2041 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002042 PSA_ASSERT( psa_mac_sign_setup( &operation,
2043 handle, alg ) );
2044 PSA_ASSERT( psa_mac_update( &operation,
2045 input->x, input->len ) );
2046 PSA_ASSERT( psa_mac_sign_finish( &operation,
2047 actual_mac, mac_buffer_size,
2048 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002049
2050 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002051 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2052 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002053
2054 /* Verify that the end of the buffer is untouched. */
2055 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2056 sizeof( actual_mac ) - mac_length ) );
2057
2058exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002059 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002060 mbedtls_psa_crypto_free( );
2061}
2062/* END_CASE */
2063
2064/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002065void mac_verify( int key_type_arg,
2066 data_t *key,
2067 int alg_arg,
2068 data_t *input,
2069 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002070{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002071 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002072 psa_key_type_t key_type = key_type_arg;
2073 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002074 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002075 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002076
Gilles Peskine69c12672018-06-28 00:07:19 +02002077 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2078
Gilles Peskine8817f612018-12-18 00:18:46 +01002079 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002080
Gilles Peskine8817f612018-12-18 00:18:46 +01002081 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2082 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002083 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002084 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad16036df908f2018-04-02 08:34:15 -07002085
Gilles Peskine8817f612018-12-18 00:18:46 +01002086 PSA_ASSERT( psa_import_key( handle, key_type,
2087 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002088
Gilles Peskine8817f612018-12-18 00:18:46 +01002089 PSA_ASSERT( psa_mac_verify_setup( &operation,
2090 handle, alg ) );
2091 PSA_ASSERT( psa_destroy_key( handle ) );
2092 PSA_ASSERT( psa_mac_update( &operation,
2093 input->x, input->len ) );
2094 PSA_ASSERT( psa_mac_verify_finish( &operation,
2095 expected_mac->x,
2096 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002097
2098exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002099 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002100 mbedtls_psa_crypto_free( );
2101}
2102/* END_CASE */
2103
2104/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002105void cipher_operation_init( )
2106{
2107 /* Test each valid way of initializing the object, except for `= {0}`, as
2108 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2109 * though it's OK by the C standard. We could test for this, but we'd need
2110 * to supress the Clang warning for the test. */
2111 psa_cipher_operation_t func = psa_cipher_operation_init( );
2112 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2113 psa_cipher_operation_t zero;
2114
2115 memset( &zero, 0, sizeof( zero ) );
2116
2117 /* Although not technically guaranteed by the C standard nor the PSA Crypto
2118 * specification, we test that all valid ways of initializing the object
2119 * have the same bit pattern. This is a stronger requirement that may not
2120 * be valid on all platforms or PSA Crypto implementations, but implies the
2121 * weaker actual requirement is met: that a freshly initialized object, no
2122 * matter how it was initialized, acts the same as any other valid
2123 * initialization. */
2124 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
2125 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
2126}
2127/* END_CASE */
2128
2129/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002130void cipher_setup( int key_type_arg,
2131 data_t *key,
2132 int alg_arg,
2133 int expected_status_arg )
2134{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002135 psa_key_handle_t handle = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002136 psa_key_type_t key_type = key_type_arg;
2137 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002138 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002139 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002140 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002141 psa_status_t status;
2142
Gilles Peskine8817f612018-12-18 00:18:46 +01002143 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002144
Gilles Peskine8817f612018-12-18 00:18:46 +01002145 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2146 &handle ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002147 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002148 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002149
Gilles Peskine8817f612018-12-18 00:18:46 +01002150 PSA_ASSERT( psa_import_key( handle, key_type,
2151 key->x, key->len ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002152
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002153 status = psa_cipher_encrypt_setup( &operation, handle, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002154 psa_cipher_abort( &operation );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002155 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002156
2157exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002158 psa_destroy_key( handle );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002159 mbedtls_psa_crypto_free( );
2160}
2161/* END_CASE */
2162
2163/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002164void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002165 data_t *key,
2166 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002167 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002168{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002169 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002170 psa_status_t status;
2171 psa_key_type_t key_type = key_type_arg;
2172 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002173 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002174 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002175 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002176 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002177 size_t output_buffer_size = 0;
2178 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002179 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002180 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002181 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002182
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002183 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2184 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002185
Gilles Peskine8817f612018-12-18 00:18:46 +01002186 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002187
Gilles Peskine8817f612018-12-18 00:18:46 +01002188 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2189 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002190 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002191 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002192
Gilles Peskine8817f612018-12-18 00:18:46 +01002193 PSA_ASSERT( psa_import_key( handle, key_type,
2194 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002195
Gilles Peskine8817f612018-12-18 00:18:46 +01002196 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2197 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002198
Gilles Peskine8817f612018-12-18 00:18:46 +01002199 PSA_ASSERT( psa_cipher_set_iv( &operation,
2200 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002201 output_buffer_size = ( (size_t) input->len +
2202 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002203 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002204
Gilles Peskine8817f612018-12-18 00:18:46 +01002205 PSA_ASSERT( psa_cipher_update( &operation,
2206 input->x, input->len,
2207 output, output_buffer_size,
2208 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002209 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002210 status = psa_cipher_finish( &operation,
2211 output + function_output_length,
2212 output_buffer_size,
2213 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002214 total_output_length += function_output_length;
2215
Gilles Peskinefe11b722018-12-18 00:24:04 +01002216 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002217 if( expected_status == PSA_SUCCESS )
2218 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002219 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002220 ASSERT_COMPARE( expected_output->x, expected_output->len,
2221 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002222 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002223
Gilles Peskine50e586b2018-06-08 14:28:46 +02002224exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002225 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002226 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002227 mbedtls_psa_crypto_free( );
2228}
2229/* END_CASE */
2230
2231/* BEGIN_CASE */
2232void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002233 data_t *key,
2234 data_t *input,
2235 int first_part_size,
2236 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002237{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002238 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002239 psa_key_type_t key_type = key_type_arg;
2240 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002241 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002242 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002243 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002244 size_t output_buffer_size = 0;
2245 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002246 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002247 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002248 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002249
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002250 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2251 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002252
Gilles Peskine8817f612018-12-18 00:18:46 +01002253 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002254
Gilles Peskine8817f612018-12-18 00:18:46 +01002255 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2256 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002257 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002258 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002259
Gilles Peskine8817f612018-12-18 00:18:46 +01002260 PSA_ASSERT( psa_import_key( handle, key_type,
2261 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002262
Gilles Peskine8817f612018-12-18 00:18:46 +01002263 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2264 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002265
Gilles Peskine8817f612018-12-18 00:18:46 +01002266 PSA_ASSERT( psa_cipher_set_iv( &operation,
2267 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002268 output_buffer_size = ( (size_t) input->len +
2269 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002270 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002271
Gilles Peskine4abf7412018-06-18 16:35:34 +02002272 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002273 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2274 output, output_buffer_size,
2275 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002276 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002277 PSA_ASSERT( psa_cipher_update( &operation,
2278 input->x + first_part_size,
2279 input->len - first_part_size,
2280 output, output_buffer_size,
2281 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002282 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002283 PSA_ASSERT( psa_cipher_finish( &operation,
2284 output + function_output_length,
2285 output_buffer_size,
2286 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002287 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002288 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002289
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002290 ASSERT_COMPARE( expected_output->x, expected_output->len,
2291 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002292
2293exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002294 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002295 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002296 mbedtls_psa_crypto_free( );
2297}
2298/* END_CASE */
2299
2300/* BEGIN_CASE */
2301void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002302 data_t *key,
2303 data_t *input,
2304 int first_part_size,
2305 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002306{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002307 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002308
2309 psa_key_type_t key_type = key_type_arg;
2310 psa_algorithm_t alg = alg_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002311 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002312 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002313 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002314 size_t output_buffer_size = 0;
2315 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002316 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002317 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002318 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002319
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002320 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2321 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002322
Gilles Peskine8817f612018-12-18 00:18:46 +01002323 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002324
Gilles Peskine8817f612018-12-18 00:18:46 +01002325 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2326 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002327 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002328 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002329
Gilles Peskine8817f612018-12-18 00:18:46 +01002330 PSA_ASSERT( psa_import_key( handle, key_type,
2331 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002332
Gilles Peskine8817f612018-12-18 00:18:46 +01002333 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2334 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002335
Gilles Peskine8817f612018-12-18 00:18:46 +01002336 PSA_ASSERT( psa_cipher_set_iv( &operation,
2337 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002338
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002339 output_buffer_size = ( (size_t) input->len +
2340 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002341 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002342
Gilles Peskine4abf7412018-06-18 16:35:34 +02002343 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002344 PSA_ASSERT( psa_cipher_update( &operation,
2345 input->x, first_part_size,
2346 output, output_buffer_size,
2347 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002348 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002349 PSA_ASSERT( psa_cipher_update( &operation,
2350 input->x + first_part_size,
2351 input->len - first_part_size,
2352 output, output_buffer_size,
2353 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002354 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002355 PSA_ASSERT( psa_cipher_finish( &operation,
2356 output + function_output_length,
2357 output_buffer_size,
2358 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002359 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002360 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002361
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002362 ASSERT_COMPARE( expected_output->x, expected_output->len,
2363 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002364
2365exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002366 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002367 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002368 mbedtls_psa_crypto_free( );
2369}
2370/* END_CASE */
2371
Gilles Peskine50e586b2018-06-08 14:28:46 +02002372/* BEGIN_CASE */
2373void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002374 data_t *key,
2375 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002376 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002377{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002378 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002379 psa_status_t status;
2380 psa_key_type_t key_type = key_type_arg;
2381 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002382 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002383 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002384 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002385 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002386 size_t output_buffer_size = 0;
2387 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002388 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002389 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002390 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002391
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002392 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2393 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002394
Gilles Peskine8817f612018-12-18 00:18:46 +01002395 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002396
Gilles Peskine8817f612018-12-18 00:18:46 +01002397 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2398 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002399 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002400 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002401
Gilles Peskine8817f612018-12-18 00:18:46 +01002402 PSA_ASSERT( psa_import_key( handle, key_type,
2403 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002404
Gilles Peskine8817f612018-12-18 00:18:46 +01002405 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
2406 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002407
Gilles Peskine8817f612018-12-18 00:18:46 +01002408 PSA_ASSERT( psa_cipher_set_iv( &operation,
2409 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002410
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002411 output_buffer_size = ( (size_t) input->len +
2412 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002413 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002414
Gilles Peskine8817f612018-12-18 00:18:46 +01002415 PSA_ASSERT( psa_cipher_update( &operation,
2416 input->x, input->len,
2417 output, output_buffer_size,
2418 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002419 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002420 status = psa_cipher_finish( &operation,
2421 output + function_output_length,
2422 output_buffer_size,
2423 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002424 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002425 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002426
2427 if( expected_status == PSA_SUCCESS )
2428 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002429 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002430 ASSERT_COMPARE( expected_output->x, expected_output->len,
2431 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002432 }
2433
Gilles Peskine50e586b2018-06-08 14:28:46 +02002434exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002435 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002436 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002437 mbedtls_psa_crypto_free( );
2438}
2439/* END_CASE */
2440
Gilles Peskine50e586b2018-06-08 14:28:46 +02002441/* BEGIN_CASE */
2442void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002443 data_t *key,
2444 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002445{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002446 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002447 psa_key_type_t key_type = key_type_arg;
2448 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002449 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002450 size_t iv_size = 16;
2451 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002452 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002453 size_t output1_size = 0;
2454 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002455 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002456 size_t output2_size = 0;
2457 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002458 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002459 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2460 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002461 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002462
Gilles Peskine8817f612018-12-18 00:18:46 +01002463 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002464
Gilles Peskine8817f612018-12-18 00:18:46 +01002465 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2466 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002467 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002468 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002469
Gilles Peskine8817f612018-12-18 00:18:46 +01002470 PSA_ASSERT( psa_import_key( handle, key_type,
2471 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002472
Gilles Peskine8817f612018-12-18 00:18:46 +01002473 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2474 handle, alg ) );
2475 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2476 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002477
Gilles Peskine8817f612018-12-18 00:18:46 +01002478 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2479 iv, iv_size,
2480 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002481 output1_size = ( (size_t) input->len +
2482 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002483 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002484
Gilles Peskine8817f612018-12-18 00:18:46 +01002485 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2486 output1, output1_size,
2487 &output1_length ) );
2488 PSA_ASSERT( psa_cipher_finish( &operation1,
2489 output1 + output1_length, output1_size,
2490 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002491
Gilles Peskine048b7f02018-06-08 14:20:49 +02002492 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002493
Gilles Peskine8817f612018-12-18 00:18:46 +01002494 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002495
2496 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002497 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002498
Gilles Peskine8817f612018-12-18 00:18:46 +01002499 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2500 iv, iv_length ) );
2501 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2502 output2, output2_size,
2503 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002504 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002505 PSA_ASSERT( psa_cipher_finish( &operation2,
2506 output2 + output2_length,
2507 output2_size,
2508 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002509
Gilles Peskine048b7f02018-06-08 14:20:49 +02002510 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002511
Gilles Peskine8817f612018-12-18 00:18:46 +01002512 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002513
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002514 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002515
2516exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002517 mbedtls_free( output1 );
2518 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002519 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03002520 mbedtls_psa_crypto_free( );
2521}
2522/* END_CASE */
2523
2524/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002525void cipher_verify_output_multipart( int alg_arg,
2526 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002527 data_t *key,
2528 data_t *input,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002529 int first_part_size )
Moran Pekerded84402018-06-06 16:36:50 +03002530{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002531 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002532 psa_key_type_t key_type = key_type_arg;
2533 psa_algorithm_t alg = alg_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002534 unsigned char iv[16] = {0};
2535 size_t iv_size = 16;
2536 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002537 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002538 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002539 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002540 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002541 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002542 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002543 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002544 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2545 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002546 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002547
Gilles Peskine8817f612018-12-18 00:18:46 +01002548 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002549
Gilles Peskine8817f612018-12-18 00:18:46 +01002550 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ),
2551 &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002552 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002553 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002554
Gilles Peskine8817f612018-12-18 00:18:46 +01002555 PSA_ASSERT( psa_import_key( handle, key_type,
2556 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03002557
Gilles Peskine8817f612018-12-18 00:18:46 +01002558 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
2559 handle, alg ) );
2560 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
2561 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002562
Gilles Peskine8817f612018-12-18 00:18:46 +01002563 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2564 iv, iv_size,
2565 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002566 output1_buffer_size = ( (size_t) input->len +
2567 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002568 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002569
Gilles Peskine4abf7412018-06-18 16:35:34 +02002570 TEST_ASSERT( (unsigned int) first_part_size < input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002571
Gilles Peskine8817f612018-12-18 00:18:46 +01002572 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2573 output1, output1_buffer_size,
2574 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002575 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002576
Gilles Peskine8817f612018-12-18 00:18:46 +01002577 PSA_ASSERT( psa_cipher_update( &operation1,
2578 input->x + first_part_size,
2579 input->len - first_part_size,
2580 output1, output1_buffer_size,
2581 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002582 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002583
Gilles Peskine8817f612018-12-18 00:18:46 +01002584 PSA_ASSERT( psa_cipher_finish( &operation1,
2585 output1 + output1_length,
2586 output1_buffer_size - output1_length,
2587 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002588 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002589
Gilles Peskine8817f612018-12-18 00:18:46 +01002590 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002591
Gilles Peskine048b7f02018-06-08 14:20:49 +02002592 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002593 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002594
Gilles Peskine8817f612018-12-18 00:18:46 +01002595 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2596 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03002597
Gilles Peskine8817f612018-12-18 00:18:46 +01002598 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2599 output2, output2_buffer_size,
2600 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002601 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002602
Gilles Peskine8817f612018-12-18 00:18:46 +01002603 PSA_ASSERT( psa_cipher_update( &operation2,
2604 output1 + first_part_size,
2605 output1_length - first_part_size,
2606 output2, output2_buffer_size,
2607 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002608 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002609
Gilles Peskine8817f612018-12-18 00:18:46 +01002610 PSA_ASSERT( psa_cipher_finish( &operation2,
2611 output2 + output2_length,
2612 output2_buffer_size - output2_length,
2613 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002614 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002615
Gilles Peskine8817f612018-12-18 00:18:46 +01002616 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002617
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002618 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002619
2620exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002621 mbedtls_free( output1 );
2622 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002623 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002624 mbedtls_psa_crypto_free( );
2625}
2626/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02002627
Gilles Peskine20035e32018-02-03 22:44:14 +01002628/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002629void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002630 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02002631 data_t *nonce,
2632 data_t *additional_data,
2633 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002634 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002635{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002636 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002637 psa_key_type_t key_type = key_type_arg;
2638 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002639 unsigned char *output_data = NULL;
2640 size_t output_size = 0;
2641 size_t output_length = 0;
2642 unsigned char *output_data2 = NULL;
2643 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002644 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002645 psa_status_t expected_result = expected_result_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002646 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002647
Gilles Peskine4abf7412018-06-18 16:35:34 +02002648 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002649 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002650
Gilles Peskine8817f612018-12-18 00:18:46 +01002651 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002652
Gilles Peskine8817f612018-12-18 00:18:46 +01002653 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2654 &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002655 psa_key_policy_set_usage( &policy,
2656 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2657 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002658 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002659
Gilles Peskine8817f612018-12-18 00:18:46 +01002660 PSA_ASSERT( psa_import_key( handle, key_type,
2661 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002662
Gilles Peskinefe11b722018-12-18 00:24:04 +01002663 TEST_EQUAL( psa_aead_encrypt( handle, alg,
2664 nonce->x, nonce->len,
2665 additional_data->x,
2666 additional_data->len,
2667 input_data->x, input_data->len,
2668 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002669 &output_length ),
2670 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002671
2672 if( PSA_SUCCESS == expected_result )
2673 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002674 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002675
Gilles Peskinefe11b722018-12-18 00:24:04 +01002676 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2677 nonce->x, nonce->len,
2678 additional_data->x,
2679 additional_data->len,
2680 output_data, output_length,
2681 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002682 &output_length2 ),
2683 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02002684
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002685 ASSERT_COMPARE( input_data->x, input_data->len,
2686 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002687 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002688
Gilles Peskinea1cac842018-06-11 19:33:02 +02002689exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002690 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002691 mbedtls_free( output_data );
2692 mbedtls_free( output_data2 );
2693 mbedtls_psa_crypto_free( );
2694}
2695/* END_CASE */
2696
2697/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002698void aead_encrypt( int key_type_arg, data_t *key_data,
2699 int alg_arg,
2700 data_t *nonce,
2701 data_t *additional_data,
2702 data_t *input_data,
2703 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002704{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002705 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002706 psa_key_type_t key_type = key_type_arg;
2707 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002708 unsigned char *output_data = NULL;
2709 size_t output_size = 0;
2710 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002711 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002712 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002713
Gilles Peskine4abf7412018-06-18 16:35:34 +02002714 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002715 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002716
Gilles Peskine8817f612018-12-18 00:18:46 +01002717 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002718
Gilles Peskine8817f612018-12-18 00:18:46 +01002719 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2720 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002721 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002722 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002723
Gilles Peskine8817f612018-12-18 00:18:46 +01002724 PSA_ASSERT( psa_import_key( handle, key_type,
2725 key_data->x,
2726 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002727
Gilles Peskine8817f612018-12-18 00:18:46 +01002728 PSA_ASSERT( psa_aead_encrypt( handle, alg,
2729 nonce->x, nonce->len,
2730 additional_data->x, additional_data->len,
2731 input_data->x, input_data->len,
2732 output_data, output_size,
2733 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002734
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002735 ASSERT_COMPARE( expected_result->x, expected_result->len,
2736 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02002737
Gilles Peskinea1cac842018-06-11 19:33:02 +02002738exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002739 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002740 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002741 mbedtls_psa_crypto_free( );
2742}
2743/* END_CASE */
2744
2745/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02002746void aead_decrypt( int key_type_arg, data_t *key_data,
2747 int alg_arg,
2748 data_t *nonce,
2749 data_t *additional_data,
2750 data_t *input_data,
2751 data_t *expected_data,
2752 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02002753{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002754 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002755 psa_key_type_t key_type = key_type_arg;
2756 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002757 unsigned char *output_data = NULL;
2758 size_t output_size = 0;
2759 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002760 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00002761 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02002762 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02002763
Gilles Peskine4abf7412018-06-18 16:35:34 +02002764 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002765 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002766
Gilles Peskine8817f612018-12-18 00:18:46 +01002767 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002768
Gilles Peskine8817f612018-12-18 00:18:46 +01002769 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
2770 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002771 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002772 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002773
Gilles Peskine8817f612018-12-18 00:18:46 +01002774 PSA_ASSERT( psa_import_key( handle, key_type,
2775 key_data->x,
2776 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002777
Gilles Peskinefe11b722018-12-18 00:24:04 +01002778 TEST_EQUAL( psa_aead_decrypt( handle, alg,
2779 nonce->x, nonce->len,
2780 additional_data->x,
2781 additional_data->len,
2782 input_data->x, input_data->len,
2783 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002784 &output_length ),
2785 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002786
Gilles Peskine2d277862018-06-18 15:41:12 +02002787 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002788 ASSERT_COMPARE( expected_data->x, expected_data->len,
2789 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002790
Gilles Peskinea1cac842018-06-11 19:33:02 +02002791exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002792 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002793 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02002794 mbedtls_psa_crypto_free( );
2795}
2796/* END_CASE */
2797
2798/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002799void signature_size( int type_arg,
2800 int bits,
2801 int alg_arg,
2802 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002803{
2804 psa_key_type_t type = type_arg;
2805 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02002806 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002807 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002808exit:
2809 ;
2810}
2811/* END_CASE */
2812
2813/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002814void sign_deterministic( int key_type_arg, data_t *key_data,
2815 int alg_arg, data_t *input_data,
2816 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01002817{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002818 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01002819 psa_key_type_t key_type = key_type_arg;
2820 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002821 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01002822 unsigned char *signature = NULL;
2823 size_t signature_size;
2824 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002825 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01002826
Gilles Peskine8817f612018-12-18 00:18:46 +01002827 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002828
Gilles Peskine8817f612018-12-18 00:18:46 +01002829 PSA_ASSERT( psa_allocate_key( key_type,
2830 KEY_BITS_FROM_DATA( key_type, key_data ),
2831 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002832 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002833 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002834
Gilles Peskine8817f612018-12-18 00:18:46 +01002835 PSA_ASSERT( psa_import_key( handle, key_type,
2836 key_data->x,
2837 key_data->len ) );
2838 PSA_ASSERT( psa_get_key_information( handle,
2839 NULL,
2840 &key_bits ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002841
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002842 /* Allocate a buffer which has the size advertized by the
2843 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002844 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2845 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01002846 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02002847 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002848 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002849
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002850 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002851 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2852 input_data->x, input_data->len,
2853 signature, signature_size,
2854 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002855 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002856 ASSERT_COMPARE( output_data->x, output_data->len,
2857 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002858
2859exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002860 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01002861 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01002862 mbedtls_psa_crypto_free( );
2863}
2864/* END_CASE */
2865
2866/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002867void sign_fail( int key_type_arg, data_t *key_data,
2868 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002869 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002870{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002871 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01002872 psa_key_type_t key_type = key_type_arg;
2873 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002874 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01002875 psa_status_t actual_status;
2876 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01002877 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01002878 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002879 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01002880
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002881 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002882
Gilles Peskine8817f612018-12-18 00:18:46 +01002883 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002884
Gilles Peskine8817f612018-12-18 00:18:46 +01002885 PSA_ASSERT( psa_allocate_key( key_type,
2886 KEY_BITS_FROM_DATA( key_type, key_data ),
2887 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002888 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002889 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07002890
Gilles Peskine8817f612018-12-18 00:18:46 +01002891 PSA_ASSERT( psa_import_key( handle, key_type,
2892 key_data->x,
2893 key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002894
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002895 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02002896 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01002897 signature, signature_size,
2898 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002899 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02002900 /* The value of *signature_length is unspecified on error, but
2901 * whatever it is, it should be less than signature_size, so that
2902 * if the caller tries to read *signature_length bytes without
2903 * checking the error code then they don't overflow a buffer. */
2904 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01002905
2906exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002907 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01002908 mbedtls_free( signature );
2909 mbedtls_psa_crypto_free( );
2910}
2911/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03002912
2913/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02002914void sign_verify( int key_type_arg, data_t *key_data,
2915 int alg_arg, data_t *input_data )
2916{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002917 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02002918 psa_key_type_t key_type = key_type_arg;
2919 psa_algorithm_t alg = alg_arg;
2920 size_t key_bits;
2921 unsigned char *signature = NULL;
2922 size_t signature_size;
2923 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00002924 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02002925
Gilles Peskine8817f612018-12-18 00:18:46 +01002926 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002927
Gilles Peskine8817f612018-12-18 00:18:46 +01002928 PSA_ASSERT( psa_allocate_key( key_type,
2929 KEY_BITS_FROM_DATA( key_type, key_data ),
2930 &handle ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002931 psa_key_policy_set_usage( &policy,
2932 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2933 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002934 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002935
Gilles Peskine8817f612018-12-18 00:18:46 +01002936 PSA_ASSERT( psa_import_key( handle, key_type,
2937 key_data->x,
2938 key_data->len ) );
2939 PSA_ASSERT( psa_get_key_information( handle,
2940 NULL,
2941 &key_bits ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002942
2943 /* Allocate a buffer which has the size advertized by the
2944 * library. */
2945 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
2946 key_bits, alg );
2947 TEST_ASSERT( signature_size != 0 );
2948 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002949 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02002950
2951 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002952 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
2953 input_data->x, input_data->len,
2954 signature, signature_size,
2955 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002956 /* Check that the signature length looks sensible. */
2957 TEST_ASSERT( signature_length <= signature_size );
2958 TEST_ASSERT( signature_length > 0 );
2959
2960 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002961 PSA_ASSERT( psa_asymmetric_verify(
2962 handle, alg,
2963 input_data->x, input_data->len,
2964 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02002965
2966 if( input_data->len != 0 )
2967 {
2968 /* Flip a bit in the input and verify that the signature is now
2969 * detected as invalid. Flip a bit at the beginning, not at the end,
2970 * because ECDSA may ignore the last few bits of the input. */
2971 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002972 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
2973 input_data->x, input_data->len,
2974 signature, signature_length ),
2975 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02002976 }
2977
2978exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002979 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02002980 mbedtls_free( signature );
2981 mbedtls_psa_crypto_free( );
2982}
2983/* END_CASE */
2984
2985/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002986void asymmetric_verify( int key_type_arg, data_t *key_data,
2987 int alg_arg, data_t *hash_data,
2988 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03002989{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002990 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03002991 psa_key_type_t key_type = key_type_arg;
2992 psa_algorithm_t alg = alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00002993 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03002994
Gilles Peskine69c12672018-06-28 00:07:19 +02002995 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
2996
Gilles Peskine8817f612018-12-18 00:18:46 +01002997 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03002998
Gilles Peskine8817f612018-12-18 00:18:46 +01002999 PSA_ASSERT( psa_allocate_key( key_type,
3000 KEY_BITS_FROM_DATA( key_type, key_data ),
3001 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003002 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003003 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
itayzafrir5c753392018-05-08 11:18:38 +03003004
Gilles Peskine8817f612018-12-18 00:18:46 +01003005 PSA_ASSERT( psa_import_key( handle, key_type,
3006 key_data->x,
3007 key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003008
Gilles Peskine8817f612018-12-18 00:18:46 +01003009 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3010 hash_data->x, hash_data->len,
3011 signature_data->x,
3012 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003013exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003014 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03003015 mbedtls_psa_crypto_free( );
3016}
3017/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003018
3019/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003020void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3021 int alg_arg, data_t *hash_data,
3022 data_t *signature_data,
3023 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003024{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003025 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003026 psa_key_type_t key_type = key_type_arg;
3027 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003028 psa_status_t actual_status;
3029 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003030 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003031
Gilles Peskine8817f612018-12-18 00:18:46 +01003032 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003033
Gilles Peskine8817f612018-12-18 00:18:46 +01003034 PSA_ASSERT( psa_allocate_key( key_type,
3035 KEY_BITS_FROM_DATA( key_type, key_data ),
3036 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003037 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003038 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003039
Gilles Peskine8817f612018-12-18 00:18:46 +01003040 PSA_ASSERT( psa_import_key( handle, key_type,
3041 key_data->x,
3042 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003043
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003044 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003045 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003046 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003047 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003048
Gilles Peskinefe11b722018-12-18 00:24:04 +01003049 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003050
3051exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003052 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003053 mbedtls_psa_crypto_free( );
3054}
3055/* END_CASE */
3056
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003057/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003058void asymmetric_encrypt( int key_type_arg,
3059 data_t *key_data,
3060 int alg_arg,
3061 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003062 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003063 int expected_output_length_arg,
3064 int expected_status_arg )
3065{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003066 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003067 psa_key_type_t key_type = key_type_arg;
3068 psa_algorithm_t alg = alg_arg;
3069 size_t expected_output_length = expected_output_length_arg;
3070 size_t key_bits;
3071 unsigned char *output = NULL;
3072 size_t output_size;
3073 size_t output_length = ~0;
3074 psa_status_t actual_status;
3075 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003076 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003077
Gilles Peskine8817f612018-12-18 00:18:46 +01003078 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003079
Gilles Peskine656896e2018-06-29 19:12:28 +02003080 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01003081 PSA_ASSERT( psa_allocate_key( key_type,
3082 KEY_BITS_FROM_DATA( key_type, key_data ),
3083 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003084 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003085 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
3086 PSA_ASSERT( psa_import_key( handle, key_type,
3087 key_data->x,
3088 key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003089
3090 /* Determine the maximum output length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003091 PSA_ASSERT( psa_get_key_information( handle,
3092 NULL,
3093 &key_bits ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003094 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003095 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003096
3097 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003098 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003099 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003100 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003101 output, output_size,
3102 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003103 TEST_EQUAL( actual_status, expected_status );
3104 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003105
Gilles Peskine68428122018-06-30 18:42:41 +02003106 /* If the label is empty, the test framework puts a non-null pointer
3107 * in label->x. Test that a null pointer works as well. */
3108 if( label->len == 0 )
3109 {
3110 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003111 if( output_size != 0 )
3112 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003113 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003114 input_data->x, input_data->len,
3115 NULL, label->len,
3116 output, output_size,
3117 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003118 TEST_EQUAL( actual_status, expected_status );
3119 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003120 }
3121
Gilles Peskine656896e2018-06-29 19:12:28 +02003122exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003123 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003124 mbedtls_free( output );
3125 mbedtls_psa_crypto_free( );
3126}
3127/* END_CASE */
3128
3129/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003130void asymmetric_encrypt_decrypt( int key_type_arg,
3131 data_t *key_data,
3132 int alg_arg,
3133 data_t *input_data,
3134 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003135{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003136 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003137 psa_key_type_t key_type = key_type_arg;
3138 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003139 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003140 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003141 size_t output_size;
3142 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003143 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003144 size_t output2_size;
3145 size_t output2_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003146 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003147
Gilles Peskine8817f612018-12-18 00:18:46 +01003148 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003149
Gilles Peskine8817f612018-12-18 00:18:46 +01003150 PSA_ASSERT( psa_allocate_key( key_type,
3151 KEY_BITS_FROM_DATA( key_type, key_data ),
3152 &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003153 psa_key_policy_set_usage( &policy,
3154 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003155 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003156 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003157
Gilles Peskine8817f612018-12-18 00:18:46 +01003158 PSA_ASSERT( psa_import_key( handle, key_type,
3159 key_data->x,
3160 key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003161
3162 /* Determine the maximum ciphertext length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003163 PSA_ASSERT( psa_get_key_information( handle,
3164 NULL,
3165 &key_bits ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003166 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003167 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003168 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003169 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003170
Gilles Peskineeebd7382018-06-08 18:11:54 +02003171 /* We test encryption by checking that encrypt-then-decrypt gives back
3172 * the original plaintext because of the non-optional random
3173 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003174 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3175 input_data->x, input_data->len,
3176 label->x, label->len,
3177 output, output_size,
3178 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003179 /* We don't know what ciphertext length to expect, but check that
3180 * it looks sensible. */
3181 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003182
Gilles Peskine8817f612018-12-18 00:18:46 +01003183 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3184 output, output_length,
3185 label->x, label->len,
3186 output2, output2_size,
3187 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003188 ASSERT_COMPARE( input_data->x, input_data->len,
3189 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003190
3191exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003192 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003193 mbedtls_free( output );
3194 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003195 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003196}
3197/* END_CASE */
3198
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003199/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003200void asymmetric_decrypt( int key_type_arg,
3201 data_t *key_data,
3202 int alg_arg,
3203 data_t *input_data,
3204 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003205 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003206{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003207 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003208 psa_key_type_t key_type = key_type_arg;
3209 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003210 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003211 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003212 size_t output_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003213 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003214
Gilles Peskine4abf7412018-06-18 16:35:34 +02003215 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003216 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003217
Gilles Peskine8817f612018-12-18 00:18:46 +01003218 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003219
Gilles Peskine8817f612018-12-18 00:18:46 +01003220 PSA_ASSERT( psa_allocate_key( key_type,
3221 KEY_BITS_FROM_DATA( key_type, key_data ),
3222 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003223 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003224 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003225
Gilles Peskine8817f612018-12-18 00:18:46 +01003226 PSA_ASSERT( psa_import_key( handle, key_type,
3227 key_data->x,
3228 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003229
Gilles Peskine8817f612018-12-18 00:18:46 +01003230 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3231 input_data->x, input_data->len,
3232 label->x, label->len,
3233 output,
3234 output_size,
3235 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003236 ASSERT_COMPARE( expected_data->x, expected_data->len,
3237 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003238
Gilles Peskine68428122018-06-30 18:42:41 +02003239 /* If the label is empty, the test framework puts a non-null pointer
3240 * in label->x. Test that a null pointer works as well. */
3241 if( label->len == 0 )
3242 {
3243 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003244 if( output_size != 0 )
3245 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003246 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3247 input_data->x, input_data->len,
3248 NULL, label->len,
3249 output,
3250 output_size,
3251 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003252 ASSERT_COMPARE( expected_data->x, expected_data->len,
3253 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003254 }
3255
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003256exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003257 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003258 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003259 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003260}
3261/* END_CASE */
3262
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003263/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003264void asymmetric_decrypt_fail( int key_type_arg,
3265 data_t *key_data,
3266 int alg_arg,
3267 data_t *input_data,
3268 data_t *label,
Gilles Peskine2d277862018-06-18 15:41:12 +02003269 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003270{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003271 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003272 psa_key_type_t key_type = key_type_arg;
3273 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003274 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003275 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003276 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003277 psa_status_t actual_status;
3278 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003279 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003280
Gilles Peskine4abf7412018-06-18 16:35:34 +02003281 output_size = key_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003282 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003283
Gilles Peskine8817f612018-12-18 00:18:46 +01003284 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003285
Gilles Peskine8817f612018-12-18 00:18:46 +01003286 PSA_ASSERT( psa_allocate_key( key_type,
3287 KEY_BITS_FROM_DATA( key_type, key_data ),
3288 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003289 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003290 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003291
Gilles Peskine8817f612018-12-18 00:18:46 +01003292 PSA_ASSERT( psa_import_key( handle, key_type,
3293 key_data->x,
3294 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003295
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003296 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003297 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003298 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003299 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003300 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003301 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003302 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003303
Gilles Peskine68428122018-06-30 18:42:41 +02003304 /* If the label is empty, the test framework puts a non-null pointer
3305 * in label->x. Test that a null pointer works as well. */
3306 if( label->len == 0 )
3307 {
3308 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003309 if( output_size != 0 )
3310 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003311 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003312 input_data->x, input_data->len,
3313 NULL, label->len,
3314 output, output_size,
3315 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003316 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003317 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003318 }
3319
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003320exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003321 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02003322 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003323 mbedtls_psa_crypto_free( );
3324}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003325/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003326
3327/* BEGIN_CASE */
Jaeden Amerod94d6712019-01-04 14:11:48 +00003328void crypto_generator_init( )
3329{
3330 /* Test each valid way of initializing the object, except for `= {0}`, as
3331 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3332 * though it's OK by the C standard. We could test for this, but we'd need
3333 * to supress the Clang warning for the test. */
3334 psa_crypto_generator_t func = psa_crypto_generator_init( );
3335 psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT;
3336 psa_crypto_generator_t zero;
3337
3338 memset( &zero, 0, sizeof( zero ) );
3339
3340 /* Although not technically guaranteed by the C standard nor the PSA Crypto
3341 * specification, we test that all valid ways of initializing the object
3342 * have the same bit pattern. This is a stronger requirement that may not
3343 * be valid on all platforms or PSA Crypto implementations, but implies the
3344 * weaker actual requirement is met: that a freshly initialized object, no
3345 * matter how it was initialized, acts the same as any other valid
3346 * initialization. */
3347 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
3348 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
3349}
3350/* END_CASE */
3351
3352/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003353void derive_setup( int key_type_arg,
3354 data_t *key_data,
3355 int alg_arg,
3356 data_t *salt,
3357 data_t *label,
3358 int requested_capacity_arg,
3359 int expected_status_arg )
3360{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003361 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003362 size_t key_type = key_type_arg;
3363 psa_algorithm_t alg = alg_arg;
3364 size_t requested_capacity = requested_capacity_arg;
3365 psa_status_t expected_status = expected_status_arg;
3366 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003367 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003368
Gilles Peskine8817f612018-12-18 00:18:46 +01003369 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003370
Gilles Peskine8817f612018-12-18 00:18:46 +01003371 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ),
3372 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003373 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003374 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003375
Gilles Peskine8817f612018-12-18 00:18:46 +01003376 PSA_ASSERT( psa_import_key( handle, key_type,
3377 key_data->x,
3378 key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003379
Gilles Peskinefe11b722018-12-18 00:24:04 +01003380 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3381 salt->x, salt->len,
3382 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003383 requested_capacity ),
3384 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003385
3386exit:
3387 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003388 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003389 mbedtls_psa_crypto_free( );
3390}
3391/* END_CASE */
3392
3393/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003394void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003395{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003396 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02003397 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003398 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003399 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003400 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003401 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02003402 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3403 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
3404 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Jaeden Amero70261c52019-01-04 11:47:20 +00003405 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003406
Gilles Peskine8817f612018-12-18 00:18:46 +01003407 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003408
Gilles Peskine8817f612018-12-18 00:18:46 +01003409 PSA_ASSERT( psa_allocate_key( key_type,
3410 PSA_BYTES_TO_BITS( sizeof( key_data ) ),
3411 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003412 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003413 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003414
Gilles Peskine8817f612018-12-18 00:18:46 +01003415 PSA_ASSERT( psa_import_key( handle, key_type,
3416 key_data,
3417 sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003418
3419 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01003420 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3421 NULL, 0,
3422 NULL, 0,
3423 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003424
3425 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003426 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
3427 NULL, 0,
3428 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003429 capacity ),
3430 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003431
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003432 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003433
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003434 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
3435 PSA_ERROR_INSUFFICIENT_CAPACITY );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003436
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003437exit:
3438 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003439 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003440 mbedtls_psa_crypto_free( );
3441}
3442/* END_CASE */
3443
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003444/* BEGIN_CASE */
3445void test_derive_invalid_generator_tests( )
3446{
3447 uint8_t output_buffer[16];
3448 size_t buffer_size = 16;
3449 size_t capacity = 0;
3450 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3451
Nir Sonnenschein50789302018-10-31 12:16:38 +02003452 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003453 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003454
3455 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003456 == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003457
Gilles Peskine8817f612018-12-18 00:18:46 +01003458 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003459
Nir Sonnenschein50789302018-10-31 12:16:38 +02003460 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003461 == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003462
Nir Sonnenschein50789302018-10-31 12:16:38 +02003463 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02003464 == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03003465
3466exit:
3467 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03003468}
3469/* END_CASE */
3470
3471/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003472void derive_output( int alg_arg,
3473 data_t *key_data,
3474 data_t *salt,
3475 data_t *label,
3476 int requested_capacity_arg,
3477 data_t *expected_output1,
3478 data_t *expected_output2 )
3479{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003480 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003481 psa_algorithm_t alg = alg_arg;
3482 size_t requested_capacity = requested_capacity_arg;
3483 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3484 uint8_t *expected_outputs[2] =
3485 {expected_output1->x, expected_output2->x};
3486 size_t output_sizes[2] =
3487 {expected_output1->len, expected_output2->len};
3488 size_t output_buffer_size = 0;
3489 uint8_t *output_buffer = NULL;
3490 size_t expected_capacity;
3491 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003492 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003493 psa_status_t status;
3494 unsigned i;
3495
3496 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3497 {
3498 if( output_sizes[i] > output_buffer_size )
3499 output_buffer_size = output_sizes[i];
3500 if( output_sizes[i] == 0 )
3501 expected_outputs[i] = NULL;
3502 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003503 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003504 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003505
Gilles Peskine8817f612018-12-18 00:18:46 +01003506 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3507 PSA_BYTES_TO_BITS( key_data->len ),
3508 &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003509 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003510 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003511
Gilles Peskine8817f612018-12-18 00:18:46 +01003512 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3513 key_data->x,
3514 key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003515
3516 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003517 if( PSA_ALG_IS_HKDF( alg ) )
3518 {
3519 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
3520 PSA_ASSERT( psa_set_generator_capacity( &generator,
3521 requested_capacity ) );
3522 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
3523 PSA_KDF_STEP_SALT,
3524 salt->x, salt->len ) );
3525 PSA_ASSERT( psa_key_derivation_input_key( &generator,
3526 PSA_KDF_STEP_SECRET,
3527 handle ) );
3528 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
3529 PSA_KDF_STEP_INFO,
3530 label->x, label->len ) );
3531 }
3532 else
3533 {
3534 // legacy
3535 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3536 salt->x, salt->len,
3537 label->x, label->len,
3538 requested_capacity ) );
3539 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003540 PSA_ASSERT( psa_get_generator_capacity( &generator,
3541 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003542 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003543 expected_capacity = requested_capacity;
3544
3545 /* Expansion phase. */
3546 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
3547 {
3548 /* Read some bytes. */
3549 status = psa_generator_read( &generator,
3550 output_buffer, output_sizes[i] );
3551 if( expected_capacity == 0 && output_sizes[i] == 0 )
3552 {
3553 /* Reading 0 bytes when 0 bytes are available can go either way. */
3554 TEST_ASSERT( status == PSA_SUCCESS ||
3555 status == PSA_ERROR_INSUFFICIENT_CAPACITY );
3556 continue;
3557 }
3558 else if( expected_capacity == 0 ||
3559 output_sizes[i] > expected_capacity )
3560 {
3561 /* Capacity exceeded. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01003562 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003563 expected_capacity = 0;
3564 continue;
3565 }
3566 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003567 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003568 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003569 ASSERT_COMPARE( output_buffer, output_sizes[i],
3570 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003571 /* Check the generator status. */
3572 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01003573 PSA_ASSERT( psa_get_generator_capacity( &generator,
3574 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003575 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003576 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003577 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003578
3579exit:
3580 mbedtls_free( output_buffer );
3581 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003582 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02003583 mbedtls_psa_crypto_free( );
3584}
3585/* END_CASE */
3586
3587/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02003588void derive_full( int alg_arg,
3589 data_t *key_data,
3590 data_t *salt,
3591 data_t *label,
3592 int requested_capacity_arg )
3593{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003594 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02003595 psa_algorithm_t alg = alg_arg;
3596 size_t requested_capacity = requested_capacity_arg;
3597 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3598 unsigned char output_buffer[16];
3599 size_t expected_capacity = requested_capacity;
3600 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00003601 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02003602
Gilles Peskine8817f612018-12-18 00:18:46 +01003603 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003604
Gilles Peskine8817f612018-12-18 00:18:46 +01003605 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3606 PSA_BYTES_TO_BITS( key_data->len ),
3607 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003608 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003609 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003610
Gilles Peskine8817f612018-12-18 00:18:46 +01003611 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
3612 key_data->x,
3613 key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003614
3615 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003616 if( PSA_ALG_IS_HKDF( alg ) )
3617 {
3618 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
3619 PSA_ASSERT( psa_set_generator_capacity( &generator,
3620 requested_capacity ) );
3621 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
3622 PSA_KDF_STEP_SALT,
3623 salt->x, salt->len ) );
3624 PSA_ASSERT( psa_key_derivation_input_key( &generator,
3625 PSA_KDF_STEP_SECRET,
3626 handle ) );
3627 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
3628 PSA_KDF_STEP_INFO,
3629 label->x, label->len ) );
3630 }
3631 else
3632 {
3633 // legacy
3634 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
3635 salt->x, salt->len,
3636 label->x, label->len,
3637 requested_capacity ) );
3638 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003639 PSA_ASSERT( psa_get_generator_capacity( &generator,
3640 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003641 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003642
3643 /* Expansion phase. */
3644 while( current_capacity > 0 )
3645 {
3646 size_t read_size = sizeof( output_buffer );
3647 if( read_size > current_capacity )
3648 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01003649 PSA_ASSERT( psa_generator_read( &generator,
3650 output_buffer,
3651 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003652 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01003653 PSA_ASSERT( psa_get_generator_capacity( &generator,
3654 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003655 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02003656 }
3657
3658 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003659 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
3660 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskined54931c2018-07-17 21:06:59 +02003661
Gilles Peskine8817f612018-12-18 00:18:46 +01003662 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02003663
3664exit:
3665 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003666 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02003667 mbedtls_psa_crypto_free( );
3668}
3669/* END_CASE */
3670
3671/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02003672void derive_key_exercise( int alg_arg,
3673 data_t *key_data,
3674 data_t *salt,
3675 data_t *label,
3676 int derived_type_arg,
3677 int derived_bits_arg,
3678 int derived_usage_arg,
3679 int derived_alg_arg )
3680{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003681 psa_key_handle_t base_handle = 0;
3682 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003683 psa_algorithm_t alg = alg_arg;
3684 psa_key_type_t derived_type = derived_type_arg;
3685 size_t derived_bits = derived_bits_arg;
3686 psa_key_usage_t derived_usage = derived_usage_arg;
3687 psa_algorithm_t derived_alg = derived_alg_arg;
3688 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
3689 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003690 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003691 psa_key_type_t got_type;
3692 size_t got_bits;
3693
Gilles Peskine8817f612018-12-18 00:18:46 +01003694 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003695
Gilles Peskine8817f612018-12-18 00:18:46 +01003696 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3697 PSA_BYTES_TO_BITS( key_data->len ),
3698 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003699 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003700 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3701 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3702 key_data->x,
3703 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003704
3705 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003706 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3707 salt->x, salt->len,
3708 label->x, label->len,
3709 capacity ) );
3710 PSA_ASSERT( psa_allocate_key( derived_type, derived_bits,
3711 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003712 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003713 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3714 PSA_ASSERT( psa_generator_import_key( derived_handle,
3715 derived_type,
3716 derived_bits,
3717 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003718
3719 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01003720 PSA_ASSERT( psa_get_key_information( derived_handle,
3721 &got_type,
3722 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003723 TEST_EQUAL( got_type, derived_type );
3724 TEST_EQUAL( got_bits, derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003725
3726 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003727 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02003728 goto exit;
3729
3730exit:
3731 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003732 psa_destroy_key( base_handle );
3733 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003734 mbedtls_psa_crypto_free( );
3735}
3736/* END_CASE */
3737
3738/* BEGIN_CASE */
3739void derive_key_export( int alg_arg,
3740 data_t *key_data,
3741 data_t *salt,
3742 data_t *label,
3743 int bytes1_arg,
3744 int bytes2_arg )
3745{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003746 psa_key_handle_t base_handle = 0;
3747 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003748 psa_algorithm_t alg = alg_arg;
3749 size_t bytes1 = bytes1_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003750 size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003751 size_t bytes2 = bytes2_arg;
3752 size_t capacity = bytes1 + bytes2;
3753 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003754 uint8_t *output_buffer = NULL;
3755 uint8_t *export_buffer = NULL;
Jaeden Amero70261c52019-01-04 11:47:20 +00003756 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02003757 size_t length;
3758
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003759 ASSERT_ALLOC( output_buffer, capacity );
3760 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01003761 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003762
Gilles Peskine8817f612018-12-18 00:18:46 +01003763 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
3764 PSA_BYTES_TO_BITS( key_data->len ),
3765 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003766 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003767 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
3768 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
3769 key_data->x,
3770 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003771
3772 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003773 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3774 salt->x, salt->len,
3775 label->x, label->len,
3776 capacity ) );
3777 PSA_ASSERT( psa_generator_read( &generator,
3778 output_buffer,
3779 capacity ) );
3780 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003781
3782 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003783 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
3784 salt->x, salt->len,
3785 label->x, label->len,
3786 capacity ) );
3787 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, derived_bits,
3788 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003789 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003790 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3791 PSA_ASSERT( psa_generator_import_key( derived_handle,
3792 PSA_KEY_TYPE_RAW_DATA,
3793 derived_bits,
3794 &generator ) );
3795 PSA_ASSERT( psa_export_key( derived_handle,
3796 export_buffer, bytes1,
3797 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003798 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01003799 PSA_ASSERT( psa_destroy_key( derived_handle ) );
3800 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA,
3801 PSA_BYTES_TO_BITS( bytes2 ),
3802 &derived_handle ) );
3803 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
3804 PSA_ASSERT( psa_generator_import_key( derived_handle,
3805 PSA_KEY_TYPE_RAW_DATA,
3806 PSA_BYTES_TO_BITS( bytes2 ),
3807 &generator ) );
3808 PSA_ASSERT( psa_export_key( derived_handle,
3809 export_buffer + bytes1, bytes2,
3810 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003811 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003812
3813 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003814 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
3815 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003816
3817exit:
3818 mbedtls_free( output_buffer );
3819 mbedtls_free( export_buffer );
3820 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003821 psa_destroy_key( base_handle );
3822 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02003823 mbedtls_psa_crypto_free( );
3824}
3825/* END_CASE */
3826
3827/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02003828void key_agreement_setup( int alg_arg,
3829 int our_key_type_arg, data_t *our_key_data,
3830 data_t *peer_key_data,
3831 int expected_status_arg )
3832{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003833 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003834 psa_algorithm_t alg = alg_arg;
3835 psa_key_type_t our_key_type = our_key_type_arg;
3836 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003837 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02003838
Gilles Peskine8817f612018-12-18 00:18:46 +01003839 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003840
Gilles Peskine8817f612018-12-18 00:18:46 +01003841 PSA_ASSERT( psa_allocate_key( our_key_type,
3842 KEY_BITS_FROM_DATA( our_key_type,
3843 our_key_data ),
3844 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003845 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003846 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3847 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3848 our_key_data->x,
3849 our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003850
Gilles Peskinefe11b722018-12-18 00:24:04 +01003851 TEST_EQUAL( psa_key_agreement( &generator,
3852 our_key,
3853 peer_key_data->x, peer_key_data->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003854 alg ),
3855 expected_status_arg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02003856
3857exit:
3858 psa_generator_abort( &generator );
3859 psa_destroy_key( our_key );
3860 mbedtls_psa_crypto_free( );
3861}
3862/* END_CASE */
3863
3864/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02003865void key_agreement_capacity( int alg_arg,
3866 int our_key_type_arg, data_t *our_key_data,
3867 data_t *peer_key_data,
3868 int expected_capacity_arg )
3869{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003870 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003871 psa_algorithm_t alg = alg_arg;
3872 psa_key_type_t our_key_type = our_key_type_arg;
3873 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003874 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02003875 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02003876 unsigned char output[16];
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 Peskinebf491972018-10-25 22:36:12 +02003895 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003896 PSA_ASSERT( psa_get_generator_capacity(
3897 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003898 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02003899
Gilles Peskinebf491972018-10-25 22:36:12 +02003900 /* Test the actual capacity by reading the output. */
3901 while( actual_capacity > sizeof( output ) )
3902 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003903 PSA_ASSERT( psa_generator_read( &generator,
3904 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02003905 actual_capacity -= sizeof( output );
3906 }
Gilles Peskine8817f612018-12-18 00:18:46 +01003907 PSA_ASSERT( psa_generator_read( &generator,
3908 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003909 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
3910 PSA_ERROR_INSUFFICIENT_CAPACITY );
Gilles Peskinebf491972018-10-25 22:36:12 +02003911
Gilles Peskine59685592018-09-18 12:11:34 +02003912exit:
3913 psa_generator_abort( &generator );
3914 psa_destroy_key( our_key );
3915 mbedtls_psa_crypto_free( );
3916}
3917/* END_CASE */
3918
3919/* BEGIN_CASE */
3920void key_agreement_output( int alg_arg,
3921 int our_key_type_arg, data_t *our_key_data,
3922 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003923 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02003924{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003925 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02003926 psa_algorithm_t alg = alg_arg;
3927 psa_key_type_t our_key_type = our_key_type_arg;
3928 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003929 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003930 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02003931
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003932 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
3933 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003934
Gilles Peskine8817f612018-12-18 00:18:46 +01003935 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003936
Gilles Peskine8817f612018-12-18 00:18:46 +01003937 PSA_ASSERT( psa_allocate_key( our_key_type,
3938 KEY_BITS_FROM_DATA( our_key_type,
3939 our_key_data ),
3940 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003941 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003942 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
3943 PSA_ASSERT( psa_import_key( our_key, our_key_type,
3944 our_key_data->x,
3945 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003946
Gilles Peskine8817f612018-12-18 00:18:46 +01003947 PSA_ASSERT( psa_key_agreement( &generator,
3948 our_key,
3949 peer_key_data->x, peer_key_data->len,
3950 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02003951
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003952 PSA_ASSERT( psa_generator_read( &generator,
3953 actual_output,
3954 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003955 ASSERT_COMPARE( actual_output, expected_output1->len,
3956 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003957 if( expected_output2->len != 0 )
3958 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003959 PSA_ASSERT( psa_generator_read( &generator,
3960 actual_output,
3961 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01003962 ASSERT_COMPARE( actual_output, expected_output2->len,
3963 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02003964 }
Gilles Peskine59685592018-09-18 12:11:34 +02003965
3966exit:
3967 psa_generator_abort( &generator );
3968 psa_destroy_key( our_key );
3969 mbedtls_psa_crypto_free( );
3970 mbedtls_free( actual_output );
3971}
3972/* END_CASE */
3973
3974/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02003975void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02003976{
Gilles Peskinea50d7392018-06-21 10:22:13 +02003977 size_t bytes = bytes_arg;
3978 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003979 unsigned char *output = NULL;
3980 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02003981 size_t i;
3982 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02003983
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003984 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
3985 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003986 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003987
Gilles Peskine8817f612018-12-18 00:18:46 +01003988 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02003989
Gilles Peskinea50d7392018-06-21 10:22:13 +02003990 /* Run several times, to ensure that every output byte will be
3991 * nonzero at least once with overwhelming probability
3992 * (2^(-8*number_of_runs)). */
3993 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02003994 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003995 if( bytes != 0 )
3996 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01003997 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02003998
3999 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004000 ASSERT_COMPARE( output + bytes, sizeof( trail ),
4001 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004002
4003 for( i = 0; i < bytes; i++ )
4004 {
4005 if( output[i] != 0 )
4006 ++changed[i];
4007 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004008 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004009
4010 /* Check that every byte was changed to nonzero at least once. This
4011 * validates that psa_generate_random is overwriting every byte of
4012 * the output buffer. */
4013 for( i = 0; i < bytes; i++ )
4014 {
4015 TEST_ASSERT( changed[i] != 0 );
4016 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004017
4018exit:
4019 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004020 mbedtls_free( output );
4021 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004022}
4023/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004024
4025/* BEGIN_CASE */
4026void generate_key( int type_arg,
4027 int bits_arg,
4028 int usage_arg,
4029 int alg_arg,
4030 int expected_status_arg )
4031{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004032 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004033 psa_key_type_t type = type_arg;
4034 psa_key_usage_t usage = usage_arg;
4035 size_t bits = bits_arg;
4036 psa_algorithm_t alg = alg_arg;
4037 psa_status_t expected_status = expected_status_arg;
4038 psa_key_type_t got_type;
4039 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004040 psa_status_t expected_info_status =
4041 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004042 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004043
Gilles Peskine8817f612018-12-18 00:18:46 +01004044 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004045
Gilles Peskine8817f612018-12-18 00:18:46 +01004046 PSA_ASSERT( psa_allocate_key( type, bits, &handle ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004047 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004048 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004049
4050 /* Generate a key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004051 TEST_EQUAL( psa_generate_key( handle, type, bits, NULL, 0 ),
4052 expected_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004053
4054 /* Test the key information */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004055 TEST_EQUAL( psa_get_key_information( handle, &got_type, &got_bits ),
4056 expected_info_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004057 if( expected_info_status != PSA_SUCCESS )
4058 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004059 TEST_EQUAL( got_type, type );
4060 TEST_EQUAL( got_bits, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004061
Gilles Peskine818ca122018-06-20 18:16:48 +02004062 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004063 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004064 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004065
4066exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004067 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004068 mbedtls_psa_crypto_free( );
4069}
4070/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004071
Darryl Greend49a4992018-06-18 17:27:26 +01004072/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
4073void persistent_key_load_key_from_storage( data_t *data, int type_arg,
4074 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00004075 int alg_arg, int generation_method,
4076 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01004077{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004078 psa_key_handle_t handle = 0;
4079 psa_key_handle_t base_key;
Darryl Greend49a4992018-06-18 17:27:26 +01004080 psa_key_type_t type = (psa_key_type_t) type_arg;
4081 psa_key_type_t type_get;
4082 size_t bits_get;
Jaeden Amero70261c52019-01-04 11:47:20 +00004083 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
4084 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004085 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
4086 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00004087 psa_key_policy_t base_policy_set = PSA_KEY_POLICY_INIT;
Darryl Green0c6575a2018-11-07 16:05:30 +00004088 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
4089 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004090 unsigned char *first_export = NULL;
4091 unsigned char *second_export = NULL;
4092 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4093 size_t first_exported_length;
4094 size_t second_exported_length;
4095
4096 ASSERT_ALLOC( first_export, export_size );
4097 ASSERT_ALLOC( second_export, export_size );
4098
Gilles Peskine8817f612018-12-18 00:18:46 +01004099 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004100
Gilles Peskine8817f612018-12-18 00:18:46 +01004101 PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4102 type, bits,
4103 &handle ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004104 psa_key_policy_set_usage( &policy_set, policy_usage,
4105 policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004106 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004107
Darryl Green0c6575a2018-11-07 16:05:30 +00004108 switch( generation_method )
4109 {
4110 case IMPORT_KEY:
4111 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004112 PSA_ASSERT( psa_import_key( handle, type,
4113 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004114 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004115
Darryl Green0c6575a2018-11-07 16:05:30 +00004116 case GENERATE_KEY:
4117 /* Generate a key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004118 PSA_ASSERT( psa_generate_key( handle, type, bits,
4119 NULL, 0 ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004120 break;
4121
4122 case DERIVE_KEY:
4123 /* Create base key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004124 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE,
4125 PSA_BYTES_TO_BITS( data->len ),
4126 &base_key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004127 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
4128 base_policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004129 PSA_ASSERT( psa_set_key_policy(
4130 base_key, &base_policy_set ) );
4131 PSA_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
4132 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004133 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004134 PSA_ASSERT( psa_key_derivation( &generator, base_key,
4135 base_policy_alg,
4136 NULL, 0, NULL, 0,
4137 export_size ) );
4138 PSA_ASSERT( psa_generator_import_key(
4139 handle, PSA_KEY_TYPE_RAW_DATA,
4140 bits, &generator ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004141 break;
4142 }
Darryl Greend49a4992018-06-18 17:27:26 +01004143
4144 /* Export the key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004145 TEST_EQUAL( psa_export_key( handle,
4146 first_export, export_size,
4147 &first_exported_length ),
4148 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004149
4150 /* Shutdown and restart */
4151 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004152 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004153
Darryl Greend49a4992018-06-18 17:27:26 +01004154 /* Check key slot still contains key data */
Gilles Peskine8817f612018-12-18 00:18:46 +01004155 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4156 &handle ) );
4157 PSA_ASSERT( psa_get_key_information(
4158 handle, &type_get, &bits_get ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004159 TEST_EQUAL( type_get, type );
4160 TEST_EQUAL( bits_get, (size_t) bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004161
Gilles Peskine8817f612018-12-18 00:18:46 +01004162 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004163 TEST_EQUAL( psa_key_policy_get_usage( &policy_get ), policy_usage );
4164 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_get ), policy_alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004165
4166 /* Export the key again */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004167 TEST_EQUAL( psa_export_key( handle,
4168 second_export, export_size,
4169 &second_exported_length ),
4170 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004171
Darryl Green0c6575a2018-11-07 16:05:30 +00004172 if( export_status == PSA_SUCCESS )
4173 {
4174 ASSERT_COMPARE( first_export, first_exported_length,
4175 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01004176
Darryl Green0c6575a2018-11-07 16:05:30 +00004177 switch( generation_method )
4178 {
4179 case IMPORT_KEY:
4180 ASSERT_COMPARE( data->x, data->len,
4181 first_export, first_exported_length );
4182 break;
4183 default:
4184 break;
4185 }
4186 }
4187
4188 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004189 if( ! exercise_key( handle, policy_usage, policy_alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004190 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004191
4192exit:
4193 mbedtls_free( first_export );
4194 mbedtls_free( second_export );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004195 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004196 mbedtls_psa_crypto_free();
4197}
4198/* END_CASE */