blob: f90a7b3ae63774d2ec598b85d978c3da02599787 [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 Peskine969c5d62019-01-16 15:53:06 +0100408 psa_key_handle_t handle )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100409{
410 psa_key_type_t private_key_type;
411 psa_key_type_t public_key_type;
412 size_t key_bits;
413 uint8_t *public_key = NULL;
414 size_t public_key_length;
415 /* Return UNKNOWN_ERROR if something other than the final call to
416 * psa_key_agreement fails. This isn't fully satisfactory, but it's
417 * good enough: callers will report it as a failed test anyway. */
418 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
419
Gilles Peskine8817f612018-12-18 00:18:46 +0100420 PSA_ASSERT( psa_get_key_information( handle,
421 &private_key_type,
422 &key_bits ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100423 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
424 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
425 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100426 PSA_ASSERT( psa_export_public_key( handle,
427 public_key, public_key_length,
428 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100429
Gilles Peskine969c5d62019-01-16 15:53:06 +0100430 status = psa_key_agreement( generator, PSA_KDF_STEP_SECRET, handle,
431 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100432exit:
433 mbedtls_free( public_key );
434 return( status );
435}
436
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100437static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200438 psa_key_usage_t usage,
439 psa_algorithm_t alg )
440{
441 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200442 unsigned char output[1];
443 int ok = 0;
444
445 if( usage & PSA_KEY_USAGE_DERIVE )
446 {
447 /* We need two keys to exercise key agreement. Exercise the
448 * private key against its own public key. */
Gilles Peskine969c5d62019-01-16 15:53:06 +0100449 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
450 PSA_ASSERT( key_agreement_with_self( &generator, handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100451 PSA_ASSERT( psa_generator_read( &generator,
452 output,
453 sizeof( output ) ) );
454 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200455 }
456 ok = 1;
457
458exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200459 return( ok );
460}
461
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200462static int is_oid_of_key_type( psa_key_type_t type,
463 const uint8_t *oid, size_t oid_length )
464{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200465 const uint8_t *expected_oid = NULL;
466 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200467#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200468 if( PSA_KEY_TYPE_IS_RSA( type ) )
469 {
470 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
471 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
472 }
473 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200474#endif /* MBEDTLS_RSA_C */
475#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200476 if( PSA_KEY_TYPE_IS_ECC( type ) )
477 {
478 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
479 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
480 }
481 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200482#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200483 {
484 char message[40];
485 mbedtls_snprintf( message, sizeof( message ),
486 "OID not known for key type=0x%08lx",
487 (unsigned long) type );
488 test_fail( message, __LINE__, __FILE__ );
489 return( 0 );
490 }
491
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200492 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200493 return( 1 );
494
495exit:
496 return( 0 );
497}
498
499static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
500 size_t min_bits, size_t max_bits,
501 int must_be_odd )
502{
503 size_t len;
504 size_t actual_bits;
505 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100506 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100507 MBEDTLS_ASN1_INTEGER ),
508 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200509 /* Tolerate a slight departure from DER encoding:
510 * - 0 may be represented by an empty string or a 1-byte string.
511 * - The sign bit may be used as a value bit. */
512 if( ( len == 1 && ( *p )[0] == 0 ) ||
513 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
514 {
515 ++( *p );
516 --len;
517 }
518 if( min_bits == 0 && len == 0 )
519 return( 1 );
520 msb = ( *p )[0];
521 TEST_ASSERT( msb != 0 );
522 actual_bits = 8 * ( len - 1 );
523 while( msb != 0 )
524 {
525 msb >>= 1;
526 ++actual_bits;
527 }
528 TEST_ASSERT( actual_bits >= min_bits );
529 TEST_ASSERT( actual_bits <= max_bits );
530 if( must_be_odd )
531 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
532 *p += len;
533 return( 1 );
534exit:
535 return( 0 );
536}
537
538static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
539 size_t *len,
540 unsigned char n, unsigned char tag )
541{
542 int ret;
543 ret = mbedtls_asn1_get_tag( p, end, len,
544 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
545 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
546 if( ret != 0 )
547 return( ret );
548 end = *p + *len;
549 ret = mbedtls_asn1_get_tag( p, end, len, tag );
550 if( ret != 0 )
551 return( ret );
552 if( *p + *len != end )
553 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
554 return( 0 );
555}
556
557static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
558 uint8_t *exported, size_t exported_length )
559{
560 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100561 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200562 else
563 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200564
565#if defined(MBEDTLS_DES_C)
566 if( type == PSA_KEY_TYPE_DES )
567 {
568 /* Check the parity bits. */
569 unsigned i;
570 for( i = 0; i < bits / 8; i++ )
571 {
572 unsigned bit_count = 0;
573 unsigned m;
574 for( m = 1; m <= 0x100; m <<= 1 )
575 {
576 if( exported[i] & m )
577 ++bit_count;
578 }
579 TEST_ASSERT( bit_count % 2 != 0 );
580 }
581 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200582 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200583#endif
584
585#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
586 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
587 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200588 uint8_t *p = exported;
589 uint8_t *end = exported + exported_length;
590 size_t len;
591 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200592 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200593 * modulus INTEGER, -- n
594 * publicExponent INTEGER, -- e
595 * privateExponent INTEGER, -- d
596 * prime1 INTEGER, -- p
597 * prime2 INTEGER, -- q
598 * exponent1 INTEGER, -- d mod (p-1)
599 * exponent2 INTEGER, -- d mod (q-1)
600 * coefficient INTEGER, -- (inverse of q) mod p
601 * }
602 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100603 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
604 MBEDTLS_ASN1_SEQUENCE |
605 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
606 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200607 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
608 goto exit;
609 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
610 goto exit;
611 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
612 goto exit;
613 /* Require d to be at least half the size of n. */
614 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
615 goto exit;
616 /* Require p and q to be at most half the size of n, rounded up. */
617 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
618 goto exit;
619 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
620 goto exit;
621 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
622 goto exit;
623 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
624 goto exit;
625 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
626 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100627 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100628 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200629 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200630#endif /* MBEDTLS_RSA_C */
631
632#if defined(MBEDTLS_ECP_C)
633 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
634 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100635 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100636 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100637 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200638 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200639#endif /* MBEDTLS_ECP_C */
640
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200641 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
642 {
643 uint8_t *p = exported;
644 uint8_t *end = exported + exported_length;
645 size_t len;
646 mbedtls_asn1_buf alg;
647 mbedtls_asn1_buf params;
648 mbedtls_asn1_bitstring bitstring;
649 /* SubjectPublicKeyInfo ::= SEQUENCE {
650 * algorithm AlgorithmIdentifier,
651 * subjectPublicKey BIT STRING }
652 * AlgorithmIdentifier ::= SEQUENCE {
653 * algorithm OBJECT IDENTIFIER,
654 * parameters ANY DEFINED BY algorithm OPTIONAL }
655 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100656 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
657 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100658 MBEDTLS_ASN1_CONSTRUCTED ),
659 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100660 TEST_EQUAL( p + len, end );
661 TEST_EQUAL( mbedtls_asn1_get_alg( &p, end, &alg, &params ), 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200662 if( ! is_oid_of_key_type( type, alg.p, alg.len ) )
663 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100664 TEST_EQUAL( mbedtls_asn1_get_bitstring( &p, end, &bitstring ), 0 );
665 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200666 p = bitstring.p;
667#if defined(MBEDTLS_RSA_C)
668 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
669 {
670 /* RSAPublicKey ::= SEQUENCE {
671 * modulus INTEGER, -- n
672 * publicExponent INTEGER } -- e
673 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100674 TEST_EQUAL( bitstring.unused_bits, 0 );
675 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
676 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100677 MBEDTLS_ASN1_CONSTRUCTED ),
678 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100679 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200680 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
681 goto exit;
682 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
683 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100684 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200685 }
686 else
687#endif /* MBEDTLS_RSA_C */
688#if defined(MBEDTLS_ECP_C)
689 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
690 {
691 /* ECPoint ::= ...
Gilles Peskinec6290c02018-08-13 17:24:59 +0200692 * -- first 8 bits: 0x04 (uncompressed representation);
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200693 * -- then x_P as an n-bit string, big endian;
694 * -- then y_P as a n-bit string, big endian,
695 * -- where n is the order of the curve.
696 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100697 TEST_EQUAL( bitstring.unused_bits, 0 );
698 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
699 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200700 }
701 else
702#endif /* MBEDTLS_ECP_C */
703 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100704 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200705 mbedtls_snprintf( message, sizeof( message ),
706 "No sanity check for public key type=0x%08lx",
707 (unsigned long) type );
708 test_fail( message, __LINE__, __FILE__ );
709 return( 0 );
710 }
711 }
712 else
713
714 {
715 /* No sanity checks for other types */
716 }
717
718 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200719
720exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200721 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200722}
723
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100724static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200725 psa_key_usage_t usage )
726{
727 psa_key_type_t type;
728 size_t bits;
729 uint8_t *exported = NULL;
730 size_t exported_size = 0;
731 size_t exported_length = 0;
732 int ok = 0;
733
Gilles Peskine8817f612018-12-18 00:18:46 +0100734 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200735
736 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
737 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200738 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100739 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
740 PSA_ERROR_NOT_PERMITTED );
Gilles Peskined14664a2018-08-10 19:07:32 +0200741 return( 1 );
742 }
743
Gilles Peskined14664a2018-08-10 19:07:32 +0200744 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200745 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200746
Gilles Peskine8817f612018-12-18 00:18:46 +0100747 PSA_ASSERT( psa_export_key( handle,
748 exported, exported_size,
749 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200750 ok = exported_key_sanity_check( type, bits, exported, exported_length );
751
752exit:
753 mbedtls_free( exported );
754 return( ok );
755}
756
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100757static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200758{
759 psa_key_type_t type;
760 psa_key_type_t public_type;
761 size_t bits;
762 uint8_t *exported = NULL;
763 size_t exported_size = 0;
764 size_t exported_length = 0;
765 int ok = 0;
766
Gilles Peskine8817f612018-12-18 00:18:46 +0100767 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200768 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
769 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100770 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100771 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200772 return( 1 );
773 }
774
775 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
776 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200777 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200778
Gilles Peskine8817f612018-12-18 00:18:46 +0100779 PSA_ASSERT( psa_export_public_key( handle,
780 exported, exported_size,
781 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200782 ok = exported_key_sanity_check( public_type, bits,
783 exported, exported_length );
784
785exit:
786 mbedtls_free( exported );
787 return( ok );
788}
789
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100790static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +0200791 psa_key_usage_t usage,
792 psa_algorithm_t alg )
793{
794 int ok;
795 if( alg == 0 )
796 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
797 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100798 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200799 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100800 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200801 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100802 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200803 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100804 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200805 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100806 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200807 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100808 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200809 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100810 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200811 else
812 {
813 char message[40];
814 mbedtls_snprintf( message, sizeof( message ),
815 "No code to exercise alg=0x%08lx",
816 (unsigned long) alg );
817 test_fail( message, __LINE__, __FILE__ );
818 ok = 0;
819 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200820
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100821 ok = ok && exercise_export_key( handle, usage );
822 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +0200823
Gilles Peskine02b75072018-07-01 22:31:34 +0200824 return( ok );
825}
826
Gilles Peskine10df3412018-10-25 22:35:43 +0200827static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
828 psa_algorithm_t alg )
829{
830 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
831 {
832 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
833 PSA_KEY_USAGE_VERIFY :
834 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
835 }
836 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
837 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
838 {
839 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
840 PSA_KEY_USAGE_ENCRYPT :
841 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
842 }
843 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
844 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
845 {
846 return( PSA_KEY_USAGE_DERIVE );
847 }
848 else
849 {
850 return( 0 );
851 }
852
853}
Darryl Green0c6575a2018-11-07 16:05:30 +0000854
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100855/* An overapproximation of the amount of storage needed for a key of the
856 * given type and with the given content. The API doesn't make it easy
857 * to find a good value for the size. The current implementation doesn't
858 * care about the value anyway. */
859#define KEY_BITS_FROM_DATA( type, data ) \
860 ( data )->len
861
Darryl Green0c6575a2018-11-07 16:05:30 +0000862typedef enum {
863 IMPORT_KEY = 0,
864 GENERATE_KEY = 1,
865 DERIVE_KEY = 2
866} generate_method;
867
Gilles Peskinee59236f2018-01-27 23:32:46 +0100868/* END_HEADER */
869
870/* BEGIN_DEPENDENCIES
871 * depends_on:MBEDTLS_PSA_CRYPTO_C
872 * END_DEPENDENCIES
873 */
874
875/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200876void static_checks( )
877{
878 size_t max_truncated_mac_size =
879 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
880
881 /* Check that the length for a truncated MAC always fits in the algorithm
882 * encoding. The shifted mask is the maximum truncated value. The
883 * untruncated algorithm may be one byte larger. */
884 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
885}
886/* END_CASE */
887
888/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200889void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100890{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100891 psa_key_handle_t handle = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200892 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100893 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100894
Gilles Peskine8817f612018-12-18 00:18:46 +0100895 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100896
Gilles Peskine8817f612018-12-18 00:18:46 +0100897 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
898 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100899 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100900 TEST_EQUAL( status, expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100901 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100902 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100903
904exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100905 mbedtls_psa_crypto_free( );
906}
907/* END_CASE */
908
909/* BEGIN_CASE */
Gilles Peskinea4261682018-12-03 11:34:01 +0100910void import_twice( int alg_arg, int usage_arg,
911 int type1_arg, data_t *data1,
912 int expected_import1_status_arg,
913 int type2_arg, data_t *data2,
914 int expected_import2_status_arg )
915{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100916 psa_key_handle_t handle = 0;
Gilles Peskinea4261682018-12-03 11:34:01 +0100917 psa_algorithm_t alg = alg_arg;
918 psa_key_usage_t usage = usage_arg;
919 psa_key_type_t type1 = type1_arg;
920 psa_status_t expected_import1_status = expected_import1_status_arg;
921 psa_key_type_t type2 = type2_arg;
922 psa_status_t expected_import2_status = expected_import2_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +0000923 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea4261682018-12-03 11:34:01 +0100924 psa_status_t status;
925
Gilles Peskine8817f612018-12-18 00:18:46 +0100926 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100927
Gilles Peskine8817f612018-12-18 00:18:46 +0100928 PSA_ASSERT( psa_allocate_key( type1,
929 MAX( KEY_BITS_FROM_DATA( type1, data1 ),
930 KEY_BITS_FROM_DATA( type2, data2 ) ),
931 &handle ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100932 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100933 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100934
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100935 status = psa_import_key( handle, type1, data1->x, data1->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100936 TEST_EQUAL( status, expected_import1_status );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100937 status = psa_import_key( handle, type2, data2->x, data2->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100938 TEST_EQUAL( status, expected_import2_status );
Gilles Peskinea4261682018-12-03 11:34:01 +0100939
940 if( expected_import1_status == PSA_SUCCESS ||
941 expected_import2_status == PSA_SUCCESS )
942 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100943 TEST_ASSERT( exercise_key( handle, usage, alg ) );
Gilles Peskinea4261682018-12-03 11:34:01 +0100944 }
945
946exit:
947 mbedtls_psa_crypto_free( );
948}
949/* END_CASE */
950
951/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200952void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
953{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100954 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200955 size_t bits = bits_arg;
956 psa_status_t expected_status = expected_status_arg;
957 psa_status_t status;
958 psa_key_type_t type =
959 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
960 size_t buffer_size = /* Slight overapproximations */
961 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200962 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200963 unsigned char *p;
964 int ret;
965 size_t length;
966
Gilles Peskine8817f612018-12-18 00:18:46 +0100967 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200968 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200969
970 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
971 bits, keypair ) ) >= 0 );
972 length = ret;
973
974 /* Try importing the key */
Gilles Peskine8817f612018-12-18 00:18:46 +0100975 PSA_ASSERT( psa_allocate_key( type, bits, &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100976 status = psa_import_key( handle, type, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100977 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200978 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100979 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200980
981exit:
982 mbedtls_free( buffer );
983 mbedtls_psa_crypto_free( );
984}
985/* END_CASE */
986
987/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300988void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300989 int type_arg,
990 int alg_arg,
991 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100992 int expected_bits,
993 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200994 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100995 int canonical_input )
996{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100997 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100998 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200999 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001000 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001001 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001002 unsigned char *exported = NULL;
1003 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001004 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001005 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001006 size_t reexported_length;
1007 psa_key_type_t got_type;
1008 size_t got_bits;
Jaeden Amero70261c52019-01-04 11:47:20 +00001009 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001010
Moran Pekercb088e72018-07-17 17:36:59 +03001011 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001012 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001013 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001014 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001015 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001016
Gilles Peskine8817f612018-12-18 00:18:46 +01001017 PSA_ASSERT( psa_allocate_key( type, expected_bits, &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001018 psa_key_policy_set_usage( &policy, usage_arg, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001019 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001020
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001021 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1022 PSA_ERROR_EMPTY_SLOT );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001023
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001024 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001025 PSA_ASSERT( psa_import_key( handle, type,
1026 data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001027
1028 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001029 PSA_ASSERT( psa_get_key_information( handle,
1030 &got_type,
1031 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001032 TEST_EQUAL( got_type, type );
1033 TEST_EQUAL( got_bits, (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001034
1035 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001036 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001037 exported, export_size,
1038 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001039 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001040
1041 /* The exported length must be set by psa_export_key() to a value between 0
1042 * and export_size. On errors, the exported length must be 0. */
1043 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1044 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1045 TEST_ASSERT( exported_length <= export_size );
1046
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001047 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001048 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001049 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001050 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001051 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001052 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001053 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001054
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001055 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001056 goto exit;
1057
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001058 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001059 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001060 else
1061 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001062 psa_key_handle_t handle2;
Gilles Peskine8817f612018-12-18 00:18:46 +01001063 PSA_ASSERT( psa_allocate_key( type, expected_bits, &handle2 ) );
1064 PSA_ASSERT( psa_set_key_policy( handle2, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001065
Gilles Peskine8817f612018-12-18 00:18:46 +01001066 PSA_ASSERT( psa_import_key( handle2, type,
1067 exported,
1068 exported_length ) );
1069 PSA_ASSERT( psa_export_key( handle2,
1070 reexported,
1071 export_size,
1072 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001073 ASSERT_COMPARE( exported, exported_length,
1074 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001075 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001076 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001077 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001078
1079destroy:
1080 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001081 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001082 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1083 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001084
1085exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001086 mbedtls_free( exported );
1087 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001088 mbedtls_psa_crypto_free( );
1089}
1090/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001091
Moran Pekerf709f4a2018-06-06 17:26:04 +03001092/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001093void import_key_nonempty_slot( )
1094{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001095 psa_key_handle_t handle = 0;
Moran Peker28a38e62018-11-07 16:18:24 +02001096 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1097 psa_status_t status;
1098 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
Gilles Peskine8817f612018-12-18 00:18:46 +01001099 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001100
Gilles Peskine8817f612018-12-18 00:18:46 +01001101 PSA_ASSERT( psa_allocate_key( type, PSA_BYTES_TO_BITS( sizeof( data ) ),
1102 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001103
Moran Peker28a38e62018-11-07 16:18:24 +02001104 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001105 PSA_ASSERT( psa_import_key( handle, type,
1106 data, sizeof( data ) ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001107
1108 /* Import the key again */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001109 status = psa_import_key( handle, type, data, sizeof( data ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001110 TEST_EQUAL( status, PSA_ERROR_OCCUPIED_SLOT );
Moran Peker28a38e62018-11-07 16:18:24 +02001111
1112exit:
1113 mbedtls_psa_crypto_free( );
1114}
1115/* END_CASE */
1116
1117/* BEGIN_CASE */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001118void export_invalid_handle( int handle, int expected_export_status_arg )
Moran Peker28a38e62018-11-07 16:18:24 +02001119{
1120 psa_status_t status;
1121 unsigned char *exported = NULL;
1122 size_t export_size = 0;
1123 size_t exported_length = INVALID_EXPORT_LENGTH;
1124 psa_status_t expected_export_status = expected_export_status_arg;
1125
Gilles Peskine8817f612018-12-18 00:18:46 +01001126 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001127
1128 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001129 status = psa_export_key( (psa_key_handle_t) handle,
Moran Peker28a38e62018-11-07 16:18:24 +02001130 exported, export_size,
1131 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001132 TEST_EQUAL( status, expected_export_status );
Moran Peker28a38e62018-11-07 16:18:24 +02001133
1134exit:
1135 mbedtls_psa_crypto_free( );
1136}
1137/* END_CASE */
1138
1139/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001140void export_with_no_key_activity( )
1141{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001142 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001143 psa_algorithm_t alg = PSA_ALG_CTR;
1144 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001145 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001146 unsigned char *exported = NULL;
1147 size_t export_size = 0;
1148 size_t exported_length = INVALID_EXPORT_LENGTH;
1149
Gilles Peskine8817f612018-12-18 00:18:46 +01001150 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001151
Gilles Peskine8817f612018-12-18 00:18:46 +01001152 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1153 &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001154 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001155 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001156
1157 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001158 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001159 exported, export_size,
1160 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001161 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001162
1163exit:
1164 mbedtls_psa_crypto_free( );
1165}
1166/* END_CASE */
1167
1168/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001169void cipher_with_no_key_activity( )
1170{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001171 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001172 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001173 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001174 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001175 int exercise_alg = PSA_ALG_CTR;
1176
Gilles Peskine8817f612018-12-18 00:18:46 +01001177 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001178
Gilles Peskine8817f612018-12-18 00:18:46 +01001179 PSA_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0,
1180 &handle ) );
Moran Pekerce500072018-11-07 16:20:07 +02001181 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001182 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerce500072018-11-07 16:20:07 +02001183
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001184 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001185 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001186
1187exit:
1188 psa_cipher_abort( &operation );
1189 mbedtls_psa_crypto_free( );
1190}
1191/* END_CASE */
1192
1193/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001194void export_after_import_failure( data_t *data, int type_arg,
1195 int expected_import_status_arg )
1196{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001197 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001198 psa_key_type_t type = type_arg;
1199 psa_status_t status;
1200 unsigned char *exported = NULL;
1201 size_t export_size = 0;
1202 psa_status_t expected_import_status = expected_import_status_arg;
1203 size_t exported_length = INVALID_EXPORT_LENGTH;
1204
Gilles Peskine8817f612018-12-18 00:18:46 +01001205 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001206
Gilles Peskine8817f612018-12-18 00:18:46 +01001207 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1208 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001209
Moran Peker34550092018-11-07 16:19:34 +02001210 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001211 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001212 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001213 TEST_EQUAL( status, expected_import_status );
Moran Peker34550092018-11-07 16:19:34 +02001214
1215 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001216 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001217 exported, export_size,
1218 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001219 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Peker34550092018-11-07 16:19:34 +02001220
1221exit:
1222 mbedtls_psa_crypto_free( );
1223}
1224/* END_CASE */
1225
1226/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001227void cipher_after_import_failure( data_t *data, int type_arg,
1228 int expected_import_status_arg )
1229{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001230 psa_key_handle_t handle = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001231 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001232 psa_key_type_t type = type_arg;
1233 psa_status_t status;
1234 psa_status_t expected_import_status = expected_import_status_arg;
1235 int exercise_alg = PSA_ALG_CTR;
1236
Gilles Peskine8817f612018-12-18 00:18:46 +01001237 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001238
Gilles Peskine8817f612018-12-18 00:18:46 +01001239 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1240 &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001241
Moran Pekerce500072018-11-07 16:20:07 +02001242 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001243 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001244 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001245 TEST_EQUAL( status, expected_import_status );
Moran Pekerce500072018-11-07 16:20:07 +02001246
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001247 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001248 TEST_EQUAL( status, PSA_ERROR_EMPTY_SLOT );
Moran Pekerce500072018-11-07 16:20:07 +02001249
1250exit:
1251 psa_cipher_abort( &operation );
1252 mbedtls_psa_crypto_free( );
1253}
1254/* END_CASE */
1255
1256/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001257void export_after_destroy_key( data_t *data, int type_arg )
1258{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001259 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001260 psa_key_type_t type = type_arg;
1261 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001262 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001263 psa_algorithm_t alg = PSA_ALG_CTR;
1264 unsigned char *exported = NULL;
1265 size_t export_size = 0;
1266 size_t exported_length = INVALID_EXPORT_LENGTH;
1267
Gilles Peskine8817f612018-12-18 00:18:46 +01001268 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001269
Gilles Peskine8817f612018-12-18 00:18:46 +01001270 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1271 &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001272 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001273 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001274 export_size = (ptrdiff_t) data->len;
1275 ASSERT_ALLOC( exported, export_size );
1276
1277 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001278 PSA_ASSERT( psa_import_key( handle, type,
1279 data->x, data->len ) );
Moran Peker34550092018-11-07 16:19:34 +02001280
Gilles Peskine8817f612018-12-18 00:18:46 +01001281 PSA_ASSERT( psa_export_key( handle, exported, export_size,
1282 &exported_length ) );
Moran Peker34550092018-11-07 16:19:34 +02001283
1284 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001285 PSA_ASSERT( psa_destroy_key( handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001286
1287 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001288 status = psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001289 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001290 TEST_EQUAL( status, PSA_ERROR_INVALID_HANDLE );
Moran Peker34550092018-11-07 16:19:34 +02001291
1292exit:
1293 mbedtls_free( exported );
1294 mbedtls_psa_crypto_free( );
1295}
1296/* END_CASE */
1297
1298/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001299void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001300 int type_arg,
1301 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001302 int export_size_delta,
1303 int expected_export_status_arg,
1304 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001305{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001306 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001307 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001308 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001309 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001310 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001311 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001312 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001313 size_t exported_length = INVALID_EXPORT_LENGTH;
Jaeden Amero70261c52019-01-04 11:47:20 +00001314 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001315
Gilles Peskine8817f612018-12-18 00:18:46 +01001316 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001317
Gilles Peskine8817f612018-12-18 00:18:46 +01001318 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1319 &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001320 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001321 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001322
1323 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001324 PSA_ASSERT( psa_import_key( handle, type,
1325 data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001326
Gilles Peskine49c25912018-10-29 15:15:31 +01001327 /* Export the public key */
1328 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001329 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001330 exported, export_size,
1331 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001332 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001333 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001334 {
1335 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1336 size_t bits;
Gilles Peskine8817f612018-12-18 00:18:46 +01001337 PSA_ASSERT( psa_get_key_information( handle, NULL, &bits ) );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001338 TEST_ASSERT( expected_public_key->len <=
1339 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001340 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1341 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001342 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001343
1344exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001345 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001346 psa_destroy_key( handle );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001347 mbedtls_psa_crypto_free( );
1348}
1349/* END_CASE */
1350
Gilles Peskine20035e32018-02-03 22:44:14 +01001351/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001352void import_and_exercise_key( data_t *data,
1353 int type_arg,
1354 int bits_arg,
1355 int alg_arg )
1356{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001357 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001358 psa_key_type_t type = type_arg;
1359 size_t bits = bits_arg;
1360 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001361 psa_key_usage_t usage = usage_to_exercise( type, alg );
Jaeden Amero70261c52019-01-04 11:47:20 +00001362 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001363 psa_key_type_t got_type;
1364 size_t got_bits;
1365 psa_status_t status;
1366
Gilles Peskine8817f612018-12-18 00:18:46 +01001367 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001368
Gilles Peskine8817f612018-12-18 00:18:46 +01001369 PSA_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ),
1370 &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001371 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001372 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001373
1374 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001375 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01001376 PSA_ASSERT( status );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001377
1378 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001379 PSA_ASSERT( psa_get_key_information( handle,
1380 &got_type,
1381 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001382 TEST_EQUAL( got_type, type );
1383 TEST_EQUAL( got_bits, bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001384
1385 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001386 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001387 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001388
1389exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001390 psa_destroy_key( handle );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001391 mbedtls_psa_crypto_free( );
1392}
1393/* END_CASE */
1394
1395/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001396void key_policy( int usage_arg, int alg_arg )
1397{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001398 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001399 psa_algorithm_t alg = alg_arg;
1400 psa_key_usage_t usage = usage_arg;
1401 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1402 unsigned char key[32] = {0};
Jaeden Amero70261c52019-01-04 11:47:20 +00001403 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
1404 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001405
1406 memset( key, 0x2a, sizeof( key ) );
1407
Gilles Peskine8817f612018-12-18 00:18:46 +01001408 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001409
Gilles Peskine8817f612018-12-18 00:18:46 +01001410 PSA_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( sizeof( key ) ),
1411 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001412 psa_key_policy_set_usage( &policy_set, usage, alg );
1413
Gilles Peskinefe11b722018-12-18 00:24:04 +01001414 TEST_EQUAL( psa_key_policy_get_usage( &policy_set ), usage );
1415 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_set ), alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001416 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001417
Gilles Peskine8817f612018-12-18 00:18:46 +01001418 PSA_ASSERT( psa_import_key( handle, key_type,
1419 key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001420
Gilles Peskine8817f612018-12-18 00:18:46 +01001421 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001422
Gilles Peskinefe11b722018-12-18 00:24:04 +01001423 TEST_EQUAL( policy_get.usage, policy_set.usage );
1424 TEST_EQUAL( policy_get.alg, policy_set.alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001425
1426exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001427 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001428 mbedtls_psa_crypto_free( );
1429}
1430/* END_CASE */
1431
1432/* BEGIN_CASE */
Jaeden Amero70261c52019-01-04 11:47:20 +00001433void key_policy_init( )
1434{
1435 /* Test each valid way of initializing the object, except for `= {0}`, as
1436 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1437 * though it's OK by the C standard. We could test for this, but we'd need
1438 * to supress the Clang warning for the test. */
1439 psa_key_policy_t func = psa_key_policy_init( );
1440 psa_key_policy_t init = PSA_KEY_POLICY_INIT;
1441 psa_key_policy_t zero;
1442
1443 memset( &zero, 0, sizeof( zero ) );
1444
1445 /* Although not technically guaranteed by the C standard nor the PSA Crypto
1446 * specification, we test that all valid ways of initializing the object
1447 * have the same bit pattern. This is a stronger requirement that may not
1448 * be valid on all platforms or PSA Crypto implementations, but implies the
1449 * weaker actual requirement is met: that a freshly initialized object, no
1450 * matter how it was initialized, acts the same as any other valid
1451 * initialization. */
1452 TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1453 TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1454}
1455/* END_CASE */
1456
1457/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001458void mac_key_policy( int policy_usage,
1459 int policy_alg,
1460 int key_type,
1461 data_t *key_data,
1462 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001463{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001464 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001465 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001466 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001467 psa_status_t status;
1468 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001469
Gilles Peskine8817f612018-12-18 00:18:46 +01001470 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001471
Gilles Peskine8817f612018-12-18 00:18:46 +01001472 PSA_ASSERT( psa_allocate_key( key_type,
1473 KEY_BITS_FROM_DATA( key_type, key_data ),
1474 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001475 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001476 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001477
Gilles Peskine8817f612018-12-18 00:18:46 +01001478 PSA_ASSERT( psa_import_key( handle, key_type,
1479 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001480
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001481 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001482 if( policy_alg == exercise_alg &&
1483 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001484 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001485 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001486 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001487 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001488
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001489 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001490 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001491 if( policy_alg == exercise_alg &&
1492 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001493 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001494 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001495 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001496
1497exit:
1498 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001499 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001500 mbedtls_psa_crypto_free( );
1501}
1502/* END_CASE */
1503
1504/* BEGIN_CASE */
1505void cipher_key_policy( int policy_usage,
1506 int policy_alg,
1507 int key_type,
1508 data_t *key_data,
1509 int exercise_alg )
1510{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001511 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001512 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001513 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001514 psa_status_t status;
1515
Gilles Peskine8817f612018-12-18 00:18:46 +01001516 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001517
Gilles Peskine8817f612018-12-18 00:18:46 +01001518 PSA_ASSERT( psa_allocate_key( key_type,
1519 KEY_BITS_FROM_DATA( key_type, key_data ),
1520 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001521 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001522 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001523
Gilles Peskine8817f612018-12-18 00:18:46 +01001524 PSA_ASSERT( psa_import_key( handle, key_type,
1525 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001526
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001527 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001528 if( policy_alg == exercise_alg &&
1529 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001530 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001531 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001532 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001533 psa_cipher_abort( &operation );
1534
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001535 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001536 if( policy_alg == exercise_alg &&
1537 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001538 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001539 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001540 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001541
1542exit:
1543 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001544 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001545 mbedtls_psa_crypto_free( );
1546}
1547/* END_CASE */
1548
1549/* BEGIN_CASE */
1550void aead_key_policy( int policy_usage,
1551 int policy_alg,
1552 int key_type,
1553 data_t *key_data,
1554 int nonce_length_arg,
1555 int tag_length_arg,
1556 int exercise_alg )
1557{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001558 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001559 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001560 psa_status_t status;
1561 unsigned char nonce[16] = {0};
1562 size_t nonce_length = nonce_length_arg;
1563 unsigned char tag[16];
1564 size_t tag_length = tag_length_arg;
1565 size_t output_length;
1566
1567 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1568 TEST_ASSERT( tag_length <= sizeof( tag ) );
1569
Gilles Peskine8817f612018-12-18 00:18:46 +01001570 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001571
Gilles Peskine8817f612018-12-18 00:18:46 +01001572 PSA_ASSERT( psa_allocate_key( key_type,
1573 KEY_BITS_FROM_DATA( key_type, key_data ),
1574 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001575 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001576 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001577
Gilles Peskine8817f612018-12-18 00:18:46 +01001578 PSA_ASSERT( psa_import_key( handle, key_type,
1579 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001580
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001581 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001582 nonce, nonce_length,
1583 NULL, 0,
1584 NULL, 0,
1585 tag, tag_length,
1586 &output_length );
1587 if( policy_alg == exercise_alg &&
1588 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001589 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001590 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001591 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001592
1593 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001594 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001595 nonce, nonce_length,
1596 NULL, 0,
1597 tag, tag_length,
1598 NULL, 0,
1599 &output_length );
1600 if( policy_alg == exercise_alg &&
1601 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001602 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001603 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001604 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001605
1606exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001607 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001608 mbedtls_psa_crypto_free( );
1609}
1610/* END_CASE */
1611
1612/* BEGIN_CASE */
1613void asymmetric_encryption_key_policy( int policy_usage,
1614 int policy_alg,
1615 int key_type,
1616 data_t *key_data,
1617 int exercise_alg )
1618{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001619 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001620 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001621 psa_status_t status;
1622 size_t key_bits;
1623 size_t buffer_length;
1624 unsigned char *buffer = NULL;
1625 size_t output_length;
1626
Gilles Peskine8817f612018-12-18 00:18:46 +01001627 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001628
Gilles Peskine8817f612018-12-18 00:18:46 +01001629 PSA_ASSERT( psa_allocate_key( key_type,
1630 KEY_BITS_FROM_DATA( key_type, key_data ),
1631 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001632 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001633 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001634
Gilles Peskine8817f612018-12-18 00:18:46 +01001635 PSA_ASSERT( psa_import_key( handle, key_type,
1636 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001637
Gilles Peskine8817f612018-12-18 00:18:46 +01001638 PSA_ASSERT( psa_get_key_information( handle,
1639 NULL,
1640 &key_bits ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001641 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1642 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001643 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001644
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001645 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001646 NULL, 0,
1647 NULL, 0,
1648 buffer, buffer_length,
1649 &output_length );
1650 if( policy_alg == exercise_alg &&
1651 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001652 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001653 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001654 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001655
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001656 if( buffer_length != 0 )
1657 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001658 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001659 buffer, buffer_length,
1660 NULL, 0,
1661 buffer, buffer_length,
1662 &output_length );
1663 if( policy_alg == exercise_alg &&
1664 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001665 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001666 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001667 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001668
1669exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001670 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001671 mbedtls_psa_crypto_free( );
1672 mbedtls_free( buffer );
1673}
1674/* END_CASE */
1675
1676/* BEGIN_CASE */
1677void asymmetric_signature_key_policy( int policy_usage,
1678 int policy_alg,
1679 int key_type,
1680 data_t *key_data,
1681 int exercise_alg )
1682{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001683 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001684 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001685 psa_status_t status;
1686 unsigned char payload[16] = {1};
1687 size_t payload_length = sizeof( payload );
1688 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1689 size_t signature_length;
1690
Gilles Peskine8817f612018-12-18 00:18:46 +01001691 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001692
Gilles Peskine8817f612018-12-18 00:18:46 +01001693 PSA_ASSERT( psa_allocate_key( key_type,
1694 KEY_BITS_FROM_DATA( key_type, key_data ),
1695 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001696 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001697 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001698
Gilles Peskine8817f612018-12-18 00:18:46 +01001699 PSA_ASSERT( psa_import_key( handle, key_type,
1700 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001701
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001702 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001703 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001704 signature, sizeof( signature ),
1705 &signature_length );
1706 if( policy_alg == exercise_alg &&
1707 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001708 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001709 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001710 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001711
1712 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001713 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001714 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001715 signature, sizeof( signature ) );
1716 if( policy_alg == exercise_alg &&
1717 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001718 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001719 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001720 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001721
1722exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001723 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001724 mbedtls_psa_crypto_free( );
1725}
1726/* END_CASE */
1727
1728/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001729void derive_key_policy( int policy_usage,
1730 int policy_alg,
1731 int key_type,
1732 data_t *key_data,
1733 int exercise_alg )
1734{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001735 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001736 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001737 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1738 psa_status_t status;
1739
Gilles Peskine8817f612018-12-18 00:18:46 +01001740 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001741
Gilles Peskine8817f612018-12-18 00:18:46 +01001742 PSA_ASSERT( psa_allocate_key( key_type,
1743 KEY_BITS_FROM_DATA( key_type, key_data ),
1744 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001745 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001746 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001747
Gilles Peskine8817f612018-12-18 00:18:46 +01001748 PSA_ASSERT( psa_import_key( handle, key_type,
1749 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001750
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001751 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001752 exercise_alg,
1753 NULL, 0,
1754 NULL, 0,
1755 1 );
1756 if( policy_alg == exercise_alg &&
1757 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001758 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001759 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001760 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001761
1762exit:
1763 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001764 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001765 mbedtls_psa_crypto_free( );
1766}
1767/* END_CASE */
1768
1769/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001770void agreement_key_policy( int policy_usage,
1771 int policy_alg,
1772 int key_type_arg,
1773 data_t *key_data,
1774 int exercise_alg )
1775{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001776 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001777 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001778 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001779 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1780 psa_status_t status;
1781
Gilles Peskine8817f612018-12-18 00:18:46 +01001782 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001783
Gilles Peskine8817f612018-12-18 00:18:46 +01001784 PSA_ASSERT( psa_allocate_key( key_type,
1785 KEY_BITS_FROM_DATA( key_type, key_data ),
1786 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001787 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001788 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001789
Gilles Peskine8817f612018-12-18 00:18:46 +01001790 PSA_ASSERT( psa_import_key( handle, key_type,
1791 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001792
Gilles Peskine969c5d62019-01-16 15:53:06 +01001793 PSA_ASSERT( psa_key_derivation_setup( &generator, exercise_alg ) );
1794 status = key_agreement_with_self( &generator, handle );
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 Peskine969c5d62019-01-16 15:53:06 +01003851 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
3852 TEST_EQUAL( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003853 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003854 peer_key_data->x, peer_key_data->len ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003855 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 Peskine969c5d62019-01-16 15:53:06 +01003890 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
3891 PSA_ASSERT( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskine8817f612018-12-18 00:18:46 +01003892 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003893 peer_key_data->x, peer_key_data->len ) );
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 Peskine969c5d62019-01-16 15:53:06 +01003947 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
3948 PSA_ASSERT( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskine8817f612018-12-18 00:18:46 +01003949 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003950 peer_key_data->x, peer_key_data->len ) );
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 */